forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUReconstructionConvert.cxx
More file actions
1499 lines (1406 loc) · 65.3 KB
/
GPUReconstructionConvert.cxx
File metadata and controls
1499 lines (1406 loc) · 65.3 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 GPUReconstructionConvert.cxx
/// \author David Rohr
#ifdef GPUCA_O2_LIB
#include "DetectorsRaw/RawFileWriter.h"
#include "TPCBase/Sector.h"
#include "DataFormatsTPC/Digit.h"
#include "TPCBase/Mapper.h"
#endif
#include "GPUReconstructionConvert.h"
#include "TPCFastTransform.h"
#include "GPUTPCClusterData.h"
#include "GPUO2DataTypes.h"
#include "GPUDataTypes.h"
#include "GPUTPCGeometry.h"
#include "AliHLTTPCRawCluster.h" // VS: It can not be removed. Used in line 93.
#include "GPUParam.h"
#include "GPULogging.h"
#include <algorithm>
#include <vector>
#include "clusterFinderDefs.h"
#include "DataFormatsTPC/ZeroSuppression.h"
#include "DataFormatsTPC/ZeroSuppressionLinkBased.h"
#include "DataFormatsTPC/Constants.h"
#include "CommonConstants/LHCConstants.h"
#include "DataFormatsTPC/Digit.h"
#include "TPCBase/RDHUtils.h"
#include "TPCBase/CRU.h"
#include "DetectorsRaw/RDHUtils.h"
#include <oneapi/tbb.h>
using namespace o2::gpu;
using namespace o2::tpc;
using namespace o2::tpc::constants;
using namespace std::string_literals;
void GPUReconstructionConvert::ConvertNativeToClusterData(o2::tpc::ClusterNativeAccess* native, std::unique_ptr<GPUTPCClusterData[]>* clusters, uint32_t* nClusters, const TPCFastTransform* transform, int32_t continuousMaxTimeBin)
{
memset(nClusters, 0, NSECTORS * sizeof(nClusters[0]));
uint32_t offset = 0;
for (uint32_t i = 0; i < NSECTORS; i++) {
uint32_t nClSector = 0;
for (int32_t j = 0; j < GPUCA_ROW_COUNT; j++) {
nClSector += native->nClusters[i][j];
}
nClusters[i] = nClSector;
clusters[i].reset(new GPUTPCClusterData[nClSector]);
nClSector = 0;
for (int32_t j = 0; j < GPUCA_ROW_COUNT; j++) {
for (uint32_t k = 0; k < native->nClusters[i][j]; k++) {
const auto& clin = native->clusters[i][j][k];
float x = 0, y = 0, z = 0;
if (continuousMaxTimeBin == 0) {
transform->Transform(i, j, clin.getPad(), clin.getTime(), x, y, z);
} else {
transform->TransformInTimeFrame(i, j, clin.getPad(), clin.getTime(), x, y, z, continuousMaxTimeBin);
}
auto& clout = clusters[i].get()[nClSector];
clout.x = x;
clout.y = y;
clout.z = z;
clout.row = j;
clout.amp = clin.qTot;
clout.flags = clin.getFlags();
clout.id = offset + k;
nClSector++;
}
native->clusterOffset[i][j] = offset;
offset += native->nClusters[i][j];
}
}
}
void GPUReconstructionConvert::ConvertRun2RawToNative(o2::tpc::ClusterNativeAccess& native, std::unique_ptr<ClusterNative[]>& nativeBuffer, const AliHLTTPCRawCluster** rawClusters, uint32_t* nRawClusters)
{
memset((void*)&native, 0, sizeof(native));
for (uint32_t i = 0; i < NSECTORS; i++) {
for (uint32_t j = 0; j < nRawClusters[i]; j++) {
native.nClusters[i][rawClusters[i][j].GetPadRow()]++;
}
native.nClustersTotal += nRawClusters[i];
}
nativeBuffer.reset(new ClusterNative[native.nClustersTotal]);
native.clustersLinear = nativeBuffer.get();
native.setOffsetPtrs();
for (uint32_t i = 0; i < NSECTORS; i++) {
for (uint32_t j = 0; j < GPUCA_ROW_COUNT; j++) {
native.nClusters[i][j] = 0;
}
for (uint32_t j = 0; j < nRawClusters[i]; j++) {
const AliHLTTPCRawCluster& org = rawClusters[i][j];
int32_t row = org.GetPadRow();
ClusterNative& c = nativeBuffer[native.clusterOffset[i][row] + native.nClusters[i][row]++];
c.setTimeFlags(org.GetTime(), org.GetFlags());
c.setPad(org.GetPad());
c.setSigmaTime(CAMath::Sqrt(org.GetSigmaTime2()));
c.setSigmaPad(CAMath::Sqrt(org.GetSigmaPad2()));
c.qMax = org.GetQMax();
c.qTot = org.GetCharge();
}
}
}
int32_t GPUReconstructionConvert::GetMaxTimeBin(const ClusterNativeAccess& native)
{
float retVal = 0;
for (uint32_t i = 0; i < NSECTORS; i++) {
for (uint32_t j = 0; j < GPUCA_ROW_COUNT; j++) {
for (uint32_t k = 0; k < native.nClusters[i][j]; k++) {
if (native.clusters[i][j][k].getTime() > retVal) {
retVal = native.clusters[i][j][k].getTime();
}
}
}
}
return ceil(retVal);
}
int32_t GPUReconstructionConvert::GetMaxTimeBin(const GPUTrackingInOutDigits& digits)
{
float retVal = 0;
for (uint32_t i = 0; i < NSECTORS; i++) {
for (uint32_t k = 0; k < digits.nTPCDigits[i]; k++) {
if (digits.tpcDigits[i][k].getTimeStamp() > retVal) {
retVal = digits.tpcDigits[i][k].getTimeStamp();
}
}
}
return ceil(retVal);
}
int32_t GPUReconstructionConvert::GetMaxTimeBin(const GPUTrackingInOutZS& zspages)
{
float retVal = 0;
for (uint32_t i = 0; i < NSECTORS; i++) {
int32_t firstHBF = zspages.sector[i].count[0] ? o2::raw::RDHUtils::getHeartBeatOrbit(*(const o2::header::RAWDataHeader*)zspages.sector[i].zsPtr[0][0]) : 0;
for (uint32_t j = 0; j < GPUTrackingInOutZS::NENDPOINTS; j++) {
for (uint32_t k = 0; k < zspages.sector[i].count[j]; k++) {
const char* page = (const char*)zspages.sector[i].zsPtr[j][k];
for (uint32_t l = 0; l < zspages.sector[i].nZSPtr[j][k]; l++) {
o2::header::RAWDataHeader* rdh = (o2::header::RAWDataHeader*)(page + l * TPCZSHDR::TPC_ZS_PAGE_SIZE);
TPCZSHDR* hdr = (TPCZSHDR*)(page + l * TPCZSHDR::TPC_ZS_PAGE_SIZE + sizeof(o2::header::RAWDataHeader));
int32_t nTimeBinSpan = hdr->nTimeBinSpan;
if (hdr->version >= o2::tpc::ZSVersion::ZSVersionDenseLinkBased) {
TPCZSHDRV2* hdr2 = (TPCZSHDRV2*)hdr;
if (hdr2->flags & TPCZSHDRV2::ZSFlags::nTimeBinSpanBit8) {
nTimeBinSpan += 256;
}
}
uint32_t timeBin = (hdr->timeOffset + (o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstHBF) * o2::constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN + nTimeBinSpan;
if (timeBin > retVal) {
retVal = timeBin;
}
}
}
}
}
return ceil(retVal);
}
// ------------------------------------------------- TPC ZS -------------------------------------------------
#ifdef GPUCA_TPC_GEOMETRY_O2
namespace o2::gpu
{
namespace // anonymous
{
// ------------------------------------------------- TPC ZS General -------------------------------------------------
typedef std::array<int64_t, TPCZSHDR::TPC_ZS_PAGE_SIZE / sizeof(int64_t)> zsPage;
struct zsEncoder {
int32_t curRegion = 0, outputRegion = 0;
uint32_t encodeBits = 0;
uint32_t zsVersion = 0;
uint32_t iSector = 0;
o2::raw::RawFileWriter* raw = nullptr;
const o2::InteractionRecord* ir = nullptr;
const GPUParam* param = nullptr;
bool padding = false;
int32_t lastEndpoint = -2, lastTime = -1, lastRow = GPUCA_ROW_COUNT;
int32_t endpoint = 0, outputEndpoint = 0;
int64_t hbf = -1, nexthbf = 0;
zsPage* page = nullptr;
uint8_t* pagePtr = nullptr;
int32_t bcShiftInFirstHBF = 0;
int32_t firstTimebinInPage = -1;
float encodeBitsFactor = 0;
bool needAnotherPage = false;
uint32_t packetCounter = 0;
uint32_t pageCounter = 0;
void ZSfillEmpty(void* ptr, int32_t shift, uint32_t feeId, int32_t orbit, int32_t linkid);
static void ZSstreamOut(uint16_t* bufIn, uint32_t& lenIn, uint8_t* bufOut, uint32_t& lenOut, uint32_t nBits);
int64_t getHbf(int64_t timestamp) { return (timestamp * LHCBCPERTIMEBIN + bcShiftInFirstHBF) / o2::constants::lhc::LHCMaxBunches; }
};
inline void zsEncoder::ZSfillEmpty(void* ptr, int32_t shift, uint32_t feeId, int32_t orbit, int32_t linkid)
{
o2::header::RAWDataHeader* rdh = (o2::header::RAWDataHeader*)ptr;
o2::raw::RDHUtils::setHeartBeatOrbit(*rdh, orbit);
o2::raw::RDHUtils::setHeartBeatBC(*rdh, shift);
o2::raw::RDHUtils::setMemorySize(*rdh, sizeof(o2::header::RAWDataHeader));
o2::raw::RDHUtils::setVersion(*rdh, o2::raw::RDHUtils::getVersion<o2::header::RAWDataHeader>());
o2::raw::RDHUtils::setFEEID(*rdh, feeId);
o2::raw::RDHUtils::setDetectorField(*rdh, 2);
o2::raw::RDHUtils::setLinkID(*rdh, linkid);
o2::raw::RDHUtils::setPacketCounter(*rdh, packetCounter++);
o2::raw::RDHUtils::setPageCounter(*rdh, pageCounter++);
}
inline void zsEncoder::ZSstreamOut(uint16_t* bufIn, uint32_t& lenIn, uint8_t* bufOut, uint32_t& lenOut, uint32_t nBits)
{
uint32_t byte = 0, bits = 0;
uint32_t mask = (1 << nBits) - 1;
for (uint32_t i = 0; i < lenIn; i++) {
byte |= (bufIn[i] & mask) << bits;
bits += nBits;
while (bits >= 8) {
bufOut[lenOut++] = (uint8_t)(byte & 0xFF);
byte = byte >> 8;
bits -= 8;
}
}
if (bits) {
bufOut[lenOut++] = byte;
}
lenIn = 0;
}
static inline auto ZSEncoderGetDigits(const GPUTrackingInOutDigits& in, int32_t i) { return in.tpcDigits[i]; }
static inline auto ZSEncoderGetNDigits(const GPUTrackingInOutDigits& in, int32_t i) { return in.nTPCDigits[i]; }
#ifdef GPUCA_O2_LIB
using DigitArray = std::array<gsl::span<const o2::tpc::Digit>, o2::tpc::Sector::MAXSECTOR>;
static inline auto ZSEncoderGetDigits(const DigitArray& in, int32_t i) { return in[i].data(); }
static inline auto ZSEncoderGetNDigits(const DigitArray& in, int32_t i) { return in[i].size(); }
#endif // GPUCA_O2_LIB
// ------------------------------------------------- TPC ZS Original Row-based ZS -------------------------------------------------
struct zsEncoderRow : public zsEncoder {
std::array<uint16_t, TPCZSHDR::TPC_ZS_PAGE_SIZE> streamBuffer = {};
std::array<uint8_t, TPCZSHDR::TPC_ZS_PAGE_SIZE> streamBuffer8 = {};
TPCZSHDR* hdr = nullptr;
TPCZSTBHDR* curTBHdr = nullptr;
uint8_t* nSeq = nullptr;
int32_t seqLen = 0;
int32_t endpointStart = 0;
int32_t nRowsInTB = 0;
uint32_t streamSize = 0, streamSize8 = 0;
constexpr static int32_t RAWLNK = rdh_utils::UserLogicLinkID;
bool checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
bool writeSubPage();
void init() { encodeBits = zsVersion == 2 ? TPCZSHDR::TPC_ZS_NBITS_V2 : TPCZSHDR::TPC_ZS_NBITS_V1; }
void initPage() {}
uint32_t encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
bool sort(const o2::tpc::Digit a, const o2::tpc::Digit b);
void decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* page, uint32_t endpoint, uint32_t firstOrbit, uint32_t triggerBC = 0);
};
inline bool zsEncoderRow::sort(const o2::tpc::Digit a, const o2::tpc::Digit b)
{
int32_t endpointa = GPUTPCGeometry::GetRegion(a.getRow());
int32_t endpointb = GPUTPCGeometry::GetRegion(b.getRow());
endpointa = 2 * endpointa + (a.getRow() >= GPUTPCGeometry::GetRegionStart(endpointa) + GPUTPCGeometry::GetRegionRows(endpointa) / 2);
endpointb = 2 * endpointb + (b.getRow() >= GPUTPCGeometry::GetRegionStart(endpointb) + GPUTPCGeometry::GetRegionRows(endpointb) / 2);
if (endpointa != endpointb) {
return endpointa <= endpointb;
}
if (a.getTimeStamp() != b.getTimeStamp()) {
return a.getTimeStamp() < b.getTimeStamp();
}
if (a.getRow() != b.getRow()) {
return a.getRow() < b.getRow();
}
return a.getPad() < b.getPad();
}
bool zsEncoderRow::checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
seqLen = 1;
if (lastRow != tmpBuffer[k].getRow()) {
endpointStart = GPUTPCGeometry::GetRegionStart(curRegion);
endpoint = curRegion * 2;
if (tmpBuffer[k].getRow() >= endpointStart + GPUTPCGeometry::GetRegionRows(curRegion) / 2) {
endpoint++;
endpointStart += GPUTPCGeometry::GetRegionRows(curRegion) / 2;
}
}
for (uint32_t l = k + 1; l < tmpBuffer.size(); l++) {
if (tmpBuffer[l].getRow() == tmpBuffer[k].getRow() && tmpBuffer[l].getTimeStamp() == tmpBuffer[k].getTimeStamp() && tmpBuffer[l].getPad() == tmpBuffer[l - 1].getPad() + 1) {
seqLen++;
} else {
break;
}
}
if (lastEndpoint >= 0 && lastTime != -1 && (int32_t)hdr->nTimeBinSpan + tmpBuffer[k].getTimeStamp() - lastTime >= 256) {
lastEndpoint = -1;
}
if (endpoint == lastEndpoint) {
uint32_t sizeChk = (uint32_t)(pagePtr - reinterpret_cast<uint8_t*>(page)); // already written
sizeChk += 2 * (nRowsInTB + (tmpBuffer[k].getRow() != lastRow && tmpBuffer[k].getTimeStamp() == lastTime)); // TB HDR
sizeChk += streamSize8; // in stream buffer
sizeChk += (lastTime != tmpBuffer[k].getTimeStamp()) && ((sizeChk + (streamSize * encodeBits + 7) / 8) & 1); // time bin alignment
sizeChk += (tmpBuffer[k].getTimeStamp() != lastTime || tmpBuffer[k].getRow() != lastRow) ? 3 : 0; // new row overhead
sizeChk += (lastTime != -1 && tmpBuffer[k].getTimeStamp() > lastTime) ? ((tmpBuffer[k].getTimeStamp() - lastTime - 1) * 2) : 0; // empty time bins
sizeChk += 2; // sequence metadata
const uint32_t streamSizeChkBits = streamSize * encodeBits + ((lastTime != tmpBuffer[k].getTimeStamp() && (streamSize * encodeBits) % 8) ? (8 - (streamSize * encodeBits) % 8) : 0);
if (sizeChk + (encodeBits + streamSizeChkBits + 7) / 8 > TPCZSHDR::TPC_ZS_PAGE_SIZE) {
lastEndpoint = -1;
} else if (sizeChk + (seqLen * encodeBits + streamSizeChkBits + 7) / 8 > TPCZSHDR::TPC_ZS_PAGE_SIZE) {
seqLen = ((TPCZSHDR::TPC_ZS_PAGE_SIZE - sizeChk) * 8 - streamSizeChkBits) / encodeBits;
}
// sizeChk += (seqLen * encodeBits + streamSizeChkBits + 7) / 8;
// printf("Endpoint %d (%d), Pos %d, Chk %d, Len %d, rows %d, StreamSize %d %d, time %d (%d), row %d (%d), pad %d\n", endpoint, lastEndpoint, (int32_t) (pagePtr - reinterpret_cast<uint8_t*>(page)), sizeChk, seqLen, nRowsInTB, streamSize8, streamSize, (int32_t)tmpBuffer[k].getTimeStamp(), lastTime, (int32_t)tmpBuffer[k].getRow(), lastRow, tmpBuffer[k].getPad());
}
return endpoint != lastEndpoint || tmpBuffer[k].getTimeStamp() != lastTime;
}
bool zsEncoderRow::writeSubPage()
{
if (pagePtr != reinterpret_cast<uint8_t*>(page)) {
pagePtr += 2 * nRowsInTB;
ZSstreamOut(streamBuffer.data(), streamSize, streamBuffer8.data(), streamSize8, encodeBits);
pagePtr = std::copy(streamBuffer8.data(), streamBuffer8.data() + streamSize8, pagePtr);
if (pagePtr - reinterpret_cast<uint8_t*>(page) > 8192) {
throw std::runtime_error("internal error during ZS encoding");
}
streamSize8 = 0;
for (int32_t l = 1; l < nRowsInTB; l++) {
curTBHdr->rowAddr1()[l - 1] += 2 * nRowsInTB;
}
}
return endpoint != lastEndpoint;
}
uint32_t zsEncoderRow::encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
if (tmpBuffer[k].getTimeStamp() != lastTime) {
if (lastTime != -1) {
hdr->nTimeBinSpan += tmpBuffer[k].getTimeStamp() - lastTime - 1;
pagePtr += (tmpBuffer[k].getTimeStamp() - lastTime - 1) * 2;
}
hdr->nTimeBinSpan++;
if ((pagePtr - reinterpret_cast<uint8_t*>(page)) & 1) {
pagePtr++;
}
curTBHdr = reinterpret_cast<TPCZSTBHDR*>(pagePtr);
curTBHdr->rowMask |= (endpoint & 1) << 15;
nRowsInTB = 0;
lastRow = GPUCA_ROW_COUNT;
}
if (tmpBuffer[k].getRow() != lastRow) {
curTBHdr->rowMask |= 1 << (tmpBuffer[k].getRow() - endpointStart);
ZSstreamOut(streamBuffer.data(), streamSize, streamBuffer8.data(), streamSize8, encodeBits);
if (nRowsInTB) {
curTBHdr->rowAddr1()[nRowsInTB - 1] = (pagePtr - reinterpret_cast<uint8_t*>(page)) + streamSize8;
}
nRowsInTB++;
nSeq = streamBuffer8.data() + streamSize8++;
*nSeq = 0;
}
(*nSeq)++;
streamBuffer8[streamSize8++] = tmpBuffer[k].getPad();
streamBuffer8[streamSize8++] = streamSize + seqLen;
for (int32_t l = 0; l < seqLen; l++) {
streamBuffer[streamSize++] = (uint16_t)(tmpBuffer[k + l].getChargeFloat() * encodeBitsFactor + 0.5f);
}
return seqLen;
}
void zsEncoderRow::decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* decPage, uint32_t decEndpoint, uint32_t firstOrbit, uint32_t triggerBC)
{
const uint8_t* decPagePtr = reinterpret_cast<const uint8_t*>(decPage);
const o2::header::RAWDataHeader* rdh = (const o2::header::RAWDataHeader*)decPagePtr;
if (o2::raw::RDHUtils::getMemorySize(*rdh) == sizeof(o2::header::RAWDataHeader)) {
return;
}
decPagePtr += sizeof(o2::header::RAWDataHeader);
const TPCZSHDR* decHDR = reinterpret_cast<const TPCZSHDR*>(decPagePtr);
decPagePtr += sizeof(*decHDR);
if (decHDR->version != 1 && decHDR->version != 2) {
throw std::runtime_error("invalid ZS version "s + std::to_string(decHDR->version) + " (1 or 2 expected)"s);
}
const float decodeBitsFactor = 1.f / (1 << (encodeBits - 10));
uint32_t mask = (1 << encodeBits) - 1;
int32_t cruid = decHDR->cruID;
uint32_t sector = cruid / 10;
if (sector != iSector) {
throw std::runtime_error("invalid TPC sector");
}
int32_t region = cruid % 10;
if ((uint32_t)region != decEndpoint / 2) {
throw std::runtime_error("CRU ID / endpoint mismatch");
}
int32_t nRowsRegion = GPUTPCGeometry::GetRegionRows(region);
int32_t timeBin = (decHDR->timeOffset + (uint64_t)(o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstOrbit) * o2::constants::lhc::LHCMaxBunches) / LHCBCPERTIMEBIN;
for (int32_t l = 0; l < decHDR->nTimeBinSpan; l++) {
if ((decPagePtr - reinterpret_cast<const uint8_t*>(decPage)) & 1) {
decPagePtr++;
}
const TPCZSTBHDR* tbHdr = reinterpret_cast<const TPCZSTBHDR*>(decPagePtr);
bool upperRows = tbHdr->rowMask & 0x8000;
if (tbHdr->rowMask != 0 && ((upperRows) ^ ((decEndpoint & 1) != 0))) {
throw std::runtime_error("invalid endpoint");
}
const int32_t rowOffset = GPUTPCGeometry::GetRegionStart(region) + (upperRows ? (nRowsRegion / 2) : 0);
const int32_t nRows = upperRows ? (nRowsRegion - nRowsRegion / 2) : (nRowsRegion / 2);
const int32_t nRowsUsed = __builtin_popcount((uint32_t)(tbHdr->rowMask & 0x7FFF));
decPagePtr += nRowsUsed ? (2 * nRowsUsed) : 2;
int32_t rowPos = 0;
for (int32_t m = 0; m < nRows; m++) {
if ((tbHdr->rowMask & (1 << m)) == 0) {
continue;
}
const uint8_t* rowData = rowPos == 0 ? decPagePtr : (reinterpret_cast<const uint8_t*>(decPage) + tbHdr->rowAddr1()[rowPos - 1]);
const int32_t nSeqRead = *rowData;
const uint8_t* adcData = rowData + 2 * nSeqRead + 1;
int32_t nADC = (rowData[2 * nSeqRead] * encodeBits + 7) / 8;
decPagePtr += 1 + 2 * nSeqRead + nADC;
uint32_t byte = 0, bits = 0, posXbits = 0;
std::array<uint16_t, TPCZSHDR::TPC_ZS_PAGE_SIZE> decBuffer;
for (int32_t n = 0; n < nADC; n++) {
byte |= *(adcData++) << bits;
bits += 8;
while (bits >= encodeBits) {
decBuffer[posXbits++] = byte & mask;
byte = byte >> encodeBits;
bits -= encodeBits;
}
}
posXbits = 0;
for (int32_t n = 0; n < nSeqRead; n++) {
const int32_t decSeqLen = rowData[(n + 1) * 2] - (n ? rowData[n * 2] : 0);
for (int32_t o = 0; o < decSeqLen; o++) {
outputBuffer.emplace_back(o2::tpc::Digit{cruid, decBuffer[posXbits++] * decodeBitsFactor, (tpccf::Row)(rowOffset + m), (tpccf::Pad)(rowData[n * 2 + 1] + o), timeBin + l});
}
}
rowPos++;
}
}
}
// ------------------------------------------------- TPC ZS Link Based ZS -------------------------------------------------
#ifdef GPUCA_O2_LIB
struct zsEncoderLinkBased : public zsEncoder {
TPCZSHDRV2* hdr = nullptr;
TPCZSHDRV2 hdrBuffer;
int32_t inverseChannelMapping[5][32];
int32_t nSamples = 0;
int32_t link = 0;
bool finishPage = false;
std::vector<uint16_t> adcValues = {};
std::bitset<80> bitmask = {};
void createBitmask(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
void init();
bool sort(const o2::tpc::Digit a, const o2::tpc::Digit b);
};
void zsEncoderLinkBased::init()
{
encodeBits = TPCZSHDRV2::TPC_ZS_NBITS_V34;
for (int32_t i = 0; i < 5; i++) {
for (int32_t j = 0; j < 32; j++) {
inverseChannelMapping[i][j] = -1;
}
}
for (int32_t iCRU = 0; iCRU < 2; iCRU++) {
for (int32_t iChannel = 0; iChannel < 80; iChannel++) {
int32_t sampaOnFEC = 0, channelOnSAMPA = 0;
Mapper::getSampaAndChannelOnFEC(iCRU, iChannel, sampaOnFEC, channelOnSAMPA);
if (inverseChannelMapping[sampaOnFEC][channelOnSAMPA] != -1 && inverseChannelMapping[sampaOnFEC][channelOnSAMPA] != iChannel) {
GPUError("ERROR: Channel conflict: %d %d: %d vs %d", sampaOnFEC, channelOnSAMPA, inverseChannelMapping[sampaOnFEC][channelOnSAMPA], iChannel);
throw std::runtime_error("ZS error");
}
inverseChannelMapping[sampaOnFEC][channelOnSAMPA] = iChannel;
}
}
for (int32_t i = 0; i < 5; i++) {
for (int32_t j = 0; j < 32; j++) {
if (inverseChannelMapping[i][j] == -1) {
GPUError("ERROR: Map missing for sampa %d channel %d", i, j);
throw std::runtime_error("ZS error");
}
}
}
}
void zsEncoderLinkBased::createBitmask(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
const auto& mapper = Mapper::instance();
nSamples = 0;
adcValues.clear();
bitmask.reset();
uint32_t l;
for (l = k; l < tmpBuffer.size(); l++) {
const auto& a = tmpBuffer[l];
int32_t cruinsector = GPUTPCGeometry::GetRegion(a.getRow());
o2::tpc::GlobalPadNumber pad = mapper.globalPadNumber(o2::tpc::PadPos(a.getRow(), a.getPad()));
o2::tpc::FECInfo fec = mapper.fecInfo(pad);
o2::tpc::CRU cru = cruinsector;
int32_t fecInPartition = fec.getIndex() - mapper.getPartitionInfo(cru.partition()).getSectorFECOffset();
int32_t tmpEndpoint = 2 * cruinsector + (fecInPartition >= (mapper.getPartitionInfo(cru.partition()).getNumberOfFECs() + 1) / 2);
if (l == k) {
link = fecInPartition;
endpoint = tmpEndpoint;
} else if (endpoint != tmpEndpoint || link != fecInPartition || tmpBuffer[l].getTimeStamp() != tmpBuffer[k].getTimeStamp()) {
break;
}
int32_t channel = inverseChannelMapping[fec.getSampaChip()][fec.getSampaChannel()];
bitmask[channel] = 1;
adcValues.emplace_back((uint16_t)(a.getChargeFloat() * encodeBitsFactor + 0.5f));
}
nSamples = l - k;
}
bool zsEncoderLinkBased::sort(const o2::tpc::Digit a, const o2::tpc::Digit b)
{
// Fixme: this is blasphemy... one shoult precompute all values and sort an index array
int32_t cruinsectora = GPUTPCGeometry::GetRegion(a.getRow());
int32_t cruinsectorb = GPUTPCGeometry::GetRegion(b.getRow());
if (cruinsectora != cruinsectorb) {
return cruinsectora < cruinsectorb;
}
const auto& mapper = Mapper::instance();
o2::tpc::GlobalPadNumber pada = mapper.globalPadNumber(o2::tpc::PadPos(a.getRow(), a.getPad()));
o2::tpc::GlobalPadNumber padb = mapper.globalPadNumber(o2::tpc::PadPos(b.getRow(), b.getPad()));
o2::tpc::FECInfo feca = mapper.fecInfo(pada);
o2::tpc::FECInfo fecb = mapper.fecInfo(padb);
o2::tpc::CRU cru = cruinsectora;
int32_t fecInPartitiona = feca.getIndex() - mapper.getPartitionInfo(cru.partition()).getSectorFECOffset();
int32_t fecInPartitionb = fecb.getIndex() - mapper.getPartitionInfo(cru.partition()).getSectorFECOffset();
int32_t endpointa = 2 * cruinsectora + (fecInPartitiona >= (mapper.getPartitionInfo(cru.partition()).getNumberOfFECs() + 1) / 2);
int32_t endpointb = 2 * cruinsectorb + (fecInPartitionb >= (mapper.getPartitionInfo(cru.partition()).getNumberOfFECs() + 1) / 2);
if (endpointa != endpointb) {
return endpointa < endpointb;
}
if (a.getTimeStamp() != b.getTimeStamp()) {
return a.getTimeStamp() < b.getTimeStamp();
}
if (fecInPartitiona != fecInPartitionb) {
return fecInPartitiona < fecInPartitionb;
}
return inverseChannelMapping[feca.getSampaChip()][feca.getSampaChannel()] < inverseChannelMapping[fecb.getSampaChip()][fecb.getSampaChannel()];
}
// ------------------------------------------------- TPC Improved Link Based ZS -------------------------------------------------
struct zsEncoderImprovedLinkBased : public zsEncoderLinkBased {
bool checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
uint32_t encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
void decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* page, uint32_t endpoint, uint32_t firstOrbit, uint32_t triggerBC = 0);
bool writeSubPage();
void initPage();
constexpr static int32_t RAWLNK = rdh_utils::ILBZSLinkID;
};
bool zsEncoderImprovedLinkBased::checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
createBitmask(tmpBuffer, k);
finishPage = endpoint != lastEndpoint;
if (firstTimebinInPage != -1 && tmpBuffer[k].getTimeStamp() - firstTimebinInPage >= 1 << (sizeof(hdr->nTimeBinSpan) * 8)) {
finishPage = true;
}
if (!finishPage) {
uint32_t sizeChk = (uint32_t)(pagePtr - reinterpret_cast<uint8_t*>(page));
sizeChk += sizeof(o2::tpc::zerosupp_link_based::CommonHeader);
if (TPCZSHDRV2::TIGHTLY_PACKED_V3) {
sizeChk += (nSamples * TPCZSHDRV2::TPC_ZS_NBITS_V34 + 127) / 128 * 16;
} else {
sizeChk += (nSamples + 2 * TPCZSHDRV2::SAMPLESPER64BIT - 1) / (2 * TPCZSHDRV2::SAMPLESPER64BIT) * 16;
}
if (sizeChk > TPCZSHDR::TPC_ZS_PAGE_SIZE) {
finishPage = true;
}
}
return finishPage;
}
uint32_t zsEncoderImprovedLinkBased::encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
o2::tpc::zerosupp_link_based::CommonHeader* tbHdr = (o2::tpc::zerosupp_link_based::CommonHeader*)pagePtr;
pagePtr += sizeof(*tbHdr);
tbHdr->bunchCrossing = (tmpBuffer[k].getTimeStamp() - firstTimebinInPage) * LHCBCPERTIMEBIN;
tbHdr->magicWord = o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZS;
tbHdr->bitMaskHigh = (bitmask >> 64).to_ulong();
tbHdr->bitMaskLow = (bitmask & std::bitset<80>(0xFFFFFFFFFFFFFFFFlu)).to_ulong();
tbHdr->syncOffsetBC = 0;
tbHdr->fecInPartition = link;
hdr->nTimeBinSpan = tmpBuffer[k].getTimeStamp() - firstTimebinInPage;
hdr->nTimebinHeaders++;
if (TPCZSHDRV2::TIGHTLY_PACKED_V3) {
tbHdr->numWordsPayload = (nSamples * TPCZSHDRV2::TPC_ZS_NBITS_V34 + 127) / 128; // tightly packed ADC samples
uint32_t tmp = 0;
uint32_t tmpIn = nSamples;
ZSstreamOut(adcValues.data(), tmpIn, pagePtr, tmp, encodeBits);
} else {
tbHdr->numWordsPayload = (nSamples + 2 * TPCZSHDRV2::SAMPLESPER64BIT - 1) / (2 * TPCZSHDRV2::SAMPLESPER64BIT);
uint64_t* payloadPtr = (uint64_t*)pagePtr;
for (uint32_t i = 0; i < 2 * tbHdr->numWordsPayload; i++) {
payloadPtr[i] = 0;
}
for (uint32_t i = 0; i < nSamples; i++) {
payloadPtr[i / TPCZSHDRV2::SAMPLESPER64BIT] |= ((uint64_t)adcValues[i]) << ((i % TPCZSHDRV2::SAMPLESPER64BIT) * TPCZSHDRV2::TPC_ZS_NBITS_V34);
}
}
pagePtr += tbHdr->numWordsPayload * 16;
return nSamples;
}
bool zsEncoderImprovedLinkBased::writeSubPage()
{
return finishPage;
}
void zsEncoderImprovedLinkBased::initPage()
{
hdr->magicWord = o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader;
hdr->nTimebinHeaders = 0;
hdr->firstZSDataOffset = 0;
}
void zsEncoderImprovedLinkBased::decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* decPage, uint32_t decEndpoint, uint32_t firstOrbit, uint32_t triggerBC)
{
const auto& mapper = Mapper::instance();
const uint8_t* decPagePtr = reinterpret_cast<const uint8_t*>(decPage);
const o2::header::RAWDataHeader* rdh = (const o2::header::RAWDataHeader*)decPagePtr;
if (o2::raw::RDHUtils::getMemorySize(*rdh) == sizeof(o2::header::RAWDataHeader)) {
return;
}
decPagePtr += sizeof(o2::header::RAWDataHeader);
const TPCZSHDRV2* decHDR = reinterpret_cast<const TPCZSHDRV2*>(decPagePtr);
decPagePtr += sizeof(*decHDR);
if (decHDR->version != ZSVersion::ZSVersionLinkBasedWithMeta) {
throw std::runtime_error("invalid ZS version "s + std::to_string(decHDR->version) + " ("s + std::to_string(ZSVersion::ZSVersionLinkBasedWithMeta) + " expected)"s);
}
if (decHDR->magicWord != o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader) {
throw std::runtime_error("Magic word missing");
}
const float decodeBitsFactor = 1.f / (1 << (encodeBits - 10));
uint32_t mask = (1 << encodeBits) - 1;
int32_t cruid = decHDR->cruID;
uint32_t sector = cruid / 10;
if (sector != iSector) {
throw std::runtime_error("invalid TPC sector");
}
int32_t region = cruid % 10;
decPagePtr += decHDR->firstZSDataOffset * 16;
for (uint32_t i = 0; i < decHDR->nTimebinHeaders; i++) {
const o2::tpc::zerosupp_link_based::Header* tbHdr = (const o2::tpc::zerosupp_link_based::Header*)decPagePtr;
#if 0 // Decoding using the function for the original linkZS
o2::tpc::CRU cru = cruid % 10;
const int32_t feeLink = tbHdr->fecInPartition - (decEndpoint & 1) * ((mapper.getPartitionInfo(cru.partition()).getNumberOfFECs() + 1) / 2);
auto fillADC = [&outputBuffer](int32_t cru, int32_t rowInSector, int32_t padInRow, int32_t timeBin, float adcValue) {
outputBuffer.emplace_back(o2::tpc::Digit{cruid, adcValue, rowInSector, padInRow, timeBin});
return true;
};
size_t size = sizeof(*tbHdr) + tbHdr->numWordsPayload * 16;
raw_processing_helpersa::processZSdata((const char*)decPagePtr, size, rdh_utils::getFEEID(cruid, decEndpoint & 1, feeLink), o2::raw::RDHUtils::getHeartBeatOrbit(*rdh), firstOrbit, decHDR->timeOffset, fillADC);
#else // Decoding directly
if (!tbHdr->isLinkZS()) {
throw std::runtime_error("ZS TB Hdr does not have linkZS magic word");
}
int32_t timeBin = (int32_t(decHDR->timeOffset) + int32_t(tbHdr->bunchCrossing) + (int32_t)(o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstOrbit) * o2::constants::lhc::LHCMaxBunches - triggerBC) / LHCBCPERTIMEBIN;
if (timeBin < 0) {
LOGP(debug, "zsEncoderImprovedLinkBased::decodePage skipping digits hdr->tOff {} + hdr->bc {} + (orbit {} - firstOrbit {}) * maxBunch {} - triggerBC {} = {} < 0", decHDR->timeOffset, tbHdr->bunchCrossing, o2::raw::RDHUtils::getHeartBeatOrbit(*rdh), firstOrbit, o2::constants::lhc::LHCMaxBunches, triggerBC, timeBin);
continue;
}
const uint8_t* adcData = (const uint8_t*)(decPagePtr + sizeof(*tbHdr));
const auto& bitmask = tbHdr->getChannelBits();
int32_t nADC = bitmask.count();
std::vector<uint16_t> decBuffer(nADC);
if (TPCZSHDRV2::TIGHTLY_PACKED_V3) {
uint32_t byte = 0, bits = 0, posXbits = 0;
while (posXbits < nADC) {
byte |= *(adcData++) << bits;
bits += 8;
while (bits >= encodeBits) {
decBuffer[posXbits++] = byte & mask;
byte = byte >> encodeBits;
bits -= encodeBits;
}
}
} else {
const uint64_t* adcData64 = (const uint64_t*)adcData;
for (int32_t j = 0; j < nADC; j++) {
decBuffer[j] = (adcData64[j / TPCZSHDRV2::SAMPLESPER64BIT] >> ((j % TPCZSHDRV2::SAMPLESPER64BIT) * TPCZSHDRV2::TPC_ZS_NBITS_V34)) & mask;
}
}
for (int32_t j = 0, k = 0; j < bitmask.size(); j++) {
if (bitmask[j]) {
int32_t sampaOnFEC = 0, channelOnSAMPA = 0;
mapper.getSampaAndChannelOnFEC(cruid, j, sampaOnFEC, channelOnSAMPA);
const auto padSecPos = mapper.padSecPos(cruid, tbHdr->fecInPartition, sampaOnFEC, channelOnSAMPA);
const auto& padPos = padSecPos.getPadPos();
outputBuffer.emplace_back(o2::tpc::Digit{cruid, decBuffer[k++] * decodeBitsFactor, (tpccf::Row)padPos.getRow(), (tpccf::Pad)padPos.getPad(), timeBin});
}
}
#endif
decPagePtr += sizeof(*tbHdr) + tbHdr->numWordsPayload * 16;
}
}
// ------------------------------------------------- TPC ZS Dense Link Based ZS -------------------------------------------------
struct zsEncoderDenseLinkBased : public zsEncoderLinkBased {
bool checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
uint32_t encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k);
void decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* page, uint32_t endpoint, uint32_t firstOrbit, uint32_t triggerBC = 0);
bool writeSubPage();
void initPage();
void amendPageErrorMessage(std::ostringstream& oss, const o2::header::RAWDataHeader* rdh, const TPCZSHDRV2* decHDR, const uint8_t* payloadEnd, const uint8_t* decPagePtr, uint32_t nOutput);
uint16_t curTimeBin = 0;
std::vector<uint8_t> sequenceBuffer;
std::vector<uint16_t> sequenceBufferADC;
constexpr static int32_t RAWLNK = rdh_utils::DLBZSLinkID;
constexpr static int32_t v2nbits = 10;
};
bool zsEncoderDenseLinkBased::checkInput(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
createBitmask(tmpBuffer, k);
finishPage = endpoint != lastEndpoint;
uint16_t newTimeBin = tmpBuffer[k].getTimeStamp() - firstTimebinInPage;
bool retVall = finishPage || newTimeBin != curTimeBin;
return retVall;
}
uint32_t zsEncoderDenseLinkBased::encodeSequence(std::vector<o2::tpc::Digit>& tmpBuffer, uint32_t k)
{
if (sequenceBuffer.size() == 0) {
uint16_t bc = (int64_t)tmpBuffer[k].getTimeStamp() * LHCBCPERTIMEBIN - (int64_t)hbf * o2::constants::lhc::LHCMaxBunches;
if (zsVersion == ZSVersion::ZSVersionDenseLinkBasedV2) {
bc &= 0xFFC;
}
sequenceBuffer.emplace_back(bc << 4);
sequenceBuffer.emplace_back(bc >> 4);
curTimeBin = tmpBuffer[k].getTimeStamp() - firstTimebinInPage;
hdr->nTimeBinSpan = curTimeBin & 0xFF;
if (curTimeBin & 0x100) {
hdr->flags |= TPCZSHDRV2::ZSFlags::nTimeBinSpanBit8;
}
hdr->nTimebinHeaders++;
}
sequenceBuffer[0]++;
sequenceBuffer.emplace_back(link);
uint8_t* plink = &sequenceBuffer.back();
std::bitset<10> bitmaskL2;
for (int32_t i = 9; i >= 0; i--) {
bitmaskL2.set(i, ((bitmask >> (i * 8)) & std::bitset<80>(0xFF)).any());
}
if (bitmaskL2.all()) {
*plink |= 0b00100000;
} else {
*plink |= (bitmaskL2.to_ulong() >> 2) & 0b11000000;
sequenceBuffer.emplace_back(bitmaskL2.to_ulong() & 0xFF);
}
for (int32_t i = 0; i < 10; i++) {
if (bitmaskL2.test(i)) {
sequenceBuffer.emplace_back(((bitmask >> (i * 8)) & std::bitset<80>(0xFF)).to_ulong());
}
}
static_assert(TPCZSHDRV2::TPC_ZS_NBITS_V34 == 12);
if (nSamples) {
sequenceBufferADC.insert(sequenceBufferADC.end(), adcValues.begin(), adcValues.end());
}
return nSamples;
}
bool zsEncoderDenseLinkBased::writeSubPage()
{
uint32_t offset = sequenceBuffer.size();
if (sequenceBufferADC.size()) {
bool need12bit = zsVersion != ZSVersion::ZSVersionDenseLinkBasedV2;
uint32_t needNow = 0;
if (zsVersion == ZSVersion::ZSVersionDenseLinkBasedV2) {
for (uint32_t i = 0; i < sequenceBufferADC.size(); i++) {
if (sequenceBufferADC[i] >= (1 << v2nbits)) {
need12bit = true;
break;
}
}
}
uint32_t encodeBitsBlock = encodeBits;
if (!need12bit) {
encodeBitsBlock = v2nbits;
sequenceBuffer[0] |= 0x10;
}
sequenceBuffer.resize(offset + (sequenceBufferADC.size() * encodeBitsBlock + 7) / 8);
uint32_t tmp = 0;
uint32_t tmpIn = sequenceBufferADC.size();
ZSstreamOut(sequenceBufferADC.data(), tmpIn, sequenceBuffer.data() + offset, tmp, encodeBitsBlock);
sequenceBufferADC.clear();
}
if (sequenceBuffer.size()) {
uint32_t sizeLeft = TPCZSHDR::TPC_ZS_PAGE_SIZE - (pagePtr - (uint8_t*)page) - sizeof(TPCZSHDRV2) - (hdr->flags & TPCZSHDRV2::ZSFlags::TriggerWordPresent ? TPCZSHDRV2::TRIGGER_WORD_SIZE : 0);
uint32_t size = sequenceBuffer.size();
uint32_t fill = std::min(sizeLeft, size);
memcpy(pagePtr, sequenceBuffer.data(), fill);
pagePtr += fill;
if (size != fill) {
hdr->flags |= o2::tpc::TPCZSHDRV2::ZSFlags::payloadExtendsToNextPage;
sequenceBuffer.erase(sequenceBuffer.begin(), sequenceBuffer.begin() + fill);
} else {
sequenceBuffer.clear();
}
finishPage = finishPage || size >= sizeLeft || needAnotherPage;
}
return finishPage;
}
void zsEncoderDenseLinkBased::initPage()
{
hdr->magicWord = o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader;
hdr->nTimebinHeaders = 0;
memcpy(pagePtr, sequenceBuffer.data(), sequenceBuffer.size());
hdr->firstZSDataOffset = sequenceBuffer.size() + sizeof(o2::header::RAWDataHeader);
pagePtr += sequenceBuffer.size();
sequenceBuffer.clear();
hdr->flags = 0;
}
void zsEncoderDenseLinkBased::decodePage(std::vector<o2::tpc::Digit>& outputBuffer, const zsPage* decPage, uint32_t decEndpoint, uint32_t firstOrbit, uint32_t triggerBC)
{
const auto& mapper = Mapper::instance();
const uint8_t* decPagePtr = reinterpret_cast<const uint8_t*>(decPage);
const o2::header::RAWDataHeader* rdh = (const o2::header::RAWDataHeader*)decPagePtr;
if (o2::raw::RDHUtils::getMemorySize(*rdh) == sizeof(o2::header::RAWDataHeader)) {
return;
}
const TPCZSHDRV2* decHDR = reinterpret_cast<const TPCZSHDRV2*>(decPagePtr + o2::raw::RDHUtils::getMemorySize(*rdh) - sizeof(TPCZSHDRV2));
decPagePtr += sizeof(o2::header::RAWDataHeader);
if (decHDR->version < ZSVersion::ZSVersionDenseLinkBased || decHDR->version > ZSVersion::ZSVersionDenseLinkBasedV2) {
throw std::runtime_error("invalid ZS version "s + std::to_string(decHDR->version) + " ("s + std::to_string(ZSVersion::ZSVersionDenseLinkBased) + " - "s + std::to_string(ZSVersion::ZSVersionDenseLinkBasedV2) + " expected)"s);
}
if (decHDR->magicWord != o2::tpc::zerosupp_link_based::CommonHeader::MagicWordLinkZSMetaHeader) {
throw std::runtime_error("Magic word missing");
}
const uint8_t* payloadEnd = ((const uint8_t*)decPage) + o2::raw::RDHUtils::getMemorySize(*rdh) - sizeof(TPCZSHDRV2) - ((decHDR->flags & TPCZSHDRV2::ZSFlags::TriggerWordPresent) ? TPCZSHDRV2::TRIGGER_WORD_SIZE : 0);
const float decodeBitsFactor = 1.f / (1 << (encodeBits - 10));
int32_t cruid = decHDR->cruID;
uint32_t sector = cruid / 10;
if (sector != iSector) {
throw std::runtime_error("invalid TPC sector");
}
int32_t region = cruid % 10;
decPagePtr += decHDR->firstZSDataOffset - sizeof(o2::header::RAWDataHeader);
std::vector<uint8_t> tmpBuffer;
bool extendFailure = false;
uint32_t nOutput = 0;
uint32_t minTimeBin = -1, maxTimeBin = 0;
for (uint32_t i = 0; i < decHDR->nTimebinHeaders; i++) {
int32_t sizeLeftInPage = payloadEnd - decPagePtr;
if (sizeLeftInPage <= 0) {
throw std::runtime_error("Decoding ran beyond end of page before processing extended timebin");
}
if (i == decHDR->nTimebinHeaders - 1 && (decHDR->flags & o2::tpc::TPCZSHDRV2::ZSFlags::payloadExtendsToNextPage)) {
if (o2::raw::RDHUtils::getMemorySize(*rdh) != TPCZSHDR::TPC_ZS_PAGE_SIZE) {
throw std::runtime_error("pageExtends signaled, but current page is not full");
}
const uint8_t* pageNext = ((const uint8_t*)decPage) + TPCZSHDR::TPC_ZS_PAGE_SIZE;
const o2::header::RAWDataHeader* rdhNext = (const o2::header::RAWDataHeader*)pageNext;
if ((uint16_t)(o2::raw::RDHUtils::getPageCounter(*rdh) + 1) != o2::raw::RDHUtils::getPageCounter(*rdhNext)) {
GPUError("Incomplete HBF: Payload extended to next page, but next page missing in stream (packet counters %d %d)", (int32_t)o2::raw::RDHUtils::getPageCounter(*rdh), (int32_t)o2::raw::RDHUtils::getPageCounter(*rdhNext));
extendFailure = true;
decPagePtr = payloadEnd; // Next 8kb page is missing in stream, cannot decode remaining data, skip it
break;
}
const TPCZSHDRV2* hdrNext = reinterpret_cast<const TPCZSHDRV2*>(pageNext + o2::raw::RDHUtils::getMemorySize(*rdhNext) - sizeof(TPCZSHDRV2));
tmpBuffer.resize(sizeLeftInPage + hdrNext->firstZSDataOffset - sizeof(o2::header::RAWDataHeader));
memcpy(tmpBuffer.data(), decPagePtr, sizeLeftInPage);
memcpy(tmpBuffer.data() + sizeLeftInPage, pageNext + sizeof(o2::header::RAWDataHeader), hdrNext->firstZSDataOffset - sizeof(o2::header::RAWDataHeader));
decPagePtr = tmpBuffer.data();
payloadEnd = decPagePtr + tmpBuffer.size();
}
uint8_t linkCount = *((const uint8_t*)decPagePtr) & 0x0F;
uint16_t linkBC = (*((const uint16_t*)decPagePtr) & 0xFFF0) >> 4;
bool v2Flag = decHDR->version == ZSVersion::ZSVersionDenseLinkBasedV2 && *((const uint8_t*)decPagePtr) & 0x10;
if (decHDR->version == ZSVersion::ZSVersionDenseLinkBasedV2) {
linkBC &= 0xFFC;
}
decPagePtr += sizeof(uint16_t);
std::vector<int32_t> links;
std::vector<std::bitset<80>> bitmasks;
uint32_t nTotalSamples = 0;
for (uint32_t l = 0; l < linkCount; l++) {
uint8_t decLinkX = *((const uint8_t*)decPagePtr);
decPagePtr += sizeof(uint8_t);
uint8_t decLink = decLinkX & 0b00011111;
std::bitset<10> bitmaskL2;
if (decLinkX & 0b00100000) {
bitmaskL2.set();
} else {
bitmaskL2 = std::bitset<10>(((((uint16_t)decLinkX) & 0b11000000) << 2) | (uint16_t)*((const uint8_t*)decPagePtr));
decPagePtr += sizeof(uint8_t);
}
std::bitset<80> bitmask(0);
for (int32_t i = 0; i < 10; i++) {
if (bitmaskL2.test(i)) {
bitmask |= std::bitset<80>(*((const uint8_t*)decPagePtr)) << i * 8;
decPagePtr += sizeof(uint8_t);
}
}
links.emplace_back(decLink);
bitmasks.emplace_back(bitmask);
nTotalSamples += bitmask.count();
}
const uint8_t* adcData = (const uint8_t*)decPagePtr;
int32_t encodeBitsBlock = v2Flag ? v2nbits : encodeBits;
decPagePtr += (nTotalSamples * encodeBitsBlock + 7) / 8;
// time bin might be smaller 0 due to triggerBC
int32_t timeBin = (int32_t(linkBC) + (int32_t)(o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstOrbit) * o2::constants::lhc::LHCMaxBunches - int32_t(triggerBC)) / LHCBCPERTIMEBIN;
if (timeBin < 0 || nTotalSamples == 0) {
if (timeBin < 0 && nTotalSamples > 0) {
LOGP(debug, "zsEncoderDenseLinkBased::decodePage skipping digits (linkBC {} + orbit {} - firstOrbit {}) * maxBunch {} - triggerBC {} = {} < 0, nTotalSamples {}", linkBC, o2::raw::RDHUtils::getHeartBeatOrbit(*rdh), firstOrbit, o2::constants::lhc::LHCMaxBunches, triggerBC, timeBin, nTotalSamples);
}
continue;
}
if (timeBin > maxTimeBin) {
maxTimeBin = timeBin;
}
if (timeBin < minTimeBin) {
minTimeBin = timeBin;
}
std::vector<uint16_t> samples(nTotalSamples);
uint32_t mask = (1 << encodeBitsBlock) - 1;
uint32_t byte = 0, bits = 0, posXbits = 0;
while (posXbits < nTotalSamples) {
byte |= *(adcData++) << bits;
bits += 8;
while (bits >= encodeBitsBlock && posXbits < nTotalSamples) {
samples[posXbits++] = byte & mask;
byte = byte >> encodeBitsBlock;
bits -= encodeBitsBlock;
}
}
uint32_t samplePos = 0;
for (uint32_t l = 0; l < linkCount; l++) {
uint8_t decLink = links[l];
const auto& bitmask = bitmasks[l];
int32_t nADC = bitmask.count();
for (int32_t j = 0; j < bitmask.size(); j++) {
if (bitmask[j]) {
int32_t sampaOnFEC = 0, channelOnSAMPA = 0;
mapper.getSampaAndChannelOnFEC(cruid, j, sampaOnFEC, channelOnSAMPA);
const auto padSecPos = mapper.padSecPos(cruid, decLink, sampaOnFEC, channelOnSAMPA);
const auto& padPos = padSecPos.getPadPos();
outputBuffer.emplace_back(o2::tpc::Digit{cruid, samples[samplePos++] * decodeBitsFactor, (tpccf::Row)padPos.getRow(), (tpccf::Pad)padPos.getPad(), timeBin});
nOutput++;
}
}
}
}
int32_t hdrMinTimeBin = (int32_t(decHDR->timeOffset) + int32_t(o2::raw::RDHUtils::getHeartBeatOrbit(*rdh) - firstOrbit) * o2::constants::lhc::LHCMaxBunches - triggerBC);
if (triggerBC > 0 && hdrMinTimeBin < 0) {
hdrMinTimeBin = 0;
}
hdrMinTimeBin /= LHCBCPERTIMEBIN;
int32_t hdrMaxTimeBin = hdrMinTimeBin + decHDR->nTimeBinSpan + ((decHDR->flags & TPCZSHDRV2::ZSFlags::nTimeBinSpanBit8) ? 256 : 0);