-
Notifications
You must be signed in to change notification settings - Fork 1
/
daq_device_drs.cc
413 lines (324 loc) · 8.21 KB
/
daq_device_drs.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
#include <iostream>
#include "DRS.h"
#include <daq_device_drs.h>
#include <string.h>
using namespace std;
DRS *daq_device_drs::_drs=0;
daq_device_drs::daq_device_drs(const int eventtype
, const int subeventid
, const int serialnumber
, const int trigger
, const float triggerthreshold
, const int slope
, const int delay
, const int speed
, const int start
, const int nch
, const int baseline)
{
m_eventType = eventtype;
m_subeventid = subeventid;
_serialnumber = serialnumber;
_trigger_handler=0;
_broken = 0;
if ( ! _drs)
{
_drs = new DRS();
}
if ( _drs->GetNumberOfBoards() == 0)
{
_broken = 1;
delete _drs;
_drs = 0;
b = 0;
return;
}
// this is new: we can now select which board we want
if ( ! _serialnumber)
{
b = _drs->GetBoard(0);
}
else
{
for ( int i = 0; i < _drs->GetNumberOfBoards(); i++)
{
b = _drs->GetBoard(i);
if ( b->GetBoardSerialNumber() == _serialnumber) break;
else b = 0;
}
}
//safety belt
if ( ! b)
{
_broken = 1;
return;
}
// the trigger allows to make an "or" of 5 triggers
_trigger = trigger & 0x1f1f;
// bit number 5 says if we also provide the system trigger
if (trigger & 0x20)
{
_trigger_handler = 1 ;
}
// cout << "** " << __FILE__ << " triggerhandler " << _trigger_handler << endl;
_tthreshold = triggerthreshold / 1000. ;
_slope = slope;
_delay = delay;
_start = start;
_nch = nch;
_baseline = baseline;
if ( _start > 1023 )
{
cout << "** " << __FILE__ << " warning: invalid start channels" << endl;
_start = 0;
}
if ( _nch == 0 ) _nch = 1024;
if ( ( _start + _nch) > 1024 )
{
cout << "** " << __FILE__ << " warning: invalid number of channels" << endl;
_nch = 1024 - _start;
}
// cout << __LINE__ << " " << __FILE__ << " speed " << speed<< endl;
if ( speed != 0
&& speed != 1
&& speed != 2
&& speed != 3 )
{
_speed = 0;
cout << "invalid speed, setting 0.7GS/s" << endl;
}
else
{
_speed = speed;
}
if ( _trigger_handler )
{
// cout << __LINE__ << " " << __FILE__ << " registering triggerhandler " << endl;
_th = new drsTriggerHandler (b);
registerTriggerHandler ( _th);
}
else
{
_th = 0;
}
b->Init();
}
daq_device_drs::~daq_device_drs()
{
// if (b) delete b;
b = 0;
if (_th)
{
clearTriggerHandler();
delete _th;
_th = 0;
}
}
int daq_device_drs::init()
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
b->Reinit();
b->SetFrequency(getGS(), 1);
b->SetTranspMode(1);
b->SetInputRange(0);
if ( b->GetBoardType() >= 8)
{
b->EnableTrigger(1,0); // lemo off, analog trigger on
b->SetTriggerSource(_trigger);
b->SetTriggerLevel(_tthreshold);
b->SetTriggerPolarity(_slope);
b->SetTriggerDelayNs(_delay);
}
else if ( b->GetBoardType() == 7)
{
if ( _trigger & 0x10) // external trigger
{
b->EnableTrigger(1,0); //external trigger
b->SetTriggerPolarity(_slope);
b->SetTriggerDelayNs(_delay);
}
else
{
int real_trigger =0;
if ( _trigger & 0x1) real_trigger = 0;
else if ( _trigger & 0x2) real_trigger = 1;
else if ( _trigger & 0x4) real_trigger = 2;
else if ( _trigger & 0x8) real_trigger = 3;
b->EnableTrigger(0,1); // lemo off, analog trigger on
b->SetTriggerSource( real_trigger);
b->SetTriggerLevel(_tthreshold);
b->SetTriggerPolarity(_slope);
b->SetTriggerDelayNs(_delay);
}
}
b->SetRefclk(0);
if ( _baseline)
{
double bl = _baseline;
bl /= -1000;
cout << __LINE__ << " " << __FILE__ << " setting baseline to " << bl << endl;
b->SetInputRange(bl);
}
// and we trigger rearm with our event type so it takes effect
rearm (m_eventType);
return 0;
}
// the put_data function
int daq_device_drs::put_data(const int etype, int * adr, const int length )
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
int len = 0;
if (etype != m_eventType ) // not our id
{
return 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 = 81;
sevt->reserved[0] = 0;
sevt->reserved[1] = 0;
if ( length < max_length(etype) )
{
// cout << __LINE__ << " " << __FILE__ << " length " << length <<endl;
return 0;
}
unsigned int data;
sevt->data = _nch | ( 0xf << 16); // say "1024 samples, all 4 channels on" for now
b->TransferWaves(0, 8);
float *d = (float *) &sevt->data; // we let "d" point to the location past the first word
d++; // which we set 2 lines above
len++;
// float *x = d;
b->GetTime(0, b->GetNominalFrequency(), b->GetTriggerCell(0), d); // the first 1024 samples are the time bins
if ( _start)
{
memmove (d, &d[_start], _nch);
}
len += _nch;
d += _nch;
for ( int i = 0; i < 8; i+=2) // and here come channel 0,2,4,6 (the actually connected channels)
{
b->GetWave(0, i, d);
if ( _start)
{
memmove (d, &d[_start], _nch);
}
len += _nch;
d += _nch;
}
// for ( int j = 0; j < 1024; j++)
// {
// cout << j
// << " " << x[j]
// << " " << x[j + 1024]
// << " " << x[j + 2*1024]
// << " " << x[j + 3*1024]
// << " " << x[j + 4*1024]
// << endl;
// }
sevt->sub_padding = len%2;
len = len + (len%2);
sevt->sub_length += len;
// cout << __LINE__ << " " << __FILE__ << " returning " << sevt->sub_length << endl;
return sevt->sub_length;
}
void daq_device_drs::identify(std::ostream& os) const
{
if ( _broken)
{
os << "DRS4 Eval Board Event Type: " << m_eventType
<< " Subevent id: " << m_subeventid
<< " ** not functional ** " << endl;
}
else
{
os << "DRS4 Eval Board Event Type: " << m_eventType
<< " Subevent id: " << m_subeventid
<< " S/N " << b->GetBoardSerialNumber()
<< " Type " << b->GetBoardType()
<< " Trg " << _trigger
<< " Thresh " << _tthreshold*1000 <<"mV";
if (_slope) os << " neg " ;
else os << " pos ";
os << " delay " << _delay
// os << " delay " << _delay << " (" << getGS() << "/" << getActualGS() << "GS) "
<< " speed " << _speed << " ("
// << getGS() << " ( " << getActualGS() << ") GS) "
<< getGS() << "GS) "
<< " start " << _start << " nch " << _nch;
if ( _baseline) os << " baseline " << _baseline;
if (_trigger_handler) os << " *Trigger*" ;
os << endl;
}
}
int daq_device_drs::max_length(const int etype) const
{
if (etype != m_eventType) return 0;
return (5*1024 + SEVTHEADERLENGTH + 10);
}
// the rearm() function
int daq_device_drs::rearm(const int etype)
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
if (etype != m_eventType) return 0;
b->StartDomino();
return 0;
}
double daq_device_drs::getActualGS() const
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
double f;
b->ReadFrequency(0, &f);
return f;
}
double daq_device_drs::getGS() const
{
if ( _broken )
{
// cout << __LINE__ << " " << __FILE__ << " broken ";
// identify();
return 0; // we had a catastrophic failure
}
switch ( _speed)
{
case 0:
return 0.7;
break;
case 1:
return 1.;
break;
case 2:
return 2.;
break;
case 3:
return 5;
break;
default:
return 1;
break;
}
}