Belle II Software  release-05-01-25
KalmanStepper.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/ckf/general/utilities/EigenHelper.h>
13 
14 #include <genfit/MeasuredStateOnPlane.h>
15 #include <genfit/MeasurementOnPlane.h>
16 
17 #include <Eigen/Dense>
18 
19 
20 namespace Belle2 {
35  template <unsigned int Dimension>
36  class KalmanStepper {
38  using MeasurementState = Eigen::Matrix<double, Dimension, 1>;
40  using MeasurementCovariance = Eigen::Matrix<double, Dimension, Dimension>;
42  using HMatrix = Eigen::Matrix<double, Dimension, 5>;
44  using TrackState = Eigen::Matrix<double, 5, 1>;
46  using TrackCovariance = Eigen::Matrix<double, 5, 5>;
47 
48  public:
50  double kalmanStep(genfit::MeasuredStateOnPlane& measuredStateOnPlane,
51  const genfit::MeasurementOnPlane& measurementOnPlane) const
52  {
53  // Extract matrices from the state
54  TrackState x_k = convertToEigen<5>(measuredStateOnPlane.getState());
55  TrackCovariance C_k = convertToEigen<5, 5>(measuredStateOnPlane.getCov());
56 
57  // extract matrices from the measurement
58  const MeasurementState& m_k = convertToEigen<Dimension>(measurementOnPlane.getState());
59  const HMatrix& H_k = convertToEigen<Dimension, 5>(measurementOnPlane.getHMatrix()->getMatrix());
60  const MeasurementCovariance& V_k = convertToEigen<Dimension, Dimension>(measurementOnPlane.getCov());
61 
62  const double chi2 = kalmanStep(x_k, C_k, m_k, V_k, H_k);
63 
64  // set the state back
65  measuredStateOnPlane.setState(TVectorD(5, x_k.data()));
66  measuredStateOnPlane.setCov(TMatrixDSym(5, C_k.data()));
67 
68  return chi2;
69  }
70 
72  double calculateResidual(genfit::MeasuredStateOnPlane& measuredStateOnPlane,
73  const genfit::MeasurementOnPlane& measurementOnPlane) const
74  {
75  // Extract matrices from the state
76  const TrackState& x_k = convertToEigen<5>(measuredStateOnPlane.getState());
77 
78  // extract matrices from the measurement
79  const MeasurementState& m_k = convertToEigen<Dimension>(measurementOnPlane.getState());
80  const HMatrix& H_k = convertToEigen<Dimension, 5>(measurementOnPlane.getHMatrix()->getMatrix());
81 
82  const MeasurementState& residual = m_k - H_k * x_k;
83  return residual.norm();
84  }
85 
86  private:
88  double kalmanStep(TrackState& x_k, TrackCovariance& C_k,
89  const MeasurementState& m_k,
90  const MeasurementCovariance& V_k,
91  const HMatrix& H_k) const
92  {
93  const Eigen::Matrix<double, 5, Dimension>& K_k = C_k * H_k.transpose() * (V_k + H_k * C_k * H_k.transpose()).inverse();
94  C_k -= K_k * H_k * C_k;
95  x_k += K_k * (m_k - H_k * x_k);
96 
97  const MeasurementState& residual = m_k - H_k * x_k;
98  const double chi2 = (residual.transpose() * (V_k - H_k * C_k * H_k.transpose()).inverse() * residual).value();
99  return chi2;
100  }
101  };
103 }
genfit::MeasuredStateOnPlane
#StateOnPlane with additional covariance matrix.
Definition: MeasuredStateOnPlane.h:39
Belle2::KalmanStepper::kalmanStep
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:58
genfit::AbsHMatrix::getMatrix
virtual const TMatrixD & getMatrix() const =0
Get the actual matrix representation.
Belle2::KalmanStepper::MeasurementState
Eigen::Matrix< double, Dimension, 1 > MeasurementState
Matrix class Dimension x 1.
Definition: KalmanStepper.h:46
Belle2::KalmanStepper< 2 >::TrackCovariance
Eigen::Matrix< double, 5, 5 > TrackCovariance
Matrix class 5 x 5.
Definition: KalmanStepper.h:54
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::KalmanStepper::TrackState
Eigen::Matrix< double, 5, 1 > TrackState
Matrix class 5 x 1.
Definition: KalmanStepper.h:52
Belle2::KalmanStepper::MeasurementCovariance
Eigen::Matrix< double, Dimension, Dimension > MeasurementCovariance
Matrix class Dimension x Dimension.
Definition: KalmanStepper.h:48
Belle2::KalmanStepper::calculateResidual
double calculateResidual(genfit::MeasuredStateOnPlane &measuredStateOnPlane, const genfit::MeasurementOnPlane &measurementOnPlane) const
Helper function to calculate a residual between the mSoP and the measurement.
Definition: KalmanStepper.h:80
Belle2::KalmanStepper::HMatrix
Eigen::Matrix< double, Dimension, 5 > HMatrix
Matrix class Dimension x 5.
Definition: KalmanStepper.h:50
genfit::MeasurementOnPlane
Measured coordinates on a plane.
Definition: MeasurementOnPlane.h:46