-
Notifications
You must be signed in to change notification settings - Fork 1
/
myReadOut.cpp.lastused
666 lines (527 loc) · 16.4 KB
/
myReadOut.cpp.lastused
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*****************************************
myReadOut.cpp
-------------
Dream DAQ program.
Version 2014: 25/11/2014
*****************************************/
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <stdint.h>
#include <cmath>
#include <ctime>
#include <climits>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#define DRS4_FULL_EV_SIZE 13836
#define DRS4_HALF_EV_SIZE 6920
using namespace std;
bool run_closing(false);
uint32_t maxevts(0), maxpedevts(0), events(0), pedevents(0), doubletrig(0), totevents(0), spills(0), totprevious(0);
uint32_t bsum(0);
bool ped_enabled(true);
double phypedratio(0);
double oldevents(0);
string tprint()
{
time_t tt(time(NULL));
char tbuf[26];
ctime_r( &tt, tbuf);
tbuf[24] = '\0';
return string(tbuf);
}
time_t t0(0);
void set_t0()
{
struct timeval t1;
gettimeofday(&t1, NULL);
t0 = t1.tv_sec;
return;
}
double get_time()
{
double time_us;
struct timeval t1;
gettimeofday(&t1, NULL);
t1.tv_sec -= t0;
time_us = (t1.tv_sec) * 1000000.0f + t1.tv_usec;
return time_us;
}
time_t tlast(0);
time_t tdiff(0);
time_t period1(0), period2(0);
void loginfo(double occ=0, double pedocc=0)
{
cout << '\n';
uint32_t neededsp(0);
if ((spills != 0) && (!run_closing))
{
time_t tnow(time(NULL));
if (tlast != 0)
{
time_t p = tnow-tlast;
if ((period1 != 0) && (period2 != 0) && (abs(period1-p)>1) && (abs(period2-p)>1))
{
cout << spills << " *** spill period was " << period1 << " " << period2 << " now is " << p << endl;
period2 = period1;
period1 = p;
}
else if (period1 == 0)
{
period1 = p;
}
else if (period2 == 0)
{
period2 = period1;
period1 = p;
}
}
tlast = tnow;
if ((events) && (maxevts > events))
{
double ratio = maxevts/double(events)-1.;
neededsp=uint32_t(ratio*spills+1);
}
}
// if (abort_run) break;
cout << spills << " : " << totevents-totprevious
<< " Events taken -- Total " << totevents
<< " -- Beam " << events
<< " ("<< occ*100 << "% occ.)"
<< " -- Pedestals " << pedevents
<< " ("<< pedocc*100 << "% occ.)"
<< " -- Both " << doubletrig
<< " -- End in ~ " << neededsp
<< " spills - " << tprint()
<< endl;
totprevious = totevents;
sync();
}
class firstlastMessages
{
public:
firstlastMessages() { set_t0(); cout << __FILE__ " compiled on " __DATE__ " " __TIME__ "\n**************************\n"; loginfo(); cout << "DREAM RUN STARTING" << endl; }
~firstlastMessages() { cout << "DREAM RUN CLOSED\n"; loginfo(); cout << "**************************\n" << endl; }
};
firstlastMessages _firstlastMsgs;
/****************************************/
#include "myFIFO-IO.h"
#include "sigHandler.h"
#include "myModules.h"
#include "myVme.h" // VME Base class
#include "myV792AC.h" // CAEN ADC V792AC class derived from VME
#include "myV262.h" // CAEN V262 I/O REG
#include "myV513.h" // CAEN V513 I/O REG
#include "myV2718.h"
#include "myV775.h"
#include "myV862AC.h"
//#define USE_DRS_V1742
/****************************************/
// Declare all the hardware
/****************************************/
#ifdef USE_DRS_V1742
#include "myV1742.h"
v1742 DRS4(0x0032000, "/V2718/cvA32_U_DATA/2", "/home/dreamtest/working/drs4command.list");
uint32_t drs4_ev_size(0);
#endif
// ADC V792
v792ac adc0(0x04000000,"/V2718/cvA32_U_DATA/0");
v792ac adc1(0x05000000,"/V2718/cvA32_U_DATA/0");
v792ac adc2(0x06000000,"/V2718/cvA32_U_DATA/0");
v792ac adc3(0x07000000,"/V2718/cvA32_U_DATA/0");
// ADC V862 - identical to V792 from SW point of view
v862ac adc4(0x03000000,"/V2718/cvA32_U_DATA/0");
uint32_t n_v792 = 5;
v792ac* v792s[] = {&adc2,&adc3,&adc4,&adc1,&adc0};
v775 ntdc(0x08000000,"/V2718/cvA32_U_DATA/0");
v262 io(0x400000,"/V2718/cvA24_U_DATA/1");
v513 ion(0xa00000,"/V2718/cvA24_U_DATA/1");
v2718 v2718_1(0,"/V2718/cvA24_U_DATA/0");
v2718 v2718_2(0,"/V2718/cvA24_U_DATA/1");
/****************************************/
uint32_t * buf;
uint32_t * bufsize;
volatile bool abort_run=false;
uint32_t rdtim;
uint32_t evinfo[2]={0,0};
void cntrl_c_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"%s cntrl_c_handler: sig%d\n\n", stime, sig);
fprintf(stderr,"aborting run\n");
abort_run = true;
}
double t_istrigger(1), t_rdoarm(0), t_readev(0), t_mtrigger(0), t_trghandler(0);
uint64_t n_loop(0);
void initIO() { io.reset(); ion.reset(); } // to reset the I/O registers
#define SET_IO_PORT(_port_nr_) io.enableNIMOut(_port_nr_)
#define CLEAR_IO_PORT(_port_nr_) io.disableNIMOut(_port_nr_)
inline void enableTriggers() { SET_IO_PORT(0); } // to enable triggers
inline void disableTriggers() { CLEAR_IO_PORT(0); } // to disable triggers
inline void enablePedTriggers() { SET_IO_PORT(3); } // to enable pedestal triggers
inline void disablePedTriggers() { CLEAR_IO_PORT(3); } // to disable pedestal triggers
inline void send_signal() { io.NIMPulse(2); };
inline void unlockTrigger() { io.NIMPulse(0); io.NIMPulse(3); } // to re-enable the trigger logic that goes auto-vetoed after any trigger
inline void resetNIMScaler() { io.NIMPulse(1); } // to reset the NIM scaler
inline bool isT1T2Trig() { return (ion.readInputRegister() & 1); /* io.getNIMIn(0); */ }
inline bool isHWBusy() { return (ion.readInputRegister() & 2); /* io.getNIMIn(1); */ } // return (v2718m.getInputRegister() & 1);
inline bool isInSpill() { return (ion.readInputRegister() & 4); /* io.getNIMIn(2); */ }
inline bool isPedTrig() { return (ion.readInputRegister() & 8); /* io.getNIMIn(3); */ }
void checkEvntCntrs()
{
cout << " DAQ evt.s : " << totevents;
for(size_t i=0; i < n_v792; ++i)
{
uint32_t evcnt = v792s[i]->eventCounter() + 1;
cout << " - hw evt.s in v792[" << i << "]: " << evcnt << " ";
}
cout << " - hw evt.s in v775: " << ntdc.evtCnt() << endl;
#ifdef USE_DRS_V1742
DRS4.dataReset();
#endif
}
void initV792(){
for(size_t i=0; i < n_v792; ++i)
{
v792s[i]->print();
v792s[i]->reset();
v792s[i]->disableSlide();
v792s[i]->disableOverflowSupp();
v792s[i]->disableZeroSupp();
uint16_t ped;
ped = v792s[i]->getI1();
uint32_t bid = v792s[i]->id();
cout << "v792[" << i << "] addr 0x" << hex << bid << dec
<< ": default ped I1 value is " << ped << endl;
ped = 255; // >~ minimum possible Iped (see manual par. 2.1.2.1)
v792s[i]->setI1(ped); // set I1 current to define pedestal position
ped =v792s[i]->getI1(); // in the range [0x00-0xFF] (see manual par. 4.34.2)
uint16_t thr = 0x00;
v792s[i]->setChannelThreshold(thr);
cout << "v792[" << i << "] addr 0x" << hex << bid << dec << ": now ped I1 value is " << ped << " thr " << thr << endl;
}
for(size_t i=0; i < n_v792; ++i)
{
v792s[i]->enableChannels();
uint32_t bid = v792s[i]->id();
uint16_t reg1 = v792s[i]->getStatus1();
uint16_t reg2 = v792s[i]->getControl1();
uint16_t reg3 = v792s[i]->getStatus2();
uint16_t reg4 = v792s[i]->getBit1();
uint16_t reg5 = v792s[i]->getBit2();
cout << "v792[" << i << "] addr 0x" << hex << bid << " status 1 0x" << reg1
<< " control 1 0x" << reg2 << " status 2 0x" << reg3 << " bit 1 0x" << reg4
<< " bit 2 0x" << reg5 << dec << endl;
}
}
void myReadOutInit(){
initV792();
ntdc.swReset();
ntdc.zeroSuppression(false);
ntdc.overSuppression(false);
ntdc.setLSB(140); // set the LSB (ps) full scale = 4096*LSB ==> 140 ps * 4096 ~ 570 ns
ntdc.storeEmpty();
ntdc.printRegisters();
}
void myReadOutArm(){
double tref = get_time();
for(size_t i=0; i < n_v792; ++i){
v792s[i]->clearData();
}
ntdc.dataReset();
#ifdef USE_DRS_V1742
DRS4.dataReset();
#endif
t_rdoarm += get_time()-tref;
}
uint32_t nSpill(0);
uint32_t nBeam(0);
uint32_t nPed(0);
uint32_t nTrig(0);
uint32_t nBoth(0);
int32_t myReadEvent(){
double tref = get_time();
uint32_t size=0;
//resets internal buffer
myNewEvent();
// V775 TDC
myFormatSubEvent(ntdc.id());
size = ntdc.readSingleEvent(&buf[*bufsize]);
myUpdateSubEvent(size);
// V792 and V862 ADCs
for(size_t i=0; i < n_v792; ++i){
myFormatSubEvent(v792s[i]->id());
size=v792s[i]->readEvent(&buf[*bufsize]);
myUpdateSubEvent(size);
}
#ifdef USE_DRS_V1742
// V1742 DRS4 Digitizer
myFormatSubEvent(DRS4.id());
time_t t1 = time(NULL);
time_t dt(0);
while (DRS4.busy() || !DRS4.eventReady())
{
m_usleep(10);
time_t t2 = time(NULL)-t1;
if (t2 > dt)
{
cout << " DRS4 event not ready ... waiting " << t2 << endl;
dt += 10;
}
}
size = DRS4.readSingleEvent(&buf[*bufsize]);
if (size != drs4_ev_size)
if (size < 10000)
{
if (size != DRS4_HALF_EV_SIZE)
cout << " ****** ERROR FROM DRS READOUT ==> TRYING TO RECOVER ******" << size << " half " << DRS4_HALF_EV_SIZE << " full " << DRS4_FULL_EV_SIZE << endl;
// do some tricks ...
uint32_t * drs_init = &buf[*bufsize];
uint32_t * drs_half = drs_init + size;
drs_half -= SIZE_OF_V1742_HEADER;
uint32_t temp_store[SIZE_OF_V1742_HEADER];
for (uint32_t i=0; i<SIZE_OF_V1742_HEADER; i++) temp_store[i] = drs_half[i];
while (!DRS4.eventReady()) { m_usleep(10); }
uint32_t size1 = DRS4.readSingleEvent(drs_half);
uint32_t evsize1 = drs_init[0] & 0xfffffff;
uint32_t evsize2 = drs_half[0] & 0xfffffff;
uint32_t gm1 = drs_init[1] & 0xf;
uint32_t gm2 = drs_half[1] & 0xf;
uint32_t ec1 = drs_init[2];
uint32_t ec2 = drs_half[2];
uint32_t ett1 = drs_init[3];
uint32_t ett2 = drs_half[3];
uint32_t tot_evsize = evsize1+evsize2-SIZE_OF_V1742_HEADER;
uint32_t gr_mask = gm1 | gm2;
uint32_t check_mask = gm1 & gm2;
if ((gr_mask != 0xf) && (check_mask != 0))
{
cout << " gr mask 1 " << hex << gm1 << " gr mask 2 " << gm2
<< " OR " << gr_mask << " AND " << check_mask << dec << endl;
}
drs_init[0] = 0xa0000000 | tot_evsize;
drs_init[1] = drs_init[1] | gr_mask ;
for (uint32_t i=0; i<SIZE_OF_V1742_HEADER; i++) drs_half[i] = temp_store[i];
if ((size1 != DRS4_HALF_EV_SIZE) || (evsize1 != DRS4_HALF_EV_SIZE) ||
(evsize2 != DRS4_HALF_EV_SIZE) || (tot_evsize != DRS4_FULL_EV_SIZE))
cout << spills << " events: -- Total " << totevents
<< " -- Beam " << events
<< " -- Pedestals " << pedevents
<< " -- Both " << doubletrig
<< " *** DRS problem size1: " << size1 << " evsize1: " << evsize1
<< " evsize2: " << evsize2 << " ==> now is: " << tot_evsize
<< "\n ev c[1] " << ec1 << " [2] " << ec2
<< hex << " ev t[1] " << ett1 << " [2] " << ett2
<< dec << endl;
size = tot_evsize;
// DRS4.acqReset();
}
myUpdateSubEvent(size);
#endif
t_readev += get_time()-tref;
return 0;
}
int32_t getEvent (uint32_t isPhysEvent){
double tref = get_time();
while (isHWBusy() && !abort_run) {}
if(abort_run) return 1;
myReadEvent();
myWriteEvent(spills,isPhysEvent);
t_mtrigger += get_time()-tref;
return 0;
}
bool isTriggerPresent (uint32_t * trigmask)
{
double tref = get_time();
bool physT1T2 = isT1T2Trig();
bool ped = isPedTrig();
if (ped) physT1T2 = isT1T2Trig();
if (ped || physT1T2) t_istrigger += get_time()-tref;
*trigmask = (ped<<1) | physT1T2;
return physT1T2 || ped;
}
void handle_trigger (uint32_t trigmask,
uint32_t maxpedevts,
uint32_t * bsum,
bool * ped_enabled)
{
double tref = get_time();
if (!getEvent(trigmask & 0x1)) {
*bsum += *bufsize;
if(trigmask & 1){
events++;
if (trigmask & 2) doubletrig++;
}else{
pedevents++;
if(pedevents>=maxpedevts){
disablePedTriggers();
*ped_enabled=false;
}
}
totevents ++;
}
myReadOutArm();
t_trghandler += get_time()-tref;
}
void check_and_handle_event()
{
uint32_t trigmask;
double tref = get_time();
if (isTriggerPresent(&trigmask)) {
send_signal();
handle_trigger(trigmask, maxpedevts, &bsum, &ped_enabled);
if (trigmask & 2) {
oldevents+=phypedratio;
if (events<(uint32_t)lrint(oldevents)) disablePedTriggers();
} else if (trigmask & 1) {
if (ped_enabled && (events>oldevents)) enablePedTriggers();
}
unlockTrigger();
rdtim += get_time()-tref;
} // if (isTriggerPresent(&trigmask))
}
/****************************************/
int32_t main(int32_t argc, char** argv)
/****************************************/
{
int32_t runnr;
uint32_t maxtotevts;
struct tms tmsbuf;
uint32_t ticks_per_sec;
double time0, cputime, tottime, percent;
time0 = times(&tmsbuf);
ticks_per_sec = sysconf(_SC_CLK_TCK);
signal(SIGINT, cntrl_c_handler); // Control-C handler
cout.precision(4);
//Maximum number of events
maxevts=myMaxEvents();
phypedratio=myPhysPedRatio();
double dmaxp = double(maxevts)/phypedratio;
maxpedevts = lrint(dmaxp);
maxtotevts=maxevts+maxpedevts;
int32_t xrunnr(0);
while (--argc > 0)
{
++argv;
if ((strcmp(*argv, "-runnr") == 0) && (argc > 1))
{
--argc; ++argv;
xrunnr = strtol(*argv, NULL, 10);
}
}
runnr = xrunnr;
if (runnr <= 0)
{
cerr << "****************** ERROR ****************** run number not found "
<< runnr << " exiting now\n" << endl;
exit(1);
}
cout << tprint() << " Run " << runnr << " starting -> going to acquire " << maxtotevts << " events with trigger T1*T2*Vbar" << endl;
#ifdef USE_DRS_V1742
//check if V1792 initialization worked
if(DRS4.getErrorState()){
m_usleep(1000);
return -1;
}
#endif
//Init I/O Registers
initIO();
//Set vetoes
disableTriggers();
disablePedTriggers();
//Reset and init readout
myReadOutInit();
//Connect the FIFO
myOpenRun(&abort_run);
//Internal buffer
buf = myBuffer(&bufsize);
#ifdef USE_DRS_V1742
drs4_ev_size = DRS4.evSize();
cout << " ************ drs4_ev_size is " << drs4_ev_size << " ********* " << endl;
while (isInSpill()) m_usleep(10);
#endif
//Start of run setting up
events=0;
spills=0;
myReadOutArm();
resetNIMScaler();
rdtim=0;
unlockTrigger();
cout << tprint() << " Waiting for a spill ... " << endl;
//Wait for a spill before to start
while(!isInSpill() && !abort_run) {}
cout << "\n Got ... start data taking at " << tprint() << endl;
//Remove the veto and start
myFIFOlock();
enableTriggers();
do { // daq loop ... exit on ctrl-c or when (totevents>=maxtotevts)
check_and_handle_event();
if ((totevents>=maxtotevts) || abort_run) break;
if(!isInSpill()){
disableTriggers();
unlockTrigger();
spills++;
myFIFOUpdateSpillNr();
myFIFOUpdateEvtNr( events, pedevents );
double occ=myFIFOOccupancy();
double pedocc=myPedFIFOOccupancy();
myFIFOunlock();
loginfo(occ,pedocc);
checkEvntCntrs();
while(!isInSpill() && !abort_run) { m_usleep(10); }
myFIFOlock();
if (abort_run) break;
enableTriggers();
} // if(!isInSpill())
} while(1); // daq loop ... exit on ctrl-c or when (totevents>=maxtotevts)
run_closing = true;
cout << tprint() << " Ending run ... setting Trigger veto" << endl;
disableTriggers();
check_and_handle_event();
myFIFOUpdateSpillNr();
myFIFOUpdateEvtNr( events, pedevents );
double occ=myFIFOOccupancy();
double pedocc=myPedFIFOOccupancy();
myFIFOunlock();
loginfo(occ,pedocc);
//Disconnect the FIFO
myCloseRun();
tottime = times(&tmsbuf) - time0;
tottime /= ticks_per_sec;
cputime = tmsbuf.tms_utime + tmsbuf.tms_stime;
cputime /= ticks_per_sec;
percent = cputime/tottime;
percent = rint(percent*10000)/100;
if (totevents) {
t_istrigger /= totevents;
t_rdoarm /= totevents;
t_readev /= totevents;
t_mtrigger /= totevents;
t_trghandler /= totevents;
}
cout << "istrigger " << uint32_t(t_istrigger)
<< " usec - rdoarm " << uint32_t(t_rdoarm)
<< " usec - readev " << uint32_t(t_readev)
<< " usec - mtrigger " << uint32_t(t_mtrigger)
<< " usec - trghandler " << uint32_t(t_trghandler)
<< " usec\n";
cout << "TIME - TOT: " << tottime << " sec - CPU: " << cputime << " sec (" << percent << "%)" << endl;
cout.precision(6);
if (totevents>0) {
uint32_t rt, meansz;
rt = rdtim/totevents;
meansz = bsum*sizeof(uint32_t)/totevents;
cout << "Average size: " << meansz << " bytes - time/event: "
<< rt << " usec" << endl;
}
return 0;
}