EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GainMatrixUpdater.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GainMatrixUpdater.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2018 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 
20 
21 #include <memory>
22 #include <variant>
23 
24 namespace Acts {
25 
28  public:
40  template <typename track_state_t>
42  track_state_t trackState,
43  const NavigationDirection& direction = forward,
44  LoggerWrapper logger = getDummyLogger()) const {
45  ACTS_VERBOSE("Invoked GainMatrixUpdater");
46  // let's make sure the types are consistent
47  using SourceLink = typename track_state_t::SourceLink;
48  using TrackStateProxy =
50  static_assert(std::is_same_v<track_state_t, TrackStateProxy>,
51  "Given track state type is not a track state proxy");
52 
53  // we should definitely have an uncalibrated measurement here
54  assert(trackState.hasUncalibrated());
55  // there should be a calibrated measurement
56  assert(trackState.hasCalibrated());
57  // we should have predicted state set
58  assert(trackState.hasPredicted());
59  // filtering should not have happened yet, but is allocated, therefore set
60  assert(trackState.hasFiltered());
61 
62  // read-only handles. Types are eigen maps to backing storage
63  const auto predicted = trackState.predicted();
64  const auto predicted_covariance = trackState.predictedCovariance();
65 
66  ACTS_VERBOSE("Predicted parameters: " << predicted.transpose());
67  ACTS_VERBOSE("Predicted covariance:\n" << predicted_covariance);
68 
69  // read-write handles. Types are eigen maps into backing storage.
70  // This writes directly into the trajectory storage
71  auto filtered = trackState.filtered();
72  auto filtered_covariance = trackState.filteredCovariance();
73 
74  std::optional<std::error_code> error{std::nullopt}; // assume ok
76  trackState.calibrated(), trackState.calibratedCovariance(),
77  trackState.calibratedSize(),
78  [&](const auto calibrated, const auto calibrated_covariance) {
79  constexpr size_t measdim = decltype(calibrated)::RowsAtCompileTime;
82 
83  ACTS_VERBOSE("Measurement dimension: " << measdim);
84  ACTS_VERBOSE("Calibrated measurement: " << calibrated.transpose());
85  ACTS_VERBOSE("Calibrated measurement covariance:\n"
86  << calibrated_covariance);
87 
89  trackState.projector()
90  .template topLeftCorner<measdim, eBoundSize>();
91 
92  ACTS_VERBOSE("Measurement projector H:\n" << H);
93 
95  predicted_covariance * H.transpose() *
96  (H * predicted_covariance * H.transpose() + calibrated_covariance)
97  .inverse();
98 
99  ACTS_VERBOSE("Gain Matrix K:\n" << K);
100 
101  if (K.hasNaN()) {
102  error =
103  (direction == forward)
104  ? KalmanFitterError::ForwardUpdateFailed
105  : KalmanFitterError::BackwardUpdateFailed; // set to error
106  return false; // abort execution
107  }
108 
109  filtered = predicted + K * (calibrated - H * predicted);
110  filtered_covariance =
111  (BoundSymMatrix::Identity() - K * H) * predicted_covariance;
112  ACTS_VERBOSE("Filtered parameters: " << filtered.transpose());
113  ACTS_VERBOSE("Filtered covariance:\n" << filtered_covariance);
114 
115  // calculate filtered residual
116  //
117  // FIXME: Without separate residual construction and assignment, we
118  // currently take a +0.7GB build memory consumption hit in the
119  // EventDataView unit tests. Revisit this once Measurement
120  // overhead problems (Acts issue #350) are sorted out.
121  //
122  par_t residual;
123  residual = calibrated - H * filtered;
124  ACTS_VERBOSE("Residual: " << residual.transpose());
125 
126  trackState.chi2() =
127  (residual.transpose() *
128  ((cov_t::Identity() - H * K) * calibrated_covariance).inverse() *
129  residual)
130  .value();
131 
132  ACTS_VERBOSE("Chi2: " << trackState.chi2());
133  return true; // continue execution
134  });
135 
136  if (error) {
137  // error is set, return result
138  return *error;
139  }
140 
141  // always succeed, no outlier logic yet
142  return Result<void>::success();
143  }
144 };
145 
146 } // namespace Acts