Belle II Software  release-08-01-10
KalmanStepper.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/ckf/general/utilities/EigenHelper.h>
11 
12 #include <genfit/MeasuredStateOnPlane.h>
13 #include <genfit/MeasurementOnPlane.h>
14 
15 #include <Eigen/Dense>
16 
17 
18 namespace Belle2 {
33  template <unsigned int Dimension>
34  class KalmanStepper {
36  using MeasurementState = Eigen::Matrix<double, Dimension, 1>;
38  using MeasurementCovariance = Eigen::Matrix<double, Dimension, Dimension>;
40  using HMatrix = Eigen::Matrix<double, Dimension, 5>;
42  using TrackState = Eigen::Matrix<double, 5, 1>;
44  using TrackCovariance = Eigen::Matrix<double, 5, 5>;
45 
46  public:
48  double kalmanStep(genfit::MeasuredStateOnPlane& measuredStateOnPlane,
49  const genfit::MeasurementOnPlane& measurementOnPlane) const
50  {
51  // Extract matrices from the state
52  TrackState x_k = convertToEigen<5>(measuredStateOnPlane.getState());
53  TrackCovariance C_k = convertToEigen<5, 5>(measuredStateOnPlane.getCov());
54 
55  // extract matrices from the measurement
56  const MeasurementState& m_k = convertToEigen<Dimension>(measurementOnPlane.getState());
57  const HMatrix& H_k = convertToEigen<Dimension, 5>(measurementOnPlane.getHMatrix()->getMatrix());
58  const MeasurementCovariance& V_k = convertToEigen<Dimension, Dimension>(measurementOnPlane.getCov());
59 
60  const double chi2 = kalmanStep(x_k, C_k, m_k, V_k, H_k);
61 
62  // set the state back
63  measuredStateOnPlane.setState(TVectorD(5, x_k.data()));
64  measuredStateOnPlane.setCov(TMatrixDSym(5, C_k.data()));
65 
66  return chi2;
67  }
68 
70  double calculateResidual(const genfit::MeasuredStateOnPlane& measuredStateOnPlane,
71  const genfit::MeasurementOnPlane& measurementOnPlane) const
72  {
73  // Extract matrices from the state
74  const TrackState& x_k = convertToEigen<5>(measuredStateOnPlane.getState());
75 
76  // extract matrices from the measurement
77  const MeasurementState& m_k = convertToEigen<Dimension>(measurementOnPlane.getState());
78  const HMatrix& H_k = convertToEigen<Dimension, 5>(measurementOnPlane.getHMatrix()->getMatrix());
79 
80  const MeasurementState& residual = m_k - H_k * x_k;
81  return residual.norm();
82  }
83 
84  private:
87  const MeasurementState& m_k,
88  const MeasurementCovariance& V_k,
89  const HMatrix& H_k) const
90  {
91  const Eigen::Matrix<double, 5, Dimension>& K_k = C_k * H_k.transpose() * (V_k + H_k * C_k * H_k.transpose()).inverse();
92  C_k -= K_k * H_k * C_k;
93  x_k += K_k * (m_k - H_k * x_k);
94 
95  const MeasurementState& residual = m_k - H_k * x_k;
96  const double chi2 = (residual.transpose() * (V_k - H_k * C_k * H_k.transpose()).inverse() * residual).value();
97  return chi2;
98  }
99  };
101 }
Class to bundle all algorithms needed for the kalman update procedure.
Definition: KalmanStepper.h:34
double kalmanStep(genfit::MeasuredStateOnPlane &measuredStateOnPlane, const genfit::MeasurementOnPlane &measurementOnPlane) const
Kalman update of the mSoP using the measurement. Is just a wrapper around the other kalmanStepper wor...
Definition: KalmanStepper.h:48
Eigen::Matrix< double, Dimension, Dimension > MeasurementCovariance
Matrix class Dimension x Dimension.
Definition: KalmanStepper.h:38
Eigen::Matrix< double, Dimension, 1 > MeasurementState
Matrix class Dimension x 1.
Definition: KalmanStepper.h:36
double calculateResidual(const genfit::MeasuredStateOnPlane &measuredStateOnPlane, const genfit::MeasurementOnPlane &measurementOnPlane) const
Helper function to calculate a residual between the mSoP and the measurement.
Definition: KalmanStepper.h:70
Eigen::Matrix< double, Dimension, 5 > HMatrix
Matrix class Dimension x 5.
Definition: KalmanStepper.h:40
Eigen::Matrix< double, 5, 1 > TrackState
Matrix class 5 x 1.
Definition: KalmanStepper.h:42
Eigen::Matrix< double, 5, 5 > TrackCovariance
Matrix class 5 x 5.
Definition: KalmanStepper.h:44
double kalmanStep(TrackState &x_k, TrackCovariance &C_k, const MeasurementState &m_k, const MeasurementCovariance &V_k, const HMatrix &H_k) const
This now is the real "update" step, where we update the X_k and the C_k.
Definition: KalmanStepper.h:86
virtual const TMatrixD & getMatrix() const =0
Get the actual matrix representation.
#StateOnPlane with additional covariance matrix.
Measured coordinates on a plane.
Abstract base class for different kinds of events.