-
Notifications
You must be signed in to change notification settings - Fork 2
/
daq_device_CAENdrs_std.cc
483 lines (369 loc) · 10.7 KB
/
daq_device_CAENdrs_std.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <iostream>
#include <daq_device_CAENdrs_std.h>
#include <string.h>
#include <CAENDigitizer.h>
#include <CAENDigitizerType.h>
#define VME_INTERRUPT_LEVEL 1
#define VME_INTERRUPT_STATUS_ID 0xAAAA
#define INTERRUPT_MODE CAEN_DGTZ_IRQ_MODE_ROAK
using namespace std;
daq_device_CAENdrs_std::daq_device_CAENdrs_std(const int eventtype
, const int subeventid
, const int linknumber
, const int nodenumber
, const int trigger // do I give the system trigger?
, const int speed
, const int delay
, const int endpulse)
{
m_eventType = eventtype;
m_subeventid = subeventid;
_linknumber = linknumber;
_nodenumber = nodenumber;
_endpulse = endpulse;
handle = 0;
_Event742 = 0;
int node = 0;
_warning = 0;
cout << "*************** opening Digitizer" << endl;
_broken = CAEN_DGTZ_OpenDigitizer( CAEN_DGTZ_OpticalLink, _linknumber , _nodenumber, 0 ,&handle);
cout << "*************** " << _broken << endl;
_broken = CAEN_DGTZ_Reset(handle);
if ( _broken )
{
cout << " Error in CAEN_DGTZ_OpenDigitizer " << _broken << endl;
return;
}
_trigger_handler=0;
if (trigger) _trigger_handler=1;
switch (speed)
{
case 2:
_speed = CAEN_DGTZ_DRS4_1GHz;
break;
case 1:
_speed = CAEN_DGTZ_DRS4_2_5GHz;
break;
case 0:
_speed = CAEN_DGTZ_DRS4_5GHz;
break;
default:
_speed = CAEN_DGTZ_DRS4_1GHz;
}
_delay = delay;
if ( _trigger_handler )
{
cout << __LINE__ << " " << __FILE__ << " registering triggerhandler " << endl;
_th = new CAENdrsTriggerHandler (handle, m_eventType);
registerTriggerHandler ( _th);
}
else
{
_th = 0;
}
}
daq_device_CAENdrs_std::~daq_device_CAENdrs_std()
{
if (_th)
{
clearTriggerHandler();
delete _th;
_th = 0;
}
if ( _Event742)
{
CAEN_DGTZ_FreeEvent(handle, (void**)&_Event742);
}
CAEN_DGTZ_CloseDigitizer(handle);
}
int daq_device_CAENdrs_std::init()
{
if ( _broken )
{
return 0; // we had a catastrophic failure
}
// set one-event readout
_broken = CAEN_DGTZ_SetMaxNumEventsBLT(handle, 1);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// enable adding the trigger channels to teh readout
_broken = CAEN_DGTZ_SetFastTriggerDigitizing(handle,CAEN_DGTZ_ENABLE);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// no trigger front panel output
_broken = CAEN_DGTZ_SetFastTriggerMode(handle,CAEN_DGTZ_TRGMODE_ACQ_ONLY);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// set nim levels for triggers
_broken = CAEN_DGTZ_SetIOLevel(handle, CAEN_DGTZ_IOLevel_NIM);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
if ( _delay < 0 )
{
cout << __FILE__ << " " << __LINE__ << " warning: delay parameter out of range, allowed is 0-100 : " << _delay << endl;
_delay = 0;
_warning = 1;
}
if ( _delay >100 )
{
cout << __FILE__ << " " << __LINE__ << " warning: delay parameter out of range, allowed is 0-100 : " << _delay << endl;
_delay = 100;
_warning = 1;
}
// extra delay after stop
_broken = CAEN_DGTZ_SetPostTriggerSize(handle,_delay);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// configure interrupts and trigger
_broken |= CAEN_DGTZ_SetInterruptConfig( handle, CAEN_DGTZ_ENABLE,
VME_INTERRUPT_LEVEL, VME_INTERRUPT_STATUS_ID,
1, INTERRUPT_MODE);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
_broken = CAEN_DGTZ_SetAcquisitionMode(handle, CAEN_DGTZ_SW_CONTROLLED);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
_broken = CAEN_DGTZ_SetExtTriggerInputMode(handle, CAEN_DGTZ_TRGMODE_ACQ_ONLY);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// enable all 4 groups
_broken = CAEN_DGTZ_SetGroupEnableMask(handle, 0xf);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// set frequency and correction
_broken = CAEN_DGTZ_SetDRS4SamplingFrequency(handle, (CAEN_DGTZ_DRS4Frequency_t) _speed) ;
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
_broken = CAEN_DGTZ_LoadDRS4CorrectionData(handle, (CAEN_DGTZ_DRS4Frequency_t) _speed);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
_broken = CAEN_DGTZ_EnableDRS4Correction(handle);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
// set DC offsets
unsigned int i;
for ( i = 0; i < 32; i++)
{
_broken = CAEN_DGTZ_SetChannelDCOffset(handle,i, 0x8f00);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
}
if ( _Event742 )
{
_broken = CAEN_DGTZ_AllocateEvent(handle, (void**)&_Event742);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
}
// and we trigger rearm with our event type so it takes effect
rearm (m_eventType);
_broken = CAEN_DGTZ_SWStartAcquisition(handle);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
return 0;
}
return 0;
}
// the put_data function
int daq_device_CAENdrs_std::put_data(const int etype, int * adr, const int length )
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
if (etype != m_eventType ) // not our id
{
return 0;
}
if ( length < max_length(etype) )
{
// cout << __LINE__ << " " << __FILE__ << " length " << length <<endl;
return 0;
}
int len = 0;
sevt = (subevtdata_ptr) adr;
// set the initial subevent length
sevt->sub_length = SEVTHEADERLENGTH;
// update id's etc
sevt->sub_id = m_subeventid;
sevt->sub_type=4;
sevt->sub_decoding = 85;
sevt->reserved[0] = 0;
sevt->reserved[1] = 0;
uint32_t buffersize = 0;
while ( buffersize == 0)
{
// Read data from the board
_broken = CAEN_DGTZ_ReadData(handle, CAEN_DGTZ_SLAVE_TERMINATED_READOUT_MBLT, (char *) &sevt->data, &buffersize);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " _broken = " << _broken<< endl;
return 1;
}
// cout << __FILE__ << " " << __LINE__ << " buffersize = " << buffersize << endl;
}
len = buffersize /4;
sevt->sub_padding = len%2;
len += sevt->sub_padding;
sevt->sub_length += len;
//cout << __LINE__ << " " << __FILE__ << " returning " << sevt->sub_length << endl;
if ( _endpulse)
{
const unsigned int reg = 0x811C;
const unsigned int pulseon = 0xC000;
const unsigned int pulseoff = 0x8000;
_broken = CAEN_DGTZ_WriteRegister(handle, reg, pulseon);
_broken = CAEN_DGTZ_WriteRegister(handle, reg, pulseoff);
}
return sevt->sub_length;
}
int daq_device_CAENdrs_std::endrun()
{
if ( _broken )
{
return 0; // we had a catastrophic failure
}
// cout << __LINE__ << " " << __FILE__ << " ending run " << endl;
_broken = CAEN_DGTZ_SWStopAcquisition(handle);
if ( _broken )
{
cout << __FILE__ << " " << __LINE__ << " Error: " << _broken << endl;
}
return _broken;
}
void daq_device_CAENdrs_std::identify(std::ostream& os) const
{
CAEN_DGTZ_BoardInfo_t BoardInfo;
if ( _broken)
{
os << "CAEN 1742 Digitizer Event Type: " << m_eventType
<< " Subevent id: " << m_subeventid
<< " ** not functional ** " << endl;
}
else
{
int ret = CAEN_DGTZ_GetInfo(handle, &BoardInfo);
if ( ret )
{
cout << " Error in CAEN_DGTZ_GetInfo" << endl;
return;
}
os << "CAEN 1742 Digitizer STD Model " << BoardInfo.ModelName
<< " Event Type: " << m_eventType
<< " Subevent id: " << m_subeventid
<< " Firmware " << BoardInfo.ROC_FirmwareRel << " / " << BoardInfo.AMC_FirmwareRel
<< " speed " << getGS() << "GS"
<< " delay " << _delay << "% ";
if (_trigger_handler) os << " *Trigger" ;
if (_endpulse) os << " *endpulse" ;
if (_warning) os << " **** warning - check setup parameters ****";
os << endl;
}
}
int daq_device_CAENdrs_std::max_length(const int etype) const
{
if (etype != m_eventType) return 0;
return (14900);
}
// the rearm() function
int daq_device_CAENdrs_std::rearm(const int etype)
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
if (etype != m_eventType) return 0;
return 0;
}
int daq_device_CAENdrs_std::SetConfigRegisterBit( const int bit)
{
const unsigned int forbidden_mask = 0x0FFFE7B7; // none of these bits must be touched
unsigned int pattern = 1<<bit;
if ( pattern & forbidden_mask)
{
cout << " attemt to set reserved bit: " << bit << endl;
return 1;
}
return CAEN_DGTZ_WriteRegister(handle,CAEN_DGTZ_BROAD_CH_CONFIGBIT_SET_ADD, pattern);
}
int daq_device_CAENdrs_std::ClearConfigRegisterBit( const int bit)
{
const unsigned int forbidden_mask = 0x0FFFE7B7; // none of these bits must be touched
unsigned int pattern = 1<<bit;
if ( pattern & forbidden_mask)
{
cout << " attemt to set reserved bit: " << bit << endl;
return 1;
}
return CAEN_DGTZ_WriteRegister(handle,CAEN_DGTZ_BROAD_CH_CLEAR_CTRL_ADD, pattern);
}
float daq_device_CAENdrs_std::getGS() const
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
switch ( _speed)
{
case CAEN_DGTZ_DRS4_1GHz:
return 1;
break;
case CAEN_DGTZ_DRS4_2_5GHz:
return 2.5;
break;
case CAEN_DGTZ_DRS4_5GHz:
return 5;
break;
default:
return 0;
break;
}
}