18#include <oneapi/tbb/blocked_range.h>
19#include <oneapi/tbb/parallel_for.h>
20#include <oneapi/tbb/combinable.h>
35template <TrackletMode Mode,
bool EvalRun,
int nLayers>
36static void trackleterKernelHost(
37 const gsl::span<const Cluster>& clustersNextLayer,
38 const gsl::span<const Cluster>& clustersCurrentLayer,
39 const gsl::span<uint8_t>& usedClustersNextLayer,
43 gsl::span<int> foundTracklets,
44 const IndexTableUtils<nLayers>&
utils,
46 const short targetRof,
47 gsl::span<int> rofFoundTrackletsOffsets,
48 const int maxTrackletsPerCluster =
static_cast<int>(2e3))
50 const int PhiBins{
utils.getNphiBins()};
51 const int ZBins{
utils.getNzBins()};
53 for (
int iCurrentLayerClusterIndex = 0; iCurrentLayerClusterIndex < clustersCurrentLayer.size(); ++iCurrentLayerClusterIndex) {
54 int storedTracklets{0};
55 const Cluster& currentCluster{clustersCurrentLayer[iCurrentLayerClusterIndex]};
56 const int4 selectedBinsRect{VertexerTraits<nLayers>::getBinsRect(currentCluster, (
int)
Mode, 0.f, 50.f, phiCut / 2,
utils)};
57 if (selectedBinsRect.x != 0 || selectedBinsRect.y != 0 || selectedBinsRect.z != 0 || selectedBinsRect.w != 0) {
58 int phiBinsNum{selectedBinsRect.
w - selectedBinsRect.y + 1};
60 phiBinsNum += PhiBins;
63 for (
int iPhiBin{selectedBinsRect.y}, iPhiCount{0}; iPhiCount < phiBinsNum; iPhiBin = ++iPhiBin == PhiBins ? 0 : iPhiBin, iPhiCount++) {
64 const int firstBinIndex{
utils.getBinIndex(selectedBinsRect.x, iPhiBin)};
65 const int firstRowClusterIndex{indexTableNext[firstBinIndex]};
66 const int maxRowClusterIndex{indexTableNext[firstBinIndex + ZBins]};
68 for (
int iNextLayerClusterIndex{firstRowClusterIndex}; iNextLayerClusterIndex < maxRowClusterIndex && iNextLayerClusterIndex < static_cast<int>(clustersNextLayer.size()); ++iNextLayerClusterIndex) {
69 if (usedClustersNextLayer[iNextLayerClusterIndex]) {
72 const Cluster& nextCluster{clustersNextLayer[iNextLayerClusterIndex]};
73 if (o2::gpu::GPUCommonMath::Abs(math_utils::smallestAngleDifference(currentCluster.phi, nextCluster.phi)) < phiCut) {
74 if (storedTracklets < maxTrackletsPerCluster) {
75 if constexpr (!EvalRun) {
77 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iNextLayerClusterIndex, iCurrentLayerClusterIndex, nextCluster, currentCluster, targetRof, pivotRof};
79 tracklets[rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] + storedTracklets] = Tracklet{iCurrentLayerClusterIndex, iNextLayerClusterIndex, currentCluster, nextCluster, pivotRof, targetRof};
88 if constexpr (EvalRun) {
89 foundTracklets[iCurrentLayerClusterIndex] += storedTracklets;
91 rofFoundTrackletsOffsets[iCurrentLayerClusterIndex] += storedTracklets;
96static void trackletSelectionKernelHost(
97 const gsl::span<const Cluster> clusters0,
98 const gsl::span<const Cluster> clusters1,
99 gsl::span<unsigned char> usedClusters0,
100 gsl::span<unsigned char> usedClusters2,
101 const gsl::span<const Tracklet>& tracklets01,
102 const gsl::span<const Tracklet>& tracklets12,
103 bounded_vector<bool>& usedTracklets,
104 const gsl::span<int> foundTracklets01,
105 const gsl::span<int> foundTracklets12,
106 bounded_vector<Line>& lines,
107 const gsl::span<const o2::MCCompLabel>& trackletLabels,
108 bounded_vector<o2::MCCompLabel>& linesLabels,
109 const short targetRofId0,
110 const short targetRofId2,
111 bool safeWrites =
false,
112 const float tanLambdaCut = 0.025f,
113 const float phiCut = 0.005f,
114 const int maxTracklets =
static_cast<int>(1e2))
116 int offset01{0}, offset12{0};
117 for (
unsigned int iCurrentLayerClusterIndex{0}; iCurrentLayerClusterIndex < clusters1.size(); ++iCurrentLayerClusterIndex) {
118 int validTracklets{0};
119 for (
int iTracklet12{offset12}; iTracklet12 < offset12 + foundTracklets12[iCurrentLayerClusterIndex]; ++iTracklet12) {
120 for (
int iTracklet01{offset01}; iTracklet01 < offset01 + foundTracklets01[iCurrentLayerClusterIndex]; ++iTracklet01) {
121 if (usedTracklets[iTracklet01]) {
125 const auto& tracklet01{tracklets01[iTracklet01]};
126 const auto& tracklet12{tracklets12[iTracklet12]};
128 if (tracklet01.rof[0] != targetRofId0 || tracklet12.rof[1] != targetRofId2) {
132 const float deltaTanLambda{o2::gpu::GPUCommonMath::Abs(tracklet01.tanLambda - tracklet12.tanLambda)};
133 const float deltaPhi{o2::gpu::GPUCommonMath::Abs(math_utils::smallestAngleDifference(tracklet01.phi, tracklet12.phi))};
134 if (deltaTanLambda < tanLambdaCut && deltaPhi < phiCut && validTracklets != maxTracklets) {
136 __atomic_store_n(&usedClusters0[tracklet01.firstClusterIndex], 1, __ATOMIC_RELAXED);
137 __atomic_store_n(&usedClusters2[tracklet12.secondClusterIndex], 1, __ATOMIC_RELAXED);
139 usedClusters0[tracklet01.firstClusterIndex] = 1;
140 usedClusters2[tracklet12.secondClusterIndex] = 1;
142 usedTracklets[iTracklet01] =
true;
143 lines.emplace_back(tracklet01, clusters0.data(), clusters1.data());
144 if (!trackletLabels.empty()) {
145 linesLabels.emplace_back(trackletLabels[iTracklet01]);
151 offset01 += foundTracklets01[iCurrentLayerClusterIndex];
152 offset12 += foundTracklets12[iCurrentLayerClusterIndex];
156template <
int nLayers>
160 mIndexTableUtils.setTrackingParameters(vrtPar[0]);
161 for (
auto& par : mVrtParams) {
163 par.zSpan =
static_cast<int>(std::ceil(par.zCut * mIndexTableUtils.getInverseZCoordinate(0)));
168template <
int nLayers>
171 mTaskArena->execute([&] {
172 tbb::parallel_for(0, mTimeFrame->getNrof(), [&](
const short pivotRofId) {
173 bool skipROF = iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold;
174 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
175 short endROF{std::min(
static_cast<short>(mTimeFrame->getNrof()),
static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
176 for (
auto targetRofId = startROF; targetRofId < endROF; ++targetRofId) {
177 trackleterKernelHost<TrackletMode::Layer0Layer1, true>(
178 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 0) : gsl::span<Cluster>(),
179 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
180 mTimeFrame->getUsedClustersROF(targetRofId, 0),
181 mTimeFrame->getIndexTable(targetRofId, 0).data(),
182 mVrtParams[iteration].phiCut,
183 mTimeFrame->getTracklets()[0],
184 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
189 mVrtParams[iteration].maxTrackletsPerCluster);
190 trackleterKernelHost<TrackletMode::Layer1Layer2, true>(
191 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 2) : gsl::span<Cluster>(),
192 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
193 mTimeFrame->getUsedClustersROF(targetRofId, 2),
194 mTimeFrame->getIndexTable(targetRofId, 2).data(),
195 mVrtParams[iteration].phiCut,
196 mTimeFrame->getTracklets()[1],
197 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
202 mVrtParams[iteration].maxTrackletsPerCluster);
204 mTimeFrame->getNTrackletsROF(pivotRofId, 0) = std::accumulate(mTimeFrame->getNTrackletsCluster(pivotRofId, 0).begin(), mTimeFrame->getNTrackletsCluster(pivotRofId, 0).end(), 0);
205 mTimeFrame->getNTrackletsROF(pivotRofId, 1) = std::accumulate(mTimeFrame->getNTrackletsCluster(pivotRofId, 1).begin(), mTimeFrame->getNTrackletsCluster(pivotRofId, 1).end(), 0);
208 mTimeFrame->computeTrackletsPerROFScans();
209 if (
auto tot0 = mTimeFrame->getTotalTrackletsTF(0), tot1 = mTimeFrame->getTotalTrackletsTF(1);
210 tot0 == 0 || tot1 == 0) {
213 mTimeFrame->getTracklets()[0].resize(tot0);
214 mTimeFrame->getTracklets()[1].resize(tot1);
217 tbb::parallel_for(0, mTimeFrame->getNrof(), [&](
const short pivotRofId) {
218 bool skipROF = iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold;
219 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
220 short endROF{std::min(
static_cast<short>(mTimeFrame->getNrof()),
static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
221 auto mobileOffset0 = mTimeFrame->getNTrackletsROF(pivotRofId, 0);
222 auto mobileOffset1 = mTimeFrame->getNTrackletsROF(pivotRofId, 1);
223 for (
auto targetRofId = startROF; targetRofId < endROF; ++targetRofId) {
224 trackleterKernelHost<TrackletMode::Layer0Layer1, false>(
225 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 0) : gsl::span<Cluster>(),
226 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
227 mTimeFrame->getUsedClustersROF(targetRofId, 0),
228 mTimeFrame->getIndexTable(targetRofId, 0).data(),
229 mVrtParams[iteration].phiCut,
230 mTimeFrame->getTracklets()[0],
231 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
235 mTimeFrame->getExclusiveNTrackletsCluster(pivotRofId, 0),
236 mVrtParams[iteration].maxTrackletsPerCluster);
237 trackleterKernelHost<TrackletMode::Layer1Layer2, false>(
238 !skipROF ? mTimeFrame->getClustersOnLayer(targetRofId, 2) : gsl::span<Cluster>(),
239 !skipROF ? mTimeFrame->getClustersOnLayer(pivotRofId, 1) : gsl::span<Cluster>(),
240 mTimeFrame->getUsedClustersROF(targetRofId, 2),
241 mTimeFrame->getIndexTable(targetRofId, 2).data(),
242 mVrtParams[iteration].phiCut,
243 mTimeFrame->getTracklets()[1],
244 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
248 mTimeFrame->getExclusiveNTrackletsCluster(pivotRofId, 1),
249 mVrtParams[iteration].maxTrackletsPerCluster);
255 if (mTimeFrame->hasMCinformation()) {
256 for (
const auto& trk : mTimeFrame->getTracklets()[0]) {
258 if (!trk.isEmpty()) {
259 int sortedId0{mTimeFrame->getSortedIndex(trk.rof[0], 0, trk.firstClusterIndex)};
260 int sortedId1{mTimeFrame->getSortedIndex(trk.rof[1], 1, trk.secondClusterIndex)};
261 for (
const auto& lab0 : mTimeFrame->getClusterLabels(0, mTimeFrame->
getClusters()[0][sortedId0].clusterId)) {
262 for (
const auto& lab1 : mTimeFrame->getClusterLabels(1, mTimeFrame->
getClusters()[1][sortedId1].clusterId)) {
263 if (lab0 == lab1 && lab0.isValid()) {
268 if (
label.isValid()) {
273 mTimeFrame->getTrackletsLabel(0).emplace_back(
label);
278 debugComputeTracklets(iteration);
282template <
int nLayers>
285 mTaskArena->execute([&] {
286 tbb::combinable<int> totalLines{0};
288 tbb::blocked_range<short>(0, (
short)mTimeFrame->getNrof()),
289 [&](
const tbb::blocked_range<short>& Rofs) {
290 for (short pivotRofId = Rofs.begin(); pivotRofId < Rofs.end(); ++pivotRofId) {
291 if (iteration && (int)mTimeFrame->getPrimaryVertices(pivotRofId).size() > mVrtParams[iteration].vertPerRofThreshold) {
294 if (mTimeFrame->getFoundTracklets(pivotRofId, 0).empty()) {
297 mTimeFrame->getLines(pivotRofId).reserve(mTimeFrame->getNTrackletsCluster(pivotRofId, 0).size());
298 bounded_vector<bool> usedTracklets(mTimeFrame->getFoundTracklets(pivotRofId, 0).size(), false, mMemoryPool.get());
299 short startROF{std::max((short)0, static_cast<short>(pivotRofId - mVrtParams[iteration].deltaRof))};
300 short endROF{std::min(static_cast<short>(mTimeFrame->getNrof()), static_cast<short>(pivotRofId + mVrtParams[iteration].deltaRof + 1))};
303 bool safeWrite = mTaskArena->max_concurrency() > 1 && mVrtParams[iteration].deltaRof != 0 && ((Rofs.begin() - startROF < 0) || (endROF - Rofs.end() > 0));
305 for (short targetRofId0 = startROF; targetRofId0 < endROF; ++targetRofId0) {
306 for (short targetRofId2 = startROF; targetRofId2 < endROF; ++targetRofId2) {
307 if (std::abs(targetRofId0 - targetRofId2) > mVrtParams[iteration].deltaRof) {
310 trackletSelectionKernelHost(
311 mTimeFrame->getClustersOnLayer(targetRofId0, 0),
312 mTimeFrame->getClustersOnLayer(pivotRofId, 1),
313 mTimeFrame->getUsedClustersROF(targetRofId0, 0),
314 mTimeFrame->getUsedClustersROF(targetRofId2, 2),
315 mTimeFrame->getFoundTracklets(pivotRofId, 0),
316 mTimeFrame->getFoundTracklets(pivotRofId, 1),
318 mTimeFrame->getNTrackletsCluster(pivotRofId, 0),
319 mTimeFrame->getNTrackletsCluster(pivotRofId, 1),
320 mTimeFrame->getLines(pivotRofId),
321 mTimeFrame->getLabelsFoundTracklets(pivotRofId, 0),
322 mTimeFrame->getLinesLabel(pivotRofId),
326 mVrtParams[iteration].tanLambdaCut,
327 mVrtParams[iteration].phiCut);
330 totalLines.local() += mTimeFrame->getLines(pivotRofId).size();
333 mTimeFrame->setNLinesTotal(totalLines.combine(std::plus<int>()));
337 debugComputeTrackletMatching(iteration);
344template <
int nLayers>
347 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 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
353 if (iteration && (
int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold) {
356 const int numTracklets{
static_cast<int>(mTimeFrame->getLines(rofId).size())};
359 for (
int line1{0}; line1 < numTracklets; ++line1) {
360 if (usedTracklets[line1]) {
363 for (
int line2{line1 + 1}; line2 < numTracklets; ++line2) {
364 if (usedTracklets[line2]) {
367 auto dca{Line::getDCA(mTimeFrame->getLines(rofId)[line1], mTimeFrame->getLines(rofId)[line2])};
368 if (dca < mVrtParams[iteration].pairCut) {
369 mTimeFrame->getTrackletClusters(rofId).emplace_back(line1, mTimeFrame->getLines(rofId)[line1], line2, mTimeFrame->getLines(rofId)[line2]);
370 std::array<float, 3> tmpVertex{mTimeFrame->getTrackletClusters(rofId).back().getVertex()};
371 if (tmpVertex[0] * tmpVertex[0] + tmpVertex[1] * tmpVertex[1] > 4.f) {
372 mTimeFrame->getTrackletClusters(rofId).pop_back();
375 usedTracklets[line1] =
true;
376 usedTracklets[line2] =
true;
377 for (
int tracklet3{0}; tracklet3 < numTracklets; ++tracklet3) {
378 if (usedTracklets[tracklet3]) {
381 if (Line::getDistanceFromPoint(mTimeFrame->getLines(rofId)[tracklet3], tmpVertex) < mVrtParams[iteration].pairCut) {
382 mTimeFrame->getTrackletClusters(rofId).back().add(tracklet3, mTimeFrame->getLines(rofId)[tracklet3]);
383 usedTracklets[tracklet3] =
true;
384 tmpVertex = mTimeFrame->getTrackletClusters(rofId).back().getVertex();
391 if (mVrtParams[iteration].allowSingleContribClusters) {
392 auto beamLine =
Line{{mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), -50.f}, {mTimeFrame->getBeamX(), mTimeFrame->getBeamY(), 50.f}};
393 for (
size_t iLine{0}; iLine < numTracklets; ++iLine) {
394 if (!usedTracklets[iLine]) {
395 auto dca = Line::getDCA(mTimeFrame->getLines(rofId)[iLine], beamLine);
396 if (dca < mVrtParams[iteration].pairCut) {
397 mTimeFrame->getTrackletClusters(rofId).emplace_back(iLine, mTimeFrame->getLines(rofId)[iLine], -1, beamLine);
404 std::sort(mTimeFrame->getTrackletClusters(rofId).begin(), mTimeFrame->getTrackletClusters(rofId).end(),
405 [](ClusterLines& cluster1, ClusterLines& cluster2) { return cluster1.getSize() > cluster2.getSize(); });
406 noClustersVec[rofId] =
static_cast<int>(mTimeFrame->getTrackletClusters(rofId).size());
407 for (
int iCluster1{0}; iCluster1 < noClustersVec[rofId]; ++iCluster1) {
408 std::array<float, 3> vertex1{mTimeFrame->getTrackletClusters(rofId)[iCluster1].getVertex()};
409 std::array<float, 3> vertex2{};
410 for (
int iCluster2{iCluster1 + 1}; iCluster2 < noClustersVec[rofId]; ++iCluster2) {
411 vertex2 = mTimeFrame->getTrackletClusters(rofId)[iCluster2].getVertex();
412 if (o2::gpu::GPUCommonMath::Abs(vertex1[2] - vertex2[2]) < mVrtParams[iteration].clusterCut) {
413 float distance{(vertex1[0] - vertex2[0]) * (vertex1[0] - vertex2[0]) +
414 (vertex1[1] - vertex2[1]) * (vertex1[1] - vertex2[1]) +
415 (vertex1[2] - vertex2[2]) * (vertex1[2] - vertex2[2])};
416 if (
distance < mVrtParams[iteration].pairCut * mVrtParams[iteration].pairCut) {
417 for (
auto label : mTimeFrame->getTrackletClusters(rofId)[iCluster2].getLabels()) {
418 mTimeFrame->getTrackletClusters(rofId)[iCluster1].add(
label, mTimeFrame->getLines(rofId)[
label]);
419 vertex1 = mTimeFrame->getTrackletClusters(rofId)[iCluster1].getVertex();
421 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster2);
423 --noClustersVec[rofId];
429 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
430 std::sort(mTimeFrame->getTrackletClusters(rofId).begin(), mTimeFrame->getTrackletClusters(rofId).end(),
431 [](
const ClusterLines& cluster1,
const ClusterLines& cluster2) { return cluster1.getSize() > cluster2.getSize(); });
432 bool atLeastOneFound{
false};
433 for (
int iCluster{0}; iCluster < noClustersVec[rofId]; ++iCluster) {
434 bool lowMultCandidate{
false};
435 double beamDistance2{(mTimeFrame->getBeamX() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[0]) * (mTimeFrame->getBeamX() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[0]) +
436 (mTimeFrame->getBeamY() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1]) * (mTimeFrame->getBeamY() - mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1])};
437 if (atLeastOneFound && (lowMultCandidate = mTimeFrame->getTrackletClusters(rofId)[iCluster].getSize() < mVrtParams[iteration].clusterContributorsCut)) {
438 lowMultCandidate &= (beamDistance2 < mVrtParams[iteration].lowMultBeamDistCut * mVrtParams[iteration].lowMultBeamDistCut);
439 if (!lowMultCandidate) {
440 mTimeFrame->getTrackletClusters(rofId).erase(mTimeFrame->getTrackletClusters(rofId).begin() + iCluster);
441 noClustersVec[rofId]--;
446 if (beamDistance2 < nsigmaCut && o2::gpu::GPUCommonMath::Abs(mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]) < mVrtParams[iteration].maxZPositionAllowed) {
447 atLeastOneFound =
true;
449 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[1],
450 mTimeFrame->getTrackletClusters(rofId)[iCluster].getVertex()[2]),
451 mTimeFrame->getTrackletClusters(rofId)[iCluster].getRMS2(),
453 mTimeFrame->getTrackletClusters(rofId)[iCluster].getSize(),
454 mTimeFrame->getTrackletClusters(rofId)[iCluster].getAvgDistance2());
457 vertex.setFlags(Vertex::UPCMode);
459 vertex.setTimeStamp(mTimeFrame->getTrackletClusters(rofId)[iCluster].getROF());
460 if (mTimeFrame->hasMCinformation()) {
462 for (
auto&
index : mTimeFrame->getTrackletClusters(rofId)[iCluster].getLabels()) {
463 labels.push_back(mTimeFrame->getLinesLabel(rofId)[
index]);
465 polls.push_back(computeMain(labels));
466 if (mVrtParams[iteration].outputContLabels) {
467 contLabels.insert(contLabels.end(), labels.begin(), labels.end());
473 mTimeFrame->addPrimaryVertices(vertices, iteration);
474 if (mTimeFrame->hasMCinformation()) {
475 mTimeFrame->addPrimaryVerticesLabels(polls);
476 if (mVrtParams[iteration].outputContLabels) {
477 mTimeFrame->addPrimaryVerticesContributorLabels(contLabels);
481 mTimeFrame->addPrimaryVerticesInROF(vertices, rofId, iteration);
482 if (mTimeFrame->hasMCinformation()) {
483 mTimeFrame->addPrimaryVerticesLabelsInROF(polls, rofId);
484 if (mVrtParams[iteration].outputContLabels) {
485 mTimeFrame->addPrimaryVerticesContributorLabelsInROF(contLabels, rofId);
489 if (vertices.empty() && !(iteration && (
int)mTimeFrame->getPrimaryVertices(rofId).size() > mVrtParams[iteration].vertPerRofThreshold)) {
490 mTimeFrame->getNoVertexROF()++;
497 debugComputeVertices(iteration);
501template <
int nLayers>
504 LOGP(info,
"Using truth seeds as vertices; will skip computations");
505 mTimeFrame->resetRofPV();
507 const auto irs = dc->getEventRecords();
516 std::map<int, VertInfo> vertices;
518 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
519 for (
int iEve{0}; iEve < mcReader.
getNEvents(iSrc); ++iEve) {
520 const auto&
ir = irs[eveId2colId[iEve]];
523 int rofId = ((
ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) / roFrameLengthInBC;
524 if (!vertices.contains(rofId)) {
532 vert.setTimeStamp(rofId);
533 vert.setNContributors(std::ranges::count_if(mcReader.
getTracks(iSrc, iEve), [](
const auto& trk) {
534 return trk.isPrimary() && trk.GetPt() > 0.2 && std::abs(trk.GetEta()) < 1.3;
536 vert.setXYZ((
float)eve.GetX(), (
float)eve.GetY(), (
float)eve.GetZ());
538 constexpr float cov = 50e-9;
539 vert.setCov(cov, cov, cov, cov, cov, cov);
540 vertices[rofId].vertices.push_back(vert);
541 vertices[rofId].srcs.push_back(iSrc);
542 vertices[rofId].events.push_back(iEve);
547 for (
int iROF{0}; iROF < mTimeFrame->getNrof(); ++iROF) {
550 if (vertices.contains(iROF)) {
551 const auto& vertInfo = vertices[iROF];
552 verts = vertInfo.vertices;
553 nVerts += verts.size();
554 for (
size_t i{0};
i < verts.size(); ++
i) {
556 polls.emplace_back(lbl, 1.f);
559 mTimeFrame->getNoVertexROF()++;
561 mTimeFrame->addPrimaryVertices(verts, 0);
562 mTimeFrame->addPrimaryVerticesLabels(polls);
564 LOGP(info,
"Found {}/{} ROFs with {} vertices -> <NV>={:.2f}", vertices.size(), mTimeFrame->getNrof(), nVerts, (
float)nVerts / (
float)vertices.size());
567template <
int nLayers>
570#if defined(VTX_DEBUG)
571 LOGP(info,
"Vertexer with debug output forcing single thread");
572 mTaskArena = std::make_shared<tbb::task_arena>(1);
574 if (arena ==
nullptr) {
575 mTaskArena = std::make_shared<tbb::task_arena>(std::abs(
n));
576 LOGP(info,
"Setting seeding vertexer with {} threads.",
n);
579 LOGP(info,
"Attaching vertexer to calling thread's arena");
584template <
int nLayers>
588 LOGP(info,
"writing debug output for computeTracklets");
589 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
590 const auto& strk0 = mTimeFrame->getFoundTracklets(rofId, 0);
591 std::vector<Tracklet> trk0(strk0.begin(), strk0.end());
592 const auto& strk1 = mTimeFrame->getFoundTracklets(rofId, 1);
593 std::vector<Tracklet> trk1(strk1.begin(), strk1.end());
594 (*stream) <<
"tracklets"
595 <<
"Tracklets0=" << trk0
596 <<
"Tracklets1=" << trk1
597 <<
"iteration=" << iteration
604template <
int nLayers>
605void VertexerTraits<nLayers>::debugComputeTrackletMatching(
int iteration)
607 auto stream =
new utils::TreeStreamRedirector(
"artefacts_tf.root",
"update");
608 LOGP(info,
"writing debug output for computeTrackletMatching");
609 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
611 <<
"Lines=" <<
toSTDVector(mTimeFrame->getLines(rofId))
612 <<
"NTrackletCluster01=" << mTimeFrame->getNTrackletsCluster(rofId, 0)
613 <<
"NTrackletCluster12=" << mTimeFrame->getNTrackletsCluster(rofId, 1)
614 <<
"iteration=" << iteration
618 if (mTimeFrame->hasMCinformation()) {
619 LOGP(info,
"\tdumping also MC information");
621 const auto irs = dc->getEventRecords();
626 std::map<int, int> eve2BcInROF, bcInRofNEve;
627 for (
int iSrc{0}; iSrc < mcReader.getNSources(); ++iSrc) {
628 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
629 for (
int iEve{0}; iEve < mcReader.getNEvents(iSrc); ++iEve) {
630 const auto&
ir = irs[eveId2colId[iEve]];
632 const auto& eve = mcReader.getMCEventHeader(iSrc, iEve);
633 const int bcInROF = ((
ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) % roFrameLengthInBC;
634 eve2BcInROF[iEve] = bcInROF;
635 ++bcInRofNEve[bcInROF];
640 std::unordered_map<int, int> bcROFNTracklets01, bcROFNTracklets12;
641 std::vector<std::vector<int>> tracklet01BC, tracklet12BC;
642 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
644 const auto& tracklet01 = mTimeFrame->getFoundTracklets(rofId, 0);
645 const auto& lbls01 = mTimeFrame->getLabelsFoundTracklets(rofId, 0);
646 auto& trkls01 = tracklet01BC.emplace_back();
647 for (
int iTrklt{0}; iTrklt < (
int)tracklet01.size(); ++iTrklt) {
648 const auto& tracklet = tracklet01[iTrklt];
649 const auto& lbl = lbls01[iTrklt];
650 if (lbl.isCorrect()) {
651 ++bcROFNTracklets01[eve2BcInROF[lbl.getEventID()]];
652 trkls01.push_back(eve2BcInROF[lbl.getEventID()]);
654 trkls01.push_back(-1);
659 const auto& tracklet12 = mTimeFrame->getFoundTracklets(rofId, 1);
660 auto& trkls12 = tracklet12BC.emplace_back();
661 for (
int iTrklt{0}; iTrklt < (
int)tracklet12.size(); ++iTrklt) {
662 const auto& tracklet = tracklet12[iTrklt];
665 int sortedId1{mTimeFrame->getSortedIndex(tracklet.rof[0], 1, tracklet.firstClusterIndex)};
666 int sortedId2{mTimeFrame->getSortedIndex(tracklet.rof[1], 2, tracklet.secondClusterIndex)};
667 for (
const auto& lab1 : mTimeFrame->getClusterLabels(1, mTimeFrame->
getClusters()[1][sortedId1].clusterId)) {
668 for (
const auto& lab2 : mTimeFrame->getClusterLabels(2, mTimeFrame->
getClusters()[2][sortedId2].clusterId)) {
669 if (lab1 == lab2 && lab1.isValid()) {
674 if (
label.isValid()) {
679 if (
label.isCorrect()) {
680 ++bcROFNTracklets12[eve2BcInROF[
label.getEventID()]];
681 trkls12.push_back(eve2BcInROF[
label.getEventID()]);
683 trkls12.push_back(-1);
688 LOGP(info,
"\tdumping ntracklets/RofBC ({})", bcInRofNEve.size());
689 for (
const auto& [bcInRof, neve] : bcInRofNEve) {
690 (*stream) <<
"ntracklets"
691 <<
"bcInROF=" << bcInRof
692 <<
"ntrkl01=" << bcROFNTracklets01[bcInRof]
693 <<
"ntrkl12=" << bcROFNTracklets12[bcInRof]
695 <<
"iteration=" << iteration
699 std::unordered_map<int, int> bcROFNLines;
700 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
701 const auto& lines = mTimeFrame->getLines(rofId);
702 const auto& lbls = mTimeFrame->getLinesLabel(rofId);
703 for (
int iLine{0}; iLine < (
int)lines.size(); ++iLine) {
704 const auto&
line = lines[iLine];
705 const auto& lbl = lbls[iLine];
706 if (lbl.isCorrect()) {
707 ++bcROFNLines[eve2BcInROF[lbl.getEventID()]];
712 LOGP(info,
"\tdumping nlines/RofBC");
713 for (
const auto& [bcInRof, neve] : bcInRofNEve) {
714 (*stream) <<
"nlines"
715 <<
"bcInROF=" << bcInRof
716 <<
"nline=" << bcROFNLines[bcInRof]
718 <<
"iteration=" << iteration
726template <
int nLayers>
727void VertexerTraits<nLayers>::debugComputeVertices(
int iteration)
729 auto stream =
new utils::TreeStreamRedirector(
"artefacts_tf.root",
"update");
730 LOGP(info,
"writing debug output for computeVertices");
731 for (
auto rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
732 (*stream) <<
"clusterlines"
733 <<
"clines_post=" <<
toSTDVector(mTimeFrame->getTrackletClusters(rofId))
734 <<
"iteration=" << iteration
738 if (mTimeFrame->hasMCinformation()) {
739 LOGP(info,
"\tdumping also MC information");
741 const auto irs = dc->getEventRecords();
746 std::map<int, int> eve2BcInROF, bcInRofNEve;
747 for (
int iSrc{0}; iSrc < mcReader.getNSources(); ++iSrc) {
748 auto eveId2colId = dc->getCollisionIndicesForSource(iSrc);
749 for (
int iEve{0}; iEve < mcReader.getNEvents(iSrc); ++iEve) {
750 const auto&
ir = irs[eveId2colId[iEve]];
752 const auto& eve = mcReader.getMCEventHeader(iSrc, iEve);
753 const int bcInROF = ((
ir - raw::HBFUtils::Instance().getFirstSampledTFIR()).toLong() - roFrameBiasInBC) % roFrameLengthInBC;
754 eve2BcInROF[iEve] = bcInROF;
755 ++bcInRofNEve[bcInROF];
760 std::unordered_map<int, int> bcROFNVtx;
761 std::unordered_map<int, float> bcROFNPur;
762 std::unordered_map<o2::MCCompLabel, size_t> uniqueVertices;
763 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
764 const auto& pvs = mTimeFrame->getPrimaryVertices(rofId);
765 const auto& lblspv = mTimeFrame->getPrimaryVerticesMCRecInfo(rofId);
766 for (
int i{0};
i < (
int)pvs.size(); ++
i) {
767 const auto& pv = pvs[
i];
768 const auto& [lbl, pur] = lblspv[
i];
769 if (lbl.isCorrect()) {
770 ++uniqueVertices[lbl];
771 ++bcROFNVtx[eve2BcInROF[lbl.getEventID()]];
772 bcROFNPur[eve2BcInROF[lbl.getEventID()]] += pur;
777 std::unordered_map<int, int> bcROFNUVtx, bcROFNCVtx;
778 for (
const auto& [k, _] : eve2BcInROF) {
779 bcROFNUVtx[k] = bcROFNCVtx[k] = 0;
782 for (
const auto& [lbl,
c] : uniqueVertices) {
784 ++bcROFNUVtx[eve2BcInROF[lbl.getEventID()]];
786 ++bcROFNCVtx[eve2BcInROF[lbl.getEventID()]];
790 LOGP(info,
"\tdumping nvtx/RofBC");
791 for (
const auto& [bcInRof, neve] : bcInRofNEve) {
793 <<
"bcInROF=" << bcInRof
794 <<
"nvtx=" << bcROFNVtx[bcInRof]
795 <<
"nuvtx=" << bcROFNUVtx[bcInRof]
796 <<
"ncvtx=" << bcROFNCVtx[bcInRof]
797 <<
"npur=" << bcROFNPur[bcInRof]
799 <<
"iteration=" << iteration
804 std::unordered_map<o2::MCCompLabel, std::vector<Vertex>> cVtx;
805 for (
int rofId{0}; rofId < mTimeFrame->getNrof(); ++rofId) {
806 const auto& pvs = mTimeFrame->getPrimaryVertices(rofId);
807 const auto& lblspv = mTimeFrame->getPrimaryVerticesMCRecInfo(rofId);
808 for (
int i{0};
i < (
int)pvs.size(); ++
i) {
809 const auto& pv = pvs[
i];
810 const auto& [lbl, pur] = lblspv[
i];
811 if (lbl.isCorrect() && uniqueVertices.contains(lbl) && uniqueVertices[lbl] > 1) {
812 if (!cVtx.contains(lbl)) {
813 cVtx[lbl] = std::vector<Vertex>();
815 cVtx[lbl].push_back(pv);
820 for (
auto& [_, vertices] : cVtx) {
821 std::sort(vertices.begin(), vertices.end(), [](
const Vertex&
a,
const Vertex&
b) { return a.getNContributors() > b.getNContributors(); });
822 for (
int i{0};
i < (
int)vertices.size(); ++
i) {
823 const auto vtx = vertices[
i];
827 <<
"dx=" << vertices[0].getX() - vtx.getX()
828 <<
"dy=" << vertices[0].getY() - vtx.getY()
829 <<
"dz=" << vertices[0].getZ() - vtx.getZ()
830 <<
"drof=" << vertices[0].getTimeStamp().getTimeStamp() - vtx.getTimeStamp().getTimeStamp()
831 <<
"dnc=" << vertices[0].getNContributors() - vtx.getNContributors()
832 <<
"iteration=" << iteration
841template 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
void releaseTracksForSourceAndEvent(int source, int event)
API to ask releasing tracks (freeing memory) for source + event.
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