Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TruthSeedingAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TruthSeedingAlgorithm.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
17 
18 #include <algorithm>
19 #include <array>
20 #include <cmath>
21 #include <cstddef>
22 #include <limits>
23 #include <ostream>
24 #include <stdexcept>
25 #include <unordered_map>
26 #include <utility>
27 
28 namespace ActsExamples {
29 struct AlgorithmContext;
30 } // namespace ActsExamples
31 
34  : ActsExamples::IAlgorithm("TruthSeedingAlgorithm", lvl),
35  m_cfg(std::move(cfg)) {
36  if (m_cfg.inputParticles.empty()) {
37  throw std::invalid_argument("Missing input truth particles collection");
38  }
39  if (m_cfg.inputMeasurementParticlesMap.empty()) {
40  throw std::invalid_argument("Missing input hit-particles map collection");
41  }
42  if (m_cfg.inputSpacePoints.empty()) {
43  throw std::invalid_argument("Missing seeds or space point collection");
44  }
45 
46  for (const auto& spName : m_cfg.inputSpacePoints) {
47  if (spName.empty()) {
48  throw std::invalid_argument("Invalid space point input collection");
49  }
50 
51  auto& handle = m_inputSpacePoints.emplace_back(
53  this,
54  "InputSpacePoints#" + std::to_string(m_inputSpacePoints.size())));
55  handle->initialize(spName);
56  }
57 
58  if (m_cfg.outputParticles.empty()) {
59  throw std::invalid_argument("Missing output particles collection");
60  }
61  if (m_cfg.outputSeeds.empty()) {
62  throw std::invalid_argument("Missing seeds output collections");
63  }
64  if (m_cfg.outputProtoTracks.empty()) {
65  throw std::invalid_argument("Missing proto tracks output collections");
66  }
67 
73 }
74 
76  const ActsExamples::AlgorithmContext& ctx) const {
77  // prepare input collections
78  const auto& particles = m_inputParticles(ctx);
79  const auto& hitParticlesMap = m_inputMeasurementParticlesMap(ctx);
80  // compute particle_id -> {hit_id...} map from the
81  // hit_id -> {particle_id...} map on the fly.
82  const auto& particleHitsMap = invertIndexMultimap(hitParticlesMap);
83 
84  // construct the combined input container of space point pointers from all
85  // configured input sources.
86  // pre-compute the total size required so we only need to allocate once
87  size_t nSpacePoints = 0;
88  for (const auto& isp : m_inputSpacePoints) {
89  nSpacePoints += (*isp)(ctx).size();
90  }
91 
92  std::vector<const SimSpacePoint*> spacePointPtrs;
93  spacePointPtrs.reserve(nSpacePoints);
94  for (const auto& isp : m_inputSpacePoints) {
95  for (const auto& spacePoint : (*isp)(ctx)) {
96  // since the event store owns the space points, their pointers should be
97  // stable and we do not need to create local copies.
98  spacePointPtrs.push_back(&spacePoint);
99  }
100  }
101 
102  SimParticleContainer seededParticles;
103  SimSeedContainer seeds;
105 
106  seededParticles.reserve(particles.size());
107  seeds.reserve(particles.size());
108  tracks.reserve(particles.size());
109 
110  std::unordered_map<Index, const SimSpacePoint*> spMap;
111 
112  for (const auto& spp : spacePointPtrs) {
113  if (spp->sourceLinks().empty()) {
114  ACTS_WARNING("Missing source link in space point");
115  continue;
116  }
117  for (const auto& slink : spp->sourceLinks()) {
118  const IndexSourceLink& islink = slink.get<IndexSourceLink>();
119  spMap.emplace(islink.index(), spp);
120  }
121  }
122 
123  for (const auto& particle : particles) {
124  // find the corresponding hits for this particle
125  const auto& hits =
126  makeRange(particleHitsMap.equal_range(particle.particleId()));
127  // fill hit indices to create the proto track
128  ProtoTrack track;
129  track.reserve(hits.size());
130  for (const auto& hit : hits) {
131  track.push_back(hit.second);
132  }
133 
134  // The list of hits and the initial start parameters
135  if (track.size() < 3) {
136  ACTS_WARNING("Particle " << particle << " has less than 3 hits");
137  continue;
138  }
139  // Space points on the proto track
140  std::vector<const SimSpacePoint*> spacePointsOnTrack;
141  spacePointsOnTrack.reserve(track.size());
142  // Loop over the hit index on the proto track to find the space points
143  for (const auto& hitIndex : track) {
144  auto it = spMap.find(hitIndex);
145  if (it != spMap.end()) {
146  spacePointsOnTrack.push_back(it->second);
147  }
148  }
149  // At least three space points are required
150  if (spacePointsOnTrack.size() < 3) {
151  continue;
152  }
153  // Sort the space points
154  std::sort(spacePointsOnTrack.begin(), spacePointsOnTrack.end(),
155  [](const SimSpacePoint* lhs, const SimSpacePoint* rhs) {
156  return std::hypot(lhs->r(), lhs->z()) <
157  std::hypot(rhs->r(), rhs->z());
158  });
159 
160  // Loop over the found space points to find the seed with maximum deltaR
161  // between the bottom and top space point
162  // @todo add the check of deltaZ
163  bool seedFound = false;
164  std::array<size_t, 3> bestSPIndices{};
165  double maxDeltaR = std::numeric_limits<double>::min();
166  for (size_t ib = 0; ib < spacePointsOnTrack.size() - 2; ++ib) {
167  for (size_t im = ib + 1; im < spacePointsOnTrack.size() - 1; ++im) {
168  for (size_t it = im + 1; it < spacePointsOnTrack.size(); ++it) {
169  double bmDeltaR = std::abs(spacePointsOnTrack[im]->r() -
170  spacePointsOnTrack[ib]->r());
171  double mtDeltaR = std::abs(spacePointsOnTrack[it]->r() -
172  spacePointsOnTrack[im]->r());
173  if (bmDeltaR >= m_cfg.deltaRMin and bmDeltaR <= m_cfg.deltaRMax and
174  mtDeltaR >= m_cfg.deltaRMin and mtDeltaR <= m_cfg.deltaRMax) {
175  if ((bmDeltaR + mtDeltaR) > maxDeltaR) {
176  maxDeltaR = bmDeltaR + mtDeltaR;
177  bestSPIndices = {ib, im, it};
178  seedFound = true;
179  }
180  }
181  }
182  }
183  }
184 
185  if (seedFound) {
186  SimSeed seed{
187  *spacePointsOnTrack[bestSPIndices[0]],
188  *spacePointsOnTrack[bestSPIndices[1]],
189  *spacePointsOnTrack[bestSPIndices[2]],
190  static_cast<float>(spacePointsOnTrack[bestSPIndices[1]]->z())};
191 
192  seededParticles.insert(particle);
193  seeds.emplace_back(std::move(seed));
194  tracks.emplace_back(std::move(track));
195  }
196  }
197 
198  ACTS_VERBOSE("Found " << seeds.size() << " seeds");
199 
200  m_outputParticles(ctx, std::move(seededParticles));
201  m_outputProtoTracks(ctx, std::move(tracks));
202  m_outputSeeds(ctx, std::move(seeds));
203 
205 }