EIC 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 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 
17 
18 #include <memory>
19 
20 #include <boost/range/adaptors.hpp>
21 
22 namespace Acts {
23 
29  public:
41  template <typename source_link_t>
44  size_t entryIndex,
45  LoggerWrapper logger = getDummyLogger()) const {
46  ACTS_VERBOSE("Invoked GainMatrixSmoother on entry index: " << entryIndex);
47  using namespace boost::adaptors;
48 
49  // For the last state: smoothed is filtered - also: switch to next
50  ACTS_VERBOSE("Getting previous track state");
51  auto prev_ts = trajectory.getTrackState(entryIndex);
52 
53  prev_ts.smoothed() = prev_ts.filtered();
54  prev_ts.smoothedCovariance() = prev_ts.filteredCovariance();
55 
56  // Smoothing gain matrix
58 
59  // make sure there is more than one track state
60  std::optional<std::error_code> error{std::nullopt}; // assume ok
61  if (prev_ts.previous() == Acts::detail_lt::IndexData::kInvalid) {
62  ACTS_VERBOSE("Only one track state given, smoothing terminates early");
63  } else {
64  ACTS_VERBOSE("Start smoothing from previous track state at index: "
65  << prev_ts.previous());
66 
67  trajectory.applyBackwards(prev_ts.previous(), [&prev_ts, &G, &error,
68  &logger](auto ts) {
69  // should have filtered and predicted, this should also include the
70  // covariances.
71  assert(ts.hasFiltered());
72  assert(ts.hasPredicted());
73  assert(ts.hasJacobian());
74 
75  // previous trackstate should have smoothed and predicted
76  assert(prev_ts.hasSmoothed());
77  assert(prev_ts.hasPredicted());
78 
79  ACTS_VERBOSE("Calculate smoothing matrix:");
80  ACTS_VERBOSE("Filtered covariance:\n" << ts.filteredCovariance());
81  ACTS_VERBOSE("Jacobian:\n" << ts.jacobian());
82  ACTS_VERBOSE("Prev. predicted covariance\n"
83  << prev_ts.predictedCovariance() << "\n, inverse: \n"
84  << prev_ts.predictedCovariance().inverse());
85 
86  // Gain smoothing matrix
87  // NB: The jacobian stored in a state is the jacobian from previous
88  // state to this state in forward propagation
89  G = ts.filteredCovariance() * prev_ts.jacobian().transpose() *
90  prev_ts.predictedCovariance().inverse();
91 
92  if (G.hasNaN()) {
93  error = KalmanFitterError::SmoothFailed; // set to error
94  return false; // abort execution
95  }
96 
97  ACTS_VERBOSE("Gain smoothing matrix G:\n" << G);
98 
99  ACTS_VERBOSE("Calculate smoothed parameters:");
100  ACTS_VERBOSE("Filtered parameters: " << ts.filtered().transpose());
101  ACTS_VERBOSE(
102  "Prev. smoothed parameters: " << prev_ts.smoothed().transpose());
103  ACTS_VERBOSE(
104  "Prev. predicted parameters: " << prev_ts.predicted().transpose());
105 
106  // Calculate the smoothed parameters
107  ts.smoothed() =
108  ts.filtered() + G * (prev_ts.smoothed() - prev_ts.predicted());
109 
110  ACTS_VERBOSE("Smoothed parameters are: " << ts.smoothed().transpose());
111 
112  ACTS_VERBOSE("Calculate smoothed covariance:");
113  ACTS_VERBOSE("Prev. smoothed covariance:\n"
114  << prev_ts.smoothedCovariance());
115 
116  // And the smoothed covariance
117  ts.smoothedCovariance() =
118  ts.filteredCovariance() -
119  G * (prev_ts.predictedCovariance() - prev_ts.smoothedCovariance()) *
120  G.transpose();
121 
122  // Check if the covariance matrix is semi-positive definite.
123  // If not, make one (could do more) attempt to replace it with the
124  // nearest semi-positive def matrix,
125  // but it could still be non semi-positive
126  BoundSymMatrix smoothedCov = ts.smoothedCovariance();
128  smoothedCov)) {
129  ACTS_DEBUG(
130  "Smoothed covariance is not positive definite. Could result in "
131  "negative covariance!");
132  }
133  // Reset smoothed covariance
134  ts.smoothedCovariance() = smoothedCov;
135  ACTS_VERBOSE("Smoothed covariance is: \n" << ts.smoothedCovariance());
136 
137  prev_ts = ts;
138  return true; // continue execution
139  });
140  }
141  if (error) {
142  // error is set, return result
143  return *error;
144  }
145 
146  // construct parameters from last track state
147  return Result<void>::success();
148  }
149 };
150 } // namespace Acts