Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
49 
51 {
52  if(predict(_node) && filter(_node))
53  {
54  return true;
55  }
56  else
57  {
58  return false;
59  }
60 }
61 
63 {
64  if(_node.isPredictionDone())
65  {
66  LogInfo("In prediction: Prediction already done for this node!");
67  return false;
68  }
69 
70  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])
71  {
72  LogInfo("Current track parameter is undefined! ");
73  //_node.getHit().print();
74  //_node.print(true);
75  return false;
76  }
77 
78  double z_pred = _node.getZ();
79  _extrapolator.setInitialStateWithCov(_trkpar_curr._z, _trkpar_curr._state_kf, _trkpar_curr._covar_kf);
80  if(!_extrapolator.extrapolateTo(z_pred))
81  {
82  return false;
83  }
84 
85  _node.getPredicted()._z = z_pred;
86  _extrapolator.getFinalStateWithCov(_node.getPredicted()._state_kf, _node.getPredicted()._covar_kf);
87  _extrapolator.getPropagator(_node.getPropagator());
88  _node.setPredictionDone();
89 
90 
92 // if(z_pred > recoConsts::instance()->get_DoubleFlag("FMAG_LENGTH"))
93 // {
94 // _node.getPredicted()._covar_kf = SMatrix::getABCt(_node.getPropagator(), _trkpar_curr._covar_kf, _node.getPropagator());
95 // }
96  /*
97  else
98  {
99  //double p_corr = ELOSS_CORR/FMAG_LENGTH*_extrapolator.getTravelLength()/10.;
100  //_node.getPredicted()._state_kf[0][0] = _node.getPredicted().get_charge()/(_node.getPredicted().get_mom() - p_corr);
101  }
102  */
103 
105  //_node.getPredicted()._z = z_pred;
106  //_node.getPredicted()._state_kf = _trkpar_curr._state_kf;
107  //_node.getPredicted()._covar_kf = _trkpar_curr._covar_kf;
108  //SMatrix::unitMatrix(_node.getPropagator());
109 
110  return true;
111 }
112 
114 {
115  if(_node.isFilterDone())
116  {
117  LogInfo("In filtering: Filter already done for this node!");
118  return false;
119  }
120 
121  if(!_node.isPredictionDone())
122  {
123  LogInfo("In filtering: Prediction not done for this node!");
124  return false;
125  }
126 
128  const TMatrixD& p_pred = _node.getPredicted()._state_kf;
129  const TMatrixD& cov_pred = _node.getPredicted()._covar_kf;
130  const TMatrixD& m = _node.getMeasurement();
131  const TMatrixD& cov_m = _node.getMeasurementCov();
132  const TMatrixD& proj = _node.getProjector();
133 
136  TMatrixD g = SMatrix::invertMatrix(cov_m);
137  TMatrixD cov_pred_inv = SMatrix::invertMatrix(cov_pred);
138  TMatrixD cov_filter = SMatrix::invertMatrix(cov_pred_inv + SMatrix::getAtBC(proj, g, proj));
139 
143  //EQ. 1:
144  //TMatrixD htgm = SMatrix::getAtBC(proj, g, m);
145  //TMatrixD p_filter = cov_filter*(cov_pred_inv*p_pred + htgm);
146  //EQ. 2:
147  TMatrixD k = SMatrix::getABtC(cov_pred, proj, SMatrix::invertMatrix(cov_m + SMatrix::getABCt(proj, cov_pred, proj)));
148  TMatrixD p_filter = p_pred + k*(m - proj*p_pred);
149  //EQ. 3:
150  //TMatrixD p_filter = p_pred + SMatrix::getABtC(cov_filter, proj, g)*(m - proj*p_pred);
151 
155  TMatrixD r_filter = m - proj*p_filter;
156  TMatrixD r_cov_filter = cov_m - SMatrix::getABCt(proj, cov_filter, proj);
157 
162  double chi2m = SMatrix::getAtBC(r_filter, g, r_filter)[0][0];
163  TMatrixD dp = p_filter - p_pred;
164  double chi2p = SMatrix::getAtBC(dp, cov_pred_inv, dp)[0][0];
165 
167  _node.getFiltered()._state_kf = p_filter;
168  _node.getFiltered()._covar_kf = cov_filter;
169  _node.getFiltered()._z = _node.getPredicted()._z;
170  _node.setChisq(chi2m + chi2p);
171  _node.setFilterDone();
172 
173  /*
174  if(_node.getFiltered().get_charge()*_node.getPredicted().get_charge() < 1)
175  {
176  LogInfo("ERROR!! Filter Wrong!");
177  SMatrix::printMatrix(p_pred, "p_pred");
178  SMatrix::printMatrix(p_filter, "p_filter");
179  SMatrix::printMatrix(k, "k matrix");
180  SMatrix::printMatrix(m - proj*p_pred, "m - proj*p_pred");
181  }
182  */
183 
184  return true;
185 }
186 
187 bool KalmanFilter::smooth(Node& _node, Node& _node_prev)
188 {
189  if(!_node.isFilterDone())
190  {
191  LogInfo("In smoother: Filter not done!");
192  return false;
193  }
194 
195  if(_node.isSmoothDone())
196  {
197  LogInfo("In smoother: Smooth already done!");
198  return false;
199  }
200 
201  if(!_node_prev.isSmoothDone())
202  {
203  LogInfo("In smoother: Smooth not done for the last node");
204  }
205 
207  const TMatrixD& p_filter = _node.getFiltered()._state_kf;
208  const TMatrixD& cov_filter = _node.getFiltered()._covar_kf;
209  const TMatrixD& p_pred_prev = _node_prev.getPredicted()._state_kf;
210  const TMatrixD& cov_pred_prev = _node_prev.getPredicted()._covar_kf;
211  const TMatrixD& p_smooth_prev = _node_prev.getSmoothed()._state_kf;
212  const TMatrixD& cov_smooth_prev = _node_prev.getSmoothed()._covar_kf;
213  const TMatrixD& prop_prev = _node_prev.getPropagator();
214 
218  TMatrixD a = SMatrix::getABtCinv(cov_filter, prop_prev, cov_pred_prev);
219  TMatrixD p_smooth = p_filter + a*(p_smooth_prev - p_pred_prev);
220 
223  TMatrixD cov_smooth = cov_filter + SMatrix::getABCt(a, cov_smooth_prev - cov_pred_prev, a);
224 
226  _node.getSmoothed()._state_kf = p_smooth;
227  _node.getSmoothed()._covar_kf = cov_smooth;
228  _node.getSmoothed()._z = _node.getFiltered()._z;
229  _node.setSmoothDone();
230 
231  return true;
232 }
#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