Project
Loading...
Searching...
No Matches
VertexerTraits.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.
12
13#include <memory>
14#include <ranges>
15#include <map>
16#include <algorithm>
17
18#include <oneapi/tbb/blocked_range.h>
19#include <oneapi/tbb/parallel_for.h>
20
30
31namespace o2::its
32{
33
34template <TrackletMode Mode, bool EvalRun, int nLayers>
35static void trackleterKernelHost(
36 const gsl::span<const Cluster>& clustersNextLayer, // 0 2
37 const gsl::span<const Cluster>& clustersCurrentLayer, // 1 1
38 const gsl::span<uint8_t>& usedClustersNextLayer, // 0 2
39 int* indexTableNext,
40 const float phiCut,
41 bounded_vector<Tracklet>& tracklets,
42 gsl::span<int> foundTracklets,
43 const IndexTableUtils<nLayers>& utils,
44 const short pivotRof,
45 const short targetRof,
46 gsl::span<int> rofFoundTrackletsOffsets, // we want to change those, to keep track of the offset in deltaRof>0
47 const int maxTrackletsPerCluster = static_cast<int>(2e3))
48{
49 const int PhiBins{utils.getNphiBins()};
50 const int ZBins{utils.getNzBins()};
51 // loop on layer1 clusters
52 for (int iCurrentLayerClusterIndex = 0; iCurrentLayerClusterIndex < clustersCurrentLayer.size(); ++iCurrentLayerClusterIndex) {
53 int storedTracklets{0};
54 const Cluster& currentCluster{clustersCurrentLayer[iCurrentLayerClusterIndex]};
55 const int4 selectedBinsRect{VertexerTraits<nLayers>::getBinsRect(currentCluster, (int)Mode, 0.f, 50.f, phiCut / 2, utils)};
56 if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) {
57 int phiBinsNum{selectedBinsRect.w - selectedBinsRect.y + 1};
58 if (phiBinsNum < 0) {
59 phiBinsNum += PhiBins;
60 }
61 // loop on phi bins next layer
62 for (int iPhiBin{selectedBinsRect.y}, iPhiCount{0}; iPhiCount < phiBinsNum; iPhiBin = ++iPhiBin == PhiBins ? 0 : iPhiBin, iPhiCount++) {
63 const int firstBinIndex{utils.getBinIndex(selectedBinsRect.x, iPhiBin)};
64 const int firstRowClusterIndex{indexTableNext[firstBinIndex]};
65 const int maxRowClusterIndex{indexTableNext[firstBinIndex + ZBins]};
66 // loop on clusters next layer
67 for (int iNextLayerClusterIndex{firstRowClusterIndex}; iNextLayerClusterIndex < maxRowClusterIndex && iNextLayerClusterIndex < static_cast<int>(clustersNextLayer.size()); ++iNextLayerClusterIndex) {
68 if (usedClustersNextLayer[iNextLayerClusterIndex]) {
69 continue;
70 }
71 const Cluster& nextCluster{clustersNextLayer[iNextLayerClusterIndex]};
72 if (o2::gpu::GPUCommonMath::Abs(math_utils::smallestAngleDifference(currentCluster.phi, nextCluster.phi)) < phiCut) {
73 if (storedTracklets < maxTrackletsPerCluster) {
74 if constexpr (!EvalRun) {
75 if constexpr (Mode == TrackletMode::Layer0Layer1) {
76 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster, targetRof, pivotRof};
77 } else {
78 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iCurrentLayerClusterIndex, iNextLayerClusterIndex, currentCluster, nextCluster, pivotRof, targetRof};
79 }
80 }
81 ++storedTracklets;
82 }
83 }
84 }
85 }
86 }
87 if constexpr (EvalRun) {
88 foundTracklets[iCurrentLayerClusterIndex] += storedTracklets;
89 } else {
90 rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] += storedTracklets;
91 }
92 }
93}
94
95static void trackletSelectionKernelHost(
96 const gsl::span<const Cluster> clusters0, // 0
97 const gsl::span<const Cluster> clusters1, // 1
98 gsl::span<unsigned char> usedClusters0, // Layer 0
99 gsl::span<unsigned char> usedClusters2, // Layer 2
100 const gsl::span<const Tracklet>& tracklets01,
101 const gsl::span<const Tracklet>& tracklets12,
102 bounded_vector<bool>& usedTracklets,
103 const gsl::span<int> foundTracklets01,
104 const gsl::span<int> foundTracklets12,
105 bounded_vector<Line>& lines,
106 const gsl::span<const o2::MCCompLabel>& trackletLabels,
107 bounded_vector<o2::MCCompLabel>& linesLabels,
108 const short targetRofId0,
109 const short targetRofId2,
110 bool safeWrites = false,
111 const float tanLambdaCut = 0.025f,
112 const float phiCut = 0.005f,
113 const int maxTracklets = static_cast<int>(1e2))
114{
115 int offset01{0}, offset12{0};
116 for (unsigned int iCurrentLayerClusterIndex{0}; iCurrentLayerClusterIndex < clusters1.size(); ++iCurrentLayerClusterIndex) {
117 int validTracklets{0};
118 for (int iTracklet12{offset12}; iTracklet12 < offset12 + foundTracklets12[iCurrentLayerClusterIndex]; ++iTracklet12) {
119 for (int iTracklet01{offset01}; iTracklet01 < offset01 + foundTracklets01[iCurrentLayerClusterIndex]; ++iTracklet01) {
120 if (usedTracklets[iTracklet01]) {
121 continue;
122 }
123
124 const auto& tracklet01{tracklets01[iTracklet01]};
125 const auto& tracklet12{tracklets12[iTracklet12]};
126
127 if (tracklet01.rof[0] != targetRofId0 || tracklet12.rof[1] != targetRofId2) {
128 continue;
129 }
130
131 const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(tracklet01.tanLambda - tracklet12.tanLambda)};
132 const float deltaPhi{o2::gpu::GPUCommonMath::Abs(math_utils::smallestAngleDifference(tracklet01.phi, tracklet12.phi))};
133 if (deltaTanLambda < tanLambdaCut && deltaPhi < phiCut && validTracklets != maxTracklets) {
134 if (safeWrites) {
135 __atomic_store_n(&usedClusters0[tracklet01.firstClusterIndex], 1, __ATOMIC_RELAXED);
136 __atomic_store_n(&usedClusters2[tracklet12.secondClusterIndex], 1, __ATOMIC_RELAXED);
137 } else {
138 usedClusters0[tracklet01.firstClusterIndex] = 1;
139 usedClusters2[tracklet12.secondClusterIndex] = 1;
140 }
141 usedTracklets[iTracklet01] = true;
142 lines.emplace_back(tracklet01, clusters0.data(), clusters1.data());
143 if (!trackletLabels.empty()) {
144 linesLabels.emplace_back(trackletLabels[iTracklet01]);
145 }
146 ++validTracklets;
147 }
148 }
149 }
150 offset01 += foundTracklets01[iCurrentLayerClusterIndex];
151 offset12 += foundTracklets12[iCurrentLayerClusterIndex];
152 }
153}
154
155template <int nLayers>
156void VertexerTraits<nLayers>::updateVertexingParameters(const std::vector<VertexingParameters>& vrtPar, const TimeFrameGPUParameters& tfPar)
157{
158 mVrtParams = vrtPar;
159 mIndexTableUtils.setTrackingParameters(vrtPar[0]);
160 for (auto& par : mVrtParams) {
161 par.phiSpan = static_cast<int>(std::ceil(mIndexTableUtils.getNphiBins() * par.phiCut / o2::constants::math::TwoPI));
162 par.zSpan = static_cast<int>(std::ceil(par.zCut * mIndexTableUtils.getInverseZCoordinate(0)));
163 }
164}
165
166// Main functions
167template <int nLayers>
169{
170 mTaskArena->execute([&] {
171 tbb::parallel_for(
172 tbb::blocked_range<short>(0, (short)mTimeFrame->getNrof()),
173 [&](const tbb::blocked_range<short>& Rofs) {
174 for (short pivotRofId = Rofs.begin(); pivotRofId < Rofs.end(); ++pivotRofId) {
175 bool skipROF = iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold;
176 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
177 short endROF{std::min(static_cast<short>(mTimeFrame->getNrof()), static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
178 for (auto targetRofId = startROF; targetRofId < endROF; ++targetRofId) {
179 trackleterKernelHost<TrackletMode::Layer0Layer1, true>(
180 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 0) : gsl::span<Cluster>(), // Clusters to be matched with the next layer in target rof
181 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(), // Clusters to be matched with the current layer in pivot rof
182 mTimeFrame->getUsedClustersROF(targetRofId, 0), // Span of the used clusters in the target rof
183 mTimeFrame->getIndexTable(targetRofId, 0).data(), // Index table to access the data on the next layer in target rof
184 mVrtParams[iteration].phiCut,
185 mTimeFrame->getTracklets()[0], // Flat tracklet buffer
186 mTimeFrame->getNTrackletsCluster(pivotRofId, 0), // Span of the number of tracklets per each cluster in pivot rof
187 mIndexTableUtils,
188 pivotRofId,
189 targetRofId,
190 gsl::span<int>(), // Offset in the tracklet buffer
191 mVrtParams[iteration].maxTrackletsPerCluster);
192 trackleterKernelHost<TrackletMode::Layer1Layer2, true>(
193 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 2) : gsl::span<Cluster>(),
194 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
195 mTimeFrame->getUsedClustersROF(targetRofId, 2),
196 mTimeFrame->getIndexTable(targetRofId, 2).data(),
197 mVrtParams[iteration].phiCut,
198 mTimeFrame->getTracklets()[1],
199 mTimeFrame->getNTrackletsCluster(pivotRofId, 1), // Span of the number of tracklets per each cluster in pivot rof
200 mIndexTableUtils,
201 pivotRofId,
202 targetRofId,
203 gsl::span<int>(), // Offset in the tracklet buffer
204 mVrtParams[iteration].maxTrackletsPerCluster);
205 }
206 mTimeFrame->getNTrackletsROF(pivotRofId, 0) = std::accumulate(mTimeFrame->getNTrackletsCluster(pivotRofId, 0).begin(), mTimeFrame->getNTrackletsCluster(pivotRofId, 0).end(), 0);
207 mTimeFrame->getNTrackletsROF(pivotRofId, 1) = std::accumulate(mTimeFrame->getNTrackletsCluster(pivotRofId, 1).begin(), mTimeFrame->getNTrackletsCluster(pivotRofId, 1).end(), 0);
208 }
209 });
210
211 mTimeFrame->computeTrackletsPerROFScans();
212 if (auto tot0 = mTimeFrame->getTotalTrackletsTF(0), tot1 = mTimeFrame->getTotalTrackletsTF(1);
213 tot0 == 0 || tot1 == 0) {
214 return;
215 } else {
216 mTimeFrame->getTracklets()[0].resize(tot0);
217 mTimeFrame->getTracklets()[1].resize(tot1);
218 }
219
220 tbb::parallel_for(
221 tbb::blocked_range<short>(0, (short)mTimeFrame->getNrof()),
222 [&](const tbb::blocked_range<short>& Rofs) {
223 for (short pivotRofId = Rofs.begin(); pivotRofId < Rofs.end(); ++pivotRofId) {
224 bool skipROF = iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold;
225 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
226 short endROF{std::min(static_cast<short>(mTimeFrame->getNrof()), static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
227 auto mobileOffset0 = mTimeFrame->getNTrackletsROF(pivotRofId, 0);
228 auto mobileOffset1 = mTimeFrame->getNTrackletsROF(pivotRofId, 1);
229 for (auto targetRofId = startROF; targetRofId < endROF; ++targetRofId) {
230 trackleterKernelHost<TrackletMode::Layer0Layer1, false>(
231 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 0) : gsl::span<Cluster>(),
232 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
233 mTimeFrame->getUsedClustersROF(targetRofId, 0),
234 mTimeFrame->getIndexTable(targetRofId, 0).data(),
235 mVrtParams[iteration].phiCut,
236 mTimeFrame->getTracklets()[0],
237 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
238 mIndexTableUtils,
239 pivotRofId,
240 targetRofId,
241 mTimeFrame->getExclusiveNTrackletsCluster(pivotRofId, 0),
242 mVrtParams[iteration].maxTrackletsPerCluster);
243 trackleterKernelHost<TrackletMode::Layer1Layer2, false>(
244 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 2) : gsl::span<Cluster>(),
245 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
246 mTimeFrame->getUsedClustersROF(targetRofId, 2),
247 mTimeFrame->getIndexTable(targetRofId, 2).data(),
248 mVrtParams[iteration].phiCut,
249 mTimeFrame->getTracklets()[1],
250 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
251 mIndexTableUtils,
252 pivotRofId,
253 targetRofId,
254 mTimeFrame->getExclusiveNTrackletsCluster(pivotRofId, 1),
255 mVrtParams[iteration].maxTrackletsPerCluster);
256 }
257 }
258 });
259 });
260
262 if (mTimeFrame->hasMCinformation()) {
263 for (const auto& trk : mTimeFrame->getTracklets()[0]) {
265 if (!trk.isEmpty()) {
266 int sortedId0{mTimeFrame->getSortedIndex(trk.rof[0], 0, trk.firstClusterIndex)};
267 int sortedId1{mTimeFrame->getSortedIndex(trk.rof[1], 1, trk.secondClusterIndex)};
268 for (const auto& lab0 : mTimeFrame->getClusterLabels(0, mTimeFrame->getClusters()[0][sortedId0].clusterId)) {
269 for (const auto& lab1 : mTimeFrame->getClusterLabels(1, mTimeFrame->getClusters()[1][sortedId1].clusterId)) {
270 if (lab0 == lab1 && lab0.isValid()) {
271 label = lab0;
272 break;
273 }
274 }
275 if (label.isValid()) {
276 break;
277 }
278 }
279 }
280 mTimeFrame->getTrackletsLabel(0).emplace_back(label);
281 }
282 }
283
284#ifdef VTX_DEBUG
285 debugComputeTracklets(iteration);
286#endif
287}
288
289template <int nLayers>
291{
292 mTaskArena->execute([&] {
293 tbb::parallel_for(
294 tbb::blocked_range<short>(0, (short)mTimeFrame->getNrof()),
295 [&](const tbb::blocked_range<short>& Rofs) {
296 for (short pivotRofId = Rofs.begin(); pivotRofId < Rofs.end(); ++pivotRofId) {
297 if (iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold) {
298 continue;
299 }
300 if (mTimeFrame->getFoundTracklets(pivotRofId, 0).empty()) {
301 continue;
302 }
303 mTimeFrame->getLines(pivotRofId).reserve(mTimeFrame->getNTrackletsCluster(pivotRofId, 0).size());
304 bounded_vector<bool> usedTracklets(mTimeFrame->getFoundTracklets(pivotRofId, 0).size(), false, mMemoryPool.get());
305 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
306 short endROF{std::min(static_cast<short>(mTimeFrame->getNrof()), static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
307
308 // needed only if multi-threaded using deltaRof and only at the overlap edges of the ranges
309 bool safeWrite = mTaskArena->max_concurrency() > 1 && mVrtParams[iteration].deltaRof != 0 && ((Rofs.begin() - startROF < 0) || (endROF - Rofs.end() > 0));
310
311 for (short targetRofId0 = startROF; targetRofId0 < endROF; ++targetRofId0) {
312 for (short targetRofId2 = startROF; targetRofId2 < endROF; ++targetRofId2) {
313 if (std::abs(targetRofId0 - targetRofId2) > mVrtParams[iteration].deltaRof) { // do not allow over 3 ROFs
314 continue;
315 }
316 trackletSelectionKernelHost(
317 mTimeFrame->getClustersOnLayer(targetRofId0, 0),
318 mTimeFrame->getClustersOnLayer(pivotRofId, 1),
319 mTimeFrame->getUsedClustersROF(targetRofId0, 0),
320 mTimeFrame->getUsedClustersROF(targetRofId2, 2),
321 mTimeFrame->getFoundTracklets(pivotRofId, 0),
322 mTimeFrame->getFoundTracklets(pivotRofId, 1),
323 usedTracklets,
324 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
325 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
326 mTimeFrame->getLines(pivotRofId),
327 mTimeFrame->getLabelsFoundTracklets(pivotRofId, 0),
328 mTimeFrame->getLinesLabel(pivotRofId),
329 targetRofId0,
330 targetRofId2,
331 safeWrite,
332 mVrtParams[iteration].tanLambdaCut,
333 mVrtParams[iteration].phiCut);
334 }
335 }
336 }
337 });
338 });
339
340#ifdef VTX_DEBUG
341 debugComputeTrackletMatching(iteration);
342#endif
343
344 // from here on we do not use tracklets from L1-2 anymore, so let's free them
345 deepVectorClear(mTimeFrame->getTracklets()[1]);
346}
347
348template <int nLayers>
350{
351 auto nsigmaCut{std::min(mVrtParams[iteration].vertNsigmaCut * mVrtParams[iteration].vertNsigmaCut * (mVrtParams[iteration].vertRadiusSigma * mVrtParams[iteration].vertRadiusSigma + mVrtParams[iteration].trackletSigma * mVrtParams[iteration].trackletSigma), 1.98f)};
352 bounded_vector<Vertex> vertices(mMemoryPool.get());
353 bounded_vector<std::pair<o2::MCCompLabel, float>> polls(mMemoryPool.get());
354 bounded_vector<o2::MCCompLabel> contLabels(mMemoryPool.get());
355 bounded_vector<int> noClustersVec(mTimeFrame->getNrof(), 0, mMemoryPool.get());
356 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
357 if (iteration && (int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold) {
358 continue;
359 }
360 const int numTracklets{static_cast<int>(mTimeFrame->getLines(rofId).size())};
361
362 bounded_vector<bool> usedTracklets(numTracklets, false, mMemoryPool.get());
363 for (int line1{0}; line1 < numTracklets; ++line1) {
364 if (usedTracklets[line1]) {
365 continue;
366 }
367 for (int line2{line1 + 1}; line2 < numTracklets; ++line2) {
368 if (usedTracklets[line2]) {
369 continue;
370 }
371 auto dca{Line::getDCA(mTimeFrame->getLines(rofId)[line1], mTimeFrame->getLines(rofId)[line2])};
372 if (dca < mVrtParams[iteration].pairCut) {
373 mTimeFrame->getTrackletClusters(rofId).emplace_back(line1, mTimeFrame->getLines(rofId)[line1], line2, mTimeFrame->getLines(rofId)[line2]);
374 std::array<float, 3> tmpVertex{mTimeFrame->getTrackletClusters(rofId).back().getVertex()};
375 if (tmpVertex[0] * tmpVertex[0] + tmpVertex[1] * tmpVertex[1] > 4.f) {
376 mTimeFrame->getTrackletClusters(rofId).pop_back();
377 break;
378 }
379 usedTracklets[line1] = true;
380 usedTracklets[line2] = true;
381 for (int tracklet3{0}; tracklet3 < numTracklets; ++tracklet3) {
382 if (usedTracklets[tracklet3]) {
383 continue;
384 }
385 if (Line::getDistanceFromPoint(mTimeFrame->getLines(rofId)[tracklet3], tmpVertex) < mVrtParams[iteration].pairCut) {
386 mTimeFrame->getTrackletClusters(rofId).back().add(tracklet3, mTimeFrame->getLines(rofId)[tracklet3]);
387 usedTracklets[tracklet3] = true;
388 tmpVertex = mTimeFrame->getTrackletClusters(rofId).back().getVertex();
389 }
390 }
391 break;
392 }
393 }
394 }
395 if (mVrtParams[iteration].allowSingleContribClusters) {
396 auto beamLine = Line{{mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), -50.f}, {mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), 50.f}}; // use beam position as contributor
397 for (size_t iLine{0}; iLine < numTracklets; ++iLine) {
398 if (!usedTracklets[iLine]) {
399 auto dca = Line::getDCA(mTimeFrame->getLines(rofId)[iLine], beamLine);
400 if (dca < mVrtParams[iteration].pairCut) {
401 mTimeFrame->getTrackletClusters(rofId).emplace_back(iLine, mTimeFrame->getLines(rofId)[iLine], -1, beamLine); // beamline must be passed as second line argument
402 }
403 }
404 }
405 }
406
407 // Cluster merging
408 std::sort(mTimeFrame->getTrackletClusters(rofId).begin(), mTimeFrame->getTrackletClusters(rofId).end(),
409 [](ClusterLines& cluster1, ClusterLines& cluster2) { return cluster1.getSize() > cluster2.getSize(); });
410 noClustersVec[rofId] = static_cast<int>(mTimeFrame->getTrackletClusters(rofId).size());
411 for (int iCluster1{0}; iCluster1 < noClustersVec[rofId]; ++iCluster1) {
412 std::array<float, 3> vertex1{mTimeFrame->getTrackletClusters(rofId)[iCluster1].getVertex()};
413 std::array<float, 3> vertex2{};
414 for (int iCluster2{iCluster1 + 1}; iCluster2 < noClustersVec[rofId]; ++iCluster2) {
415 vertex2 = mTimeFrame->getTrackletClusters(rofId)[iCluster2].getVertex();
416 if (o2::gpu::GPUCommonMath::Abs(vertex1[2] - vertex2[2]) < mVrtParams[iteration].clusterCut) {
417 float distance{(vertex1[0] - vertex2[0]) * (vertex1[0] - vertex2[0]) +
418 (vertex1[1] - vertex2[1]) * (vertex1[1] - vertex2[1]) +
419 (vertex1[2] - vertex2[2]) * (vertex1[2] - vertex2[2])};
420 if (distance < mVrtParams[iteration].pairCut * mVrtParams[iteration].pairCut) {
421 for (auto label : mTimeFrame->getTrackletClusters(rofId)[iCluster2].getLabels()) {
422 mTimeFrame->getTrackletClusters(rofId)[iCluster1].add(label, mTimeFrame->getLines(rofId)[label]);
423 vertex1 = mTimeFrame->getTrackletClusters(rofId)[iCluster1].getVertex();
424 }
425 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster2);
426 --iCluster2;
427 --noClustersVec[rofId];
428 }
429 }
430 }
431 }
432 }
433 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
434 std::sort(mTimeFrame->getTrackletClusters(rofId).begin(), mTimeFrame->getTrackletClusters(rofId).end(),
435 [](const ClusterLines& cluster1, const ClusterLines& cluster2) { return cluster1.getSize() > cluster2.getSize(); }); // ensure clusters are ordered by contributors, so that we can cat after the first.
436 bool atLeastOneFound{false};
437 for (int iCluster{0}; iCluster < noClustersVec[rofId]; ++iCluster) {
438 bool lowMultCandidate{false};
439 double beamDistance2{(mTimeFrame->getBeamX() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[0]) * (mTimeFrame->getBeamX() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[0]) +
440 (mTimeFrame->getBeamY() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1]) * (mTimeFrame->getBeamY() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1])};
441 if (atLeastOneFound && (lowMultCandidate = mTimeFrame->getTrackletClusters(rofId)[iCluster].getSize() < mVrtParams[iteration].clusterContributorsCut)) { // We might have pile up with nContr > cut.
442 lowMultCandidate &= (beamDistance2 < mVrtParams[iteration].lowMultBeamDistCut * mVrtParams[iteration].lowMultBeamDistCut);
443 if (!lowMultCandidate) { // Not the first cluster and not a low multiplicity candidate, we can remove it
444 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster);
445 noClustersVec[rofId]--;
446 continue;
447 }
448 }
449
450 if (beamDistance2 < nsigmaCut && o2::gpu::GPUCommonMath::Abs(mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]) < mVrtParams[iteration].maxZPositionAllowed) {
451 atLeastOneFound = true;
452 auto& vertex = vertices.emplace_back(o2::math_utils::Point3D<float>(mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[0],
453 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1],
454 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]),
455 mTimeFrame->getTrackletClusters(rofId)[iCluster].getRMS2(), // Symm matrix. Diagonal: RMS2 components,
456 // off-diagonal: square mean of projections on planes.
457 mTimeFrame->getTrackletClusters(rofId)[iCluster].getSize(), // Contributors
458 mTimeFrame->getTrackletClusters(rofId)[iCluster].getAvgDistance2()); // In place of chi2
459
460 if (iteration) {
461 vertex.setFlags(Vertex::UPCMode);
462 }
463 vertex.setTimeStamp(mTimeFrame->getTrackletClusters(rofId)[iCluster].getROF());
464 if (mTimeFrame->hasMCinformation()) {
465 bounded_vector<o2::MCCompLabel> labels(mMemoryPool.get());
466 for (auto& index : mTimeFrame->getTrackletClusters(rofId)[iCluster].getLabels()) {
467 labels.push_back(mTimeFrame->getLinesLabel(rofId)[index]); // then we can use nContributors from vertices to get the labels
468 }
469 polls.push_back(computeMain(labels));
470 if (mVrtParams[iteration].outputContLabels) {
471 contLabels.insert(contLabels.end(), labels.begin(), labels.end());
472 }
473 }
474 }
475 }
476 if (!iteration) {
477 mTimeFrame->addPrimaryVertices(vertices, iteration);
478 if (mTimeFrame->hasMCinformation()) {
479 mTimeFrame->addPrimaryVerticesLabels(polls);
480 if (mVrtParams[iteration].outputContLabels) {
481 mTimeFrame->addPrimaryVerticesContributorLabels(contLabels);
482 }
483 }
484 } else {
485 mTimeFrame->addPrimaryVerticesInROF(vertices, rofId, iteration);
486 if (mTimeFrame->hasMCinformation()) {
487 mTimeFrame->addPrimaryVerticesLabelsInROF(polls, rofId);
488 if (mVrtParams[iteration].outputContLabels) {
489 mTimeFrame->addPrimaryVerticesContributorLabelsInROF(contLabels, rofId);
490 }
491 }
492 }
493 if (vertices.empty() && !(iteration && (int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold)) {
494 mTimeFrame->getNoVertexROF()++;
495 }
496 vertices.clear();
497 polls.clear();
498 }
499
500#ifdef VTX_DEBUG
501 debugComputeVertices(iteration);
502#endif
503}
504
505template <int nLayers>
507{
508 LOGP(info, "Using truth seeds as vertices; will skip computations");
509 mTimeFrame->resetRofPV();
510 const auto dc = o2::steer::DigitizationContext::loadFromFile("collisioncontext.root");
511 const auto irs = dc->getEventRecords();
512 int64_t roFrameBiasInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameBiasInBC;
513 int64_t roFrameLengthInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameLengthInBC;
515 struct VertInfo {
516 bounded_vector<Vertex> vertices;
518 bounded_vector<int> events;
519 };
520 std::map<int, VertInfo> vertices;
521 for (int iSrc{0}; iSrc < mcReader.getNSources(); ++iSrc) {
522 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
523 for (int iEve{0}; iEve < mcReader.getNEvents(iSrc); ++iEve) {
524 const auto& ir = irs[eveId2colId[iEve]];
525 if (!ir.isDummy()) { // do we need this, is this for diffractive events?
526 const auto& eve = mcReader.getMCEventHeader(iSrc, iEve);
527 int rofId = ((ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) / roFrameLengthInBC;
528 if (!vertices.contains(rofId)) {
529 vertices[rofId] = {
530 .vertices = bounded_vector<Vertex>(mMemoryPool.get()),
531 .srcs = bounded_vector<int>(mMemoryPool.get()),
532 .events = bounded_vector<int>(mMemoryPool.get()),
533 };
534 }
535 Vertex vert;
536 vert.setTimeStamp(rofId);
537 vert.setNContributors(std::ranges::count_if(mcReader.getTracks(iSrc, iEve), [](const auto& trk) {
538 return trk.isPrimary() && trk.GetPt() > 0.2 && std::abs(trk.GetEta()) < 1.3;
539 }));
540 vert.setXYZ((float)eve.GetX(), (float)eve.GetY(), (float)eve.GetZ());
541 vert.setChi2(1);
542 constexpr float cov = 50e-9;
543 vert.setCov(cov, cov, cov, cov, cov, cov);
544 vertices[rofId].vertices.push_back(vert);
545 vertices[rofId].srcs.push_back(iSrc);
546 vertices[rofId].events.push_back(iEve);
547 }
548 }
549 }
550 size_t nVerts{0};
551 for (int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
552 bounded_vector<Vertex> verts(mMemoryPool.get());
553 bounded_vector<std::pair<o2::MCCompLabel, float>> polls(mMemoryPool.get());
554 if (vertices.contains(iROF)) {
555 const auto& vertInfo = vertices[iROF];
556 verts = vertInfo.vertices;
557 nVerts += verts.size();
558 for (size_t i{0}; i < verts.size(); ++i) {
559 o2::MCCompLabel lbl(o2::MCCompLabel::maxTrackID(), vertInfo.events[i], vertInfo.srcs[i], false);
560 polls.emplace_back(lbl, 1.f);
561 }
562 } else {
563 mTimeFrame->getNoVertexROF()++;
564 }
565 mTimeFrame->addPrimaryVertices(verts, 0);
566 mTimeFrame->addPrimaryVerticesLabels(polls);
567 }
568 LOGP(info, "Found {}/{} ROFs with {} vertices -> <NV>={:.2f}", vertices.size(), mTimeFrame->getNrof(), nVerts, (float)nVerts / (float)vertices.size());
569}
570
571template <int nLayers>
572void VertexerTraits<nLayers>::setNThreads(int n, std::shared_ptr<tbb::task_arena>& arena)
573{
574#if defined(VTX_DEBUG)
575 LOGP(info, "Vertexer with debug output forcing single thread");
576 mTaskArena = std::make_shared<tbb::task_arena>(1);
577#else
578 if (arena == nullptr) {
579 mTaskArena = std::make_shared<tbb::task_arena>(std::abs(n));
580 LOGP(info, "Setting seeding vertexer with {} threads.", n);
581 } else {
582 mTaskArena = arena;
583 LOGP(info, "Attaching vertexer to calling thread's arena");
584 }
585#endif
586}
587
588template <int nLayers>
590{
591 auto stream = new utils::TreeStreamRedirector("artefacts_tf.root", "recreate");
592 LOGP(info, "writing debug output for computeTracklets");
593 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
594 const auto& strk0 = mTimeFrame->getFoundTracklets(rofId, 0);
595 std::vector<Tracklet> trk0(strk0.begin(), strk0.end());
596 const auto& strk1 = mTimeFrame->getFoundTracklets(rofId, 1);
597 std::vector<Tracklet> trk1(strk1.begin(), strk1.end());
598 (*stream) << "tracklets"
599 << "Tracklets0=" << trk0
600 << "Tracklets1=" << trk1
601 << "iteration=" << iteration
602 << "\n";
603 }
604 stream->Close();
605 delete stream;
606}
607
608template <int nLayers>
609void VertexerTraits<nLayers>::debugComputeTrackletMatching(int iteration)
610{
611 auto stream = new utils::TreeStreamRedirector("artefacts_tf.root", "update");
612 LOGP(info, "writing debug output for computeTrackletMatching");
613 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
614 (*stream) << "lines"
615 << "Lines=" << toSTDVector(mTimeFrame->getLines(rofId))
616 << "NTrackletCluster01=" << mTimeFrame->getNTrackletsCluster(rofId, 0)
617 << "NTrackletCluster12=" << mTimeFrame->getNTrackletsCluster(rofId, 1)
618 << "iteration=" << iteration
619 << "\n";
620 }
621
622 if (mTimeFrame->hasMCinformation()) {
623 LOGP(info, "\tdumping also MC information");
624 const auto dc = o2::steer::DigitizationContext::loadFromFile("collisioncontext.root");
625 const auto irs = dc->getEventRecords();
626 int64_t roFrameBiasInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameBiasInBC;
627 int64_t roFrameLengthInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameLengthInBC;
629
630 std::map<int, int> eve2BcInROF, bcInRofNEve;
631 for (int iSrc{0}; iSrc < mcReader.getNSources(); ++iSrc) {
632 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
633 for (int iEve{0}; iEve < mcReader.getNEvents(iSrc); ++iEve) {
634 const auto& ir = irs[eveId2colId[iEve]];
635 if (!ir.isDummy()) { // do we need this, is this for diffractive events?
636 const auto& eve = mcReader.getMCEventHeader(iSrc, iEve);
637 const int bcInROF = ((ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) % roFrameLengthInBC;
638 eve2BcInROF[iEve] = bcInROF;
639 ++bcInRofNEve[bcInROF];
640 }
641 }
642 }
643
644 std::unordered_map<int, int> bcROFNTracklets01, bcROFNTracklets12;
645 std::vector<std::vector<int>> tracklet01BC, tracklet12BC;
646 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
647 { // 0-1
648 const auto& tracklet01 = mTimeFrame->getFoundTracklets(rofId, 0);
649 const auto& lbls01 = mTimeFrame->getLabelsFoundTracklets(rofId, 0);
650 auto& trkls01 = tracklet01BC.emplace_back();
651 for (int iTrklt{0}; iTrklt < (int)tracklet01.size(); ++iTrklt) {
652 const auto& tracklet = tracklet01[iTrklt];
653 const auto& lbl = lbls01[iTrklt];
654 if (lbl.isCorrect()) {
655 ++bcROFNTracklets01[eve2BcInROF[lbl.getEventID()]];
656 trkls01.push_back(eve2BcInROF[lbl.getEventID()]);
657 } else {
658 trkls01.push_back(-1);
659 }
660 }
661 }
662 { // 1-2 computed on the fly!
663 const auto& tracklet12 = mTimeFrame->getFoundTracklets(rofId, 1);
664 auto& trkls12 = tracklet12BC.emplace_back();
665 for (int iTrklt{0}; iTrklt < (int)tracklet12.size(); ++iTrklt) {
666 const auto& tracklet = tracklet12[iTrklt];
668
669 int sortedId1{mTimeFrame->getSortedIndex(tracklet.rof[0], 1, tracklet.firstClusterIndex)};
670 int sortedId2{mTimeFrame->getSortedIndex(tracklet.rof[1], 2, tracklet.secondClusterIndex)};
671 for (const auto& lab1 : mTimeFrame->getClusterLabels(1, mTimeFrame->getClusters()[1][sortedId1].clusterId)) {
672 for (const auto& lab2 : mTimeFrame->getClusterLabels(2, mTimeFrame->getClusters()[2][sortedId2].clusterId)) {
673 if (lab1 == lab2 && lab1.isValid()) {
674 label = lab1;
675 break;
676 }
677 }
678 if (label.isValid()) {
679 break;
680 }
681 }
682
683 if (label.isCorrect()) {
684 ++bcROFNTracklets12[eve2BcInROF[label.getEventID()]];
685 trkls12.push_back(eve2BcInROF[label.getEventID()]);
686 } else {
687 trkls12.push_back(-1);
688 }
689 }
690 }
691 }
692 LOGP(info, "\tdumping ntracklets/RofBC ({})", bcInRofNEve.size());
693 for (const auto& [bcInRof, neve] : bcInRofNEve) {
694 (*stream) << "ntracklets"
695 << "bcInROF=" << bcInRof
696 << "ntrkl01=" << bcROFNTracklets01[bcInRof]
697 << "ntrkl12=" << bcROFNTracklets12[bcInRof]
698 << "neve=" << neve
699 << "iteration=" << iteration
700 << "\n";
701 }
702
703 std::unordered_map<int, int> bcROFNLines;
704 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
705 const auto& lines = mTimeFrame->getLines(rofId);
706 const auto& lbls = mTimeFrame->getLinesLabel(rofId);
707 for (int iLine{0}; iLine < (int)lines.size(); ++iLine) {
708 const auto& line = lines[iLine];
709 const auto& lbl = lbls[iLine];
710 if (lbl.isCorrect()) {
711 ++bcROFNLines[eve2BcInROF[lbl.getEventID()]];
712 }
713 }
714 }
715
716 LOGP(info, "\tdumping nlines/RofBC");
717 for (const auto& [bcInRof, neve] : bcInRofNEve) {
718 (*stream) << "nlines"
719 << "bcInROF=" << bcInRof
720 << "nline=" << bcROFNLines[bcInRof]
721 << "neve=" << neve
722 << "iteration=" << iteration
723 << "\n";
724 }
725 }
726 stream->Close();
727 delete stream;
728}
729
730template <int nLayers>
731void VertexerTraits<nLayers>::debugComputeVertices(int iteration)
732{
733 auto stream = new utils::TreeStreamRedirector("artefacts_tf.root", "update");
734 LOGP(info, "writing debug output for computeVertices");
735 for (auto rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
736 (*stream) << "clusterlines"
737 << "clines_post=" << toSTDVector(mTimeFrame->getTrackletClusters(rofId))
738 << "iteration=" << iteration
739 << "\n";
740 }
741
742 if (mTimeFrame->hasMCinformation()) {
743 LOGP(info, "\tdumping also MC information");
744 const auto dc = o2::steer::DigitizationContext::loadFromFile("collisioncontext.root");
745 const auto irs = dc->getEventRecords();
746 int64_t roFrameBiasInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameBiasInBC;
747 int64_t roFrameLengthInBC = o2::itsmft::DPLAlpideParam<o2::detectors::DetID::ITS>::Instance().roFrameLengthInBC;
749
750 std::map<int, int> eve2BcInROF, bcInRofNEve;
751 for (int iSrc{0}; iSrc < mcReader.getNSources(); ++iSrc) {
752 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
753 for (int iEve{0}; iEve < mcReader.getNEvents(iSrc); ++iEve) {
754 const auto& ir = irs[eveId2colId[iEve]];
755 if (!ir.isDummy()) { // do we need this, is this for diffractive events?
756 const auto& eve = mcReader.getMCEventHeader(iSrc, iEve);
757 const int bcInROF = ((ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) % roFrameLengthInBC;
758 eve2BcInROF[iEve] = bcInROF;
759 ++bcInRofNEve[bcInROF];
760 }
761 }
762 }
763
764 std::unordered_map<int, int> bcROFNVtx;
765 std::unordered_map<int, float> bcROFNPur;
766 std::unordered_map<o2::MCCompLabel, size_t> uniqueVertices;
767 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
768 const auto& pvs = mTimeFrame->getPrimaryVertices(rofId);
769 const auto& lblspv = mTimeFrame->getPrimaryVerticesMCRecInfo(rofId);
770 for (int i{0}; i < (int)pvs.size(); ++i) {
771 const auto& pv = pvs[i];
772 const auto& [lbl, pur] = lblspv[i];
773 if (lbl.isCorrect()) {
774 ++uniqueVertices[lbl];
775 ++bcROFNVtx[eve2BcInROF[lbl.getEventID()]];
776 bcROFNPur[eve2BcInROF[lbl.getEventID()]] += pur;
777 }
778 }
779 }
780
781 std::unordered_map<int, int> bcROFNUVtx, bcROFNCVtx;
782 for (const auto& [k, _] : eve2BcInROF) {
783 bcROFNUVtx[k] = bcROFNCVtx[k] = 0;
784 }
785
786 for (const auto& [lbl, c] : uniqueVertices) {
787 if (c <= 1) {
788 ++bcROFNUVtx[eve2BcInROF[lbl.getEventID()]];
789 } else {
790 ++bcROFNCVtx[eve2BcInROF[lbl.getEventID()]];
791 }
792 }
793
794 LOGP(info, "\tdumping nvtx/RofBC");
795 for (const auto& [bcInRof, neve] : bcInRofNEve) {
796 (*stream) << "nvtx"
797 << "bcInROF=" << bcInRof
798 << "nvtx=" << bcROFNVtx[bcInRof] // all vertices
799 << "nuvtx=" << bcROFNUVtx[bcInRof] // unique vertices
800 << "ncvtx=" << bcROFNCVtx[bcInRof] // cloned vertices
801 << "npur=" << bcROFNPur[bcInRof]
802 << "neve=" << neve
803 << "iteration=" << iteration
804 << "\n";
805 }
806
807 // check dist of clones
808 std::unordered_map<o2::MCCompLabel, std::vector<Vertex>> cVtx;
809 for (int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
810 const auto& pvs = mTimeFrame->getPrimaryVertices(rofId);
811 const auto& lblspv = mTimeFrame->getPrimaryVerticesMCRecInfo(rofId);
812 for (int i{0}; i < (int)pvs.size(); ++i) {
813 const auto& pv = pvs[i];
814 const auto& [lbl, pur] = lblspv[i];
815 if (lbl.isCorrect() && uniqueVertices.contains(lbl) && uniqueVertices[lbl] > 1) {
816 if (!cVtx.contains(lbl)) {
817 cVtx[lbl] = std::vector<Vertex>();
818 }
819 cVtx[lbl].push_back(pv);
820 }
821 }
822 }
823
824 for (auto& [_, vertices] : cVtx) {
825 std::sort(vertices.begin(), vertices.end(), [](const Vertex& a, const Vertex& b) { return a.getNContributors() > b.getNContributors(); });
826 for (int i{0}; i < (int)vertices.size(); ++i) {
827 const auto vtx = vertices[i];
828 (*stream) << "cvtx"
829 << "vertex=" << vtx
830 << "i=" << i
831 << "dx=" << vertices[0].getX() - vtx.getX()
832 << "dy=" << vertices[0].getY() - vtx.getY()
833 << "dz=" << vertices[0].getZ() - vtx.getZ()
834 << "drof=" << vertices[0].getTimeStamp().getTimeStamp() - vtx.getTimeStamp().getTimeStamp()
835 << "dnc=" << vertices[0].getNContributors() - vtx.getNContributors()
836 << "iteration=" << iteration
837 << "\n";
838 }
839 }
840 }
841 stream->Close();
842 delete stream;
843}
844
845template class VertexerTraits<7>;
846} // namespace o2::its
uint64_t vertex
Definition RawEventData.h:9
int32_t i
Mode
Definition Utils.h:89
uint32_t c
Definition RawData.h:2
Class to compute the primary vertex in ITS from tracklets.
static constexpr int maxTrackID()
HMPID cluster implementation.
Definition Cluster.h:27
virtual void computeTracklets(const int iteration=0)
virtual void updateVertexingParameters(const std::vector< VertexingParameters > &vrtPar, const TimeFrameGPUParameters &gpuTfPar)
static DigitizationContext * loadFromFile(std::string_view filename="")
size_t getNEvents(int source) const
Get number of events.
o2::dataformats::MCEventHeader const & getMCEventHeader(int source, int event) const
retrieves the MCEventHeader for a given eventID and sourceID
size_t getNSources() const
Get number of sources.
std::vector< MCTrack > const & getTracks(int source, int event) const
variant returning all tracks for source and event at once
GLdouble n
Definition glcorearb.h:1982
GLuint index
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei GLsizei GLfloat distance
Definition glcorearb.h:5506
GLuint GLsizei const GLchar * label
Definition glcorearb.h:2519
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLuint GLuint stream
Definition glcorearb.h:1806
constexpr float TwoPI
int32_t const char int32_t line
void deepVectorClear(std::vector< T > &vec)
std::pmr::vector< T > bounded_vector
std::vector< T > toSTDVector(const bounded_vector< T > &b)
std::vector< Cluster > getClusters(int event)
Common utility functions.
int32_t w
o2::InteractionRecord ir(0, 0)
std::vector< Tracklet64 > tracklets