EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultiTrajectory.ipp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MultiTrajectory.ipp
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 
10 
11 #include <bitset>
12 #include <cstdint>
13 #include <type_traits>
14 #include <vector>
15 
16 #include <Eigen/Core>
17 
18 namespace Acts {
19 namespace detail_lt {
20 template <typename SL, size_t M, bool ReadOnly>
22  ConstIf<MultiTrajectory<SL>, ReadOnly>& trajectory, size_t istate)
23  : m_traj(&trajectory), m_istate(istate) {}
24 
25 template <typename SL, size_t M, bool ReadOnly>
27  using PM = TrackStatePropMask;
28  PM mask = PM::None;
29  if (hasPredicted()) {
30  mask |= PM::Predicted;
31  }
32 
33  if (hasFiltered()) {
34  mask |= PM::Filtered;
35  }
36  if (hasSmoothed()) {
37  mask |= PM::Smoothed;
38  }
39  if (hasJacobian()) {
40  mask |= PM::Jacobian;
41  }
42  if (hasUncalibrated()) {
43  mask |= PM::Uncalibrated;
44  }
45  if (hasCalibrated()) {
46  mask |= PM::Calibrated;
47  }
48 
49  return mask;
50 }
51 
52 template <typename SL, size_t M, bool ReadOnly>
55  if (hasSmoothed()) {
56  idx = data().ismoothed;
57  } else if (hasFiltered()) {
58  idx = data().ifiltered;
59  } else {
60  idx = data().ipredicted;
61  }
62 
63  return Parameters(m_traj->m_params.data.col(idx).data());
64 }
65 
66 template <typename SL, size_t M, bool ReadOnly>
69  if (hasSmoothed()) {
70  idx = data().ismoothed;
71  } else if (hasFiltered()) {
72  idx = data().ifiltered;
73  } else {
74  idx = data().ipredicted;
75  }
76  return Covariance(m_traj->m_cov.data.col(idx).data());
77 }
78 
79 template <typename SL, size_t M, bool ReadOnly>
81  assert(data().ipredicted != IndexData::kInvalid);
82  return Parameters(m_traj->m_params.col(data().ipredicted).data());
83 }
84 
85 template <typename SL, size_t M, bool ReadOnly>
87  -> Covariance {
88  assert(data().ipredicted != IndexData::kInvalid);
89  return Covariance(m_traj->m_cov.col(data().ipredicted).data());
90 }
91 
92 template <typename SL, size_t M, bool ReadOnly>
94  assert(data().ifiltered != IndexData::kInvalid);
95  return Parameters(m_traj->m_params.col(data().ifiltered).data());
96 }
97 
98 template <typename SL, size_t M, bool ReadOnly>
100  -> Covariance {
101  assert(data().ifiltered != IndexData::kInvalid);
102  return Covariance(m_traj->m_cov.col(data().ifiltered).data());
103 }
104 
105 template <typename SL, size_t M, bool ReadOnly>
107  assert(data().ismoothed != IndexData::kInvalid);
108  return Parameters(m_traj->m_params.col(data().ismoothed).data());
109 }
110 
111 template <typename SL, size_t M, bool ReadOnly>
113  -> Covariance {
114  assert(data().ismoothed != IndexData::kInvalid);
115  return Covariance(m_traj->m_cov.col(data().ismoothed).data());
116 }
117 
118 template <typename SL, size_t M, bool ReadOnly>
120  assert(data().ijacobian != IndexData::kInvalid);
121  return Covariance(m_traj->m_jac.col(data().ijacobian).data());
122 }
123 
124 template <typename SL, size_t M, bool ReadOnly>
126  assert(data().iprojector != IndexData::kInvalid);
127  return bitsetToMatrix<Projector>(m_traj->m_projectors[data().iprojector]);
128 }
129 
130 template <typename SL, size_t M, bool ReadOnly>
132  -> const SourceLink& {
133  assert(data().iuncalibrated != IndexData::kInvalid);
134  return m_traj->m_sourceLinks[data().iuncalibrated];
135 }
136 
137 template <typename SL, size_t M, bool ReadOnly>
139  -> Measurement {
140  assert(data().icalibrated != IndexData::kInvalid);
141  return Measurement(m_traj->m_meas.col(data().icalibrated).data());
142 }
143 
144 template <typename SL, size_t M, bool ReadOnly>
146  -> const SourceLink& {
147  assert(data().icalibratedsourcelink != IndexData::kInvalid);
148  return m_traj->m_sourceLinks[data().icalibratedsourcelink];
149 }
150 
151 template <typename SL, size_t M, bool ReadOnly>
154  assert(data().icalibrated != IndexData::kInvalid);
155  return MeasurementCovariance(
156  m_traj->m_measCov.col(data().icalibrated).data());
157 }
158 
159 } // namespace detail_lt
160 
161 template <typename SL>
163  size_t iprevious) {
164  using PropMask = TrackStatePropMask;
165 
166  m_index.emplace_back();
167  detail_lt::IndexData& p = m_index.back();
168  size_t index = m_index.size() - 1;
169 
170  if (iprevious != SIZE_MAX) {
171  p.iprevious = static_cast<uint16_t>(iprevious);
172  }
173 
174  // always set, but can be null
175  m_referenceSurfaces.emplace_back(nullptr);
176  p.irefsurface = m_referenceSurfaces.size() - 1;
177 
178  if (ACTS_CHECK_BIT(mask, PropMask::Predicted)) {
179  m_params.addCol();
180  m_cov.addCol();
181  p.ipredicted = m_params.size() - 1;
182  }
183 
184  if (ACTS_CHECK_BIT(mask, PropMask::Filtered)) {
185  m_params.addCol();
186  m_cov.addCol();
187  p.ifiltered = m_params.size() - 1;
188  }
189 
190  if (ACTS_CHECK_BIT(mask, PropMask::Smoothed)) {
191  m_params.addCol();
192  m_cov.addCol();
193  p.ismoothed = m_params.size() - 1;
194  }
195 
196  if (ACTS_CHECK_BIT(mask, PropMask::Jacobian)) {
197  m_jac.addCol();
198  p.ijacobian = m_jac.size() - 1;
199  }
200 
201  if (ACTS_CHECK_BIT(mask, PropMask::Uncalibrated)) {
202  m_sourceLinks.emplace_back();
203  p.iuncalibrated = m_sourceLinks.size() - 1;
204  }
205 
206  if (ACTS_CHECK_BIT(mask, PropMask::Calibrated)) {
207  m_meas.addCol();
208  m_measCov.addCol();
209  p.icalibrated = m_meas.size() - 1;
210 
211  m_sourceLinks.emplace_back();
212  p.icalibratedsourcelink = m_sourceLinks.size() - 1;
213 
214  m_projectors.emplace_back();
215  p.iprojector = m_projectors.size() - 1;
216  }
217 
218  return index;
219 }
220 
221 template <typename SL>
222 template <typename F>
223 void MultiTrajectory<SL>::visitBackwards(size_t iendpoint, F&& callable) const {
224  static_assert(detail_lt::VisitorConcept<F, ConstTrackStateProxy>,
225  "Callable needs to satisfy VisitorConcept");
226 
227  while (true) {
228  if constexpr (std::is_same_v<std::invoke_result_t<F, ConstTrackStateProxy>,
229  bool>) {
230  bool proceed = callable(getTrackState(iendpoint));
231  // this point has no parent and ends the trajectory, or a break was
232  // requested
233  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid ||
234  !proceed) {
235  break;
236  }
237  } else {
238  callable(getTrackState(iendpoint));
239  // this point has no parent and ends the trajectory
240  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid) {
241  break;
242  }
243  }
244  iendpoint = m_index[iendpoint].iprevious;
245  }
246 }
247 
248 template <typename SL>
249 template <typename F>
250 void MultiTrajectory<SL>::applyBackwards(size_t iendpoint, F&& callable) {
251  static_assert(detail_lt::VisitorConcept<F, TrackStateProxy>,
252  "Callable needs to satisfy VisitorConcept");
253 
254  while (true) {
255  if constexpr (std::is_same_v<std::invoke_result_t<F, TrackStateProxy>,
256  bool>) {
257  bool proceed = callable(getTrackState(iendpoint));
258  // this point has no parent and ends the trajectory, or a break was
259  // requested
260  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid ||
261  !proceed) {
262  break;
263  }
264  } else {
265  callable(getTrackState(iendpoint));
266  // this point has no parent and ends the trajectory
267  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid) {
268  break;
269  }
270  }
271  iendpoint = m_index[iendpoint].iprevious;
272  }
273 }
274 } // namespace Acts