-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRawDataInput.cc
1922 lines (1670 loc) · 55.2 KB
/
RawDataInput.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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////
// RawDataInput
//
// Reads the files provided by the DAQ and converts them into ART
// Files containing Digit, DATEHeader and Trigger data Products.
//
////////////////////////////////////////////////////////////////////////
#include "RawDataInput.h"
namespace spd = spdlog;
double const next::RawDataInput::CLOCK_TICK_ = 0.025;
next::RawDataInput::RawDataInput()
{
verbosity_ = 0;
}
next::RawDataInput::RawDataInput(ReadConfig * config, HDF5Writer * writer) :
run_(0),
cfptr_(),
entriesThisFile_(-1),
event_(),
eventNo_(0),
skip_(config->skip()),
max_events_(config->max_events()),
buffer_(NULL),
dualChannels(48,0),
verbosity_(config->verbosity()),
headOut_(),
pmtDgts_(),
sipmDgts_(),
eventReader_(),
twoFiles_(config->two_files()),
discard_(config->discard()),
read_pmts_(config->readPmts()),
read_sipms_(config->readSipms()),
externalTriggerCh_(config->extTrigger()),
fileError_(0)
{
fMaxSample = 65536;
_log = spd::stdout_color_mt("rawdata");
_logerr = spd::stderr_color_mt("decoder");
if(verbosity_ > 0){
_log->set_level(spd::level::debug);
_logerr->set_level(spd::level::debug);
}
_writer = writer;
payloadSipm = (char*) malloc(MEMSIZE*NUM_FEC_SIPM);
config_ = config;
// Initialize huffman to NULL
huffmanPmt_.next[0] = NULL;
huffmanPmt_.next[1] = NULL;
huffmanSipm_.next[0] = NULL;
huffmanSipm_.next[1] = NULL;
}
Huffman* next::RawDataInput::getHuffmanTree(){
return &huffmanPmt_;
}
void next::RawDataInput::countEvents(std::FILE* file, int * nevents, int * firstEvt){
*nevents = 0;
*firstEvt = 0;
unsigned char * buffer;
*firstEvt = loadNextEvent(file, &buffer);
free(buffer);
int evt_number= *firstEvt;
while(evt_number > 0){
(*nevents)++;
evt_number = loadNextEvent(file, &buffer);
// If loadNextEvent enter in the first if, there is no malloc
// and then free will give a segfault
if (evt_number > 0){
free(buffer);
}
}
}
//Loads one event from the file
int next::RawDataInput::loadNextEvent(std::FILE* file, unsigned char ** buffer){
int evt_number = -1;
eventHeaderStruct header;
unsigned int headerSize = sizeof(eventHeaderStruct);
unsigned int readSize = this->readHeaderSize(file);
if ((readSize != headerSize) && readSize) { //if we read 0 is the end of the file
//TODO throw art::Exception(art::errors::FileReadError;)
//_logerr->error("Event header size of {} bytes read from data does not match expected size of {}", readSize, headerSize);
return -1;
}
size_t bytes_read = fread(&header, 1, headerSize, file);
while (bytes_read == headerSize){
if (!isEventSelected(header)){
fseek(file, header.eventSize-headerSize, SEEK_CUR);
}else{
*buffer = (unsigned char *) malloc(header.eventSize);
//Come back to the init of event
fseek(file, -(long)headerSize, SEEK_CUR);
bytes_read = fread(*buffer, 1, header.eventSize, file);
//bytes_read += fread(*buffer, 1, header.eventSize-(long)headerSize, file);
if (bytes_read != header.eventSize ){
//TODO throw art::Exception(art::errors::FileReadError)
_logerr->error("Unable to read event from file");
}
evt_number = EVENT_ID_GET_NB_IN_RUN(header.eventId);
//std::cout << "evt number: " << evt_number << std::endl;
break;
}
bytes_read = fread(&header, 1, headerSize, file);
}
return evt_number;
}
std::FILE* next::RawDataInput::openDATEFile(std::string const & filename){
_log->debug("Open file: {}", filename);
std::FILE* file = std::fopen(filename.c_str(), "rb");
if ( !file ){
_log->warn("Unable to open specified DATE file {}. Retrying in 10 seconds...", filename);
sleep(10);
file = std::fopen(filename.c_str(), "rb");
if ( !file ){
_logerr->error("Unable to open specified DATE file {}", filename);
fileError_ = true;
exit(-1);
}
_log->warn("File opened succesfully", filename);
}
return file;
}
void next::RawDataInput::readFile(std::string const & filename)
{
bool gdc2first = false;
int nevents1=0, firstEvtGDC1;
int nevents2=0, firstEvtGDC2;
std::FILE* file1 = NULL;
std::FILE* file2 = NULL;
std::string filename2 = filename;
file1 = openDATEFile(filename);
countEvents(file1, &nevents1, &firstEvtGDC1);
if (twoFiles_){
filename2.replace(filename2.find("gdc1"), 4, "gdc2");
_log->info("Reading from files {} and {}", filename, filename2);
file2 = openDATEFile(filename2);
countEvents(file2, &nevents2, &firstEvtGDC2);
//Check which gdc goes first
if (firstEvtGDC2 < firstEvtGDC1){
gdc2first = true;
}
}
entriesThisFile_ = nevents1 + nevents2;
if(max_events_ < entriesThisFile_){
entriesThisFile_ = max_events_;
}
if(twoFiles_){
_log->info("Total events: {}, Events in file1: {}, file2: {}", entriesThisFile_, nevents1, nevents2);
_log->debug("firstEvtGDC1: {}\t firstEvtGDC2: {}", firstEvtGDC1, firstEvtGDC2);
}else{
_log->info("Events in file = {}", entriesThisFile_);
_log->debug("firstEvt: {}", firstEvtGDC1);
}
//Rewind back to the start of the file
if (file1) fseek(file1, 0, SEEK_SET);
if (file2) fseek(file2, 0, SEEK_SET);
//TODO: Study what is this for here
eventReader_ = new EventReader(verbosity_);
fptr1_ = file1;
fptr2_ = file2;
if(twoFiles_ && gdc2first){
cfptr_ = fptr2_;
nextGdc2_ = true; //Changes in readNext()
}else{
cfptr_ = fptr1_;
nextGdc2_ = false;
}
}
bool next::RawDataInput::readNext()
{
event_ = 0;
bool toSkip = eventNo_ < skip_;
// If we're at the end of the file, don't advance
if ( eventNo_ == entriesThisFile_ ){
return false;
}
//Try 2 times. Usually first time will be ok, but if next
//event is missing, skip it and go on with the next file
for(int files=0;files<2;files++){
//Choose file
if(twoFiles_){
if (nextGdc2_){
cfptr_ = fptr2_;
}else{
cfptr_ = fptr1_;
}
//Change file for next time
nextGdc2_ = !nextGdc2_;
}
int evt_number = loadNextEvent(cfptr_, &buffer_);
if (evt_number > 0){
event_ = (eventHeaderStruct*) buffer_;
eventNo_++;
if(!toSkip){
for(int indexSipm=0;indexSipm<NUM_FEC_SIPM;indexSipm++){
sipmFec[indexSipm] = false;
}
bool result = ReadDATEEvent();
if (result){
if(!eventError_ && discard_){
writeEvent();
}
freeWaveformMemory(&*pmtDgts_);
freeWaveformMemory(&*sipmDgts_);
}
free(buffer_);
return result;
}
//Unless error (missing event), we should read only one event at a time
break;
}
}
return eventNo_ < entriesThisFile_;
}
bool isEventSelected(eventHeaderStruct& event){
return event.eventType == PHYSICS_EVENT ||
event.eventType == CALIBRATION_EVENT;
}
bool next::RawDataInput::ReadDATEEvent()
{
eventHeaderStruct* subEvent = nullptr;
equipmentHeaderStruct * equipment = nullptr;
unsigned char* position = nullptr;
unsigned char* end = nullptr;
int count = 0;
fFirstFT=0; /// Position in the buffer of the first FT
fFecId=0; ///ID of the Front End Card
eventError_ = false;
//Num FEB
std::fill(sipmPosition, sipmPosition+NSIPMS,-1);
std::fill(sipmLastValues, sipmLastValues+NSIPMS,0);
std::fill(pmtPosition, pmtPosition+NPMTS,-1);
// Reset the output pointers.
pmtDgts_.reset(new DigitCollection);
sipmDgts_.reset(new DigitCollection);
trigOut_.clear();
triggerChans_.clear();
if (!event_) return false;
// now fill DATEEventHeader
headOut_.reset(new EventHeaderCollection);
(*headOut_).emplace_back();
auto myheader = (*headOut_).rbegin();
myheader->SetEventSize(event_->eventSize);
myheader->SetEventMagic(event_->eventMagic);
myheader->SetEventHeadSize(event_->eventHeadSize);
myheader->SetEventVersion(event_->eventVersion);
myheader->SetEventType(event_->eventType);
myheader->SetRunNb(event_->eventRunNb);
myheader->SetNbInRun(EVENT_ID_GET_NB_IN_RUN(event_->eventId));
_log->info("Event number: {}", myheader->NbInRun());
myheader->SetBurstNb( EVENT_ID_GET_BURST_NB(event_->eventId));
myheader->SetNbInBurst( EVENT_ID_GET_NB_IN_BURST(event_->eventId));
// check whether there are sub events
if (event_->eventSize <= event_->eventHeadSize) return false;
do {
if (count > 0) position += count;
// get the first or the next equipment if at the end of an equipment
if (!equipment || (position >= end)) {
equipment = nullptr;
// get the first or the next sub event if at the end of a sub event
if (!subEvent ||
(position >= ((unsigned char*)subEvent) + subEvent->eventSize)) {
// check for end of event data
if (position >= ((unsigned char*)event_)+event_->eventSize){
// Ready for save, return successful read.
return true;
}
if (!TEST_SYSTEM_ATTRIBUTE(event_->eventTypeAttribute,
ATTR_SUPER_EVENT)) {
subEvent = event_; // no super event
} else if (subEvent) {
subEvent = (eventHeaderStruct*) (((unsigned char*)subEvent) +
subEvent->eventSize);
} else {
subEvent = (eventHeaderStruct*) (((unsigned char*)event_) +
event_->eventHeadSize);
}
if ( verbosity_ >= 2 ) {
_log->debug("subEvent ldcId: {}", subEvent->eventLdcId);
_log->debug("subEvent eventSize: {}", subEvent->eventSize);
}
// check the magic word of the sub event
if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
_logerr->error("NEXT1ELEventHandle::ReadDATEEvent, wrong magic number in sub event!");
return false;
}
// continue if no data in the subevent
if (subEvent->eventSize == subEvent->eventHeadSize) {
position = end = ((unsigned char*)subEvent) + subEvent->eventSize;
continue;
}
equipment = (equipmentHeaderStruct*)
(((unsigned char*)subEvent) + subEvent->eventHeadSize);
} else {
equipment = (equipmentHeaderStruct*) end;
}
position = ((unsigned char*)equipment) + sizeof(equipmentHeaderStruct);
if (subEvent->eventVersion <= 0x00030001) {
end = position + equipment->equipmentSize;
} else {
end = ((unsigned char*)equipment) + equipment->equipmentSize;
}
}
// continue with the next sub event if no data left in the payload
if (position >= end){
printf("Going to next subevent \n");
continue;
}
if ( verbosity_ >= 2 ) {
_log->debug("equipmentSize: {}", equipment->equipmentSize);
_log->debug("equipmentType: {}", equipment->equipmentType);
_log->debug("equipmentId: {}", equipment->equipmentId);
_log->debug("equipmentBasicElementSize: {}", equipment->equipmentBasicElementSize);
}
unsigned char *buffer = position;
unsigned int size = equipment->equipmentSize - sizeof(equipmentHeaderStruct);
//////////Getting firmware version: foxtrot, golf, etc //////////////////
//
//Flip words
int16_t * buffer_cp = (int16_t*) buffer;
int16_t * payload_flip = (int16_t *) malloc(MEMSIZE);
int16_t * payload_flip_free = payload_flip;
flipWords(size, buffer_cp, payload_flip);
// for(int i=0; i<100; i++){
// printf("payload[%d] = 0x%04x\n", i, payload_flip[i]);
// }
eventReader_->ReadCommonHeader(payload_flip);
fwVersion = eventReader_->FWVersion();
int FECtype = eventReader_->FecType();
if(eventReader_->TriggerCounter() != myheader->NbInRun()){
_logerr->error("EventID ({}) & TriggerCounter ({}) mismatch, possible loss of data in DATE in FEC {}", myheader->NbInRun(), eventReader_->TriggerCounter(), eventReader_->FecId());
}else{
if(verbosity_ >= 2){
_log->debug("EventID ({}) & TriggerCounter ({}) ok in FEC {}", myheader->NbInRun(), eventReader_->TriggerCounter(), eventReader_->FecId());
}
}
//FWVersion HOTEL
if (fwVersion == 8){
if (FECtype==0){
if( verbosity_ >= 1 ){
_log->debug("This is a PMT FEC");
}
// for(unsigned int i=0; i<pmtDgts_->size(); i++){
// if((*pmtDgts_)[i].active()){
// std::cout << (*pmtDgts_)[i].nSamples() << " before read pmtDgts " << (*pmtDgts_)[i].chID() << "\t charge[0]: " << (*pmtDgts_)[i].waveform()[0] << std::endl;
// }
// }
ReadHotelPmt(payload_flip,size);
fwVersionPmt = fwVersion;
// for(unsigned int i=0; i<pmtDgts_->size(); i++){
// if((*pmtDgts_)[i].active()){
// std::cout << (*pmtDgts_)[i].nSamples() << "after read pmtDgts " << (*pmtDgts_)[i].chID() << "\t charge[0]: " << (*pmtDgts_)[i].waveform()[0] << std::endl;
// }
// }
}else if (FECtype==2){
if( verbosity_ >= 1 ){
_log->debug("This is a Trigger FEC");
}
ReadHotelTrigger(payload_flip,size);
}else if (FECtype==1){
if( verbosity_ >= 1 ){
_log->debug("This is a SIPM FEC");
}
ReadHotelSipm(payload_flip, size);
}
}
//FWVersion INDIA
if (fwVersion == 9){
if (FECtype==0){
if( verbosity_ >= 1 ){
_log->debug("This is a PMT FEC");
}
ReadIndiaJuliettPmt(payload_flip,size);
fwVersionPmt = fwVersion;
}else if (FECtype==2){
if( verbosity_ >= 1 ){
_log->debug("This is a Trigger FEC");
}
ReadIndiaTrigger(payload_flip,size);
}else if (FECtype==1){
if( verbosity_ >= 1 ){
_log->debug("This is a SIPM FEC");
}
ReadHotelSipm(payload_flip, size);
}
}
//FWVersion JULIETT
if (fwVersion == 10){
if (FECtype==0){
if( verbosity_ >= 1 ){
_log->debug("This is a PMT FEC");
}
if (read_pmts_){
ReadIndiaJuliettPmt(payload_flip,size);
}
fwVersionPmt = fwVersion;
}else if (FECtype==2){
if( verbosity_ >= 1 ){
_log->debug("This is a Trigger FEC");
}
ReadIndiaTrigger(payload_flip,size);
}else if (FECtype==1){
if( verbosity_ >= 1 ){
_log->debug("This is a SIPM FEC");
}
if (read_sipms_){
ReadHotelSipm(payload_flip, size);
}
}
}
count = end - position;
free(payload_flip_free);
}while(1);
return true;
}
void flipWords(unsigned int size, int16_t* in, int16_t* out){
unsigned int pos_in = 0, pos_out = 0;
// This will stop just before FAFAFAFA, usually there are FFFFFFFF before
// With compression mode there could be some FAFAFAFA along the data
// The pattern FF...FFFF FAFAFAFA is a valid one 0,0,0.... 8,8,8,8...
// The only way to stop safely is to count the words.
// Each 16-bit word has 2 bytes, therefore pos_in*2 in the condition
while((pos_in*2 < size)) {
//Size taken empirically from data (probably due to
//UDP headers and/or DATE)
if (pos_in > 0 && pos_in % 3996 == 0){
pos_in += 2;
}
out[pos_out] = in[pos_in+1];
out[pos_out+1] = in[pos_in];
// if(size < 100){
// printf("out[%d]: 0x%04x\n", pos_out, out[pos_out]);
// printf("out[%d]: 0x%04x\n", pos_out+1, out[pos_out+1]);
// }
pos_in += 2;
pos_out += 2;
}
// printf("pos_in: %d, size: %d\n", pos_in, size);
}
void next::RawDataInput::ReadIndiaTrigger(int16_t * buffer, unsigned int size){
int BufferSamples = eventReader_->BufferSamples();
triggerType_ = eventReader_->TriggerType();
if(verbosity_ >= 2){
for(int dbg=0;dbg<9;dbg++){
_log->debug("TrgConf[{}] = 0x{:04x}", dbg, buffer[dbg]);
//printf("TrgConf[%d] = 0x%04x\n", dbg, buffer[dbg]);
}
_log->debug("Ch info:");
for(int dbg=0;dbg<4;dbg++){
_log->debug("Ch info[{}] = 0x{:04x}", dbg, buffer[dbg+6]);
// printf("Ch info[%d] = 0x%04x\n", dbg, buffer[dbg+6]);
}
for(int dbg=0;dbg<4;dbg++){
_log->debug("Trigger lost[{}] = 0x{:04x}", dbg, buffer[dbg+12]);
// printf("Trigger lost[%d] = 0x%04x\n", dbg, buffer[dbg+12]);
}
}
//TRG conf 8
int triggerMask = (*buffer & 0x003FF) << 16;
buffer++;
//TRG conf 7
triggerMask = triggerMask | (*buffer & 0x0FFFF);
buffer++;
//TRG conf 6
int triggerDiff1 = *buffer & 0x0FFFF;
buffer++;
//TRG conf 5
int triggerDiff2 = *buffer & 0x0FFFF;
buffer++;
//TRG conf 4
int triggerWindowA1 = *buffer & 0x003f;
int triggerChanA1 = (*buffer & 0x01FC0) >> 6;
bool autoTrigger = (*buffer & 0x02000) >> 13;
bool dualTrigger = (*buffer & 0x04000) >> 14;
bool externalTrigger = (*buffer & 0x08000) >> 15;
buffer++;
//TRG conf 3
int triggerWindowB1 = *buffer & 0x003f;
int triggerChanB1 = (*buffer & 0x01FC0) >> 6;
bool mask = (*buffer & 0x02000) >> 13;
bool triggerB2 = (*buffer & 0x04000) >> 14;
bool triggerB1 = (*buffer & 0x08000) >> 15;
buffer++;
//TRG conf 2
int triggerWindowA2 = *buffer & 0x003f;
int triggerChanA2 = (*buffer & 0x01FC0) >> 6;
buffer++;
//TRG conf 1
int triggerWindowB2 = *buffer & 0x003f;
int triggerChanB2 = (*buffer & 0x01FC0) >> 6;
buffer++;
//TRG conf 0
int triggerExtN = *buffer & 0x000F;
int triggerIntN = (*buffer & 0x0FFF0) >> 4;
buffer++;
//Trigger type
int triggerType = (*buffer & 0x0FFFF) >> 15;
buffer++;
//Channels producing trigger
int activePMT = 0;
int channelNumber = 47;
for (int chinfo = 0; chinfo < 3; chinfo++){
for (int j=15;j>=0;j--){
activePMT = 0;
activePMT = CheckBit(*buffer & 0x0FFFF, j);
if (activePMT > 0){
if( verbosity_ >= 2){
_log->debug("PMT Number {} is {}", channelNumber, activePMT);
}
//printf("ch %d: %d\n", channelNumber, activePMT);
triggerChans_.push_back(channelNumber);
}
channelNumber--;
}
buffer++;
}
//Trigger lost type 2
int triggerLost2 = (*buffer & 0x0FFFF) << 16;
buffer++;
triggerLost2 = triggerLost2 | (*buffer & 0x0FFFF);
buffer++;
//Trigger lost type 1
int triggerLost1 = (*buffer & 0x0FFFF) << 16;
buffer++;
triggerLost1 = triggerLost1 | (*buffer & 0x0FFFF);
buffer++;
if(verbosity_ >= 2){
_log->debug("triggerMask: 0x{:04x}\n", triggerMask);
_log->debug("triggerDiff1: 0x{:04x}\n", triggerDiff1);
_log->debug("triggerDiff2: 0x{:04x}\n", triggerDiff2);
_log->debug("Trg Chan A 1: 0x{:04x}\n", triggerChanA1);
_log->debug("Trg Window A 1: 0x{:04x}\n", triggerWindowA1);
_log->debug("autoTrigger: 0x{:04x}04x\n", autoTrigger);
_log->debug("dualTrigger: 0x{:04x}\n", dualTrigger);
_log->debug("externalTrigger: 0x{:04x}\n", externalTrigger);
_log->debug("Trg Chan B 1: 0x{:04x}\n", triggerChanB1);
_log->debug("Trg Window B 1: 0x{:04x}\n", triggerWindowB1);
_log->debug("mask: 0x{:04x}\n", mask);
_log->debug("triggerB2: 0x{:04x}\n", triggerB2);
_log->debug("triggerB1: 0x{:04x}\n", triggerB1);
_log->debug("Trg Chan A 2: 0x{:04x}\n", triggerChanA2);
_log->debug("Trg Window A 2: 0x{:04x}\n", triggerWindowA2);
_log->debug("Trg Chan B 2: 0x{:04x}\n", triggerChanB2);
_log->debug("Trg Window B 2: 0x{:04x}\n", triggerWindowB2);
_log->debug("triggerIntN: 0x{:04x}\n", triggerIntN);
_log->debug("triggerExtN: 0x{:04x}\n", triggerExtN);
_log->debug("triggerType: 0x{:04x}\n", triggerType);
_log->debug("triggerLost2: 0x{:04x}\n", triggerLost2);
_log->debug("triggerLost1: 0x{:04x}\n", triggerLost1);
}
trigOut_.push_back(std::make_pair("triggerType", triggerType));
trigOut_.push_back(std::make_pair("triggerLost1", triggerLost1));
trigOut_.push_back(std::make_pair("triggerLost2", triggerLost2));
trigOut_.push_back(std::make_pair("triggerMask", triggerMask));
trigOut_.push_back(std::make_pair("triggerDiff1", triggerDiff1));
trigOut_.push_back(std::make_pair("triggerDiff2", triggerDiff2));
trigOut_.push_back(std::make_pair("autoTrigger", autoTrigger));
trigOut_.push_back(std::make_pair("dualTrigger", dualTrigger));
trigOut_.push_back(std::make_pair("externalTrigger", externalTrigger));
trigOut_.push_back(std::make_pair("mask", mask));
trigOut_.push_back(std::make_pair("triggerB2", triggerB2));
trigOut_.push_back(std::make_pair("triggerB1", triggerB1));
trigOut_.push_back(std::make_pair("chanA1", triggerChanA1));
trigOut_.push_back(std::make_pair("chanA2", triggerChanA2));
trigOut_.push_back(std::make_pair("chanB1", triggerChanB1));
trigOut_.push_back(std::make_pair("chanB2", triggerChanB2));
trigOut_.push_back(std::make_pair("windowA1", triggerWindowA1));
trigOut_.push_back(std::make_pair("windowB1", triggerWindowB1));
trigOut_.push_back(std::make_pair("windowA2", triggerWindowA2));
trigOut_.push_back(std::make_pair("windowB2", triggerWindowB2));
trigOut_.push_back(std::make_pair("triggerIntN", triggerIntN));
trigOut_.push_back(std::make_pair("triggerExtN", triggerExtN));
}
void next::RawDataInput::ReadHotelTrigger(int16_t * buffer, unsigned int size){
int temp = 0;
int BufferSamples = eventReader_->BufferSamples();
if(verbosity_ >= 2){
for(int dbg=0;dbg<6;dbg++){
_log->debug("TrgConf[{}] = 0x{:04x}", dbg, buffer[dbg]);
}
_log->debug("Ch info:");
for(int dbg=0;dbg<4;dbg++){
_log->debug("Ch info[{}] = 0x{:04x}", dbg, buffer[dbg+6]);
}
}
// Read new fields in the header (TRG conf)
//TRG conf 5
temp = (*buffer+0x10000) & 0x0FFFF;
int triggerMask = ((temp & 0x003FF) << 16);
int triggerCode = (temp & 0x0FC00) >> 10;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("triggerCode: {:x}", triggerCode);
}
//TRG conf 4
buffer++;
temp = (*buffer+0x10000) & 0x0FFFF;
triggerMask = triggerMask + temp;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("triggerMask: {:x}", triggerMask);
}
//TRG conf 3
buffer++;
temp = (*buffer+0x10000) & 0x0FFFF;
int triggerDiff = temp;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("triggerDiff: {:x}", triggerDiff);
}
//TRG conf 2
buffer++;
temp = (*buffer+0x10000) & 0x0FFFF;
int triggerWindow1 = temp & 0x003f;
int triggerChan1 = (temp & 0x1FC0) >> 6;
bool autoTrigger = (temp & 0x02000) >> 13;
bool dualTrigger = (temp & 0x04000) >> 14;
bool externalTrigger = (temp & 0x08000) >> 15;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("autoTrigger: {:x}", autoTrigger);
_log->debug("dualTrigger: {:x}", dualTrigger);
_log->debug("externalTrigger: {:x}", externalTrigger);
_log->debug("triggerWindow1: {:x}", triggerWindow1);
_log->debug("triggerChan1: {:x}", triggerChan1);
}
//TRG conf 1
buffer++;
temp = (*buffer+0x10000) & 0x0FFFF;
int triggerWindow2 = temp & 0x003f;
int triggerChan2 = (temp & 0x1FC0) >> 6;
bool mask = (temp & 0x04000) >> 14;
bool twoTrigger = (temp & 0x08000) >> 15;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("mask: {:x}", mask);
_log->debug("twoTrigger: {:x}", twoTrigger);
_log->debug("triggerWindow2: {:x}", triggerWindow2);
_log->debug("triggerChan2: {:x}", triggerChan2);
}
//TRG conf 0
buffer++;
temp = (*buffer+0x10000) & 0x0FFFF;
int triggerIntN = (temp & 0x0FFF0) >> 4;
int triggerExtN = temp & 0x000F;
if(verbosity_ >= 2){
_log->debug("temp = 0x{:04x}", temp);
_log->debug("triggerIntN: {:x}", triggerIntN);
_log->debug("triggerExtN: {:x}", triggerExtN);
}
buffer++;
int activePMT = 0;
int channelNumber = 63;
for (int chinfo = 0; chinfo < 4; chinfo++){
for (int j=15;j>=0;j--){
activePMT = 0;
temp = (buffer[chinfo] + 0x10000) & 0x0FFFF;
activePMT = CheckBit(temp, j);
if (activePMT > 0){
if( verbosity_ >= 2){
_log->debug("PMT Number {} is {}", channelNumber, activePMT);
}
triggerChans_.push_back(channelNumber);
}
channelNumber--;
}
}
trigOut_.push_back(std::make_pair("triggerCode", triggerCode));
trigOut_.push_back(std::make_pair("triggerMask", triggerMask));
trigOut_.push_back(std::make_pair("triggerDiff", triggerDiff));
trigOut_.push_back(std::make_pair("autoTrigger", autoTrigger));
trigOut_.push_back(std::make_pair("dualTrigger", dualTrigger));
trigOut_.push_back(std::make_pair("externalTrigger", externalTrigger));
trigOut_.push_back(std::make_pair("mask", mask));
trigOut_.push_back(std::make_pair("channel1", triggerChan1));
trigOut_.push_back(std::make_pair("channel2", triggerChan2));
trigOut_.push_back(std::make_pair("window1", triggerWindow1));
trigOut_.push_back(std::make_pair("window2", triggerWindow2));
trigOut_.push_back(std::make_pair("triggerIntN", triggerIntN));
trigOut_.push_back(std::make_pair("triggerExtN", triggerExtN));
}
void next::RawDataInput::ReadHotelPmt(int16_t * buffer, unsigned int size){
double timeinmus = 0.;
int time = -1;
fFecId = eventReader_->FecId();
eventTime_ = eventReader_->TimeStamp();
int ZeroSuppression = eventReader_->ZeroSuppression();
int Baseline = eventReader_->Baseline();
fPreTrgSamples = eventReader_->PreTriggerSamples();
int BufferSamples = eventReader_->BufferSamples();
int TriggerFT = eventReader_->TriggerFT();
int FTBit = eventReader_->GetFTBit();
int ErrorBit = eventReader_->GetErrorBit();
int FWVersion = eventReader_->FWVersion();
if (ErrorBit){
auto myheader = (*headOut_).rbegin();
_logerr->error("Event {} ErrorBit is {}, fec: {}", myheader->NbInRun(), ErrorBit, fFecId);
fileError_ = true;
eventError_ = true;
if(discard_){
return;
}
}
// Get here channel numbers & dualities
unsigned int TotalNumberOfPMTs = setDualChannels(eventReader_);
///Reading the payload
fFirstFT = TriggerFT;
timeinmus = 0 - CLOCK_TICK_;
if(ZeroSuppression){
int singleBuff = TriggerFT + FTBit*fMaxSample;
int trigDiff = BufferSamples - fPreTrgSamples;
fFirstFT = (singleBuff + trigDiff) % BufferSamples;
}
int nextFT = -1; //At start we don't know next FT value
int nextFThm = -1;
//Map febid -> channelmask
std::map<int, std::vector<int> > fec_chmask;
fec_chmask.emplace(fFecId, std::vector<int>());
int ChannelMask = eventReader_->ChannelMask();
pmtsChannelMask(ChannelMask, fec_chmask[fFecId], fFecId, FWVersion);
//Create digits and waveforms for active channels
CreatePMTs(&*pmtDgts_, pmtPosition, &(fec_chmask[fFecId]), BufferSamples, ZeroSuppression);
setActiveSensors(&(fec_chmask[fFecId]), &*pmtDgts_, pmtPosition);
for(unsigned int i=0; i<pmtDgts_->size(); i++){
int elecID = (*pmtDgts_)[i].chID();
int pos = pmtPosition[elecID];
// printf("index: %d, elecID: %d, position: %d\n", i, elecID, pos);
}
///Write pedestal
if(Baseline){
writePmtPedestals(eventReader_, &*pmtDgts_, &(fec_chmask[fFecId]), pmtPosition);
}
//TODO maybe size of payload could be used here to stop, but the size is
//2x size per link and there are manu FFFF at the end, which are the actual
//stop condition...
while (true){
int FT = *buffer & 0x0FFFF;
//printf("FT: 0x%04x\n", FT);
timeinmus = timeinmus + CLOCK_TICK_;
time++;
buffer++;
//TODO ZS
if(ZeroSuppression){
// for(int i=0; i<6; i++){
// printf("buffer[%d] = 0x%04x\n", i, buffer[i]);
// }
//stop condition
//printf("nextword = 0x%04x\n", *buffer);
if(FT == 0x0FFFF){
break;
}
// Read new FThm bit and channel mask
int ftHighBit = (*buffer & 0x8000) >> 15;
ChannelMask = (*buffer & 0x0FF0) >> 4;
//printf("fthbit: %d, chmask: %d\n", ftHighBit, ChannelMask);
pmtsChannelMask(ChannelMask, fec_chmask[fFecId], fFecId, FWVersion);
// for(int i=0; i<fec_chmask[fFecId].size(); i++){
// printf("pmt channel: %d\n", fec_chmask[fFecId][i]);
// }
FT = FT + ftHighBit*fMaxSample - fFirstFT;
if ( FT < 0 ){
FT += BufferSamples;
}
timeinmus = FT * CLOCK_TICK_;
// printf("timeinmus: %lf\n", timeinmus);
// printf("FT: %d\n", FT);
decodeChargeHotelPmtZS(buffer, *pmtDgts_, fec_chmask[fFecId], pmtPosition, FT);
}else{
//If not ZS check next FT value, if not expected (0xffff) end of data
if(!ZeroSuppression){
computeNextFThm(&nextFT, &nextFThm, eventReader_);
if(FT != (nextFThm & 0x0FFFF)){
if( verbosity_ >= 2 ){
_log->debug("nextFThm != FT: 0x{:04x}, 0x{:04x}", (nextFThm&0x0ffff), FT);
}
break;
}
}
//decodeCharge(buffer, *pmtDgts_, fec_chmask[fFecId], pmtPosition, timeinmus);
decodeCharge(buffer, *pmtDgts_, fec_chmask[fFecId], pmtPosition, time);
}
}
// for(unsigned int i=0; i<pmtDgts_->size(); i++){
// if((*pmtDgts_)[i].active()){
// std::cout << (*pmtDgts_)[i].nSamples() << " hotel pmtDgts " << (*pmtDgts_)[i].chID() << "\t charge[0]: " << (*pmtDgts_)[i].waveform()[0] << std::endl;
// }
// }
}
void next::RawDataInput::ReadIndiaJuliettPmt(int16_t * buffer, unsigned int size){
int time = -1;
int current_bit = 31;
fFecId = eventReader_->FecId();
eventTime_ = eventReader_->TimeStamp();
triggerType_ = eventReader_->TriggerType();
int ZeroSuppression = eventReader_->ZeroSuppression();
int Baseline = eventReader_->Baseline();
fPreTrgSamples = eventReader_->PreTriggerSamples();
int BufferSamples = eventReader_->BufferSamples();
if (eventReader_->FWVersion() == 10){
if (eventReader_->TriggerType() >= 8){
fPreTrgSamples = eventReader_->PreTriggerSamples2();
BufferSamples = eventReader_->BufferSamples2();
}
}
int TriggerFT = eventReader_->TriggerFT();
int FTBit = eventReader_->GetFTBit();
int ErrorBit = eventReader_->GetErrorBit();
int FWVersion = eventReader_->FWVersion();
if (ZeroSuppression){
if ((!huffmanPmt_.next[0]) && (!huffmanPmt_.next[1])){
auto myheader = (*headOut_).rbegin();
run_ = myheader->RunNb();
getHuffmanFromDB(config_, &huffmanPmt_, run_, HuffmannSensor::pmt);
if( verbosity_ >= 1 ){
_log->debug("Huffman tree:\n");
print_huffman(_log, &huffmanPmt_, 1);
}
}
}
if (ErrorBit){
auto myheader = (*headOut_).rbegin();
_logerr->error("Event {} ErrorBit is {}, fec: {}", myheader->NbInRun(), ErrorBit, fFecId);
fileError_ = true;
eventError_ = true;
if(discard_){
return;
}
}
// Get here channel numbers & dualities
unsigned int TotalNumberOfPMTs = setDualChannels(eventReader_);
///Reading the payload
fFirstFT = TriggerFT;
int nextFT = -1; //At start we don't know next FT value
int nextFThm = -1;
//Map febid -> channelmask
std::map<int, std::vector<int> > fec_chmask;
fec_chmask.emplace(fFecId, std::vector<int>());
int ChannelMask = eventReader_->ChannelMask();
pmtsChannelMask(ChannelMask, fec_chmask[fFecId], fFecId, FWVersion);
//Create digits and waveforms for active channels
CreatePMTs(&*pmtDgts_, pmtPosition, &(fec_chmask[fFecId]), BufferSamples, ZeroSuppression);
setActiveSensors(&(fec_chmask[fFecId]), &*pmtDgts_, pmtPosition);
for(unsigned int i=0; i<pmtDgts_->size(); i++){
int elecID = (*pmtDgts_)[i].chID();
int pos = pmtPosition[elecID];
}
///Write pedestal
if(Baseline){
writePmtPedestals(eventReader_, &*pmtDgts_, &(fec_chmask[fFecId]), pmtPosition);
}
//TODO maybe size of payload could be used here to stop, but the size is
//2x size per link and there are manu FFFF at the end, which are the actual
//stop condition...
while (true){
// timeinmus = timeinmus + CLOCK_TICK_;
time++;
if(ZeroSuppression){
// Skip FTm
if (time == 0) {
buffer++;
}
if (time == BufferSamples){
break;
}
decodeChargeIndiaPmtCompressed(buffer, ¤t_bit, *pmtDgts_, &huffmanPmt_, fec_chmask[fFecId], pmtPosition, time);
}else{
int FT = *buffer & 0x0FFFF;