-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyReadOutNoStop2024.cpp
713 lines (619 loc) · 22.6 KB
/
myReadOutNoStop2024.cpp
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdint>
#include <csignal>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sstream>
#include <iomanip>
#include "myModules.h" // VME Module Id.s
#include "myV2718.h" // CAEN V2718 VME Bridge class
#include "myV513.h" // CAEN V513 I/O REG class
#include "myV792AC.h" // CAEN V792AC QDC class
#include "myV862AC.h"
#include "myV775N.h" // CAEN V775N TDC class
#include "myV775.h" // CAEN V775 TDC class
using namespace std;
#define TRIGGER_MASK 7 // channels 0, 1, 2 -> 0 = PHYS TRIG, 1 = PED TRIG, 2 = IN SPILL
#define TRIGGER_OR_IN 7 // channel 7 -> TRIGGER OR INPUT
#define PEDESTAL_VETO 8 // channel 8 -> PEDESTAL VETO
#define DAQ_VETO 9 // channel 9 -> DAQ VETO
#define UNLOCK_PED_TRIGGER 10 // channel 10 -> UNLOCK PEDESTAL TRIGGER
#define UNLOCK_PHYS_TRIGGER 11 // channel 11 -> UNLOCK PHYSICS TRIGGER
#define SCALER_RESET 12 // channel 12 -> SCALER RESET
#define NSLEEPT 50 // for sleeping 50 ns
#define V775_35ps 35 // 35 ps as scale for V775
#define V775_100ps 100 // 100 ps as scale for V775
#define V775_140ps 140 // 140 ps as scale for V775
const uint32_t nQDCs = 4;
const uint32_t QDCchans = 32;
const uint32_t QDCevsize = QDCchans+2;
const uint32_t QDCtotsize = QDCevsize*nQDCs;
v792ac* qdcPtrs[nQDCs];
// const uint32_t qdcAds[nQDCs] = { 0x03000000, 0x04000000, 0x05000000, 0x06000000 }; // standar addressing
// const uint32_t qdcAds[nQDCs] = { 0x03000000, 0x1B110000, 0x05000000, 0x06000000 }; // modified address 2024.08.29
const uint32_t qdcAds[nQDCs] = { 0x1B110000, 0x05000000, 0x06000000, 0x03000000 }; // new standard addressing 2024.08.29
const uint32_t V775Nchans = 16;
void dlwait( string msg )
{
cout << "Press any key to continue ... " << msg;
getchar();
cout << endl;
}
const uint32_t ONEK = 1000;
const uint32_t ONEM = ONEK*ONEK;
const uint32_t ONEG = ONEK*ONEM;
void nsleep ( uint32_t ns )
{
uint32_t secs = ns / ONEG;
uint32_t nsrem = ns % ONEG;
struct timespec ndelay = { secs, nsrem };
nanosleep ( &ndelay, NULL );
}
void myusleep (uint32_t us )
{
uint32_t secs = us / ONEM;
uint32_t usrem = us % ONEM;
struct timespec ndelay = { secs, usrem*ONEK };
nanosleep ( &ndelay, NULL );
}
inline void enableTriggers( v513& ioreg ) { ioreg.clearOutputBit( DAQ_VETO ); }
inline void disableTriggers( v513& ioreg ) { ioreg.setOutputBit( DAQ_VETO ); }
inline void enablePedestals( v513& ioreg ) { ioreg.clearOutputBit( PEDESTAL_VETO ); }
inline void disablePedestals( v513& ioreg ) { ioreg.setOutputBit( PEDESTAL_VETO ); }
inline void resetScaler( v513& ioreg )
{
nsleep(NSLEEPT);
ioreg.setOutputBit( SCALER_RESET );
nsleep(NSLEEPT);
ioreg.clearOutputBit( SCALER_RESET );
}
inline void unlockTrigger( v513& ioreg )
{
nsleep(NSLEEPT);
ioreg.setOutputBit( UNLOCK_PED_TRIGGER );
nsleep(NSLEEPT);
ioreg.setOutputBit( UNLOCK_PHYS_TRIGGER );
nsleep(NSLEEPT);
ioreg.clearOutputBit( UNLOCK_PED_TRIGGER );
nsleep(NSLEEPT);
ioreg.clearOutputBit( UNLOCK_PHYS_TRIGGER );
}
int initV513( v513& ioreg )
{
uint16_t w;
ioreg.read16phys(0xFE, &w);
cout << hex << " V513 FE " << w << endl;
myusleep(10);
ioreg.read16phys(0xFC, &w);
cout << hex << " V513 FC " << w << endl;
myusleep(10);
ioreg.read16phys(0xFA, &w);
cout << hex << " V513 FA " << w << endl;
myusleep(10);
w=0;
ioreg.write16phys(0x48, w);
myusleep(10);
ioreg.write16phys(0x46, w);
myusleep(10);
ioreg.write16phys(0x42, w);
myusleep(10);
ioreg.reset();
myusleep(10);
ioreg.read16phys(0x04, &w);
cout << hex << " V513 0x4 " << w << endl;
myusleep(10);
for (int i=0; i<8; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelInput(i);
myusleep(10);
ioreg.read16phys(reg, &w);
cout << hex << " V513 input ch " << i << " reg " << reg << " " << w << endl;
myusleep(10);
}
for (int i=8; i<16; i++)
{
int reg = 0x10+i*2;
ioreg.setChannelOutput(i);
myusleep(10);
ioreg.read16phys(reg, &w);
cout << hex << " V513 output ch " << i << " reg " << reg << " " << w << endl;
myusleep(10);
}
disableTriggers( ioreg );
myusleep ( 10 );
disablePedestals( ioreg );
myusleep ( 10 );
return 0;
}
void initV792( v792ac* v792adc )
{
v792adc->print();
v792adc->reset();
v792adc->disableSlide();
v792adc->disableOverflowSupp();
v792adc->disableZeroSupp();
v792adc->clearEventCounter();
v792adc->clearData();
uint16_t ped;
ped = v792adc->getI1();
uint32_t bid = v792adc->id();
cout << "v792ac addr 0x" << hex << bid << dec
<< ": default ped I1 value is " << ped << endl;
ped = 255; // >~ minimum possible Iped (see manual par. 2.1.2.1)
v792adc->setI1(ped); // set I1 current to define pedestal position
ped =v792adc->getI1(); // in the range [0x00-0xFF] (see manual par. 4.34.2)
uint16_t thr = 0x00;
v792adc->setChannelThreshold(thr);
cout << "v792ac addr 0x" << hex << bid << dec << ": now ped I1 value is " << ped << " thr " << thr << endl;
v792adc->enableChannels();
uint16_t reg1 = v792adc->getStatus1();
uint16_t reg2 = v792adc->getControl1();
uint16_t reg3 = v792adc->getStatus2();
uint16_t reg4 = v792adc->getBit1();
uint16_t reg5 = v792adc->getBit2();
uint32_t evc = v792adc->eventCounter();
cout << "v792ac 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;
cout << "v792ac event counter " << evc << endl;
}
void initV775N( v775n& xtdc )
{
xtdc.swReset();
xtdc.setCommon ( v775n::commonStart );
xtdc.zeroSuppression(false);
xtdc.overSuppression(false);
xtdc.setLSB(V775_140ps); // set the LSB (ps) full scale = 4096*LSB ==> 100 ps * 4096 ~ 410 ns
xtdc.storeEmpty();
xtdc.evCntReset();
xtdc.dataReset();
xtdc.printRegisters();
cout << "v775 mode " << xtdc.getMode() << endl;
cout << "v775 fsr (ns) " << 4096*8.9/xtdc.fullScale() << endl;
cout << "v775 LSB (ns) " << 8.9/xtdc.fullScale() << endl;
}
void initV775( v775& xtdc )
{
xtdc.swReset();
xtdc.setCommon ( v775::commonStart );
xtdc.zeroSuppression(false);
xtdc.overSuppression(false);
xtdc.setLSB(V775_35ps); // set the LSB (ps) full scale = 4096*LSB ==> 100 ps * 4096 ~ 410 ns
xtdc.storeEmpty();
xtdc.evCntReset();
xtdc.dataReset();
xtdc.printRegisters();
cout << "v775 mode " << xtdc.getMode() << endl;
cout << "v775 fsr (ns) " << 4096*8.9/xtdc.fullScale() << endl;
cout << "v775 LSB (ns) " << 8.9/xtdc.fullScale() << endl;
}
volatile bool abort_run(false);
volatile bool pause_run(false);
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;
}
void sigusr1_handler ( int32_t sig )
{
time_t timestr = time(NULL);
char * stime = ctime(×tr);
stime[24] = 0;
fprintf(stderr,"%s sigusr1_handler: sig%d pause_run is %d\n\n", stime, sig, pause_run);
pause_run = not pause_run;
pause_run ? fprintf(stderr,"pausing run\n") : fprintf(stderr,"resuming run\n");
}
uint32_t resetHardware ( const uint32_t nQDCs, v792ac* qdcPtrs[], v775n& xtdc0 )
{
for (uint32_t j=0; j<nQDCs; j++)
{
initV792 ( qdcPtrs[j] );
sched_yield();
}
initV775N ( xtdc0 );
sched_yield();
return 0;
}
uint32_t alignEventCounter ( uint32_t evtnumber, const uint32_t nQDCs, v792ac* qdcPtrs[], v775n& xtdc0 )
{
for (uint32_t j=0; j<nQDCs; j++)
{
qdcPtrs[j]->eventCounterOffset ( evtnumber );
}
xtdc0.eventCounterOffset ( evtnumber );
return 0;
}
uint64_t xreadtime(0);
uint32_t readEvent ( const uint32_t nQDCs, v792ac* qdcPtrs[], v775n& xtdc0, uint32_t ntriggers, uint32_t* buffer, uint32_t* qdcsize, uint32_t* tdcsize )
{
struct timeval startread;
gettimeofday ( &startread, NULL );
uint32_t nevqdc[nQDCs];
uint32_t nevtdc0;
// cerr << " evt " << ntriggers << " event counters ";
for (uint32_t j=0; j<nQDCs; j++)
{
int32_t dr;
do
dr = qdcPtrs[j]->dready(1);
while (!dr);
nevqdc[j] = qdcPtrs[j]->eventCounter();
// cerr << " " << j << " " << nevqdc[j];
}
nevtdc0 = xtdc0.eventCounter();
// cerr << " TDC " << nevtdc0;
uint32_t qdcsz[nQDCs];
*qdcsize = 0;
for (uint32_t j=0; j<nQDCs; j++)
{
qdcPtrs[j]->setEvents( ntriggers );
qdcsz[j] = qdcPtrs[j]->readEvent( buffer );
buffer += qdcsz[j];
*qdcsize += qdcsz[j];
}
uint32_t sizetdc = xtdc0.readSingleEvent( buffer );
buffer += sizetdc;
*tdcsize = sizetdc;
uint32_t errcode = 0;
for (uint32_t j=0; j<nQDCs; j++)
{
errcode |= (ntriggers != nevqdc[0]) << j;
}
errcode |= (ntriggers != nevtdc0) << nQDCs;
if ((errcode == 31) && (nevqdc[0] == ntriggers+1)) errcode = 0xff;
if (errcode)
{
time_t rtime = time(NULL);
struct tm myrtime;
localtime_r( &rtime, &myrtime );
cerr << " time is " << dec << rtime << " " << myrtime.tm_year+1900 << "." << myrtime.tm_mon+1 << "." << myrtime.tm_mday << "-" << myrtime.tm_hour << ":" << myrtime.tm_min << ":" << myrtime.tm_sec << endl;
cerr << dec << ntriggers;
for (uint32_t j=0; j<nQDCs; j++)
{
cerr << " nq[" << j << "] " << nevqdc[j];
}
cerr << " nt0 " << nevtdc0 << " - error code " << hex << errcode << endl;
}
uint32_t productsiz = qdcsz[0];
for (uint32_t j=1; j<nQDCs; j++)
{
productsiz *= qdcsz[j];
}
if (productsiz == 0)
{
errcode |= 1 << 7;
struct timeval tod;
gettimeofday ( &tod, NULL );
struct tm * timeinfo = localtime ( &tod.tv_sec );
double dsecs = timeinfo->tm_sec + tod.tv_usec*1E-6;
cerr << " time is " << dec << timeinfo->tm_yday << ":" << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << dsecs << " ev # " << ntriggers;
for (uint32_t j=0; j<nQDCs; j++)
{
cerr << " size" << j << qdcsz[j];
}
cerr << " sizetdc " << sizetdc << " - error code " << hex << errcode << endl;
}
struct timeval endread;
gettimeofday ( &endread, NULL );
xreadtime += (endread.tv_sec-startread.tv_sec)*ONEM
+ (endread.tv_usec-startread.tv_usec);
return errcode;
}
void print_event ( ofstream& ofs, uint32_t trignum, struct timeval tod, uint32_t spill_nr, uint32_t* Tcts, uint32_t tmask, uint32_t* myBuffer, uint32_t qdcsize, uint32_t tdcsize )
{
struct tm * timeinfo = localtime ( &tod.tv_sec );
ofs << dec << " ev # " << trignum << " time " << timeinfo->tm_yday
<< "-" << timeinfo->tm_hour << "-" << timeinfo->tm_min << "-"
<< timeinfo->tm_sec << "-" << tod.tv_usec << " spill " << spill_nr
<< " tow cts " << Tcts[0] << " " << Tcts[1] << " " << Tcts[2] << " trigger mask " << hex << tmask << " values:";
for (uint32_t kqdc=0; kqdc<nQDCs; kqdc++)
{
uint32_t offs = kqdc*QDCevsize; // QDCevsize = 34
uint32_t ch_offs = kqdc*QDCchans; // QDCchans = 32
for (uint32_t j=offs+1; j<offs+QDCevsize-1; j++) // j=k*34+1; j<k*34+33
{
uint32_t chan = (myBuffer[j] >> 16) & 0x1f;
chan += ch_offs;
ofs << " " << dec << chan << " " << hex << (myBuffer[j] & 0x3fff);
}
}
ofs << " TDC size " << dec << tdcsize << " val.s ";
uint32_t* tdcbuffer = &myBuffer[qdcsize];
if (tdcsize != 0xffffffff) for (uint32_t j=0; j<tdcsize; j++)
{
uint32_t _v = tdcbuffer[j];
uint32_t chan = dataDecodeNChannel(_v);
uint32_t mk = dataDecodeNFlags(_v);
uint32_t val = dataDecodeNValue(_v);
ofs << " " << chan << " " << mk << " " << val;
}
ofs << endl;
return;
}
void print_fake_event ( ofstream& ofs, uint32_t trignum, struct timeval tod, uint32_t spill_nr, uint32_t* Tcts, uint32_t* myBuffer )
{
struct tm * timeinfo = localtime ( &tod.tv_sec );
ofs << dec << " ev # " << trignum << " time " << timeinfo->tm_yday
<< "-" << timeinfo->tm_hour << "-" << timeinfo->tm_min << "-"
<< timeinfo->tm_sec << "-" << tod.tv_usec << " spill " << spill_nr
<< " tow cts " << Tcts[0] << " " << Tcts[1] << " " << Tcts[2] << " trigger mask " << hex << 0xffffffff << " values:";
for (uint32_t kqdc=0; kqdc<nQDCs; kqdc++)
{
uint32_t offs = kqdc*QDCevsize;
uint32_t ch_offs = kqdc*QDCchans;
for (uint32_t j=offs+1; j<offs+QDCevsize-1; j++)
{
uint32_t chan = (myBuffer[j] >> 16) & 0x1f;
chan += ch_offs;
ofs << " " << dec << chan << " " << 0;
}
}
ofs << " TDC size " << dec << 0 << " val.s ";
ofs << endl;
return;
}
int main( int argc, char** argv )
{
ofstream ofs;
ifstream ifs;
double readoutTime(0);
uint32_t trignum(0);
uint32_t numevofs(0);
uint32_t Tcts[4] = { 0, 0, 0, 0 };
uint32_t myBuffer[4096];
signal(SIGINT, cntrl_c_handler); // Control-C handler
signal(SIGUSR1, sigusr1_handler); // Control-USR1 handler
// v2718 v2718_0(0,"/V2718/cvA24_U_DATA/0"); // VME interface
v513 ioreg(0xa00000,"/V2718/cvA24_U_DATA/0"); // I/O register
myusleep ( 10 );
ioreg.print();
myusleep ( 10 );
// Init I/O Register
initV513 ( ioreg );
sched_yield();
for (uint32_t j=0; j<nQDCs; j++) qdcPtrs[j] = new v792ac(qdcAds[j], "/V2718/cvA32_U_DATA/0");
cout << " nQDCs " << nQDCs << " ads ";
for (uint32_t j=0; j<nQDCs; j++) cout << hex << " " << j << " " << qdcAds[j] << " pointer " << qdcPtrs[j];
cout << endl;
v775n xtdc0(0x08000000,"/V2718/cvA32_U_DATA/0"); // V775N TDC
// Init QDCs and TDCs
resetHardware ( nQDCs, qdcPtrs, xtdc0 );
resetScaler( ioreg );
unlockTrigger( ioreg );
cout << " ****************** start of run ****************** " << endl << endl;
time_t stime = time(NULL);
cout << " time is " << stime << endl;
struct tm mytime;
localtime_r( &stime, &mytime );
cout << mytime.tm_year+1900 << "." << mytime.tm_mon+1 << "." << mytime.tm_mday << "." << mytime.tm_hour
<< ":" << mytime.tm_min << ":" << mytime.tm_sec << endl;
cout << asctime(&mytime) << endl;
uint32_t runnbr;
ifs.open ("/home/dreamtest/working/run.number",std::ofstream::in);
ifs >> runnbr;
ifs.close();
ofs.open ("/home/dreamtest/working/run.number",std::ofstream::out);
ofs << ++runnbr;
ofs.close();
stime -= 1600000000;
std::stringstream s;
s << std::setfill ('0') << std::setw (4) << runnbr;
string fname = "sps2024data.run" + s.str() + ".txt";
string fname_r = "sps2024data_r.run" + s.str() + ".txt";
string fname_z = "sps2024data_z.run" + s.str() + ".txt";
if (argc == 2) fname = argv[1];
cout << " filename is " << fname << endl;
ofs.open (fname.c_str(), std::ofstream::out);
time_t tr0 = time(NULL);
bool running(true);
volatile bool exit_now(false);
volatile bool on_error(false);
uint32_t spill_nr(0);
enableTriggers( ioreg );
uint32_t prevTrig(0), prevCts[4] = {0};
bool is_spill(false);
while (1)
{
if (abort_run) exit_now = true;
if (exit_now) disableTriggers( ioreg );
if (running and pause_run)
{
disableTriggers(ioreg);
running = false;
time_t tr = time(NULL) - tr0;
cout << dec << tr << "(sec) " << trignum << "(evt) ioreg A " << hex << ioreg.readInputRegister()
<< " running was true (now false) and pause_run true" << endl;
}
else if (not running and not pause_run)
{
enableTriggers(ioreg);
running = true;
time_t tr = time(NULL) - tr0;
cout << dec << tr << "(sec) " << trignum << "(evt) ioreg A " << hex << ioreg.readInputRegister()
<< " running was false (now true) and pause_run false" << endl;
}
uint16_t patt_a = ioreg.readInputRegister();
myusleep ( 1 );
uint16_t patt_r = ioreg.readInputRegister();
myusleep ( 1 );
int howmany(0);
while (patt_a != patt_r)
{
patt_a = ioreg.readInputRegister();
myusleep ( 1 );
patt_r = ioreg.readInputRegister();
myusleep ( 1 );
howmany ++;
if (howmany > 10) break;
}
if (howmany > 1) std::cout << " ************************** HOW MANY " << howmany << " ******************* " << endl;
ioreg.clearDataRegister();
bool isTrig_a = (patt_a & 1<<TRIGGER_OR_IN) != 0;
bool isTrig_r = (patt_r & 1<<TRIGGER_OR_IN) != 0;
if (1 && (isTrig_a != isTrig_r))
{
time_t tr = time(NULL) - tr0;
cerr << dec << tr << "(sec) " << trignum << hex << "(evt) ioreg A " << patt_a << " ioreg B " << patt_r
<< dec << " isTrig_a and isTrig_r differ " << isTrig_a << " " << isTrig_r << endl;
}
uint32_t tmask_a = patt_a & TRIGGER_MASK;
uint32_t tmask_r = patt_r & TRIGGER_MASK;
if (1 && (tmask_a != tmask_r))
{
time_t tr = time(NULL) - tr0;
cerr << dec << tr << "(sec) " << trignum << hex << "(evt) ioreg A " << patt_a << " ioreg B " << patt_r
<< dec << " tmask_a and tmask_r differ " << tmask_a << " " << tmask_r << endl;
}
bool spill_now = (tmask_a & 4);
if (is_spill != spill_now)
{
is_spill = spill_now;
if (is_spill)
{
spill_nr ++;
}
else
{
uint32_t triginspill, nowCts[4];
triginspill = trignum - prevTrig;
nowCts[0] = Tcts[0]-prevCts[0];
nowCts[1] = Tcts[1]-prevCts[1];
nowCts[2] = Tcts[2]-prevCts[2];
nowCts[3] = Tcts[3]-prevCts[3];
time_t tr = time(NULL) - tr0;
cout << " END of spill: " << dec << spill_nr << " at " << tr << "(sec) " << triginspill << "(evt) "
<< "beam/pede/inSpill/beamInSpill: "
<< nowCts[0] << "/" << nowCts[1] << "/" << nowCts[2] << "/" << nowCts[3] << endl;
prevTrig = trignum;
prevCts[0] = Tcts[0];
prevCts[1] = Tcts[1];
prevCts[2] = Tcts[2];
prevCts[3] = Tcts[3];
cout << " Tot events " << trignum << " readout time " << readoutTime << " rate " << double(trignum)/readoutTime << " Hz " << endl;
}
}
if (isTrig_a) do
{
if ((trignum % 10000) == 0)
{
time_t tr = time(NULL) - tr0;
double rate = trignum/double(tr);
double daqtime = xreadtime/double(trignum);
cout << dec << tr << "(sec) " << trignum << "(evt) "
<< "beam/pede/inSpill/beamInSpill: "
<< Tcts[0] << "/" << Tcts[1] << "/" << Tcts[2] << "/" << Tcts[3]
<< " daqtime " << daqtime << " "
<< rate << "(evt/sec) last isTrig " << isTrig_a << " tmask_a " << hex << tmask_a << endl;
}
if (tmask_a & 1) Tcts[0] ++; // physics triggers
if (tmask_a & 2) Tcts[1] ++; // pedestal triggers
if (tmask_a & 4) Tcts[2] ++; // in spill
if ((tmask_a & 5) == 5) Tcts[3] ++; // physics in spill
(Tcts[1]*10 < Tcts[0]) ? enablePedestals( ioreg ) : disablePedestals( ioreg );
uint32_t qdcsize, tdcsize;
struct timeval todb, toda;
gettimeofday ( &todb, NULL );
uint32_t retcode = readEvent ( nQDCs, qdcPtrs, xtdc0, trignum, myBuffer, &qdcsize, &tdcsize );
gettimeofday ( &toda, NULL );
double diff = double (toda.tv_sec - todb.tv_sec) + double (toda.tv_usec - todb.tv_usec)*1E-6;
readoutTime += diff;
uint32_t head = myBuffer[0];
if (qdcsize == 0) break;
uint32_t trail = myBuffer[qdcsize-1] & 0xff000000;
uint32_t evnum = numevofs + (myBuffer[qdcsize-1] & 0xffffff);
if (head != 0xfa002000)
{
cerr << dec << trignum << " Error in header " << hex << head << endl;
tmask_a |= 0xffff0000;
on_error = true;
disableTriggers( ioreg );
}
if (trail != 0xfc000000)
{
cerr << dec << trignum << " Error in trailer " << hex << trail << endl;
tmask_a |= 0xffff0000;
on_error = true;
disableTriggers( ioreg );
}
if (qdcsize != QDCtotsize)
{
cerr << dec << trignum << " Size error " << qdcsize << endl;
tmask_a |= 0xffff0000;
on_error = true;
disableTriggers( ioreg );
}
if (evnum != trignum)
{
cerr << dec << trignum << " Error in V792 event number " << evnum << endl;
tmask_a |= 0xffff0000;
on_error = true;
disableTriggers( ioreg );
}
if (retcode)
{
cerr << dec << trignum << " Error return code " << retcode << endl;
tmask_a |= 0xffff0000;
on_error = true;
disableTriggers( ioreg );
}
struct timeval tod;
gettimeofday ( &tod, NULL );
if (1) print_event ( ofs, trignum, tod, spill_nr, Tcts, tmask_a, myBuffer, qdcsize, tdcsize );
// while (trignum <= evnum) trignum ++; // evt_realign ( ioreg, qdc, Tcts, ofs, trignum );
trignum ++;
if (retcode == 0xff) // spurious trigger to all elx -- add one fake event to align VME data with FERS data
{
print_fake_event ( ofs, trignum, tod, spill_nr, Tcts, myBuffer );
trignum ++;
}
if (on_error)
{
// Re-init QDCs and TDCs
cerr << dec << trignum << " ********* resetting DAQ ********* " << endl;
struct timeval tod;
gettimeofday ( &tod, NULL );
struct tm * timeinfo = localtime ( &tod.tv_sec );
double dsecs = timeinfo->tm_sec + tod.tv_usec*1E-6;
cerr << " time is " << timeinfo->tm_yday << ":" << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << dsecs << endl;
tmask_a |= 0xffff0000;
resetHardware ( nQDCs, qdcPtrs, xtdc0 );
myusleep ( 100000 );
alignEventCounter ( trignum, nQDCs, qdcPtrs, xtdc0 );
numevofs = trignum;
myusleep ( 100000 );
cerr << dec << trignum << " ********* reenabling triggers ********* " << endl;
enableTriggers( ioreg );
myusleep ( 100000 );
on_error = false;
}
if (!exit_now) unlockTrigger( ioreg );
} while(0);
if (exit_now) break;
nsleep( NSLEEPT );
}
disableTriggers( ioreg );
ofs.close();
for (uint32_t j=0; j<nQDCs; j++) delete qdcPtrs[j];
time_t etime = time(NULL);
cout << " time is " << dec << etime << endl;
struct tm myetime;
localtime_r( &etime, &myetime );
cout << myetime.tm_year+1900 << "." << myetime.tm_mon+1 << "." << myetime.tm_mday << "."
<< myetime.tm_hour << ":" << myetime.tm_min << ":" << myetime.tm_sec << endl;
cout << asctime(&myetime) << endl;
etime -= 1600000000;
uint32_t tdiff = etime-stime;
double rate = trignum/double(tdiff);
cout << dec << " time is " << etime << " diff(sec) " << (etime-stime) << " events " << trignum
<< " rate(Hz) " << rate << endl;
cout << " ****************** end of run ****************** " << endl << endl;
return 0;
}