25 #include <unordered_map>
28 namespace ActsExamples {
29 struct AlgorithmContext;
34 : ActsExamples::
IAlgorithm(
"TruthSeedingAlgorithm", lvl),
37 throw std::invalid_argument(
"Missing input truth particles collection");
40 throw std::invalid_argument(
"Missing input hit-particles map collection");
43 throw std::invalid_argument(
"Missing seeds or space point collection");
48 throw std::invalid_argument(
"Invalid space point input collection");
55 handle->initialize(spName);
59 throw std::invalid_argument(
"Missing output particles collection");
62 throw std::invalid_argument(
"Missing seeds output collections");
65 throw std::invalid_argument(
"Missing proto tracks output collections");
78 const auto&
particles = m_inputParticles(ctx);
79 const auto& hitParticlesMap = m_inputMeasurementParticlesMap(ctx);
87 size_t nSpacePoints = 0;
88 for (
const auto& isp : m_inputSpacePoints) {
89 nSpacePoints += (*isp)(ctx).
size();
92 std::vector<const SimSpacePoint*> spacePointPtrs;
93 spacePointPtrs.reserve(nSpacePoints);
94 for (
const auto& isp : m_inputSpacePoints) {
95 for (
const auto& spacePoint : (*isp)(ctx)) {
98 spacePointPtrs.push_back(&spacePoint);
106 seededParticles.reserve(
particles.size());
110 std::unordered_map<Index, const SimSpacePoint*> spMap;
112 for (
const auto& spp : spacePointPtrs) {
113 if (spp->sourceLinks().empty()) {
117 for (
const auto& slink : spp->sourceLinks()) {
119 spMap.emplace(islink.
index(), spp);
129 track.reserve(hits.size());
130 for (
const auto& hit : hits) {
131 track.push_back(hit.second);
135 if (track.size() < 3) {
140 std::vector<const SimSpacePoint*> spacePointsOnTrack;
141 spacePointsOnTrack.reserve(track.size());
143 for (
const auto& hitIndex : track) {
144 auto it = spMap.find(hitIndex);
145 if (
it != spMap.end()) {
146 spacePointsOnTrack.push_back(
it->second);
150 if (spacePointsOnTrack.size() < 3) {
154 std::sort(spacePointsOnTrack.begin(), spacePointsOnTrack.end(),
156 return std::hypot(lhs->
r(), lhs->
z()) <
157 std::hypot(
rhs->r(),
rhs->z());
163 bool seedFound =
false;
164 std::array<size_t, 3> bestSPIndices{};
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};
187 *spacePointsOnTrack[bestSPIndices[0]],
188 *spacePointsOnTrack[bestSPIndices[1]],
189 *spacePointsOnTrack[bestSPIndices[2]],
190 static_cast<float>(spacePointsOnTrack[bestSPIndices[1]]->z())};
200 m_outputParticles(ctx,
std::move(seededParticles));
201 m_outputProtoTracks(ctx,
std::move(tracks));