Project
Loading...
Searching...
No Matches
Tracker.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.
15
16#include "ITStracking/Tracker.h"
17
19#include "ITStracking/Cell.h"
26
28#include <cassert>
29#include <format>
30#include <cstdlib>
31#include <string>
32#include <climits>
33
34namespace o2::its
35{
37
38Tracker::Tracker(TrackerTraits7* traits) : mTraits(traits)
39{
41 mTrkParams.resize(1);
42}
43
44void Tracker::clustersToTracks(const LogFunc& logger, const LogFunc& error)
45{
46 LogFunc evalLog = [](const std::string&) {};
47
48 double total{0};
49 mTraits->updateTrackingParameters(mTrkParams);
50 int maxNvertices{-1};
51 if (mTrkParams[0].PerPrimaryVertexProcessing) {
52 for (int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
53 maxNvertices = std::max(maxNvertices, (int)mTimeFrame->getPrimaryVertices(iROF).size());
54 }
55 }
56
57 int iteration{0}, iROFs{0}, iVertex{0};
58 auto handleException = [&](const auto& err) {
59 LOGP(error, "Too much memory used during {} in iteration {} in ROF span {}-{} iVtx={}: {:.2f} GB. Current limit is {:.2f} GB, check the detector status and/or the selections.",
60 StateNames[mCurState], iteration, iROFs, iROFs + mTrkParams[iteration].nROFsPerIterations, iVertex,
61 (double)mTimeFrame->getArtefactsMemory() / GB, (double)mTrkParams[iteration].MaxMemory / GB);
62 LOGP(error, "Exception: {}", err.what());
63 if (mTrkParams[iteration].DropTFUponFailure) {
64 mTimeFrame->wipe();
65 mMemoryPool->print();
66 ++mNumberOfDroppedTFs;
67 error("...Dropping Timeframe...");
68 } else {
69 throw err;
70 }
71 };
72
73 try {
74 for (iteration = 0; iteration < (int)mTrkParams.size(); ++iteration) {
75 mMemoryPool->setMaxMemory(mTrkParams[iteration].MaxMemory);
76 if (iteration == 3 && mTrkParams[0].DoUPCIteration) {
77 mTimeFrame->swapMasks();
78 }
79 double timeTracklets{0.}, timeCells{0.}, timeNeighbours{0.}, timeRoads{0.};
80 int nTracklets{0}, nCells{0}, nNeighbours{0}, nTracks{-static_cast<int>(mTimeFrame->getNumberOfTracks())};
81 int nROFsIterations = mTrkParams[iteration].nROFsPerIterations > 0 ? mTimeFrame->getNrof() / mTrkParams[iteration].nROFsPerIterations + bool(mTimeFrame->getNrof() % mTrkParams[iteration].nROFsPerIterations) : 1;
82 iVertex = std::min(maxNvertices, 0);
83 logger(std::format("==== ITS {} Tracking iteration {} summary ====", mTraits->getName(), iteration));
84
85 total += evaluateTask(&Tracker::initialiseTimeFrame, StateNames[mCurState = TFInit], iteration, logger, iteration);
86 do {
87 for (iROFs = 0; iROFs < nROFsIterations; ++iROFs) {
88 timeTracklets += evaluateTask(&Tracker::computeTracklets, StateNames[mCurState = Trackleting], iteration, evalLog, iteration, iROFs, iVertex);
89 nTracklets += mTraits->getTFNumberOfTracklets();
90 float trackletsPerCluster = mTraits->getTFNumberOfClusters() > 0 ? float(mTraits->getTFNumberOfTracklets()) / float(mTraits->getTFNumberOfClusters()) : 0.f;
91 if (trackletsPerCluster > mTrkParams[iteration].TrackletsPerClusterLimit) {
92 error(std::format("Too many tracklets per cluster ({}) in iteration {} in ROF span {}-{}:, check the detector status and/or the selections. Current limit is {}",
93 trackletsPerCluster, iteration, iROFs, iROFs + mTrkParams[iteration].nROFsPerIterations, mTrkParams[iteration].TrackletsPerClusterLimit));
94 break;
95 }
96 timeCells += evaluateTask(&Tracker::computeCells, StateNames[mCurState = Celling], iteration, evalLog, iteration);
97 nCells += mTraits->getTFNumberOfCells();
98 float cellsPerCluster = mTraits->getTFNumberOfClusters() > 0 ? float(mTraits->getTFNumberOfCells()) / float(mTraits->getTFNumberOfClusters()) : 0.f;
99 if (cellsPerCluster > mTrkParams[iteration].CellsPerClusterLimit) {
100 error(std::format("Too many cells per cluster ({}) in iteration {} in ROF span {}-{}, check the detector status and/or the selections. Current limit is {}",
101 cellsPerCluster, iteration, iROFs, iROFs + mTrkParams[iteration].nROFsPerIterations, mTrkParams[iteration].CellsPerClusterLimit));
102 break;
103 }
104 timeNeighbours += evaluateTask(&Tracker::findCellsNeighbours, StateNames[mCurState = Neighbouring], iteration, evalLog, iteration);
105 nNeighbours += mTimeFrame->getNumberOfNeighbours();
106 timeRoads += evaluateTask(&Tracker::findRoads, StateNames[mCurState = Roading], iteration, evalLog, iteration);
107 }
108 } while (++iVertex < maxNvertices);
109 logger(std::format(" - Tracklet finding: {} tracklets found in {:.2f} ms", nTracklets, timeTracklets));
110 logger(std::format(" - Cell finding: {} cells found in {:.2f} ms", nCells, timeCells));
111 logger(std::format(" - Neighbours finding: {} neighbours found in {:.2f} ms", nNeighbours, timeNeighbours));
112 logger(std::format(" - Track finding: {} tracks found in {:.2f} ms", nTracks + mTimeFrame->getNumberOfTracks(), timeRoads));
113 total += timeTracklets + timeCells + timeNeighbours + timeRoads;
114 if (mTraits->supportsExtendTracks() && mTrkParams[iteration].UseTrackFollower) {
115 int nExtendedTracks{-mTimeFrame->mNExtendedTracks}, nExtendedClusters{-mTimeFrame->mNExtendedUsedClusters};
116 auto timeExtending = evaluateTask(&Tracker::extendTracks, "Extending tracks", iteration, evalLog, iteration);
117 total += timeExtending;
118 logger(std::format(" - Extending Tracks: {} extended tracks using {} clusters found in {:.2f} ms", nExtendedTracks + mTimeFrame->mNExtendedTracks, nExtendedClusters + mTimeFrame->mNExtendedUsedClusters, timeExtending));
119 }
120 if (mTrkParams[iteration].PrintMemory) {
121 mMemoryPool->print();
122 }
123 }
124 if (mTraits->supportsFindShortPrimaries() && mTrkParams[0].FindShortTracks) {
125 auto nTracksB = mTimeFrame->getNumberOfTracks();
126 total += evaluateTask(&Tracker::findShortPrimaries, "Short primaries finding", 0, logger);
127 auto nTracksA = mTimeFrame->getNumberOfTracks();
128 logger(std::format(" `-> found {} additional tracks", nTracksA - nTracksB));
129 }
130 if (mTrkParams[iteration].PrintMemory) {
131 mMemoryPool->print();
132 }
133 if constexpr (constants::DoTimeBenchmarks) {
134 logger(std::format("=== TimeFrame {} processing completed in: {:.2f} ms using {} thread(s) ===", mTimeFrameCounter, total, mTraits->getNThreads()));
135 }
136 } catch (const BoundedMemoryResource::MemoryLimitExceeded& err) {
137 handleException(err);
138 return;
139 } catch (const std::bad_alloc& err) {
140 handleException(err);
141 return;
142 } catch (...) {
143 error("Uncaught exception, all bets are off...");
144 }
145
146 if (mTrkParams[0].PrintMemory) {
147 mTimeFrame->printArtefactsMemory();
148 mMemoryPool->print();
149 }
150
151 if (mTimeFrame->hasMCinformation()) {
152 computeTracksMClabels();
153 }
154 rectifyClusterIndices();
155 ++mTimeFrameCounter;
156 mTotalTime += total;
157}
158
159void Tracker::computeRoadsMClabels()
160{
162 if (!mTimeFrame->hasMCinformation()) {
163 return;
164 }
165
166 mTimeFrame->initialiseRoadLabels();
167
168 int roadsNum{static_cast<int>(mTimeFrame->getRoads().size())};
169
170 for (int iRoad{0}; iRoad < roadsNum; ++iRoad) {
171
172 Road<5>& currentRoad{mTimeFrame->getRoads()[iRoad]};
173 std::vector<std::pair<MCCompLabel, size_t>> occurrences;
174 bool isFakeRoad{false};
175 bool isFirstRoadCell{true};
176
177 for (int iCell{0}; iCell < mTrkParams[0].CellsPerRoad(); ++iCell) {
178 const int currentCellIndex{currentRoad[iCell]};
179
180 if (currentCellIndex == constants::UnusedIndex) {
181 if (isFirstRoadCell) {
182 continue;
183 } else {
184 break;
185 }
186 }
187
188 const CellSeed& currentCell{mTimeFrame->getCells()[iCell][currentCellIndex]};
189
190 if (isFirstRoadCell) {
191
192 const int cl0index{mTimeFrame->getClusters()[iCell][currentCell.getFirstClusterIndex()].clusterId};
193 auto cl0labs{mTimeFrame->getClusterLabels(iCell, cl0index)};
194 bool found{false};
195 for (size_t iOcc{0}; iOcc < occurrences.size(); ++iOcc) {
196 std::pair<o2::MCCompLabel, size_t>& occurrence = occurrences[iOcc];
197 for (const auto& label : cl0labs) {
198 if (label == occurrence.first) {
199 ++occurrence.second;
200 found = true;
201 // break; // uncomment to stop to the first hit
202 }
203 }
204 }
205 if (!found) {
206 for (const auto& label : cl0labs) {
207 occurrences.emplace_back(label, 1);
208 }
209 }
210
211 const int cl1index{mTimeFrame->getClusters()[iCell + 1][currentCell.getSecondClusterIndex()].clusterId};
212
213 const auto& cl1labs{mTimeFrame->getClusterLabels(iCell + 1, cl1index)};
214 found = false;
215 for (size_t iOcc{0}; iOcc < occurrences.size(); ++iOcc) {
216 std::pair<o2::MCCompLabel, size_t>& occurrence = occurrences[iOcc];
217 for (auto& label : cl1labs) {
218 if (label == occurrence.first) {
219 ++occurrence.second;
220 found = true;
221 // break; // uncomment to stop to the first hit
222 }
223 }
224 }
225 if (!found) {
226 for (auto& label : cl1labs) {
227 occurrences.emplace_back(label, 1);
228 }
229 }
230
231 isFirstRoadCell = false;
232 }
233
234 const int cl2index{mTimeFrame->getClusters()[iCell + 2][currentCell.getThirdClusterIndex()].clusterId};
235 const auto& cl2labs{mTimeFrame->getClusterLabels(iCell + 2, cl2index)};
236 bool found{false};
237 for (size_t iOcc{0}; iOcc < occurrences.size(); ++iOcc) {
238 std::pair<o2::MCCompLabel, size_t>& occurrence = occurrences[iOcc];
239 for (auto& label : cl2labs) {
240 if (label == occurrence.first) {
241 ++occurrence.second;
242 found = true;
243 // break; // uncomment to stop to the first hit
244 }
245 }
246 }
247 if (!found) {
248 for (auto& label : cl2labs) {
249 occurrences.emplace_back(label, 1);
250 }
251 }
252 }
253
254 std::sort(occurrences.begin(), occurrences.end(), [](auto e1, auto e2) {
255 return e1.second > e2.second;
256 });
257
258 auto maxOccurrencesValue = occurrences[0].first;
259 mTimeFrame->setRoadLabel(iRoad, maxOccurrencesValue.getRawValue(), isFakeRoad);
260 }
261}
262
263void Tracker::computeTracksMClabels()
264{
265 for (int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
266 for (auto& track : mTimeFrame->getTracks(iROF)) {
267 std::vector<std::pair<MCCompLabel, size_t>> occurrences;
268 occurrences.clear();
269
270 for (int iCluster = 0; iCluster < TrackITSExt::MaxClusters; ++iCluster) {
271 const int index = track.getClusterIndex(iCluster);
273 continue;
274 }
275 auto labels = mTimeFrame->getClusterLabels(iCluster, index);
276 bool found{false};
277 for (size_t iOcc{0}; iOcc < occurrences.size(); ++iOcc) {
278 std::pair<o2::MCCompLabel, size_t>& occurrence = occurrences[iOcc];
279 for (const auto& label : labels) {
280 if (label == occurrence.first) {
281 ++occurrence.second;
282 found = true;
283 // break; // uncomment to stop to the first hit
284 }
285 }
286 }
287 if (!found) {
288 for (const auto& label : labels) {
289 occurrences.emplace_back(label, 1);
290 }
291 }
292 }
293 std::sort(std::begin(occurrences), std::end(occurrences), [](auto e1, auto e2) {
294 return e1.second > e2.second;
295 });
296
297 auto maxOccurrencesValue = occurrences[0].first;
298 uint32_t pattern = track.getPattern();
299 // set fake clusters pattern
300 for (int ic{TrackITSExt::MaxClusters}; ic--;) {
301 auto clid = track.getClusterIndex(ic);
302 if (clid != constants::UnusedIndex) {
303 auto labelsSpan = mTimeFrame->getClusterLabels(ic, clid);
304 for (const auto& currentLabel : labelsSpan) {
305 if (currentLabel == maxOccurrencesValue) {
306 pattern |= 0x1 << (16 + ic); // set bit if correct
307 break;
308 }
309 }
310 }
311 }
312 track.setPattern(pattern);
313 if (occurrences[0].second < track.getNumberOfClusters()) {
314 maxOccurrencesValue.setFakeFlag();
315 }
316 mTimeFrame->getTracksLabel(iROF).emplace_back(maxOccurrencesValue);
317 }
318 }
319}
320
321void Tracker::rectifyClusterIndices()
322{
323 for (int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
324 for (auto& track : mTimeFrame->getTracks(iROF)) {
325 for (int iCluster = 0; iCluster < TrackITSExt::MaxClusters; ++iCluster) {
326 const int index = track.getClusterIndex(iCluster);
328 track.setExternalClusterIndex(iCluster, mTimeFrame->getClusterExternalIndex(iCluster, index));
329 }
330 }
331 }
332 }
333}
334
336{
337 const auto& tc = o2::its::TrackerParamConfig::Instance();
338 if (tc.useMatCorrTGeo) {
340 } else if (tc.useFastMaterial) {
342 } else {
344 }
345 int nROFsPerIterations = tc.nROFsPerIterations > 0 ? tc.nROFsPerIterations : -1;
346 if (tc.nOrbitsPerIterations > 0) {
348 }
349 for (auto& params : mTrkParams) {
350 if (params.NLayers == 7) {
351 for (int i{0}; i < 7; ++i) {
352 params.SystErrorY2[i] = tc.sysErrY2[i] > 0 ? tc.sysErrY2[i] : params.SystErrorY2[i];
353 params.SystErrorZ2[i] = tc.sysErrZ2[i] > 0 ? tc.sysErrZ2[i] : params.SystErrorZ2[i];
354 }
355 }
356 params.DeltaROF = tc.deltaRof;
357 params.DoUPCIteration = tc.doUPCIteration;
358 params.MaxChi2ClusterAttachment = tc.maxChi2ClusterAttachment > 0 ? tc.maxChi2ClusterAttachment : params.MaxChi2ClusterAttachment;
359 params.MaxChi2NDF = tc.maxChi2NDF > 0 ? tc.maxChi2NDF : params.MaxChi2NDF;
360 params.PhiBins = tc.LUTbinsPhi > 0 ? tc.LUTbinsPhi : params.PhiBins;
361 params.ZBins = tc.LUTbinsZ > 0 ? tc.LUTbinsZ : params.ZBins;
362 params.PVres = tc.pvRes > 0 ? tc.pvRes : params.PVres;
363 params.NSigmaCut *= tc.nSigmaCut > 0 ? tc.nSigmaCut : 1.f;
364 params.CellDeltaTanLambdaSigma *= tc.deltaTanLres > 0 ? tc.deltaTanLres : 1.f;
365 params.TrackletMinPt *= tc.minPt > 0 ? tc.minPt : 1.f;
366 params.nROFsPerIterations = nROFsPerIterations;
367 params.PerPrimaryVertexProcessing = tc.perPrimaryVertexProcessing;
368 params.SaveTimeBenchmarks = tc.saveTimeBenchmarks;
369 params.FataliseUponFailure = tc.fataliseUponFailure;
370 params.DropTFUponFailure = tc.dropTFUponFailure;
371 for (int iD{0}; iD < 3; ++iD) {
372 params.Diamond[iD] = tc.diamondPos[iD];
373 }
374 params.UseDiamond = tc.useDiamond;
375 if (tc.maxMemory) {
376 params.MaxMemory = tc.maxMemory;
377 }
378 if (tc.useTrackFollower > 0) {
379 params.UseTrackFollower = true;
380 // Bit 0: Allow for mixing of top&bot extension --> implies Bits 1&2 set
381 // Bit 1: Allow for top extension
382 // Bit 2: Allow for bot extension
383 params.UseTrackFollowerMix = ((tc.useTrackFollower & (1 << 0)) != 0);
384 params.UseTrackFollowerTop = ((tc.useTrackFollower & (1 << 1)) != 0);
385 params.UseTrackFollowerBot = ((tc.useTrackFollower & (1 << 2)) != 0);
386 params.TrackFollowerNSigmaCutZ = tc.trackFollowerNSigmaZ;
387 params.TrackFollowerNSigmaCutPhi = tc.trackFollowerNSigmaPhi;
388 }
389 if (tc.cellsPerClusterLimit >= 0) {
390 params.CellsPerClusterLimit = tc.cellsPerClusterLimit;
391 }
392 if (tc.trackletsPerClusterLimit >= 0) {
393 params.TrackletsPerClusterLimit = tc.trackletsPerClusterLimit;
394 }
395 if (tc.findShortTracks >= 0) {
396 params.FindShortTracks = tc.findShortTracks;
397 }
398 }
399}
400
402{
403 mTimeFrame = &tf;
404 mTraits->adoptTimeFrame(&tf);
405}
406
408{
409 auto avgTF = mTotalTime * 1.e-3 / ((mTimeFrameCounter > 0) ? (double)mTimeFrameCounter : -1.0);
410 auto avgTFwithDropped = mTotalTime * 1.e-3 / (((mTimeFrameCounter + mNumberOfDroppedTFs) > 0) ? (double)(mTimeFrameCounter + mNumberOfDroppedTFs) : -1.0);
411 LOGP(info, "Tracker summary: Processed {} TFs (dropped {}) in TOT={:.2f} s, AVG/TF={:.2f} ({:.2f}) s", mTimeFrameCounter, mNumberOfDroppedTFs, mTotalTime * 1.e-3, avgTF, avgTFwithDropped);
412}
413
414} // namespace o2::its
Base track model for the Barrel, params only, w/o covariance.
int32_t i
#define GB
Definition Utils.h:40
Class to handle Kalman smoothing for ITS tracking. Its instance stores the state of the track to the ...
static constexpr int MaxClusters
< heavy version of TrackITS, with clusters embedded
Definition TrackITS.h:169
virtual int getTFNumberOfTracklets() const
virtual int getTFNumberOfClusters() const
void updateTrackingParameters(const std::vector< TrackingParameters > &trkPars)
virtual void adoptTimeFrame(TimeFrame< nLayers > *tf)
void setCorrType(const o2::base::PropagatorImpl< float >::MatCorrType type)
virtual bool supportsFindShortPrimaries() const noexcept
virtual bool supportsExtendTracks() const noexcept
virtual const char * getName() const noexcept
virtual int getTFNumberOfCells() const
void printSummary() const
Definition Tracker.cxx:407
void getGlobalConfiguration()
Definition Tracker.cxx:335
void clustersToTracks(const LogFunc &=[](const std::string &s) { std::cout<< s<< '\n';}, const LogFunc &=[](const std::string &s) { std::cerr<< s<< '\n';})
Definition Tracker.cxx:44
void adoptTimeFrame(TimeFrame< NLayers > &tf)
Definition Tracker.cxx:401
Tracker(TrackerTraits< NLayers > *traits)
Definition Tracker.cxx:38
GLuint index
Definition glcorearb.h:781
GLenum const GLfloat * params
Definition glcorearb.h:272
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
constexpr int UnusedIndex
Definition Constants.h:30
constexpr float GB
Definition Constants.h:24
constexpr bool DoTimeBenchmarks
Definition Constants.h:25
std::unique_ptr< GPUReconstructionTimeframe > tf
int getNrof() const
Definition TimeFrame.h:101
bool hasMCinformation() const
Definition TimeFrame.h:147
size_t getNumberOfTracks() const
Definition TimeFrame.h:655
int getClusterExternalIndex(int layerId, const int clId) const
Definition TimeFrame.h:140
gsl::span< const Vertex > getPrimaryVertices(int rofId) const
Definition TimeFrame.h:354
void initialiseRoadLabels()
Definition TimeFrame.h:540
auto & getClusters()
Definition TimeFrame.h:163
gsl::span< const MCCompLabel > getClusterLabels(int layerId, const Cluster &cl) const
Definition TimeFrame.h:138
unsigned long getArtefactsMemory() const
virtual int getNumberOfNeighbours() const
Definition TimeFrame.h:645
void printArtefactsMemory() const
auto & getTracksLabel(const int rofId)
Definition TimeFrame.h:173
void setRoadLabel(int i, const unsigned long long &lab, bool fake)
Definition TimeFrame.h:547
std::array< uint16_t, 5 > pattern