ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TruthFitTrack.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TruthFitTrack.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 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 
9 #pragma once
10 
11 #include <boost/none.hpp>
12 #include <boost/optional.hpp>
13 #include <optional>
14 #include <utility>
15 
20 
21 namespace FW {
22 
27 struct TruthFitTrack {
28  public:
29  // Default constructor
30  TruthFitTrack() = default;
31 
36  TruthFitTrack(size_t tTip,
38  : m_trajectory(trajectory), m_trackTip(tTip) {}
39 
44  : m_trackParameters(parameter) {}
45 
51  TruthFitTrack(size_t tTip,
53  const Acts::BoundParameters& parameter)
54  : m_trajectory(trajectory),
55  m_trackTip(tTip),
56  m_trackParameters(parameter) {}
57 
59  const std::pair<size_t, Acts::MultiTrajectory<SimSourceLink>> trajectory()
60  const {
61  if (m_trajectory) {
62  return std::make_pair(m_trackTip, *m_trajectory);
63  } else {
64  throw std::runtime_error("No fitted states on this trajectory!");
65  };
66  }
67 
70  if (m_trackParameters) {
71  return *m_trackParameters;
72  } else {
73  throw std::runtime_error(
74  "No fitted track parameter for this trajectory!");
75  }
76  }
77 
79  size_t numStates() const {
80  size_t nStates = 0;
81  if (m_trajectory) {
82  (*m_trajectory).visitBackwards(m_trackTip, [&](const auto&) {
83  nStates++;
84  });
85  }
86  return nStates;
87  }
88 
90  size_t numMeasurements() const {
91  size_t nMeasurements = 0;
92  if (m_trajectory) {
93  (*m_trajectory).visitBackwards(m_trackTip, [&](const auto& state) {
94  if (state.typeFlags().test(Acts::TrackStateFlag::MeasurementFlag)) {
95  nMeasurements++;
96  }
97  });
98  }
99  return nMeasurements;
100  }
101 
103  bool hasTrajectory() const { return m_trajectory ? true : false; }
104 
106  bool hasTrackParameters() const { return m_trackParameters ? true : false; }
107 
109  std::vector<ParticleHitCount> identifyMajorityParticle() const {
110  std::vector<ParticleHitCount> particleHitCount;
111 
112  if (m_trajectory) {
113  (*m_trajectory).visitBackwards(m_trackTip, [&](const auto& state) {
114  // No truth info with non-measurement state
115  if (not state.typeFlags().test(Acts::TrackStateFlag::MeasurementFlag)) {
116  return true;
117  }
118  // Find the truth particle associated with this state
119  const auto particleId = state.uncalibrated().truthHit().particleId();
120  // Find if the particle already exists
121  auto it = std::find_if(particleHitCount.begin(), particleHitCount.end(),
122  [=](const ParticleHitCount& phc) {
123  return phc.particleId == particleId;
124  });
125 
126  // Either increase count if we saw the particle before or add it
127  if (it != particleHitCount.end()) {
128  it->hitCount += 1;
129  } else {
130  particleHitCount.push_back({particleId, 1u});
131  }
132  return true;
133  });
134  }
135 
136  if (not particleHitCount.empty()) {
137  // sort by hit count, i.e. majority particle first
138  std::sort(particleHitCount.begin(), particleHitCount.end(),
139  [](const ParticleHitCount& lhs, const ParticleHitCount& rhs) {
140  return lhs.hitCount > rhs.hitCount;
141  });
142  }
143 
144  return particleHitCount;
145  }
146 
147  private:
148  // The optional fitted multitrajectory
149  std::optional<Acts::MultiTrajectory<SimSourceLink>> m_trajectory;
150 
151  // This is the index of the 'tip' of the track stored in multitrajectory.
152  size_t m_trackTip = SIZE_MAX;
153 
154  // The optional Parameters at the provided surface
155  std::optional<Acts::BoundParameters> m_trackParameters;
156 };
157 
158 } // namespace FW