1#include "VertexFit/KinematicFit.h"
2#include "VertexFit/KinematicConstraints.h"
3#include "VertexFit/HTrackParameter.h"
7const int KinematicFit::NTRKPAR = 3;
9const int KinematicFit::Resonance = 1;
10const int KinematicFit::TotalEnergy = 2;
11const int KinematicFit::TotalMomentum = 4;
12const int KinematicFit::Position = 8;
13const int KinematicFit::ThreeMomentum = 16;
14const int KinematicFit::FourMomentum = 32;
15const int KinematicFit::EqualMass = 64;
21 if(m_pointer)
return m_pointer;
26KinematicFit::KinematicFit(){;}
58 m_collideangle = 11e-3;
62 m_cpu = HepVector(10, 0);
63 m_massvector = HepVector(12,0);
64 m_virtual_wtrk.clear();
94 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5);
100 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6);
105 int n5,
int n6,
int n7) {
106 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7);
111 int n5,
int n6,
int n7,
int n8) {
112 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8);
117 int n5,
int n6,
int n7,
int n8,
int n9) {
118 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9);
123 int n5,
int n6,
int n7,
int n8,
int n9,
int n10) {
124 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10);
130 int n5,
int n6,
int n7,
int n8,
int n9,
132 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11);
137 int n5,
int n6,
int n7,
int n8,
int n9,
138 int n10,
int n11,
int n12) {
139 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12);
145 HepSymMatrix Vre = HepSymMatrix(1,0);
148 if((
unsigned int) number != m_kc.size()-1)
149 std::cout <<
"wrong kinematic constraints index" << std::endl;
178 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5);
184 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6);
189 int n5,
int n6,
int n7) {
190 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7);
195 int n5,
int n6,
int n7,
int n8) {
196 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8);
201 int n5,
int n6,
int n7,
int n8,
int n9) {
202 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9);
207 int n5,
int n6,
int n7,
int n8,
int n9,
209 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10);
215 int n5,
int n6,
int n7,
int n8,
int n9,
217 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11);
222 int n5,
int n6,
int n7,
int n8,
int n9,
223 int n10,
int n11,
int n12) {
224 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12);
232 if((
unsigned int) number != m_kc.size()-1)
233 std::cout <<
"wrong kinematic constraints index" << std::endl;
261 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5);
267 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6);
272 int n5,
int n6,
int n7) {
273 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7);
278 int n5,
int n6,
int n7,
int n8) {
279 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8);
284 int n5,
int n6,
int n7,
int n8,
int n9) {
285 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9);
290 int n5,
int n6,
int n7,
int n8,
int n9,
292 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10);
298 int n5,
int n6,
int n7,
int n8,
int n9,
300 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11);
305 int n5,
int n6,
int n7,
int n8,
int n9,
306 int n10,
int n11,
int n12) {
307 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12);
315 if((
unsigned int) number != m_kc.size()-1)
316 std::cout <<
"wrong kinematic constraints index" << std::endl;
324 HepSymMatrix Vne = HepSymMatrix(1,0);
327 if((
unsigned int) number != m_kc.size()-1)
328 std::cout <<
"wrong kinematic constraints index" << std::endl;
336 std::vector<int> tlis;
346 if((
unsigned int) number != m_kc.size()-1)
347 std::cout <<
"wrong kinematic constraints index" << std::endl;
356 std::vector<int> tlis;
367 HepSymMatrix Vme = HepSymMatrix(4,0);
368 Vme[0][0] = 2*m_espread*m_espread*
sin(m_collideangle)*
sin(m_collideangle);
369 Vme[0][3] = 2*m_espread*m_espread*
sin(m_collideangle);
370 Vme[3][3] = 2*m_espread*m_espread;
375 if((
unsigned int) number != m_kc.size()-1)
376 std::cout <<
"wrong kinematic constraints index" << std::endl;
381 HepLorentzVector p4(0.0, 0.0, 0.0,
etot);
382 std::vector<int> tlis;
389 HepSymMatrix Vme = HepSymMatrix (4,0);
390 Vme[3][3] = 2*m_espread*m_espread;
394 if((
unsigned int) number != m_kc.size()-1)
395 std::cout <<
"wrong kinematic constraints index" << std::endl;
422void KinematicFit::fit() {
431 m_VD = HepSymMatrix(m_nc, 0);
432 m_VD = m_covOrigin.similarity(m_D);
434 m_cpu[1] += timer.CpuTime();
441 m_cpu[2] += timer.CpuTime();
444 HepVector G(m_nc, 0);
445 G = m_D * (m_pOrigin - m_pInfit) + m_d;
447 m_KP = HepMatrix(NTRKPAR*m_nktrk, m_nc, 0);
448 m_KP = m_covOrigin * m_DT * m_VD;
449 m_chi = (m_VD.similarity(G.T()))[0][0];
453 m_cpu[3] += timer.CpuTime();
456 m_pInfit = m_pOrigin - m_KP * G;
461 m_cpu[4] += timer.CpuTime();
464 if(m_dynamicerror ==1)
468 m_cpu[6] += timer.CpuTime();
475void KinematicFit::upCovmtx() {
478 HepSymMatrix
I(NTRKPAR*m_nktrk, 1);
479 m_covInfit = m_covOrigin.similarity(
I - m_KP*m_D);
480 for(
int i = 0; i < m_nktrk; i++) {
481 HepSymMatrix ew3 (3, 0);
482 HepSymMatrix ew6 (6, 0);
483 HepSymMatrix Ew1 (7, 0);
484 ew3 = m_covInfit.sub(i*NTRKPAR + 1, (i+1)*NTRKPAR);
485 for(
int j = 0; j < NTRKPAR; j++) {
486 for(
int k = 0; k <
NTRKPAR; k++) {
487 ew6[j][k] = ew3[j][k];
490 for(
int m = NTRKPAR; m < 6; m++) {
491 for(
int n = NTRKPAR;
n < 6;
n++) {
495 double px = p4Infit(i).px();
496 double py = p4Infit(i).py();
497 double pz = p4Infit(i).pz();
498 double m = p4Infit(i).m();
499 double e = p4Infit(i).e();
501 HepMatrix J(7, 6, 0);
511 Ew1 = ew6.similarity(J);
514 for(
int j=0; j<4; j++) {
515 W[j] = p4Infit(i)[j];
533void KinematicFit::fit(
int n) {
535 if(m_chisq.size() == 0) {
536 for(
unsigned int i = 0; i < m_kc.size(); i++)
537 m_chisq.push_back(9999.0);
543 m_VD = HepSymMatrix(m_nc, 0);
544 m_VD = m_covOrigin.similarity(m_D);
547 HepVector G(m_nc, 0);
548 G = m_D * (m_pOrigin - m_pInfit) + m_d;
549 m_KP = HepMatrix(NTRKPAR*m_nktrk, m_nc, 0);
550 m_KP = m_covOrigin * m_DT * m_VD;
551 m_chisq[
n] = (m_VD.similarity(G.T()))[0][0];
552 m_pInfit = m_pOrigin - m_KP * G;
558void KinematicFit::covMatrix(
int n) {
561 int ntrk = (kc.
Ltrk()).size();
562 HepSymMatrix Va0(7*ntrk, 0);
563 for (
int i = 0; i < ntrk; i++) {
564 int itk = (kc.
Ltrk())[i];
565 for(
int j = 0; j < 7; j++)
566 for (
int k = 0; k < 7; k++)
570 HepMatrix D(nc, 7*ntrk, 0);
572 for(
int j = 0; j < kc.
nc(); j++) {
573 for(
int l = 0; l < ntrk; l++) {
574 for(
int k = 0; k < 7; k++)
575 D[ncc][7*l+k] = (kc.
Dc()[l])[j][k];
581 HepSymMatrix VD(nc, 0);
583 HepSymMatrix Va(7*ntrk, 0);
584 Va = Va0 - (VD.similarity(D.T())).similarity(Va0);
585 for(
int i = 0; i < ntrk; i++) {
586 int itk = (kc.
Ltrk())[i];
588 HepSymMatrix Ew(7, 0);
589 for(
int j = 0; j < 7; j++) {
590 for(
int k = 0; k < 7; k++)
591 Ew[j][k] = Va[7*i + j] [7*i + k];
608 m_pOrigin = HepVector(m_nktrk * NTRKPAR, 0);
609 m_pInfit = HepVector(m_nktrk * NTRKPAR, 0);
610 m_covOrigin = HepSymMatrix(m_nktrk * NTRKPAR, 0);
611 m_covInfit = HepSymMatrix(m_nktrk * NTRKPAR, 0);
612 m_massvector = HepVector(m_nktrk, 0);
617 setCovOrigin(i, (
wTrackOrigin(i).Ew()).sub(1, NTRKPAR));
628 std::vector<double>
chisq;
631 for(
int i = 0; i < m_kc.size(); i++)
634 m_D = HepMatrix(nc, m_nktrk * NTRKPAR, 0);
635 m_DT = HepMatrix(m_nktrk * NTRKPAR, nc, 0);
636 m_d = HepVector(nc, 0);
638 for(
int it = 0; it < m_niter; it++) {
642 for(
unsigned int i = 0; i < m_kc.size(); i++) {
662 updateConstraints(kc);
666 m_cpu[0] += timer.CpuTime();
669 chisq.push_back(m_chi);
672 if(fabs(delchi) < m_chiter)
676 if(m_chi >= m_chicut) {
686 m_cpu[5] += timer.CpuTime();
743 if(
n < 0 || (
unsigned int)
n >= m_kc.size())
return okfit;
746 m_pOrigin = HepVector(m_nktrk * NTRKPAR, 0);
747 m_pInfit = HepVector(m_nktrk * NTRKPAR, 0);
748 m_covOrigin = HepSymMatrix(m_nktrk * NTRKPAR, 0);
749 m_covInfit = HepSymMatrix(m_nktrk * NTRKPAR, 0);
750 m_massvector = HepVector(m_nktrk * NTRKPAR, 0);
755 setCovOrigin(i, (
wTrackOrigin(i).Ew()).sub(1, NTRKPAR));
763 std::vector<double>
chisq;
766 m_D = HepMatrix(m_kc[
n].nc(), m_nktrk * NTRKPAR, 0);
767 m_DT = HepMatrix(m_nktrk * NTRKPAR, m_kc[
n].nc(), 0);
768 m_d = HepVector(m_kc[
n].nc(), 0);
770 for(
int it = 0; it < m_niter; it++) {
773 updateConstraints(kc);
777 chisq.push_back(m_chisq[
n]);
780 if(fabs(delchi) < m_chiter)
785 if(m_chisq[
n] >= m_chicut)
return okfit;
799 int ntrk = (kc.
Ltrk()).size();
802 HepSymMatrix ew(7, 0);
803 HepMatrix dwdp(7, 7, 0);
811 for (
int i = 0; i < ntrk; i++) {
812 int itk = (kc.
Ltrk())[i];
821 ew = ew + (
wTrackInfit(itk).Ew()).similarity(dwdp);
823 double m = sqrt(w[3]*w[3] - w[0]*w[0] - w[1]*w[1] - w[2]*w[2]);
829 m_virtual_wtrk.push_back(vwtrk);
834void KinematicFit::gda(){
842 HepSymMatrix Ew(NTRKPAR, 0);
843 HepLorentzVector p1 = p4Infit(i);
844 double eorigin = pOrigin(i)[3];
846 HepMatrix dwda1(NTRKPAR, 3, 0);
847 dwda1[0][0] = -p1.py();
848 dwda1[1][0] = p1.px();
849 dwda1[0][1] = p1.px()*p1.pz()/p1.perp();
850 dwda1[1][1] = p1.py()*p1.pz()/p1.perp();
851 dwda1[2][1] = -p1.perp();
852 dwda1[0][2] = p1.px()/p1.rho();
853 dwda1[1][2] = p1.py()/p1.rho();
854 dwda1[2][2] = p1.pz()/p1.rho();
855 dwda1[3][2] = p1.rho()/p1.e();
856 HepSymMatrix emcmea1(3, 0);
861 Ew = emcmea1.similarity(dwda1);
875 HepSymMatrix Ew(6,0);
877 HepSymMatrix Ew1(7,0);
884 for(
int i=0; i<3; i++) {
890 for(
int j=0; j<3; j++) {
891 for(
int k=0; k<3; k++) {
892 Ew[j][k] = covInfit(
n)[j][k];
896 for(
int j=3; j<6; j++) {
897 for(
int k=3; k<6; k++) {
904 double px = p4Infit(
n).px();
905 double py = p4Infit(
n).py();
906 double pz = p4Infit(
n).pz();
907 double e = p4Infit(
n).e();
908 HepMatrix J(7, 6, 0);
919 Ew1 = Ew.similarity(J) ;
936 HepVector a0 = horigin.
hel();
937 HepVector a1 = hinfit.
hel();
938 HepSymMatrix v0 = horigin.
eHel();
939 HepSymMatrix v1 = hinfit.
eHel();
940 HepVector
pull(11,0);
941 for (
int k=0; k<5; k++) {
942 pull[k] = (a0[k]-a1[k])/sqrt(
abs(v0[k][k]-v1[k][k]));
945 for (
int l=5; l<9; l++) {
955 for (
int m=0; m<3; m++) {
984 int type = kc.
Type();
990 double mres = kc.
mres();
991 HepLorentzVector pmis;
992 for(
unsigned int j = 0; j< (kc.
Ltrk()).size(); j++){
993 int n = (kc.
Ltrk())[j];
995 pmis = pmis + p4Infit(
n);
999 for(
unsigned int j = 0; j< (kc.
Ltrk()).size(); j++){
1000 int n = (kc.
Ltrk())[j];
1001 HepMatrix Dc(1, NTRKPAR, 0);
1002 Dc[0][0] = -2 * pmis.px() + 2 * pmis.e() * p4Infit(
n).px() / p4Infit(
n).e();
1003 Dc[0][1] = -2 * pmis.py() + 2 * pmis.e() * p4Infit(
n).py() / p4Infit(
n).e();
1004 Dc[0][2] = -2 * pmis.pz() + 2 * pmis.e() * p4Infit(
n).pz() / p4Infit(
n).e();
1007 setDT(
n, m_nc, Dc.T());
1021 dc[0] = pmis.m2() - mres * mres;
1040 HepLorentzVector pmis;
1041 for(
unsigned int j = 0; j < (kc.
Ltrk()).size(); j++){
1042 int n = (kc.
Ltrk())[j];
1043 pmis = pmis + p4Infit(
n);
1047 for(
unsigned int j = 0; j < (kc.
Ltrk()).size(); j++) {
1048 int n = (kc.
Ltrk())[j];
1049 HepMatrix Dc(1, NTRKPAR, 0);
1050 Dc[0][0] = p4Infit(
n).px()/p4Infit(
n).e();
1051 Dc[0][1] = p4Infit(
n).py()/p4Infit(
n).e();
1052 Dc[0][2] = p4Infit(
n).pz()/p4Infit(
n).e();
1055 setDT(
n, m_nc, Dc.T());
1068 case TotalMomentum: {
1072 double ptot = kc.
ptot();
1073 HepLorentzVector pmis;
1074 for(
unsigned int j = 0; j< (kc.
Ltrk()).size(); j++){
1075 int n = (kc.
Ltrk())[j];
1076 pmis = pmis + p4Infit(
n);
1081 for(
unsigned int j = 0; j < (kc.
Ltrk()).size(); j++) {
1082 int n = (kc.
Ltrk())[j];
1083 HepMatrix Dc(1, NTRKPAR, 0);
1084 Dc[0][0] = pmis.px()/pmis.rho();
1085 Dc[0][1] = pmis.py()/pmis.rho();
1086 Dc[0][2] = pmis.pz()/pmis.rho();
1088 setDT(
n, m_nc, Dc.T());
1092 dc[0] = pmis.rho() - ptot;
1170 case ThreeMomentum: {
1176 Hep3Vector p3 = kc.
p3();
1177 HepLorentzVector pmis;
1178 for(
unsigned int j = 0; j< (kc.
Ltrk()).size(); j++){
1179 int n = (kc.
Ltrk())[j];
1180 pmis = pmis + p4Infit(
n);
1184 for(
unsigned int j = 0; j < (kc.
Ltrk()).size(); j++) {
1185 int n = (kc.
Ltrk())[j];
1187 HepMatrix Dc(kc.
nc(), NTRKPAR, 0);
1191 HepVector
dc(kc.
nc(), 0);
1192 dc[0] = pmis.px() - p3.x();
1193 dc[1] = pmis.py() - p3.y();
1194 dc[2] = pmis.pz() - p3.z();
1195 for(
int i = 0; i < kc.
nc(); i++) {
1196 setD(m_nc+i,
n, Dc.sub(i+1, i+1, 1, NTRKPAR));
1197 setDT(
n, m_nc+i, (Dc.sub(i+1, i+1, 1, NTRKPAR)).T());
1198 m_d[m_nc+i] =
dc[i];
1221 HepLorentzVector pmis1, pmis2;
1222 for(
int n = 0;
n < isiz;
n++) {
1224 pmis1 = pmis1 + p4Infit(
n1);
1227 for(
int n = 0;
n < jsiz;
n++){
1229 pmis2 = pmis2 + p4Infit(
n2);
1231 for(
int i = 0; i < isiz; i++) {
1233 HepMatrix Dc(1, NTRKPAR, 0);
1234 Dc[0][0] = -2 * pmis1.px() + 2 * pmis1.e() * p4Infit(
n1).px() / p4Infit(
n1).e();
1235 Dc[0][1] = -2 * pmis1.py() + 2 * pmis1.e() * p4Infit(
n1).py() / p4Infit(
n1).e();
1236 Dc[0][2] = -2 * pmis1.pz() + 2 * pmis1.e() * p4Infit(
n1).pz() / p4Infit(
n1).e();
1239 setDT(
n1,m_nc,Dc.T());
1241 for(
int i = 0; i < jsiz; i++) {
1242 int n2 = (kc.
Ltrk())[i+isiz];
1243 HepMatrix Dc(1, NTRKPAR, 0);
1244 Dc[0][0] = 2 * pmis2.px() - 2 * pmis2.e() * p4Infit(
n2).px() / p4Infit(
n2).e();
1245 Dc[0][1] = 2 * pmis2.py() - 2 * pmis2.e() * p4Infit(
n2).py() / p4Infit(
n2).e();
1246 Dc[0][2] = 2 * pmis2.pz() - 2 * pmis2.e() * p4Infit(
n2).pz() / p4Infit(
n2).e();
1247 Dc[0][3] = -2 * pmis2.e();
1249 setDT(
n2,m_nc,Dc.T());
1281 dc[0] = pmis1.m2() - pmis2.m2();
1303 HepLorentzVector p4 = kc.
p4();
1304 HepLorentzVector pmis;
1305 for(
unsigned int j = 0; j< (kc.
Ltrk()).size(); j++){
1306 int n = (kc.
Ltrk())[j];
1307 pmis = pmis + p4Infit(
n);
1310 for(
unsigned int j = 0; j < (kc.
Ltrk()).size(); j++) {
1311 int n = (kc.
Ltrk())[j];
1312 HepMatrix Dc(kc.
nc(), NTRKPAR, 0);
1316 Dc[3][0] = p4Infit(
n).px() / p4Infit(
n).e();
1317 Dc[3][1] = p4Infit(
n).py() / p4Infit(
n).e();
1318 Dc[3][2] = p4Infit(
n).pz() / p4Infit(
n).e();
1322 HepVector
dc(kc.
nc(), 0);
1323 dc[0] = pmis.px() - p4.px();
1324 dc[1] = pmis.py() - p4.py();
1325 dc[2] = pmis.pz() - p4.pz();
1326 dc[3] = pmis.e() - p4.e();
1327 for(
int i = 0; i < kc.
nc(); i++) {
1328 setD(m_nc+i,
n, Dc.sub(i+1, i+1, 1, NTRKPAR));
1329 setDT(
n, m_nc+i, (Dc.sub(i+1, i+1, 1, NTRKPAR)).T());
1330 m_d[m_nc+i] =
dc[i];
double abs(const EvtComplex &c)
HepGeom::Point3D< double > HepPoint3D
double sin(const BesAngle a)
HepSymMatrix eHel() const
std::vector< HepSymMatrix > VD()
void FourMomentumConstraints(const HepLorentzVector p4, std::vector< int > tlis, HepSymMatrix Vme)
std::vector< int > Ltrk()
void ResonanceConstraints(const double mres, std::vector< int > tlis, HepSymMatrix Vre)
void ThreeMomentumConstraints(const Hep3Vector p3, std::vector< int > tlis)
void TotalEnergyConstraints(const double etot, std::vector< int > tlis)
std::vector< int > numEqual()
HepLorentzVector p4() const
std::vector< HepMatrix > Dc()
void TotalMomentumConstraints(const double ptot, std::vector< int > tlis)
void EqualMassConstraints(std::vector< int > tlis1, std::vector< int > tlis2, HepSymMatrix Vne)
static KinematicFit * instance()
void BuildVirtualParticle(int number)
void AddResonance(int number, double mres, std::vector< int > tlis)
void AddEqualMass(int number, std::vector< int > tlis1, std::vector< int > tlis2)
void AddThreeMomentum(int number, Hep3Vector p3)
void AddFourMomentum(int number, HepLorentzVector p4)
void AddTotalEnergy(int number, double etot, std::vector< int > lis)
void AddTotalMomentum(int number, double ptot, std::vector< int > lis)
std::vector< int > AddList(int n1)
std::vector< WTrackParameter > wTrackInfit() const
void setVBeamPosition(const HepSymMatrix VBeamPosition)
void clearGammaShapeList()
int numberGammaShape() const
std::vector< GammaShape > GammaShapeValue() const
std::vector< WTrackParameter > wTrackOrigin() const
std::vector< int > GammaShapeList() const
void setBeamPosition(const HepPoint3D BeamPosition)
void setWTrackInfit(const int n, const WTrackParameter wtrk)
void setEw(const HepSymMatrix &Ew)
void setMass(const double mass)
void setCharge(const int charge)
void setW(const HepVector &w)