Belle II Software development
KalmanCalculator.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * External Contributor: Wouter Hulsbergen *
5 * *
6 * See git log for contributors and copyright holders. *
7 * This file is licensed under LGPL-3.0, see LICENSE.md. *
8 **************************************************************************/
9
10#include <analysis/VertexFitting/TreeFitter/KalmanCalculator.h>
11#include <analysis/VertexFitting/TreeFitter/FitParams.h>
12
13namespace TreeFitter {
14
16 int sizeRes,
17 int sizeState
18 ) :
19 m_constrDim(sizeRes),
20 m_chisq(1e10),
21 m_res(sizeRes),
22 m_G(sizeRes, sizeState),
23 m_R(sizeRes, sizeRes),
24 m_Rinverse(sizeRes, sizeRes),
25 m_K(sizeState, sizeRes),
26 m_CGt(sizeState, sizeRes)
27 {
28 m_R = Eigen::Matrix < double, -1, -1, 0, 7, 7 >::Zero(m_constrDim, m_constrDim);
29 }
30
31
33 const Eigen::Matrix < double, -1, 1, 0, 7, 1 > & residuals,
34 const Eigen::Matrix < double, -1, -1, 0, 7, MAX_MATRIX_SIZE > & G,
35 const FitParams& fitparams,
36 const Eigen::Matrix < double, -1, -1, 0, 7, 7 > * V,
37 double weight)
38 {
39 m_res = residuals;
40 m_G = G;
41
42 Eigen::Matrix < double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > C = fitparams.getCovariance().triangularView<Eigen::Lower>();
43
44 m_CGt = C.selfadjointView<Eigen::Lower>() * G.transpose();
45 Eigen::Matrix < double, -1, -1, 0, 7, 7 > Rtemp = G * m_CGt;
46 if (V && (weight) && ((*V).diagonal().array() != 0).all()) {
47
48 const Eigen::Matrix < double, -1, -1, 0, 7, 7 > weightedV =
49 weight * (*V).selfadjointView<Eigen::Lower>();
50
51 m_R = Rtemp + weightedV;
52
53 } else {
54 m_R = Rtemp.triangularView<Eigen::Lower>();
55 }
56
57 Eigen::Matrix < double, -1, -1, 0, 7, 7 > RInvtemp;
58 RInvtemp = m_R.selfadjointView<Eigen::Lower>();
59 m_Rinverse = RInvtemp.inverse();
60 if (!m_Rinverse.allFinite()) { return ErrCode(ErrCode::Status::inversionerror); }
61
62 m_K = m_CGt * m_Rinverse.selfadjointView<Eigen::Lower>();
63 return ErrCode(ErrCode::Status::success);
64 }
65
67 {
68 double eps = Eigen::NumTraits<double>::epsilon();
69 fitparams.getStateVector() = (fitparams.getStateVector().array() - (m_K * m_res).array()).matrix()
70 + eps * Eigen::MatrixXd::Identity(fitparams.getStateVector().rows(), fitparams.getStateVector().cols());
71 m_chisq = m_res.transpose() * m_Rinverse.selfadjointView<Eigen::Lower>() * m_res;
72 }
73
75 {
76 double eps = Eigen::NumTraits<double>::epsilon();
77 Eigen::Matrix < double, -1, 1, 0, 7, 1 > res_prime =
78 m_res + m_G * ((oldState.getStateVector().array() - fitparams.getStateVector().array()).matrix() +
79 + eps * Eigen::MatrixXd::Identity(fitparams.getStateVector().rows(), fitparams.getStateVector().cols()));
80 fitparams.getStateVector() = (oldState.getStateVector().array() - (m_K * res_prime).array()).matrix()
81 + eps * Eigen::MatrixXd::Identity(oldState.getStateVector().rows(), oldState.getStateVector().cols());
82
83 m_chisq = res_prime.transpose() * m_Rinverse.selfadjointView<Eigen::Lower>() * res_prime;
84 }
85
86 TREEFITTER_NO_STACK_WARNING
87
89 {
90 Eigen::Matrix < double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > fitCov =
91 fitparams.getCovariance().triangularView<Eigen::Lower>();
92
93 Eigen::Matrix < double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > GRinvGt =
94 m_G.transpose() * m_Rinverse.selfadjointView<Eigen::Lower>() * m_G;
95
96 //fitcov is sym so no transpose needed (not that it would have runtime cost)
97 Eigen::Matrix < double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > deltaCov =
98 fitCov.selfadjointView<Eigen::Lower>() * GRinvGt * fitCov.selfadjointView<Eigen::Lower>();
99
100 double eps = Eigen::NumTraits<double>::epsilon();
101 Eigen::Matrix < double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > delta =
102 (fitCov.array() - deltaCov.array()).matrix() + eps * Eigen::MatrixXd::Identity(fitCov.rows(), fitCov.cols());
103
104 fitparams.getCovariance().triangularView<Eigen::Lower>() = delta.triangularView<Eigen::Lower>();
105
106 }//end function
107
108 TREEFITTER_RESTORE_WARNINGS
109
110}// end namespace
abstract errorocode be aware that the default is success
Definition ErrCode.h:14
Class to store and manage fitparams (statevector)
Definition FitParams.h:20
Eigen::Matrix< double, -1, 1, 0, MAX_MATRIX_SIZE, 1 > & getStateVector()
getter for the fit parameters/statevector
Definition FitParams.h:65
Eigen::Matrix< double, -1, -1, 0, MAX_MATRIX_SIZE, MAX_MATRIX_SIZE > & getCovariance()
getter for the states covariance
Definition FitParams.h:53
void updateState(FitParams &fitparams)
update statevector
ErrCode calculateGainMatrix(const Eigen::Matrix< double, -1, 1, 0, 7, 1 > &residuals, const Eigen::Matrix< double, -1, -1, 0, 7, MAX_MATRIX_SIZE > &G, const FitParams &fitparams, const Eigen::Matrix< double, -1, -1, 0, 7, 7 > *V=0, double weight=1)
init the kalman machienery
Eigen::Matrix< double, -1, -1, 0, 7, 7 > m_Rinverse
R inverse.
KalmanCalculator(int sizeRes, int sizeState)
constructor
Eigen::Matrix< double, -1, -1, 0, 7, 7 > m_R
R residual covariance.
Eigen::Matrix< double, -1, -1, 0, 7, MAX_MATRIX_SIZE > m_G
G former H, transforms covraince of {residuals}<->{x,p,E}.
Eigen::Matrix< double, -1, 1, 0, 7, 1 > m_res
we know the max sizes of the matrices we assume the tree is smaller than MAX_MATRIX_SIZE parameters a...
void updateCovariance(FitParams &fitparams)
update the statevectors covariance
Eigen::Matrix< double, -1, -1, 0, MAX_MATRIX_SIZE, 7 > m_K
K kalman gain matrix.
int m_constrDim
dimension of the constraint
Eigen::Matrix< double, -1, -1, 0, MAX_MATRIX_SIZE, 7 > m_CGt
C times G^t.
STL class.