EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TruthVertexFinder.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TruthVertexFinder.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 
15 
16 #include <algorithm>
17 #include <stdexcept>
18 #include <vector>
19 
22  : BareAlgorithm("TruthVertexFinder", lvl), m_cfg(cfg) {
23  if (m_cfg.inputParticles.empty()) {
24  throw std::invalid_argument("Missing input truth particles collection");
25  }
26  if (m_cfg.outputProtoVertices.empty()) {
27  throw std::invalid_argument("Missing output proto vertices collection");
28  }
29 }
30 
32  const AlgorithmContext& ctx) const {
33  // prepare input and output collections
34  const auto& particles =
35  ctx.eventStore.get<SimParticleContainer>(m_cfg.inputParticles);
36  ProtoVertexContainer protoVertices;
37 
38  // assumes the begin/end iterator references the particles container
39  auto addProtoVertex = [&](SimParticleContainer::const_iterator begin,
40  SimParticleContainer::const_iterator end) {
41  ProtoVertex protoVertex;
42  protoVertex.reserve(std::distance(begin, end));
43  // determine each particle index
44  for (; begin != end; ++begin) {
45  protoVertex.push_back(std::distance(particles.begin(), begin));
46  }
47  protoVertices.push_back(std::move(protoVertex));
48  };
49 
50  if (m_cfg.excludeSecondaries) {
51  // if secondaries are excluded, the `separateSecondaries` flag has no effect
52  // since there will be no secondary vertices to separate
53  for (auto&& [vtxId, vtxParticles] : groupBySecondaryVertex(particles)) {
54  if (vtxId.vertexSecondary() != 0u) {
55  continue;
56  }
57  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
58  }
59  } else {
60  // particles from secondary vertices should be included
61  if (m_cfg.separateSecondaries) {
62  // secondary particles are added to separate secondary vertices
63  for (auto&& [vtxId, vtxParticles] : groupBySecondaryVertex(particles)) {
64  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
65  }
66  } else {
67  // secondary particles are included in the primary vertex
68  for (auto&& [vtxId, vtxParticles] : groupByPrimaryVertex(particles)) {
69  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
70  }
71  }
72  }
73 
74  ctx.eventStore.add(m_cfg.outputProtoVertices, std::move(protoVertices));
75  return ProcessCode::SUCCESS;
76 }