3#include "VertexFit/VertexFit.h"
4#include "VertexFit/VertexConstraints.h"
5#include "VertexFit/BField.h"
6#include "VertexFit/HTrackParameter.h"
9const int VertexFit::NTRKPAR = 6;
10const int VertexFit::NVTXPAR = 3;
11const int VertexFit::NCONSTR = 2;
17 if (m_pointer)
return m_pointer;
27VertexFit::VertexFit() {;}
43 m_vpar_origin.clear();
48 m_virtual_wtrk.clear();
53 m_TRA = HepMatrix(6, 7, 0);
60 m_TRB = HepMatrix(7, 6, 0);
80 m_vpar_origin.push_back(
vpar);
81 m_vpar_infit.push_back(
vpar);
82 if ((
unsigned int)number != m_vc.size() - 1)
83 std::cout <<
"wrong kinematic constraints index" << std::endl;
95 for (
unsigned int i = 0; i < tlis.size(); i++)
100 m_vpar_origin.push_back(n_vpar);
101 m_vpar_infit.push_back(n_vpar);
103 m_virtual_wtrk.push_back(vwtrk);
104 if ((
unsigned int)number != m_vc.size() - 1)
105 std::cout <<
"wrong kinematic constraints index" << std::endl;
129 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5);
135 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6);
141 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7);
147 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8);
151void VertexFit::AddVertex(
int number,
VertexParameter vpar,
int n1,
int n2,
int n3,
int n4,
int n5,
int n6,
int n7,
int n8,
int n9)
153 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9);
157void VertexFit::AddVertex(
int number,
VertexParameter vpar,
int n1,
int n2,
int n3,
int n4,
int n5,
int n6,
int n7,
int n8,
int n9,
int n10)
159 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10);
163void VertexFit::AddVertex(
int number,
VertexParameter vpar,
int n1,
int n2,
int n3,
int n4,
int n5,
int n6,
int n7,
int n8,
int n9,
int n10,
int n11)
165 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11);
169void VertexFit::AddVertex(
int number,
VertexParameter vpar,
int n1,
int n2,
int n3,
int n4,
int n5,
int n6,
int n7,
int n8,
int n9,
int n10,
int n11,
int n12)
171 std::vector<int> tlis =
AddList(
n1,
n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12);
179 m_cpu = HepVector(10, 0);
180 if (
n < 0 || (
unsigned int)
n >= m_vc.size())
return okfit;
186 m_pOrigin = HepVector(m_nvtrk * NTRKPAR, 0);
187 m_pInfit = HepVector(m_nvtrk * NTRKPAR, 0);
188 m_pcovOrigin = HepSymMatrix(m_nvtrk * NTRKPAR, 0);
189 m_pcovInfit = HepSymMatrix(m_nvtrk * NTRKPAR, 0);
192 for(
unsigned int itk = 0; itk < ntrk; itk++)
198 m_pInfit = m_pOrigin;
200 m_xOrigin = HepVector(NVTXPAR, 0);
201 m_xInfit = HepVector(NVTXPAR, 0);
202 m_xcovOrigin = HepSymMatrix(NVTXPAR, 0);
203 m_xcovInfit = HepSymMatrix(NVTXPAR, 0);
204 m_xcovOriginInversed = HepSymMatrix(NVTXPAR, 0);
205 m_xcovInfitInversed = HepSymMatrix(NVTXPAR, 0);
207 m_xOrigin = m_vpar_origin[
n].Vx();
208 m_xcovOrigin = m_vpar_origin[
n].Evx();
209 m_xcovOriginInversed = m_xcovOrigin.inverse(ifail);
210 m_xInfit = m_xOrigin;
212 m_vpar_infit[
n] = m_vpar_origin[
n];
214 m_B = HepMatrix(NCONSTR*m_nvtrk, NVTXPAR, 0);
215 m_A = HepMatrix(NCONSTR*m_nvtrk, NTRKPAR*m_nvtrk, 0);
216 m_BT = HepMatrix(NVTXPAR,NCONSTR*m_nvtrk, 0);
217 m_AT = HepMatrix(NTRKPAR*m_nvtrk, NCONSTR*m_nvtrk, 0);
218 m_G = HepVector(NCONSTR*m_nvtrk, 0);
219 m_W = HepSymMatrix(NCONSTR*m_nvtrk, 0);
220 m_E = HepMatrix(NTRKPAR*m_nvtrk, NVTXPAR, 0);
223 m_cpu[0] += timer.CpuTime();
226 std::vector<double>
chisq;
228 for (
int it = 0; it < m_niter; it++)
231 UpdateConstraints(m_vc[
n]);
233 m_cpu[1] += timer.CpuTime();
237 m_cpu[2] += timer.CpuTime();
238 chisq.push_back(m_chisq[
n]);
242 if (fabs(delchi) < m_chiter)
break;
249 if (m_chisq[
n] >= m_chicut)
return okfit;
252 m_vpar_infit[
n].setVx(m_xInfit);
253 m_vpar_infit[
n].setEvx(m_xcovInfit);
262 if (
n < 0 || (
unsigned int)
n >= m_vc.size())
264 for (
unsigned int i = 0; i < (m_vc[
n].Ltrk()).size(); i++)
266 int itk = (m_vc[
n].Ltrk())[i];
269 m_vpar_infit[
n] = m_vpar_origin[
n];
272 std::vector<double>
chisq;
274 for (
int it = 0; it < m_niter; it++)
276 std::vector<WTrackParameter> wlis;
278 for (
unsigned int i = 0; i < (m_vc[
n].Ltrk()).size(); i++)
280 int itk = (m_vc[
n].Ltrk())[i];
284 m_vc[
n].UpdateConstraints(
vpar, wlis);
287 chisq.push_back(m_chisq[
n]);
291 if(fabs(delchi) < m_chiter)
295 if(m_chisq[
n]>=m_chicut)
return okfit;
305 for (
unsigned int n = 0;
n<(int)(m_vc.size());
n++)
308 if (m_chisq[
n] >= m_chicut)
return okfit;
310 mychi = mychi + m_chisq[
n];
317void VertexFit::fitVertex(
int n)
319 if (m_chisq.size() == 0)
321 for (
unsigned int i = 0; i < m_vc.size(); i++)
322 m_chisq.push_back(9999.0);
328 int NSIZE = (vc.
Ltrk()).size();
332 m_xcovInfitInversed = m_xcovOriginInversed;
334 for (
unsigned int i = 0; i < NSIZE; i++)
336 int itk = (vc.
Ltrk())[i];
337 m_xcovInfitInversed += vfW(itk).similarity(vfBT(itk));
339 m_xcovInfit = m_xcovInfitInversed.inverse(ifail);
342 m_KQ = HepMatrix(NVTXPAR, m_nvtrk * NCONSTR, 0);
343 m_E = HepMatrix(m_nvtrk*NTRKPAR, NVTXPAR, 0);
344 for (
unsigned int i = 0; i < NSIZE; i++)
346 int itk = (vc.
Ltrk())[i];
347 setKQ(itk, (m_xcovInfit * vfBT(itk) * vfW(itk)));
348 setE(itk, (-pcovOrigin(itk) * vfAT(itk) * vfKQ(itk).T()));
351 m_xInfit = m_xOrigin;
352 for (
unsigned int i = 0; i < NSIZE; i++)
354 int itk = (vc.
Ltrk())[i];
355 m_xInfit -= vfKQ(itk) * vfG(itk);
358 HepVector dq0q(NVTXPAR, 0);
359 dq0q = m_xcovInfitInversed * (m_xOrigin - m_xInfit);
360 for (
unsigned int i = 0; i < NSIZE; i++)
362 int itk = (vc.
Ltrk())[i];
363 HepVector
alpha(NTRKPAR, 0);
364 alpha = pOrigin(itk) - pcovOrigin(itk) * vfAT(itk) * (vfW(itk)*vfG(itk) - vfKQ(itk).T()*dq0q);
365 setPInfit(itk,
alpha);
368 m_chisq[
n] = (m_W.similarity(m_G.T())- m_xcovInfitInversed.similarity((m_xInfit-m_xOrigin).T()))[0][0];
371void VertexFit::vertexCovMatrix(
int n)
378 unsigned int NSIZE = vc.
Ltrk().size();
380 m_pcovInfit = HepSymMatrix(NTRKPAR*m_nvtrk, 0);
381 for (
unsigned int i = 0; i < NSIZE; i++)
383 int itk = vc.
Ltrk()[i];
384 setPCovInfit(itk, pcovOrigin(itk) - vfW(itk).similarity(pcovOrigin(itk) * vfAT(itk)));
386 m_pcovInfit = m_pcovInfit + m_xcovInfitInversed.similarity(m_E);
389 m_cpu[3] += timer.CpuTime();
392void VertexFit::swimVertex(
int n)
409 unsigned int NSIZE = vc.
Ltrk().size();
411 HepMatrix
A(6, 6, 0);
415 HepMatrix
B(6, 3, 0);
421 HepSymMatrix
Ew(7, 0);
422 HepMatrix ew1(6, 6, 0);
423 HepMatrix ew2(7, 7, 0);
425 for (
unsigned int i = 0; i < NSIZE; i++)
427 int itk = vc.
Ltrk()[i];
436 w1 =
A * pInfit(itk) +
B * m_xInfit;
437 ew1 = pcovInfit(itk).similarity(A) + m_xcovInfit.similarity(B) +
A*vfE(itk)*
B.T() +
B*(vfE(itk).T())*
A.T();
443 m_TRB[3][0] = w2[0] / w2[3];
444 m_TRB[3][1] = w2[1] / w2[3];
445 m_TRB[3][2] = w2[2] / w2[3];
447 ew2 = m_TRB * ew1 * m_TRB.T();
454 m_cpu[4] += timer.CpuTime();
459 assert(p.num_row() == 5);
465 HepSymMatrix ew1(6, 0);
466 HepSymMatrix ew2(7, 0);
469 ew1 = pcovInfit(itk);
470 w2 = Convert67(wtrk0.
mass(), w1);
471 m_TRB[3][0] = w2[0] / w2[3];
472 m_TRB[3][1] = w2[1] / w2[3];
473 m_TRB[3][2] = w2[2] / w2[3];
474 ew2 = ew1.similarity(m_TRB);
481 for (
int i = 0; i < 5; i++)
483 double del = htrk0.
eHel()[i][i] - htrk1.
eHel()[i][i];
488 p[i] = (htrk0.
helix()[i] - htrk1.
helix()[i]) / sqrt(
abs(del));
493void VertexFit::fitBeam(
int n)
555void VertexFit::swimBeam(
int n)
626 HepMatrix A(NTRKPAR, NTRKPAR * m_nvtrk, 0);
627 HepMatrix B(NTRKPAR, NVTXPAR, 0);
629 unsigned int NSIZE = vc.
Ltrk().size();
632 HepMatrix Ai(6, 6, 0);
636 HepMatrix Bi(6, 3, 0);
642 HepSymMatrix
Ew(7, 0);
643 HepMatrix ew1(6, 6, 0);
644 HepMatrix ew2(7, 7, 0);
647 for(
unsigned int i = 0; i < NSIZE; i++)
649 int itk = vc.
Ltrk()[i];
660 A.sub(1, NTRKPAR*itk + 1, Ai);
667 w1 = A * m_pInfit + B * m_xInfit;
668 ew1 = m_pcovInfit.similarity(A) + m_xcovInfit.similarity(B) + A*m_E*B.T()+B*(m_E.T())*A.T();
679 m_TRB[3][0] = w1[0] / totalE;
680 m_TRB[3][1] = w1[1] / totalE;
681 m_TRB[3][2] = w1[2] / totalE;
682 ew2 = m_TRB * ew1 * m_TRB.T();
689 m_virtual_wtrk[
n] = vwtrk;
691 m_cpu[5] += timer.CpuTime();
696 int ntrk = (vc.
Ltrk()).size();
697 int type = vc.
type();
702 for (
unsigned int i = 0; i < ntrk; i++)
704 int itk = (vc.
Ltrk())[i];
705 HepVector
alpha(6, 0);
723 double J = a * (delx.x()*p.px() + delx.y()*p.py()) / p.perp2();
724 J = std::min(J, 1-1e-4);
725 J = std::max(J, -1+1e-4);
726 double Rx = delx.x() - 2 * p.px() * (delx.x()*p.px() + delx.y()*p.py()) / p.perp2();
727 double Ry = delx.y() - 2 * p.py() * (delx.x()*p.px() + delx.y()*p.py()) / p.perp2();
728 double S = 1.0 / sqrt(1-J*J) / p.perp2();
731 dc[0] = delx.y()*p.px() - delx.x()*p.py() - 0.5*a*(delx.x()*delx.x() + delx.y()*delx.y());
732 dc[1] = delx.z() - p.pz()/a*asin(J);
735 HepMatrix Ec(2, 3, 0);
738 HepMatrix Dc(2, 6, 0);
740 Dc[0][1] = -delx.x();
742 Dc[0][3] = p.py() + a * delx.x();
743 Dc[0][4] = -p.px() + a * delx.y();
745 Dc[1][0] = -p.pz() * S * Rx;
746 Dc[1][1] = -p.pz() * S * Ry;
747 Dc[1][2] = - asin(J) / a;
748 Dc[1][3] = p.px() * p.pz() * S;
749 Dc[1][4] = p.py() * p.pz() * S;
754 HepSymMatrix vd(2, 0);
756 vd = pcovOrigin(itk).similarity(Dc);
765 for (
unsigned int i = 0; i < ntrk; i++)
767 int itk = (vc.
Ltrk())[i];
768 HepVector
alpha(6, 0);
788 dc[0] = p.pz()*delx.x() - p.px()*delx.z();
789 dc[1] = p.pz()*delx.y() - p.py()*delx.z();
791 HepMatrix Ec(2, 3, 0);
801 HepMatrix Dc(2, 6, 0);
802 Dc[0][0] = -delx.z();
809 Dc[1][1] = -delx.z();
818 HepSymMatrix vd(2, 0);
821 vd = pcovOrigin(itk).similarity(Dc);
827 gc = Dc*(pOrigin(itk)-pInfit(itk)) + Ec*(m_xOrigin-m_xInfit) +
dc;
836 double J = a * (delx.x()*p.px() + delx.y()*p.py())/p.perp2();
837 J = std::min(J, 1-1e-4);
838 J = std::max(J, -1+1e-4);
839 double Rx = delx.x() - 2*p.px()*(delx.x()*p.px() + delx.y()*p.py())/p.perp2();
840 double Ry = delx.y() - 2*p.py()*(delx.x()*p.px() + delx.y()*p.py())/p.perp2();
841 double S = 1.0 / sqrt(1-J*J) / p.perp2();
845 dc[0] = delx.y() * p.px() - delx.x() * p.py() - 0.5 * a * (delx.x() * delx.x() + delx.y() * delx.y());
846 dc[1] = delx.z() - p.pz() / a*asin(J);
848 HepMatrix Ec(2, 3, 0);
849 Ec[0][0] = -p.py()- a * delx.x();
850 Ec[0][1] = p.px() - a * delx.y();
852 Ec[1][0] = -p.px() * p.pz() * S ;
853 Ec[1][1] = -p.py() * p.pz() * S ;
861 Dc[0][1] = -delx.x();
863 Dc[0][3] = p.py() + a*delx.x();
864 Dc[0][4] = -p.px() + a*delx.y();
867 Dc[1][0] = -p.pz()*S*Rx;
868 Dc[1][1] = -p.pz()*S*Ry;
869 Dc[1][2] = -asin(J)/a;
870 Dc[1][3] = p.px()*p.pz()*S;
871 Dc[1][4] = p.py()*p.pz()*S;
876 HepSymMatrix vd(2, 0);
878 vd = pcovOrigin(itk).similarity(Dc);
883 gc = Dc * (pOrigin(itk) - pInfit(itk)) + Ec *(m_xOrigin - m_xInfit) +
dc;
892HepVector VertexFit::Convert67(
const double &
mass,
const HepVector &p)
896 m.sub(1, p.sub(1, 3));
897 m.sub(5, p.sub(4, 6));
898 m[3] = sqrt(
mass*
mass + p[0]*p[0] + p[1]*p[1] + p[2]*p[2]);
902HepVector VertexFit::Convert76(
const HepVector &p)
906 m.sub(1, p.sub(1, 3));
907 m.sub(4, p.sub(5, 7));
double abs(const EvtComplex &c)
HepGeom::Point3D< double > HepPoint3D
HepSymMatrix eHel() const
std::vector< int > AddList(int n1)
std::vector< WTrackParameter > wTrackInfit() const
void clearGammaShapeList()
std::vector< WTrackParameter > wTrackOrigin() const
void setWTrackInfit(const int n, const WTrackParameter wtrk)
std::vector< int > Ltrk() const
void FixedVertexConstraints(std::vector< int > tlis)
void CommonVertexConstraints(std::vector< int > tlis)
double getCBz(const HepVector &vtx, const HepVector &trackPosition)
static VertexFitBField * instance()
WTrackParameter wtrk(int n) const
HepSymMatrix Ew(int n) const
void AddBeamFit(int number, VertexParameter vpar, int n)
void AddVertex(int number, VertexParameter vpar, std::vector< int > lis)
bool pull(int n, int itk, HepVector &p)
HepPoint3D vx(int n) const
static VertexFit * instance()
VertexParameter vpar(int n) const
void BuildVirtualParticle(int number)
void setVx(const HepPoint3D &vx)
void setEw(const HepSymMatrix &Ew)
void setCharge(const int charge)
void setW(const HepVector &w)