Class Reference for E1039 Core & Analysis Software
KalmanUtil.h
Go to the documentation of this file.
1 /*
2 KalmanUtil.h
3 
4 Utilities for kalman filter, including:
5 1. TrkPar: track parameter defination: (q/p, px, py, x, y), its covariance, z
6 2. Node: node defination for kalman filter
7 3. SMatrix: some frequently used matrix manipulations
8 
9 Author: Kun Liu, liuk@fnal.gov
10 Created: 11-20-2011
11 */
12 
13 #ifndef _KALMANUTIL_H
14 #define _KALMANUTIL_H
15 
16 #include <GlobalConsts.h>
17 
18 #include <iostream>
19 #include <cmath>
20 #include <string>
21 
22 #include <TMatrixD.h>
23 #include <TVector3.h>
24 
25 #include "SRawEvent.h"
26 #include "FastTracklet.h"
27 
28 class SMatrix
29 {
30 public:
31  static void printMatrix(const TMatrixD& m);
32  static void printMatrix(const TMatrixD& m, std::string str);
33  static TMatrixD invertMatrix(const TMatrixD& m);
34  static TMatrixD invertMatrixFast(const TMatrixD& m);
35  static TMatrixD transposeMatrix(const TMatrixD& m);
36  static void unitMatrix(TMatrixD& m);
37  static void zeroMatrix(TMatrixD& m);
38  static TMatrixD getABC(const TMatrixD& A, const TMatrixD& B, const TMatrixD& C);
39  static TMatrixD getABCt(const TMatrixD& A, const TMatrixD& B, const TMatrixD& C);
40  static TMatrixD getAtBC(const TMatrixD& A, const TMatrixD& B, const TMatrixD& C);
41  static TMatrixD getABtC(const TMatrixD& A, const TMatrixD& B, const TMatrixD& C);
42  static TMatrixD getABtCinv(const TMatrixD& A, const TMatrixD& B, const TMatrixD& C);
43 };
44 
45 class TrkPar
46 {
47 public:
49  {
50  _state_kf.ResizeTo(5, 1);
51  _covar_kf.ResizeTo(5, 5);
52  _z = 0;
53  }
54 
56  const TMatrixD& get_state_vector() { return _state_kf; }
57  const TMatrixD& get_covariance() {return _covar_kf; }
58  double get_x() { return _state_kf(3, 0); }
59  double get_y() { return _state_kf(4, 0); }
60  double get_z() { return _z; }
61  double get_dxdz() { return _state_kf(1, 0); }
62  double get_dydz() { return _state_kf(2, 0); }
63  double get_mom(double& px, double& py, double& pz);
64  double get_mom() { return 1./fabs(_state_kf[0][0]); }
65  TVector3 get_mom_vec() { double px, py, pz; get_mom(px, py, pz); return TVector3(px, py, pz); }
66  double get_pos(double& x, double& y, double& z);
67  int get_charge() { return _state_kf[0][0] > 0 ? 1 : -1; }
68 
70  void set_state_vector(TMatrixD& state) { _state_kf = state; }
71  void set_covariance(TMatrixD& cov) { _covar_kf = cov; }
72  void set_x(double val) { _state_kf[3][0] = val; }
73  void set_y(double val) { _state_kf[4][0] = val; }
74  void set_z(double val) { _z = val; }
75  void set_dxdz(double val) { _state_kf[1][0] = val; }
76  void set_dydz(double val) { _state_kf[2][0] = val; }
77 
78  //Flip the sign of this track parameter
79  void flip_charge();
80 
82  void print();
83 
85  TMatrixD _state_kf;
86  TMatrixD _covar_kf;
87  double _z;
88 };
89 
90 class Node
91 {
92 public:
94  Node();
95 
97  Node(const Hit& _hit);
98 
100  Node(const SignedHit& _hit_signed);
101 
103  void print(bool verbose = true);
104 
106  TrkPar& getPredicted() { return _predicted; }
107  TrkPar& getFiltered() { return _filtered; }
108  TrkPar& getSmoothed() { return _smoothed; }
109 
110  TMatrixD& getMeasurement() { return _measurement; }
111  TMatrixD& getMeasurementCov() { return _measurement_cov; }
112 
114  TMatrixD getPredictedResidual();
115  TMatrixD getPredictedResidualCov();
116  TMatrixD getFilteredResidual();
117  TMatrixD getFilteredResidualCov();
118  TMatrixD getSmoothedResidual();
119  TMatrixD getSmoothedResidualCov();
120 
121  TMatrixD& getPropagator() { return _propagator; }
122  TMatrixD& getProjector() { return _projector; }
123 
124  TMatrixD getKalmanGain();
125 
126  double getZ() { return _z; }
127  double getChisq() { return _chisq; }
128 
129  Hit& getHit() { return _hit; }
130 
131  bool isPredictionDone() { return _prediction_done; }
132  bool isFilterDone() { return _filter_done; }
133  bool isSmoothDone() { return _smooth_done; }
134 
136  void setMeasurement(TMatrixD& m, TMatrixD& cov);
137  void setZ(double z) { _z = z; }
138  void setProjector(TMatrixD& p) { _projector = p; }
139  void setPropagator(TMatrixD& p) { _propagator = p; };
140 
141  void setPredictionDone(bool flag = true) { _prediction_done = flag; }
142  void setFilterDone(bool flag = true) { _filter_done = flag; }
143  void setSmoothDone(bool flag = true) { _smooth_done = flag; }
144 
145  void setChisq(double chisq) { _chisq = chisq; }
146  void addChisq(double chisq) { _chisq += chisq; }
147 
148  void resetFlags();
149 
151  bool operator<(const Node& elem) const { return _z < elem._z; };
152 
153 private:
154  TMatrixD _measurement;
155  TMatrixD _measurement_cov;
156  TMatrixD _projector;
157  TMatrixD _propagator;
158 
159  double _z;
160 
161  bool _prediction_done;
162  bool _filter_done;
163  bool _smooth_done;
164 
165  TrkPar _predicted;
166  TrkPar _filtered;
167  TrkPar _smoothed;
168  double _chisq;
169 
170  Hit _hit;
171 };
172 
173 #endif
Definition of hit structure.
Definition: SRawEvent.h:35
TMatrixD getSmoothedResidual()
Definition: KalmanUtil.cxx:359
void setPredictionDone(bool flag=true)
Definition: KalmanUtil.h:141
TMatrixD getSmoothedResidualCov()
Definition: KalmanUtil.cxx:367
void print(bool verbose=true)
print for debugging purposes
Definition: KalmanUtil.cxx:197
bool isFilterDone()
Definition: KalmanUtil.h:132
TrkPar & getSmoothed()
Definition: KalmanUtil.h:108
void setZ(double z)
Definition: KalmanUtil.h:137
double getChisq()
Definition: KalmanUtil.h:127
TrkPar & getPredicted()
Gets.
Definition: KalmanUtil.h:106
void setFilterDone(bool flag=true)
Definition: KalmanUtil.h:142
TMatrixD & getMeasurement()
Definition: KalmanUtil.h:110
TMatrixD getKalmanGain()
Definition: KalmanUtil.cxx:375
void setProjector(TMatrixD &p)
Definition: KalmanUtil.h:138
TMatrixD getPredictedResidualCov()
Definition: KalmanUtil.cxx:335
bool isSmoothDone()
Definition: KalmanUtil.h:133
void setChisq(double chisq)
Definition: KalmanUtil.h:145
bool isPredictionDone()
Definition: KalmanUtil.h:131
void addChisq(double chisq)
Definition: KalmanUtil.h:146
TMatrixD getFilteredResidual()
Definition: KalmanUtil.cxx:343
void resetFlags()
Definition: KalmanUtil.cxx:381
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 setPropagator(TMatrixD &p)
Definition: KalmanUtil.h:139
void setSmoothDone(bool flag=true)
Definition: KalmanUtil.h:143
Node()
default constructor, only initialize the matrix dimension
Definition: KalmanUtil.cxx:234
Hit & getHit()
Definition: KalmanUtil.h:129
TMatrixD getPredictedResidual()
Matrix calculations, should be called as less as possible.
Definition: KalmanUtil.cxx:327
void setMeasurement(TMatrixD &m, TMatrixD &cov)
Sets.
Definition: KalmanUtil.cxx:321
bool operator<(const Node &elem) const
Overriden operators.
Definition: KalmanUtil.h:151
TMatrixD & getPropagator()
Definition: KalmanUtil.h:121
TMatrixD getFilteredResidualCov()
Definition: KalmanUtil.cxx:351
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 getABC(const TMatrixD &A, const TMatrixD &B, const TMatrixD &C)
Definition: KalmanUtil.cxx:120
static void unitMatrix(TMatrixD &m)
Definition: KalmanUtil.cxx:87
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 void zeroMatrix(TMatrixD &m)
Definition: KalmanUtil.cxx:107
static TMatrixD invertMatrix(const TMatrixD &m)
Definition: KalmanUtil.cxx:52
static TMatrixD invertMatrixFast(const TMatrixD &m)
Definition: KalmanUtil.cxx:71
static void printMatrix(const TMatrixD &m)
Definition: KalmanUtil.cxx:17
static TMatrixD transposeMatrix(const TMatrixD &m)
Definition: KalmanUtil.cxx:79
double get_y()
Definition: KalmanUtil.h:59
int get_charge()
Definition: KalmanUtil.h:67
void set_covariance(TMatrixD &cov)
Definition: KalmanUtil.h:71
TVector3 get_mom_vec()
Definition: KalmanUtil.h:65
double get_dydz()
Definition: KalmanUtil.h:62
TrkPar()
Definition: KalmanUtil.h:48
void set_y(double val)
Definition: KalmanUtil.h:73
void print()
print for debugging purpose
Definition: KalmanUtil.cxx:188
const TMatrixD & get_state_vector()
Gets.
Definition: KalmanUtil.h:56
TMatrixD _state_kf
State vectors and its covariance.
Definition: KalmanUtil.h:85
void set_state_vector(TMatrixD &state)
Sets.
Definition: KalmanUtil.h:70
void flip_charge()
Definition: KalmanUtil.cxx:158
void set_dxdz(double val)
Definition: KalmanUtil.h:75
double get_z()
Definition: KalmanUtil.h:60
TMatrixD _covar_kf
Definition: KalmanUtil.h:86
void set_z(double val)
Definition: KalmanUtil.h:74
double get_pos(double &x, double &y, double &z)
Definition: KalmanUtil.cxx:179
double get_mom()
Definition: KalmanUtil.h:64
double get_dxdz()
Definition: KalmanUtil.h:61
double _z
Definition: KalmanUtil.h:87
const TMatrixD & get_covariance()
Definition: KalmanUtil.h:57
void set_dydz(double val)
Definition: KalmanUtil.h:76
void set_x(double val)
Definition: KalmanUtil.h:72
double get_x()
Definition: KalmanUtil.h:58