Belle II Software development
VertexFitKFit.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * External Contributor: J. Tanaka *
5 * *
6 * See git log for contributors and copyright holders. *
7 * This file is licensed under LGPL-3.0, see LICENSE.md. *
8 **************************************************************************/
9
10
11#include <algorithm>
12#include <cstdio>
13
14#include <TMath.h>
15#include <TMatrixFSym.h>
16
17#include <analysis/VertexFitting/KFit/MakeMotherKFit.h>
18#include <analysis/VertexFitting/KFit/VertexFitKFit.h>
19#include <analysis/utility/CLHEPToROOT.h>
20#include <analysis/utility/ROOTToCLHEP.h>
21#include <framework/gearbox/Const.h>
22
23using namespace std;
24using namespace Belle2;
25using namespace Belle2::analysis;
26using namespace CLHEP;
27
29 m_BeforeVertex(HepPoint3D(0, 0, 0)),
30 m_AfterVertexError(HepSymMatrix(3, 0)),
31 m_BeamError(HepSymMatrix(3, 0))
32{
33 m_CorrelationMode = false;
34 m_FlagFitted = false;
35 m_FlagKnownVertex = false;
36 m_FlagBeam = false;
37 m_FlagTube = false;
38 m_iTrackTube = -1;
39 m_CHIsqVertex = 0;
41 m_V_E = HepMatrix(3, 3, 0);
42 m_v = HepMatrix(3, 1, 0);
43 m_v_a = HepMatrix(3, 1, 0);
44
46}
47
49
50
52VertexFitKFit::setInitialVertex(const HepPoint3D& v) {
54
56}
57
58enum KFitError::ECode VertexFitKFit::setInitialVertex(const ROOT::Math::XYZVector& v)
59{
60 m_BeforeVertex = ROOTToCLHEP::getPoint3D(v);
62 return m_ErrorCode;
63}
64
66VertexFitKFit::setIpProfile(const HepPoint3D& ip, const HepSymMatrix& ipe) {
67 if (m_FlagTube)
68 {
69 char buf[1024];
70 sprintf(buf, "%s:%s(): already constrained to IPtube", __FILE__, __func__);
71 B2FATAL(buf);
72 }
73
74 m_FlagBeam = true;
75 m_BeforeVertex = ip;
76 m_BeamError = ipe;
77
79}
80
81
83VertexFitKFit::setIpTubeProfile(const HepLorentzVector& p, const HepPoint3D& x, const HepSymMatrix& e, const double q) {
84 if (m_FlagBeam)
85 {
86 char buf[1024];
87 sprintf(buf, "%s:%s(): already constrained to IP", __FILE__, __func__);
88 B2FATAL(buf);
89 }
90
91 m_FlagTube = true;
92 m_TubeTrack = KFitTrack(p, x, e, q);
93
95}
96
97
100 m_FlagKnownVertex = flag;
101
103}
104
105
112
113
114const HepPoint3D
115VertexFitKFit::getVertex(const int flag) const
116{
117 if (flag == KFitConst::kAfterFit && !isFitted()) return HepPoint3D();
118
119 switch (flag) {
121 return m_BeforeVertex;
122
124 return m_AfterVertex;
125
126 default:
127 KFitError::displayError(__FILE__, __LINE__, __func__, KFitError::kOutOfRange);
128 return HepPoint3D();
129 }
130}
131
132
133const HepSymMatrix
138
139
140double
142{
143 return KFitBase::getCHIsq();
144}
145
146
147double
149{
150 // only m_FlagBeam = 1
151 return m_CHIsqVertex;
152}
153
154
155const HepMatrix
157{
158 if (!isTrackIDInRange(id)) return HepMatrix(3, KFitConst::kNumber7, 0);
159 return m_AfterTrackVertexError[id];
160}
161
162
163double
165{
166 if (!isTrackIDInRange(id)) return -1;
167
169 return KFitBase::getTrackCHIsq(id);
170 }
171
172 return m_EachCHIsq[id];
173}
174
175
176double
178{
180 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
181 return -1;
182 }
183
184 if (m_TrackCount == 0) {
185 KFitError::displayError(__FILE__, __LINE__, __func__, KFitError::kBadTrackSize);
186 return -1;
187 }
188
189 double chisq = 0.0;
190 for (int i = 0; i < m_TrackCount; i++) {
191 const double i_chisq = this->getTrackCHIsq(i);
192 chisq += i_chisq;
193 }
194
195 return chisq;
196}
197
198
199int
201{
203 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
204 return 0;
205 }
206
207 if (m_TrackCount == 0) {
208 KFitError::displayError(__FILE__, __LINE__, __func__, KFitError::kBadTrackSize);
209 return 0;
210 }
211
212 return m_TrackCount * 2 - 2;
213}
214
215
218 if (m_FlagTube) this->appendTube();
219
220 if (m_FlagBeam) m_ErrorCode = this->doFit4();
221 else if (m_FlagKnownVertex) m_ErrorCode = this->doFit5();
222 else if (m_CorrelationMode) m_ErrorCode = this->doFit2();
223 else
224 m_ErrorCode = this->doFit3();
225
226 const enum KFitError::ECode tmp_ErrorCode = m_ErrorCode;
227
228 if (m_FlagTube) this->deleteTube();
229
230 if (tmp_ErrorCode == KFitError::kNoError) m_FlagFitted = true;
231
232 return m_ErrorCode = tmp_ErrorCode;
233}
234
235
238 // use small Matrix --> No Correlation
240
242 {
244 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
245 return m_ErrorCode;
246 }
247
250
251
252 double chisq = 0;
253 double tmp2_chisq = KFitConst::kInitialCHIsq;
254 int err_inverse = 0;
255
256 m_al_a = m_al_0;
257 HepMatrix tmp_al_a(m_al_a);
258
259 HepMatrix tmp_D(m_D), tmp_E(m_E);
260 HepMatrix tmp_V_D(m_V_D), tmp_V_E(m_V_E);
261 HepMatrix tmp_lam0(m_lam0), tmp_v_a(m_v_a);
262
263 HepMatrix tmp2_D(m_D), tmp2_E(m_E);
264 HepMatrix tmp2_V_D(m_V_D), tmp2_V_E(m_V_E);
265 HepMatrix tmp2_lam0(m_lam0), tmp2_v_a(m_v_a);
266
267 std::vector<double> tmp_each_chisq(m_TrackCount);
268 std::vector<double> tmp2_each_chisq(m_TrackCount);
269
270 for (int j = 0; j < KFitConst::kMaxIterationCount; j++) // j'th loop start
271 {
272
273 double tmp_chisq = KFitConst::kInitialCHIsq;
274
275 for (int i = 0; i < KFitConst::kMaxIterationCount; i++) { // i'th loop start
276
279
280 HepMatrix tV_Ein(3, 3, 0);
281 chisq = 0;
282
283 for (int k = 0; k < m_TrackCount; k++) { // k'th loop start
284
285 HepMatrix tD = m_D.sub(2 * k + 1, 2 * (k + 1), KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1)); // 2x6
286 HepMatrix tV_D = ((m_V_al_0.sub(KFitConst::kNumber6 * k + 1,
287 (int)(KFitConst::kNumber6 * (k + 1)))).similarity(tD)).inverse(err_inverse); // 2x2
288 if (err_inverse) {
290 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
291 return m_ErrorCode;
292 }
293
294 m_V_D.sub(2 * k + 1, 2 * k + 1, tV_D);
295 HepMatrix tE = m_E.sub(2 * k + 1, 2 * (k + 1), 1, 3); // 2x3
296 tV_Ein += (tE.T()) * tV_D * tE; // 3x3
297 HepMatrix tDeltaAl = (m_al_0 - m_al_1).sub(KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1), 1, 1); // 6x1
298 HepMatrix td = m_d.sub(2 * k + 1, 2 * (k + 1), 1, 1); // 2x1
299 HepMatrix tlam0 = tV_D * (tD * tDeltaAl + td); // 2x2x(2x6x6x1+2x1) = 2x1
300 m_lam0.sub(2 * k + 1, 1, tlam0);
301 m_EachCHIsq[k] = ((tlam0.T()) * (tD * tDeltaAl + tE * (m_v - m_v_a) + td))(1, 1); // 1x2x(2x6x6x1+2x3x3x1+2x1)
302 chisq += m_EachCHIsq[k];
303 } // k'th loop over
304
305 m_V_E = tV_Ein.inverse(err_inverse);
306 if (err_inverse) {
308 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
309 return m_ErrorCode;
310 }
311
312 m_v_a = m_v_a - m_V_E * (m_E.T()) * m_lam0;
313
314 if (tmp_chisq <= chisq) {
315 if (i == 0) {
317 } else {
318 for (int k = 0; k < m_TrackCount; k++) m_EachCHIsq[k] = tmp_each_chisq[k];
319 chisq = tmp_chisq;
320 m_v_a = tmp_v_a;
321 m_V_E = tmp_V_E;
322 m_V_D = tmp_V_D;
323 m_lam0 = tmp_lam0;
324 m_E = tmp_E;
325 m_D = tmp_D;
326 }
327 break;
328 } else {
329 for (int k = 0; k < m_TrackCount; k++) tmp_each_chisq[k] = m_EachCHIsq[k];
330 tmp_chisq = chisq;
331 tmp_v_a = m_v_a;
332 tmp_V_E = m_V_E;
333 tmp_V_D = m_V_D;
334 tmp_lam0 = m_lam0;
335 tmp_E = m_E;
336 tmp_D = m_D;
337 if (i == KFitConst::kMaxIterationCount - 1) {
338 m_FlagOverIteration = true;
339 }
340 }
341 } // i'th loop over
342
343 m_al_a = m_al_1;
344 m_lam = m_lam0 - m_V_D * m_E * m_V_E * (m_E.T()) * m_lam0;
345 m_al_1 = m_al_0 - m_V_al_0 * (m_D.T()) * m_lam;
346
347 if (j == 0) {
348
349 for (int k = 0; k < m_TrackCount; k++) tmp2_each_chisq[k] = m_EachCHIsq[k];
350 tmp2_chisq = chisq;
351 tmp2_v_a = m_v_a;
352 tmp2_V_E = m_V_E;
353 tmp2_V_D = m_V_D;
354 tmp2_lam0 = m_lam0;
355 tmp2_E = m_E;
356 tmp2_D = m_D;
357 tmp_al_a = m_al_a;
358
359 } else {
360
361 if (tmp2_chisq <= chisq) {
362 for (int k = 0; k < m_TrackCount; k++) m_EachCHIsq[k] = tmp2_each_chisq[k];
363 chisq = tmp2_chisq;
364 m_v_a = tmp2_v_a;
365 m_V_E = tmp2_V_E;
366 m_V_D = tmp2_V_D;
367 m_lam0 = tmp2_lam0;
368 m_E = tmp2_E;
369 m_D = tmp2_D;
370 m_al_a = tmp_al_a;
371 break;
372 } else {
373 for (int k = 0; k < m_TrackCount; k++) tmp2_each_chisq[k] = m_EachCHIsq[k];
374 tmp2_chisq = chisq;
375 tmp2_v_a = m_v_a;
376 tmp2_V_E = m_V_E;
377 tmp2_V_D = m_V_D;
378 tmp2_lam0 = m_lam0;
379 tmp2_E = m_E;
380 tmp2_D = m_D;
381 tmp_al_a = m_al_a;
382 }
383 }
384 } // j'th loop over
385
386
388
389 m_lam = m_lam0 - m_V_D * m_E * m_V_E * (m_E.T()) * m_lam0;
390 m_al_1 = m_al_0 - m_V_al_0 * (m_D.T()) * m_lam;
391 m_V_Dt = m_V_D - m_V_D * m_E * m_V_E * (m_E.T()) * m_V_D;
392 m_V_al_1 = m_V_al_0 - m_V_al_0 * (m_D.T()) * m_V_Dt * m_D * m_V_al_0;
393 m_Cov_v_al_1 = -m_V_E * (m_E.T()) * m_V_D * m_D * m_V_al_0;
394
396
397 m_CHIsq = chisq;
398
400}
401
402
405 // included beam position constraint (only no correlation)
407
409 {
411 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
412 return m_ErrorCode;
413 }
414
417
418
419 double chisq = 0;
420 double tmp_chisq = KFitConst::kInitialCHIsq;
421 int err_inverse = 0;
422
423 m_al_a = m_al_0;
424 HepMatrix tmp_al_a(m_al_a);
425
426 HepMatrix tmp_D(m_D), tmp_E(m_E);
427 HepMatrix tmp_lam(m_lam);
428
429 // vertex
430 m_v[0][0] = m_BeforeVertex.x();
431 m_v[1][0] = m_BeforeVertex.y();
432 m_v[2][0] = m_BeforeVertex.z();
433
434 std::vector<double> tmp_each_chisq(m_TrackCount);
435 double tmp_vertex_chisq = 1.e+30; // An init-value is not needed but the C++ compiler requires the init-value.
436
437 // to avoid overestimation of vertex-z error.
438 bool it_flag = false;
439
440 for (int i = 0; i < KFitConst::kMaxIterationCount ; i++)
441 {
442
444
445 chisq = 0;
446
447 HepMatrix tV_Dtin = m_V_al_0.similarity(m_D) + m_BeamError.similarity(m_E);
448 HepMatrix tV_Dt = tV_Dtin.inverse(err_inverse);
449 if (err_inverse) {
451 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
452 return m_ErrorCode;
453 }
454 m_lam = tV_Dt * (m_D * (m_al_0 - m_al_1) + m_E * (m_v - m_v_a) + m_d); // (2*nTrk)x1
455 for (int k = 0; k < m_TrackCount; k++) {
456 HepMatrix tD = m_D.sub(2 * k + 1, 2 * (k + 1), KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1)); // 2x6
457 HepMatrix tDeltaAl = (m_al_0 - m_al_1).sub(KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1), 1, 1); // 6x1
458 HepMatrix td = m_d.sub(2 * k + 1, 2 * (k + 1), 1, 1); // 2x1
459 HepMatrix tE = m_E.sub(2 * k + 1, 2 * (k + 1), 1, 3); // 2x3
460 chisq += ((m_lam.sub(2 * k + 1, 2 * (k + 1), 1, 1).T()) * (tD * tDeltaAl + tE * (m_v - m_v_a) + td))(1,
461 1); // 1x2x(2x6x6x1+2x3x3x1+2x1)
462 m_EachCHIsq[k] = (m_lam.sub(2 * k + 1, 2 * (k + 1), 1, 1).T() * tD * m_V_al_0.sub(KFitConst::kNumber6 * k + 1,
463 KFitConst::kNumber6 * (k + 1)) * (tD.T()) * m_lam.sub(2 * k + 1, 2 * (k + 1), 1, 1))(1, 1);
464 }
465
466 m_CHIsqVertex = (m_lam.T() * m_E * m_BeamError * (m_E.T()) * m_lam)(1, 1);
467 m_al_a = m_al_1;
468 m_v_a = m_v - m_BeamError * (m_E.T()) * m_lam;
469 m_al_1 = m_al_0 - m_V_al_0 * (m_D.T()) * m_lam;
470
471 if (tmp_chisq <= chisq && it_flag) {
472 if (i == 0) {
474 } else {
475 for (int k = 0; k < m_TrackCount; k++) m_EachCHIsq[k] = tmp_each_chisq[k];
476 m_CHIsqVertex = tmp_vertex_chisq;
477 chisq = tmp_chisq;
478 m_lam = tmp_lam;
479 m_E = tmp_E;
480 m_D = tmp_D;
481 m_al_a = tmp_al_a;
482 }
483 break;
484 } else {
485 if (tmp_chisq <= chisq) it_flag = true;
486 for (int k = 0; k < m_TrackCount; k++) tmp_each_chisq[k] = m_EachCHIsq[k];
487 tmp_vertex_chisq = m_CHIsqVertex;
488 tmp_chisq = chisq;
489 tmp_lam = m_lam;
490 tmp_E = m_E;
491 tmp_D = m_D;
492 tmp_al_a = m_al_a;
493 if (i == KFitConst::kMaxIterationCount - 1) {
494 m_FlagOverIteration = true;
495 }
496 }
497 }
498
499
501
502 m_al_1 = m_al_0 - m_V_al_0 * (m_D.T()) * m_lam;
503 m_v_a = m_v - m_BeamError * (m_E.T()) * m_lam;
504 HepMatrix tV_Dtin = m_V_al_0.similarity(m_D) + m_BeamError.similarity(m_E);
505 m_V_Dt = tV_Dtin.inverse(err_inverse);
506 if (err_inverse)
507 {
509 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
510 return m_ErrorCode;
511 }
512
513 m_V_al_1 = m_V_al_0 - m_V_al_0 * (m_D.T()) * m_V_Dt * m_D * m_V_al_0;
515 // m_V_v is m_V_E
516 // --> need to replace m_V_E for my implementation.
518
520
521 m_CHIsq = chisq;
522
524}
525
526
529 // known vertex --> do not find vertex. (only no correlation)
531
533 {
535 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
536 return m_ErrorCode;
537 }
538
541
542
543 double chisq = 0;
544 double tmp_chisq = KFitConst::kInitialCHIsq;
545 int err_inverse = 0;
546
547 m_al_a = m_al_0;
548 HepMatrix tmp_al_a(m_al_a);
549
550 HepMatrix tmp_al_0(m_al_1);
551 HepMatrix tmp_V_al_0(m_V_al_1);
552
553 std::vector<double> tmp_each_chisq(m_TrackCount);
554
555 for (int i = 0; i < KFitConst::kMaxIterationCount; i++)
556 {
557
559
560 chisq = 0;
561
562 for (int k = 0; k < m_TrackCount; k++) {
563 HepMatrix tD = m_D.sub(2 * k + 1, 2 * (k + 1), KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1)); // 2x6
564 HepMatrix tV_D = ((m_V_al_0.sub(KFitConst::kNumber6 * k + 1,
565 (int)(KFitConst::kNumber6 * (k + 1)))).similarity(tD)).inverse(err_inverse); // 2x2
566 if (err_inverse) {
568 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
569 return m_ErrorCode;
570 }
571 m_V_D.sub(2 * k + 1, 2 * k + 1, tV_D);
572
573 HepMatrix tDeltaAl = (m_al_0 - m_al_1).sub(KFitConst::kNumber6 * k + 1, KFitConst::kNumber6 * (k + 1), 1, 1); // 6x1
574 HepMatrix td = m_d.sub(2 * k + 1, 2 * (k + 1), 1, 1); // 2x1
575 HepMatrix tlam = tV_D * (tD * tDeltaAl + td); // 2x2x(2x6x6x1+2x1) = 2x1
576 m_lam.sub(2 * k + 1, 1, tlam);
577 m_EachCHIsq[k] = ((tlam.T()) * (tD * tDeltaAl + td))(1, 1); // 1x2x(2x6x6x1+2x1)
578 chisq += m_EachCHIsq[k];
579 }
580
581 m_al_a = m_al_1;
582 m_al_1 = m_al_0 - m_V_al_0 * (m_D.T()) * m_lam;
583 m_V_al_1 = m_V_al_0 - m_V_al_0 * (m_D.T()) * m_V_D * m_D * m_V_al_0;
584
585 if (tmp_chisq <= chisq) {
586 if (i == 0) {
588 } else {
589 for (int k = 0; k < m_TrackCount; k++) m_EachCHIsq[k] = tmp_each_chisq[k];
590 chisq = tmp_chisq;
591 m_al_1 = tmp_al_0;
592 m_V_al_1 = tmp_V_al_0;
593 m_al_a = tmp_al_a;
594 }
595 break;
596 } else {
597 for (int k = 0; k < m_TrackCount; k++) tmp_each_chisq[k] = m_EachCHIsq[k];
598 tmp_chisq = chisq;
599 tmp_al_0 = m_al_1;
600 tmp_V_al_0 = m_V_al_1;
601 tmp_al_a = m_al_a;
602 if (i == KFitConst::kMaxIterationCount - 1) {
603 m_FlagOverIteration = true;
604 }
605 }
606 }
607
608
610
612
613 m_CHIsq = chisq;
614
616}
617
618
622 {
625 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
626 return m_ErrorCode;
627 }
628 } else
629 {
632 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
633 return m_ErrorCode;
634 }
635 }
636
637
638 int index = 0;
639 HepMatrix tmp_al_0(KFitConst::kNumber6 * m_TrackCount, 1, 0);
640 HepSymMatrix tmp_V_al_0(KFitConst::kNumber6 * m_TrackCount, 0);
641 HepMatrix tmp_property(m_TrackCount, 3, 0);
642
643
644 for (auto& track : m_Tracks)
645 {
646 // momentum x,y,z and position x,y,z
647 for (int j = 0; j < KFitConst::kNumber6; j++)
648 tmp_al_0[index * KFitConst::kNumber6 + j][0] = track.getFitParameter(j, KFitConst::kBeforeFit);
649 // these error
650 tmp_V_al_0.sub(index * KFitConst::kNumber6 + 1, track.getFitError(KFitConst::kBeforeFit));
651 // charge , mass , a
652 tmp_property[index][0] = track.getCharge();
653 tmp_property[index][1] = track.getMass();
654 const double c = Belle2::Const::speedOfLight * 1e-4;
655 tmp_property[index][2] = -c * m_MagneticField * track.getCharge();
656 index++;
657 }
658
659 // error between tarck and track
660 m_V_al_0 = tmp_V_al_0;
662 {
663 if (m_FlagCorrelation) {
664 this->prepareCorrelation();
666 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
667 return m_ErrorCode;
668 }
669 }
670 }
671
672 // vertex
673 m_v_a[0][0] = m_BeforeVertex.x();
674 m_v_a[1][0] = m_BeforeVertex.y();
675 m_v_a[2][0] = m_BeforeVertex.z();
676
677 // set member matrix
678 m_al_0 = tmp_al_0;
679 m_al_1 = m_al_0;
680 m_property = tmp_property;
681
682 // define size of matrix
685 m_E = m_V_al_1.sub(1, m_TrackCount * 2, 1, 3);
686 m_d = m_V_al_1.sub(1, m_TrackCount * 2, 1, 1);
687 m_V_D = m_V_al_1.sub(1, m_TrackCount * 2, 1, m_TrackCount * 2);
688 m_lam = m_V_al_1.sub(1, m_TrackCount * 2, 1, 1);
689 m_lam0 = m_V_al_1.sub(1, m_TrackCount * 2, 1, 1);
690 m_V_Dt = m_V_al_1.sub(1, m_TrackCount * 2, 1, m_TrackCount * 2);
692
694}
695
696
699 // vertex
700 for (int i = 0; i < 3; i++)
701 {
702 m_v[i][0] = m_v_a[i][0];
703 }
705}
706
707
710 Hep3Vector h3v;
711 unsigned index = 0;
712
713 for (auto& pdata : m_Tracks)
714 {
715 // tracks
716 // momentum
717 h3v.setX(m_al_1[index * KFitConst::kNumber6 + 0][0]);
718 h3v.setY(m_al_1[index * KFitConst::kNumber6 + 1][0]);
719 h3v.setZ(m_al_1[index * KFitConst::kNumber6 + 2][0]);
720 pdata.setMomentum(HepLorentzVector(h3v, sqrt(h3v.mag2() + pdata.getMass()*pdata.getMass())), KFitConst::kAfterFit);
721 // position
722 pdata.setPosition(HepPoint3D(
723 m_al_1[index * KFitConst::kNumber6 + 3][0],
724 m_al_1[index * KFitConst::kNumber6 + 4][0],
726 // error of the tracks
727 pdata.setError(makeError1(pdata.getMomentum(),
728 m_V_al_1.sub(
729 index * KFitConst::kNumber6 + 1,
730 (index + 1)*KFitConst::kNumber6,
731 index * KFitConst::kNumber6 + 1,
732 (index + 1)*KFitConst::kNumber6)),
734 if (m_ErrorCode != KFitError::kNoError) break;
735 index++;
736 }
737
738 // vertex
739 m_AfterVertex.setX(m_v_a[0][0]);
740 m_AfterVertex.setY(m_v_a[1][0]);
741 m_AfterVertex.setZ(m_v_a[2][0]);
742
743 // error of the vertex
744 for (int i = 0; i < 3; i++) for (int j = i; j < 3; j++)
745 {
746 m_AfterVertexError[i][j] = m_V_E[i][j];
747 }
748
749 // error between vertex and tracks
750 for (int i = 0; i < m_TrackCount; i++)
751 {
752 HepMatrix hm(3, KFitConst::kNumber6, 0);
753 for (int j = 0; j < 3; j++) for (int k = 0; k < KFitConst::kNumber6; k++) {
754 hm[j][k] = m_Cov_v_al_1[j][KFitConst::kNumber6 * i + k];
755 }
756 m_AfterTrackVertexError.push_back(makeError2(m_Tracks[i].getMomentum(), hm));
757 }
758
760}
761
762
765 // vertex fit
766 for (int i = 0; i < m_TrackCount; i++)
767 {
768 double S, U;
769 double sininv;
770
771 double px = m_al_1[i * KFitConst::kNumber6 + 0][0];
772 double py = m_al_1[i * KFitConst::kNumber6 + 1][0];
773 double pz = m_al_1[i * KFitConst::kNumber6 + 2][0];
774 double x = m_al_1[i * KFitConst::kNumber6 + 3][0];
775 double y = m_al_1[i * KFitConst::kNumber6 + 4][0];
776 double z = m_al_1[i * KFitConst::kNumber6 + 5][0];
777 double a = m_property[i][2];
778
779 double pt = sqrt(px * px + py * py);
780 if (pt == 0) {
782 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
783 return m_ErrorCode;
784 }
785
786 double invPt = 1. / pt;
787 double invPt2 = invPt * invPt;
788 double dlx = m_v_a[0][0] - x;
789 double dly = m_v_a[1][0] - y;
790 double dlz = m_v_a[2][0] - z;
791 double a1 = -dlx * py + dly * px;
792 double a2 = dlx * px + dly * py;
793 double r2d2 = dlx * dlx + dly * dly;
794 double Rx = dlx - 2.*px * a2 * invPt2;
795 double Ry = dly - 2.*py * a2 * invPt2;
796
797 if (a != 0) { // charged
798
799 double B = a * a2 * invPt2;
800 if (fabs(B) > 1) {
802 B2DEBUG(10, "KFitError: Cannot calculate arcsin");
803 //KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
804 return m_ErrorCode;
805 }
806 // sin^(-1)(B)
807 sininv = asin(B);
808 double tmp0 = 1.0 - B * B;
809 if (tmp0 == 0) {
811 KFitError::displayError(__FILE__, __LINE__, __func__, m_ErrorCode);
812 return m_ErrorCode;
813 }
814 // 1/sqrt(1-B^2)
815 double sqrtag = 1.0 / sqrt(tmp0);
816 S = sqrtag * invPt2;
817 U = dlz - pz * sininv / a;
818
819 } else { // neutral
820
821 sininv = 0.0;
822 S = invPt2;
823 U = dlz - pz * a2 * invPt2;
824 }
825
826 // d
827 m_d[i * 2 + 0][0] = a1 - 0.5 * a * r2d2;
828 m_d[i * 2 + 1][0] = U * pt;
829
830 // D
831 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 0] = dly;
832 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 1] = -dlx;
833 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 2] = 0.0;
834 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 3] = py + a * dlx;
835 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 4] = -px + a * dly;
836 m_D[i * 2 + 0][i * KFitConst::kNumber6 + 5] = 0.0;
837 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 0] = -pz * pt * S * Rx + U * px * invPt;
838 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 1] = -pz * pt * S * Ry + U * py * invPt;
839 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 2] = a != 0 ? -sininv * pt / a : -a2 * invPt;
840 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 3] = px * pz * pt * S;
841 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 4] = py * pz * pt * S;
842 m_D[i * 2 + 1][i * KFitConst::kNumber6 + 5] = -pt;
843
844 // E
845 m_E[i * 2 + 0][0] = -py - a * dlx;
846 m_E[i * 2 + 0][1] = px - a * dly;
847 m_E[i * 2 + 0][2] = 0.0;
848 m_E[i * 2 + 1][0] = -px * pz * pt * S;
849 m_E[i * 2 + 1][1] = -py * pz * pt * S;
850 m_E[i * 2 + 1][2] = pt;
851 }
852
854}
855
856
859 if (m_FlagBeam) m_NDF = 2 * m_TrackCount;
860 else if (m_FlagTube) m_NDF = 2 * (m_TrackCount - 1) - 1;
861 else if (m_FlagKnownVertex) m_NDF = 2 * m_TrackCount;
862 else m_NDF = 2 * m_TrackCount - 3;
863
865}
866
867
871
872 if (m_iTrackTube != -1)
873 {
874 char buf[1024];
875 sprintf(buf, "%s:%s(): internal error; duplicated appendTube() call?", __FILE__, __func__);
876 B2FATAL(buf);
877 }
878
879 m_Tracks.push_back(m_TubeTrack);
880 m_TrackCount = m_Tracks.size();
882
884}
885
886
890
891 if (m_iTrackTube == -1)
892 {
893 char buf[1024];
894 sprintf(buf, "%s:%s(): internal error; duplicated deleteTube() call?", __FILE__, __func__);
895 B2FATAL(buf);
896 }
897
898 m_Tracks.pop_back();
899 m_TrackCount = m_Tracks.size();
900 m_iTrackTube = -1;
901
903}
904
906{
907 MakeMotherKFit kmm;
909 unsigned n = getTrackCount();
910 for (unsigned i = 0; i < n; ++i) {
912 getTrack(i).getCharge());
914 for (unsigned j = i + 1; j < n; ++j) {
916 }
917 }
918 kmm.setVertex(getVertex());
920 m_ErrorCode = kmm.doMake();
922 return m_ErrorCode;
923 double chi2 = getCHIsq();
924 int ndf = getNDF();
925 double prob = TMath::Prob(chi2, ndf);
926 //
927 bool haschi2 = mother->hasExtraInfo("chiSquared");
928 if (haschi2) {
929 mother->setExtraInfo("chiSquared", chi2);
930 mother->setExtraInfo("ndf", ndf);
931 } else {
932 mother->addExtraInfo("chiSquared", chi2);
933 mother->addExtraInfo("ndf", ndf);
934 }
935
936 mother->updateMomentum(
937 CLHEPToROOT::getLorentzVector(kmm.getMotherMomentum()),
938 CLHEPToROOT::getXYZVector(kmm.getMotherPosition()),
939 CLHEPToROOT::getTMatrixFSym(kmm.getMotherError()),
940 prob);
942 return m_ErrorCode;
943}
static const double speedOfLight
[cm/ns]
Definition Const.h:695
Class to store reconstructed particles.
Definition Particle.h:76
void setExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition Particle.cc:1402
bool hasExtraInfo(const std::string &name) const
Return whether the extra info with the given name is set.
Definition Particle.cc:1351
void addExtraInfo(const std::string &name, double value)
Sets the user-defined data of given name to the given value.
Definition Particle.cc:1421
void updateMomentum(const ROOT::Math::PxPyPzEVector &p4, const ROOT::Math::XYZVector &vertex, const TMatrixFSym &errMatrix, double pValue)
Sets Lorentz vector, position, 7x7 error matrix and p-value.
Definition Particle.h:397
int m_NecessaryTrackCount
Number needed tracks to perform fit.
Definition KFitBase.h:303
double m_MagneticField
Magnetic field.
Definition KFitBase.h:311
const CLHEP::HepSymMatrix makeError1(const CLHEP::HepLorentzVector &p, const CLHEP::HepMatrix &e) const
Rebuild an error matrix from a Lorentz vector and an error matrix.
Definition KFitBase.cc:221
CLHEP::HepMatrix m_al_1
See J.Tanaka Ph.D (2001) p136 for definition.
Definition KFitBase.h:259
CLHEP::HepMatrix m_V_Dt
See J.Tanaka Ph.D (2001) p138 for definition.
Definition KFitBase.h:289
const CLHEP::HepSymMatrix getTrackError(const int id) const
Get an error matrix of the track.
Definition KFitBase.cc:168
virtual double getCHIsq(void) const
Get a chi-square of the fit.
Definition KFitBase.cc:121
const CLHEP::HepLorentzVector getTrackMomentum(const int id) const
Get a Lorentz vector of the track.
Definition KFitBase.cc:154
CLHEP::HepMatrix m_lam
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:276
enum KFitError::ECode doFit2(void)
Perform a fit (used in VertexFitKFit::doFit() and MassVertexFitKFit::doFit()).
Definition KFitBase.cc:578
CLHEP::HepMatrix m_E
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:279
const HepPoint3D getTrackPosition(const int id) const
Get a position of the track.
Definition KFitBase.cc:161
bool m_FlagOverIteration
Flag whether the iteration count exceeds the limit.
Definition KFitBase.h:308
CLHEP::HepMatrix m_property
Container of charges and masses.
Definition KFitBase.h:263
virtual double getTrackCHIsq(const int id) const
Get a chi-square of the track.
Definition KFitBase.cc:135
enum KFitError::ECode m_ErrorCode
Error code.
Definition KFitBase.h:243
CLHEP::HepMatrix m_V_al_1
See J.Tanaka Ph.D (2001) p138 for definition.
Definition KFitBase.h:274
virtual int getNDF(void) const
Get an NDF of the fit.
Definition KFitBase.cc:114
CLHEP::HepMatrix m_d
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:268
CLHEP::HepMatrix m_lam0
See J.Tanaka Ph.D (2001) p138 for definition.
Definition KFitBase.h:283
bool isFitted(void) const
Return false if fit is not performed yet or performed fit is failed; otherwise true.
Definition KFitBase.cc:728
CLHEP::HepMatrix m_al_a
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:261
CLHEP::HepMatrix m_D
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:266
CLHEP::HepMatrix m_V_D
See J.Tanaka Ph.D (2001) p138 for definition.
Definition KFitBase.h:271
bool isTrackIDInRange(const int id) const
Check if the id is in the range.
Definition KFitBase.cc:739
CLHEP::HepMatrix m_v_a
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:287
virtual const CLHEP::HepMatrix getCorrelation(const int id1, const int id2, const int flag=KFitConst::kAfterFit) const
Get a correlation matrix between two tracks.
Definition KFitBase.cc:183
bool m_FlagCorrelation
Flag whether a correlation among tracks exists.
Definition KFitBase.h:306
CLHEP::HepSymMatrix m_V_al_0
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:255
CLHEP::HepMatrix m_V_E
See J.Tanaka Ph.D (2001) p138 for definition.
Definition KFitBase.h:281
CLHEP::HepMatrix m_Cov_v_al_1
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:291
const KFitTrack getTrack(const int id) const
Get a specified track object.
Definition KFitBase.cc:175
virtual enum KFitError::ECode prepareCorrelation(void)
Build a grand correlation matrix from input-track properties.
Definition KFitBase.cc:459
const CLHEP::HepMatrix makeError2(const CLHEP::HepLorentzVector &p, const CLHEP::HepMatrix &e) const
Rebuild an error matrix from a Lorentz vector and an error matrix.
Definition KFitBase.cc:296
bool m_FlagFitted
Flag to indicate if the fit is performed and succeeded.
Definition KFitBase.h:245
double m_CHIsq
chi-square of the fit.
Definition KFitBase.h:297
int getTrackCount(void) const
Get the number of added tracks.
Definition KFitBase.cc:107
int m_NDF
NDF of the fit.
Definition KFitBase.h:295
std::vector< KFitTrack > m_Tracks
Container of input tracks.
Definition KFitBase.h:249
CLHEP::HepMatrix m_v
See J.Tanaka Ph.D (2001) p137 for definition.
Definition KFitBase.h:285
int m_TrackCount
Number of tracks.
Definition KFitBase.h:301
CLHEP::HepMatrix m_al_0
See J.Tanaka Ph.D (2001) p136 for definition.
Definition KFitBase.h:257
static void displayError(const char *file, const int line, const char *func, const enum ECode code)
Display a description of error and its location.
Definition KFitError.h:72
ECode
ECode is a error code enumerate.
Definition KFitError.h:34
@ kCannotGetARCSIN
Cannot get arcsin (bad track property or internal error)
Definition KFitError.h:60
@ kCannotGetMatrixInverse
Cannot calculate matrix inverse (bad track property or internal error)
Definition KFitError.h:58
@ kOutOfRange
Specified track-id out of range.
Definition KFitError.h:42
@ kDivisionByZero
Division by zero (bad track property or internal error)
Definition KFitError.h:56
@ kBadInitialCHIsq
Bad initial chi-square (internal error)
Definition KFitError.h:53
@ kBadTrackSize
Track count too small to perform fit.
Definition KFitError.h:47
KFitTrack is a container of the track information (Lorentz vector, position, and error matrix),...
Definition KFitTrack.h:38
MakeMotherKFit is a class to build mother particle from kinematically fitted daughters.
enum KFitError::ECode setVertex(const HepPoint3D &v)
Set a vertex position of the mother particle.
enum KFitError::ECode addTrack(const KFitTrack &kp)
Add a track to the make-mother object.
enum KFitError::ECode doMake(void)
Perform a reconstruction of mother particle.
const CLHEP::HepSymMatrix getMotherError(void) const
Get an error matrix of the mother particle.
enum KFitError::ECode setCorrelation(const CLHEP::HepMatrix &e)
Set a correlation matrix.
const HepPoint3D getMotherPosition(void) const
Get a position of the mother particle.
enum KFitError::ECode setVertexError(const CLHEP::HepSymMatrix &e)
Set a vertex error matrix of the mother particle.
enum KFitError::ECode setTrackVertexError(const CLHEP::HepMatrix &e)
Set a vertex error matrix of the child particle in the addTrack'ed order.
const CLHEP::HepLorentzVector getMotherMomentum(void) const
Get a Lorentz vector of the mother particle.
enum KFitError::ECode setMagneticField(const double mf)
Change a magnetic field from the default value KFitConst::kDefaultMagneticField.
enum KFitError::ECode setIpTubeProfile(const CLHEP::HepLorentzVector &p, const HepPoint3D &x, const CLHEP::HepSymMatrix &e, const double q)
Set a virtual IP-tube track for the vertex constraint fit.
enum KFitError::ECode doFit5(void)
Perform a fixed-vertex-position fit mainly for slow pion.
enum KFitError::ECode setKnownVertex(const bool flag=true)
Tell the object to perform a fit with vertex position fixed.
enum KFitError::ECode prepareInputMatrix(void) override
Build grand matrices for minimum search from input-track properties.
enum KFitError::ECode doFit4(void)
Perform a IP-ellipsoid and vertex-constraint fit.
enum KFitError::ECode calculateNDF(void) override
Calculate an NDF of the fit.
enum KFitError::ECode setInitialVertex(const HepPoint3D &v)
Set an initial vertex point for the vertex-vertex constraint fit.
double getCHIsq(void) const override
Get a chi-square of the fit.
KFitTrack m_TubeTrack
Entity of the virtual IP-tube track.
VertexFitKFit(void)
Construct an object with no argument.
enum KFitError::ECode deleteTube(void)
Delete the virtual tube track to m_Tracks just after the internal minimization call.
bool m_FlagTube
Flag if to perform IP-tube constraint fit.
bool m_FlagKnownVertex
Flag controlled by setKnownVertex().
enum KFitError::ECode updateMother(Particle *mother)
Update mother particle.
const CLHEP::HepSymMatrix getVertexError(void) const
Get a fitted vertex error matrix.
enum KFitError::ECode prepareInputSubMatrix(void) override
Build sub-matrices for minimum search from input-track properties.
CLHEP::HepSymMatrix m_BeamError
Error matrix modeling the IP ellipsoid.
enum KFitError::ECode doFit(void)
Perform a vertex-constraint fit.
double getTrackPartCHIsq(void) const
Get a sum of the chi-square associated to the input tracks.
enum KFitError::ECode doFit3(void)
Perform a standard vertex-constraint fit including IP-tube constraint.
~VertexFitKFit(void)
Destruct the object.
enum KFitError::ECode makeCoreMatrix(void) override
Build matrices using the kinematical constraint.
HepPoint3D m_AfterVertex
Vertex position after the fit.
std::vector< CLHEP::HepMatrix > m_AfterTrackVertexError
Array of vertex error matrices after the fit.
double m_EachCHIsq[KFitConst::kMaxTrackCount2]
Container of chi-square's of the input tracks.
double getTrackCHIsq(const int id) const override
Get a chi-square of the track.
enum KFitError::ECode setCorrelationMode(const bool m)
Tell the object to perform a fit with track correlations.
int getTrackPartNDF(void) const
Get an NDF relevant to the getTrackPartCHIsq().
bool m_FlagBeam
Flag if to perform IP-ellipsoid constraint fit.
int m_iTrackTube
ID of the virtual tube track in the m_Tracks.
enum KFitError::ECode setIpProfile(const HepPoint3D &ip, const CLHEP::HepSymMatrix &ipe)
Set an IP-ellipsoid shape for the vertex constraint fit.
const HepPoint3D getVertex(const int flag=KFitConst::kAfterFit) const
Get a vertex position.
double m_CHIsqVertex
chi-square of the fit excluding IP-constraint part.
double getCHIsqVertex(void) const
Get a chi-square of the fit excluding IP-constraint part.
enum KFitError::ECode appendTube(void)
Add the virtual tube track to m_Tracks just before the internal minimization call.
CLHEP::HepSymMatrix m_AfterVertexError
Vertex error matrix after the fit.
bool m_CorrelationMode
Flag controlled by setCorrelationMode().
enum KFitError::ECode prepareOutputMatrix(void) override
Build an output error matrix.
const CLHEP::HepMatrix getTrackVertexError(const int id) const
Get a vertex error matrix of the track.
HepPoint3D m_BeforeVertex
Vertex position before the fit.
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.
STL namespace.
static constexpr double kInitialCHIsq
Initial chi-square value (internal use)
Definition KFitConst.h:48
static const int kMaxTrackCount
Maximum track size.
Definition KFitConst.h:40
static const int kMaxTrackCount2
Maximum track size (internal use)
Definition KFitConst.h:42
static const int kNumber6
Constant 6 to check matrix size (internal use)
Definition KFitConst.h:30
static const int kMaxIterationCount
Maximum iteration step (internal use)
Definition KFitConst.h:45
static const int kAfterFit
Input parameter to specify after-fit when setting/getting a track attribute.
Definition KFitConst.h:37
static const int kBeforeFit
Input parameter to specify before-fit when setting/getting a track attribute.
Definition KFitConst.h:35
static const int kNumber7
Constant 7 to check matrix size (internal use)
Definition KFitConst.h:32