ECCE @ 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 
11 #include <memory>
12 #include <variant>
22 
23 namespace Acts {
24 
30 template <typename parameters_t>
32  public:
39  std::shared_ptr<const Logger> logger = std::shared_ptr<const Logger>(
40  getDefaultLogger("GainMatrixUpdater", Logging::INFO).release()))
41  : m_logger(std::move(logger)) {}
42 
54  template <typename track_state_t>
56  const GeometryContext& /*gctx*/, track_state_t trackState,
57  const NavigationDirection& direction = forward) const {
58  ACTS_VERBOSE("Invoked GainMatrixUpdater");
59  // let's make sure the types are consistent
60  using SourceLink = typename track_state_t::SourceLink;
61  using TrackStateProxy =
63  static_assert(std::is_same_v<track_state_t, TrackStateProxy>,
64  "Given track state type is not a track state proxy");
65 
66  using CovMatrix_t = typename parameters_t::CovMatrix_t;
67  using ParVector_t = typename parameters_t::ParVector_t;
68 
69  // we should definitely have an uncalibrated measurement here
70  assert(trackState.hasUncalibrated());
71  // there should be a calibrated measurement
72  assert(trackState.hasCalibrated());
73  // we should have predicted state set
74  assert(trackState.hasPredicted());
75  // filtering should not have happened yet, but is allocated, therefore set
76  assert(trackState.hasFiltered());
77 
78  // read-only handles. Types are eigen maps to backing storage
79  const auto predicted = trackState.predicted();
80  const auto predicted_covariance = trackState.predictedCovariance();
81 
82  ACTS_VERBOSE("Predicted parameters: " << predicted.transpose());
83  ACTS_VERBOSE("Predicted covariance:\n" << predicted_covariance);
84 
85  // read-write handles. Types are eigen maps into backing storage.
86  // This writes directly into the trajectory storage
87  auto filtered = trackState.filtered();
88  auto filtered_covariance = trackState.filteredCovariance();
89 
90  std::optional<std::error_code> error{std::nullopt}; // assume ok
92  trackState.calibrated(), trackState.calibratedCovariance(),
93  trackState.calibratedSize(),
94  [&](const auto calibrated, const auto calibrated_covariance) {
95  constexpr size_t measdim = decltype(calibrated)::RowsAtCompileTime;
98 
99  ACTS_VERBOSE("Measurement dimension: " << measdim);
100  ACTS_VERBOSE("Calibrated measurement: " << calibrated.transpose());
101  ACTS_VERBOSE("Calibrated measurement covariance:\n"
102  << calibrated_covariance);
103 
105  trackState.projector()
106  .template topLeftCorner<measdim, eBoundParametersSize>();
107 
108  ACTS_VERBOSE("Measurement projector H:\n" << H);
109 
111  predicted_covariance * H.transpose() *
112  (H * predicted_covariance * H.transpose() + calibrated_covariance)
113  .inverse();
114 
115  ACTS_VERBOSE("Gain Matrix K:\n" << K);
116 
117  if (K.hasNaN()) {
118  error =
119  (direction == forward)
120  ? KalmanFitterError::ForwardUpdateFailed
121  : KalmanFitterError::BackwardUpdateFailed; // set to error
122  return false; // abort execution
123  }
124 
125  filtered = predicted + K * (calibrated - H * predicted);
126  filtered_covariance =
129  K * H) *
130  predicted_covariance;
131  ACTS_VERBOSE("Filtered parameters: " << filtered.transpose());
132  ACTS_VERBOSE("Filtered covariance:\n" << filtered_covariance);
133 
134  // calculate filtered residual
135  par_t residual(trackState.calibratedSize());
136  residual = (calibrated - H * filtered);
137  ACTS_VERBOSE("Residual: " << residual.transpose());
138 
139  trackState.chi2() =
140  (residual.transpose() *
141  ((cov_t::Identity() - H * K) * calibrated_covariance).inverse() *
142  residual)
143  .value();
144 
145  ACTS_VERBOSE("Chi2: " << trackState.chi2());
146  return true; // continue execution
147  });
148 
149  if (error) {
150  // error is set, return result
151  return *error;
152  }
153 
154  // always succeed, no outlier logic yet
155  return Result<void>::success();
156  }
157 
159  std::shared_ptr<const Logger> m_logger{nullptr};
160 
162  const Logger& logger() const {
163  assert(m_logger);
164  return *m_logger;
165  }
166 };
167 
168 } // namespace Acts