-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathCompressor.cxx
More file actions
1515 lines (1327 loc) · 53.6 KB
/
Compressor.cxx
File metadata and controls
1515 lines (1327 loc) · 53.6 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file Compressor.h
/// @author Roberto Preghenella
/// @since 2019-12-18
/// @brief TOF raw data compressor
#include "TOFCompression/Compressor.h"
#include "TOFBase/Geo.h"
#include "DetectorsRaw/RDHUtils.h"
#include <fairlogger/Logger.h>
#include <cstring>
#include <iostream>
// o2::ctf::CTFIOSize iosize;
#define ENCODER_PARANOID
//#define CHECKER_COUNTER
#ifdef DECODER_PARANOID
#warning "Building code with DecoderParanoid option. This may limit the speed."
#endif
#ifdef CHECKER_COUNTER
#warning "Building code with CheckerCounter option. This may limit the speed."
#endif
#define colorReset "\033[0m"
#define colorRed "\033[1;31m"
#define colorGreen "\033[1;32m"
#define colorYellow "\033[1;33m"
#define colorBlue "\033[1;34m"
// decoding macros
#define IS_DRM_COMMON_HEADER(x) ((x & 0xF0000000) == 0x40000000)
#define IS_DRM_GLOBAL_HEADER(x) ((x & 0xF000000F) == 0x40000001)
#define IS_DRM_GLOBAL_TRAILER(x) ((x & 0xF000000F) == 0x50000001)
#define IS_LTM_GLOBAL_HEADER(x) ((x & 0xF000000F) == 0x40000002)
#define IS_LTM_GLOBAL_TRAILER(x) ((x & 0xF000000F) == 0x50000002)
#define IS_TRM_GLOBAL_HEADER(x) ((x & 0xF0000000) == 0x40000000)
#define IS_TRM_GLOBAL_TRAILER(x) ((x & 0xF0000003) == 0x50000003)
#define IS_TRM_CHAINA_HEADER(x) ((x & 0xF0000000) == 0x00000000)
#define IS_TRM_CHAINA_TRAILER(x) ((x & 0xF0000000) == 0x10000000)
#define IS_TRM_CHAINB_HEADER(x) ((x & 0xF0000000) == 0x20000000)
#define IS_TRM_CHAINB_TRAILER(x) ((x & 0xF0000000) == 0x30000000)
#define IS_TRM_CHAIN_TRAILER(x, c) ((x & 0xF0000000) == (c == 0 ? 0x10000000 : 0x30000000))
#define IS_TDC_ERROR(x) ((x & 0xF0000000) == 0x60000000)
#define IS_FILLER(x) ((x & 0xFFFFFFFF) == 0x70000000)
#define IS_TDC_HIT(x) ((x & 0x80000000) == 0x80000000)
#define IS_TDC_HIT_LEADING(x) ((x & 0xA0000000) == 0xA0000000)
#define IS_TDC_HIT_TRAILING(x) ((x & 0xC0000000) == 0xC0000000)
#define IS_DRM_TEST_WORD(x) ((x & 0xF000000F) == 0xE000000F)
// DRM getters
#define GET_DRMDATAHEADER_DRMID(x) DRM_DRMID(x)
#define GET_DRMDATAHEADER_EVENTWORDS(x) DRM_EVWORDS(x)
#define GET_DRMHEADW1_PARTSLOTMASK(x) DRM_SLOTID(x)
#define GET_DRMHEADW1_CLOCKSTATUS(x) DRM_CLKFLG(x)
#define GET_DRMHEADW1_DRMHVERSION(x) DRM_VERSID(x)
#define GET_DRMHEADW1_DRMHSIZE(x) DRM_HSIZE(x)
#define GET_DRMHEADW2_ENASLOTMASK(x) DRM_ENABLEID(x)
#define GET_DRMHEADW2_FAULTSLOTMASK(x) DRM_FAULTID(x)
#define GET_DRMHEADW2_READOUTTIMEOUT(x) DRM_RTMO(x)
#define GET_DRMHEADW3_GBTBUNCHCNT(x) DRM_BCGBT(x)
#define GET_DRMHEADW3_LOCBUNCHCNT(x) DRM_BCLOC(x)
#define GET_DRMHEADW5_EVENTCRC(x) DRM_EVCRC(x)
#define GET_DRMDATATRAILER_LOCEVCNT(x) DRM_LOCEVCNT(x)
// LTM getters
#define GET_LTMDATAHEADER_EVENTWORDS(x) LTM_EVENTSIZE(x)
// TRM getters
#define GET_TRMDATAHEADER_SLOTID(x) TOF_GETGEO(x)
#define GET_TRMDATAHEADER_EVENTCNT(x) TRM_EVCNT_GH(x)
#define GET_TRMDATAHEADER_EVENTWORDS(x) TRM_EVWORDS(x)
#define GET_TRMDATAHEADER_EMPTYBIT(x) TRM_EMPTYBIT(x)
#define GET_TRMDATATRAILER_LUTERRORBIT(x) TRM_LUTERRBIT(x)
#define GET_TRMDATATRAILER_EVENTCRC(x) TRM_EVCRC2(x)
// TRM Chain getters
#define GET_TRMCHAINHEADER_SLOTID(x) TOF_GETGEO(x)
#define GET_TRMCHAINHEADER_BUNCHCNT(x) TRM_BUNCHID(x)
#define GET_TRMCHAINTRAILER_EVENTCNT(x) TRM_EVCNT_CT(x)
#define GET_TRMCHAINTRAILER_STATUS(x) TRM_CHAINSTAT(x)
// TDC getters
#define GET_TRMDATAHIT_TIME(x) TRM_TIME(x)
#define GET_TRMDATAHIT_CHANID(x) TRM_CHANID(x)
#define GET_TRMDATAHIT_TDCID(x) TRM_TDCID(x)
#define GET_TRMDATAHIT_EBIT(x) ((x & 0x10000000) >> 28)
namespace o2
{
namespace tof
{
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::processHBF()
{
if (verbose && mDecoderVerbose) {
std::cout << colorBlue
<< "--- PROCESS HBF"
<< colorReset
<< std::endl;
}
mDecoderRDH = reinterpret_cast<const RDH*>(mDecoderPointer);
mEncoderRDH = reinterpret_cast<RDH*>(mEncoderPointer);
auto rdh = mDecoderRDH;
if (!o2::raw::RDHUtils::checkRDH(rdh, false)) {
LOG(warning) << "Bad RDH found in TOF compressor -> skip it";
o2::raw::RDHUtils::checkRDH(rdh, true);
return true;
}
uint8_t rdhFormat = o2::raw::RDHUtils::getDataFormat(mDecoderPointer);
setDecoderCRUZEROES(!rdhFormat);
/** check that we got the first RDH open **/
if (!rdh || rdh->stop || rdh->pageCnt != 0) {
std::cout << colorRed
<< "[FATAL] this does not look like the first RDH in the HBF"
<< colorReset
<< std::endl;
o2::raw::RDHUtils::printRDH(*rdh);
return true;
}
/** loop until RDH close **/
while (!rdh->stop) {
if (verbose && mDecoderVerbose) {
std::cout << colorBlue
<< "--- RDH open/continue detected"
<< colorReset
<< std::endl;
o2::raw::RDHUtils::printRDH(*rdh);
}
/** do some minimal RDH sanity checks **/
if (rdh->feeId != mDecoderRDH->feeId ||
rdh->orbit != mDecoderRDH->orbit) {
std::cout << colorRed
<< "[FATAL] something does not match between this and first RDH"
<< colorReset
<< std::endl;
return true;
}
auto headerSize = rdh->headerSize;
auto memorySize = rdh->memorySize;
auto offsetToNext = rdh->offsetToNext;
auto drmPayload = memorySize - headerSize;
if (drmPayload < 0) {
LOG(warning) << "link = " << rdh->feeId << ": memorySize < headerSize (" << memorySize << " < " << headerSize << ")";
return true;
}
if (mDecoderSaveBufferDataSize + drmPayload >= mDecoderSaveBufferSize) {
// avoid to allocate memory out of the buffer
LOG(warning) << "link = " << rdh->feeId << ": beyond the buffer size " << mDecoderSaveBufferSize;
return true;
}
/** copy DRM payload to save buffer **/
std::memcpy(mDecoderSaveBuffer + mDecoderSaveBufferDataSize, reinterpret_cast<const char*>(rdh) + headerSize, drmPayload);
mDecoderSaveBufferDataSize += drmPayload;
/** move to next RDH **/
rdh = reinterpret_cast<const RDH*>(reinterpret_cast<const char*>(rdh) + offsetToNext);
/** check next RDH is within buffer **/
if (reinterpret_cast<const char*>(rdh) < mDecoderBuffer + mDecoderBufferSize) {
continue;
}
/** otherwise return **/
return true;
}
if (verbose && mDecoderVerbose) {
std::cout << colorBlue
<< "--- RDH close detected"
<< colorReset
<< std::endl;
o2::raw::RDHUtils::printRDH(*rdh);
}
/** do some minimal RDH sanity checks **/
if (rdh->feeId != mDecoderRDH->feeId ||
rdh->orbit != mDecoderRDH->orbit) {
std::cout << colorRed
<< "[FATAL] something does not match between this and first RDH"
<< colorReset
<< std::endl;
return true;
}
/** copy RDH open to encoder buffer **/
if (mEncoderPointer + mDecoderRDH->headerSize >= mEncoderPointerMax) {
LOG(warning) << "link = " << rdh->feeId << ": beyond the buffer size mEncoderPointer+mDecoderRDH->headerSize = " << mEncoderPointer + mDecoderRDH->headerSize << " >= "
<< "mEncoderPointerMax = " << mEncoderPointerMax;
encoderRewind();
return true;
}
std::memcpy(mEncoderPointer, mDecoderRDH, mDecoderRDH->headerSize);
mEncoderPointer = reinterpret_cast<uint32_t*>(reinterpret_cast<char*>(mEncoderPointer) + rdh->headerSize);
/** process DRM data **/
mDecoderPointer = reinterpret_cast<const uint32_t*>(mDecoderSaveBuffer);
mDecoderPointerMax = reinterpret_cast<const uint32_t*>(mDecoderSaveBuffer + mDecoderSaveBufferDataSize);
int nsteps = 0;
while (mDecoderPointer < mDecoderPointerMax) {
nsteps++;
if (nsteps > 3 && !(nsteps % 4)) {
LOG(debug) << "processHBF: nsteps in while loop = " << nsteps << ", infity loop?";
}
mEventCounter++;
if (processDRM()) { // if this breaks, we did not run the checker and the summary is not reset!
mDecoderSummary = {nullptr}; // reset it like this, perhaps a better way can be found
break;
}
}
mDecoderSaveBufferDataSize = 0;
/** bring encoder pointer back if fatal error and flag it **/
if (mDecoderFatal) {
mFatalCounter++;
mEncoderPointer = mEncoderPointerStart;
mEncoderRDH->detectorField |= 0x00001000;
}
/** updated encoder RDH open **/
mEncoderRDH->memorySize = reinterpret_cast<char*>(mEncoderPointer) - reinterpret_cast<char*>(mEncoderRDH);
mEncoderRDH->offsetToNext = mEncoderRDH->memorySize;
if (mDecoderError) {
mErrorCounter++;
}
// before to move to RDH close check we are not already out of buffer (it is the last chance to call rewind and not to store RDH open)
if (mEncoderPointer >= mEncoderPointerMax) {
LOG(error) << "link = " << rdh->feeId << ": beyond the buffer size mEncoderPointer in RDH open = " << mEncoderPointer << " >= "
<< "mEncoderPointerMax = " << mEncoderPointerMax;
long byteOutOfBuffer = mEncoderPointer + rdh->headerSize - mEncoderPointerMax;
LOG(error) << "byte out of buffer = " << byteOutOfBuffer;
encoderRewind();
return true;
}
/** copy RDH close to encoder buffer **/
/** CAREFUL WITH THE PAGE COUNTER **/
if (mEncoderPointer + rdh->headerSize >= mEncoderPointerMax) {
LOG(warning) << "link = " << rdh->feeId << ": beyond the buffer size mEncoderPointer+rdh->headerSize = " << mEncoderPointer + rdh->headerSize << " >= "
<< "mEncoderPointerMax = " << mEncoderPointerMax;
return true;
}
mEncoderRDH = reinterpret_cast<RDH*>(mEncoderPointer);
std::memcpy(mEncoderRDH, rdh, rdh->headerSize);
mEncoderRDH->memorySize = rdh->headerSize;
mEncoderRDH->offsetToNext = mEncoderRDH->memorySize;
mEncoderPointer = reinterpret_cast<uint32_t*>(reinterpret_cast<char*>(mEncoderPointer) + rdh->headerSize);
if (verbose && mDecoderVerbose) {
std::cout << colorBlue
<< "--- END PROCESS HBF"
<< colorReset
<< std::endl;
}
/** move to next RDH **/
mDecoderPointer = reinterpret_cast<const uint32_t*>(reinterpret_cast<const char*>(rdh) + rdh->offsetToNext);
/** check next RDH is within buffer **/
if (reinterpret_cast<const char*>(mDecoderPointer) < mDecoderBuffer + mDecoderBufferSize) {
return false;
}
/** otherwise return **/
return true;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::processDRM()
{
if (verbose && mDecoderVerbose) {
std::cout << colorBlue << "--- PROCESS DRM"
<< colorReset
<< std::endl;
}
/** init decoder **/
mDecoderNextWord = 1;
mDecoderError = false;
mDecoderFatal = false;
mEncoderPointerStart = mEncoderPointer;
/** check TOF Data Header **/
if (!IS_DRM_COMMON_HEADER(*mDecoderPointer)) {
if (verbose) {
printf("%s %08x [ERROR] fatal error %s \n", colorRed, *mDecoderPointer, colorReset);
}
mDecoderFatal = true;
return true;
}
mDecoderSummary.tofDataHeader = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto tofDataHeader = reinterpret_cast<const raw::TOFDataHeader_t*>(mDecoderPointer);
auto bytePayload = tofDataHeader->bytePayload;
printf(" %08x TOF Data Header (bytePayload=%d) \n", *mDecoderPointer, bytePayload);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** TOF Orbit **/
mDecoderSummary.tofOrbit = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto tofOrbit = reinterpret_cast<const raw::TOFOrbit_t*>(mDecoderPointer);
auto orbit = tofOrbit->orbit;
printf(" %08x TOF Orbit (orbit=%u) \n", *mDecoderPointer, orbit);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** check DRM Data Header **/
if (!IS_DRM_GLOBAL_HEADER(*mDecoderPointer)) {
if (verbose) {
printf("%s %08x [ERROR] fatal error %s \n", colorRed, *mDecoderPointer, colorReset);
}
mDecoderFatal = true;
return true;
}
mDecoderSummary.drmDataHeader = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto drmDataHeader = reinterpret_cast<const raw::DRMDataHeader_t*>(mDecoderPointer);
auto drmId = drmDataHeader->drmId;
auto eventWords = drmDataHeader->eventWords;
printf(" %08x DRM Data Header (drmId=%d, eventWords=%d) \n", *mDecoderPointer, drmId, eventWords);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** DRM Header Word 1 **/
mDecoderSummary.drmHeadW1 = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto drmHeadW1 = reinterpret_cast<const raw::DRMHeadW1_t*>(mDecoderPointer);
auto partSlotMask = drmHeadW1->partSlotMask;
auto clockStatus = drmHeadW1->clockStatus;
auto drmHSize = drmHeadW1->drmHSize;
printf(" %08x DRM Header Word 1 (partSlotMask=0x%03x, clockStatus=%d, drmHSize=%d) \n", *mDecoderPointer, partSlotMask, clockStatus, drmHSize);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** DRM Header Word 2 **/
mDecoderSummary.drmHeadW2 = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto drmHeadW2 = reinterpret_cast<const raw::DRMHeadW2_t*>(mDecoderPointer);
auto enaSlotMask = drmHeadW2->enaSlotMask;
auto faultSlotMask = drmHeadW2->faultSlotMask;
auto readoutTimeOut = drmHeadW2->readoutTimeOut;
printf(" %08x DRM Header Word 2 (enaSlotMask=0x%03x, faultSlotMask=0x%03x, readoutTimeOut=%d) \n", *mDecoderPointer, enaSlotMask, faultSlotMask, readoutTimeOut);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** DRM Header Word 3 **/
mDecoderSummary.drmHeadW3 = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto drmHeadW3 = reinterpret_cast<const raw::DRMHeadW3_t*>(mDecoderPointer);
auto gbtBunchCnt = drmHeadW3->gbtBunchCnt;
auto locBunchCnt = drmHeadW3->locBunchCnt;
printf(" %08x DRM Header Word 3 (gbtBunchCnt=%d, locBunchCnt=%d) \n", *mDecoderPointer, gbtBunchCnt, locBunchCnt);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** DRM Header Word 4 **/
mDecoderSummary.drmHeadW4 = mDecoderPointer;
if (verbose && mDecoderVerbose) {
printf(" %08x DRM Header Word 4 \n", *mDecoderPointer);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** DRM Header Word 5 **/
mDecoderSummary.drmHeadW5 = mDecoderPointer;
if (verbose && mDecoderVerbose) {
printf(" %08x DRM Header Word 5 \n", *mDecoderPointer);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** encode Crate Header **/
*mEncoderPointer = 0x80000000;
*mEncoderPointer |= GET_DRMHEADW1_PARTSLOTMASK(*mDecoderSummary.drmHeadW1) << 12;
// R+OLD *mEncoderPointer |= GET_DRMDATAHEADER_DRMID(*mDecoderSummary.drmDataHeader) << 24;
*mEncoderPointer |= (mDecoderRDH->feeId & 0xFF) << 24;
*mEncoderPointer |= GET_DRMHEADW3_GBTBUNCHCNT(*mDecoderSummary.drmHeadW3);
if (verbose && mEncoderVerbose) {
auto crateHeader = reinterpret_cast<compressed::CrateHeader_t*>(mEncoderPointer);
auto bunchID = crateHeader->bunchID;
auto drmID = crateHeader->drmID;
auto slotPartMask = crateHeader->slotPartMask;
printf("%s %08x Crate header (drmID=%d, bunchID=%d, slotPartMask=0x%x) %s \n", colorGreen, *mEncoderPointer, drmID, bunchID, slotPartMask, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
/** encode Crate Orbit **/
*mEncoderPointer = *mDecoderSummary.tofOrbit;
if (verbose && mEncoderVerbose) {
auto crateOrbit = reinterpret_cast<compressed::CrateOrbit_t*>(mEncoderPointer);
auto orbitID = crateOrbit->orbitID;
printf("%s %08x Crate orbit (orbitID=%u) %s \n", colorGreen, *mEncoderPointer, orbitID, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
/** loop over DRM payload **/
int nsteps = 0;
while (true) {
nsteps++;
if (nsteps > 19 && !(nsteps % 20)) {
LOG(debug) << "processDRM: nsteps in while loop = " << nsteps << ", infity loop?";
}
/** LTM global header detected **/
if (IS_LTM_GLOBAL_HEADER(*mDecoderPointer)) {
if (processLTM()) {
return true;
}
}
/** TRM Data Header detected **/
if (IS_TRM_GLOBAL_HEADER(*mDecoderPointer) && GET_TRMDATAHEADER_SLOTID(*mDecoderPointer) > 2) {
if (processTRM()) {
return true;
}
continue;
}
/** DRM Data Trailer detected **/
if (IS_DRM_GLOBAL_TRAILER(*mDecoderPointer)) {
mDecoderSummary.drmDataTrailer = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto drmDataTrailer = reinterpret_cast<const raw::DRMDataTrailer_t*>(mDecoderPointer);
auto locEvCnt = drmDataTrailer->locEvCnt;
printf(" %08x DRM Data Trailer (locEvCnt=%d) \n", *mDecoderPointer, locEvCnt);
}
decoderNext();
/** filler detected **/
while ((mDecoderPointer < mDecoderPointerMax) && IS_FILLER(*mDecoderPointer)) {
if (verbose && mDecoderVerbose) {
printf(" %08x Filler \n", *mDecoderPointer);
}
decoderNext();
}
/** encode Crate Trailer **/
*mEncoderPointer = 0x80000000;
*mEncoderPointer |= GET_DRMDATATRAILER_LOCEVCNT(*mDecoderSummary.drmDataTrailer) << 4;
/** check event **/
checkerCheck();
*mEncoderPointer |= mCheckerSummary.nDiagnosticWords;
#if ENCODE_TDC_ERRORS
*mEncoderPointer |= (mCheckerSummary.nTDCErrors << 16);
#endif
if (verbose && mEncoderVerbose) {
auto CrateTrailer = reinterpret_cast<compressed::CrateTrailer_t*>(mEncoderPointer);
auto EventCounter = CrateTrailer->eventCounter;
auto NumberOfDiagnostics = CrateTrailer->numberOfDiagnostics;
auto NumberOfErrors = CrateTrailer->numberOfErrors;
printf("%s %08x Crate trailer (EventCounter=%d, NumberOfDiagnostics=%d, NumberOfErrors=%d) %s \n", colorGreen, *mEncoderPointer, EventCounter, NumberOfDiagnostics, NumberOfErrors, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
/** encode Diagnostic Words **/
for (int iword = 0; iword < mCheckerSummary.nDiagnosticWords; ++iword) {
auto itrm = (mCheckerSummary.DiagnosticWord[iword] & 0xF) - 3;
*mEncoderPointer = mCheckerSummary.DiagnosticWord[iword];
if (verbose && mEncoderVerbose) {
auto Diagnostic = reinterpret_cast<compressed::Diagnostic_t*>(mEncoderPointer);
auto slotId = Diagnostic->slotID;
auto faultBits = Diagnostic->faultBits;
printf("%s %08x Diagnostic (slotId=%d, faultBits=0x%x) %s \n", colorGreen, *mEncoderPointer, slotId, faultBits, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
}
/** encode TDC errors **/
for (int itrm = 0; itrm < 10; ++itrm) {
for (int ichain = 0; ichain < 2; ++ichain) {
#if ENCODE_TDC_ERRORS
for (int ierror = 0; ierror < mDecoderSummary.trmErrors[itrm][ichain]; ++ierror) {
*mEncoderPointer = *mDecoderSummary.trmError[itrm][ichain][ierror];
*mEncoderPointer &= 0xFF07FFFF;
*mEncoderPointer |= ((itrm + 3) << 19);
*mEncoderPointer |= (ichain << 23);
if (verbose && mEncoderVerbose) {
auto Error = reinterpret_cast<compressed::Error_t*>(mEncoderPointer);
auto errorFlags = Error->errorFlags;
auto slotID = Error->slotID;
auto chain = Error->chain;
auto tdcID = Error->tdcID;
printf("%s %08x Error (slotId=%d, chain=%d, tdcId=%d, errorFlags=0x%x) %s \n", colorGreen, *mEncoderPointer, slotID, chain, tdcID, errorFlags, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
}
#endif
mDecoderSummary.trmErrors[itrm][ichain] = 0;
}
}
mCheckerSummary.nDiagnosticWords = 0;
mCheckerSummary.nTDCErrors = 0;
break;
}
/** DRM Test Word detected **/
if (IS_DRM_TEST_WORD(*mDecoderPointer)) {
if (verbose && mDecoderVerbose) {
printf(" %08x DRM Test Word \n", *mDecoderPointer);
}
decoderNext();
continue;
}
/** decode error **/
mDecoderError = true;
mDecoderSummary.drmDecodeError = true;
if (verbose && mDecoderVerbose) {
printf("%s %08x [ERROR] trying to recover DRM decode stream %s \n", colorRed, *mDecoderPointer, colorReset);
}
/** decode error detected, be paranoid **/
if (decoderParanoid()) {
return true;
}
decoderNext();
} /** end of loop over DRM payload **/
mIntegratedBytes += getDecoderByteCounter();
if (verbose && mDecoderVerbose) {
std::cout << colorBlue
<< "--- END PROCESS DRM"
<< colorReset
<< std::endl;
}
return false;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::processLTM()
{
/** process LTM **/
mDecoderSummary.ltmDataHeader = mDecoderPointer;
uint32_t eventWords = GET_LTMDATAHEADER_EVENTWORDS(*mDecoderPointer); // this is the total event size, including header/trailer
uint32_t payload = eventWords - 2;
if (verbose && mDecoderVerbose) {
auto ltmDataHeader = reinterpret_cast<const raw::LTMDataHeader_t*>(mDecoderPointer);
auto eventWords = ltmDataHeader->eventWords;
auto cycloneErr = ltmDataHeader->cycloneErr;
auto fault = ltmDataHeader->cycloneErr;
printf(" %08x LTM Data Header (eventWords=%d, cycloneErr=%d, fault=%d) \n", *mDecoderPointer, eventWords, cycloneErr, fault);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** loop over LTM payload **/
for (int i = 0; i < payload; ++i) {
if (verbose && mDecoderVerbose) {
printf(" %08x LTM Data \n", *mDecoderPointer);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
}
/** LTM global trailer detected **/
if (IS_LTM_GLOBAL_TRAILER(*mDecoderPointer)) {
mDecoderSummary.ltmDataTrailer = mDecoderPointer;
if (verbose && mDecoderVerbose) {
printf(" %08x LTM Global Trailer \n", *mDecoderPointer);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
}
/** success **/
return false;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::processTRM()
{
/** process TRM **/
uint32_t slotId = GET_TRMDATAHEADER_SLOTID(*mDecoderPointer);
int itrm = slotId - 3;
mDecoderSummary.trmDataHeader[itrm] = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto trmDataHeader = reinterpret_cast<const raw::TRMDataHeader_t*>(mDecoderPointer);
auto eventWords = trmDataHeader->eventWords;
auto eventCnt = trmDataHeader->eventCnt;
auto emptyBit = trmDataHeader->emptyBit;
printf(" %08x TRM Data Header (slotId=%u, eventWords=%d, eventCnt=%d, emptyBit=%01x) \n", *mDecoderPointer, slotId, eventWords, eventCnt, emptyBit);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** loop over TRM payload **/
int nsteps = 0;
while (true) {
nsteps++;
if (nsteps > 19 && !(nsteps % 20)) {
LOG(debug) << "processTRM: nsteps in while loop = " << nsteps << ", infity loop?";
}
/** TRM Chain-A Header detected **/
if (IS_TRM_CHAINA_HEADER(*mDecoderPointer) && GET_TRMCHAINHEADER_SLOTID(*mDecoderPointer) == slotId) {
if (processTRMchain(itrm, 0)) {
return true;
}
}
/** TRM Chain-B Header detected **/
if (IS_TRM_CHAINB_HEADER(*mDecoderPointer) && GET_TRMCHAINHEADER_SLOTID(*mDecoderPointer) == slotId) {
if (processTRMchain(itrm, 1)) {
return true;
}
}
/** TRM Data Trailer detected **/
if (IS_TRM_GLOBAL_TRAILER(*mDecoderPointer)) {
mDecoderSummary.trmDataTrailer[itrm] = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto trmDataTrailer = reinterpret_cast<const raw::TRMDataTrailer_t*>(mDecoderPointer);
auto eventCRC = trmDataTrailer->eventCRC;
auto lutErrorBit = trmDataTrailer->lutErrorBit;
printf(" %08x TRM Data Trailer (slotId=%u, eventCRC=%d, lutErrorBit=%d) \n", *mDecoderPointer, slotId, eventCRC, lutErrorBit);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** filler detected **/
if (IS_FILLER(*mDecoderPointer)) {
if (verbose && mDecoderVerbose) {
printf(" %08x Filler \n", *mDecoderPointer);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
}
/** encoder Spider **/
if (mDecoderSummary.hasHits[itrm][0] || mDecoderSummary.hasHits[itrm][1]) {
if (encoderSpider(itrm)) {
encoderRewind();
return true;
}
}
/** success **/
return false;
}
/** decode error **/
mDecoderError = true;
mDecoderSummary.trmDecodeError[itrm] = true;
if (verbose && mDecoderVerbose) {
printf("%s %08x [ERROR] breaking TRM decode stream %s \n", colorRed, *mDecoderPointer, colorReset);
}
decoderNext();
if (decoderParanoid()) { /** decode error detected, be always paranoid **/
return true;
}
return false;
} /** end of loop over TRM payload **/
/** never reached **/
return false;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::processTRMchain(int itrm, int ichain)
{
/** process TRM chain **/
int slotId = itrm + 3;
mDecoderSummary.trmChainHeader[itrm][ichain] = mDecoderPointer;
mDecoderSummary.hasHits[itrm][ichain] = false;
mDecoderSummary.hasErrors[itrm][ichain] = false;
if (verbose && mDecoderVerbose) {
auto trmChainHeader = reinterpret_cast<const raw::TRMChainHeader_t*>(mDecoderPointer);
auto bunchCnt = trmChainHeader->bunchCnt;
printf(" %08x TRM Chain-%c Header (slotId=%u, bunchCnt=%d) \n", *mDecoderPointer, ichain == 0 ? 'A' : 'B', slotId, bunchCnt);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
/** loop over TRM Chain payload **/
int nsteps = 0;
while (true) {
nsteps++;
if (nsteps > 99 && !(nsteps % 100)) {
LOG(debug) << "processTRMchain: nsteps in while loop = " << nsteps << ", infity loop?";
}
/** TDC hit detected **/
if (IS_TDC_HIT(*mDecoderPointer)) {
mDecoderSummary.hasHits[itrm][ichain] = true;
auto itdc = GET_TRMDATAHIT_TDCID(*mDecoderPointer);
auto ihit = mDecoderSummary.trmDataHits[ichain][itdc];
mDecoderSummary.trmDataHit[ichain][itdc][ihit] = mDecoderPointer;
mDecoderSummary.trmDataHits[ichain][itdc]++;
if (verbose && mDecoderVerbose) {
auto trmDataHit = reinterpret_cast<const raw::TRMDataHit_t*>(mDecoderPointer);
auto time = trmDataHit->time;
auto chanId = trmDataHit->chanId;
auto tdcId = trmDataHit->tdcId;
auto dataId = trmDataHit->dataId;
printf(" %08x TRM Data Hit (time=%d, chanId=%d, tdcId=%d, dataId=0x%x) \n", *mDecoderPointer, time, chanId, tdcId, dataId);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
continue;
}
/** TDC error detected **/
if (IS_TDC_ERROR(*mDecoderPointer)) {
mDecoderSummary.hasErrors[itrm][ichain] = true;
auto ierror = mDecoderSummary.trmErrors[itrm][ichain];
mDecoderSummary.trmError[itrm][ichain][ierror] = mDecoderPointer;
mDecoderSummary.trmErrors[itrm][ichain]++;
if (verbose && mDecoderVerbose) {
printf("%s %08x TDC error %s \n", colorRed, *mDecoderPointer, colorReset);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
continue;
}
/** TRM Chain Trailer detected **/
if (IS_TRM_CHAIN_TRAILER(*mDecoderPointer, ichain)) {
mDecoderSummary.trmChainTrailer[itrm][ichain] = mDecoderPointer;
if (verbose && mDecoderVerbose) {
auto trmChainTrailer = reinterpret_cast<const raw::TRMChainTrailer_t*>(mDecoderPointer);
auto eventCnt = trmChainTrailer->eventCnt;
printf(" %08x TRM Chain-A Trailer (slotId=%u, eventCnt=%d) \n", *mDecoderPointer, slotId, eventCnt);
}
decoderNext();
if (paranoid && decoderParanoid()) {
return true;
}
break;
}
/** decode error **/
mDecoderError = true;
mDecoderSummary.trmDecodeError[itrm] = true;
if (verbose && mDecoderVerbose) {
printf("%s %08x [ERROR] breaking TRM Chain-%c decode stream %s \n", colorRed, *mDecoderPointer, ichain == 0 ? 'A' : 'B', colorReset);
}
decoderNext();
if (decoderParanoid()) { /** decode error detected, be alway paranoid **/
return true;
}
break;
} /** end of loop over TRM chain payload **/
/** success **/
return false;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::decoderParanoid()
{
/** decoder paranoid **/
if (mDecoderPointer >= mDecoderPointerMax) {
if (verbose) {
printf("%s %08x [ERROR] fatal error: beyond memory size %s \n", colorRed, *mDecoderPointer, colorReset);
}
mDecoderFatal = true;
return true;
}
return false;
}
template <typename RDH, bool verbose, bool paranoid>
int Compressor<RDH, verbose, paranoid>::encoderSpider(int itrm)
{
/** encoder spider **/
int slotId = itrm + 3;
/** reset packed hits counter **/
int firstFilledFrame = 255;
int lastFilledFrame = 0;
/** loop over TRM chains **/
for (int ichain = 0; ichain < 2; ++ichain) {
if (!mDecoderSummary.hasHits[itrm][ichain]) {
continue;
}
/** loop over TDCs **/
for (int itdc = 0; itdc < 15; ++itdc) {
auto nhits = mDecoderSummary.trmDataHits[ichain][itdc];
if (nhits == 0) {
continue;
}
/** loop over hits **/
for (int ihit = 0; ihit < nhits; ++ihit) {
auto lhit = *mDecoderSummary.trmDataHit[ichain][itdc][ihit];
if (!IS_TDC_HIT_LEADING(lhit)) { // must be a leading hit
continue;
}
auto chan = GET_TRMDATAHIT_CHANID(lhit);
auto hitTime = GET_TRMDATAHIT_TIME(lhit);
auto eBit = GET_TRMDATAHIT_EBIT(lhit);
uint32_t totWidth = 0;
// check next hits for packing
for (int jhit = ihit + 1; jhit < nhits; ++jhit) {
auto thit = *mDecoderSummary.trmDataHit[ichain][itdc][jhit];
if (IS_TDC_HIT_TRAILING(thit) && GET_TRMDATAHIT_CHANID(thit) == chan) { // must be a trailing hit from same channel
totWidth = (GET_TRMDATAHIT_TIME(thit) - hitTime) / Geo::RATIO_TOT_TDC_BIN; // compute TOT
lhit = 0x0; // mark as used
break;
}
}
auto iframe = hitTime >> 13;
auto phit = mSpiderSummary.nFramePackedHits[iframe];
mSpiderSummary.FramePackedHit[iframe][phit] = 0x00000000;
mSpiderSummary.FramePackedHit[iframe][phit] |= (totWidth & 0x7FF) << 0;
mSpiderSummary.FramePackedHit[iframe][phit] |= (hitTime & 0x1FFF) << 11;
mSpiderSummary.FramePackedHit[iframe][phit] |= chan << 24;
mSpiderSummary.FramePackedHit[iframe][phit] |= itdc << 27;
mSpiderSummary.FramePackedHit[iframe][phit] |= ichain << 31;
mSpiderSummary.nFramePackedHits[iframe]++;
if (iframe < firstFilledFrame) {
firstFilledFrame = iframe;
}
if (iframe > lastFilledFrame) {
lastFilledFrame = iframe;
}
}
mDecoderSummary.trmDataHits[ichain][itdc] = 0;
}
}
/** loop over frames **/
for (int iframe = firstFilledFrame; iframe < lastFilledFrame + 1; iframe++) {
/** check if frame is empty **/
if (mSpiderSummary.nFramePackedHits[iframe] == 0) {
continue;
}
// encode Frame Header
*mEncoderPointer = 0x00000000;
*mEncoderPointer |= slotId << 24;
*mEncoderPointer |= iframe << 16;
*mEncoderPointer |= mSpiderSummary.nFramePackedHits[iframe];
if (verbose && mEncoderVerbose) {
auto FrameHeader = reinterpret_cast<const compressed::FrameHeader_t*>(mEncoderPointer);
auto NumberOfHits = FrameHeader->numberOfHits;
auto FrameID = FrameHeader->frameID;
auto TRMID = FrameHeader->trmID;
printf("%s %08x Frame header (TRMID=%d, FrameID=%d, NumberOfHits=%d) %s \n", colorGreen, *mEncoderPointer, TRMID, FrameID, NumberOfHits, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
// packed hits
for (int ihit = 0; ihit < mSpiderSummary.nFramePackedHits[iframe]; ++ihit) {
*mEncoderPointer = mSpiderSummary.FramePackedHit[iframe][ihit];
if (verbose && mEncoderVerbose) {
auto PackedHit = reinterpret_cast<const compressed::PackedHit_t*>(mEncoderPointer);
auto Chain = PackedHit->chain;
auto TDCID = PackedHit->tdcID;
auto Channel = PackedHit->channel;
auto Time = PackedHit->time;
auto TOT = PackedHit->tot;
printf("%s %08x Packed hit (Chain=%d, TDCID=%d, Channel=%d, Time=%d, TOT=%d) %s \n", colorGreen, *mEncoderPointer, Chain, TDCID, Channel, Time, TOT, colorReset);
}
if (encoderNext()) {
encoderRewind();
return true;
}
}
mSpiderSummary.nFramePackedHits[iframe] = 0;
}
return 0;
}
template <typename RDH, bool verbose, bool paranoid>
bool Compressor<RDH, verbose, paranoid>::checkerCheck()
{
/** checker check **/
mCheckerSummary.nDiagnosticWords = 0;
if (verbose && mCheckerVerbose) {
std::cout << colorBlue
<< "--- CHECK EVENT"
<< colorReset
<< std::endl;
}
/** increment check counter **/
// mCheckerCounter++;