forked from trackreco/mkFit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKalmanUtils.cc
236 lines (203 loc) · 9.04 KB
/
KalmanUtils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "KalmanUtils.h"
//#define DEBUG
#include "Debug.h"
namespace mkfit {
static const SMatrix36 projMatrix = ROOT::Math::SMatrixIdentity();
static const SMatrix63 projMatrixT = ROOT::Math::Transpose(projMatrix);
//==============================================================================
void updateParameters66(TrackState& propagatedState, MeasurementState& measurementState,
TrackState& result)
{
SMatrixSym66& propErr = propagatedState.errors;
SMatrixSym66 measErr;
measErr.Place_at(measurementState.errors(),0,0);
SMatrixSym66 resErr = measErr+propErr;
int invFail(0);
SMatrixSym33 resErrInv33 = resErr.Sub<SMatrixSym33>(0,0).InverseFast(invFail);
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
result = propagatedState;
return;
}
SMatrixSym66 resErrInv;
resErrInv.Place_at(resErrInv33,0,0);
SVector6 residual = SVector6(measurementState.parameters()[0]-propagatedState.parameters[0],
measurementState.parameters()[1]-propagatedState.parameters[1],
measurementState.parameters()[2]-propagatedState.parameters[2],0,0,0);
SMatrix66 kalmanGain = propErr*resErrInv;
result.parameters = propagatedState.parameters + kalmanGain*residual;
result.errors = propErr - ROOT::Math::SimilarityT(propErr,resErrInv);
}
//==============================================================================
//see e.g. http://inspirehep.net/record/259509?ln=en
TrackState updateParameters(const TrackState& propagatedState, const MeasurementState& measurementState)
{
float r = getHypot(measurementState.pos_[0],measurementState.pos_[1]);
SMatrix33 rot;
rot[0][0] = -(measurementState.pos_[1]+propagatedState.parameters[1])/(2*r);
rot[0][1] = 0;
rot[0][2] = (measurementState.pos_[0]+propagatedState.parameters[0])/(2*r);
rot[1][0] = rot[0][2];
rot[1][1] = 0;
rot[1][2] = -rot[0][0];
rot[2][0] = 0;
rot[2][1] = 1;
rot[2][2] = 0;
const SMatrix33 rotT = ROOT::Math::Transpose(rot);
const SVector3 res_glo = measurementState.parameters()-propagatedState.parameters.Sub<SVector3>(0);
const SVector3 res_loc3 = rotT * res_glo;
const SVector3 res(res_loc3[0],res_loc3[1],0);
const SMatrixSym33 resErr_glo = measurementState.errors() + propagatedState.errors.Sub<SMatrixSym33>(0,0);
//the matrix to invert has to be 2x2
int invFail(0);
const SMatrixSym22 resErr22 = ROOT::Math::SimilarityT(rot,resErr_glo).Sub<SMatrixSym22>(0,0);
const SMatrixSym22 resErrInv22 = resErr22.InverseFast(invFail);
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
return propagatedState;
}
//now go back to 3x3
SMatrixSym33 resErrInv;
resErrInv[0][0] = resErrInv22[0][0];
resErrInv[1][1] = resErrInv22[1][1];
resErrInv[1][0] = resErrInv22[1][0];
SVector6 pred_ccs = propagatedState.parameters;
pred_ccs[3] = 1./propagatedState.pT();
pred_ccs[4] = propagatedState.momPhi();
pred_ccs[5] = propagatedState.theta();
SMatrix66 jac_ccs = ROOT::Math::SMatrixIdentity();
jac_ccs[3][3] = -propagatedState.px()/pow(propagatedState.pT(),3);
jac_ccs[3][4] = -propagatedState.py()/pow(propagatedState.pT(),3);
jac_ccs[4][3] = -propagatedState.py()/pow(propagatedState.pT(),2);
jac_ccs[4][4] = propagatedState.px()/pow(propagatedState.pT(),2);
jac_ccs[5][3] = propagatedState.px()*propagatedState.pz()/(propagatedState.pT()*pow(propagatedState.p(),2));
jac_ccs[5][4] = propagatedState.py()*propagatedState.pz()/(propagatedState.pT()*pow(propagatedState.p(),2));
jac_ccs[5][5] = -propagatedState.pT()/pow(propagatedState.p(),2);
SMatrixSym66 pred_err_ccs = ROOT::Math::Similarity(jac_ccs,propagatedState.errors);
SVector6 up_pars_ccs = pred_ccs + pred_err_ccs*projMatrixT*rot*resErrInv*res;
SMatrixSym66 I66 = ROOT::Math::SMatrixIdentity();
SMatrix36 H = rotT*projMatrix;
SMatrix63 K = pred_err_ccs*ROOT::Math::Transpose(H)*resErrInv;
SMatrixSym33 locErrMeas = ROOT::Math::SimilarityT(rot,measurementState.errors());
locErrMeas[2][0] = 0;
locErrMeas[2][1] = 0;
locErrMeas[2][2] = 0;
locErrMeas[1][2] = 0;
locErrMeas[0][2] = 0;
SMatrixSym66 up_errs_ccs = ROOT::Math::Similarity(I66-K*H,pred_err_ccs) + ROOT::Math::Similarity(K,locErrMeas);
SMatrix66 jac_back_ccs = ROOT::Math::SMatrixIdentity();
jac_back_ccs[3][3] = -cos(up_pars_ccs[4])/pow(up_pars_ccs[3],2);
jac_back_ccs[3][4] = -sin(up_pars_ccs[4])/up_pars_ccs[3];
jac_back_ccs[4][3] = -sin(up_pars_ccs[4])/pow(up_pars_ccs[3],2);
jac_back_ccs[4][4] = cos(up_pars_ccs[4])/up_pars_ccs[3];
jac_back_ccs[5][3] = -cos(up_pars_ccs[5])/(sin(up_pars_ccs[5])*pow(up_pars_ccs[3],2));
jac_back_ccs[5][5] = -1./(pow(sin(up_pars_ccs[5]),2)*up_pars_ccs[3]);
TrackState result;
result.parameters = up_pars_ccs;
result.parameters[3] = cos(up_pars_ccs[4])/up_pars_ccs[3];
result.parameters[4] = sin(up_pars_ccs[4])/up_pars_ccs[3];
result.parameters[5] = cos(up_pars_ccs[5])/(sin(up_pars_ccs[5])*up_pars_ccs[3]);
result.errors = ROOT::Math::Similarity(jac_back_ccs,up_errs_ccs);
result.charge = propagatedState.charge;
result.valid = propagatedState.valid;
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
return propagatedState;
}
#ifdef DEBUG
{
dmutex_guard;
std::cout << "\n updateParameters \n" << std::endl << "propErr" << std::endl;
dumpMatrix(propagatedState.errors);
std::cout << "residual: " << res[0] << " " << res[1] << std::endl
<< "resErr22" << std::endl;
dumpMatrix(resErr22);
std::cout << "resErrInv22" << std::endl;
dumpMatrix(resErrInv22);
std::cout << "jac_ccs" << std::endl;
dumpMatrix(jac_ccs);
std::cout << "pred_err_ccs" << std::endl;
dumpMatrix(pred_err_ccs);
std::cout << "K" << std::endl;
dumpMatrix(K);
std::cout << "H" << std::endl;
dumpMatrix(H);
std::cout << "locErrMeas" << std::endl;
dumpMatrix(locErrMeas);
std::cout << "updatedPars" << std::endl;
std::cout << result.parameters << std::endl;
std::cout << "updatedErrs" << std::endl;
dumpMatrix(result.errors);
std::cout << std::endl;
}
#endif
return result;
}
float computeChi2(const TrackState& propagatedState, const MeasurementState& measurementState)
{
float r = getHypot(measurementState.pos_[0],measurementState.pos_[1]);
//rotate to the tangent plane to the cylinder of radius r at the hit position
SMatrix33 rot;
rot[0][0] = -(measurementState.pos_[1]+propagatedState.parameters[1])/(2*r);
rot[0][1] = 0;
rot[0][2] = (measurementState.pos_[0]+propagatedState.parameters[0])/(2*r);
rot[1][0] = rot[0][2];
rot[1][1] = 0;
rot[1][2] = -rot[0][0];
rot[2][0] = 0;
rot[2][1] = 1;
rot[2][2] = 0;
const SMatrix33 rotT = ROOT::Math::Transpose(rot);
const SVector3 res_glo = measurementState.parameters()-propagatedState.parameters.Sub<SVector3>(0);
const SVector3 res_loc3 = rotT * res_glo;
//the matrix to invert has to be 2x2
const SVector2 res(res_loc3[0],res_loc3[1]);
const SMatrixSym33 resErr_glo = measurementState.errors() + propagatedState.errors.Sub<SMatrixSym33>(0,0);
const SMatrixSym22 resErr = ROOT::Math::SimilarityT(rot,resErr_glo).Sub<SMatrixSym22>(0,0);
int invFail(0);
SMatrixSym22 resErrInv = resErr.InverseFast(invFail);
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
return 9999.;
}
return ROOT::Math::Similarity(res,resErrInv);
}
TrackState updateParametersEndcap(const TrackState& propagatedState, const MeasurementState& measurementState)
{
TrackState result = propagatedState;
const SMatrixSym66& propErr = propagatedState.errors;
const SMatrixSym33 herr = measurementState.errors();
const SVector3& hpos = measurementState.parameters();
SMatrixSym22 resErr = herr.Sub<SMatrixSym22>(0,0)+propErr.Sub<SMatrixSym22>(0,0);
int invFail(0);
SMatrixSym22 resErrInv22 = resErr.InverseFast(invFail);
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
return propagatedState;
}
SVector2 res2 = SVector2(hpos.At(0)-propagatedState.x(),
hpos.At(1)-propagatedState.y());
SMatrix62 projMatrixT_zp;
projMatrixT_zp(0,0) = 1.;
projMatrixT_zp(1,1) = 1.;
SMatrix62 K = propErr*projMatrixT_zp*resErrInv22;
result.parameters = propagatedState.parameters + K*res2;
result.errors = propErr - ROOT::Math::SimilarityT(propErr,ROOT::Math::Similarity(projMatrixT_zp,resErrInv22));
return result;
}
float computeChi2Endcap(const TrackState& propagatedState, const MeasurementState& measurementState)
{
const SMatrixSym66& propErr = propagatedState.errors;
const SMatrixSym33 herr = measurementState.errors();
const SVector3& hpos = measurementState.parameters();
SMatrixSym22 resErr = herr.Sub<SMatrixSym22>(0,0)+propErr.Sub<SMatrixSym22>(0,0);
int invFail(0);
SMatrixSym22 resErrInv = resErr.InverseFast(invFail);
if (0 != invFail) {
dprint(__FILE__ << ":" << __LINE__ << ": FAILED INVERSION");
return 9999.;
}
const SVector2 res(hpos.At(0)-propagatedState.x(), hpos.At(1)-propagatedState.y());
return ROOT::Math::Similarity(res,resErrInv);
}
} // end namespace mkfit