Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GainMatrixSmoother.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GainMatrixSmoother.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 
14 
15 #include <algorithm>
16 #include <ostream>
17 #include <utility>
18 
19 namespace Acts {
20 
22  void* ts, void* prev_ts, const GetParameters& filtered,
23  const GetCovariance& filteredCovariance, const GetParameters& smoothed,
24  const GetParameters& predicted, const GetCovariance& predictedCovariance,
25  const GetCovariance& smoothedCovariance, const GetCovariance& jacobian,
26  const Logger& logger) const {
27  ACTS_VERBOSE("Prev. predicted covariance\n"
28  << predictedCovariance(prev_ts) << "\n, inverse: \n"
29  << predictedCovariance(prev_ts).inverse());
30 
31  // Gain smoothing matrix
32  // NB: The jacobian stored in a state is the jacobian from previous
33  // state to this state in forward propagation
34  BoundMatrix G = filteredCovariance(ts) * jacobian(prev_ts).transpose() *
35  predictedCovariance(prev_ts).inverse();
36 
37  if (G.hasNaN()) {
38  // error = KalmanFitterError::SmoothFailed; // set to error
39  // return false; // abort execution
40  return KalmanFitterError::SmoothFailed;
41  }
42 
43  ACTS_VERBOSE("Gain smoothing matrix G:\n" << G);
44 
45  ACTS_VERBOSE("Calculate smoothed parameters:");
46  ACTS_VERBOSE("Filtered parameters: " << filtered(ts).transpose());
47  ACTS_VERBOSE("Prev. smoothed parameters: " << smoothed(prev_ts).transpose());
49  "Prev. predicted parameters: " << predicted(prev_ts).transpose());
50 
51  // Calculate the smoothed parameters
52  smoothed(ts) = filtered(ts) + G * (smoothed(prev_ts) - predicted(prev_ts));
53 
54  ACTS_VERBOSE("Smoothed parameters are: " << smoothed(ts).transpose());
55  ACTS_VERBOSE("Calculate smoothed covariance:");
56  ACTS_VERBOSE("Prev. smoothed covariance:\n" << smoothedCovariance(prev_ts));
57 
58  // And the smoothed covariance
59  smoothedCovariance(ts) =
60  filteredCovariance(ts) +
61  G * (smoothedCovariance(prev_ts) - predictedCovariance(prev_ts)) *
62  G.transpose();
63 
64  // Check if the covariance matrix is semi-positive definite.
65  // If not, make one (could do more) attempt to replace it with the
66  // nearest semi-positive def matrix,
67  // but it could still be non semi-positive
68  BoundSquareMatrix smoothedCov = smoothedCovariance(ts);
70  ACTS_DEBUG(
71  "Smoothed covariance is not positive definite. Could result in "
72  "negative covariance!");
73  }
74  // Reset smoothed covariance
75  smoothedCovariance(ts) = smoothedCov;
76  ACTS_VERBOSE("Smoothed covariance is: \n" << smoothedCovariance(ts));
77 
78  return Result<void>::success();
79 }
80 } // namespace Acts