forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGPUTPCGMMerger.cxx
More file actions
2129 lines (1955 loc) · 88.7 KB
/
GPUTPCGMMerger.cxx
File metadata and controls
2129 lines (1955 loc) · 88.7 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 GPUTPCGMMerger.cxx
/// \author Sergey Gorbunov, David Rohr
#define GPUCA_CADEBUG 0
#define GPUCA_MERGE_LOOPER_MC 0
// #define GPUCA_CADEBUG_ENABLED
#include "GPUCommonDef.h"
#if !defined(GPUCA_GPUCODE) && (defined(GPUCA_MERGER_BY_MC_LABEL) || defined(GPUCA_CADEBUG_ENABLED) || GPUCA_MERGE_LOOPER_MC)
#include "AliHLTTPCClusterMCData.h"
#include "GPUROOTDump.h"
#endif
#ifndef GPUCA_GPUCODE_DEVICE
#include <cstdio>
#include <cstring>
#include <cmath>
#include "GPUReconstruction.h"
#endif
#include "GPUTPCTracker.h"
#include "GPUTPCTrackParam.h"
#include "GPUTPCGMMerger.h"
#include "GPUO2DataTypes.h"
#include "TPCFastTransform.h"
#include "GPUTPCConvertImpl.h"
#include "GPUTPCGeometry.h"
#include "GPUDefParametersRuntime.h"
#include "GPUCommonMath.h"
#include "GPUCommonAlgorithm.h"
#include "GPUCommonConstants.h"
#include "GPUTPCTrackParam.h"
#include "GPUTPCGMMergedTrack.h"
#include "GPUParam.h"
#include "GPUTPCTrackLinearisation.h"
#include "GPUTPCGMTrackParam.h"
#include "GPUTPCGMSectorTrack.h"
#include "GPUTPCGMBorderTrack.h"
#include "DataFormatsTPC/ClusterNative.h"
#include "DataFormatsTPC/TrackTPC.h"
#ifndef GPUCA_GPUCODE
#include "SimulationDataFormat/ConstMCTruthContainer.h"
#include "SimulationDataFormat/MCCompLabel.h"
#endif
using namespace o2::gpu;
using namespace o2::tpc;
using namespace gputpcgmmergertypes;
namespace o2::gpu::internal
{
struct MergeLooperParam {
float refz;
float x;
float y;
uint32_t id;
};
struct MergeBorderTracks_compMax {
GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b)
{
return GPUCA_DETERMINISTIC_CODE((a.fMax != b.fMax) ? (a.fMax < b.fMax) : (a.fId < b.fId), a.fMax < b.fMax);
}
};
struct MergeBorderTracks_compMin {
GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b)
{
return GPUCA_DETERMINISTIC_CODE((a.fMin != b.fMin) ? (a.fMin < b.fMin) : (a.fId < b.fId), a.fMin < b.fMin);
}
};
struct GPUTPCGMMergerSortTracks_comp {
const GPUTPCGMMergedTrack* const mCmp;
GPUhd() GPUTPCGMMergerSortTracks_comp(GPUTPCGMMergedTrack* cmp) : mCmp(cmp) {}
GPUd() bool operator()(const int32_t aa, const int32_t bb)
{
const GPUTPCGMMergedTrack& GPUrestrict() a = mCmp[aa];
const GPUTPCGMMergedTrack& GPUrestrict() b = mCmp[bb];
if (a.OK() != b.OK()) {
return a.OK();
}
if (a.CCE() != b.CCE()) {
return a.CCE() > b.CCE();
}
GPUCA_DETERMINISTIC_CODE( // clang-format off
if (a.NClusters() != b.NClusters()) {
return a.NClusters() > b.NClusters();
} if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) {
return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
} if (a.GetParam().GetY() != b.GetParam().GetY()) {
return a.GetParam().GetY() > b.GetParam().GetY();
}
return aa > bb;
, // !GPUCA_DETERMINISTIC_CODE
return a.NClusters() > b.NClusters();
) // clang-format on
}
};
struct GPUTPCGMMergerSortTracksQPt_comp {
const GPUTPCGMMergedTrack* const mCmp;
GPUhd() GPUTPCGMMergerSortTracksQPt_comp(GPUTPCGMMergedTrack* cmp) : mCmp(cmp) {}
GPUd() bool operator()(const int32_t aa, const int32_t bb)
{
const GPUTPCGMMergedTrack& GPUrestrict() a = mCmp[aa];
const GPUTPCGMMergedTrack& GPUrestrict() b = mCmp[bb];
GPUCA_DETERMINISTIC_CODE( // clang-format off
if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) {
return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
} if (a.GetParam().GetY() != b.GetParam().GetY()) {
return a.GetParam().GetY() > b.GetParam().GetY();
}
return a.GetParam().GetZ() > b.GetParam().GetZ();
, // !GPUCA_DETERMINISTIC_CODE
return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
) // clang-format on
}
};
struct GPUTPCGMMergerMergeLoopers_comp {
GPUd() bool operator()(const MergeLooperParam& a, const MergeLooperParam& b)
{
return GPUCA_DETERMINISTIC_CODE(CAMath::Abs(a.refz) != CAMath::Abs(b.refz) ? CAMath::Abs(a.refz) < CAMath::Abs(b.refz) : a.id < b.id, CAMath::Abs(a.refz) < CAMath::Abs(b.refz));
}
};
} // namespace o2::gpu::internal
using namespace o2::gpu::internal;
#ifndef GPUCA_GPUCODE
#include "GPUQA.h"
#include "GPUMemorySizeScalers.h"
GPUTPCGMMerger::GPUTPCGMMerger()
{
for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
mNextSectorInd[iSector] = iSector + 1;
mPrevSectorInd[iSector] = iSector - 1;
}
int32_t mid = NSECTORS / 2 - 1;
int32_t last = NSECTORS - 1;
mNextSectorInd[mid] = 0;
mPrevSectorInd[0] = mid;
mNextSectorInd[last] = NSECTORS / 2;
mPrevSectorInd[NSECTORS / 2] = last;
}
// DEBUG CODE
#if !defined(GPUCA_GPUCODE) && (defined(GPUCA_MERGER_BY_MC_LABEL) || defined(GPUCA_CADEBUG_ENABLED) || GPUCA_MERGE_LOOPER_MC)
#include "GPUQAHelper.h"
template <class T>
inline const auto* resolveMCLabels(const o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>* a, const AliHLTTPCClusterMCLabel* b)
{
return a;
}
template <>
inline const auto* resolveMCLabels<AliHLTTPCClusterMCLabel>(const o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>* a, const AliHLTTPCClusterMCLabel* b)
{
return b;
}
template <class T, class S>
int64_t GPUTPCGMMerger::GetTrackLabelA(const S& trk) const
{
GPUTPCGMSectorTrack* sectorTrack = nullptr;
int32_t nClusters = 0;
if constexpr (std::is_same_v<S, GPUTPCGMBorderTrack&>) {
sectorTrack = &mSectorTrackInfos[trk.TrackID()];
nClusters = sectorTrack->OrigTrack()->NHits();
} else {
nClusters = trk.NClusters();
}
auto acc = GPUTPCTrkLbl<false, GPUTPCTrkLbl_ret>(resolveMCLabels<T>(GetConstantMem()->ioPtrs.clustersNative ? GetConstantMem()->ioPtrs.clustersNative->clustersMCTruth : nullptr, GetConstantMem()->ioPtrs.mcLabelsTPC), 0.5f);
for (int32_t i = 0; i < nClusters; i++) {
int32_t id;
if constexpr (std::is_same_v<S, GPUTPCGMBorderTrack&>) {
const GPUTPCTracker& tracker = GetConstantMem()->tpcTrackers[sectorTrack->Sector()];
const GPUTPCHitId& ic = tracker.TrackHits()[sectorTrack->OrigTrack()->FirstHitID() + i];
id = tracker.Data().ClusterDataIndex(tracker.Data().Row(ic.RowIndex()), ic.HitIndex()) + GetConstantMem()->ioPtrs.clustersNative->clusterOffset[sectorTrack->Sector()][0];
} else {
id = mClusters[trk.FirstClusterRef() + i].num;
}
acc.addLabel(id);
}
return acc.computeLabel().id;
}
template <class S>
int64_t GPUTPCGMMerger::GetTrackLabel(const S& trk) const
{
#ifdef GPUCA_TPC_GEOMETRY_O2
if (GetConstantMem()->ioPtrs.clustersNative->clustersMCTruth) {
return GetTrackLabelA<o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>, S>(trk);
} else
#endif
{
return GetTrackLabelA<AliHLTTPCClusterMCLabel, S>(trk);
}
}
#endif
// END DEBUG CODE
void GPUTPCGMMerger::CheckCollectedTracks()
{
uint32_t nErr = 0;
for (uint32_t i = 0; i < mMemory->nMergedTracks; i++) {
const GPUTPCGMMergedTrack& trk = mMergedTracks[i];
if (trk.OK()) {
if (trk.NClusters() == 0) {
GPUError("FAILURE: Track marked ok but has 0 clusters");
nErr++;
}
if (!trk.CCE() && !trk.MergedLooper()) {
const GPUTPCGMMergedTrack* updTrk = &trk;
while (updTrk->PrevSegment() >= 0) {
auto next = &mMergedTracks[updTrk->PrevSegment()];
if (!next->MergedLooper()) {
GPUError("FAILURE: prev segment not marked as merged looper\n");
nErr++;
}
if (next == &trk) {
GPUError("FAILURE: segment cycle found\n");
break;
}
updTrk = next;
}
if (updTrk->NClusters() == 0) {
printf("FAILURE: segment leg has 0 clusters");
}
}
}
}
if (nErr == 0) {
GPUInfo("Merged Tracks OK");
} else {
throw std::runtime_error("Error during track merging");
}
}
void GPUTPCGMMerger::CheckMergeGraph()
{
uint32_t nErr = 0;
std::vector<bool> trkUsed(SectorTrackInfoLocalTotal());
for (int32_t i = 0; i < SectorTrackInfoLocalTotal(); i++) {
trkUsed[i] = false;
}
for (int32_t itr = 0; itr < SectorTrackInfoLocalTotal(); itr++) {
GPUTPCGMSectorTrack& track = mSectorTrackInfos[itr];
if (track.PrevSegmentNeighbour() >= 0 && mSectorTrackInfos[track.PrevSegmentNeighbour()].NextSegmentNeighbour() != itr) {
GPUError("FAILURE: Invalid reciprocal segment link: %d PrevSegmentNeighbour %d NextSegmentNeighbour %d", itr, track.PrevSegmentNeighbour(), mSectorTrackInfos[track.PrevSegmentNeighbour()].NextSegmentNeighbour());
nErr++;
}
if (track.NextSegmentNeighbour() >= 0 && mSectorTrackInfos[track.NextSegmentNeighbour()].PrevSegmentNeighbour() != itr) {
GPUError("FAILURE: Invalid reciprocal segment link: %d NextSegmentNeighbour %d PrevSegmentNeighbour %d", itr, track.NextSegmentNeighbour(), mSectorTrackInfos[track.NextSegmentNeighbour()].PrevSegmentNeighbour());
nErr++;
}
if (track.PrevNeighbour() >= 0 && mSectorTrackInfos[track.PrevNeighbour()].NextNeighbour() != itr) {
GPUError("FAILURE: Invalid reciprocal link: %d PrevNeighbour %d NextNeighbour %d", itr, track.PrevNeighbour(), mSectorTrackInfos[track.PrevNeighbour()].NextNeighbour());
nErr++;
}
if (track.NextNeighbour() >= 0 && mSectorTrackInfos[track.NextNeighbour()].PrevNeighbour() != itr) {
GPUError("FAILURE: Invalid reciprocal link: %d NextNeighbour %d PrevNeighbour %d", itr, track.NextNeighbour(), mSectorTrackInfos[track.NextNeighbour()].PrevNeighbour());
nErr++;
}
if (track.PrevSegmentNeighbour() >= 0) {
continue;
}
if (track.PrevNeighbour() >= 0) {
continue;
}
GPUTPCGMSectorTrack *trbase = &track, *tr = &track;
while (true) {
int32_t iTrk = tr - mSectorTrackInfos;
if (trkUsed[iTrk]) {
GPUError("FAILURE: double use");
nErr++;
break;
}
trkUsed[iTrk] = true;
int32_t jtr = tr->NextSegmentNeighbour();
if (jtr >= 0) {
tr = &(mSectorTrackInfos[jtr]);
if (tr->PrevNeighbour() >= 0) {
GPUError("FAILURE: Non-base segment has previous leg");
nErr++;
}
continue;
}
jtr = trbase->NextNeighbour();
if (jtr >= 0) {
trbase = &(mSectorTrackInfos[jtr]);
tr = trbase;
if (tr->PrevSegmentNeighbour() >= 0) {
GPUError("FAILURE: Neibhbour leg has previous segment neightbout");
nErr++;
break;
}
continue;
}
break;
}
}
for (int32_t i = 0; i < SectorTrackInfoLocalTotal(); i++) {
if (trkUsed[i] == false) {
GPUError("FAILURE: trk missed");
nErr++;
}
}
if (nErr == 0) {
GPUInfo("Merged Track Graph OK");
} else {
throw std::runtime_error("Invalid merge graph");
}
}
void GPUTPCGMMerger::PrintMergeGraph(const GPUTPCGMSectorTrack* trk, std::ostream& out) const
{
const GPUTPCGMSectorTrack* orgTrack = trk;
while (trk->PrevSegmentNeighbour() >= 0) {
trk = &mSectorTrackInfos[trk->PrevSegmentNeighbour()];
}
const GPUTPCGMSectorTrack* orgTower = trk;
while (trk->PrevNeighbour() >= 0) {
trk = &mSectorTrackInfos[trk->PrevNeighbour()];
}
int32_t nextId = trk - mSectorTrackInfos;
out << "Graph of track " << (orgTrack - mSectorTrackInfos) << "\n";
while (nextId >= 0) {
trk = &mSectorTrackInfos[nextId];
if (trk->PrevSegmentNeighbour() >= 0) {
out << "TRACK TREE INVALID!!! " << trk->PrevSegmentNeighbour() << " --> " << nextId << "\n";
}
out << (trk == orgTower ? "--" : " ");
while (nextId >= 0) {
GPUTPCGMSectorTrack* trk2 = &mSectorTrackInfos[nextId];
if (trk != trk2 && (trk2->PrevNeighbour() >= 0 || trk2->NextNeighbour() >= 0)) {
out << " (TRACK TREE INVALID!!! " << trk2->PrevNeighbour() << " <-- " << nextId << " --> " << trk2->NextNeighbour() << ") ";
}
char tmp[128];
snprintf(tmp, 128, " %s%5d(%5.2f)", trk2 == orgTrack ? "!" : " ", nextId, trk2->QPt());
out << tmp;
nextId = trk2->NextSegmentNeighbour();
}
out << "\n";
nextId = trk->NextNeighbour();
}
}
void GPUTPCGMMerger::InitializeProcessor() {}
void* GPUTPCGMMerger::SetPointersMerger(void* mem)
{
computePointerWithAlignment(mem, mSectorTrackInfos, mNTotalSectorTracks);
computePointerWithAlignment(mem, mSectorTrackInfoIndex, NSECTORS * 2 + 1);
if (mRec->GetProcessingSettings().deterministicGPUReconstruction) {
computePointerWithAlignment(mem, mTmpSortMemory, std::max(mNTotalSectorTracks, mNMaxTracks * 2));
}
void* memBase = mem;
computePointerWithAlignment(mem, mBorderMemory, 2 * mNTotalSectorTracks); // MergeBorders & Resolve
computePointerWithAlignment(mem, mBorderRangeMemory, 2 * mNTotalSectorTracks);
int32_t nTracks = 0;
for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
const int32_t n = *mRec->GetConstantMem().tpcTrackers[iSector].NTracks();
mBorder[iSector] = mBorderMemory + 2 * nTracks;
mBorder[NSECTORS + iSector] = mBorderMemory + 2 * nTracks + n;
mBorderRange[iSector] = mBorderRangeMemory + 2 * nTracks;
nTracks += n;
}
computePointerWithAlignment(mem, mTrackLinks, mNTotalSectorTracks);
computePointerWithAlignment(mem, mTrackCCRoots, mNTotalSectorTracks);
void* memMax = mem;
mem = memBase;
computePointerWithAlignment(mem, mTrackIDs, GPUCA_NSECTORS * mNMaxSingleSectorTracks); // UnpackResetIds - RefitSectorTracks - UnpackSectorGlobal
memMax = (void*)std::max((size_t)mem, (size_t)memMax);
mem = memBase;
computePointerWithAlignment(mem, mTrackSort, mNMaxTracks); // PrepareForFit0 - SortTracksQPt - PrepareForFit1 - PrepareForFit1 / Finalize0 - Finalize2
computePointerWithAlignment(mem, mSharedCount, mNMaxClusters);
memMax = (void*)std::max((size_t)mem, (size_t)memMax);
mem = memBase;
computePointerWithAlignment(mem, mLoopData, mNMaxTracks); // GPUTPCGMMergerTrackFit - GPUTPCGMMergerFollowLoopers
computePointerWithAlignment(mem, mRetryRefitIds, mNMaxTracks); // Reducing mNMaxTracks for mLoopData / mRetryRefitIds does not save memory, since the other parts are larger anyway
memMax = (void*)std::max((size_t)mem, (size_t)memMax);
mem = memBase;
computePointerWithAlignment(mem, mLooperCandidates, mNMaxLooperMatches); // MergeLoopers 1-3
memMax = (void*)std::max((size_t)mem, (size_t)memMax);
return memMax;
}
void* GPUTPCGMMerger::SetPointersMemory(void* mem)
{
computePointerWithAlignment(mem, mMemory);
return mem;
}
void* GPUTPCGMMerger::SetPointersRefitScratch(void* mem)
{
computePointerWithAlignment(mem, mTrackOrderAttach, mNMaxTracks);
const bool mergerSortTracks = mRec->GetProcessingSettings().mergerSortTracks == -1 ? mRec->getGPUParameters(mRec->GetRecoStepsGPU() & gpudatatypes::RecoStep::TPCMerging).par_SORT_BEFORE_FIT : mRec->GetProcessingSettings().mergerSortTracks;
if (mergerSortTracks) {
computePointerWithAlignment(mem, mTrackOrderProcess, mNMaxTracks);
}
return mem;
}
void* GPUTPCGMMerger::SetPointersOutput(void* mem)
{
computePointerWithAlignment(mem, mMergedTracks, mNMaxTracks);
if (mRec->GetParam().dodEdxEnabled) {
computePointerWithAlignment(mem, mMergedTracksdEdx, mNMaxTracks);
if (mRec->GetParam().rec.tpc.dEdxClusterRejectionFlagMask != mRec->GetParam().rec.tpc.dEdxClusterRejectionFlagMaskAlt) {
computePointerWithAlignment(mem, mMergedTracksdEdxAlt, mNMaxTracks);
}
}
computePointerWithAlignment(mem, mClusters, mNMaxMergedTrackClusters);
computePointerWithAlignment(mem, mClusterAttachment, mNMaxClusters);
return mem;
}
void* GPUTPCGMMerger::SetPointersOutputState(void* mem)
{
if ((mRec->GetRecoSteps() & gpudatatypes::RecoStep::Refit) || mRec->GetProcessingSettings().outputSharedClusterMap) {
computePointerWithAlignment(mem, mClusterStateExt, mNMaxClusters);
} else {
mClusterStateExt = nullptr;
}
return mem;
}
void* GPUTPCGMMerger::SetPointersOutputO2(void* mem)
{
computePointerWithAlignment(mem, mOutputTracksTPCO2, HostProcessor(this).NOutputTracksTPCO2());
return mem;
}
void* GPUTPCGMMerger::SetPointersOutputO2Clus(void* mem)
{
computePointerWithAlignment(mem, mOutputClusRefsTPCO2, HostProcessor(this).NOutputClusRefsTPCO2());
return mem;
}
void* GPUTPCGMMerger::SetPointersOutputO2MC(void* mem)
{
computePointerWithAlignment(mem, mOutputTracksTPCO2MC, HostProcessor(this).NOutputTracksTPCO2());
return mem;
}
void* GPUTPCGMMerger::SetPointersOutputO2Scratch(void* mem)
{
computePointerWithAlignment(mem, mTrackSortO2, mNMaxTracks);
computePointerWithAlignment(mem, mClusRefTmp, mNMaxTracks);
return mem;
}
void GPUTPCGMMerger::RegisterMemoryAllocation()
{
AllocateAndInitializeLate();
mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersMerger, GPUMemoryResource::MEMORY_SCRATCH | GPUMemoryResource::MEMORY_STACK, "TPCMerger");
mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersRefitScratch, GPUMemoryResource::MEMORY_SCRATCH | GPUMemoryResource::MEMORY_STACK, "TPCMergerRefitScratch");
mMemoryResOutput = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutput, (mRec->GetProcessingSettings().createO2Output > 1 ? GPUMemoryResource::MEMORY_SCRATCH : GPUMemoryResource::MEMORY_OUTPUT) | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutput");
mMemoryResOutputState = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutputState, (mRec->GetProcessingSettings().outputSharedClusterMap ? GPUMemoryResource::MEMORY_OUTPUT : GPUMemoryResource::MEMORY_GPU) | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutputState");
if (mRec->GetProcessingSettings().createO2Output) {
mMemoryResOutputO2Scratch = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutputO2Scratch, GPUMemoryResource::MEMORY_SCRATCH | GPUMemoryResource::MEMORY_STACK | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutputO2Scratch");
mMemoryResOutputO2 = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutputO2, GPUMemoryResource::MEMORY_OUTPUT | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutputO2");
mMemoryResOutputO2Clus = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutputO2Clus, GPUMemoryResource::MEMORY_OUTPUT | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutputO2Clus");
if (mRec->GetProcessingSettings().runMC) {
mMemoryResOutputO2MC = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersOutputO2MC, GPUMemoryResource::MEMORY_OUTPUT_FLAG | GPUMemoryResource::MEMORY_HOST | GPUMemoryResource::MEMORY_CUSTOM, "TPCMergerOutputO2MC");
}
}
mMemoryResMemory = mRec->RegisterMemoryAllocation(this, &GPUTPCGMMerger::SetPointersMemory, GPUMemoryResource::MEMORY_PERMANENT, "TPCMergerMemory");
}
void GPUTPCGMMerger::SetMaxData(const GPUTrackingInOutPointers& io)
{
mNTotalSectorTracks = 0;
mNClusters = 0;
mNMaxSingleSectorTracks = 0;
for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
uint32_t ntrk = *mRec->GetConstantMem().tpcTrackers[iSector].NTracks();
mNTotalSectorTracks += ntrk;
mNClusters += *mRec->GetConstantMem().tpcTrackers[iSector].NTrackHits();
if (mNMaxSingleSectorTracks < ntrk) {
mNMaxSingleSectorTracks = ntrk;
}
}
mNMaxMergedTrackClusters = mRec->MemoryScalers()->NTPCMergedTrackHits(mNClusters);
if (CAMath::Abs(Param().polynomialField.GetNominalBz()) < (gpu_common_constants::kZeroFieldCut * gpu_common_constants::kCLight)) {
mNMaxTracks = mRec->MemoryScalers()->getValue(mNTotalSectorTracks, mNTotalSectorTracks); // 0 magnetic field
} else {
mNMaxTracks = mRec->MemoryScalers()->NTPCMergedTracks(mNTotalSectorTracks);
}
if (io.clustersNative) {
mNMaxClusters = io.clustersNative->nClustersTotal;
} else if (mRec->GetRecoSteps() & gpudatatypes::RecoStep::TPCSectorTracking) {
mNMaxClusters = 0;
for (int32_t i = 0; i < NSECTORS; i++) {
mNMaxClusters += mRec->GetConstantMem().tpcTrackers[i].NHitsTotal();
}
} else {
mNMaxClusters = mNClusters;
}
mNMaxLooperMatches = mNMaxClusters / 4; // We have that much scratch memory anyway
}
int32_t GPUTPCGMMerger::CheckSectors()
{
for (int32_t i = 0; i < NSECTORS; i++) {
if (mRec->GetConstantMem().tpcTrackers[i].CommonMemory()->nLocalTracks > (int32_t)mNMaxSingleSectorTracks) {
throw std::runtime_error("mNMaxSingleSectorTracks too small");
}
}
if (!(mRec->GetRecoSteps() & gpudatatypes::RecoStep::TPCSectorTracking)) {
throw std::runtime_error("Must run also sector tracking");
}
return 0;
}
#endif // GPUCA_GPUCODE
GPUd() void GPUTPCGMMerger::ClearTrackLinks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, bool output)
{
const int32_t n = output ? mMemory->nMergedTracks : SectorTrackInfoLocalTotal();
for (int32_t i = iBlock * nThreads + iThread; i < n; i += nThreads * nBlocks) {
mTrackLinks[i] = -1;
}
}
GPUd() int32_t GPUTPCGMMerger::RefitSectorTrack(GPUTPCGMSectorTrack& sectorTrack, const GPUTPCTrack* inTrack, float alpha, int32_t sector)
{
GPUTPCGMPropagator prop;
prop.SetMaterialTPC();
prop.SetMaxSinPhi(GPUCA_MAX_SIN_PHI);
prop.SetSeedingErrors(true); // Larger errors for seeds, better since we don't start with good hypothesis
prop.SetFitInProjections(true); // TODO: Was false, consider reenabling after fitInProjection is fixed
prop.SetPolynomialField(&Param().polynomialField);
GPUTPCGMTrackParam trk;
trk.X() = inTrack->Param().GetX();
trk.Y() = inTrack->Param().GetY();
trk.Z() = inTrack->Param().GetZ();
trk.SinPhi() = inTrack->Param().GetSinPhi();
trk.DzDs() = inTrack->Param().GetDzDs();
trk.QPt() = inTrack->Param().GetQPt();
trk.TOffset() = Param().par.continuousTracking ? GetConstantMem()->calibObjects.fastTransformHelper->getCorrMap()->convZOffsetToVertexTime(sector, inTrack->Param().GetZOffset(), Param().continuousMaxTimeBin) : 0;
const auto tmp = sectorTrack.ClusterTN() > sectorTrack.ClusterT0() ? std::array<float, 2>{sectorTrack.ClusterTN(), sectorTrack.ClusterT0()} : std::array<float, 2>{sectorTrack.ClusterT0(), sectorTrack.ClusterTN()};
trk.ShiftZ(this, sector, tmp[0], tmp[1], inTrack->Param().GetX()); // We do not store the inner / outer cluster X, so we just use the track X instead
sectorTrack.SetX2(0.f);
for (int32_t way = 0; way < 2; way++) {
if (way) {
prop.SetFitInProjections(true);
prop.SetPropagateBzOnly(true);
}
trk.ResetCovariance();
prop.SetTrack(&trk, alpha);
int32_t start = way ? inTrack->NHits() - 1 : 0;
int32_t end = way ? 0 : (inTrack->NHits() - 1);
int32_t incr = way ? -1 : 1;
for (int32_t i = start; i != end; i += incr) {
float x, y, z;
int32_t row, flags;
const GPUTPCTracker& tracker = GetConstantMem()->tpcTrackers[sector];
const GPUTPCHitId& ic = tracker.TrackHits()[inTrack->FirstHitID() + i];
int32_t clusterIndex = tracker.Data().ClusterDataIndex(tracker.Data().Row(ic.RowIndex()), ic.HitIndex());
row = ic.RowIndex();
const ClusterNative& cl = GetConstantMem()->ioPtrs.clustersNative->clustersLinear[GetConstantMem()->ioPtrs.clustersNative->clusterOffset[sector][0] + clusterIndex];
flags = cl.getFlags();
GetConstantMem()->calibObjects.fastTransformHelper->Transform(sector, row, cl.getPad(), cl.getTime(), x, y, z, trk.TOffset());
if (prop.PropagateToXAlpha(x, alpha, way == 0)) {
return way == 0;
}
trk.ConstrainSinPhi();
if (prop.Update(y, z, row, Param(), flags & GPUTPCGMMergedTrackHit::clustererAndSharedFlags, 0, false, sector, -1.f, 0.f, 0.f)) { // TODO: Use correct time / avgCharge
return way == 0;
}
trk.ConstrainSinPhi();
}
if (way) {
sectorTrack.SetParam2(trk);
} else {
sectorTrack.Set(trk, inTrack, alpha, sector);
}
}
return 0;
}
GPUd() void GPUTPCGMMerger::SetTrackClusterT(GPUTPCGMSectorTrack& track, int32_t iSector, const GPUTPCTrack* sectorTr)
{
const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
const GPUTPCHitId& ic1 = trk.TrackHits()[sectorTr->FirstHitID()];
const GPUTPCHitId& ic2 = trk.TrackHits()[sectorTr->FirstHitID() + sectorTr->NHits() - 1];
int32_t clusterIndex1 = trk.Data().ClusterDataIndex(trk.Data().Row(ic1.RowIndex()), ic1.HitIndex());
int32_t clusterIndex2 = trk.Data().ClusterDataIndex(trk.Data().Row(ic2.RowIndex()), ic2.HitIndex());
const ClusterNative* cl = GetConstantMem()->ioPtrs.clustersNative->clustersLinear + GetConstantMem()->ioPtrs.clustersNative->clusterOffset[iSector][0];
track.SetClusterT(cl[clusterIndex1].getTime(), cl[clusterIndex2].getTime());
}
GPUd() void GPUTPCGMMerger::UnpackSaveNumber(int32_t id)
{
mSectorTrackInfoIndex[id] = mMemory->nUnpackedTracks;
}
GPUd() void GPUTPCGMMerger::UnpackSectorGlobal(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
{
const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
float alpha = Param().Alpha(iSector);
const GPUTPCTrack* sectorTr = mMemory->firstExtrapolatedTracks[iSector];
uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
uint32_t nTracks = *trk.NTracks();
for (uint32_t itr = nLocalTracks + iBlock * nThreads + iThread; itr < nTracks; itr += nBlocks * nThreads) {
sectorTr = &trk.Tracks()[itr];
int32_t localId = mTrackIDs[((sectorTr->LocalTrackId() >> 24) & 0x3F) * mNMaxSingleSectorTracks + (sectorTr->LocalTrackId() & 0xFFFFFF)];
if (localId == -1) {
continue;
}
uint32_t myTrack = CAMath::AtomicAdd(&mMemory->nUnpackedTracks, 1u);
GPUTPCGMSectorTrack& track = mSectorTrackInfos[myTrack];
SetTrackClusterT(track, iSector, sectorTr);
track.Set(this, sectorTr, alpha, iSector);
track.SetGlobalSectorTrackCov();
track.SetPrevNeighbour(-1);
track.SetNextNeighbour(-1);
track.SetNextSegmentNeighbour(-1);
track.SetPrevSegmentNeighbour(-1);
track.SetLocalTrackId(localId | (sectorTr->LocalTrackId() & 0x40000000));
}
}
GPUd() void GPUTPCGMMerger::UnpackResetIds(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
{
const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
for (uint32_t i = iBlock * nThreads + iThread; i < nLocalTracks; i += nBlocks * nThreads) {
mTrackIDs[iSector * mNMaxSingleSectorTracks + i] = -1;
}
}
GPUd() void GPUTPCGMMerger::RefitSectorTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
{
const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
float alpha = Param().Alpha(iSector);
const GPUTPCTrack* sectorTr = nullptr;
for (uint32_t itr = iBlock * nThreads + iThread; itr < nLocalTracks; itr += nBlocks * nThreads) {
sectorTr = &trk.Tracks()[itr];
GPUTPCGMSectorTrack track;
SetTrackClusterT(track, iSector, sectorTr);
if (RefitSectorTrack(track, sectorTr, alpha, iSector)) {
track.Set(this, sectorTr, alpha, iSector); // TODO: Why does the refit fail, it shouldn't, this workaround should be removed
if (!track.FilterErrors(this, iSector, GPUCA_MAX_SIN_PHI, 0.1f)) {
continue;
}
}
CADEBUG(GPUInfo("INPUT Sector %d, Track %u, QPt %f DzDs %f", iSector, itr, track.QPt(), track.DzDs()));
track.SetPrevNeighbour(-1);
track.SetNextNeighbour(-1);
track.SetNextSegmentNeighbour(-1);
track.SetPrevSegmentNeighbour(-1);
track.SetExtrapolatedTrackId(0, -1);
track.SetExtrapolatedTrackId(1, -1);
uint32_t myTrack = CAMath::AtomicAdd(&mMemory->nUnpackedTracks, 1u);
mTrackIDs[iSector * mNMaxSingleSectorTracks + sectorTr->LocalTrackId()] = myTrack;
mSectorTrackInfos[myTrack] = track;
}
}
GPUd() void GPUTPCGMMerger::LinkExtrapolatedTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
{
for (int32_t itr = SectorTrackInfoGlobalFirst(0) + iBlock * nThreads + iThread; itr < SectorTrackInfoGlobalLast(NSECTORS - 1); itr += nThreads * nBlocks) {
GPUTPCGMSectorTrack& extrapolatedTrack = mSectorTrackInfos[itr];
GPUTPCGMSectorTrack& localTrack = mSectorTrackInfos[extrapolatedTrack.LocalTrackId() & 0xFFFFFF];
int up = (extrapolatedTrack.LocalTrackId() & 0x40000000) ? 1 : 0;
localTrack.SetExtrapolatedTrackId(up, itr);
}
}
GPUd() void GPUTPCGMMerger::MergeSectorsPrepareStep2(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iBorder, GPUTPCGMBorderTrack** B, GPUAtomic(uint32_t) * nB, bool useOrigTrackParam)
{
//* prepare sector tracks for merging with next/previous/same sector
//* each track transported to the border line
float fieldBz = Param().bzCLight;
float dAlpha = Param().dAlpha / 2;
float x0 = 0;
if (iBorder == 0) { // transport to the left edge of the sector and rotate horizontally
dAlpha = dAlpha - CAMath::Pi() / 2;
} else if (iBorder == 1) { // transport to the right edge of the sector and rotate horizontally
dAlpha = -dAlpha - CAMath::Pi() / 2;
} else if (iBorder == 2) { // transport to the middle of the sector and rotate vertically to the border on the left
x0 = GPUTPCGeometry::Row2X(63);
} else if (iBorder == 3) { // transport to the middle of the sector and rotate vertically to the border on the right
dAlpha = -dAlpha;
x0 = GPUTPCGeometry::Row2X(63);
}
const float maxSin = CAMath::Sin(60.f / 180.f * CAMath::Pi());
float cosAlpha = CAMath::Cos(dAlpha);
float sinAlpha = CAMath::Sin(dAlpha);
GPUTPCGMSectorTrack trackTmp;
for (int32_t itr = iBlock * nThreads + iThread; itr < SectorTrackInfoLocalTotal(); itr += nThreads * nBlocks) {
const GPUTPCGMSectorTrack* track = &mSectorTrackInfos[itr];
int32_t iSector = track->Sector();
if (track->PrevSegmentNeighbour() >= 0 && track->Sector() == mSectorTrackInfos[track->PrevSegmentNeighbour()].Sector()) {
continue;
}
if (useOrigTrackParam) { // TODO: Check how far this makes sense with sector track refit
if (CAMath::Abs(track->QPt()) * Param().qptB5Scaler < Param().rec.tpc.mergerLooperQPtB5Limit) {
continue;
}
const GPUTPCGMSectorTrack* trackMin = track;
while (track->NextSegmentNeighbour() >= 0 && track->Sector() == mSectorTrackInfos[track->NextSegmentNeighbour()].Sector()) {
track = &mSectorTrackInfos[track->NextSegmentNeighbour()];
if (track->OrigTrack()->Param().X() < trackMin->OrigTrack()->Param().X()) {
trackMin = track;
}
}
trackTmp = *trackMin;
track = &trackTmp;
if (trackTmp.X2() != 0.f) {
trackTmp.UseParam2();
} else {
trackTmp.Set(this, trackMin->OrigTrack(), trackMin->Alpha(), trackMin->Sector());
}
} else {
if (CAMath::Abs(track->QPt()) * Param().qptB5Scaler < Param().rec.tpc.mergerLooperSecondHorizontalQPtB5Limit) {
if (iBorder == 0 && track->NextNeighbour() >= 0) {
continue;
}
if (iBorder == 1 && track->PrevNeighbour() >= 0) {
continue;
}
}
}
GPUTPCGMBorderTrack b;
if (track->TransportToXAlpha(this, x0, sinAlpha, cosAlpha, fieldBz, b, maxSin)) {
b.SetTrackID(itr);
b.SetNClusters(track->NClusters());
for (int32_t i = 0; i < 4; i++) {
if (CAMath::Abs(b.Cov()[i]) >= 5.0f) {
b.SetCov(i, 5.0f);
}
}
if (CAMath::Abs(b.Cov()[4]) >= 0.5f) {
b.SetCov(4, 0.5f);
}
uint32_t myTrack = CAMath::AtomicAdd(&nB[iSector], 1u);
B[iSector][myTrack] = b;
}
}
}
template <>
GPUd() void GPUTPCGMMerger::MergeBorderTracks<0>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector1, const GPUTPCGMBorderTrack* B1, int32_t N1, int32_t iSector2, const GPUTPCGMBorderTrack* B2, int32_t N2, int32_t mergeMode)
{
CADEBUG(GPUInfo("\nMERGING Sectors %d %d NTracks %d %d CROSS %d", iSector1, iSector2, N1, N2, mergeMode));
GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
bool sameSector = (iSector1 == iSector2);
for (int32_t itr = iBlock * nThreads + iThread; itr < N1; itr += nThreads * nBlocks) {
const GPUTPCGMBorderTrack& b = B1[itr];
float d = CAMath::Max(0.5f, 3.5f * CAMath::Sqrt(b.Cov()[1]));
if (CAMath::Abs(b.Par()[4]) * Param().qptB5Scaler >= 20) {
d *= 2;
} else if (d > 3) {
d = 3;
}
CADEBUG(printf(" Input Sector 1 %d Track %d: ", iSector1, itr); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b.Par()[i]); } printf(" - "); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b.Cov()[i]); } printf(" - D %8.3f\n", d));
GPUTPCGMBorderRange range;
range.fId = itr;
range.fMin = b.Par()[1] + b.ZOffsetLinear() - d;
range.fMax = b.Par()[1] + b.ZOffsetLinear() + d;
range1[itr] = range;
if (sameSector) {
range2[itr] = range;
}
}
if (!sameSector) {
for (int32_t itr = iBlock * nThreads + iThread; itr < N2; itr += nThreads * nBlocks) {
const GPUTPCGMBorderTrack& b = B2[itr];
float d = CAMath::Max(0.5f, 3.5f * CAMath::Sqrt(b.Cov()[1]));
if (CAMath::Abs(b.Par()[4]) * Param().qptB5Scaler >= 20) {
d *= 2;
} else if (d > 3) {
d = 3;
}
CADEBUG(printf(" Input Sector 2 %d Track %d: ", iSector2, itr); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b.Par()[i]); } printf(" - "); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b.Cov()[i]); } printf(" - D %8.3f\n", d));
GPUTPCGMBorderRange range;
range.fId = itr;
range.fMin = b.Par()[1] + b.ZOffsetLinear() - d;
range.fMax = b.Par()[1] + b.ZOffsetLinear() + d;
range2[itr] = range;
}
}
}
template <>
GPUd() void GPUTPCGMMerger::MergeBorderTracks<1>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector1, const GPUTPCGMBorderTrack* B1, int32_t N1, int32_t iSector2, const GPUTPCGMBorderTrack* B2, int32_t N2, int32_t mergeMode)
{
#if !defined(GPUCA_GPUCODE_COMPILEKERNELS)
GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
if (iThread == 0) {
if (iBlock == 0) {
GPUCommonAlgorithm::sortDeviceDynamic(range1, range1 + N1, [](const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b) { return GPUCA_DETERMINISTIC_CODE((a.fMin != b.fMin) ? (a.fMin < b.fMin) : (a.fId < b.fId), a.fMin < b.fMin); });
} else if (iBlock == 1) {
GPUCommonAlgorithm::sortDeviceDynamic(range2, range2 + N2, [](const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b) { return GPUCA_DETERMINISTIC_CODE((a.fMax != b.fMax) ? (a.fMax < b.fMax) : (a.fId < b.fId), a.fMax < b.fMax); });
}
}
#else
printf("This sorting variant is disabled for RTC");
#endif
}
template <>
GPUd() void GPUTPCGMMerger::MergeBorderTracks<3>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUTPCGMBorderRange* range, int32_t N, int32_t cmpMax)
{
#ifndef GPUCA_SPECIALIZE_THRUST_SORTS
if (iThread == 0 && iBlock == 0) {
if (cmpMax) {
GPUCommonAlgorithm::sortDeviceDynamic(range, range + N, MergeBorderTracks_compMax());
} else {
GPUCommonAlgorithm::sortDeviceDynamic(range, range + N, MergeBorderTracks_compMin());
}
}
#endif
}
template <>
GPUd() void GPUTPCGMMerger::MergeBorderTracks<2>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector1, const GPUTPCGMBorderTrack* B1, int32_t N1, int32_t iSector2, const GPUTPCGMBorderTrack* B2, int32_t N2, int32_t mergeMode)
{
// int32_t statAll = 0, statMerged = 0;
float factor2ys = Param().rec.tpc.trackMergerFactor2YS;
float factor2zt = Param().rec.tpc.trackMergerFactor2ZT;
float factor2k = Param().rec.tpc.trackMergerFactor2K;
float factor2General = Param().rec.tpc.trackMergerFactor2General;
factor2k = factor2General * factor2k;
factor2ys = factor2General * factor2ys;
factor2zt = factor2General * factor2zt;
int32_t minNPartHits = Param().rec.tpc.trackMergerMinPartHits;
int32_t minNTotalHits = Param().rec.tpc.trackMergerMinTotalHits;
bool sameSector = (iSector1 == iSector2);
GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
int32_t i2 = 0;
for (int32_t i1 = iBlock * nThreads + iThread; i1 < N1; i1 += nThreads * nBlocks) {
GPUTPCGMBorderRange r1 = range1[i1];
while (i2 < N2 && range2[i2].fMax < r1.fMin) {
i2++;
}
const GPUTPCGMBorderTrack& b1 = B1[r1.fId];
if (b1.NClusters() < minNPartHits) {
continue;
}
int32_t iBest2 = -1;
int32_t lBest2 = 0;
// statAll++;
for (int32_t k2 = i2; k2 < N2; k2++) {
GPUTPCGMBorderRange r2 = range2[k2];
if (r2.fMin > r1.fMax) {
break;
}
if (sameSector && (r1.fId >= r2.fId)) {
continue;
}
// do check
const GPUTPCGMBorderTrack& b2 = B2[r2.fId];
#if defined(GPUCA_MERGER_BY_MC_LABEL) && !defined(GPUCA_GPUCODE)
int64_t label1 = GetTrackLabel(b1);
int64_t label2 = GetTrackLabel(b2);
if (label1 != label2 && label1 != -1) // DEBUG CODE, match by MC label
#endif
{
CADEBUG(if (GetConstantMem()->ioPtrs.mcLabelsTPC) {printf("Comparing track %3d to %3d: ", r1.fId, r2.fId); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b1.Par()[i]); } printf(" - "); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b1.Cov()[i]); } printf("\n%28s", ""); });
CADEBUG(if (GetConstantMem()->ioPtrs.mcLabelsTPC) {for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b2.Par()[i]); } printf(" - "); for (int32_t i = 0; i < 5; i++) { printf("%8.3f ", b2.Cov()[i]); } printf(" - %5s - ", GetTrackLabel(b1) == GetTrackLabel(b2) ? "CLONE" : "FAKE"); });
if (b2.NClusters() < lBest2) {
CADEBUG2(continue, printf("!NCl1\n"));
}
if (mergeMode > 0) {
// Merging CE tracks
int32_t maxRowDiff = mergeMode == 2 ? 1 : 3; // TODO: check cut
if (CAMath::Abs(b1.Row() - b2.Row()) > maxRowDiff) {
CADEBUG2(continue, printf("!ROW\n"));
}
if (CAMath::Abs(b1.Par()[2] - b2.Par()[2]) > 0.5f || CAMath::Abs(b1.Par()[3] - b2.Par()[3]) > 0.5f) {
CADEBUG2(continue, printf("!CE SinPhi/Tgl\n")); // Crude cut to avoid totally wrong matches, TODO: check cut
}
}
GPUCA_DEBUG_STREAMER_CHECK(float weight = b1.Par()[4] * b1.Par()[4]; if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamMergeBorderTracksAll, b1.TrackID(), weight)) { MergedTrackStreamer(b1, b2, "merge_all_tracks", iSector1, iSector2, mergeMode, weight, o2::utils::DebugStreamer::getSamplingFrequency(o2::utils::StreamFlags::streamMergeBorderTracksAll)); });
if (!b1.CheckChi2Y(b2, factor2ys)) {
CADEBUG2(continue, printf("!Y\n"));
}
// if( !b1.CheckChi2Z(b2, factor2zt ) ) CADEBUG2(continue, printf("!NCl1\n"));
if (!b1.CheckChi2QPt(b2, factor2k)) {
CADEBUG2(continue, printf("!QPt\n"));
}
float fys = CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20 ? factor2ys : (2.f * factor2ys);
float fzt = CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20 ? factor2zt : (2.f * factor2zt);
if (!b1.CheckChi2YS(b2, fys)) {
CADEBUG2(continue, printf("!YS\n"));
}
if (!b1.CheckChi2ZT(b2, fzt)) {
CADEBUG2(continue, printf("!ZT\n"));
}
if (CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20) {
if (b2.NClusters() < minNPartHits) {
CADEBUG2(continue, printf("!NCl2\n"));
}
if (b1.NClusters() + b2.NClusters() < minNTotalHits) {
CADEBUG2(continue, printf("!NCl3\n"));
}
}
CADEBUG(printf("OK: dZ %8.3f D1 %8.3f D2 %8.3f\n", CAMath::Abs(b1.Par()[1] - b2.Par()[1]), 3.5f * sqrt(b1.Cov()[1]), 3.5f * sqrt(b2.Cov()[1])));
} // DEBUG CODE, match by MC label
lBest2 = b2.NClusters();
iBest2 = b2.TrackID();
}
if (iBest2 < 0) {
continue;
}
GPUCA_DEBUG_STREAMER_CHECK(float weight = b1.Par()[4] * b1.Par()[4]; if (o2::utils::DebugStreamer::checkStream(o2::utils::StreamFlags::streamMergeBorderTracksBest, b1.TrackID(), weight)) { MergedTrackStreamer(b1, MergedTrackStreamerFindBorderTrack(B2, N2, iBest2), "merge_best_track", iSector1, iSector2, mergeMode, weight, o2::utils::DebugStreamer::getSamplingFrequency(o2::utils::StreamFlags::streamMergeBorderTracksBest)); });
// statMerged++;
CADEBUG(GPUInfo("Found match %d %d", b1.TrackID(), iBest2));
mTrackLinks[b1.TrackID()] = iBest2;
if (mergeMode > 0) {
GPUCA_DETERMINISTIC_CODE(CAMath::AtomicMax(&mTrackLinks[iBest2], b1.TrackID()), mTrackLinks[iBest2] = b1.TrackID());
}
}
// GPUInfo("STAT: sectors %d, %d: all %d merged %d", iSector1, iSector2, statAll, statMerged);
}
GPUdii() void GPUTPCGMMerger::MergeBorderTracksSetup(int32_t& n1, int32_t& n2, GPUTPCGMBorderTrack*& b1, GPUTPCGMBorderTrack*& b2, int32_t& jSector, int32_t iSector, int8_t withinSector, int8_t mergeMode) const
{
if (withinSector == 1) { // Merge tracks within the same sector
jSector = iSector;
n1 = n2 = mMemory->tmpCounter[iSector];
b1 = b2 = mBorder[iSector];
} else if (withinSector == -1) { // Merge tracks accross the central electrode
jSector = (iSector + NSECTORS / 2);
const int32_t offset = mergeMode == 2 ? NSECTORS : 0;
n1 = mMemory->tmpCounter[iSector + offset];
n2 = mMemory->tmpCounter[jSector + offset];
b1 = mBorder[iSector + offset];
b2 = mBorder[jSector + offset];
} else { // Merge tracks of adjacent sectors
jSector = mNextSectorInd[iSector];
n1 = mMemory->tmpCounter[iSector];
n2 = mMemory->tmpCounter[NSECTORS + jSector];
b1 = mBorder[iSector];
b2 = mBorder[NSECTORS + jSector];
}
}
template <int32_t I>
GPUd() void GPUTPCGMMerger::MergeBorderTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector, int8_t withinSector, int8_t mergeMode)
{
int32_t n1, n2;