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  if(z_pred > recoConsts::instance()->get_DoubleFlag("FMAG_LENGTH"))
91  {
92  _node.getPredicted()._covar_kf = SMatrix::getABCt(_node.getPropagator(), _trkpar_curr._covar_kf, _node.getPropagator());
93  }
94  /*
95  else
96  {
97  //double p_corr = ELOSS_CORR/FMAG_LENGTH*_extrapolator.getTravelLength()/10.;
98  //_node.getPredicted()._state_kf[0][0] = _node.getPredicted().get_charge()/(_node.getPredicted().get_mom() - p_corr);
99  }
100  */
101 
103  //_node.getPredicted()._z = z_pred;
104  //_node.getPredicted()._state_kf = _trkpar_curr._state_kf;
105  //_node.getPredicted()._covar_kf = _trkpar_curr._covar_kf;
106  //SMatrix::unitMatrix(_node.getPropagator());
107 
108  return true;
109 }
110 
112 {
113  if(_node.isFilterDone())
114  {
115  LogInfo("In filtering: Filter already done for this node!");
116  return false;
117  }
118 
119  if(!_node.isPredictionDone())
120  {
121  LogInfo("In filtering: Prediction not done for this node!");
122  return false;
123  }
124 
126  const TMatrixD& p_pred = _node.getPredicted()._state_kf;
127  const TMatrixD& cov_pred = _node.getPredicted()._covar_kf;
128  const TMatrixD& m = _node.getMeasurement();
129  const TMatrixD& cov_m = _node.getMeasurementCov();
130  const TMatrixD& proj = _node.getProjector();
131 
134  TMatrixD g = SMatrix::invertMatrix(cov_m);
135  TMatrixD cov_pred_inv = SMatrix::invertMatrix(cov_pred);
136  TMatrixD cov_filter = SMatrix::invertMatrix(cov_pred_inv + SMatrix::getAtBC(proj, g, proj));
137 
141  //EQ. 1:
142  //TMatrixD htgm = SMatrix::getAtBC(proj, g, m);
143  //TMatrixD p_filter = cov_filter*(cov_pred_inv*p_pred + htgm);
144  //EQ. 2:
145  TMatrixD k = SMatrix::getABtC(cov_pred, proj, SMatrix::invertMatrix(cov_m + SMatrix::getABCt(proj, cov_pred, proj)));
146  TMatrixD p_filter = p_pred + k*(m - proj*p_pred);
147  //EQ. 3:
148  //TMatrixD p_filter = p_pred + SMatrix::getABtC(cov_filter, proj, g)*(m - proj*p_pred);
149 
153  TMatrixD r_filter = m - proj*p_filter;
154  TMatrixD r_cov_filter = cov_m - SMatrix::getABCt(proj, cov_filter, proj);
155 
160  double chi2m = SMatrix::getAtBC(r_filter, g, r_filter)[0][0];
161  TMatrixD dp = p_filter - p_pred;
162  double chi2p = SMatrix::getAtBC(dp, cov_pred_inv, dp)[0][0];
163 
165  _node.getFiltered()._state_kf = p_filter;
166  _node.getFiltered()._covar_kf = cov_filter;
167  _node.getFiltered()._z = _node.getPredicted()._z;
168  _node.setChisq(chi2m + chi2p);
169  _node.setFilterDone();
170 
171  /*
172  if(_node.getFiltered().get_charge()*_node.getPredicted().get_charge() < 1)
173  {
174  LogInfo("ERROR!! Filter Wrong!");
175  SMatrix::printMatrix(p_pred, "p_pred");
176  SMatrix::printMatrix(p_filter, "p_filter");
177  SMatrix::printMatrix(k, "k matrix");
178  SMatrix::printMatrix(m - proj*p_pred, "m - proj*p_pred");
179  }
180  */
181 
182  return true;
183 }
184 
185 bool KalmanFilter::smooth(Node& _node, Node& _node_prev)
186 {
187  if(!_node.isFilterDone())
188  {
189  LogInfo("In smoother: Filter not done!");
190  return false;
191  }
192 
193  if(_node.isSmoothDone())
194  {
195  LogInfo("In smoother: Smooth already done!");
196  return false;
197  }
198 
199  if(!_node_prev.isSmoothDone())
200  {
201  LogInfo("In smoother: Smooth not done for the last node");
202  }
203 
205  const TMatrixD& p_filter = _node.getFiltered()._state_kf;
206  const TMatrixD& cov_filter = _node.getFiltered()._covar_kf;
207  const TMatrixD& p_pred_prev = _node_prev.getPredicted()._state_kf;
208  const TMatrixD& cov_pred_prev = _node_prev.getPredicted()._covar_kf;
209  const TMatrixD& p_smooth_prev = _node_prev.getSmoothed()._state_kf;
210  const TMatrixD& cov_smooth_prev = _node_prev.getSmoothed()._covar_kf;
211  const TMatrixD& prop_prev = _node_prev.getPropagator();
212 
216  TMatrixD a = SMatrix::getABtCinv(cov_filter, prop_prev, cov_pred_prev);
217  TMatrixD p_smooth = p_filter + a*(p_smooth_prev - p_pred_prev);
218 
221  TMatrixD cov_smooth = cov_filter + SMatrix::getABCt(a, cov_smooth_prev - cov_pred_prev, a);
222 
224  _node.getSmoothed()._state_kf = p_smooth;
225  _node.getSmoothed()._covar_kf = cov_smooth;
226  _node.getSmoothed()._z = _node.getFiltered()._z;
227  _node.setSmoothDone();
228 
229  return true;
230 }
bool predict(Node &_node)
Kalman filter steps.
bool isPredictionDone()
Definition: KalmanUtil.h:131
bool isFilterDone()
Definition: KalmanUtil.h:132
TrkPar & getFiltered()
Definition: KalmanUtil.h:107
TrkPar & getPredicted()
Gets.
Definition: KalmanUtil.h:106
bool extrapolateTo(double z_out)
void getPropagator(TMatrixD &prop)
Get the propagator.
TMatrixD & getMeasurement()
Definition: KalmanUtil.h:110
static TMatrixD getABtCinv(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:149
static TMatrixD getAtBC(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:133
void setChisq(double chisq)
Definition: KalmanUtil.h:145
transient DST object for field storage and access
Definition: PHField.h:13
bool isSmoothDone()
Definition: KalmanUtil.h:133
static recoConsts * instance()
Definition: recoConsts.cc:7
static TMatrixD invertMatrix(const TMatrixD &m)
Definition: KalmanUtil.cxx:52
void setPredictionDone(bool flag=true)
Definition: KalmanUtil.h:141
TMatrixD & getProjector()
Definition: KalmanUtil.h:122
bool init(const PHField *field, const TGeoManager *geom)
Initialize geometry and physics.
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.
double _z
Definition: KalmanUtil.h:87
double getZ()
Definition: KalmanUtil.h:126
bool fit_node(Node &_node)
Fit one node.
TMatrixD & getMeasurementCov()
Definition: KalmanUtil.h:111
bool filter(Node &_node)
static KalmanFilter * instance()
singlton instance
TMatrixD _state_kf
State vectors and its covariance.
Definition: KalmanUtil.h:85
static TMatrixD getABCt(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:125
bool smooth(Node &_node, Node &_node_prev)
void setFilterDone(bool flag=true)
Definition: KalmanUtil.h:142
TMatrixD _covar_kf
Definition: KalmanUtil.h:86
#define LogInfo(message)
Definition: DbSvc.cc:14
TMatrixD & getPropagator()
Definition: KalmanUtil.h:121
KalmanFilter(bool limitedStep=true)
Real constructor.
static TMatrixD getABtC(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:141
bool initExtrapolator(const PHField *field, const TGeoManager *geom)
TrkPar & getSmoothed()
Definition: KalmanUtil.h:108
void setSmoothDone(bool flag=true)
Definition: KalmanUtil.h:143