Analysis Software
Documentation for sPHENIX simulation 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 
16 
17 #include <iterator>
18 #include <ostream>
19 #include <stdexcept>
20 #include <utility>
21 
22 namespace ActsExamples {
23 struct AlgorithmContext;
24 } // namespace ActsExamples
25 
28  : IAlgorithm("TruthVertexFinder", level), m_cfg(config) {
29  if (m_cfg.inputParticles.empty()) {
30  throw std::invalid_argument("Missing input truth particles collection");
31  }
32  if (m_cfg.outputProtoVertices.empty()) {
33  throw std::invalid_argument("Missing output proto vertices collection");
34  }
35 
38 }
39 
41  const AlgorithmContext& ctx) const {
42  // prepare input and output collections
43  ACTS_VERBOSE("Reading particles from " << m_cfg.inputParticles);
44  const auto& particles = m_inputParticles(ctx);
45  ProtoVertexContainer protoVertices;
46  ACTS_VERBOSE("Have " << particles.size() << " particles");
47 
48  // assumes the begin/end iterator references the particles container
49  auto addProtoVertex = [&](SimParticleContainer::const_iterator begin,
50  const SimParticleContainer::const_iterator& end) {
51  ProtoVertex protoVertex;
52  protoVertex.reserve(std::distance(begin, end));
53  // determine each particle index
54  for (; begin != end; ++begin) {
55  protoVertex.push_back(std::distance(particles.begin(), begin));
56  }
57  protoVertices.push_back(std::move(protoVertex));
58  };
59 
60  if (m_cfg.excludeSecondaries) {
61  // if secondaries are excluded, the `separateSecondaries` flag has no effect
62  // since there will be no secondary vertices to separate
63  for (auto&& [vtxId, vtxParticles] : groupBySecondaryVertex(particles)) {
64  if (vtxId.vertexSecondary() != 0u) {
65  continue;
66  }
67  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
68  }
69  } else {
70  // particles from secondary vertices should be included
71  if (m_cfg.separateSecondaries) {
72  // secondary particles are added to separate secondary vertices
73  for (auto&& [vtxId, vtxParticles] : groupBySecondaryVertex(particles)) {
74  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
75  }
76  } else {
77  // secondary particles are included in the primary vertex
78  for (auto&& [vtxId, vtxParticles] : groupByPrimaryVertex(particles)) {
79  addProtoVertex(vtxParticles.begin(), vtxParticles.end());
80  }
81  }
82  }
83 
84  ACTS_VERBOSE("Write " << protoVertices.size() << " proto vertex to "
85  << m_cfg.outputProtoVertices);
86  m_outputProtoVertices(ctx, std::move(protoVertices));
87  return ProcessCode::SUCCESS;
88 }