Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GainMatrixSmoother.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GainMatrixSmoother.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018-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 
9 #pragma once
10 
18 
19 #include <cassert>
20 #include <cstddef>
21 #include <system_error>
22 
23 namespace Acts {
24 
31  public:
38  template <typename traj_t>
40  size_t entryIndex,
41  const Logger& logger = getDummyLogger()) const {
42  (void)gctx;
43 
44  using TrackStateProxy = typename traj_t::TrackStateProxy;
45 
46  GetParameters filtered;
47  GetCovariance filteredCovariance;
48  GetParameters smoothed;
49  GetParameters predicted;
50  GetCovariance predictedCovariance;
51  GetCovariance smoothedCovariance;
52  GetCovariance jacobian;
53 
54  filtered.connect([](const void*, void* ts) {
55  return static_cast<TrackStateProxy*>(ts)->filtered();
56  });
57  filteredCovariance.connect([](const void*, void* ts) {
58  return static_cast<TrackStateProxy*>(ts)->filteredCovariance();
59  });
60 
61  smoothed.connect([](const void*, void* ts) {
62  return static_cast<TrackStateProxy*>(ts)->smoothed();
63  });
64  smoothedCovariance.connect([](const void*, void* ts) {
65  return static_cast<TrackStateProxy*>(ts)->smoothedCovariance();
66  });
67 
68  predicted.connect([](const void*, void* ts) {
69  return static_cast<TrackStateProxy*>(ts)->predicted();
70  });
71  predictedCovariance.connect([](const void*, void* ts) {
72  return static_cast<TrackStateProxy*>(ts)->predictedCovariance();
73  });
74 
75  jacobian.connect([](const void*, void* ts) {
76  return static_cast<TrackStateProxy*>(ts)->jacobian();
77  });
78 
79  ACTS_VERBOSE("Invoked GainMatrixSmoother on entry index: " << entryIndex);
80 
81  // For the last state: smoothed is filtered - also: switch to next
82  ACTS_VERBOSE("Getting previous track state");
83  auto prev_ts = trajectory.getTrackState(entryIndex);
84 
85  prev_ts.smoothed() = prev_ts.filtered();
86  prev_ts.smoothedCovariance() = prev_ts.filteredCovariance();
87 
88  // make sure there is more than one track state
89  if (!prev_ts.hasPrevious()) {
90  ACTS_VERBOSE("Only one track state given, smoothing terminates early");
91  return Result<void>::success();
92  }
93 
94  ACTS_VERBOSE("Start smoothing from previous track state at index: "
95  << prev_ts.previous());
96 
97  // default-constructed error represents success, i.e. an invalid error code
98  std::error_code error;
99  trajectory.applyBackwards(prev_ts.previous(), [&, this](auto ts) {
100  // should have filtered and predicted, this should also include the
101  // covariances.
102  assert(ts.hasFiltered());
103  assert(ts.hasPredicted());
104 
105  // previous trackstate should have smoothed and predicted
106  assert(prev_ts.hasSmoothed());
107  assert(prev_ts.hasPredicted());
108  assert(prev_ts.hasJacobian());
109 
110  ACTS_VERBOSE("Calculate smoothing matrix:");
111  ACTS_VERBOSE("Filtered covariance:\n" << ts.filteredCovariance());
112  ACTS_VERBOSE("Jacobian:\n" << prev_ts.jacobian());
113 
114  if (auto res = calculate(&ts, &prev_ts, filtered, filteredCovariance,
115  smoothed, predicted, predictedCovariance,
116  smoothedCovariance, jacobian, logger);
117  !res.ok()) {
118  error = res.error();
119  return false;
120  }
121 
122  prev_ts = ts;
123  return true; // continue execution
124  });
125 
126  return error ? Result<void>::failure(error) : Result<void>::success();
127  }
128 
129  using GetParameters =
131  false>::Parameters(void*)>;
132  using GetCovariance =
134  false>::Covariance(void*)>;
135 
136  Result<void> calculate(void* ts, void* prev_ts, const GetParameters& filtered,
137  const GetCovariance& filteredCovariance,
138  const GetParameters& smoothed,
139  const GetParameters& predicted,
140  const GetCovariance& predictedCovariance,
141  const GetCovariance& smoothedCovariance,
142  const GetCovariance& jacobian,
143  const Logger& logger) const;
144 };
145 
146 } // namespace Acts