forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BrokenLine.h
561 lines (467 loc) · 24.7 KB
/
BrokenLine.h
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#ifndef RecoPixelVertexing_PixelTrackFitting_interface_BrokenLine_h
#define RecoPixelVertexing_PixelTrackFitting_interface_BrokenLine_h
#include <Eigen/Eigenvalues>
#include "RecoPixelVertexing/PixelTrackFitting/interface/FitUtils.h"
namespace BrokenLine {
using namespace Rfit;
//!< Karimäki's parameters: (phi, d, k=1/R)
/*!< covariance matrix: \n
|cov(phi,phi)|cov( d ,phi)|cov( k ,phi)| \n
|cov(phi, d )|cov( d , d )|cov( k , d )| \n
|cov(phi, k )|cov( d , k )|cov( k , k )|
*/
using karimaki_circle_fit = Rfit::circle_fit;
/*!
\brief data needed for the Broken Line fit procedure.
*/
template <int N>
struct PreparedBrokenLineData {
int q; //!< particle charge
Matrix2xNd<N> radii; //!< xy data in the system in which the pre-fitted center is the origin
VectorNd<N> s; //!< total distance traveled in the transverse plane starting from the pre-fitted closest approach
VectorNd<N> S; //!< total distance traveled (three-dimensional)
VectorNd<N> Z; //!< orthogonal coordinate to the pre-fitted line in the sz plane
VectorNd<N> VarBeta; //!< kink angles in the SZ plane
};
/*!
\brief Computes the Coulomb multiple scattering variance of the planar angle.
\param length length of the track in the material.
\param B magnetic field in Gev/cm/c.
\param R radius of curvature (needed to evaluate p).
\param Layer denotes which of the four layers of the detector is the endpoint of the multiple scattered track. For example, if Layer=3, then the particle has just gone through the material between the second and the third layer.
\todo add another Layer variable to identify also the start point of the track, so if there are missing hits or multiple hits, the part of the detector that the particle has traversed can be exactly identified.
\warning the formula used here assumes beta=1, and so neglects the dependence of theta_0 on the mass of the particle at fixed momentum.
\return the variance of the planar angle ((theta_0)^2 /3).
*/
__host__ __device__ inline double MultScatt(
const double& length, const double B, const double R, int Layer, double slope) {
// limit R to 20GeV...
auto pt2 = std::min(20., B * R);
pt2 *= pt2;
constexpr double XXI_0 = 0.06 / 16.; //!< inverse of radiation length of the material in cm
//if(Layer==1) XXI_0=0.06/16.;
// else XXI_0=0.06/16.;
//XX_0*=1;
constexpr double geometry_factor =
0.7; //!< number between 1/3 (uniform material) and 1 (thin scatterer) to be manually tuned
constexpr double fact = geometry_factor * sqr(13.6 / 1000.);
return fact / (pt2 * (1. + sqr(slope))) * (std::abs(length) * XXI_0) *
sqr(1. + 0.038 * log(std::abs(length) * XXI_0));
}
/*!
\brief Computes the 2D rotation matrix that transforms the line y=slope*x into the line y=0.
\param slope tangent of the angle of rotation.
\return 2D rotation matrix.
*/
__host__ __device__ inline Matrix2d RotationMatrix(double slope) {
Matrix2d Rot;
Rot(0, 0) = 1. / sqrt(1. + sqr(slope));
Rot(0, 1) = slope * Rot(0, 0);
Rot(1, 0) = -Rot(0, 1);
Rot(1, 1) = Rot(0, 0);
return Rot;
}
/*!
\brief Changes the Karimäki parameters (and consequently their covariance matrix) under a translation of the coordinate system, such that the old origin has coordinates (x0,y0) in the new coordinate system. The formulas are taken from Karimäki V., 1990, Effective circle fitting for particle trajectories, Nucl. Instr. and Meth. A305 (1991) 187.
\param circle circle fit in the old coordinate system.
\param x0 x coordinate of the translation vector.
\param y0 y coordinate of the translation vector.
\param Jacob passed by reference in order to save stack.
*/
__host__ __device__ inline void TranslateKarimaki(karimaki_circle_fit& circle, double x0, double y0, Matrix3d& Jacob) {
double A, U, BB, C, DO, DP, uu, xi, v, mu, lambda, zeta;
DP = x0 * cos(circle.par(0)) + y0 * sin(circle.par(0));
DO = x0 * sin(circle.par(0)) - y0 * cos(circle.par(0)) + circle.par(1);
uu = 1 + circle.par(2) * circle.par(1);
C = -circle.par(2) * y0 + uu * cos(circle.par(0));
BB = circle.par(2) * x0 + uu * sin(circle.par(0));
A = 2. * DO + circle.par(2) * (sqr(DO) + sqr(DP));
U = sqrt(1. + circle.par(2) * A);
xi = 1. / (sqr(BB) + sqr(C));
v = 1. + circle.par(2) * DO;
lambda = (0.5 * A) / (U * sqr(1. + U));
mu = 1. / (U * (1. + U)) + circle.par(2) * lambda;
zeta = sqr(DO) + sqr(DP);
Jacob << xi * uu * v, -xi * sqr(circle.par(2)) * DP, xi * DP, 2. * mu * uu * DP, 2. * mu * v,
mu * zeta - lambda * A, 0, 0, 1.;
circle.par(0) = atan2(BB, C);
circle.par(1) = A / (1 + U);
// circle.par(2)=circle.par(2);
circle.cov = Jacob * circle.cov * Jacob.transpose();
}
/*!
\brief Computes the data needed for the Broken Line fit procedure that are mainly common for the circle and the line fit.
\param hits hits coordinates.
\param hits_cov hits covariance matrix.
\param fast_fit pre-fit result in the form (X0,Y0,R,tan(theta)).
\param B magnetic field in Gev/cm/c.
\param results PreparedBrokenLineData to be filled (see description of PreparedBrokenLineData).
*/
template <typename M3xN, typename V4, int N>
__host__ __device__ inline void prepareBrokenLineData(const M3xN& hits,
const V4& fast_fit,
const double B,
PreparedBrokenLineData<N>& results) {
constexpr auto n = N;
u_int i;
Vector2d d;
Vector2d e;
d = hits.block(0, 1, 2, 1) - hits.block(0, 0, 2, 1);
e = hits.block(0, n - 1, 2, 1) - hits.block(0, n - 2, 2, 1);
results.q = cross2D(d, e) > 0 ? -1 : 1;
const double slope = -results.q / fast_fit(3);
Matrix2d R = RotationMatrix(slope);
// calculate radii and s
results.radii = hits.block(0, 0, 2, n) - fast_fit.head(2) * MatrixXd::Constant(1, n, 1);
e = -fast_fit(2) * fast_fit.head(2) / fast_fit.head(2).norm();
for (i = 0; i < n; i++) {
d = results.radii.block(0, i, 2, 1);
results.s(i) = results.q * fast_fit(2) * atan2(cross2D(d, e), d.dot(e)); // calculates the arc length
}
VectorNd<N> z = hits.block(2, 0, 1, n).transpose();
//calculate S and Z
Matrix2xNd<N> pointsSZ = Matrix2xNd<N>::Zero();
for (i = 0; i < n; i++) {
pointsSZ(0, i) = results.s(i);
pointsSZ(1, i) = z(i);
pointsSZ.block(0, i, 2, 1) = R * pointsSZ.block(0, i, 2, 1);
}
results.S = pointsSZ.block(0, 0, 1, n).transpose();
results.Z = pointsSZ.block(1, 0, 1, n).transpose();
//calculate VarBeta
results.VarBeta(0) = results.VarBeta(n - 1) = 0;
for (i = 1; i < n - 1; i++) {
results.VarBeta(i) = MultScatt(results.S(i + 1) - results.S(i), B, fast_fit(2), i + 2, slope) +
MultScatt(results.S(i) - results.S(i - 1), B, fast_fit(2), i + 1, slope);
}
}
/*!
\brief Computes the n-by-n band matrix obtained minimizing the Broken Line's cost function w.r.t u. This is the whole matrix in the case of the line fit and the main n-by-n block in the case of the circle fit.
\param w weights of the first part of the cost function, the one with the measurements and not the angles (\sum_{i=1}^n w*(y_i-u_i)^2).
\param S total distance traveled by the particle from the pre-fitted closest approach.
\param VarBeta kink angles' variance.
\return the n-by-n matrix of the linear system
*/
template <int N>
__host__ __device__ inline MatrixNd<N> MatrixC_u(const VectorNd<N>& w,
const VectorNd<N>& S,
const VectorNd<N>& VarBeta) {
constexpr u_int n = N;
u_int i;
MatrixNd<N> C_U = MatrixNd<N>::Zero();
for (i = 0; i < n; i++) {
C_U(i, i) = w(i);
if (i > 1)
C_U(i, i) += 1. / (VarBeta(i - 1) * sqr(S(i) - S(i - 1)));
if (i > 0 && i < n - 1)
C_U(i, i) += (1. / VarBeta(i)) * sqr((S(i + 1) - S(i - 1)) / ((S(i + 1) - S(i)) * (S(i) - S(i - 1))));
if (i < n - 2)
C_U(i, i) += 1. / (VarBeta(i + 1) * sqr(S(i + 1) - S(i)));
if (i > 0 && i < n - 1)
C_U(i, i + 1) =
1. / (VarBeta(i) * (S(i + 1) - S(i))) * (-(S(i + 1) - S(i - 1)) / ((S(i + 1) - S(i)) * (S(i) - S(i - 1))));
if (i < n - 2)
C_U(i, i + 1) += 1. / (VarBeta(i + 1) * (S(i + 1) - S(i))) *
(-(S(i + 2) - S(i)) / ((S(i + 2) - S(i + 1)) * (S(i + 1) - S(i))));
if (i < n - 2)
C_U(i, i + 2) = 1. / (VarBeta(i + 1) * (S(i + 2) - S(i + 1)) * (S(i + 1) - S(i)));
C_U(i, i) *= 0.5;
}
return C_U + C_U.transpose();
}
/*!
\brief A very fast helix fit.
\param hits the measured hits.
\return (X0,Y0,R,tan(theta)).
\warning sign of theta is (intentionally, for now) mistaken for negative charges.
*/
template <typename M3xN, typename V4>
__host__ __device__ inline void BL_Fast_fit(const M3xN& hits, V4& result) {
constexpr uint32_t N = M3xN::ColsAtCompileTime;
constexpr auto n = N; // get the number of hits
const Vector2d a = hits.block(0, n / 2, 2, 1) - hits.block(0, 0, 2, 1);
const Vector2d b = hits.block(0, n - 1, 2, 1) - hits.block(0, n / 2, 2, 1);
const Vector2d c = hits.block(0, 0, 2, 1) - hits.block(0, n - 1, 2, 1);
auto tmp = 0.5 / cross2D(c, a);
result(0) = hits(0, 0) - (a(1) * c.squaredNorm() + c(1) * a.squaredNorm()) * tmp;
result(1) = hits(1, 0) + (a(0) * c.squaredNorm() + c(0) * a.squaredNorm()) * tmp;
// check Wikipedia for these formulas
result(2) = sqrt(a.squaredNorm() * b.squaredNorm() * c.squaredNorm()) / (2. * std::abs(cross2D(b, a)));
// Using Math Olympiad's formula R=abc/(4A)
const Vector2d d = hits.block(0, 0, 2, 1) - result.head(2);
const Vector2d e = hits.block(0, n - 1, 2, 1) - result.head(2);
result(3) = result(2) * atan2(cross2D(d, e), d.dot(e)) / (hits(2, n - 1) - hits(2, 0));
// ds/dz slope between last and first point
}
/*!
\brief Performs the Broken Line fit in the curved track case (that is, the fit parameters are the interceptions u and the curvature correction \Delta\kappa).
\param hits hits coordinates.
\param hits_cov hits covariance matrix.
\param fast_fit pre-fit result in the form (X0,Y0,R,tan(theta)).
\param B magnetic field in Gev/cm/c.
\param data PreparedBrokenLineData.
\param circle_results struct to be filled with the results in this form:
-par parameter of the line in this form: (phi, d, k); \n
-cov covariance matrix of the fitted parameter; \n
-chi2 value of the cost function in the minimum.
\details The function implements the steps 2 and 3 of the Broken Line fit with the curvature correction.\n
The step 2 is the least square fit, done by imposing the minimum constraint on the cost function and solving the consequent linear system. It determines the fitted parameters u and \Delta\kappa and their covariance matrix.
The step 3 is the correction of the fast pre-fitted parameters for the innermost part of the track. It is first done in a comfortable coordinate system (the one in which the first hit is the origin) and then the parameters and their covariance matrix are transformed to the original coordinate system.
*/
template <typename M3xN, typename M6xN, typename V4, int N>
__host__ __device__ inline void BL_Circle_fit(const M3xN& hits,
const M6xN& hits_ge,
const V4& fast_fit,
const double B,
PreparedBrokenLineData<N>& data,
karimaki_circle_fit& circle_results) {
constexpr u_int n = N;
u_int i;
circle_results.q = data.q;
auto& radii = data.radii;
const auto& s = data.s;
const auto& S = data.S;
auto& Z = data.Z;
auto& VarBeta = data.VarBeta;
const double slope = -circle_results.q / fast_fit(3);
VarBeta *= 1. + sqr(slope); // the kink angles are projected!
for (i = 0; i < n; i++) {
Z(i) = radii.block(0, i, 2, 1).norm() - fast_fit(2);
}
Matrix2d V; // covariance matrix
VectorNd<N> w; // weights
Matrix2d RR; // rotation matrix point by point
//double Slope; // slope of the circle point by point
for (i = 0; i < n; i++) {
V(0, 0) = hits_ge.col(i)[0]; // x errors
V(0, 1) = V(1, 0) = hits_ge.col(i)[1]; // cov_xy
V(1, 1) = hits_ge.col(i)[2]; // y errors
//Slope=-radii(0,i)/radii(1,i);
RR = RotationMatrix(-radii(0, i) / radii(1, i));
w(i) = 1. / ((RR * V * RR.transpose())(1, 1)); // compute the orthogonal weight point by point
}
VectorNplusONEd<N> r_u;
r_u(n) = 0;
for (i = 0; i < n; i++) {
r_u(i) = w(i) * Z(i);
}
MatrixNplusONEd<N> C_U;
C_U.block(0, 0, n, n) = MatrixC_u(w, s, VarBeta);
C_U(n, n) = 0;
//add the border to the C_u matrix
for (i = 0; i < n; i++) {
C_U(i, n) = 0;
if (i > 0 && i < n - 1) {
C_U(i, n) +=
-(s(i + 1) - s(i - 1)) * (s(i + 1) - s(i - 1)) / (2. * VarBeta(i) * (s(i + 1) - s(i)) * (s(i) - s(i - 1)));
}
if (i > 1) {
C_U(i, n) += (s(i) - s(i - 2)) / (2. * VarBeta(i - 1) * (s(i) - s(i - 1)));
}
if (i < n - 2) {
C_U(i, n) += (s(i + 2) - s(i)) / (2. * VarBeta(i + 1) * (s(i + 1) - s(i)));
}
C_U(n, i) = C_U(i, n);
if (i > 0 && i < n - 1)
C_U(n, n) += sqr(s(i + 1) - s(i - 1)) / (4. * VarBeta(i));
}
#ifdef CPP_DUMP
std::cout << "CU5\n" << C_U << std::endl;
#endif
MatrixNplusONEd<N> I;
math::cholesky::invert(C_U, I);
// MatrixNplusONEd<N> I = C_U.inverse();
#ifdef CPP_DUMP
std::cout << "I5\n" << I << std::endl;
#endif
VectorNplusONEd<N> u = I * r_u; // obtain the fitted parameters by solving the linear system
// compute (phi, d_ca, k) in the system in which the midpoint of the first two corrected hits is the origin...
radii.block(0, 0, 2, 1) /= radii.block(0, 0, 2, 1).norm();
radii.block(0, 1, 2, 1) /= radii.block(0, 1, 2, 1).norm();
Vector2d d = hits.block(0, 0, 2, 1) + (-Z(0) + u(0)) * radii.block(0, 0, 2, 1);
Vector2d e = hits.block(0, 1, 2, 1) + (-Z(1) + u(1)) * radii.block(0, 1, 2, 1);
circle_results.par << atan2((e - d)(1), (e - d)(0)),
-circle_results.q * (fast_fit(2) - sqrt(sqr(fast_fit(2)) - 0.25 * (e - d).squaredNorm())),
circle_results.q * (1. / fast_fit(2) + u(n));
assert(circle_results.q * circle_results.par(1) <= 0);
Vector2d eMinusd = e - d;
double tmp1 = eMinusd.squaredNorm();
Matrix3d Jacob;
Jacob << (radii(1, 0) * eMinusd(0) - eMinusd(1) * radii(0, 0)) / tmp1,
(radii(1, 1) * eMinusd(0) - eMinusd(1) * radii(0, 1)) / tmp1, 0,
(circle_results.q / 2) * (eMinusd(0) * radii(0, 0) + eMinusd(1) * radii(1, 0)) /
sqrt(sqr(2 * fast_fit(2)) - tmp1),
(circle_results.q / 2) * (eMinusd(0) * radii(0, 1) + eMinusd(1) * radii(1, 1)) /
sqrt(sqr(2 * fast_fit(2)) - tmp1),
0, 0, 0, circle_results.q;
circle_results.cov << I(0, 0), I(0, 1), I(0, n), I(1, 0), I(1, 1), I(1, n), I(n, 0), I(n, 1), I(n, n);
circle_results.cov = Jacob * circle_results.cov * Jacob.transpose();
//...Translate in the system in which the first corrected hit is the origin, adding the m.s. correction...
TranslateKarimaki(circle_results, 0.5 * (e - d)(0), 0.5 * (e - d)(1), Jacob);
circle_results.cov(0, 0) += (1 + sqr(slope)) * MultScatt(S(1) - S(0), B, fast_fit(2), 2, slope);
//...And translate back to the original system
TranslateKarimaki(circle_results, d(0), d(1), Jacob);
// compute chi2
circle_results.chi2 = 0;
for (i = 0; i < n; i++) {
circle_results.chi2 += w(i) * sqr(Z(i) - u(i));
if (i > 0 && i < n - 1)
circle_results.chi2 +=
sqr(u(i - 1) / (s(i) - s(i - 1)) - u(i) * (s(i + 1) - s(i - 1)) / ((s(i + 1) - s(i)) * (s(i) - s(i - 1))) +
u(i + 1) / (s(i + 1) - s(i)) + (s(i + 1) - s(i - 1)) * u(n) / 2) /
VarBeta(i);
}
// assert(circle_results.chi2>=0);
}
/*!
\brief Performs the Broken Line fit in the straight track case (that is, the fit parameters are only the interceptions u).
\param hits hits coordinates.
\param hits_cov hits covariance matrix.
\param fast_fit pre-fit result in the form (X0,Y0,R,tan(theta)).
\param B magnetic field in Gev/cm/c.
\param data PreparedBrokenLineData.
\param line_results struct to be filled with the results in this form:
-par parameter of the line in this form: (cot(theta), Zip); \n
-cov covariance matrix of the fitted parameter; \n
-chi2 value of the cost function in the minimum.
\details The function implements the steps 2 and 3 of the Broken Line fit without the curvature correction.\n
The step 2 is the least square fit, done by imposing the minimum constraint on the cost function and solving the consequent linear system. It determines the fitted parameters u and their covariance matrix.
The step 3 is the correction of the fast pre-fitted parameters for the innermost part of the track. It is first done in a comfortable coordinate system (the one in which the first hit is the origin) and then the parameters and their covariance matrix are transformed to the original coordinate system.
*/
template <typename V4, typename M6xN, int N>
__host__ __device__ inline void BL_Line_fit(const M6xN& hits_ge,
const V4& fast_fit,
const double B,
const PreparedBrokenLineData<N>& data,
line_fit& line_results) {
constexpr u_int n = N;
u_int i;
const auto& radii = data.radii;
const auto& S = data.S;
const auto& Z = data.Z;
const auto& VarBeta = data.VarBeta;
const double slope = -data.q / fast_fit(3);
Matrix2d R = RotationMatrix(slope);
Matrix3d V = Matrix3d::Zero(); // covariance matrix XYZ
Matrix2x3d JacobXYZtosZ = Matrix2x3d::Zero(); // jacobian for computation of the error on s (xyz -> sz)
VectorNd<N> w = VectorNd<N>::Zero();
for (i = 0; i < n; i++) {
V(0, 0) = hits_ge.col(i)[0]; // x errors
V(0, 1) = V(1, 0) = hits_ge.col(i)[1]; // cov_xy
V(0, 2) = V(2, 0) = hits_ge.col(i)[3]; // cov_xz
V(1, 1) = hits_ge.col(i)[2]; // y errors
V(2, 1) = V(1, 2) = hits_ge.col(i)[4]; // cov_yz
V(2, 2) = hits_ge.col(i)[5]; // z errors
auto tmp = 1. / radii.block(0, i, 2, 1).norm();
JacobXYZtosZ(0, 0) = radii(1, i) * tmp;
JacobXYZtosZ(0, 1) = -radii(0, i) * tmp;
JacobXYZtosZ(1, 2) = 1.;
w(i) = 1. / ((R * JacobXYZtosZ * V * JacobXYZtosZ.transpose() * R.transpose())(
1, 1)); // compute the orthogonal weight point by point
}
VectorNd<N> r_u;
for (i = 0; i < n; i++) {
r_u(i) = w(i) * Z(i);
}
#ifdef CPP_DUMP
std::cout << "CU4\n" << MatrixC_u(w, S, VarBeta) << std::endl;
#endif
MatrixNd<N> I;
math::cholesky::invert(MatrixC_u(w, S, VarBeta), I);
// MatrixNd<N> I=MatrixC_u(w,S,VarBeta).inverse();
#ifdef CPP_DUMP
std::cout << "I4\n" << I << std::endl;
#endif
VectorNd<N> u = I * r_u; // obtain the fitted parameters by solving the linear system
// line parameters in the system in which the first hit is the origin and with axis along SZ
line_results.par << (u(1) - u(0)) / (S(1) - S(0)), u(0);
auto idiff = 1. / (S(1) - S(0));
line_results.cov << (I(0, 0) - 2 * I(0, 1) + I(1, 1)) * sqr(idiff) +
MultScatt(S(1) - S(0), B, fast_fit(2), 2, slope),
(I(0, 1) - I(0, 0)) * idiff, (I(0, 1) - I(0, 0)) * idiff, I(0, 0);
// translate to the original SZ system
Matrix2d Jacob;
Jacob(0, 0) = 1.;
Jacob(0, 1) = 0;
Jacob(1, 0) = -S(0);
Jacob(1, 1) = 1.;
line_results.par(1) += -line_results.par(0) * S(0);
line_results.cov = Jacob * line_results.cov * Jacob.transpose();
// rotate to the original sz system
auto tmp = R(0, 0) - line_results.par(0) * R(0, 1);
Jacob(1, 1) = 1. / tmp;
Jacob(0, 0) = Jacob(1, 1) * Jacob(1, 1);
Jacob(0, 1) = 0;
Jacob(1, 0) = line_results.par(1) * R(0, 1) * Jacob(0, 0);
line_results.par(1) = line_results.par(1) * Jacob(1, 1);
line_results.par(0) = (R(0, 1) + line_results.par(0) * R(0, 0)) * Jacob(1, 1);
line_results.cov = Jacob * line_results.cov * Jacob.transpose();
// compute chi2
line_results.chi2 = 0;
for (i = 0; i < n; i++) {
line_results.chi2 += w(i) * sqr(Z(i) - u(i));
if (i > 0 && i < n - 1)
line_results.chi2 +=
sqr(u(i - 1) / (S(i) - S(i - 1)) - u(i) * (S(i + 1) - S(i - 1)) / ((S(i + 1) - S(i)) * (S(i) - S(i - 1))) +
u(i + 1) / (S(i + 1) - S(i))) /
VarBeta(i);
}
// assert(line_results.chi2>=0);
}
/*!
\brief Helix fit by three step:
-fast pre-fit (see Fast_fit() for further info); \n
-circle fit of the hits projected in the transverse plane by Broken Line algorithm (see BL_Circle_fit() for further info); \n
-line fit of the hits projected on the (pre-fitted) cilinder surface by Broken Line algorithm (see BL_Line_fit() for further info); \n
Points must be passed ordered (from inner to outer layer).
\param hits Matrix3xNd hits coordinates in this form: \n
|x1|x2|x3|...|xn| \n
|y1|y2|y3|...|yn| \n
|z1|z2|z3|...|zn|
\param hits_cov Matrix3Nd covariance matrix in this form (()->cov()): \n
|(x1,x1)|(x2,x1)|(x3,x1)|(x4,x1)|.|(y1,x1)|(y2,x1)|(y3,x1)|(y4,x1)|.|(z1,x1)|(z2,x1)|(z3,x1)|(z4,x1)| \n
|(x1,x2)|(x2,x2)|(x3,x2)|(x4,x2)|.|(y1,x2)|(y2,x2)|(y3,x2)|(y4,x2)|.|(z1,x2)|(z2,x2)|(z3,x2)|(z4,x2)| \n
|(x1,x3)|(x2,x3)|(x3,x3)|(x4,x3)|.|(y1,x3)|(y2,x3)|(y3,x3)|(y4,x3)|.|(z1,x3)|(z2,x3)|(z3,x3)|(z4,x3)| \n
|(x1,x4)|(x2,x4)|(x3,x4)|(x4,x4)|.|(y1,x4)|(y2,x4)|(y3,x4)|(y4,x4)|.|(z1,x4)|(z2,x4)|(z3,x4)|(z4,x4)| \n
. . . . . . . . . . . . . . . \n
|(x1,y1)|(x2,y1)|(x3,y1)|(x4,y1)|.|(y1,y1)|(y2,y1)|(y3,x1)|(y4,y1)|.|(z1,y1)|(z2,y1)|(z3,y1)|(z4,y1)| \n
|(x1,y2)|(x2,y2)|(x3,y2)|(x4,y2)|.|(y1,y2)|(y2,y2)|(y3,x2)|(y4,y2)|.|(z1,y2)|(z2,y2)|(z3,y2)|(z4,y2)| \n
|(x1,y3)|(x2,y3)|(x3,y3)|(x4,y3)|.|(y1,y3)|(y2,y3)|(y3,x3)|(y4,y3)|.|(z1,y3)|(z2,y3)|(z3,y3)|(z4,y3)| \n
|(x1,y4)|(x2,y4)|(x3,y4)|(x4,y4)|.|(y1,y4)|(y2,y4)|(y3,x4)|(y4,y4)|.|(z1,y4)|(z2,y4)|(z3,y4)|(z4,y4)| \n
. . . . . . . . . . . . . . . \n
|(x1,z1)|(x2,z1)|(x3,z1)|(x4,z1)|.|(y1,z1)|(y2,z1)|(y3,z1)|(y4,z1)|.|(z1,z1)|(z2,z1)|(z3,z1)|(z4,z1)| \n
|(x1,z2)|(x2,z2)|(x3,z2)|(x4,z2)|.|(y1,z2)|(y2,z2)|(y3,z2)|(y4,z2)|.|(z1,z2)|(z2,z2)|(z3,z2)|(z4,z2)| \n
|(x1,z3)|(x2,z3)|(x3,z3)|(x4,z3)|.|(y1,z3)|(y2,z3)|(y3,z3)|(y4,z3)|.|(z1,z3)|(z2,z3)|(z3,z3)|(z4,z3)| \n
|(x1,z4)|(x2,z4)|(x3,z4)|(x4,z4)|.|(y1,z4)|(y2,z4)|(y3,z4)|(y4,z4)|.|(z1,z4)|(z2,z4)|(z3,z4)|(z4,z4)|
\param B magnetic field in the center of the detector in Gev/cm/c, in order to perform the p_t calculation.
\warning see BL_Circle_fit(), BL_Line_fit() and Fast_fit() warnings.
\bug see BL_Circle_fit(), BL_Line_fit() and Fast_fit() bugs.
\return (phi,Tip,p_t,cot(theta)),Zip), their covariance matrix and the chi2's of the circle and line fits.
*/
template <int N>
inline helix_fit BL_Helix_fit(const Matrix3xNd<N>& hits, const Eigen::Matrix<float, 6, 4>& hits_ge, const double B) {
helix_fit helix;
Vector4d fast_fit;
BL_Fast_fit(hits, fast_fit);
PreparedBrokenLineData<N> data;
karimaki_circle_fit circle;
line_fit line;
Matrix3d Jacob;
prepareBrokenLineData(hits, fast_fit, B, data);
BL_Line_fit(hits_ge, fast_fit, B, data, line);
BL_Circle_fit(hits, hits_ge, fast_fit, B, data, circle);
// the circle fit gives k, but here we want p_t, so let's change the parameter and the covariance matrix
Jacob << 1., 0, 0, 0, 1., 0, 0, 0, -std::abs(circle.par(2)) * B / (sqr(circle.par(2)) * circle.par(2));
circle.par(2) = B / std::abs(circle.par(2));
circle.cov = Jacob * circle.cov * Jacob.transpose();
helix.par << circle.par, line.par;
helix.cov = MatrixXd::Zero(5, 5);
helix.cov.block(0, 0, 3, 3) = circle.cov;
helix.cov.block(3, 3, 2, 2) = line.cov;
helix.q = circle.q;
helix.chi2_circle = circle.chi2;
helix.chi2_line = line.chi2;
return helix;
}
} // namespace BrokenLine
#endif // RecoPixelVertexing_PixelTrackFitting_interface_BrokenLine_h