Project
Loading...
Searching...
No Matches
GPUTPCGMMerger.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#define GPUCA_CADEBUG 0
16#define GPUCA_MERGE_LOOPER_MC 0
17// #define GPUCA_CADEBUG_ENABLED
18
19#include "GPUCommonDef.h"
20
21#if !defined(GPUCA_GPUCODE) && (defined(GPUCA_MERGER_BY_MC_LABEL) || defined(GPUCA_CADEBUG_ENABLED) || GPUCA_MERGE_LOOPER_MC)
23#include "GPUROOTDump.h"
24#endif
25
26#ifndef GPUCA_GPUCODE_DEVICE
27#include <cstdio>
28#include <cstring>
29#include <cmath>
30#include "GPUReconstruction.h"
31#endif
32
33#include "GPUTPCTracker.h"
34#include "GPUTPCTrackParam.h"
35#include "GPUTPCGMMerger.h"
36#include "GPUO2DataTypes.h"
37#include "TPCFastTransformPOD.h"
38#include "GPUTPCConvertImpl.h"
39#include "GPUTPCGeometry.h"
40#include "GPUDefParametersRuntime.h"
41
42#include "GPUCommonMath.h"
43#include "GPUCommonAlgorithm.h"
44#include "GPUCommonConstants.h"
45
46#include "GPUTPCTrackParam.h"
47#include "GPUTPCGMMergedTrack.h"
48#include "GPUParam.h"
50
51#include "GPUTPCGMTrackParam.h"
52#include "GPUTPCGMSectorTrack.h"
53#include "GPUTPCGMBorderTrack.h"
54
57#ifndef GPUCA_GPUCODE
60#endif
61
62using namespace o2::gpu;
63using namespace o2::tpc;
64using namespace gputpcgmmergertypes;
65
66namespace o2::gpu::internal
67{
69 float refz;
70 float x;
71 float y;
72 uint32_t id;
73};
74
76 GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b)
77 {
78 return GPUCA_DETERMINISTIC_CODE((a.fMax != b.fMax) ? (a.fMax < b.fMax) : (a.fId < b.fId), a.fMax < b.fMax);
79 }
80};
82 GPUd() bool operator()(const GPUTPCGMBorderRange& a, const GPUTPCGMBorderRange& b)
83 {
84 return GPUCA_DETERMINISTIC_CODE((a.fMin != b.fMin) ? (a.fMin < b.fMin) : (a.fId < b.fId), a.fMin < b.fMin);
85 }
86};
87
91 GPUd() bool operator()(const int32_t aa, const int32_t bb)
92 {
95 if (a.OK() != b.OK()) {
96 return a.OK();
97 }
98 if (a.CCE() != b.CCE()) {
99 return a.CCE() > b.CCE();
100 }
101 GPUCA_DETERMINISTIC_CODE( // clang-format off
102 if (a.NClusters() != b.NClusters()) {
103 return a.NClusters() > b.NClusters();
104 } if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) {
105 return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
106 } if (a.GetParam().GetY() != b.GetParam().GetY()) {
107 return a.GetParam().GetY() > b.GetParam().GetY();
108 }
109 return aa > bb;
110 , // !GPUCA_DETERMINISTIC_CODE
111 return a.NClusters() > b.NClusters();
112 ) // clang-format on
113 }
114};
115
119 GPUd() bool operator()(const int32_t aa, const int32_t bb)
120 {
121 const GPUTPCGMMergedTrack& GPUrestrict() a = mCmp[aa];
123 GPUCA_DETERMINISTIC_CODE( // clang-format off
124 if (CAMath::Abs(a.GetParam().GetQPt()) != CAMath::Abs(b.GetParam().GetQPt())) {
125 return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
126 } if (a.GetParam().GetY() != b.GetParam().GetY()) {
127 return a.GetParam().GetY() > b.GetParam().GetY();
128 }
129 return a.GetParam().GetZ() > b.GetParam().GetZ();
130 , // !GPUCA_DETERMINISTIC_CODE
131 return CAMath::Abs(a.GetParam().GetQPt()) > CAMath::Abs(b.GetParam().GetQPt());
132 ) // clang-format on
133 }
134};
135
137 GPUd() bool operator()(const MergeLooperParam& a, const MergeLooperParam& b)
138 {
139 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));
140 }
141};
142
143} // namespace o2::gpu::internal
144
145using namespace o2::gpu::internal;
146
147#ifndef GPUCA_GPUCODE
148
149#include "GPUQA.h"
150#include "GPUMemorySizeScalers.h"
151
153{
154 for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
155 mNextSectorInd[iSector] = iSector + 1;
156 mPrevSectorInd[iSector] = iSector - 1;
157 }
158 int32_t mid = NSECTORS / 2 - 1;
159 int32_t last = NSECTORS - 1;
160 mNextSectorInd[mid] = 0;
161 mPrevSectorInd[0] = mid;
162 mNextSectorInd[last] = NSECTORS / 2;
163 mPrevSectorInd[NSECTORS / 2] = last;
164}
165
166// DEBUG CODE
167#if !defined(GPUCA_GPUCODE) && (defined(GPUCA_MERGER_BY_MC_LABEL) || defined(GPUCA_CADEBUG_ENABLED) || GPUCA_MERGE_LOOPER_MC)
168#include "GPUQAHelper.h"
169
170template <class T>
171inline const auto* resolveMCLabels(const o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>* a, const AliHLTTPCClusterMCLabel* b)
172{
173 return a;
174}
175template <>
176inline const auto* resolveMCLabels<AliHLTTPCClusterMCLabel>(const o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>* a, const AliHLTTPCClusterMCLabel* b)
177{
178 return b;
179}
180
181template <class T, class S>
182int64_t GPUTPCGMMerger::GetTrackLabelA(const S& trk) const
183{
184 GPUTPCGMSectorTrack* sectorTrack = nullptr;
185 int32_t nClusters = 0;
186 if constexpr (std::is_same_v<S, GPUTPCGMBorderTrack&>) {
187 sectorTrack = &mSectorTrackInfos[trk.TrackID()];
188 nClusters = sectorTrack->OrigTrack()->NHits();
189 } else {
190 nClusters = trk.NClusters();
191 }
192 auto acc = GPUTPCTrkLbl<false, GPUTPCTrkLbl_ret>(resolveMCLabels<T>(GetConstantMem()->ioPtrs.clustersNative ? GetConstantMem()->ioPtrs.clustersNative->clustersMCTruth : nullptr, GetConstantMem()->ioPtrs.mcLabelsTPC), 0.5f);
193 for (int32_t i = 0; i < nClusters; i++) {
194 int32_t id;
195 if constexpr (std::is_same_v<S, GPUTPCGMBorderTrack&>) {
196 const GPUTPCTracker& tracker = GetConstantMem()->tpcTrackers[sectorTrack->Sector()];
197 const GPUTPCHitId& ic = tracker.TrackHits()[sectorTrack->OrigTrack()->FirstHitID() + i];
198 id = tracker.Data().ClusterDataIndex(tracker.Data().Row(ic.RowIndex()), ic.HitIndex()) + GetConstantMem()->ioPtrs.clustersNative->clusterOffset[sectorTrack->Sector()][0];
199 } else {
200 id = mClusters[trk.FirstClusterRef() + i].num;
201 }
202 acc.addLabel(id);
203 }
204 return acc.computeLabel().id;
205}
206
207template <class S>
208int64_t GPUTPCGMMerger::GetTrackLabel(const S& trk) const
209{
210#ifndef GPUCA_RUN2
211 if (GetConstantMem()->ioPtrs.clustersNative->clustersMCTruth) {
212 return GetTrackLabelA<o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>, S>(trk);
213 } else
214#endif
215 {
216 return GetTrackLabelA<AliHLTTPCClusterMCLabel, S>(trk);
217 }
218}
219
220#endif
221// END DEBUG CODE
222
224{
225 uint32_t nErr = 0;
226 for (uint32_t i = 0; i < mMemory->nMergedTracks; i++) {
227 const GPUTPCGMMergedTrack& trk = mMergedTracks[i];
228 if (trk.OK()) {
229 if (trk.NClusters() == 0) {
230 GPUError("FAILURE: Track marked ok but has 0 clusters");
231 nErr++;
232 }
233 if (!trk.CCE() && !trk.MergedLooper()) {
234 const GPUTPCGMMergedTrack* updTrk = &trk;
235 while (updTrk->PrevSegment() >= 0) {
236 auto next = &mMergedTracks[updTrk->PrevSegment()];
237 if (!next->MergedLooper()) {
238 GPUError("FAILURE: prev segment not marked as merged looper\n");
239 nErr++;
240 }
241 if (next == &trk) {
242 GPUError("FAILURE: segment cycle found\n");
243 break;
244 }
245 updTrk = next;
246 }
247 if (updTrk->NClusters() == 0) {
248 printf("FAILURE: segment leg has 0 clusters");
249 }
250 }
251 }
252 }
253
254 if (nErr == 0) {
255 GPUInfo("Merged Tracks OK");
256 } else {
257 throw std::runtime_error("Error during track merging");
258 }
259}
260
262{
263 uint32_t nErr = 0;
264 std::vector<bool> trkUsed(SectorTrackInfoLocalTotal());
265 for (int32_t i = 0; i < SectorTrackInfoLocalTotal(); i++) {
266 trkUsed[i] = false;
267 }
268
269 for (int32_t itr = 0; itr < SectorTrackInfoLocalTotal(); itr++) {
270 GPUTPCGMSectorTrack& track = mSectorTrackInfos[itr];
271 if (track.PrevSegmentNeighbour() >= 0 && mSectorTrackInfos[track.PrevSegmentNeighbour()].NextSegmentNeighbour() != itr) {
272 GPUError("FAILURE: Invalid reciprocal segment link: %d PrevSegmentNeighbour %d NextSegmentNeighbour %d", itr, track.PrevSegmentNeighbour(), mSectorTrackInfos[track.PrevSegmentNeighbour()].NextSegmentNeighbour());
273 nErr++;
274 }
275 if (track.NextSegmentNeighbour() >= 0 && mSectorTrackInfos[track.NextSegmentNeighbour()].PrevSegmentNeighbour() != itr) {
276 GPUError("FAILURE: Invalid reciprocal segment link: %d NextSegmentNeighbour %d PrevSegmentNeighbour %d", itr, track.NextSegmentNeighbour(), mSectorTrackInfos[track.NextSegmentNeighbour()].PrevSegmentNeighbour());
277 nErr++;
278 }
279 if (track.PrevNeighbour() >= 0 && mSectorTrackInfos[track.PrevNeighbour()].NextNeighbour() != itr) {
280 GPUError("FAILURE: Invalid reciprocal link: %d PrevNeighbour %d NextNeighbour %d", itr, track.PrevNeighbour(), mSectorTrackInfos[track.PrevNeighbour()].NextNeighbour());
281 nErr++;
282 }
283 if (track.NextNeighbour() >= 0 && mSectorTrackInfos[track.NextNeighbour()].PrevNeighbour() != itr) {
284 GPUError("FAILURE: Invalid reciprocal link: %d NextNeighbour %d PrevNeighbour %d", itr, track.NextNeighbour(), mSectorTrackInfos[track.NextNeighbour()].PrevNeighbour());
285 nErr++;
286 }
287 if (track.PrevSegmentNeighbour() >= 0) {
288 continue;
289 }
290 if (track.PrevNeighbour() >= 0) {
291 continue;
292 }
293 GPUTPCGMSectorTrack *trbase = &track, *tr = &track;
294 while (true) {
295 int32_t iTrk = tr - mSectorTrackInfos;
296 if (trkUsed[iTrk]) {
297 GPUError("FAILURE: double use");
298 nErr++;
299 break;
300 }
301 trkUsed[iTrk] = true;
302
303 int32_t jtr = tr->NextSegmentNeighbour();
304 if (jtr >= 0) {
305 tr = &(mSectorTrackInfos[jtr]);
306 if (tr->PrevNeighbour() >= 0) {
307 GPUError("FAILURE: Non-base segment has previous leg");
308 nErr++;
309 }
310 continue;
311 }
312 jtr = trbase->NextNeighbour();
313 if (jtr >= 0) {
314 trbase = &(mSectorTrackInfos[jtr]);
315 tr = trbase;
316 if (tr->PrevSegmentNeighbour() >= 0) {
317 GPUError("FAILURE: Neibhbour leg has previous segment neightbout");
318 nErr++;
319 break;
320 }
321 continue;
322 }
323 break;
324 }
325 }
326 for (int32_t i = 0; i < SectorTrackInfoLocalTotal(); i++) {
327 if (trkUsed[i] == false) {
328 GPUError("FAILURE: trk missed");
329 nErr++;
330 }
331 }
332 if (nErr == 0) {
333 GPUInfo("Merged Track Graph OK");
334 } else {
335 throw std::runtime_error("Invalid merge graph");
336 }
337}
338
339void GPUTPCGMMerger::PrintMergeGraph(const GPUTPCGMSectorTrack* trk, std::ostream& out) const
340{
341 const GPUTPCGMSectorTrack* orgTrack = trk;
342 while (trk->PrevSegmentNeighbour() >= 0) {
343 trk = &mSectorTrackInfos[trk->PrevSegmentNeighbour()];
344 }
345 const GPUTPCGMSectorTrack* orgTower = trk;
346 while (trk->PrevNeighbour() >= 0) {
347 trk = &mSectorTrackInfos[trk->PrevNeighbour()];
348 }
349
350 int32_t nextId = trk - mSectorTrackInfos;
351 out << "Graph of track " << (orgTrack - mSectorTrackInfos) << "\n";
352 while (nextId >= 0) {
353 trk = &mSectorTrackInfos[nextId];
354 if (trk->PrevSegmentNeighbour() >= 0) {
355 out << "TRACK TREE INVALID!!! " << trk->PrevSegmentNeighbour() << " --> " << nextId << "\n";
356 }
357 out << (trk == orgTower ? "--" : " ");
358 while (nextId >= 0) {
359 GPUTPCGMSectorTrack* trk2 = &mSectorTrackInfos[nextId];
360 if (trk != trk2 && (trk2->PrevNeighbour() >= 0 || trk2->NextNeighbour() >= 0)) {
361 out << " (TRACK TREE INVALID!!! " << trk2->PrevNeighbour() << " <-- " << nextId << " --> " << trk2->NextNeighbour() << ") ";
362 }
363 char tmp[128];
364 snprintf(tmp, 128, " %s%5d(%5.2f)", trk2 == orgTrack ? "!" : " ", nextId, trk2->QPt());
365 out << tmp;
366 nextId = trk2->NextSegmentNeighbour();
367 }
368 out << "\n";
369 nextId = trk->NextNeighbour();
370 }
371}
372
374
376{
377 computePointerWithAlignment(mem, mSectorTrackInfos, mNTotalSectorTracks);
378 computePointerWithAlignment(mem, mSectorTrackInfoIndex, NSECTORS * 2 + 1);
379 if (mRec->GetProcessingSettings().deterministicGPUReconstruction) {
380 computePointerWithAlignment(mem, mTmpSortMemory, std::max(mNTotalSectorTracks, mNMaxTracks * 2));
381 }
382
383 void* memBase = mem;
384 computePointerWithAlignment(mem, mBorderMemory, 2 * mNTotalSectorTracks); // MergeBorders & Resolve
385 computePointerWithAlignment(mem, mBorderRangeMemory, 2 * mNTotalSectorTracks);
386 int32_t nTracks = 0;
387 for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
388 const int32_t n = *mRec->GetConstantMem().tpcTrackers[iSector].NTracks();
389 mBorder[iSector] = mBorderMemory + 2 * nTracks;
390 mBorder[NSECTORS + iSector] = mBorderMemory + 2 * nTracks + n;
391 mBorderRange[iSector] = mBorderRangeMemory + 2 * nTracks;
392 nTracks += n;
393 }
394 computePointerWithAlignment(mem, mTrackLinks, mNTotalSectorTracks);
395 computePointerWithAlignment(mem, mTrackCCRoots, mNTotalSectorTracks);
396 void* memMax = mem;
397 mem = memBase;
398 computePointerWithAlignment(mem, mTrackIDs, GPUTPCGeometry::NSECTORS * mNMaxSingleSectorTracks); // UnpackResetIds - RefitSectorTracks - UnpackSectorGlobal
399 memMax = (void*)std::max((size_t)mem, (size_t)memMax);
400 mem = memBase;
401 computePointerWithAlignment(mem, mTrackSort, mNMaxTracks); // PrepareForFit0 - SortTracksQPt - PrepareForFit1 - PrepareForFit1 / Finalize0 - Finalize2
402 computePointerWithAlignment(mem, mSharedCount, mNMaxClusters);
403 memMax = (void*)std::max((size_t)mem, (size_t)memMax);
404 mem = memBase;
405 computePointerWithAlignment(mem, mLoopData, mNMaxTracks); // GPUTPCGMMergerTrackFit - GPUTPCGMMergerFollowLoopers
406 computePointerWithAlignment(mem, mRetryRefitIds, mNMaxTracks); // Reducing mNMaxTracks for mLoopData / mRetryRefitIds does not save memory, since the other parts are larger anyway
407 memMax = (void*)std::max((size_t)mem, (size_t)memMax);
408 mem = memBase;
409 computePointerWithAlignment(mem, mLooperCandidates, mNMaxLooperMatches); // MergeLoopers 1-3
410 memMax = (void*)std::max((size_t)mem, (size_t)memMax);
411 return memMax;
412}
413
415{
416 computePointerWithAlignment(mem, mMemory);
417 return mem;
418}
419
421{
422 computePointerWithAlignment(mem, mTrackOrderAttach, mNMaxTracks);
423 const bool mergerSortTracks = mRec->GetProcessingSettings().mergerSortTracks == -1 ? mRec->getGPUParameters(mRec->GetRecoStepsGPU() & gpudatatypes::RecoStep::TPCMerging).par_SORT_BEFORE_FIT : mRec->GetProcessingSettings().mergerSortTracks;
424 if (mergerSortTracks) {
425 computePointerWithAlignment(mem, mTrackOrderProcess, mNMaxTracks);
426 }
427 return mem;
428}
429
431{
432 computePointerWithAlignment(mem, mMergedTracks, mNMaxTracks);
433 if (mRec->GetParam().dodEdxEnabled) {
434 computePointerWithAlignment(mem, mMergedTracksdEdx, mNMaxTracks);
435 if (mRec->GetParam().rec.tpc.dEdxClusterRejectionFlagMask != mRec->GetParam().rec.tpc.dEdxClusterRejectionFlagMaskAlt) {
436 computePointerWithAlignment(mem, mMergedTracksdEdxAlt, mNMaxTracks);
437 }
438 }
439 computePointerWithAlignment(mem, mClusters, mNMaxMergedTrackClusters);
440 computePointerWithAlignment(mem, mClusterAttachment, mNMaxClusters);
441 return mem;
442}
443
445{
446 if ((mRec->GetRecoSteps() & gpudatatypes::RecoStep::Refit) || mRec->GetProcessingSettings().outputSharedClusterMap) {
447 computePointerWithAlignment(mem, mClusterStateExt, mNMaxClusters);
448 } else {
449 mClusterStateExt = nullptr;
450 }
451 return mem;
452}
453
455{
456 computePointerWithAlignment(mem, mOutputTracksTPCO2, HostProcessor(this).NOutputTracksTPCO2());
457 return mem;
458}
459
461{
462 computePointerWithAlignment(mem, mOutputClusRefsTPCO2, HostProcessor(this).NOutputClusRefsTPCO2());
463 return mem;
464}
465
467{
468 computePointerWithAlignment(mem, mOutputTracksTPCO2MC, HostProcessor(this).NOutputTracksTPCO2());
469 return mem;
470}
471
473{
474 computePointerWithAlignment(mem, mTrackSortO2, mNMaxTracks);
475 computePointerWithAlignment(mem, mClusRefTmp, mNMaxTracks);
476 return mem;
477}
478
480{
486 if (mRec->GetProcessingSettings().createO2Output) {
490 if (mRec->GetProcessingSettings().runMC) {
492 }
493 }
495}
496
498{
499 mNTotalSectorTracks = 0;
500 mNClusters = 0;
501 mNMaxSingleSectorTracks = 0;
502 for (int32_t iSector = 0; iSector < NSECTORS; iSector++) {
503 uint32_t ntrk = *mRec->GetConstantMem().tpcTrackers[iSector].NTracks();
504 mNTotalSectorTracks += ntrk;
505 mNClusters += *mRec->GetConstantMem().tpcTrackers[iSector].NTrackHits();
506 if (mNMaxSingleSectorTracks < ntrk) {
507 mNMaxSingleSectorTracks = ntrk;
508 }
509 }
510 mNMaxMergedTrackClusters = mRec->MemoryScalers()->NTPCMergedTrackHits(mNClusters);
511 if (CAMath::Abs(Param().polynomialField.GetNominalBz()) < (gpu_common_constants::kZeroFieldCut * gpu_common_constants::kCLight)) {
512 mNMaxTracks = mRec->MemoryScalers()->getValue(mNTotalSectorTracks, mNTotalSectorTracks); // 0 magnetic field
513 } else {
514 mNMaxTracks = mRec->MemoryScalers()->NTPCMergedTracks(mNTotalSectorTracks);
515 }
516 if (io.clustersNative) {
517 mNMaxClusters = io.clustersNative->nClustersTotal;
519 mNMaxClusters = 0;
520 for (int32_t i = 0; i < NSECTORS; i++) {
521 mNMaxClusters += mRec->GetConstantMem().tpcTrackers[i].NHitsTotal();
522 }
523 } else {
524 mNMaxClusters = mNClusters;
525 }
526 mNMaxLooperMatches = mNMaxClusters / 4; // We have that much scratch memory anyway
527}
528
530{
531 for (int32_t i = 0; i < NSECTORS; i++) {
532 if (mRec->GetConstantMem().tpcTrackers[i].CommonMemory()->nLocalTracks > (int32_t)mNMaxSingleSectorTracks) {
533 throw std::runtime_error("mNMaxSingleSectorTracks too small");
534 }
535 }
537 throw std::runtime_error("Must run also sector tracking");
538 }
539 return 0;
540}
541
542#endif // GPUCA_GPUCODE
543
544GPUd() void GPUTPCGMMerger::ClearTrackLinks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, bool output)
545{
546 const int32_t n = output ? mMemory->nMergedTracks : SectorTrackInfoLocalTotal();
547 for (int32_t i = iBlock * nThreads + iThread; i < n; i += nThreads * nBlocks) {
548 mTrackLinks[i] = -1;
549 }
550}
551
552GPUd() int32_t GPUTPCGMMerger::RefitSectorTrack(GPUTPCGMSectorTrack& sectorTrack, const GPUTPCTrack* inTrack, float alpha, int32_t sector)
553{
555 prop.SetMaterialTPC();
556 prop.SetMaxSinPhi(constants::MAX_SIN_PHI);
557 prop.SetSeedingErrors(true); // Larger errors for seeds, better since we don't start with good hypothesis
558 prop.SetFitInProjections(true); // TODO: Was false, consider reenabling after fitInProjection is fixed
559 prop.SetPolynomialField(&Param().polynomialField);
561 trk.X() = inTrack->Param().GetX();
562 trk.Y() = inTrack->Param().GetY();
563 trk.Z() = inTrack->Param().GetZ();
564 trk.SinPhi() = inTrack->Param().GetSinPhi();
565 trk.DzDs() = inTrack->Param().GetDzDs();
566 trk.QPt() = inTrack->Param().GetQPt();
567 trk.TOffset() = Param().par.continuousTracking ? GetConstantMem()->calibObjects.fastTransform->convZOffsetToVertexTime(sector, inTrack->Param().GetZOffset(), Param().continuousMaxTimeBin) : 0;
568 const auto tmp = sectorTrack.ClusterTN() > sectorTrack.ClusterT0() ? std::array<float, 2>{sectorTrack.ClusterTN(), sectorTrack.ClusterT0()} : std::array<float, 2>{sectorTrack.ClusterT0(), sectorTrack.ClusterTN()};
569 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
570 sectorTrack.SetX2(0.f);
571 for (int32_t way = 0; way < 2; way++) {
572 if (way) {
573 prop.SetFitInProjections(true);
574 prop.SetPropagateBzOnly(true);
575 }
576 trk.ResetCovariance();
577 prop.SetTrack(&trk, alpha);
578 int32_t start = way ? inTrack->NHits() - 1 : 0;
579 int32_t end = way ? 0 : (inTrack->NHits() - 1);
580 int32_t incr = way ? -1 : 1;
581 for (int32_t i = start; i != end; i += incr) {
582 float x, y, z;
583 int32_t row, flags;
584 const GPUTPCTracker& tracker = GetConstantMem()->tpcTrackers[sector];
585 const GPUTPCHitId& ic = tracker.TrackHits()[inTrack->FirstHitID() + i];
586 int32_t clusterIndex = tracker.Data().ClusterDataIndex(tracker.Data().Row(ic.RowIndex()), ic.HitIndex());
587 row = ic.RowIndex();
588 const ClusterNative& cl = GetConstantMem()->ioPtrs.clustersNative->clustersLinear[GetConstantMem()->ioPtrs.clustersNative->clusterOffset[sector][0] + clusterIndex];
589 flags = cl.getFlags();
590 GetConstantMem()->calibObjects.fastTransform->Transform(sector, row, cl.getPad(), cl.getTime(), x, y, z, trk.TOffset());
591 if (prop.PropagateToXAlpha(x, alpha, way == 0)) {
592 return way == 0;
593 }
594 trk.ConstrainSinPhi();
595 if (prop.Update(y, z, row, Param(), flags & GPUTPCGMMergedTrackHit::clustererAndSharedFlags, 0, false, sector, -1.f, 0.f, 0.f)) { // TODO: Use correct time / avgCharge
596 return way == 0;
597 }
598 trk.ConstrainSinPhi();
599 }
600 if (way) {
601 sectorTrack.SetParam2(trk);
602 } else {
603 sectorTrack.Set(trk, inTrack, alpha, sector);
604 }
605 }
606 return 0;
607}
608
609GPUd() void GPUTPCGMMerger::SetTrackClusterT(GPUTPCGMSectorTrack& track, int32_t iSector, const GPUTPCTrack* sectorTr)
610{
611 const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
612 const GPUTPCHitId& ic1 = trk.TrackHits()[sectorTr->FirstHitID()];
613 const GPUTPCHitId& ic2 = trk.TrackHits()[sectorTr->FirstHitID() + sectorTr->NHits() - 1];
614 int32_t clusterIndex1 = trk.Data().ClusterDataIndex(trk.Data().Row(ic1.RowIndex()), ic1.HitIndex());
615 int32_t clusterIndex2 = trk.Data().ClusterDataIndex(trk.Data().Row(ic2.RowIndex()), ic2.HitIndex());
616 const ClusterNative* cl = GetConstantMem()->ioPtrs.clustersNative->clustersLinear + GetConstantMem()->ioPtrs.clustersNative->clusterOffset[iSector][0];
617 track.SetClusterT(cl[clusterIndex1].getTime(), cl[clusterIndex2].getTime());
618}
619
620GPUd() void GPUTPCGMMerger::UnpackSaveNumber(int32_t id)
621{
622 mSectorTrackInfoIndex[id] = mMemory->nUnpackedTracks;
623}
624
625GPUd() void GPUTPCGMMerger::UnpackSectorGlobal(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
626{
627 const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
628 float alpha = Param().Alpha(iSector);
629 const GPUTPCTrack* sectorTr = mMemory->firstExtrapolatedTracks[iSector];
630 uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
631 uint32_t nTracks = *trk.NTracks();
632 for (uint32_t itr = nLocalTracks + iBlock * nThreads + iThread; itr < nTracks; itr += nBlocks * nThreads) {
633 sectorTr = &trk.Tracks()[itr];
634 int32_t localId = mTrackIDs[((sectorTr->LocalTrackId() >> 24) & 0x3F) * mNMaxSingleSectorTracks + (sectorTr->LocalTrackId() & 0xFFFFFF)];
635 if (localId == -1) {
636 continue;
637 }
638 uint32_t myTrack = CAMath::AtomicAdd(&mMemory->nUnpackedTracks, 1u);
639 GPUTPCGMSectorTrack& track = mSectorTrackInfos[myTrack];
640 SetTrackClusterT(track, iSector, sectorTr);
641 track.Set(this, sectorTr, alpha, iSector);
642 track.SetGlobalSectorTrackCov();
643 track.SetX2(0.f);
644 track.SetPrevNeighbour(-1);
645 track.SetNextNeighbour(-1);
646 track.SetNextSegmentNeighbour(-1);
647 track.SetPrevSegmentNeighbour(-1);
648 track.SetLocalTrackId(localId | (sectorTr->LocalTrackId() & 0x40000000));
649 }
650}
651
652GPUd() void GPUTPCGMMerger::UnpackResetIds(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
653{
654 const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
655 uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
656 for (uint32_t i = iBlock * nThreads + iThread; i < nLocalTracks; i += nBlocks * nThreads) {
657 mTrackIDs[iSector * mNMaxSingleSectorTracks + i] = -1;
658 }
659}
660
661GPUd() void GPUTPCGMMerger::RefitSectorTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector)
662{
663 const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[iSector];
664 uint32_t nLocalTracks = trk.CommonMemory()->nLocalTracks;
665
666 float alpha = Param().Alpha(iSector);
667 const GPUTPCTrack* sectorTr = nullptr;
668
669 for (uint32_t itr = iBlock * nThreads + iThread; itr < nLocalTracks; itr += nBlocks * nThreads) {
670 sectorTr = &trk.Tracks()[itr];
672 SetTrackClusterT(track, iSector, sectorTr);
673 if (RefitSectorTrack(track, sectorTr, alpha, iSector)) {
674 track.Set(this, sectorTr, alpha, iSector); // TODO: Why does the refit fail, it shouldn't, this workaround should be removed
675 if (!track.FilterErrors(this, iSector, constants::MAX_SIN_PHI, 0.1f)) {
676 continue;
677 }
678 }
679
680 CADEBUG(GPUInfo("INPUT Sector %d, Track %u, QPt %f DzDs %f", iSector, itr, track.QPt(), track.DzDs()));
681 track.SetPrevNeighbour(-1);
682 track.SetNextNeighbour(-1);
683 track.SetNextSegmentNeighbour(-1);
684 track.SetPrevSegmentNeighbour(-1);
685 track.SetExtrapolatedTrackId(0, -1);
686 track.SetExtrapolatedTrackId(1, -1);
687 uint32_t myTrack = CAMath::AtomicAdd(&mMemory->nUnpackedTracks, 1u);
688 mTrackIDs[iSector * mNMaxSingleSectorTracks + sectorTr->LocalTrackId()] = myTrack;
689 mSectorTrackInfos[myTrack] = track;
690 }
691}
692
693GPUd() void GPUTPCGMMerger::LinkExtrapolatedTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
694{
695 for (int32_t itr = SectorTrackInfoGlobalFirst(0) + iBlock * nThreads + iThread; itr < SectorTrackInfoGlobalLast(NSECTORS - 1); itr += nThreads * nBlocks) {
696 GPUTPCGMSectorTrack& extrapolatedTrack = mSectorTrackInfos[itr];
697 GPUTPCGMSectorTrack& localTrack = mSectorTrackInfos[extrapolatedTrack.LocalTrackId() & 0xFFFFFF];
698 int up = (extrapolatedTrack.LocalTrackId() & 0x40000000) ? 1 : 0;
699 localTrack.SetExtrapolatedTrackId(up, itr);
700 }
701}
702
703GPUd() 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)
704{
705 //* prepare sector tracks for merging with next/previous/same sector
706 //* each track transported to the border line
707
708 float fieldBz = Param().bzCLight;
709
710 float dAlpha = Param().dAlpha / 2;
711 float x0 = 0;
712
713 if (iBorder == 0) { // transport to the left edge of the sector and rotate horizontally
714 dAlpha = dAlpha - CAMath::Pi() / 2;
715 } else if (iBorder == 1) { // transport to the right edge of the sector and rotate horizontally
716 dAlpha = -dAlpha - CAMath::Pi() / 2;
717 } else if (iBorder == 2) { // transport to the middle of the sector and rotate vertically to the border on the left
718 x0 = GPUTPCGeometry::Row2X(63);
719 } else if (iBorder == 3) { // transport to the middle of the sector and rotate vertically to the border on the right
720 dAlpha = -dAlpha;
721 x0 = GPUTPCGeometry::Row2X(63);
722 }
723
724 const float maxSin = CAMath::Sin(60.f / 180.f * CAMath::Pi());
725 float cosAlpha = CAMath::Cos(dAlpha);
726 float sinAlpha = CAMath::Sin(dAlpha);
727
728 GPUTPCGMSectorTrack trackTmp;
729 for (int32_t itr = iBlock * nThreads + iThread; itr < SectorTrackInfoLocalTotal(); itr += nThreads * nBlocks) {
730 const GPUTPCGMSectorTrack* track = &mSectorTrackInfos[itr];
731 int32_t iSector = track->Sector();
732
733 if (track->PrevSegmentNeighbour() >= 0 && track->Sector() == mSectorTrackInfos[track->PrevSegmentNeighbour()].Sector()) {
734 continue;
735 }
736 if (useOrigTrackParam) { // TODO: Check how far this makes sense with sector track refit
737 if (CAMath::Abs(track->QPt()) * Param().qptB5Scaler < Param().rec.tpc.mergerLooperQPtB5Limit) {
738 continue;
739 }
740 const GPUTPCGMSectorTrack* trackMin = track;
741 while (track->NextSegmentNeighbour() >= 0 && track->Sector() == mSectorTrackInfos[track->NextSegmentNeighbour()].Sector()) {
742 track = &mSectorTrackInfos[track->NextSegmentNeighbour()];
743 if (track->OrigTrack()->Param().X() < trackMin->OrigTrack()->Param().X()) {
744 trackMin = track;
745 }
746 }
747 trackTmp = *trackMin;
748 track = &trackTmp;
749 if (trackTmp.X2() != 0.f) {
750 trackTmp.UseParam2();
751 } else {
752 trackTmp.Set(this, trackMin->OrigTrack(), trackMin->Alpha(), trackMin->Sector());
753 }
754 } else {
755 if (CAMath::Abs(track->QPt()) * Param().qptB5Scaler < Param().rec.tpc.mergerLooperSecondHorizontalQPtB5Limit) {
756 if (iBorder == 0 && track->NextNeighbour() >= 0) {
757 continue;
758 }
759 if (iBorder == 1 && track->PrevNeighbour() >= 0) {
760 continue;
761 }
762 }
763 }
765
766 if (track->TransportToXAlpha(this, x0, sinAlpha, cosAlpha, fieldBz, b, maxSin)) {
767 b.SetTrackID(itr);
768 b.SetNClusters(track->NClusters());
769 for (int32_t i = 0; i < 4; i++) {
770 if (CAMath::Abs(b.Cov()[i]) >= 5.0f) {
771 b.SetCov(i, 5.0f);
772 }
773 }
774 if (CAMath::Abs(b.Cov()[4]) >= 0.5f) {
775 b.SetCov(4, 0.5f);
776 }
777 uint32_t myTrack = CAMath::AtomicAdd(&nB[iSector], 1u);
778 B[iSector][myTrack] = b;
779 }
780 }
781}
782
783template <>
784GPUd() 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, uint8_t mergeMode)
785{
786 CADEBUG(GPUInfo("\nMERGING Sectors %d %d NTracks %d %d CROSS %d", iSector1, iSector2, N1, N2, mergeMode));
787 GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
788 GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
789 for (int32_t itr = iBlock * nThreads + iThread; itr < N1; itr += nThreads * nBlocks) {
790 const GPUTPCGMBorderTrack& b = B1[itr];
791 float d = CAMath::Max(0.5f, 3.5f * CAMath::Sqrt(b.Cov()[1]));
792 if (CAMath::Abs(b.Par()[4]) * Param().qptB5Scaler >= 20) {
793 d *= 2;
794 } else if (d > 3) {
795 d = 3;
796 }
797 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));
798 const GPUTPCGMBorderRange range = {.fId = itr, .fMin = b.Par()[1] + b.ZOffsetLinear() - d, .fMax = b.Par()[1] + b.ZOffsetLinear() + d};
799 range1[itr] = range;
800 if (iSector1 == iSector2) {
801 range2[itr] = range;
802 }
803 }
804 if (iSector1 != iSector2) {
805 for (int32_t itr = iBlock * nThreads + iThread; itr < N2; itr += nThreads * nBlocks) {
806 const GPUTPCGMBorderTrack& b = B2[itr];
807 float d = CAMath::Max(0.5f, 3.5f * CAMath::Sqrt(b.Cov()[1]));
808 if (CAMath::Abs(b.Par()[4]) * Param().qptB5Scaler >= 20) {
809 d *= 2;
810 } else if (d > 3) {
811 d = 3;
812 }
813 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));
814 range2[itr] = {.fId = itr, .fMin = b.Par()[1] + b.ZOffsetLinear() - d, .fMax = b.Par()[1] + b.ZOffsetLinear() + d};
815 }
816 }
817}
818
819template <>
820GPUd() 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, uint8_t mergeMode)
821{
822#if !defined(GPUCA_GPUCODE_GENRTC)
823 GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
824 GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
825
826 if (iThread == 0) {
827 if (iBlock == 0) {
828 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); });
829 } else if (iBlock == 1) {
830 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); });
831 }
832 }
833#else
834 printf("This sorting variant is disabled for RTC\n");
835#endif
836}
837
838template <>
839GPUd() void GPUTPCGMMerger::MergeBorderTracks<3>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, GPUTPCGMBorderRange* range, int32_t N, int32_t cmpMax)
840{
841#ifndef GPUCA_SPECIALIZE_THRUST_SORTS
842 if (iThread == 0 && iBlock == 0) {
843 if (cmpMax) {
844 GPUCommonAlgorithm::sortDeviceDynamic(range, range + N, MergeBorderTracks_compMax());
845 } else {
846 GPUCommonAlgorithm::sortDeviceDynamic(range, range + N, MergeBorderTracks_compMin());
847 }
848 }
849#endif
850}
851
852template <>
853GPUd() 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, uint8_t mergeMode)
854{
855 // int32_t statAll = 0, statMerged = 0;
856 float factor2ys = Param().rec.tpc.trackMergerFactor2YS;
857 float factor2zt = Param().rec.tpc.trackMergerFactor2ZT;
858 float factor2k = Param().rec.tpc.trackMergerFactor2K;
859 float factor2General = Param().rec.tpc.trackMergerFactor2General;
860
861 factor2k = factor2General * factor2k;
862 factor2ys = factor2General * factor2ys;
863 factor2zt = factor2General * factor2zt;
864
865 int32_t minNPartHits = Param().rec.tpc.trackMergerMinPartHits;
866 int32_t minNTotalHits = Param().rec.tpc.trackMergerMinTotalHits;
867
868 bool sameSector = (iSector1 == iSector2);
869
870 GPUTPCGMBorderRange* range1 = mBorderRange[iSector1];
871 GPUTPCGMBorderRange* range2 = mBorderRange[iSector2] + *GetConstantMem()->tpcTrackers[iSector2].NTracks();
872
873 int32_t i2 = 0;
874 for (int32_t i1 = iBlock * nThreads + iThread; i1 < N1; i1 += nThreads * nBlocks) {
875 GPUTPCGMBorderRange r1 = range1[i1];
876 while (i2 < N2 && range2[i2].fMax < r1.fMin) {
877 i2++;
878 }
879
880 const GPUTPCGMBorderTrack& b1 = B1[r1.fId];
881 if (b1.NClusters() < minNPartHits) {
882 continue;
883 }
884 int32_t iBest2 = -1;
885 int32_t lBest2 = 0;
886 // statAll++;
887 for (int32_t k2 = i2; k2 < N2; k2++) {
888 GPUTPCGMBorderRange r2 = range2[k2];
889 if (r2.fMin > r1.fMax) {
890 break;
891 }
892 if (sameSector && (r1.fId >= r2.fId)) {
893 continue;
894 }
895 // do check
896
897 const GPUTPCGMBorderTrack& b2 = B2[r2.fId];
898#if defined(GPUCA_MERGER_BY_MC_LABEL) && !defined(GPUCA_GPUCODE)
899 int64_t label1 = GetTrackLabel(b1);
900 int64_t label2 = GetTrackLabel(b2);
901 if (label1 != label2 && label1 != -1) // DEBUG CODE, match by MC label
902#endif
903 {
904 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", ""); });
905 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"); });
906 if (b2.NClusters() < lBest2) {
907 CADEBUG2(continue, printf("!NCl1\n"));
908 }
909 if (mergeMode & mergeModes::mergeAcrossCE) {
910 // Merging CE tracks
911 int32_t maxRowDiff = (mergeMode & mergeModes::mergeAtCluster) ? 1 : 3; // TODO: check cut
912 if (CAMath::Abs(b1.Row() - b2.Row()) > maxRowDiff) {
913 CADEBUG2(continue, printf("!ROW\n"));
914 }
915 if (CAMath::Abs(b1.Par()[2] - b2.Par()[2]) > 0.5f || CAMath::Abs(b1.Par()[3] - b2.Par()[3]) > 0.5f) {
916 CADEBUG2(continue, printf("!CE SinPhi/Tgl\n")); // Crude cut to avoid totally wrong matches, TODO: check cut
917 }
918 }
919
920 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)); });
921
922 if (!b1.CheckChi2Y(b2, factor2ys)) {
923 CADEBUG2(continue, printf("!Y\n"));
924 }
925 // if( !b1.CheckChi2Z(b2, factor2zt ) ) CADEBUG2(continue, printf("!NCl1\n"));
926 if (!b1.CheckChi2QPt(b2, factor2k)) {
927 CADEBUG2(continue, printf("!QPt\n"));
928 }
929 float fys = CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20 ? factor2ys : (2.f * factor2ys);
930 float fzt = CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20 ? factor2zt : (2.f * factor2zt);
931 if (!b1.CheckChi2YS(b2, fys)) {
932 CADEBUG2(continue, printf("!YS\n"));
933 }
934 if (!b1.CheckChi2ZT(b2, fzt)) {
935 CADEBUG2(continue, printf("!ZT\n"));
936 }
937 if (CAMath::Abs(b1.Par()[4]) * Param().qptB5Scaler < 20) {
938 if (b2.NClusters() < minNPartHits) {
939 CADEBUG2(continue, printf("!NCl2\n"));
940 }
941 if (b1.NClusters() + b2.NClusters() < minNTotalHits) {
942 CADEBUG2(continue, printf("!NCl3\n"));
943 }
944 }
945 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])));
946 } // DEBUG CODE, match by MC label
947 lBest2 = b2.NClusters();
948 iBest2 = b2.TrackID();
949 }
950
951 if (iBest2 < 0) {
952 continue;
953 }
954 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)); });
955
956 // statMerged++;
957
958 CADEBUG(GPUInfo("Found match %d %d", b1.TrackID(), iBest2));
959
960 mTrackLinks[b1.TrackID()] = iBest2;
961 if (mergeMode & mergeModes::mergeAcrossCE) {
962 GPUCA_DETERMINISTIC_CODE(CAMath::AtomicMax(&mTrackLinks[iBest2], b1.TrackID()), mTrackLinks[iBest2] = b1.TrackID());
963 }
964 }
965 // GPUInfo("STAT: sectors %d, %d: all %d merged %d", iSector1, iSector2, statAll, statMerged);
966}
967
968GPUdii() void GPUTPCGMMerger::MergeBorderTracksSetup(int32_t& n1, int32_t& n2, GPUTPCGMBorderTrack*& b1, GPUTPCGMBorderTrack*& b2, int32_t& jSector, int32_t iSector, uint8_t mergeMode) const
969{
970 if (mergeMode & mergeModes::mergeWithinSector) { // Merge tracks within the same sector
971 jSector = iSector;
972 n1 = n2 = mMemory->tmpCounter[iSector];
973 b1 = b2 = mBorder[iSector];
974 } else if (mergeMode & mergeModes::mergeAcrossCE) { // Merge tracks accross the central electrode
975 jSector = (iSector + NSECTORS / 2);
976 const int32_t offset = (mergeMode & mergeModes::mergeAtCluster) ? NSECTORS : 0;
977 n1 = mMemory->tmpCounter[iSector + offset];
978 n2 = mMemory->tmpCounter[jSector + offset];
979 b1 = mBorder[iSector + offset];
980 b2 = mBorder[jSector + offset];
981 } else { // Merge tracks of adjacent sectors
982 jSector = mNextSectorInd[iSector];
983 n1 = mMemory->tmpCounter[iSector];
984 n2 = mMemory->tmpCounter[NSECTORS + jSector];
985 b1 = mBorder[iSector];
986 b2 = mBorder[NSECTORS + jSector];
987 }
988}
989
990template <int32_t I>
991GPUd() void GPUTPCGMMerger::MergeBorderTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector, uint8_t mergeMode)
992{
993 int32_t n1, n2;
994 GPUTPCGMBorderTrack *b1, *b2;
995 int32_t jSector;
996 MergeBorderTracksSetup(n1, n2, b1, b2, jSector, iSector, mergeMode);
997 MergeBorderTracks<I>(nBlocks, nThreads, iBlock, iThread, iSector, b1, n1, jSector, b2, n2, mergeMode);
998}
999
1000#if !defined(GPUCA_GPUCODE) || defined(GPUCA_GPUCODE_DEVICE) // FIXME: DR: WORKAROUND to avoid CUDA bug creating host symbols for device code.
1001template GPUdni() void GPUTPCGMMerger::MergeBorderTracks<0>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector, uint8_t mergeMode);
1002template GPUdni() void GPUTPCGMMerger::MergeBorderTracks<1>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector, uint8_t mergeMode);
1003template GPUdni() void GPUTPCGMMerger::MergeBorderTracks<2>(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t iSector, uint8_t mergeMode);
1004#endif
1005
1006GPUd() void GPUTPCGMMerger::MergeWithinSectorsPrepare(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1007{
1008 float x0 = GPUTPCGeometry::Row2X(63);
1009 const float maxSin = CAMath::Sin(60.f / 180.f * CAMath::Pi());
1010
1011 for (int32_t itr = iBlock * nThreads + iThread; itr < SectorTrackInfoLocalTotal(); itr += nThreads * nBlocks) {
1012 const GPUTPCGMSectorTrack& track = mSectorTrackInfos[itr];
1013 int32_t iSector = track.Sector();
1015 if (track.TransportToX(this, x0, Param().bzCLight, b, maxSin)) {
1016 b.SetTrackID(itr);
1017 CADEBUG(printf("WITHIN SECTOR %d Track %d - ", iSector, 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("\n"));
1018 b.SetNClusters(track.NClusters());
1019 uint32_t myTrack = CAMath::AtomicAdd(&mMemory->tmpCounter[iSector], 1u);
1020 mBorder[iSector][myTrack] = b;
1021 }
1022 }
1023}
1024
1025GPUd() void GPUTPCGMMerger::MergeSectorsPrepare(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int32_t border0, int32_t border1, int8_t useOrigTrackParam)
1026{
1027 bool part2 = iBlock & 1;
1028 int32_t border = part2 ? border1 : border0;
1029 GPUAtomic(uint32_t)* n = mMemory->tmpCounter;
1030 GPUTPCGMBorderTrack** b = mBorder;
1031 if (part2) {
1032 n += NSECTORS;
1033 b += NSECTORS;
1034 }
1035 MergeSectorsPrepareStep2((nBlocks + !part2) >> 1, nThreads, iBlock >> 1, iThread, border, b, n, useOrigTrackParam);
1036}
1037
1038GPUdi() void GPUTPCGMMerger::setBlockRange(int32_t elems, int32_t nBlocks, int32_t iBlock, int32_t& start, int32_t& end)
1039{
1040 start = (elems + nBlocks - 1) / nBlocks * iBlock;
1041 end = (elems + nBlocks - 1) / nBlocks * (iBlock + 1);
1042 end = CAMath::Min(elems, end);
1043}
1044
1045GPUd() void GPUTPCGMMerger::hookEdge(int32_t u, int32_t v)
1046{
1047 if (v < 0) {
1048 return;
1049 }
1050 while (true) {
1051 u = mTrackCCRoots[u];
1052 v = mTrackCCRoots[v];
1053 if (u == v) {
1054 break;
1055 }
1056 int32_t h = CAMath::Max(u, v);
1057 int32_t l = CAMath::Min(u, v);
1058
1059 int32_t old = CAMath::AtomicCAS(&mTrackCCRoots[h], h, l);
1060 if (old == h) {
1061 break;
1062 }
1063
1064 u = mTrackCCRoots[h];
1065 v = l;
1066 }
1067}
1068
1069GPUd() void GPUTPCGMMerger::ResolveFindConnectedComponentsSetup(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1070{
1071 int32_t start, end;
1072 setBlockRange(SectorTrackInfoLocalTotal(), nBlocks, iBlock, start, end);
1073 for (int32_t i = start + iThread; i < end; i += nThreads) {
1074 mTrackCCRoots[i] = i;
1075 }
1076}
1077
1078GPUd() void GPUTPCGMMerger::ResolveFindConnectedComponentsHookLinks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1079{
1080 // Compute connected components in parallel, step 1.
1081 // Source: Adaptive Work-Efficient Connected Components on the GPU, Sutton et al, 2016 (https://arxiv.org/pdf/1612.01178.pdf)
1082 int32_t start, end;
1083 setBlockRange(SectorTrackInfoLocalTotal(), nBlocks, iBlock, start, end);
1084 for (int32_t itr = start + iThread; itr < end; itr += nThreads) {
1085 hookEdge(itr, mTrackLinks[itr]);
1086 }
1087}
1088
1089GPUd() void GPUTPCGMMerger::ResolveFindConnectedComponentsHookNeighbors(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1090{
1091 // Compute connected components in parallel, step 1 - Part 2.
1092 nBlocks = nBlocks / 4 * 4;
1093 if (iBlock >= nBlocks) {
1094 return;
1095 }
1096
1097 int32_t start, end;
1098 setBlockRange(SectorTrackInfoLocalTotal(), nBlocks / 4, iBlock / 4, start, end);
1099
1100 int32_t myNeighbor = iBlock % 4;
1101
1102 for (int32_t itr = start + iThread; itr < end; itr += nThreads) {
1103 int32_t v = mSectorTrackInfos[itr].AnyNeighbour(myNeighbor);
1104 hookEdge(itr, v);
1105 }
1106}
1107
1108GPUd() void GPUTPCGMMerger::ResolveFindConnectedComponentsMultiJump(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1109{
1110 // Compute connected components in parallel, step 2.
1111 int32_t start, end;
1112 setBlockRange(SectorTrackInfoLocalTotal(), nBlocks, iBlock, start, end);
1113 for (int32_t itr = start + iThread; itr < end; itr += nThreads) {
1114 int32_t root = itr;
1115 int32_t next = mTrackCCRoots[root];
1116 if (root == next) {
1117 continue;
1118 }
1119 do {
1120 root = next;
1121 next = mTrackCCRoots[next];
1122 } while (root != next);
1123 mTrackCCRoots[itr] = root;
1124 }
1125}
1126
1127GPUd() void GPUTPCGMMerger::ResolveMergeSectors(GPUResolveSharedMemory& smem, int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread, int8_t useOrigTrackParam, int8_t mergeAll)
1128{
1129 if (!mergeAll) {
1130 /*int32_t neighborType = useOrigTrackParam ? 1 : 0;
1131 int32_t old1 = newTrack2.PrevNeighbour(0);
1132 int32_t old2 = newTrack1.NextNeighbour(0);
1133 if (old1 < 0 && old2 < 0) neighborType = 0;
1134 if (old1 == itr) continue;
1135 if (neighborType) old1 = newTrack2.PrevNeighbour(1);
1136 if ( old1 >= 0 )
1137 {
1138 GPUTPCGMSectorTrack &oldTrack1 = mSectorTrackInfos[old1];
1139 if ( oldTrack1.NClusters() < newTrack1.NClusters() ) {
1140 newTrack2.SetPrevNeighbour( -1, neighborType );
1141 oldTrack1.SetNextNeighbour( -1, neighborType );
1142 } else continue;
1143 }
1144
1145 if (old2 == itr2) continue;
1146 if (neighborType) old2 = newTrack1.NextNeighbour(1);
1147 if ( old2 >= 0 )
1148 {
1149 GPUTPCGMSectorTrack &oldTrack2 = mSectorTrackInfos[old2];
1150 if ( oldTrack2.NClusters() < newTrack2.NClusters() )
1151 {
1152 oldTrack2.SetPrevNeighbour( -1, neighborType );
1153 } else continue;
1154 }
1155 newTrack1.SetNextNeighbour( itr2, neighborType );
1156 newTrack2.SetPrevNeighbour( itr, neighborType );*/
1157 }
1158
1159 int32_t start, end;
1160 setBlockRange(SectorTrackInfoLocalTotal(), nBlocks, iBlock, start, end);
1161
1162 // Walk a segment chain to its end, dir 0 towards the head and 1 towards the
1163 // tail, reporting whether target was met on the way.
1164 auto walkSegmentChain = [tracks = mSectorTrackInfos](GPUTPCGMSectorTrack*& track, const GPUTPCGMSectorTrack* target, int32_t dir) {
1165 while (track->SegmentNeighbour(dir) >= 0) {
1166 track = &tracks[track->SegmentNeighbour(dir)];
1167 if (track == target) {
1168 return true;
1169 }
1170 }
1171 return false;
1172 };
1173 // Report whether target sits on either neighbour chain starting at from.
1174 auto neighbourChainReaches = [tracks = mSectorTrackInfos](const GPUTPCGMSectorTrack* from, const GPUTPCGMSectorTrack* target) {
1175 for (int32_t k = 0; k < 2; k++) {
1176 const GPUTPCGMSectorTrack* tmp = from;
1177 while (tmp->Neighbour(k) >= 0) {
1178 tmp = &tracks[tmp->Neighbour(k)];
1179 if (tmp == target) {
1180 return true;
1181 }
1182 }
1183 }
1184 return false;
1185 };
1186
1187 for (int32_t baseIdx = 0; baseIdx < SectorTrackInfoLocalTotal(); baseIdx += nThreads) {
1188 int32_t itr = baseIdx + iThread;
1189 bool inRange = itr < SectorTrackInfoLocalTotal();
1190
1191 int32_t itr2 = -1;
1192 if (inRange) {
1193 itr2 = mTrackLinks[itr];
1194 }
1195
1196 bool resolveSector = (itr2 > -1);
1197 if (resolveSector) {
1198 int32_t root = mTrackCCRoots[itr];
1199 resolveSector &= (start <= root) && (root < end);
1200 }
1201
1202 int16_t smemIdx = work_group_scan_inclusive_add(int16_t(resolveSector));
1203
1204 if (resolveSector) {
1205 smem.iTrack1[smemIdx - 1] = itr;
1206 smem.iTrack2[smemIdx - 1] = itr2;
1207 }
1208 GPUbarrier();
1209
1210 if (iThread < nThreads - 1) {
1211 continue;
1212 }
1213
1214 const int32_t nSectors = smemIdx;
1215
1216 for (int32_t i = 0; i < nSectors; i++) {
1217 itr = smem.iTrack1[i];
1218 itr2 = smem.iTrack2[i];
1219
1220 GPUTPCGMSectorTrack* track1 = &mSectorTrackInfos[itr];
1221 GPUTPCGMSectorTrack* track2 = &mSectorTrackInfos[itr2];
1222 GPUTPCGMSectorTrack* track1Base = track1;
1223 GPUTPCGMSectorTrack* track2Base = track2;
1224
1225 bool sameSegment = CAMath::Abs(track1->NClusters() > track2->NClusters() ? track1->QPt() : track2->QPt()) * Param().qptB5Scaler < 2 || track1->QPt() * track2->QPt() > 0;
1226 // GPUInfo("\nMerge %d with %d - same segment %d", itr, itr2, (int32_t) sameSegment);
1227 // PrintMergeGraph(track1, std::cout);
1228 // PrintMergeGraph(track2, std::cout);
1229
1230 walkSegmentChain(track2, nullptr, 0);
1231 if (sameSegment) {
1232 if (track1 == track2) {
1233 continue;
1234 }
1235 if (walkSegmentChain(track1, track2, 0)) {
1236 continue;
1237 }
1238 GPUCommonAlgorithm::swap(track1, track1Base);
1239 if (neighbourChainReaches(track1Base, track2)) {
1240 continue;
1241 }
1242 if (walkSegmentChain(track1, track2, 1)) {
1243 continue;
1244 }
1245 } else {
1246 walkSegmentChain(track1, nullptr, 0);
1247
1248 if (track1 == track2) {
1249 continue;
1250 }
1251 if (neighbourChainReaches(track1, track2)) {
1252 continue;
1253 }
1254
1255 float z1min, z1max, z2min, z2max;
1256 z1min = track1->MinClusterT();
1257 z1max = track1->MaxClusterT();
1258 z2min = track2->MinClusterT();
1259 z2max = track2->MaxClusterT();
1260 if (track1 != track1Base) {
1261 z1min = CAMath::Min(z1min, track1Base->MinClusterT());
1262 z1max = CAMath::Max(z1max, track1Base->MaxClusterT());
1263 }
1264 if (track2 != track2Base) {
1265 z2min = CAMath::Min(z2min, track2Base->MinClusterT());
1266 z2max = CAMath::Max(z2max, track2Base->MaxClusterT());
1267 }
1268 bool goUp = z2max - z1min > z1max - z2min;
1269
1270 if (track1->Neighbour(goUp) < 0 && track2->Neighbour(!goUp) < 0) {
1271 track1->SetNeighbor(track2 - mSectorTrackInfos, goUp);
1272 track2->SetNeighbor(track1 - mSectorTrackInfos, !goUp);
1273 // GPUInfo("Result (simple neighbor)");
1274 // PrintMergeGraph(track1, std::cout);
1275 continue;
1276 } else if (track1->Neighbour(goUp) < 0) {
1277 track2 = &mSectorTrackInfos[track2->Neighbour(!goUp)];
1278 GPUCommonAlgorithm::swap(track1, track2);
1279 } else if (track2->Neighbour(!goUp) < 0) {
1280 track1 = &mSectorTrackInfos[track1->Neighbour(goUp)];
1281 } else { // Both would work, but we use the simpler one
1282 track1 = &mSectorTrackInfos[track1->Neighbour(goUp)];
1283 }
1284 track1Base = track1;
1285 }
1286
1287 track2Base = track2;
1288 if (!sameSegment) {
1289 walkSegmentChain(track1, nullptr, 1);
1290 }
1291 track1->SetNextSegmentNeighbour(track2 - mSectorTrackInfos);
1292 track2->SetPrevSegmentNeighbour(track1 - mSectorTrackInfos);
1293 // k = 0: Merge right side
1294 // k = 1: Merge left side
1295 for (int32_t k = 0; k < 2; k++) {
1296 track1 = track1Base;
1297 track2 = track2Base;
1298 while (track2->Neighbour(k) >= 0) {
1299 if (track1->Neighbour(k) >= 0) {
1300 GPUTPCGMSectorTrack* track1new = &mSectorTrackInfos[track1->Neighbour(k)];
1301 GPUTPCGMSectorTrack* track2new = &mSectorTrackInfos[track2->Neighbour(k)];
1302 track2->SetNeighbor(-1, k);
1303 track2new->SetNeighbor(-1, k ^ 1);
1304 track1 = track1new;
1305 walkSegmentChain(track1, nullptr, 1);
1306 track1->SetNextSegmentNeighbour(track2new - mSectorTrackInfos);
1307 track2new->SetPrevSegmentNeighbour(track1 - mSectorTrackInfos);
1308 track1 = track1new;
1309 track2 = track2new;
1310 } else {
1311 GPUTPCGMSectorTrack* track2new = &mSectorTrackInfos[track2->Neighbour(k)];
1312 track1->SetNeighbor(track2->Neighbour(k), k);
1313 track2->SetNeighbor(-1, k);
1314 track2new->SetNeighbor(track1 - mSectorTrackInfos, k ^ 1);
1315 }
1316 }
1317 }
1318 // GPUInfo("Result");
1319 // PrintMergeGraph(track1, std::cout);
1320 }
1321 }
1322}
1323
1324GPUd() void GPUTPCGMMerger::MergeCEFill(const GPUTPCGMSectorTrack* track, const GPUTPCGMMergedTrackHit& cls, int32_t itr)
1325{
1326 if (Param().rec.tpc.mergerCERowLimit > 0 && CAMath::Abs(track->QPt()) * Param().qptB5Scaler < 0.3f && (cls.row < Param().rec.tpc.mergerCERowLimit || cls.row >= GPUTPCGeometry::NROWS - Param().rec.tpc.mergerCERowLimit)) {
1327 return;
1328 }
1329
1330 float z = 0;
1331 {
1332 float x, y;
1333 auto& cln = mConstantMem->ioPtrs.clustersNative->clustersLinear[cls.num];
1334 GPUTPCConvertImpl::convert(*mConstantMem, cls.sector, cls.row, cln.getPad(), cln.getTime(), x, y, z);
1335 }
1336
1337 if (!Param().par.continuousTracking && CAMath::Abs(z) > 10) {
1338 return;
1339 }
1340 int32_t sector = track->Sector();
1341 for (int32_t attempt = 0; attempt < 2; attempt++) {
1343 const float x0 = GPUTPCGeometry::Row2X(attempt == 0 ? 63 : cls.row); // TODO: Fix 63
1344 if (track->TransportToX(this, x0, Param().bzCLight, b, constants::MAX_SIN_PHI_LOW)) {
1345 b.SetTrackID(itr);
1346 b.SetNClusters(mMergedTracks[itr].NClusters());
1347 if (CAMath::Abs(b.Cov()[4]) >= 0.5f) {
1348 b.SetCov(4, 0.5f); // TODO: Is this needed and better than the cut in BorderTrack?
1349 }
1350 if (track->CSide()) {
1351 b.SetPar(1, b.Par()[1] - 2 * (z - b.ZOffsetLinear()));
1352 b.SetZOffsetLinear(-b.ZOffsetLinear());
1353 }
1354 b.SetRow(cls.row);
1355 uint32_t id = sector + attempt * NSECTORS;
1356 uint32_t myTrack = CAMath::AtomicAdd(&mMemory->tmpCounter[id], 1u);
1357 mBorder[id][myTrack] = b;
1358 break;
1359 }
1360 }
1361}
1362
1363GPUd() void GPUTPCGMMerger::MergeCE(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1364{
1365 const ClusterNative* cls = mConstantMem->ioPtrs.clustersNative->clustersLinear;
1366 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nThreads * nBlocks) {
1367 if (mMergedTracks[i].CSide() == 0 && mTrackLinks[i] >= 0) {
1368 if (mTrackLinks[mTrackLinks[i]] != (int32_t)i) {
1369 continue;
1370 }
1371 GPUTPCGMMergedTrack* trk[2] = {&mMergedTracks[i], &mMergedTracks[mTrackLinks[i]]};
1372
1373 if (!trk[1]->OK() || trk[1]->CCE()) {
1374 continue;
1375 }
1376 bool celooper = (trk[0]->GetParam().GetQPt() * Param().qptB5Scaler > 1 && trk[0]->GetParam().GetQPt() * trk[1]->GetParam().GetQPt() < 0);
1377 celooper |= trk[0]->PrevSegment() != -1 && trk[1]->PrevSegment() != -1;
1378 if (!celooper && trk[0]->GetParam().GetPar(3) * trk[1]->GetParam().GetPar(3) < 0) {
1379 continue;
1380 }
1381
1382 bool needswap = false;
1383 if (trk[0]->PrevSegment() == -1 && trk[1]->PrevSegment() >= 0) {
1384 needswap = true;
1385 } else if (celooper) {
1386 const float z0max = -CAMath::Min(cls[mClusters[trk[0]->FirstClusterRef()].num].getTime(), cls[mClusters[trk[0]->FirstClusterRef() + trk[0]->NClusters() - 1].num].getTime());
1387 const float z1max = -CAMath::Min(cls[mClusters[trk[1]->FirstClusterRef()].num].getTime(), cls[mClusters[trk[1]->FirstClusterRef() + trk[1]->NClusters() - 1].num].getTime());
1388 if (z1max < z0max) {
1389 needswap = true;
1390 }
1391 } else {
1392 if (mClusters[trk[0]->FirstClusterRef()].row > mClusters[trk[1]->FirstClusterRef()].row) {
1393 needswap = true;
1394 }
1395 }
1396 if (needswap) {
1397 GPUCommonAlgorithm::swap(trk[0], trk[1]);
1398 }
1399
1400 if (Param().par.continuousTracking) {
1401 GPUTPCGMMergedTrackHit* clsmax;
1402 const float tmax = CAMath::MaxWithRef(cls[mClusters[trk[0]->FirstClusterRef()].num].getTime(), cls[mClusters[trk[0]->FirstClusterRef() + trk[0]->NClusters() - 1].num].getTime(),
1403 cls[mClusters[trk[1]->FirstClusterRef()].num].getTime(), cls[mClusters[trk[1]->FirstClusterRef() + trk[1]->NClusters() - 1].num].getTime(),
1404 &mClusters[trk[0]->FirstClusterRef()], &mClusters[trk[0]->FirstClusterRef() + trk[0]->NClusters() - 1],
1405 &mClusters[trk[1]->FirstClusterRef()], &mClusters[trk[1]->FirstClusterRef() + trk[1]->NClusters() - 1], clsmax);
1406 const float offset = CAMath::Max(tmax - mConstantMem->calibObjects.fastTransform->getMaxDriftTime(clsmax->sector, clsmax->row, cls[clsmax->num].getPad()), 0.f);
1407 trk[1]->Param().Z() += mConstantMem->calibObjects.fastTransform->convDeltaTimeToDeltaZinTimeFrame(trk[1]->CSide() * NSECTORS / 2, trk[1]->Param().TOffset() - offset);
1408 trk[1]->Param().TOffset() = offset;
1409 if (celooper) {
1410 trk[0]->Param().Z() += mConstantMem->calibObjects.fastTransform->convDeltaTimeToDeltaZinTimeFrame(trk[0]->CSide() * NSECTORS / 2, trk[0]->Param().TOffset() - offset);
1411 trk[0]->Param().TOffset() = offset;
1412 }
1413 }
1414
1415 if (celooper) { // TODO: Need propper handling, avoid falsely flagging the primary leg as looper
1416 trk[0]->SetMergedLooperConnected(true);
1417 trk[0]->SetCCE(true);
1418 trk[0]->SetLooper(true);
1419 trk[1]->SetMergedLooperConnected(true);
1420 trk[1]->SetCCE(true);
1421 trk[1]->SetLooper(true);
1422 continue;
1423 }
1424
1425 uint32_t newRef = CAMath::AtomicAdd(&mMemory->nMergedTrackClusters, trk[0]->NClusters() + trk[1]->NClusters());
1426 if (newRef + trk[0]->NClusters() + trk[1]->NClusters() >= mNMaxMergedTrackClusters) {
1427 raiseError(GPUErrors::ERROR_MERGER_CE_HIT_OVERFLOW, newRef + trk[0]->NClusters() + trk[1]->NClusters(), mNMaxMergedTrackClusters);
1428 for (uint32_t k = newRef; k < mNMaxMergedTrackClusters; k++) {
1429 mClusters[k].num = 0;
1430 mClusters[k].state = 0;
1431 }
1432 CAMath::AtomicExch(&mMemory->nMergedTrackClusters, mNMaxMergedTrackClusters);
1433 return;
1434 }
1435
1436 int32_t pos = newRef;
1437#pragma unroll
1438 for (int32_t k = 1; k >= 0; k--) {
1439 for (uint32_t j = 0; j != trk[k]->NClusters(); j++) {
1440 mClusters[pos++] = mClusters[trk[k]->FirstClusterRef() + j];
1441 }
1442 }
1443 trk[1]->SetFirstClusterRef(newRef);
1444 trk[1]->SetNClusters(trk[0]->NClusters() + trk[1]->NClusters());
1445 if (trk[1]->NClusters() > constants::MERGER_MAX_TRACK_CLUSTERS) {
1446 trk[1]->SetFirstClusterRef(trk[1]->FirstClusterRef() + trk[1]->NClusters() - constants::MERGER_MAX_TRACK_CLUSTERS);
1447 trk[1]->SetNClusters(constants::MERGER_MAX_TRACK_CLUSTERS);
1448 }
1449 trk[1]->SetCCE(true);
1450 trk[0]->SetNClusters(0);
1451 trk[0]->SetOK(false);
1452 }
1453 }
1454
1455 // for (int32_t i = 0;i < mMemory->nMergedTracks;i++) {if (mMergedTracks[i].CCE() == false) {mMergedTracks[i].SetNClusters(0);mMergedTracks[i].SetOK(false);}} //Remove all non-CE tracks
1456}
1457
1458namespace o2::gpu::internal
1459{
1460namespace // anonymous
1461{
1462struct GPUTPCGMMerger_CompareClusterIds {
1464 const bool revert;
1465 GPUd() GPUTPCGMMerger_CompareClusterIds(const GPUTPCGMMerger::trackCluster* cmp, bool r) : mCmp(cmp), revert(r) {}
1466 GPUd() bool operator()(const int16_t aa, const int16_t bb)
1467 {
1468 const GPUTPCGMMerger::trackCluster& a = mCmp[aa];
1470 if (a.row != b.row) {
1471 return (a.row > b.row) ^ revert;
1472 }
1473 return GPUCA_DETERMINISTIC_CODE((a.id != b.id) ? (a.id > b.id) : (aa > bb), a.id > b.id);
1474 }
1475};
1476} // anonymous namespace
1477} // namespace o2::gpu::internal
1478
1479GPUd() void GPUTPCGMMerger::CollectMergedTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1480{
1481 constexpr int32_t kMaxParts = 16;
1482 constexpr int32_t kMaxClusters = constants::MERGER_MAX_TRACK_CLUSTERS;
1483
1484 GPUTPCGMSectorTrack* trackParts[kMaxParts];
1485
1486 int32_t itr = iBlock * nThreads + iThread;
1487 GPUTPCGMSectorTrack* trbase = nullptr;
1488 int32_t leg = 0;
1489 int32_t lastMergedSegment = -1;
1490 bool revertSegments = false;
1491 bool revertInSegment = false;
1492 while (true) {
1493 if (trbase && !Param().rec.tpc.dropLoopers) {
1494 int32_t jtr = trbase->NextNeighbour();
1495 if (jtr >= 0) {
1496 trbase = &(mSectorTrackInfos[jtr]);
1497 if (trbase->PrevSegmentNeighbour() >= 0) {
1498 trbase = nullptr;
1499 } else {
1500 if (Param().rec.enableCyclicGraphWorkarounds) {
1501 trbase->SetPrevSegmentNeighbour(1000000001);
1502 }
1503 leg += revertSegments ? 1 : -1;
1504 }
1505 } else {
1506 trbase = nullptr;
1507 }
1508 }
1509
1510 if (trbase == nullptr) {
1511 while (itr < SectorTrackInfoLocalTotal()) {
1512 trbase = &mSectorTrackInfos[itr];
1513 if (trbase->PrevSegmentNeighbour() >= 0 || trbase->PrevNeighbour() >= 0) {
1514 itr += nThreads * nBlocks;
1515 continue;
1516 }
1517 break;
1518 }
1519 if (itr >= SectorTrackInfoLocalTotal()) {
1520 break;
1521 }
1522 revertSegments = false;
1523 revertInSegment = false;
1524 if (Param().rec.enableCyclicGraphWorkarounds) {
1525 trbase->SetPrevSegmentNeighbour(1000000000);
1526 }
1527 int32_t jtr = trbase->NextNeighbour();
1528 leg = 0;
1529 if (jtr >= 0) {
1530 int32_t lasttr = itr;
1531 while (jtr >= 0) { // --------------- count segments ---------------
1532 if (Param().rec.enableCyclicGraphWorkarounds && &mSectorTrackInfos[jtr] == trbase) {
1533 break; // Break cyclic graph
1534 }
1535 lasttr = jtr;
1536 leg++;
1537 jtr = mSectorTrackInfos[jtr].NextNeighbour();
1538 }
1539
1540 float mainT = 1e9;
1541 revertSegments = true;
1542 for (uint32_t k = 0; k < 2; k++) { // --------------- check if first or last segment is primary ---------------
1543 int32_t ichk = k ? lasttr : itr;
1544 const GPUTPCGMSectorTrack* trchk = &mSectorTrackInfos[ichk];
1545 while (true) {
1546 float t = -trchk->MinClusterT();
1547 if (t < mainT) {
1548 if (k) {
1549 revertSegments = false;
1550 break;
1551 }
1552 mainT = t;
1553 }
1554 int32_t next = trchk->NextSegmentNeighbour();
1555 if (next < 0 || (Param().rec.enableCyclicGraphWorkarounds && next == ichk)) {
1556 break; // Breaks also cycles
1557 }
1558 trchk = &mSectorTrackInfos[next];
1559 }
1560 }
1561 if (revertSegments) {
1562 leg = 0;
1563 }
1564
1565 { // --------------- find longest sector track of main segment ---------------
1566 int32_t length = 0;
1567 int32_t ichk = revertSegments ? itr : lasttr;
1568 const GPUTPCGMSectorTrack* trchk = &mSectorTrackInfos[ichk];
1569 const GPUTPCGMSectorTrack* longest = trchk;
1570 while (true) {
1571 if (trchk->OrigTrack()->NHits() > length) {
1572 longest = trchk;
1573 length = trchk->OrigTrack()->NHits();
1574 }
1575 int32_t next = trchk->NextSegmentNeighbour();
1576 if (next < 0 || (Param().rec.enableCyclicGraphWorkarounds && next == ichk)) {
1577 break; // Breaks also cycles
1578 }
1579 trchk = &mSectorTrackInfos[next];
1580 }
1581 revertInSegment = longest->ClusterT0() < longest->ClusterTN();
1582 }
1583 }
1584 lastMergedSegment = -1;
1585 itr += nThreads * nBlocks;
1586 }
1587
1588 do {
1589 int32_t nParts = 0;
1590 uint32_t nHits = 0;
1591
1592 GPUTPCGMSectorTrack* tr = trbase;
1593 while (true) {
1594 if (nParts >= kMaxParts) {
1595 break;
1596 }
1597 if (nHits + tr->NClusters() > kMaxClusters) {
1598 break;
1599 }
1600 nHits += tr->NClusters();
1601
1602 trackParts[nParts++] = tr;
1603 for (int32_t i = 0; i < 2; i++) {
1604 if (tr->ExtrapolatedTrackId(i) != -1) {
1605 if (nParts >= kMaxParts) {
1606 break;
1607 }
1608 if (nHits + mSectorTrackInfos[tr->ExtrapolatedTrackId(i)].NClusters() > kMaxClusters) {
1609 break;
1610 }
1611 trackParts[nParts++] = &mSectorTrackInfos[tr->ExtrapolatedTrackId(i)];
1612 nHits += mSectorTrackInfos[tr->ExtrapolatedTrackId(i)].NClusters();
1613 }
1614 }
1615 int32_t jtr = tr->NextSegmentNeighbour();
1616 if (jtr >= 0) {
1617 tr = &(mSectorTrackInfos[jtr]);
1618 if (Param().rec.enableCyclicGraphWorkarounds) {
1619 tr->SetPrevSegmentNeighbour(1000000002);
1620 }
1621 continue;
1622 }
1623 break;
1624 }
1625
1626 // unpack and sort clusters
1627 if (nParts > 1 && (!revertInSegment ^ (leg & 1))) {
1628#pragma GCC diagnostic push // TODO: Fixme, needed for bug in GCC 16
1629#pragma GCC diagnostic ignored "-Warray-bounds"
1630 GPUCommonAlgorithm::sort(trackParts, trackParts + nParts, [](const GPUTPCGMSectorTrack* a, const GPUTPCGMSectorTrack* b) {
1631#pragma GCC diagnostic pop
1632 GPUCA_DETERMINISTIC_CODE( // clang-format off
1633 if (a->X() != b->X()) {
1634 return (a->X() > b->X());
1635 }
1636 if (a->Y() != b->Y()) {
1637 return (a->Y() > b->Y());
1638 }
1639 if (a->Z() != b->Z()) {
1640 return (a->Z() > b->Z());
1641 }
1642 return a->QPt() > b->QPt();
1643 , // !GPUCA_DETERMINISTIC_CODE
1644 return (a->X() > b->X());
1645 ) // clang-format on
1646 });
1647 }
1648
1649 trackCluster trackClusters[kMaxClusters];
1650 nHits = 0;
1651 for (int32_t ipart = 0; ipart < nParts; ipart++) {
1652 const GPUTPCGMSectorTrack* t = trackParts[ipart];
1653 CADEBUG(printf("Collect Track %d Part %d QPt %f DzDs %f\n", mMemory->nMergedTracks, ipart, t->QPt(), t->DzDs()));
1654 int32_t nTrackHits = t->NClusters();
1655 trackCluster* c2 = trackClusters + nHits + nTrackHits - 1;
1656 for (int32_t i = 0; i < nTrackHits; i++, c2--) {
1657 const GPUTPCTracker& trk = GetConstantMem()->tpcTrackers[t->Sector()];
1658 const GPUTPCHitId& ic = trk.TrackHits()[t->OrigTrack()->FirstHitID() + i];
1659 uint32_t id = trk.Data().ClusterDataIndex(trk.Data().Row(ic.RowIndex()), ic.HitIndex()) + GetConstantMem()->ioPtrs.clustersNative->clusterOffset[t->Sector()][0];
1660 *c2 = trackCluster{id, (uint8_t)ic.RowIndex(), t->Sector()};
1661 }
1662 nHits += nTrackHits;
1663 }
1664 if (nHits < Param().tpcMinHitsB5(trbase->QPt() * Param().qptB5Scaler)) {
1665 break;
1666 }
1667
1668 const bool mustReverse = revertInSegment ^ (leg & 1);
1669 bool ordered = !mustReverse;
1670 if (ordered) {
1671 for (uint32_t i = 1; i < nHits; i++) {
1672 if ((trackClusters[i].row > trackClusters[i - 1].row) ^ mustReverse || trackClusters[i].id == trackClusters[i - 1].id) {
1673 ordered = false;
1674 break;
1675 }
1676 }
1677 }
1678 int32_t firstTrackIndex = 0;
1679 int32_t lastTrackIndex = nParts - 1;
1680 if (ordered == 0) {
1681 int32_t nTmpHits = 0;
1682 trackCluster trackClustersUnsorted[kMaxClusters];
1683 int16_t clusterIndices[kMaxClusters];
1684 for (uint32_t i = 0; i < nHits; i++) {
1685 trackClustersUnsorted[i] = trackClusters[i];
1686 clusterIndices[i] = i;
1687 }
1688
1689 GPUCommonAlgorithm::sort(clusterIndices, clusterIndices + nHits, GPUTPCGMMerger_CompareClusterIds(trackClusters, mustReverse));
1690
1691 nTmpHits = 0;
1692 firstTrackIndex = lastTrackIndex = -1;
1693 for (int32_t i = 0; i < nParts; i++) {
1694 nTmpHits += trackParts[i]->NClusters();
1695 if (nTmpHits > clusterIndices[0] && firstTrackIndex == -1) {
1696 firstTrackIndex = i;
1697 }
1698 if (nTmpHits > clusterIndices[nHits - 1] && lastTrackIndex == -1) {
1699 lastTrackIndex = i;
1700 }
1701 }
1702
1703 int32_t nFilteredHits = 0;
1704 int32_t indPrev = -1;
1705 for (uint32_t i = 0; i < nHits; i++) {
1706 int32_t ind = clusterIndices[i];
1707 if (indPrev >= 0 && trackClustersUnsorted[ind].id == trackClustersUnsorted[indPrev].id) {
1708 continue;
1709 }
1710 indPrev = ind;
1711 trackClusters[nFilteredHits] = trackClustersUnsorted[ind];
1712 nFilteredHits++;
1713 }
1714 nHits = nFilteredHits;
1715 }
1716
1717 const uint32_t iMergedTrackFirstCluster = CAMath::AtomicAdd(&mMemory->nMergedTrackClusters, (uint32_t)nHits);
1718 if (iMergedTrackFirstCluster + nHits > mNMaxMergedTrackClusters) {
1719 raiseError(GPUErrors::ERROR_MERGER_HIT_OVERFLOW, iMergedTrackFirstCluster, mNMaxMergedTrackClusters);
1720 CAMath::AtomicExch(&mMemory->nMergedTrackClusters, mNMaxMergedTrackClusters);
1721 break;
1722 }
1723
1724 GPUTPCGMMergedTrackHit* const cl = mClusters + iMergedTrackFirstCluster;
1725
1726 for (uint32_t i = 0; i < nHits; i++) {
1727 uint8_t state;
1728 const ClusterNative& c = GetConstantMem()->ioPtrs.clustersNative->clustersLinear[trackClusters[i].id];
1729 state = c.getFlags();
1730 cl[i].state = state & GPUTPCGMMergedTrackHit::clustererAndSharedFlags; // Only allow edge, deconvoluted, and shared flags
1731 cl[i].row = trackClusters[i].row;
1732 cl[i].num = trackClusters[i].id;
1733 cl[i].sector = trackClusters[i].sector;
1734 }
1735
1736 uint32_t iOutputTrack = CAMath::AtomicAdd(&mMemory->nMergedTracks, 1u);
1737 if (iOutputTrack >= mNMaxTracks) {
1738 raiseError(GPUErrors::ERROR_MERGER_TRACK_OVERFLOW, iOutputTrack, mNMaxTracks);
1739 CAMath::AtomicExch(&mMemory->nMergedTracks, mNMaxTracks);
1740 break;
1741 }
1742
1743 GPUTPCGMMergedTrack& mergedTrack = mMergedTracks[iOutputTrack];
1744 GPUTPCGMTrackParam& p1 = mergedTrack.Param();
1745 const GPUTPCGMSectorTrack& p2 = *trackParts[firstTrackIndex];
1746 mergedTrack.SetFlags(0);
1747 mergedTrack.SetOK(true);
1748 mergedTrack.SetLeg(leg);
1749 mergedTrack.SetLooper(leg > 0);
1750 mergedTrack.SetNClusters(nHits);
1751 mergedTrack.SetFirstClusterRef(iMergedTrackFirstCluster);
1752 mergedTrack.SetCSide(p2.CSide());
1753 mergedTrack.SetMergedLooperConnected(leg > 0);
1754 if (revertSegments) {
1755 mergedTrack.SetPrevSegment(-1);
1756 if (lastMergedSegment >= 0) {
1757 mMergedTracks[lastMergedSegment].SetPrevSegment(iOutputTrack);
1758 }
1759 } else {
1760 mergedTrack.SetPrevSegment(lastMergedSegment);
1761 }
1762 lastMergedSegment = iOutputTrack;
1763
1765 const float toX = GPUTPCGeometry::Row2X(cl[0].row);
1766 if (p2.TransportToX<2>(this, toX, Param().bzCLight, b, constants::MAX_SIN_PHI, false)) {
1767 p1.X() = toX;
1768 p1.Y() = b.Par()[0];
1769 p1.Z() = b.Par()[1];
1770 p1.SinPhi() = b.Par()[2];
1771 } else {
1772 p1.X() = p2.X();
1773 p1.Y() = p2.Y();
1774 p1.Z() = p2.Z();
1775 p1.SinPhi() = p2.SinPhi();
1776 }
1777 p1.TOffset() = p2.TOffset();
1778 p1.DzDs() = p2.DzDs();
1779 p1.QPt() = p2.QPt();
1780 mergedTrack.SetAlpha(p2.Alpha());
1781 if (CAMath::Abs(Param().polynomialField.GetNominalBz()) < (gpu_common_constants::kZeroFieldCut * gpu_common_constants::kCLight)) {
1782 p1.QPt() = 100.f / Param().rec.bz0Pt10MeV;
1783 }
1784
1785 // if (nParts > 1) printf("Merged %d: QPt %f %d parts %d hits\n", mMemory->nMergedTracks, p1.QPt(), nParts, nHits);
1786
1787 /*if (GPUQA::QAAvailable() && mRec->GetQA() && mRec->GetQA()->SuppressTrack(mMemory->nMergedTracks))
1788 {
1789 mergedTrack.SetOK(0);
1790 mergedTrack.SetNClusters(0);
1791 }
1792 if (mergedTrack.NClusters() && mergedTrack.OK()) */
1793 if (leg == 0 && Param().rec.tpc.mergeCE) {
1794 auto& cls = mConstantMem->ioPtrs.clustersNative->clustersLinear;
1795 bool CEside = cls[cl[0].num].getTime() < cls[cl[nHits - 1].num].getTime();
1796 MergeCEFill(trackParts[CEside ? lastTrackIndex : firstTrackIndex], cl[CEside ? (nHits - 1) : 0], iOutputTrack);
1797 }
1798 } while (false);
1799 }
1800}
1801
1802GPUd() void GPUTPCGMMerger::SortTracksPrepare(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1803{
1804 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nThreads * nBlocks) {
1805 mTrackOrderProcess[i] = i;
1806 }
1807}
1808
1809GPUd() void GPUTPCGMMerger::PrepareForFit0(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1810{
1811 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nBlocks * nThreads) {
1812 mTrackSort[i] = i;
1813 }
1814}
1815
1816GPUd() void GPUTPCGMMerger::SortTracks(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1817{
1818#ifndef GPUCA_SPECIALIZE_THRUST_SORTS
1819 if (iThread == 0 && iBlock == 0) {
1820 GPUCommonAlgorithm::sortDeviceDynamic(mTrackOrderProcess, mTrackOrderProcess + mMemory->nMergedTracks, GPUTPCGMMergerSortTracks_comp(mMergedTracks));
1821 }
1822#endif
1823}
1824
1825GPUd() void GPUTPCGMMerger::SortTracksQPt(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1826{
1827#ifndef GPUCA_SPECIALIZE_THRUST_SORTS
1828 if (iThread == 0 && iBlock == 0) {
1829 GPUCommonAlgorithm::sortDeviceDynamic(mTrackSort, mTrackSort + mMemory->nMergedTracks, GPUTPCGMMergerSortTracksQPt_comp(mMergedTracks));
1830 }
1831#endif
1832}
1833
1834GPUd() void GPUTPCGMMerger::PrepareForFit1(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1835{
1836 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nBlocks * nThreads) {
1837 mTrackOrderAttach[mTrackSort[i]] = i;
1838 GPUTPCGMMergedTrack& trk = mMergedTracks[i];
1839 if (trk.OK()) {
1840 for (uint32_t j = 0; j < trk.NClusters(); j++) {
1841 uint32_t weight = attachAttached | attachGood;
1842 if (CAMath::Abs(trk.GetParam().GetQPt() * Param().qptB5Scaler) <= Param().rec.tpc.rejectQPtB5 && !trk.MergedLooper() && trk.Leg() == 0) {
1844 }
1845 CAMath::AtomicMax(&mClusterAttachment[mClusters[trk.FirstClusterRef() + j].num], weight);
1846 CAMath::AtomicAdd(&mSharedCount[mClusters[trk.FirstClusterRef() + j].num], 1u);
1847 }
1848 if (!trk.CCE() && !trk.MergedLooper()) {
1849 GPUTPCGMMergedTrack* updTrk = trk.GetFirstSegment(mMergedTracks, Param().rec.enableCyclicGraphWorkarounds);
1850 const auto &cl0 = mClusters[trk.FirstClusterRef()], &cln = mClusters[updTrk->FirstClusterRef() + updTrk->NClusters() - 1];
1851 const auto& GPUrestrict() cls = GetConstantMem()->ioPtrs.clustersNative->clustersLinear;
1852 float z0 = cls[cl0.num].getTime(), zn = cls[cln.num].getTime();
1853 const auto tmp = zn > z0 ? std::array<float, 3>{zn, z0, GPUTPCGeometry::Row2X(cln.row)} : std::array<float, 3>{z0, zn, GPUTPCGeometry::Row2X(cl0.row)};
1854 trk.Param().ShiftZ(this, cl0.sector, tmp[0], tmp[1], tmp[2]);
1855 updTrk = &trk;
1856 while (updTrk->PrevSegment() >= 0) {
1857 auto next = &mMergedTracks[updTrk->PrevSegment()];
1858 if (Param().rec.enableCyclicGraphWorkarounds && next == &trk) {
1859 break;
1860 }
1861 updTrk = next;
1862 updTrk->Param().TOffset() = trk.Param().TOffset();
1863 }
1864 }
1865 }
1866 }
1867}
1868
1869GPUd() void GPUTPCGMMerger::PrepareForFit2(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1870{
1871 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTrackClusters; i += nBlocks * nThreads) {
1872 if (mSharedCount[mClusters[i].num] > 1) {
1873 mClusters[i].state |= GPUTPCGMMergedTrackHit::flagShared;
1874 }
1875 }
1876 if (mClusterStateExt) {
1877 for (uint32_t i = iBlock * nThreads + iThread; i < mNMaxClusters; i += nBlocks * nThreads) {
1878 uint8_t state = GetConstantMem()->ioPtrs.clustersNative->clustersLinear[i].getFlags();
1879 if (mSharedCount[i] > 1) {
1881 }
1882 mClusterStateExt[i] = state;
1883 }
1884 }
1885}
1886
1887GPUd() void GPUTPCGMMerger::Finalize0(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1888{
1889 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nThreads * nBlocks) {
1890 mTrackSort[mTrackOrderAttach[i]] = i;
1891 }
1892 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTrackClusters; i += nThreads * nBlocks) {
1893 if (!(mClusterAttachment[mClusters[i].num] & attachProtect)) {
1894 mClusterAttachment[mClusters[i].num] = 0; // Reset adjacent attachment for attached clusters, set correctly below
1895 }
1896 }
1897}
1898
1899GPUd() void GPUTPCGMMerger::Finalize1(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1900{
1901 for (uint32_t i = iBlock * nThreads + iThread; i < mMemory->nMergedTracks; i += nThreads * nBlocks) {
1902 const GPUTPCGMMergedTrack& trk = mMergedTracks[i];
1903 if (!trk.OK() || trk.NClusters() == 0) {
1904 continue;
1905 }
1906 for (uint32_t j = 0; j < trk.NClusters(); j++) {
1907 int32_t id = mClusters[trk.FirstClusterRef() + j].num;
1908 uint32_t weight = mTrackOrderAttach[i] | attachAttached;
1909 uint8_t clusterState = mClusters[trk.FirstClusterRef() + j].state;
1910 if (!(clusterState & GPUTPCGMMergedTrackHit::flagReject)) {
1911 weight |= attachGood;
1912 } else if (clusterState & GPUTPCGMMergedTrackHit::flagHighIncl) {
1914 }
1915 if (trk.Leg() == 0) {
1917 }
1918 if (CAMath::Abs(trk.GetParam().GetQPt() * Param().qptB5Scaler) <= Param().rec.tpc.rejectQPtB5 && !trk.MergedLooper() && trk.Leg() == 0) {
1920 }
1921 CAMath::AtomicMax(&mClusterAttachment[id], weight);
1922 }
1923 }
1924}
1925
1926GPUd() void GPUTPCGMMerger::Finalize2(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1927{
1928 for (uint32_t i = iBlock * nThreads + iThread; i < mNMaxClusters; i += nThreads * nBlocks) {
1929 if (mClusterAttachment[i] != 0) {
1930 mClusterAttachment[i] = (mClusterAttachment[i] & attachFlagMask) | mTrackSort[mClusterAttachment[i] & attachTrackMask];
1931 }
1932 }
1933}
1934
1935GPUd() void GPUTPCGMMerger::MergeLoopersInit(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1936{
1937 const float lowPtThresh = Param().rec.tpc.rejectQPtB5 * 1.1f; // Might need to merge tracks above the threshold with parts below the rejection threshold
1938 for (uint32_t i = get_global_id(0); i < mMemory->nMergedTracks; i += get_global_size(0)) {
1939 const auto& trk = mMergedTracks[i];
1940 const auto& p = trk.GetParam();
1941 const float qptabs = CAMath::Abs(p.GetQPt());
1942 if (trk.OK() && trk.NClusters() && trk.Leg() == 0 && qptabs * Param().qptB5Scaler > 5.f && qptabs * Param().qptB5Scaler <= lowPtThresh) {
1943 const int32_t sector = mClusters[trk.FirstClusterRef() + trk.NClusters() - 1].sector;
1944 const float refz = p.GetZ() + (Param().par.continuousTracking ? GetConstantMem()->calibObjects.fastTransform->convVertexTimeToZOffset(sector, p.GetTOffset(), Param().continuousMaxTimeBin) : 0) + (trk.CSide() ? -100 : 100);
1945 float sinA, cosA;
1946 CAMath::SinCos(trk.GetAlpha(), sinA, cosA);
1947 float gx = cosA * p.GetX() - sinA * p.GetY();
1948 float gy = cosA * p.GetY() + sinA * p.GetX();
1949 float bz = Param().polynomialField.GetFieldBz(gx, gy, p.GetZ());
1950 const float r1 = p.GetQPt() * bz;
1951 const float r = CAMath::Abs(r1) > 0.0001f ? (1.f / r1) : 10000;
1952 const float mx = p.GetX() + r * p.GetSinPhi();
1953 const float my = p.GetY() - r * CAMath::Sqrt(1 - p.GetSinPhi() * p.GetSinPhi());
1954 const float gmx = cosA * mx - sinA * my;
1955 const float gmy = cosA * my + sinA * mx;
1956 uint32_t myId = CAMath::AtomicAdd(&mMemory->nLooperMatchCandidates, 1u);
1957 if (myId >= mNMaxLooperMatches) {
1958 raiseError(GPUErrors::ERROR_LOOPER_MATCH_OVERFLOW, myId, mNMaxLooperMatches);
1959 CAMath::AtomicExch(&mMemory->nLooperMatchCandidates, mNMaxLooperMatches);
1960 return;
1961 }
1962 mLooperCandidates[myId] = MergeLooperParam{refz, gmx, gmy, i};
1963
1964 /*printf("Track %u Sanity qpt %f snp %f bz %f\n", mMemory->nLooperMatchCandidates, p.GetQPt(), p.GetSinPhi(), bz);
1965 for (uint32_t k = 0;k < trk.NClusters();k++) {
1966 float xx, yy, zz;
1967 const ClusterNative& GPUrestrict() cl = GetConstantMem()->ioPtrs.clustersNative->clustersLinear[mClusters[trk.FirstClusterRef() + k].num];
1968 GetConstantMem()->calibObjects.fastTransformHelper->Transform(mClusters[trk.FirstClusterRef() + k].sector, mClusters[trk.FirstClusterRef() + k].row, cl.getPad(), cl.getTime(), xx, yy, zz, p.GetTOffset());
1969 float sa2, ca2;
1970 CAMath::SinCos(Param().Alpha(mClusters[trk.FirstClusterRef() + k].sector), sa2, ca2);
1971 float cx = ca2 * xx - sa2 * yy;
1972 float cy = ca2 * yy + sa2 * xx;
1973 float dist = CAMath::Sqrt((cx - gmx) * (cx - gmx) + (cy - gmy) * (cy - gmy));
1974 printf("Hit %3d/%3d sector %d xy %f %f R %f\n", k, trk.NClusters(), (int32_t)mClusters[trk.FirstClusterRef() + k].sector, cx, cy, dist);
1975 }*/
1976 }
1977 }
1978}
1979
1980GPUd() void GPUTPCGMMerger::MergeLoopersSort(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1981{
1982#ifndef GPUCA_SPECIALIZE_THRUST_SORTS
1983 if (iThread == 0 && iBlock == 0) {
1984 GPUCommonAlgorithm::sortDeviceDynamic(mLooperCandidates, mLooperCandidates + mMemory->nLooperMatchCandidates, GPUTPCGMMergerMergeLoopers_comp());
1985 }
1986#endif
1987}
1988
1989GPUd() void GPUTPCGMMerger::MergeLoopersMain(int32_t nBlocks, int32_t nThreads, int32_t iBlock, int32_t iThread)
1990{
1991 const MergeLooperParam* candidates = mLooperCandidates;
1992
1993#if GPUCA_MERGE_LOOPER_MC && !defined(GPUCA_GPUCODE)
1994 std::vector<int64_t> paramLabels(mMemory->nLooperMatchCandidates);
1995 for (uint32_t i = 0; i < mMemory->nLooperMatchCandidates; i++) {
1996 paramLabels[i] = GetTrackLabel(mMergedTracks[candidates[i].id]);
1997 }
1998 /*std::vector<bool> dropped(mMemory->nLooperMatchCandidates);
1999 std::vector<bool> droppedMC(mMemory->nLooperMatchCandidates);
2000 std::vector<int32_t> histMatch(101);
2001 std::vector<int32_t> histFail(101);*/
2002 if (!mRec->GetProcessingSettings().runQA) {
2003 throw std::runtime_error("Need QA enabled for the Merge Loopers MC QA");
2004 }
2005#endif
2006
2007 for (uint32_t i = get_global_id(0); i < mMemory->nLooperMatchCandidates; i += get_global_size(0)) {
2008 for (uint32_t j = i + 1; j < mMemory->nLooperMatchCandidates; j++) {
2009 // int32_t bs = 0;
2010 assert(CAMath::Abs(candidates[i].refz) <= CAMath::Abs(candidates[j].refz));
2011 if (CAMath::Abs(candidates[j].refz) > CAMath::Abs(candidates[i].refz) + 100.f) {
2012 break;
2013 }
2014 const float d2xy = CAMath::Sum2(candidates[i].x - candidates[j].x, candidates[i].y - candidates[j].y);
2015 if (d2xy > 15.f) {
2016 // bs |= 1;
2017 continue;
2018 }
2019
2020 const GPUTPCGMMergedTrack* trkI = &mMergedTracks[candidates[i].id];
2021 float refZI = candidates[i].refz;
2022 {
2023 const auto* tmp = trkI->GetFirstSegment(mMergedTracks, Param().rec.enableCyclicGraphWorkarounds);
2024 if (tmp != trkI && tmp->CSide() == trkI->CSide() && CAMath::Abs(tmp->GetParam().GetZ()) > CAMath::Abs(trkI->GetParam().GetZ())) {
2025 float tmpRefZ = refZI + tmp->GetParam().GetZ() - trkI->GetParam().GetZ();
2026 if (CAMath::Abs(tmpRefZ) < CAMath::Abs(candidates[j].refz) && CAMath::Abs(tmpRefZ) > CAMath::Abs(refZI)) {
2027 trkI = tmp;
2028 refZI = tmpRefZ;
2029 }
2030 }
2031 };
2032 const auto& trk1 = *trkI;
2033 const auto& trk2 = mMergedTracks[candidates[j].id];
2034 const auto& param1 = trk1.GetParam();
2035 const auto& param2 = trk2.GetParam();
2036 if (CAMath::Abs(param1.GetDzDs()) > 0.03f && CAMath::Abs(param2.GetDzDs()) > 0.03f && param1.GetDzDs() * param2.GetDzDs() * param1.GetQPt() * param2.GetQPt() < 0) {
2037 // bs |= 2;
2038 continue;
2039 }
2040
2041 const float dznormalized = (CAMath::Abs(candidates[j].refz) - CAMath::Abs(refZI)) / (CAMath::TwoPi() * 0.5f * (CAMath::Abs(param1.GetDzDs()) + CAMath::Abs(param2.GetDzDs())) * 1.f / (0.5f * (CAMath::Abs(param1.GetQPt()) + CAMath::Abs(param2.GetQPt())) * CAMath::Abs(Param().polynomialField.GetNominalBz())));
2042 const float phasecorr = CAMath::Modf((CAMath::ASin(param1.GetSinPhi()) + trk1.GetAlpha() - CAMath::ASin(param2.GetSinPhi()) - trk2.GetAlpha()) / CAMath::TwoPi() + 5.5f, 1.f) - 0.5f;
2043 const float phasecorrdirection = (candidates[j].refz * param1.GetQPt() * param1.GetDzDs()) > 0 ? 1 : -1;
2044 const float dzcorr = dznormalized + phasecorr * phasecorrdirection;
2045 const bool sameside = !(trk1.CSide() ^ trk2.CSide());
2046 const float dzcorrlimit[4] = {sameside ? 0.018f : 0.012f, sameside ? 0.12f : 0.025f, 0.14f, 0.15f};
2047 const int32_t dzcorrcount = sameside ? 4 : 2;
2048 bool dzcorrok = false;
2049 float dznorm = 0.f;
2050 for (int32_t k = 0; k < dzcorrcount; k++) {
2051 const float d = CAMath::Abs(dzcorr - 0.5f * k);
2052 if (d <= dzcorrlimit[k]) {
2053 dzcorrok = true;
2054 dznorm = d / dzcorrlimit[k];
2055 break;
2056 }
2057 }
2058 if (!dzcorrok) {
2059 // bs |= 4;
2060 continue;
2061 }
2062
2063 const float dtgl = param1.GetDzDs() - (param1.GetQPt() * param2.GetQPt() > 0 ? param2.GetDzDs() : -param2.GetDzDs());
2064 const float dqpt = (CAMath::Abs(param1.GetQPt()) - CAMath::Abs(param2.GetQPt())) / CAMath::Min(param1.GetQPt(), param2.GetQPt());
2065 float d = CAMath::Sum2(dtgl * (1.f / 0.03f), dqpt * (1.f / 0.04f)) + d2xy * (1.f / 4.f) + dznorm * (1.f / 0.3f);
2066 bool EQ = d < 6.f;
2067#if GPUCA_MERGE_LOOPER_MC && !defined(GPUCA_GPUCODE)
2068 const int64_t label1 = paramLabels[i];
2069 const int64_t label2 = paramLabels[j];
2070 bool labelEQ = label1 != -1 && label1 == label2;
2071 if (1 || EQ || labelEQ) {
2072 // printf("Matching track %d/%d %u-%u (%ld/%ld): dist %f side %d %d, tgl %f %f, qpt %f %f, x %f %f, y %f %f\n", (int32_t)EQ, (int32_t)labelEQ, i, j, label1, label2, d, (int32_t)mMergedTracks[candidates[i].id].CSide(), (int32_t)mMergedTracks[candidates[j].id].CSide(), candidates[i].tgl, candidates[j].tgl, candidates[i].qpt, candidates[j].qpt, candidates[i].x, candidates[j].x, candidates[i].y, candidates[j].y);
2073 static auto& tup = GPUROOTDump<TNtuple>::get("mergeloopers", "labeleq:sides:d2xy:tgl1:tgl2:qpt1:qpt2:dz:dzcorr:dtgl:dqpt:dznorm:bs");
2074 tup.Fill((float)labelEQ, (trk1.CSide() ? 1 : 0) | (trk2.CSide() ? 2 : 0), d2xy, param1.GetDzDs(), param2.GetDzDs(), param1.GetQPt(), param2.GetQPt(), CAMath::Abs(candidates[j].refz) - CAMath::Abs(refZI), dzcorr, dtgl, dqpt, dznorm, bs);
2075 static auto tup2 = GPUROOTDump<TNtuple>::getNew("mergeloopers2", "labeleq:refz1:refz2:tgl1:tgl2:qpt1:qpt2:snp1:snp2:a1:a2:dzn:phasecor:phasedir:dzcorr");
2076 tup2.Fill((float)labelEQ, refZI, candidates[j].refz, param1.GetDzDs(), param2.GetDzDs(), param1.GetQPt(), param2.GetQPt(), param1.GetSinPhi(), param2.GetSinPhi(), trk1.GetAlpha(), trk2.GetAlpha(), dznormalized, phasecorr, phasecorrdirection, dzcorr);
2077 }
2078 /*if (EQ) {
2079 dropped[j] = true;
2080 }
2081 if (labelEQ) {
2082 droppedMC[j] = true;
2083 histMatch[CAMath::Min<int32_t>(100, d * 10.f)]++;
2084 }
2085 if (d < 10.f && !labelEQ) {
2086 histFail[CAMath::Min<int32_t>(100, d * 10.f)]++;
2087 }*/
2088#endif
2089 if (EQ) {
2090 mMergedTracks[candidates[j].id].SetMergedLooperUnconnected(true);
2091 if (CAMath::Abs(param2.GetQPt() * Param().qptB5Scaler) >= Param().rec.tpc.rejectQPtB5) {
2092 mMergedTracks[candidates[i].id].SetMergedLooperUnconnected(true);
2093 }
2094 }
2095 }
2096 }
2097 /*#if GPUCA_MERGE_LOOPER_MC && !defined(GPUCA_GPUCODE)
2098 int32_t total = 0, totalmc = 0, good = 0, missed = 0, fake = 0;
2099 for (uint32_t i = 0; i < mMemory->nLooperMatchCandidates; i++) {
2100 total += dropped[i];
2101 totalmc += droppedMC[i];
2102 good += dropped[i] && droppedMC[i];
2103 missed += droppedMC[i] && !dropped[i];
2104 fake += dropped[i] && !droppedMC[i];
2105 }
2106 if (good) {
2107 printf("%20s: %8d\n", "Total", total);
2108 printf("%20s: %8d\n", "TotalMC", totalmc);
2109 printf("%20s: %8d (%8.3f%% %8.3f%%)\n", "Good", good, 100.f * good / total, 100.f * good / totalmc);
2110 printf("%20s: %8d (%8.3f%%)\n", "Missed", missed, 100.f * missed / totalmc);
2111 printf("%20s: %8d (%8.3f%%)\n", "Fake", fake, 100.f * fake / total);
2112 }
2113 printf("Match histo\n");
2114 for (uint32_t i = 0; i < histMatch.size(); i++) {
2115 printf("%8.3f: %3d\n", i / 10.f + 0.05f, histMatch[i]);
2116 }
2117 printf("Fake histo\n");
2118 for (uint32_t i = 0; i < histFail.size(); i++) {
2119 printf("%8.3f: %3d\n", i / 10.f + 0.05f, histFail[i]);
2120 }
2121#endif*/
2122}
Class of a TPC cluster in TPC-native coordinates (row, time)
A const (ready only) version of MCTruthContainer.
int32_t i
#define GPUdii()
#define get_global_size(dim)
#define GPUAtomic(type)
#define GPUbarrier()
#define GPUdni()
#define GPUrestrict()
#define get_global_id(dim)
#define GPUCA_DEBUG_STREAMER_CHECK(...)
#define GPUCA_DETERMINISTIC_CODE(det, indet)
#define CADEBUG(...)
Definition GPUDef.h:82
#define CADEBUG2(cmd,...)
Definition GPUDef.h:83
uint32_t iSector
const GPUTPCGMMerger::trackCluster *const mCmp
const int16_t bb
const bool revert
constexpr int p2()
constexpr int p1()
constexpr to accelerate the coordinates changing
SurfaceTrackState state
std::vector< int > clusterIndices
std::vector< o2::its::TrackITS > tracks
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
uint16_t pos
Definition RawData.h:3
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
POD correction map.
int nClusters
double num
Definition B.h:16
Class for time synchronization of RawReader instances.
GPUReconstruction * mRec
static void computePointerWithAlignment(T *&basePtr, S *&objPtr, size_t nEntries=1)
RecoStepField GetRecoStepsGPU() const
int16_t RegisterMemoryAllocation(T *proc, void *(T::*setPtr)(void *), int32_t type, const char *name="", const GPUMemoryReuse &re=GPUMemoryReuse())
RecoStepField GetRecoSteps() const
const GPUParam & GetParam() const
const GPUConstantMem & GetConstantMem() const
GPUMemorySizeScalers * MemoryScalers()
const GPUSettingsProcessing & GetProcessingSettings() const
virtual const GPUDefParameters & getGPUParameters(bool doGPU) const =0
void * SetPointersRefitScratch(void *mem)
void * SetPointersMemory(void *mem)
void * SetPointersOutput(void *mem)
void * SetPointersOutputO2MC(void *mem)
void * SetPointersOutputO2Scratch(void *mem)
void * SetPointersMerger(void *mem)
void * SetPointersOutputO2Clus(void *mem)
void * SetPointersOutputState(void *mem)
void SetMaxData(const GPUTrackingInOutPointers &io)
void * SetPointersOutputO2(void *mem)
int32_t int32_t int32_t bool float float Z
int32_t int32_t int32_t bool float Y
GLint GLint GLsizei GLint border
Definition glcorearb.h:275
GLdouble n
Definition glcorearb.h:1982
GLfloat GLfloat GLfloat alpha
Definition glcorearb.h:279
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint GLuint end
Definition glcorearb.h:469
const GLdouble * v
Definition glcorearb.h:832
GLenum array
Definition glcorearb.h:4274
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLenum GLint * range
Definition glcorearb.h:1899
GLint y
Definition glcorearb.h:270
GLenum target
Definition glcorearb.h:1641
GLintptr offset
Definition glcorearb.h:660
GLuint GLsizei GLsizei * length
Definition glcorearb.h:790
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint GLfloat x0
Definition glcorearb.h:5034
GLbitfield flags
Definition glcorearb.h:1570
GLboolean r
Definition glcorearb.h:1233
GLuint start
Definition glcorearb.h:469
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLuint id
Definition glcorearb.h:650
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
uint8_t itsSharedClusterMap uint8_t
GPUdoubleBinary64 b
GPUdi() o2
Definition TrackTRD.h:39
GPUd() const expr uint32_t MultivariatePolynomialHelper< Dim
const bool const int TrackITSInternal< NLayers > & track
o2::track::TrackParametrization< float > par
const float const float const float bz
if(!okForPhiMin(phi0, phi1))
Global TPC definitions and constants.
Definition SimTraits.h:172
@ streamMergeBorderTracksBest
stream MergeBorderTracks best track
@ streamMergeBorderTracksAll
stream MergeBorderTracks all tracks
std::string getTime(uint64_t ts)
GPUReconstruction * rec
GPUTPCTracker tpcTrackers[GPUTPCGeometry::NSECTORS]
size_t NTPCMergedTracks(size_t tpcSectorTracks)
size_t NTPCMergedTrackHits(size_t tpcSectorTrackHitss)
size_t getValue(size_t maxVal, size_t val)
const o2::tpc::ClusterNativeAccess * clustersNative
const GPUTPCGMMergedTrack & GPUrestrict() b
GPUd() bool operator()(const int32_t aa
GPUhd() GPUTPCGMMergerSortTracksQPt_comp(GPUTPCGMMergedTrack *cmp)
const GPUTPCGMMergedTrack & GPUrestrict() b
GPUhd() GPUTPCGMMergerSortTracks_comp(GPUTPCGMMergedTrack *cmp)
GPUd() bool operator()(const int32_t aa
std::vector< int > row
char const *restrict const cmp
Definition x9.h:96