Class Reference for E1039 Core & Analysis Software
KalmanFilter.cxx
Go to the documentation of this file.
1 /*
2 kalmanFilter.cxx
3 
4 Implimentation of the class KalmanFilter, provides all core
5 functionalities of the track fitting procedure
6 
7 Author: Kun Liu, liuk@fnal.gov
8 Created: 11-21-2011
9 */
10 
11 #include <iostream>
12 #include <algorithm>
13 #include <cmath>
14 
15 #include <phool/recoConsts.h>
16 
17 #include "KalmanFilter.h"
18 
19 KalmanFilter* KalmanFilter::p_kmfit = nullptr;
20 
22 {
23  if(p_kmfit == nullptr)
24  {
25  p_kmfit = new KalmanFilter();
26  }
27 
28  return p_kmfit;
29 }
30 
32 {
33  if(p_kmfit != nullptr)
34  {
35  delete p_kmfit;
36  }
37 }
38 
39 KalmanFilter::KalmanFilter(bool limitedStep)
40 {
41 // JobOptsSvc* p_jobOptsSvc = JobOptsSvc::instance();
42 // _extrapolator.init(p_jobOptsSvc);
43 }
44 
45 bool KalmanFilter::initExtrapolator(const PHField *field, const TGeoManager *geom)
46 {
47  _extrapolator.init(field, geom);
48  return true;
49 }
50 
52 {
53  if(predict(_node) && filter(_node))
54  {
55  return true;
56  }
57  else
58  {
59  return false;
60  }
61 }
62 
64 {
65  if(_node.isPredictionDone())
66  {
67  LogInfo("In prediction: Prediction already done for this node!");
68  return false;
69  }
70 
71  if(fabs(_trkpar_curr._state_kf[0][0]) < 1E-5 || fabs(_trkpar_curr._state_kf[0][0]) > 1. || _trkpar_curr._state_kf[0][0] != _trkpar_curr._state_kf[0][0])
72  {
73  LogInfo("Current track parameter is undefined! ");
74  //_node.getHit().print();
75  //_node.print(true);
76  return false;
77  }
78 
79  double z_pred = _node.getZ();
80  _extrapolator.setInitialStateWithCov(_trkpar_curr._z, _trkpar_curr._state_kf, _trkpar_curr._covar_kf);
81  if(!_extrapolator.extrapolateTo(z_pred))
82  {
83  return false;
84  }
85 
86  _node.getPredicted()._z = z_pred;
87  _extrapolator.getFinalStateWithCov(_node.getPredicted()._state_kf, _node.getPredicted()._covar_kf);
88  _extrapolator.getPropagator(_node.getPropagator());
89  _node.setPredictionDone();
90 
91 
93 // if(z_pred > recoConsts::instance()->get_DoubleFlag("FMAG_LENGTH"))
94 // {
95 // _node.getPredicted()._covar_kf = SMatrix::getABCt(_node.getPropagator(), _trkpar_curr._covar_kf, _node.getPropagator());
96 // }
97  /*
98  else
99  {
100  //double p_corr = ELOSS_CORR/FMAG_LENGTH*_extrapolator.getTravelLength()/10.;
101  //_node.getPredicted()._state_kf[0][0] = _node.getPredicted().get_charge()/(_node.getPredicted().get_mom() - p_corr);
102  }
103  */
104 
106  //_node.getPredicted()._z = z_pred;
107  //_node.getPredicted()._state_kf = _trkpar_curr._state_kf;
108  //_node.getPredicted()._covar_kf = _trkpar_curr._covar_kf;
109  //SMatrix::unitMatrix(_node.getPropagator());
110 
111  return true;
112 }
113 
115 {
116  if(_node.isFilterDone())
117  {
118  LogInfo("In filtering: Filter already done for this node!");
119  return false;
120  }
121 
122  if(!_node.isPredictionDone())
123  {
124  LogInfo("In filtering: Prediction not done for this node!");
125  return false;
126  }
127 
129  const TMatrixD& p_pred = _node.getPredicted()._state_kf;
130  const TMatrixD& cov_pred = _node.getPredicted()._covar_kf;
131  const TMatrixD& m = _node.getMeasurement();
132  const TMatrixD& cov_m = _node.getMeasurementCov();
133  const TMatrixD& proj = _node.getProjector();
134 
137  TMatrixD g = SMatrix::invertMatrix(cov_m);
138  TMatrixD cov_pred_inv = SMatrix::invertMatrix(cov_pred);
139  TMatrixD cov_filter = SMatrix::invertMatrix(cov_pred_inv + SMatrix::getAtBC(proj, g, proj));
140 
144  //EQ. 1:
145  //TMatrixD htgm = SMatrix::getAtBC(proj, g, m);
146  //TMatrixD p_filter = cov_filter*(cov_pred_inv*p_pred + htgm);
147  //EQ. 2:
148  TMatrixD k = SMatrix::getABtC(cov_pred, proj, SMatrix::invertMatrix(cov_m + SMatrix::getABCt(proj, cov_pred, proj)));
149  TMatrixD p_filter = p_pred + k*(m - proj*p_pred);
150  //EQ. 3:
151  //TMatrixD p_filter = p_pred + SMatrix::getABtC(cov_filter, proj, g)*(m - proj*p_pred);
152 
156  TMatrixD r_filter = m - proj*p_filter;
157  TMatrixD r_cov_filter = cov_m - SMatrix::getABCt(proj, cov_filter, proj);
158 
163  double chi2m = SMatrix::getAtBC(r_filter, g, r_filter)[0][0];
164  TMatrixD dp = p_filter - p_pred;
165  double chi2p = SMatrix::getAtBC(dp, cov_pred_inv, dp)[0][0];
166 
168  _node.getFiltered()._state_kf = p_filter;
169  _node.getFiltered()._covar_kf = cov_filter;
170  _node.getFiltered()._z = _node.getPredicted()._z;
171  _node.setChisq(chi2m + chi2p);
172  _node.setFilterDone();
173 
174  /*
175  if(_node.getFiltered().get_charge()*_node.getPredicted().get_charge() < 1)
176  {
177  LogInfo("ERROR!! Filter Wrong!");
178  SMatrix::printMatrix(p_pred, "p_pred");
179  SMatrix::printMatrix(p_filter, "p_filter");
180  SMatrix::printMatrix(k, "k matrix");
181  SMatrix::printMatrix(m - proj*p_pred, "m - proj*p_pred");
182  }
183  */
184 
185  return true;
186 }
187 
188 bool KalmanFilter::smooth(Node& _node, Node& _node_prev)
189 {
190  if(!_node.isFilterDone())
191  {
192  LogInfo("In smoother: Filter not done!");
193  return false;
194  }
195 
196  if(_node.isSmoothDone())
197  {
198  LogInfo("In smoother: Smooth already done!");
199  return false;
200  }
201 
202  if(!_node_prev.isSmoothDone())
203  {
204  LogInfo("In smoother: Smooth not done for the last node");
205  }
206 
208  const TMatrixD& p_filter = _node.getFiltered()._state_kf;
209  const TMatrixD& cov_filter = _node.getFiltered()._covar_kf;
210  const TMatrixD& p_pred_prev = _node_prev.getPredicted()._state_kf;
211  const TMatrixD& cov_pred_prev = _node_prev.getPredicted()._covar_kf;
212  const TMatrixD& p_smooth_prev = _node_prev.getSmoothed()._state_kf;
213  const TMatrixD& cov_smooth_prev = _node_prev.getSmoothed()._covar_kf;
214  const TMatrixD& prop_prev = _node_prev.getPropagator();
215 
219  TMatrixD a = SMatrix::getABtCinv(cov_filter, prop_prev, cov_pred_prev);
220  TMatrixD p_smooth = p_filter + a*(p_smooth_prev - p_pred_prev);
221 
224  TMatrixD cov_smooth = cov_filter + SMatrix::getABCt(a, cov_smooth_prev - cov_pred_prev, a);
225 
227  _node.getSmoothed()._state_kf = p_smooth;
228  _node.getSmoothed()._covar_kf = cov_smooth;
229  _node.getSmoothed()._z = _node.getFiltered()._z;
230  _node.setSmoothDone();
231 
232  return true;
233 }
#define LogInfo(message)
Definition: DbSvc.cc:15
bool init(const PHField *field, const TGeoManager *geom)
Initialize geometry and physics.
bool extrapolateTo(double z_out)
void getPropagator(TMatrixD &prop)
Get the propagator.
void getFinalStateWithCov(TMatrixD &state_out, TMatrixD &cov_out)
Get the final state parameters and covariance.
void setInitialStateWithCov(double z_in, TMatrixD &state_in, TMatrixD &cov_in)
Set input initial state parameters.
bool filter(Node &_node)
bool initExtrapolator(const PHField *field, const TGeoManager *geom)
bool smooth(Node &_node, Node &_node_prev)
static KalmanFilter * instance()
singlton instance
bool fit_node(Node &_node)
Fit one node.
bool predict(Node &_node)
Kalman filter steps.
KalmanFilter(bool limitedStep=true)
Real constructor.
void setPredictionDone(bool flag=true)
Definition: KalmanUtil.h:141
bool isFilterDone()
Definition: KalmanUtil.h:132
TrkPar & getSmoothed()
Definition: KalmanUtil.h:108
TrkPar & getPredicted()
Gets.
Definition: KalmanUtil.h:106
void setFilterDone(bool flag=true)
Definition: KalmanUtil.h:142
TMatrixD & getMeasurement()
Definition: KalmanUtil.h:110
bool isSmoothDone()
Definition: KalmanUtil.h:133
void setChisq(double chisq)
Definition: KalmanUtil.h:145
bool isPredictionDone()
Definition: KalmanUtil.h:131
double getZ()
Definition: KalmanUtil.h:126
TMatrixD & getProjector()
Definition: KalmanUtil.h:122
TrkPar & getFiltered()
Definition: KalmanUtil.h:107
TMatrixD & getMeasurementCov()
Definition: KalmanUtil.h:111
void setSmoothDone(bool flag=true)
Definition: KalmanUtil.h:143
TMatrixD & getPropagator()
Definition: KalmanUtil.h:121
transient DST object for field storage and access
Definition: PHField.h:14
static TMatrixD getABtCinv(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:149
static TMatrixD getABtC(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:141
static TMatrixD getABCt(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:125
static TMatrixD getAtBC(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:133
static TMatrixD invertMatrix(const TMatrixD &m)
Definition: KalmanUtil.cxx:52
TMatrixD _state_kf
State vectors and its covariance.
Definition: KalmanUtil.h:85
TMatrixD _covar_kf
Definition: KalmanUtil.h:86
double _z
Definition: KalmanUtil.h:87