18#include <oneapi/tbb/blocked_range.h>
19#include <oneapi/tbb/parallel_for.h>
34template <TrackletMode Mode,
bool EvalRun,
int nLayers>
35static void trackleterKernelHost(
36 const gsl::span<const Cluster>& clustersNextLayer,
37 const gsl::span<const Cluster>& clustersCurrentLayer,
38 const gsl::span<uint8_t>& usedClustersNextLayer,
42 gsl::span<int> foundTracklets,
43 const IndexTableUtils<nLayers>&
utils,
45 const short targetRof,
46 gsl::span<int> rofFoundTrackletsOffsets,
47 const int maxTrackletsPerCluster =
static_cast<int>(2e3))
49 const int PhiBins{
utils.getNphiBins()};
50 const int ZBins{
utils.getNzBins()};
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};
59 phiBinsNum += PhiBins;
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]};
67 for (
int iNextLayerClusterIndex{firstRowClusterIndex}; iNextLayerClusterIndex < maxRowClusterIndex && iNextLayerClusterIndex < static_cast<int>(clustersNextLayer.size()); ++iNextLayerClusterIndex) {
68 if (usedClustersNextLayer[iNextLayerClusterIndex]) {
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) {
76 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster, targetRof, pivotRof};
78 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iCurrentLayerClusterIndex, iNextLayerClusterIndex, currentCluster, nextCluster, pivotRof, targetRof};
87 if constexpr (EvalRun) {
88 foundTracklets[iCurrentLayerClusterIndex] += storedTracklets;
90 rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] += storedTracklets;
95static void trackletSelectionKernelHost(
96 const gsl::span<const Cluster> clusters0,
97 const gsl::span<const Cluster> clusters1,
98 gsl::span<unsigned char> usedClusters0,
99 gsl::span<unsigned char> usedClusters2,
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))
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]) {
124 const auto& tracklet01{tracklets01[iTracklet01]};
125 const auto& tracklet12{tracklets12[iTracklet12]};
127 if (tracklet01.rof[0] != targetRofId0 || tracklet12.rof[1] != targetRofId2) {
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) {
135 __atomic_store_n(&usedClusters0[tracklet01.firstClusterIndex], 1, __ATOMIC_RELAXED);
136 __atomic_store_n(&usedClusters2[tracklet12.secondClusterIndex], 1, __ATOMIC_RELAXED);
138 usedClusters0[tracklet01.firstClusterIndex] = 1;
139 usedClusters2[tracklet12.secondClusterIndex] = 1;
141 usedTracklets[iTracklet01] =
true;
142 lines.emplace_back(tracklet01, clusters0.data(), clusters1.data());
143 if (!trackletLabels.empty()) {
144 linesLabels.emplace_back(trackletLabels[iTracklet01]);
150 offset01 += foundTracklets01[iCurrentLayerClusterIndex];
151 offset12 += foundTracklets12[iCurrentLayerClusterIndex];
155template <
int nLayers>
159 mIndexTableUtils.setTrackingParameters(vrtPar[0]);
160 for (
auto& par : mVrtParams) {
162 par.zSpan =
static_cast<int>(std::ceil(par.zCut * mIndexTableUtils.getInverseZCoordinate(0)));
167template <
int nLayers>
170 mTaskArena->execute([&] {
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>(),
181 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
182 mTimeFrame->getUsedClustersROF(targetRofId, 0),
183 mTimeFrame->getIndexTable(targetRofId, 0).data(),
184 mVrtParams[iteration].phiCut,
185 mTimeFrame->getTracklets()[0],
186 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
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),
204 mVrtParams[iteration].maxTrackletsPerCluster);
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);
211 mTimeFrame->computeTrackletsPerROFScans();
212 if (
auto tot0 = mTimeFrame->getTotalTrackletsTF(0), tot1 = mTimeFrame->getTotalTrackletsTF(1);
213 tot0 == 0 || tot1 == 0) {
216 mTimeFrame->getTracklets()[0].resize(tot0);
217 mTimeFrame->getTracklets()[1].resize(tot1);
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),
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),
254 mTimeFrame->getExclusiveNTrackletsCluster(pivotRofId, 1),
255 mVrtParams[iteration].maxTrackletsPerCluster);
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()) {
275 if (
label.isValid()) {
280 mTimeFrame->getTrackletsLabel(0).emplace_back(
label);
285 debugComputeTracklets(iteration);
289template <
int nLayers>
292 mTaskArena->execute([&] {
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) {
300 if (mTimeFrame->getFoundTracklets(pivotRofId, 0).empty()) {
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))};
309 bool safeWrite = mTaskArena->max_concurrency() > 1 && mVrtParams[iteration].deltaRof != 0 && ((Rofs.begin() - startROF < 0) || (endROF - Rofs.end() > 0));
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) {
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),
324 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
325 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
326 mTimeFrame->getLines(pivotRofId),
327 mTimeFrame->getLabelsFoundTracklets(pivotRofId, 0),
328 mTimeFrame->getLinesLabel(pivotRofId),
332 mVrtParams[iteration].tanLambdaCut,
333 mVrtParams[iteration].phiCut);
341 debugComputeTrackletMatching(iteration);
348template <
int nLayers>
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)};
356 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
357 if (iteration && (
int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold) {
360 const int numTracklets{
static_cast<int>(mTimeFrame->getLines(rofId).size())};
363 for (
int line1{0}; line1 < numTracklets; ++line1) {
364 if (usedTracklets[line1]) {
367 for (
int line2{line1 + 1}; line2 < numTracklets; ++line2) {
368 if (usedTracklets[line2]) {
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();
379 usedTracklets[line1] =
true;
380 usedTracklets[line2] =
true;
381 for (
int tracklet3{0}; tracklet3 < numTracklets; ++tracklet3) {
382 if (usedTracklets[tracklet3]) {
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();
395 if (mVrtParams[iteration].allowSingleContribClusters) {
396 auto beamLine =
Line{{mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), -50.f}, {mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), 50.f}};
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);
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();
425 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster2);
427 --noClustersVec[rofId];
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(); });
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)) {
442 lowMultCandidate &= (beamDistance2 < mVrtParams[iteration].lowMultBeamDistCut * mVrtParams[iteration].lowMultBeamDistCut);
443 if (!lowMultCandidate) {
444 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster);
445 noClustersVec[rofId]--;
450 if (beamDistance2 < nsigmaCut && o2::gpu::GPUCommonMath::Abs(mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]) < mVrtParams[iteration].maxZPositionAllowed) {
451 atLeastOneFound =
true;
453 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1],
454 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]),
455 mTimeFrame->getTrackletClusters(rofId)[iCluster].getRMS2(),
457 mTimeFrame->getTrackletClusters(rofId)[iCluster].getSize(),
458 mTimeFrame->getTrackletClusters(rofId)[iCluster].getAvgDistance2());
461 vertex.setFlags(Vertex::UPCMode);
463 vertex.setTimeStamp(mTimeFrame->getTrackletClusters(rofId)[iCluster].getROF());
464 if (mTimeFrame->hasMCinformation()) {
466 for (
auto&
index : mTimeFrame->getTrackletClusters(rofId)[iCluster].getLabels()) {
467 labels.push_back(mTimeFrame->getLinesLabel(rofId)[
index]);
469 polls.push_back(computeMain(labels));
470 if (mVrtParams[iteration].outputContLabels) {
471 contLabels.insert(contLabels.end(), labels.begin(), labels.end());
477 mTimeFrame->addPrimaryVertices(vertices, iteration);
478 if (mTimeFrame->hasMCinformation()) {
479 mTimeFrame->addPrimaryVerticesLabels(polls);
480 if (mVrtParams[iteration].outputContLabels) {
481 mTimeFrame->addPrimaryVerticesContributorLabels(contLabels);
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);
493 if (vertices.empty() && !(iteration && (
int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold)) {
494 mTimeFrame->getNoVertexROF()++;
501 debugComputeVertices(iteration);
505template <
int nLayers>
508 LOGP(info,
"Using truth seeds as vertices; will skip computations");
509 mTimeFrame->resetRofPV();
511 const auto irs = dc->getEventRecords();
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]];
527 int rofId = ((
ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) / roFrameLengthInBC;
528 if (!vertices.contains(rofId)) {
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;
540 vert.setXYZ((
float)eve.GetX(), (
float)eve.GetY(), (
float)eve.GetZ());
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);
551 for (
int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
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) {
560 polls.emplace_back(lbl, 1.f);
563 mTimeFrame->getNoVertexROF()++;
565 mTimeFrame->addPrimaryVertices(verts, 0);
566 mTimeFrame->addPrimaryVerticesLabels(polls);
568 LOGP(info,
"Found {}/{} ROFs with {} vertices -> <NV>={:.2f}", vertices.size(), mTimeFrame->getNrof(), nVerts, (
float)nVerts / (
float)vertices.size());
571template <
int nLayers>
574#if defined(VTX_DEBUG)
575 LOGP(info,
"Vertexer with debug output forcing single thread");
576 mTaskArena = std::make_shared<tbb::task_arena>(1);
578 if (arena ==
nullptr) {
579 mTaskArena = std::make_shared<tbb::task_arena>(std::abs(
n));
580 LOGP(info,
"Setting seeding vertexer with {} threads.",
n);
583 LOGP(info,
"Attaching vertexer to calling thread's arena");
588template <
int nLayers>
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
608template <
int nLayers>
609void VertexerTraits<nLayers>::debugComputeTrackletMatching(
int iteration)
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) {
615 <<
"Lines=" <<
toSTDVector(mTimeFrame->getLines(rofId))
616 <<
"NTrackletCluster01=" << mTimeFrame->getNTrackletsCluster(rofId, 0)
617 <<
"NTrackletCluster12=" << mTimeFrame->getNTrackletsCluster(rofId, 1)
618 <<
"iteration=" << iteration
622 if (mTimeFrame->hasMCinformation()) {
623 LOGP(info,
"\tdumping also MC information");
625 const auto irs = dc->getEventRecords();
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]];
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];
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) {
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()]);
658 trkls01.push_back(-1);
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];
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()) {
678 if (
label.isValid()) {
683 if (
label.isCorrect()) {
684 ++bcROFNTracklets12[eve2BcInROF[
label.getEventID()]];
685 trkls12.push_back(eve2BcInROF[
label.getEventID()]);
687 trkls12.push_back(-1);
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]
699 <<
"iteration=" << iteration
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()]];
716 LOGP(info,
"\tdumping nlines/RofBC");
717 for (
const auto& [bcInRof, neve] : bcInRofNEve) {
718 (*stream) <<
"nlines"
719 <<
"bcInROF=" << bcInRof
720 <<
"nline=" << bcROFNLines[bcInRof]
722 <<
"iteration=" << iteration
730template <
int nLayers>
731void VertexerTraits<nLayers>::debugComputeVertices(
int iteration)
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
742 if (mTimeFrame->hasMCinformation()) {
743 LOGP(info,
"\tdumping also MC information");
745 const auto irs = dc->getEventRecords();
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]];
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];
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;
781 std::unordered_map<int, int> bcROFNUVtx, bcROFNCVtx;
782 for (
const auto& [k, _] : eve2BcInROF) {
783 bcROFNUVtx[k] = bcROFNCVtx[k] = 0;
786 for (
const auto& [lbl,
c] : uniqueVertices) {
788 ++bcROFNUVtx[eve2BcInROF[lbl.getEventID()]];
790 ++bcROFNCVtx[eve2BcInROF[lbl.getEventID()]];
794 LOGP(info,
"\tdumping nvtx/RofBC");
795 for (
const auto& [bcInRof, neve] : bcInRofNEve) {
797 <<
"bcInROF=" << bcInRof
798 <<
"nvtx=" << bcROFNVtx[bcInRof]
799 <<
"nuvtx=" << bcROFNUVtx[bcInRof]
800 <<
"ncvtx=" << bcROFNCVtx[bcInRof]
801 <<
"npur=" << bcROFNPur[bcInRof]
803 <<
"iteration=" << iteration
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>();
819 cVtx[lbl].push_back(pv);
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];
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
845template class VertexerTraits<7>;
Class to compute the primary vertex in ITS from tracklets.
static constexpr int maxTrackID()
static const DPLAlpideParam< N > & Instance()
HMPID cluster implementation.
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
GLboolean GLboolean GLboolean b
GLsizei GLsizei GLfloat distance
GLuint GLsizei const GLchar * label
GLboolean GLboolean GLboolean GLboolean a
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.
o2::InteractionRecord ir(0, 0)
std::vector< Tracklet64 > tracklets