ECCE @ 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 
9 #include <bitset>
10 #include <cstdint>
11 #include <type_traits>
12 #include <vector>
13 
14 #include <Eigen/Core>
15 
18 
19 namespace Acts {
20 namespace detail_lt {
21 template <typename SL, size_t N, size_t M, bool ReadOnly>
23  ConstIf<MultiTrajectory<SL>, ReadOnly>& trajectory, size_t istate)
24  : m_traj(&trajectory), m_istate(istate) {}
25 
26 template <typename SL, size_t N, size_t M, bool ReadOnly>
28  -> Parameters {
30  if (hasSmoothed()) {
31  idx = data().ismoothed;
32  } else if (hasFiltered()) {
33  idx = data().ifiltered;
34  } else {
35  idx = data().ipredicted;
36  }
37 
38  return Parameters(m_traj->m_params.data.col(idx).data());
39 }
40 
41 template <typename SL, size_t N, size_t M, bool ReadOnly>
43  -> Covariance {
45  if (hasSmoothed()) {
46  idx = data().ismoothed;
47  } else if (hasFiltered()) {
48  idx = data().ifiltered;
49  } else {
50  idx = data().ipredicted;
51  }
52  return Covariance(m_traj->m_cov.data.col(idx).data());
53 }
54 
55 template <typename SL, size_t N, size_t M, bool ReadOnly>
57  -> Parameters {
58  assert(data().ipredicted != IndexData::kInvalid);
59  return Parameters(m_traj->m_params.col(data().ipredicted).data());
60 }
61 
62 template <typename SL, size_t N, size_t M, bool ReadOnly>
64  -> Covariance {
65  assert(data().ipredicted != IndexData::kInvalid);
66  return Covariance(m_traj->m_cov.col(data().ipredicted).data());
67 }
68 
69 template <typename SL, size_t N, size_t M, bool ReadOnly>
71  const Acts::GeometryContext& gctx) const {
72  return {gctx, predictedCovariance(), predicted(),
73  m_traj->m_referenceSurfaces[data().irefsurface]};
74 }
75 
76 template <typename SL, size_t N, size_t M, bool ReadOnly>
78  -> Parameters {
79  assert(data().ifiltered != IndexData::kInvalid);
80  return Parameters(m_traj->m_params.col(data().ifiltered).data());
81 }
82 
83 template <typename SL, size_t N, size_t M, bool ReadOnly>
85  -> Covariance {
86  assert(data().ifiltered != IndexData::kInvalid);
87  return Covariance(m_traj->m_cov.col(data().ifiltered).data());
88 }
89 
90 template <typename SL, size_t N, size_t M, bool ReadOnly>
92  const Acts::GeometryContext& gctx) const {
93  return {gctx, filteredCovariance(), filtered(),
94  m_traj->m_referenceSurfaces[data().irefsurface]};
95 }
96 
97 template <typename SL, size_t N, size_t M, bool ReadOnly>
99  -> Parameters {
100  assert(data().ismoothed != IndexData::kInvalid);
101  return Parameters(m_traj->m_params.col(data().ismoothed).data());
102 }
103 
104 template <typename SL, size_t N, size_t M, bool ReadOnly>
106  -> Covariance {
107  assert(data().ismoothed != IndexData::kInvalid);
108  return Covariance(m_traj->m_cov.col(data().ismoothed).data());
109 }
110 
111 template <typename SL, size_t N, size_t M, bool ReadOnly>
113  const Acts::GeometryContext& gctx) const {
114  return {gctx, smoothedCovariance(), smoothed(),
115  m_traj->m_referenceSurfaces[data().irefsurface]};
116 }
117 
118 template <typename SL, size_t N, size_t M, bool ReadOnly>
120  -> Covariance {
121  assert(data().ijacobian != IndexData::kInvalid);
122  return Covariance(m_traj->m_jac.col(data().ijacobian).data());
123 }
124 
125 template <typename SL, size_t N, size_t M, bool ReadOnly>
127  -> Projector {
128  assert(data().iprojector != IndexData::kInvalid);
129  return bitsetToMatrix<Projector>(m_traj->m_projectors[data().iprojector]);
130 }
131 
132 template <typename SL, size_t N, size_t M, bool ReadOnly>
134  -> const SourceLink& {
135  assert(data().iuncalibrated != IndexData::kInvalid);
136  return m_traj->m_sourceLinks[data().iuncalibrated];
137 }
138 
139 template <typename SL, size_t N, size_t M, bool ReadOnly>
141  -> Measurement {
142  assert(data().icalibrated != IndexData::kInvalid);
143  return Measurement(m_traj->m_meas.col(data().icalibrated).data());
144 }
145 
146 template <typename SL, size_t N, size_t M, bool ReadOnly>
148  -> const SourceLink& {
149  assert(data().icalibratedsourcelink != IndexData::kInvalid);
150  return m_traj->m_sourceLinks[data().icalibratedsourcelink];
151 }
152 
153 template <typename SL, size_t N, size_t M, bool ReadOnly>
156  assert(data().icalibrated != IndexData::kInvalid);
157  return MeasurementCovariance(
158  m_traj->m_measCov.col(data().icalibrated).data());
159 }
160 
161 } // namespace detail_lt
162 
163 template <typename SL>
164 template <typename parameters_t>
166  const TrackState<SL, parameters_t>& ts, size_t iprevious) {
167  using CovMap =
169 
170  // use a TrackStateProxy to do the assignments
171  m_index.emplace_back();
172  detail_lt::IndexData& p = m_index.back();
173  size_t index = m_index.size() - 1;
174 
175  TrackStateProxy nts = getTrackState(index);
176 
177  // make shared ownership held by this multi trajectory
178  m_referenceSurfaces.push_back(ts.referenceSurface().getSharedPtr());
179  p.irefsurface = m_referenceSurfaces.size() - 1;
180 
181  if (iprevious != SIZE_MAX) {
182  p.iprevious = static_cast<uint16_t>(iprevious);
183  }
184 
185  if (ts.parameter.predicted) {
186  const auto& predicted = *ts.parameter.predicted;
187  m_params.addCol() = predicted.parameters();
188  CovMap(m_cov.addCol().data()) = *predicted.covariance();
189  p.ipredicted = m_params.size() - 1;
190  }
191 
192  if (ts.parameter.filtered) {
193  const auto& filtered = *ts.parameter.filtered;
194  m_params.addCol() = filtered.parameters();
195  CovMap(m_cov.addCol().data()) = *filtered.covariance();
196  p.ifiltered = m_params.size() - 1;
197  }
198 
199  if (ts.parameter.smoothed) {
200  const auto& smoothed = *ts.parameter.smoothed;
201  m_params.addCol() = smoothed.parameters();
202  CovMap(m_cov.addCol().data()) = *smoothed.covariance();
203  p.ismoothed = m_params.size() - 1;
204  }
205 
206  // store jacobian
207  if (ts.parameter.jacobian) {
208  CovMap(m_jac.addCol().data()) = *ts.parameter.jacobian;
209  p.ijacobian = m_jac.size() - 1;
210  }
211 
212  // handle measurements
213  if (ts.measurement.uncalibrated) {
214  m_sourceLinks.push_back(*ts.measurement.uncalibrated);
215  p.iuncalibrated = m_sourceLinks.size() - 1;
216  }
217 
218  if (ts.measurement.calibrated) {
219  std::visit([&](const auto& m) { nts.resetCalibrated(m); },
220  *ts.measurement.calibrated);
221  }
222 
223  nts.chi2() = ts.parameter.chi2;
224  nts.pathLength() = ts.parameter.pathLength;
225  nts.typeFlags() = ts.type();
226 
227  return index;
228 }
229 
230 template <typename SL>
232  const TrackStatePropMask::Type& mask, size_t iprevious) {
233  namespace PropMask = TrackStatePropMask;
234 
235  m_index.emplace_back();
236  detail_lt::IndexData& p = m_index.back();
237  size_t index = m_index.size() - 1;
238 
239  if (iprevious != SIZE_MAX) {
240  p.iprevious = static_cast<uint16_t>(iprevious);
241  }
242 
243  // always set, but can be null
244  m_referenceSurfaces.emplace_back(nullptr);
245  p.irefsurface = m_referenceSurfaces.size() - 1;
246 
247  if (ACTS_CHECK_BIT(mask, PropMask::Predicted)) {
248  m_params.addCol();
249  m_cov.addCol();
250  p.ipredicted = m_params.size() - 1;
251  }
252 
253  if (ACTS_CHECK_BIT(mask, PropMask::Filtered)) {
254  m_params.addCol();
255  m_cov.addCol();
256  p.ifiltered = m_params.size() - 1;
257  }
258 
259  if (ACTS_CHECK_BIT(mask, PropMask::Smoothed)) {
260  m_params.addCol();
261  m_cov.addCol();
262  p.ismoothed = m_params.size() - 1;
263  }
264 
265  if (ACTS_CHECK_BIT(mask, PropMask::Jacobian)) {
266  m_jac.addCol();
267  p.ijacobian = m_jac.size() - 1;
268  }
269 
271  m_sourceLinks.emplace_back();
272  p.iuncalibrated = m_sourceLinks.size() - 1;
273  }
274 
276  m_meas.addCol();
277  m_measCov.addCol();
278  p.icalibrated = m_meas.size() - 1;
279 
280  m_sourceLinks.emplace_back();
281  p.icalibratedsourcelink = m_sourceLinks.size() - 1;
282 
283  m_projectors.emplace_back();
284  p.iprojector = m_projectors.size() - 1;
285  }
286 
287  return index;
288 }
289 
290 template <typename SL>
291 template <typename F>
292 void MultiTrajectory<SL>::visitBackwards(size_t iendpoint, F&& callable) const {
293  static_assert(detail_lt::VisitorConcept<F, ConstTrackStateProxy>,
294  "Callable needs to satisfy VisitorConcept");
295 
296  while (true) {
297  if constexpr (std::is_same_v<std::invoke_result_t<F, ConstTrackStateProxy>,
298  bool>) {
299  bool proceed = callable(getTrackState(iendpoint));
300  // this point has no parent and ends the trajectory, or a break was
301  // requested
302  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid ||
303  !proceed) {
304  break;
305  }
306  } else {
307  callable(getTrackState(iendpoint));
308  // this point has no parent and ends the trajectory
309  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid) {
310  break;
311  }
312  }
313  iendpoint = m_index[iendpoint].iprevious;
314  }
315 }
316 
317 template <typename SL>
318 template <typename F>
319 void MultiTrajectory<SL>::applyBackwards(size_t iendpoint, F&& callable) {
320  static_assert(detail_lt::VisitorConcept<F, TrackStateProxy>,
321  "Callable needs to satisfy VisitorConcept");
322 
323  while (true) {
324  if constexpr (std::is_same_v<std::invoke_result_t<F, TrackStateProxy>,
325  bool>) {
326  bool proceed = callable(getTrackState(iendpoint));
327  // this point has no parent and ends the trajectory, or a break was
328  // requested
329  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid ||
330  !proceed) {
331  break;
332  }
333  } else {
334  callable(getTrackState(iendpoint));
335  // this point has no parent and ends the trajectory
336  if (m_index[iendpoint].iprevious == detail_lt::IndexData::kInvalid) {
337  break;
338  }
339  }
340  iendpoint = m_index[iendpoint].iprevious;
341  }
342 }
343 } // namespace Acts