Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GainMatrixUpdater.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GainMatrixUpdater.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
15 
16 #include <algorithm>
17 #include <cstddef>
18 #include <utility>
19 
20 #include <Eigen/src/Core/MatrixBase.h>
21 
22 namespace Acts {
23 
24 std::tuple<double, std::error_code> GainMatrixUpdater::visitMeasurement(
25  InternalTrackState trackState, Direction direction,
26  const Logger& logger) const {
27  // default-constructed error represents success, i.e. an invalid error code
28  std::error_code error;
29  double chi2 = 0;
30 
31  visit_measurement(trackState.calibratedSize, [&](auto N) -> bool {
32  constexpr size_t kMeasurementSize = decltype(N)::value;
33  using ParametersVector = ActsVector<kMeasurementSize>;
34  using CovarianceMatrix = ActsSquareMatrix<kMeasurementSize>;
35 
37  trackState.calibrated};
39  calibratedCovariance{trackState.calibratedCovariance};
40 
41  ACTS_VERBOSE("Measurement dimension: " << kMeasurementSize);
42  ACTS_VERBOSE("Calibrated measurement: " << calibrated.transpose());
43  ACTS_VERBOSE("Calibrated measurement covariance:\n"
44  << calibratedCovariance);
45 
46  const auto H = trackState.projector
47  .template topLeftCorner<kMeasurementSize, eBoundSize>()
48  .eval();
49 
50  ACTS_VERBOSE("Measurement projector H:\n" << H);
51 
52  const auto K = (trackState.predictedCovariance * H.transpose() *
53  (H * trackState.predictedCovariance * H.transpose() +
54  calibratedCovariance)
55  .inverse())
56  .eval();
57 
58  ACTS_VERBOSE("Gain Matrix K:\n" << K);
59 
60  if (K.hasNaN()) {
61  error = (direction == Direction::Forward)
62  ? KalmanFitterError::ForwardUpdateFailed
63  : KalmanFitterError::BackwardUpdateFailed; // set to error
64  return false; // abort execution
65  }
66 
67  trackState.filtered =
68  trackState.predicted + K * (calibrated - H * trackState.predicted);
69  trackState.filteredCovariance = (BoundSquareMatrix::Identity() - K * H) *
70  trackState.predictedCovariance;
71  ACTS_VERBOSE("Filtered parameters: " << trackState.filtered.transpose());
72  ACTS_VERBOSE("Filtered covariance:\n" << trackState.filteredCovariance);
73 
74  // calculate filtered residual
75  //
76  // FIXME: Without separate residual construction and assignment, we
77  // currently take a +0.7GB build memory consumption hit in the
78  // EventDataView unit tests. Revisit this once Measurement
79  // overhead problems (Acts issue #350) are sorted out.
80  //
81  ParametersVector residual;
82  residual = calibrated - H * trackState.filtered;
83  ACTS_VERBOSE("Residual: " << residual.transpose());
84 
85  CovarianceMatrix m =
86  ((CovarianceMatrix::Identity() - H * K) * calibratedCovariance).eval();
87 
88  chi2 = (residual.transpose() * m.inverse() * residual).value();
89 
90  ACTS_VERBOSE("Chi2: " << chi2);
91  return true; // continue execution
92  });
93 
94  return {chi2, error};
95 }
96 
97 } // namespace Acts