forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackingKernels.cu
More file actions
1524 lines (1441 loc) · 70.8 KB
/
TrackingKernels.cu
File metadata and controls
1524 lines (1441 loc) · 70.8 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.
///
#include <cuda_runtime.h>
#include <array>
#include <unistd.h>
#include <thrust/execution_policy.h>
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <thrust/unique.h>
#include <thrust/remove.h>
#include "ITStracking/Constants.h"
#include "ITStracking/Definitions.h"
#include "ITStracking/IndexTableUtils.h"
#include "ITStracking/MathUtils.h"
#include "ITStracking/ExternalAllocator.h"
#include "ITStracking/Tracklet.h"
#include "ITStracking/Cluster.h"
#include "ITStracking/Cell.h"
#include "DataFormatsITS/TrackITS.h"
#include "ITStrackingGPU/TrackingKernels.h"
#include "ITStrackingGPU/Utils.h"
#include "utils/strtag.h"
// O2 track model
#include "ReconstructionDataFormats/Track.h"
#include "DetectorsBase/Propagator.h"
using namespace o2::track;
namespace o2::its
{
namespace gpu
{
GPUdii() bool fitTrack(TrackITSExt& track,
int start,
int end,
int step,
float chi2clcut,
float chi2ndfcut,
float maxQoverPt,
int nCl,
float bz,
const TrackingFrameInfo** tfInfos,
const o2::base::Propagator* prop,
o2::base::PropagatorF::MatCorrType matCorrType,
o2::track::TrackPar* linRef,
const bool shiftRefToCluster)
{
for (int iLayer{start}; iLayer != end; iLayer += step) {
if (track.getClusterIndex(iLayer) == constants::UnusedIndex) {
continue;
}
const TrackingFrameInfo& trackingHit = tfInfos[iLayer][track.getClusterIndex(iLayer)];
if (linRef) {
if (!track.o2::track::TrackParCovF::rotate(trackingHit.alphaTrackingFrame, *linRef, bz)) {
return false;
}
if (!prop->propagateToX(track,
*linRef,
trackingHit.xTrackingFrame,
bz,
o2::base::PropagatorImpl<float>::MAX_SIN_PHI,
o2::base::PropagatorImpl<float>::MAX_STEP,
matCorrType)) {
return false;
}
if (matCorrType == o2::base::PropagatorF::MatCorrType::USEMatCorrNONE) {
const float xx0 = (iLayer > 2) ? 1.e-2f : 5.e-3f; // Rough layer thickness
if (!track.correctForMaterial(*linRef, xx0, xx0 * constants::Radl * constants::Rho, true)) {
return false;
}
}
} else {
if (!track.o2::track::TrackParCovF::rotate(trackingHit.alphaTrackingFrame)) {
return false;
}
if (!prop->propagateToX(track,
trackingHit.xTrackingFrame,
bz,
o2::base::PropagatorImpl<float>::MAX_SIN_PHI,
o2::base::PropagatorImpl<float>::MAX_STEP,
matCorrType)) {
return false;
}
if (matCorrType == o2::base::PropagatorF::MatCorrType::USEMatCorrNONE) {
const float xx0 = (iLayer > 2) ? 1.e-2f : 5.e-3f; // Rough layer thickness
if (!track.correctForMaterial(xx0, xx0 * constants::Radl * constants::Rho, true)) {
return false;
}
}
}
auto predChi2{track.getPredictedChi2(trackingHit.positionTrackingFrame, trackingHit.covarianceTrackingFrame)};
if ((nCl >= 3 && predChi2 > chi2clcut) || predChi2 < 0.f) {
return false;
}
track.setChi2(track.getChi2() + predChi2);
if (!track.o2::track::TrackParCov::update(trackingHit.positionTrackingFrame, trackingHit.covarianceTrackingFrame)) {
return false;
}
if (linRef && shiftRefToCluster) { // displace the reference to the last updated cluster
linRef->setY(trackingHit.positionTrackingFrame[0]);
linRef->setZ(trackingHit.positionTrackingFrame[1]);
}
nCl++;
}
return o2::gpu::CAMath::Abs(track.getQ2Pt()) < maxQoverPt && track.getChi2() < chi2ndfcut * (nCl * 2 - 5);
}
GPUdii() o2::track::TrackParCov buildTrackSeed(const Cluster& cluster1,
const Cluster& cluster2,
const TrackingFrameInfo& tf3,
const float bz,
const bool reverse = false)
{
const float sign = reverse ? -1.f : 1.f;
float ca, sa;
o2::gpu::CAMath::SinCos(tf3.alphaTrackingFrame, sa, ca);
const float x1 = cluster1.xCoordinate * ca + cluster1.yCoordinate * sa;
const float y1 = -cluster1.xCoordinate * sa + cluster1.yCoordinate * ca;
const float x2 = cluster2.xCoordinate * ca + cluster2.yCoordinate * sa;
const float y2 = -cluster2.xCoordinate * sa + cluster2.yCoordinate * ca;
const float x3 = tf3.xTrackingFrame;
const float y3 = tf3.positionTrackingFrame[0];
float snp, q2pt, q2pt2;
if (o2::gpu::CAMath::Abs(bz) < 0.01f) {
const float dx = x3 - x1;
const float dy = y3 - y1;
snp = sign * dy / o2::gpu::CAMath::Hypot(dx, dy);
q2pt = sign / track::kMostProbablePt;
q2pt2 = 1.f;
} else {
const float crv = math_utils::computeCurvature(x3, y3, x2, y2, x1, y1);
snp = sign * crv * (x3 - math_utils::computeCurvatureCentreX(x3, y3, x2, y2, x1, y1));
q2pt = sign * crv / (bz * o2::constants::math::B2C);
q2pt2 = crv * crv;
}
const float tgl = 0.5f * (math_utils::computeTanDipAngle(x1, y1, x2, y2, cluster1.zCoordinate, cluster2.zCoordinate) +
math_utils::computeTanDipAngle(x2, y2, x3, y3, cluster2.zCoordinate, tf3.positionTrackingFrame[1]));
const float sg2q2pt = track::kC1Pt2max * (q2pt2 > 0.0005f ? (q2pt2 < 1.f ? q2pt2 : 1.f) : 0.0005f);
return {x3, tf3.alphaTrackingFrame, {y3, tf3.positionTrackingFrame[1], snp, tgl, q2pt}, {tf3.covarianceTrackingFrame[0], tf3.covarianceTrackingFrame[1], tf3.covarianceTrackingFrame[2], 0.f, 0.f, track::kCSnp2max, 0.f, 0.f, 0.f, track::kCTgl2max, 0.f, 0.f, 0.f, 0.f, sg2q2pt}};
}
template <int nLayers>
GPUdii() TrackITSExt seedTrackForRefit(const CellSeed<nLayers>& seed,
const TrackingFrameInfo** foundTrackingFrameInfo,
const Cluster** unsortedClusters,
const float* layerRadii,
const float bz,
const int reseedIfShorter)
{
TrackITSExt temporaryTrack(seed);
int lrMin = nLayers, lrMax = 0, lrMid = 0;
for (int iL{0}; iL < nLayers; ++iL) {
const int idx = seed.getCluster(iL);
temporaryTrack.setExternalClusterIndex(iL, idx, idx != constants::UnusedIndex);
if (idx != constants::UnusedIndex) {
// TODO only works if does not have holes
lrMin = o2::gpu::CAMath::Min(lrMin, iL);
lrMax = o2::gpu::CAMath::Max(lrMax, iL);
}
}
const int ncl = temporaryTrack.getNClusters();
if (ncl < reseedIfShorter && ncl > 0) { // need to check if there are any clusters since we keep invalidate seeeds around
if (ncl == nLayers) {
lrMin = 0;
lrMax = nLayers - 1;
lrMid = (lrMin + lrMax) / 2;
} else {
lrMid = lrMin + 1;
float midR = 0.5f * (layerRadii[lrMax] + layerRadii[lrMin]), dstMidR = o2::gpu::CAMath::Abs(midR - layerRadii[lrMid]);
for (int iL = lrMid + 1; iL < lrMax; ++iL) { // find the midpoint as closest to the midR
auto dst = o2::gpu::GPUCommonMath::Abs(midR - layerRadii[iL]);
if (dst < dstMidR) {
lrMid = iL;
dstMidR = dst;
}
}
}
const auto& cluster0_tf = foundTrackingFrameInfo[lrMin][seed.getCluster(lrMin)];
const auto& cluster1_gl = unsortedClusters[lrMid][seed.getCluster(lrMid)];
const auto& cluster2_gl = unsortedClusters[lrMax][seed.getCluster(lrMax)];
temporaryTrack.getParamIn() = buildTrackSeed(cluster2_gl, cluster1_gl, cluster0_tf, bz, true);
}
temporaryTrack.resetCovariance();
temporaryTrack.setCov(temporaryTrack.getQ2Pt() * temporaryTrack.getQ2Pt() * temporaryTrack.getCov()[o2::track::CovLabels::kSigQ2Pt2], o2::track::CovLabels::kSigQ2Pt2);
return temporaryTrack;
}
struct sort_tracklets {
GPUhd() bool operator()(const Tracklet& a, const Tracklet& b)
{
if (a.firstClusterIndex != b.firstClusterIndex) {
return a.firstClusterIndex < b.firstClusterIndex;
}
return a.secondClusterIndex < b.secondClusterIndex;
}
};
struct equal_tracklets {
GPUhd() bool operator()(const Tracklet& a, const Tracklet& b) { return a.firstClusterIndex == b.firstClusterIndex && a.secondClusterIndex == b.secondClusterIndex; }
};
template <typename T1, typename T2>
struct sort_by_second {
GPUhd() bool operator()(const gpuPair<T1, T2>& a, const gpuPair<T1, T2>& b) const { return a.second < b.second; }
};
template <typename T1, typename T2>
struct pair_to_first {
GPUhd() int operator()(const gpuPair<T1, T2>& a) const
{
return a.first;
}
};
template <typename T1, typename T2>
struct pair_to_second {
GPUhd() int operator()(const gpuPair<T1, T2>& a) const
{
return a.second;
}
};
template <typename T1, typename T2>
struct is_invalid_pair {
GPUhd() bool operator()(const gpuPair<T1, T2>& p) const
{
return p.first == -1 && p.second == -1;
}
};
template <typename T1, typename T2>
struct is_valid_pair {
GPUhd() bool operator()(const gpuPair<T1, T2>& p) const
{
return !(p.first == -1 && p.second == -1);
}
};
template <int nLayers>
struct seed_selector {
float maxQ2Pt;
float maxChi2;
GPUhd() seed_selector(float maxQ2Pt, float maxChi2) : maxQ2Pt(maxQ2Pt), maxChi2(maxChi2) {}
GPUhd() bool operator()(const CellSeed<nLayers>& seed) const
{
return !(seed.getQ2Pt() > maxQ2Pt || seed.getChi2() > maxChi2);
}
};
struct compare_track_chi2 {
GPUhd() bool operator()(const TrackITSExt& a, const TrackITSExt& b) const
{
return a.getChi2() < b.getChi2();
}
};
template <bool initRun, int nLayers>
GPUg() void __launch_bounds__(256, 1) fitTrackSeedsKernel(
CellSeed<nLayers>* trackSeeds,
const TrackingFrameInfo** foundTrackingFrameInfo,
const Cluster** unsortedClusters,
o2::its::TrackITSExt* tracks,
maybe_const<!initRun, int>* seedLUT,
const float* layerRadii,
const float* minPts,
const unsigned int nSeeds,
const float bz,
const int startLevel,
const float maxChi2ClusterAttachment,
const float maxChi2NDF,
const int reseedIfShorter,
const bool repeatRefitOut,
const bool shifRefToCluster,
const o2::base::Propagator* propagator,
const o2::base::PropagatorF::MatCorrType matCorrType)
{
for (int iCurrentTrackSeedIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentTrackSeedIndex < nSeeds; iCurrentTrackSeedIndex += blockDim.x * gridDim.x) {
if constexpr (!initRun) {
if (seedLUT[iCurrentTrackSeedIndex] == seedLUT[iCurrentTrackSeedIndex + 1]) {
continue;
}
}
TrackITSExt temporaryTrack = seedTrackForRefit<nLayers>(trackSeeds[iCurrentTrackSeedIndex], foundTrackingFrameInfo, unsortedClusters, layerRadii, bz, reseedIfShorter);
o2::track::TrackPar linRef{temporaryTrack};
bool fitSuccess = fitTrack(temporaryTrack, // TrackITSExt& track,
0, // int lastLayer,
nLayers, // int firstLayer,
1, // int firstCluster,
maxChi2ClusterAttachment, // float maxChi2ClusterAttachment,
maxChi2NDF, // float maxChi2NDF,
o2::constants::math::VeryBig, // float maxQoverPt,
0, // nCl,
bz, // float bz,
foundTrackingFrameInfo, // TrackingFrameInfo** trackingFrameInfo,
propagator, // const o2::base::Propagator* propagator,
matCorrType, // o2::base::PropagatorF::MatCorrType matCorrType
&linRef,
shifRefToCluster);
if (!fitSuccess) {
continue;
}
temporaryTrack.getParamOut() = temporaryTrack.getParamIn();
linRef = temporaryTrack.getParamOut(); // use refitted track as lin.reference
temporaryTrack.resetCovariance();
temporaryTrack.setCov(temporaryTrack.getQ2Pt() * temporaryTrack.getQ2Pt() * temporaryTrack.getCov()[o2::track::CovLabels::kSigQ2Pt2], o2::track::CovLabels::kSigQ2Pt2);
temporaryTrack.setChi2(0);
fitSuccess = fitTrack(temporaryTrack, // TrackITSExt& track,
nLayers - 1, // int lastLayer,
-1, // int firstLayer,
-1, // int firstCluster,
maxChi2ClusterAttachment, // float maxChi2ClusterAttachment,
maxChi2NDF, // float maxChi2NDF,
50.f, // float maxQoverPt,
0, // nCl,
bz, // float bz,
foundTrackingFrameInfo, // TrackingFrameInfo** trackingFrameInfo,
propagator, // const o2::base::Propagator* propagator,
matCorrType, // o2::base::PropagatorF::MatCorrType matCorrType
&linRef,
shifRefToCluster);
if (!fitSuccess || temporaryTrack.getPt() < minPts[nLayers - temporaryTrack.getNClusters()]) {
continue;
}
if (repeatRefitOut) { // repeat outward refit seeding and linearizing with the stable inward fit result
o2::track::TrackParCov saveInw{temporaryTrack};
linRef = saveInw; // use refitted track as lin.reference
float saveChi2 = temporaryTrack.getChi2();
temporaryTrack.resetCovariance();
temporaryTrack.setCov(temporaryTrack.getQ2Pt() * temporaryTrack.getQ2Pt() * temporaryTrack.getCov()[o2::track::CovLabels::kSigQ2Pt2], o2::track::CovLabels::kSigQ2Pt2);
temporaryTrack.setChi2(0);
fitSuccess = fitTrack(temporaryTrack, // TrackITSExt& track,
0, // int lastLayer,
nLayers, // int firstLayer,
1, // int firstCluster,
maxChi2ClusterAttachment, // float maxChi2ClusterAttachment,
maxChi2NDF, // float maxChi2NDF,
o2::constants::math::VeryBig, // float maxQoverPt,
0, // nCl,
bz, // float bz,
foundTrackingFrameInfo, // TrackingFrameInfo** trackingFrameInfo,
propagator, // const o2::base::Propagator* propagator,
matCorrType, // o2::base::PropagatorF::MatCorrType matCorrType
&linRef,
shifRefToCluster);
if (!fitSuccess) {
continue;
}
temporaryTrack.getParamOut() = temporaryTrack.getParamIn();
temporaryTrack.getParamIn() = saveInw;
temporaryTrack.setChi2(saveChi2);
}
if constexpr (initRun) {
seedLUT[iCurrentTrackSeedIndex] = 1;
} else {
tracks[seedLUT[iCurrentTrackSeedIndex]] = temporaryTrack;
}
}
}
template <bool initRun, int nLayers = 7>
GPUg() void __launch_bounds__(256, 1) computeLayerCellNeighboursKernel(
CellSeed<nLayers>** cellSeedArray,
int* neighboursLUT,
int* neighboursIndexTable,
int** cellsLUTs,
gpuPair<int, int>* cellNeighbours,
const Tracklet** tracklets,
const int deltaROF,
const float maxChi2ClusterAttachment,
const float bz,
const int layerIndex,
const unsigned int nCells,
const int maxCellNeighbours = 1e2)
{
for (int iCurrentCellIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentCellIndex < nCells; iCurrentCellIndex += blockDim.x * gridDim.x) {
if constexpr (!initRun) {
if (neighboursIndexTable[iCurrentCellIndex] == neighboursIndexTable[iCurrentCellIndex + 1]) {
continue;
}
}
const auto& currentCellSeed{cellSeedArray[layerIndex][iCurrentCellIndex]};
const int nextLayerTrackletIndex{currentCellSeed.getSecondTrackletIndex()};
const int nextLayerFirstCellIndex{cellsLUTs[layerIndex + 1][nextLayerTrackletIndex]};
const int nextLayerLastCellIndex{cellsLUTs[layerIndex + 1][nextLayerTrackletIndex + 1]};
int foundNeighbours{0};
for (int iNextCell{nextLayerFirstCellIndex}; iNextCell < nextLayerLastCellIndex; ++iNextCell) {
auto nextCellSeed{cellSeedArray[layerIndex + 1][iNextCell]}; // Copy
if (nextCellSeed.getFirstTrackletIndex() != nextLayerTrackletIndex) { // Check if cells share the same tracklet
break;
}
if (deltaROF) {
const auto& trkl00 = tracklets[layerIndex][currentCellSeed.getFirstTrackletIndex()];
const auto& trkl01 = tracklets[layerIndex + 1][currentCellSeed.getSecondTrackletIndex()];
const auto& trkl10 = tracklets[layerIndex + 1][nextCellSeed.getFirstTrackletIndex()];
const auto& trkl11 = tracklets[layerIndex + 2][nextCellSeed.getSecondTrackletIndex()];
if ((o2::gpu::CAMath::Max(trkl00.getMaxRof(), o2::gpu::CAMath::Max(trkl01.getMaxRof(), o2::gpu::CAMath::Max(trkl10.getMaxRof(), trkl11.getMaxRof()))) -
o2::gpu::CAMath::Min(trkl00.getMinRof(), o2::gpu::CAMath::Min(trkl01.getMinRof(), o2::gpu::CAMath::Min(trkl10.getMinRof(), trkl11.getMinRof())))) > deltaROF) {
continue;
}
}
if (!nextCellSeed.rotate(currentCellSeed.getAlpha()) ||
!nextCellSeed.propagateTo(currentCellSeed.getX(), bz)) {
continue;
}
float chi2 = currentCellSeed.getPredictedChi2(nextCellSeed);
if (chi2 > maxChi2ClusterAttachment) /// TODO: switch to the chi2 wrt cluster to avoid correlation
{
continue;
}
if constexpr (initRun) {
atomicAdd(neighboursLUT + iNextCell, 1);
neighboursIndexTable[iCurrentCellIndex]++;
} else {
cellNeighbours[neighboursIndexTable[iCurrentCellIndex] + foundNeighbours] = {iCurrentCellIndex, iNextCell};
foundNeighbours++;
const int currentCellLevel{currentCellSeed.getLevel()};
if (currentCellLevel >= nextCellSeed.getLevel()) {
atomicMax(cellSeedArray[layerIndex + 1][iNextCell].getLevelPtr(), currentCellLevel + 1);
}
}
}
}
}
template <bool initRun, int nLayers>
GPUg() void __launch_bounds__(256, 1) computeLayerCellsKernel(
const Cluster** sortedClusters,
const Cluster** unsortedClusters,
const TrackingFrameInfo** tfInfo,
Tracklet** tracklets,
int** trackletsLUT,
const int nTrackletsCurrent,
const int layer,
CellSeed<nLayers>* cells,
int** cellsLUTs,
const int deltaROF,
const float bz,
const float maxChi2ClusterAttachment,
const float cellDeltaTanLambdaSigma,
const float nSigmaCut)
{
constexpr float layerxX0[7] = {5.e-3f, 5.e-3f, 5.e-3f, 1.e-2f, 1.e-2f, 1.e-2f, 1.e-2f}; // FIXME: Hardcoded here for the moment.
for (int iCurrentTrackletIndex = blockIdx.x * blockDim.x + threadIdx.x; iCurrentTrackletIndex < nTrackletsCurrent; iCurrentTrackletIndex += blockDim.x * gridDim.x) {
if constexpr (!initRun) {
if (cellsLUTs[layer][iCurrentTrackletIndex] == cellsLUTs[layer][iCurrentTrackletIndex + 1]) {
continue;
}
}
const Tracklet& currentTracklet = tracklets[layer][iCurrentTrackletIndex];
const int nextLayerClusterIndex{currentTracklet.secondClusterIndex};
const int nextLayerFirstTrackletIndex{trackletsLUT[layer + 1][nextLayerClusterIndex]};
const int nextLayerLastTrackletIndex{trackletsLUT[layer + 1][nextLayerClusterIndex + 1]};
if (nextLayerFirstTrackletIndex == nextLayerLastTrackletIndex) {
continue;
}
int foundCells{0};
for (int iNextTrackletIndex{nextLayerFirstTrackletIndex}; iNextTrackletIndex < nextLayerLastTrackletIndex; ++iNextTrackletIndex) {
if (tracklets[layer + 1][iNextTrackletIndex].firstClusterIndex != nextLayerClusterIndex) {
break;
}
const Tracklet& nextTracklet = tracklets[layer + 1][iNextTrackletIndex];
if (deltaROF && currentTracklet.getSpanRof(nextTracklet) > deltaROF) {
continue;
}
const float deltaTanLambda{o2::gpu::CAMath::Abs(currentTracklet.tanLambda - nextTracklet.tanLambda)};
if (deltaTanLambda / cellDeltaTanLambdaSigma < nSigmaCut) {
const int clusId[3]{
sortedClusters[layer][currentTracklet.firstClusterIndex].clusterId,
sortedClusters[layer + 1][nextTracklet.firstClusterIndex].clusterId,
sortedClusters[layer + 2][nextTracklet.secondClusterIndex].clusterId};
const auto& cluster1_glo = unsortedClusters[layer][clusId[0]];
const auto& cluster2_glo = unsortedClusters[layer + 1][clusId[1]];
const auto& cluster3_tf = tfInfo[layer + 2][clusId[2]];
auto track{buildTrackSeed(cluster1_glo, cluster2_glo, cluster3_tf, bz)};
float chi2{0.f};
bool good{false};
for (int iC{2}; iC--;) {
const TrackingFrameInfo& trackingHit = tfInfo[layer + iC][clusId[iC]];
if (!track.rotate(trackingHit.alphaTrackingFrame)) {
break;
}
if (!track.propagateTo(trackingHit.xTrackingFrame, bz)) {
break;
}
if (!track.correctForMaterial(layerxX0[layer + iC], layerxX0[layer + iC] * constants::Radl * constants::Rho, true)) {
break;
}
const auto predChi2{track.getPredictedChi2Quiet(trackingHit.positionTrackingFrame, trackingHit.covarianceTrackingFrame)};
if (!track.o2::track::TrackParCov::update(trackingHit.positionTrackingFrame, trackingHit.covarianceTrackingFrame)) {
break;
}
if (!iC && predChi2 > maxChi2ClusterAttachment) {
break;
}
good = !iC;
chi2 += predChi2;
}
if (!good) {
continue;
}
if constexpr (!initRun) {
new (cells + cellsLUTs[layer][iCurrentTrackletIndex] + foundCells) CellSeed<nLayers>{layer, clusId[0], clusId[1], clusId[2], iCurrentTrackletIndex, iNextTrackletIndex, track, chi2};
}
++foundCells;
}
}
if constexpr (initRun) {
cellsLUTs[layer][iCurrentTrackletIndex] = foundCells;
}
}
}
template <bool initRun, int nLayers>
GPUg() void __launch_bounds__(256, 1) computeLayerTrackletsMultiROFKernel(
const IndexTableUtils<nLayers>* utils,
const uint8_t* multMask,
const int layerIndex,
const int startROF,
const int endROF,
const int totalROFs,
const int deltaROF,
const Vertex* vertices,
const int* rofPV,
const int nVertices,
const int vertexId,
const Cluster** clusters, // Input data rof0
const int** ROFClusters, // Number of clusters on layers per ROF
const unsigned char** usedClusters, // Used clusters
const int** indexTables, // Input data rof0-delta <rof0< rof0+delta (up to 3 rofs)
Tracklet** tracklets, // Output data
int** trackletsLUT,
const int iteration,
const float NSigmaCut,
const float phiCut,
const float resolutionPV,
const float minR,
const float maxR,
const float positionResolution,
const float meanDeltaR = -42.f,
const float MSAngle = -42.f)
{
const int phiBins{utils->getNphiBins()};
const int zBins{utils->getNzBins()};
const int tableSize{phiBins * zBins + 1};
for (unsigned int iROF{blockIdx.x}; iROF < endROF - startROF; iROF += gridDim.x) {
const short pivotROF = iROF + startROF;
const short minROF = o2::gpu::CAMath::Max(startROF, static_cast<int>(pivotROF - deltaROF));
const short maxROF = o2::gpu::CAMath::Min(endROF - 1, static_cast<int>(pivotROF + deltaROF));
auto primaryVertices = getPrimaryVertices(minROF, maxROF, rofPV, totalROFs, vertices);
if (primaryVertices.empty()) {
continue;
}
const auto startVtx{vertexId >= 0 ? vertexId : 0};
const auto endVtx{vertexId >= 0 ? o2::gpu::CAMath::Min(vertexId + 1, static_cast<int>(primaryVertices.size())) : static_cast<int>(primaryVertices.size())};
if ((endVtx - startVtx) <= 0) {
continue;
}
auto clustersCurrentLayer = getClustersOnLayer(pivotROF, totalROFs, layerIndex, ROFClusters, clusters);
if (clustersCurrentLayer.empty()) {
continue;
}
for (int currentClusterIndex = threadIdx.x; currentClusterIndex < clustersCurrentLayer.size(); currentClusterIndex += blockDim.x) {
unsigned int storedTracklets{0};
const auto& currentCluster{clustersCurrentLayer[currentClusterIndex]};
const int currentSortedIndex{ROFClusters[layerIndex][pivotROF] + currentClusterIndex};
if (usedClusters[layerIndex][currentCluster.clusterId]) {
continue;
}
if constexpr (!initRun) {
if (trackletsLUT[layerIndex][currentSortedIndex] == trackletsLUT[layerIndex][currentSortedIndex + 1]) {
continue;
}
}
const float inverseR0{1.f / currentCluster.radius};
for (int iV{startVtx}; iV < endVtx; ++iV) {
auto& primaryVertex{primaryVertices[iV]};
if ((primaryVertex.isFlagSet(Vertex::Flags::UPCMode) && iteration != 3) || (iteration == 3 && !primaryVertex.isFlagSet(Vertex::Flags::UPCMode))) {
continue;
}
const float resolution = o2::gpu::CAMath::Sqrt(math_utils::Sq(resolutionPV) / primaryVertex.getNContributors() + math_utils::Sq(positionResolution));
const float tanLambda{(currentCluster.zCoordinate - primaryVertex.getZ()) * inverseR0};
const float zAtRmin{tanLambda * (minR - currentCluster.radius) + currentCluster.zCoordinate};
const float zAtRmax{tanLambda * (maxR - currentCluster.radius) + currentCluster.zCoordinate};
const float sqInverseDeltaZ0{1.f / (math_utils::Sq(currentCluster.zCoordinate - primaryVertex.getZ()) + constants::Tolerance)}; /// protecting from overflows adding the detector resolution
const float sigmaZ{o2::gpu::CAMath::Sqrt(math_utils::Sq(resolution) * math_utils::Sq(tanLambda) * ((math_utils::Sq(inverseR0) + sqInverseDeltaZ0) * math_utils::Sq(meanDeltaR) + 1.f) + math_utils::Sq(meanDeltaR * MSAngle))};
const int4 selectedBinsRect{getBinsRect<nLayers>(currentCluster, layerIndex + 1, utils, zAtRmin, zAtRmax, sigmaZ * NSigmaCut, phiCut)};
if (selectedBinsRect.x == 0 && selectedBinsRect.y == 0 && selectedBinsRect.z == 0 && selectedBinsRect.w == 0) {
continue;
}
int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1};
if (phiBinsNum < 0) {
phiBinsNum += phiBins;
}
for (short targetROF{minROF}; targetROF <= maxROF; ++targetROF) {
auto clustersNextLayer = getClustersOnLayer(targetROF, totalROFs, layerIndex + 1, ROFClusters, clusters);
if (clustersNextLayer.empty()) {
continue;
}
for (int iPhiCount{0}; iPhiCount < phiBinsNum; iPhiCount++) {
int iPhiBin = (selectedBinsRect.y + iPhiCount) % phiBins;
const int firstBinIndex{utils->getBinIndex(selectedBinsRect.x, iPhiBin)};
const int maxBinIndex{firstBinIndex + selectedBinsRect.z - selectedBinsRect.x + 1};
const int firstRowClusterIndex = indexTables[layerIndex + 1][(targetROF)*tableSize + firstBinIndex];
const int maxRowClusterIndex = indexTables[layerIndex + 1][(targetROF)*tableSize + maxBinIndex];
for (int nextClusterIndex{firstRowClusterIndex}; nextClusterIndex < maxRowClusterIndex; ++nextClusterIndex) {
if (nextClusterIndex >= clustersNextLayer.size()) {
break;
}
const Cluster& nextCluster{clustersNextLayer[nextClusterIndex]};
if (usedClusters[layerIndex + 1][nextCluster.clusterId]) {
continue;
}
const float deltaPhi{o2::gpu::CAMath::Abs(currentCluster.phi - nextCluster.phi)};
const float deltaZ{o2::gpu::CAMath::Abs(tanLambda * (nextCluster.radius - currentCluster.radius) + currentCluster.zCoordinate - nextCluster.zCoordinate)};
if (deltaZ / sigmaZ < NSigmaCut && (deltaPhi < phiCut || o2::gpu::CAMath::Abs(deltaPhi - o2::constants::math::TwoPI) < phiCut)) {
if constexpr (initRun) {
trackletsLUT[layerIndex][currentSortedIndex]++; // we need l0 as well for usual exclusive sums.
} else {
const float phi{o2::gpu::CAMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)};
const float tanL{(currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius)};
const int nextSortedIndex{ROFClusters[layerIndex + 1][targetROF] + nextClusterIndex};
new (tracklets[layerIndex] + trackletsLUT[layerIndex][currentSortedIndex] + storedTracklets) Tracklet{currentSortedIndex, nextSortedIndex, tanL, phi, pivotROF, targetROF};
}
++storedTracklets;
}
}
}
}
}
}
}
}
GPUg() void __launch_bounds__(256, 1) compileTrackletsLookupTableKernel(
const Tracklet* tracklets,
int* trackletsLookUpTable,
const int nTracklets)
{
for (int currentTrackletIndex = blockIdx.x * blockDim.x + threadIdx.x; currentTrackletIndex < nTracklets; currentTrackletIndex += blockDim.x * gridDim.x) {
atomicAdd(&trackletsLookUpTable[tracklets[currentTrackletIndex].firstClusterIndex], 1);
}
}
template <bool dryRun, int nLayers = 7>
GPUg() void __launch_bounds__(256, 1) processNeighboursKernel(
const int layer,
const int level,
CellSeed<nLayers>** allCellSeeds,
CellSeed<nLayers>* currentCellSeeds,
const int* currentCellIds,
const unsigned int nCurrentCells,
CellSeed<nLayers>* updatedCellSeeds,
int* updatedCellsIds,
int* foundSeedsTable, // auxiliary only in GPU code to compute the number of cells per iteration
const unsigned char** usedClusters, // Used clusters
int* neighbours,
int* neighboursLUT,
const TrackingFrameInfo** foundTrackingFrameInfo,
const float bz,
const float maxChi2ClusterAttachment,
const o2::base::Propagator* propagator,
const o2::base::PropagatorF::MatCorrType matCorrType)
{
constexpr float layerxX0[7] = {5.e-3f, 5.e-3f, 5.e-3f, 1.e-2f, 1.e-2f, 1.e-2f, 1.e-2f}; // FIXME: Hardcoded here for the moment.
for (unsigned int iCurrentCell = blockIdx.x * blockDim.x + threadIdx.x; iCurrentCell < nCurrentCells; iCurrentCell += blockDim.x * gridDim.x) {
if constexpr (!dryRun) {
if (foundSeedsTable[iCurrentCell] == foundSeedsTable[iCurrentCell + 1]) {
continue;
}
}
int foundSeeds{0};
const auto& currentCell{currentCellSeeds[iCurrentCell]};
if (currentCell.getLevel() != level) {
continue;
}
if (currentCellIds == nullptr && (usedClusters[layer][currentCell.getFirstClusterIndex()] ||
usedClusters[layer + 1][currentCell.getSecondClusterIndex()] ||
usedClusters[layer + 2][currentCell.getThirdClusterIndex()])) {
continue;
}
const int cellId = currentCellIds == nullptr ? iCurrentCell : currentCellIds[iCurrentCell];
const int startNeighbourId{cellId ? neighboursLUT[cellId - 1] : 0};
const int endNeighbourId{neighboursLUT[cellId]};
for (int iNeighbourCell{startNeighbourId}; iNeighbourCell < endNeighbourId; ++iNeighbourCell) {
const int neighbourCellId = neighbours[iNeighbourCell];
const auto& neighbourCell = allCellSeeds[layer - 1][neighbourCellId];
if (neighbourCell.getSecondTrackletIndex() != currentCell.getFirstTrackletIndex()) {
continue;
}
if (usedClusters[layer - 1][neighbourCell.getFirstClusterIndex()]) {
continue;
}
if (currentCell.getLevel() - 1 != neighbourCell.getLevel()) {
continue;
}
auto seed{currentCell};
auto& trHit = foundTrackingFrameInfo[layer - 1][neighbourCell.getFirstClusterIndex()];
if (!seed.rotate(trHit.alphaTrackingFrame)) {
continue;
}
if (!propagator->propagateToX(seed, trHit.xTrackingFrame, bz, o2::base::PropagatorImpl<float>::MAX_SIN_PHI, o2::base::PropagatorImpl<float>::MAX_STEP, matCorrType)) {
continue;
}
if (matCorrType == o2::base::PropagatorF::MatCorrType::USEMatCorrNONE) {
if (!seed.correctForMaterial(layerxX0[layer - 1], layerxX0[layer - 1] * constants::Radl * constants::Rho, true)) {
continue;
}
}
auto predChi2{seed.getPredictedChi2Quiet(trHit.positionTrackingFrame, trHit.covarianceTrackingFrame)};
if ((predChi2 > maxChi2ClusterAttachment) || predChi2 < 0.f) {
continue;
}
seed.setChi2(seed.getChi2() + predChi2);
if (!seed.o2::track::TrackParCov::update(trHit.positionTrackingFrame, trHit.covarianceTrackingFrame)) {
continue;
}
if constexpr (dryRun) {
foundSeedsTable[iCurrentCell]++;
} else {
seed.getClusters()[layer - 1] = neighbourCell.getFirstClusterIndex();
seed.setLevel(neighbourCell.getLevel());
seed.setFirstTrackletIndex(neighbourCell.getFirstTrackletIndex());
seed.setSecondTrackletIndex(neighbourCell.getSecondTrackletIndex());
updatedCellsIds[foundSeedsTable[iCurrentCell] + foundSeeds] = neighbourCellId;
updatedCellSeeds[foundSeedsTable[iCurrentCell] + foundSeeds] = seed;
}
foundSeeds++;
}
}
}
} // namespace gpu
template <int nLayers>
void countTrackletsInROFsHandler(const IndexTableUtils<nLayers>* utils,
const uint8_t* multMask,
const int layer,
const int startROF,
const int endROF,
const int maxROF,
const int deltaROF,
const int vertexId,
const Vertex* vertices,
const int* rofPV,
const int nVertices,
const Cluster** clusters,
std::vector<unsigned int> nClusters,
const int** ROFClusters,
const unsigned char** usedClusters,
const int** clustersIndexTables,
int** trackletsLUTs,
gsl::span<int*> trackletsLUTsHost,
const int iteration,
const float NSigmaCut,
bounded_vector<float>& phiCuts,
const float resolutionPV,
std::array<float, nLayers>& minRs,
std::array<float, nLayers>& maxRs,
bounded_vector<float>& resolutions,
std::vector<float>& radii,
bounded_vector<float>& mulScatAng,
o2::its::ExternalAllocator* alloc,
const int nBlocks,
const int nThreads,
gpu::Streams& streams)
{
gpu::computeLayerTrackletsMultiROFKernel<true><<<nBlocks, nThreads, 0, streams[layer].get()>>>(
utils,
multMask,
layer,
startROF,
endROF,
maxROF,
deltaROF,
vertices,
rofPV,
nVertices,
vertexId,
clusters,
ROFClusters,
usedClusters,
clustersIndexTables,
nullptr,
trackletsLUTs,
iteration,
NSigmaCut,
phiCuts[layer],
resolutionPV,
minRs[layer + 1],
maxRs[layer + 1],
resolutions[layer],
radii[layer + 1] - radii[layer],
mulScatAng[layer]);
auto nosync_policy = THRUST_NAMESPACE::par_nosync(gpu::TypedAllocator<char>(alloc)).on(streams[layer].get());
thrust::exclusive_scan(nosync_policy, trackletsLUTsHost[layer], trackletsLUTsHost[layer] + nClusters[layer] + 1, trackletsLUTsHost[layer]);
}
template <int nLayers>
void computeTrackletsInROFsHandler(const IndexTableUtils<nLayers>* utils,
const uint8_t* multMask,
const int layer,
const int startROF,
const int endROF,
const int maxROF,
const int deltaROF,
const int vertexId,
const Vertex* vertices,
const int* rofPV,
const int nVertices,
const Cluster** clusters,
std::vector<unsigned int> nClusters,
const int** ROFClusters,
const unsigned char** usedClusters,
const int** clustersIndexTables,
Tracklet** tracklets,
gsl::span<Tracklet*> spanTracklets,
gsl::span<int> nTracklets,
int** trackletsLUTs,
gsl::span<int*> trackletsLUTsHost,
const int iteration,
const float NSigmaCut,
bounded_vector<float>& phiCuts,
const float resolutionPV,
std::array<float, nLayers>& minRs,
std::array<float, nLayers>& maxRs,
bounded_vector<float>& resolutions,
std::vector<float>& radii,
bounded_vector<float>& mulScatAng,
o2::its::ExternalAllocator* alloc,
const int nBlocks,
const int nThreads,
gpu::Streams& streams)
{
gpu::computeLayerTrackletsMultiROFKernel<false><<<nBlocks, nThreads, 0, streams[layer].get()>>>(
utils,
multMask,
layer,
startROF,
endROF,
maxROF,
deltaROF,
vertices,
rofPV,
nVertices,
vertexId,
clusters,
ROFClusters,
usedClusters,
clustersIndexTables,
tracklets,
trackletsLUTs,
iteration,
NSigmaCut,
phiCuts[layer],
resolutionPV,
minRs[layer + 1],
maxRs[layer + 1],
resolutions[layer],
radii[layer + 1] - radii[layer],
mulScatAng[layer]);
thrust::device_ptr<Tracklet> tracklets_ptr(spanTracklets[layer]);
auto nosync_policy = THRUST_NAMESPACE::par_nosync(gpu::TypedAllocator<char>(alloc)).on(streams[layer].get());
thrust::sort(nosync_policy, tracklets_ptr, tracklets_ptr + nTracklets[layer], gpu::sort_tracklets());
auto unique_end = thrust::unique(nosync_policy, tracklets_ptr, tracklets_ptr + nTracklets[layer], gpu::equal_tracklets());
nTracklets[layer] = unique_end - tracklets_ptr;
if (layer) {
GPUChkErrS(cudaMemsetAsync(trackletsLUTsHost[layer], 0, (nClusters[layer] + 1) * sizeof(int), streams[layer].get()));
gpu::compileTrackletsLookupTableKernel<<<nBlocks, nThreads, 0, streams[layer].get()>>>(
spanTracklets[layer],
trackletsLUTsHost[layer],
nTracklets[layer]);
thrust::exclusive_scan(nosync_policy, trackletsLUTsHost[layer], trackletsLUTsHost[layer] + nClusters[layer] + 1, trackletsLUTsHost[layer]);
}
}
template <int nLayers>
void countCellsHandler(
const Cluster** sortedClusters,
const Cluster** unsortedClusters,
const TrackingFrameInfo** tfInfo,
Tracklet** tracklets,
int** trackletsLUT,
const int nTracklets,
const int layer,
CellSeed<nLayers>* cells,
int** cellsLUTsArrayDevice,
int* cellsLUTsHost,
const int deltaROF,
const float bz,
const float maxChi2ClusterAttachment,
const float cellDeltaTanLambdaSigma,
const float nSigmaCut,
o2::its::ExternalAllocator* alloc,
const int nBlocks,
const int nThreads,
gpu::Streams& streams)
{
gpu::computeLayerCellsKernel<true><<<nBlocks, nThreads, 0, streams[layer].get()>>>(
sortedClusters, // const Cluster**
unsortedClusters, // const Cluster**
tfInfo, // const TrackingFrameInfo**
tracklets, // const Tracklets**
trackletsLUT, // const int**
nTracklets, // const int
layer, // const int
cells, // CellSeed*
cellsLUTsArrayDevice, // int**
deltaROF, // const int
bz, // const float
maxChi2ClusterAttachment, // const float
cellDeltaTanLambdaSigma, // const float
nSigmaCut); // const float
auto nosync_policy = THRUST_NAMESPACE::par_nosync(gpu::TypedAllocator<char>(alloc)).on(streams[layer].get());
thrust::exclusive_scan(nosync_policy, cellsLUTsHost, cellsLUTsHost + nTracklets + 1, cellsLUTsHost);
}
template <int nLayers>
void computeCellsHandler(
const Cluster** sortedClusters,
const Cluster** unsortedClusters,
const TrackingFrameInfo** tfInfo,
Tracklet** tracklets,
int** trackletsLUT,
const int nTracklets,
const int layer,
CellSeed<nLayers>* cells,
int** cellsLUTsArrayDevice,
int* cellsLUTsHost,
const int deltaROF,
const float bz,
const float maxChi2ClusterAttachment,
const float cellDeltaTanLambdaSigma,
const float nSigmaCut,
const int nBlocks,
const int nThreads,
gpu::Streams& streams)
{
gpu::computeLayerCellsKernel<false><<<nBlocks, nThreads, 0, streams[layer].get()>>>(
sortedClusters, // const Cluster**
unsortedClusters, // const Cluster**
tfInfo, // const TrackingFrameInfo**
tracklets, // const Tracklets**
trackletsLUT, // const int**
nTracklets, // const int
layer, // const int
cells, // CellSeed*
cellsLUTsArrayDevice, // int**
deltaROF, // const int
bz, // const float
maxChi2ClusterAttachment, // const float
cellDeltaTanLambdaSigma, // const float