46 totP.Px=0.;totP.Py=0.;totP.Pz=0.;totP.E=0.;
55 if( target_trk ==
nullptr ) {
56 std::cout<<
" ERROR in CASCADE STRUCTURE"<<
'\n';
59 double Pt=sqrt(totP.Px*totP.Px + totP.Py*totP.Py);
61 if(
Pt > std::abs(totP.Pz)){
62 Mass = sqrt( (totP.E-
Pt)*(totP.E+
Pt) - totP.Pz*totP.Pz);
64 Mass = sqrt( (totP.E-totP.Pz)*(totP.E+totP.Pz) -
Pt*
Pt);
78 double cnstRemnants=0.;
79 for(
int iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
114 bool existPointingCnst=
false;
123 if(IERR)
return IERR;
147 if( target_trk ==
nullptr )
return -12;
150 if(Charge!=0) Charge=std::copysign(1,Charge);
151 target_trk->
Charge=Charge;
153 double dptot[4],VrtMomCov[21];
154 double parV0[5],covParV0[15],tmpCov[15],fittedVrt[3];
167 if (IERR)
return -13;
179 if(existPointingCnst){
182 VrtMomCov[0] *= 1000.;
183 VrtMomCov[2] *= 1000.;
184 VrtMomCov[5] *= 1000.;
185 VrtMomCov[9] *= 1000.;
186 VrtMomCov[14] *= 1000.;
187 VrtMomCov[20] *= 1000.;
193 combinedTrack( Charge, dptot, VrtMomCov, localField, parV0, covParV0);
194 covParV0[0]=std::abs(covParV0[0]); covParV0[2]=std::abs(covParV0[2]); covParV0[5]=std::abs(covParV0[5]);
195 covParV0[9]=std::abs(covParV0[9]); covParV0[14]=std::abs(covParV0[14]);
202 if( target_trk->
fitP[2] == 0.){
207 target_trk->
iniP[0]=target_trk->
cnstP[0]=target_trk->
fitP[0];
208 target_trk->
iniP[1]=target_trk->
cnstP[1]=target_trk->
fitP[1];
209 target_trk->
iniP[2]=target_trk->
cnstP[2]=target_trk->
fitP[2];
211 target_trk->
iniP[0]=target_trk->
cnstP[0]=(target_trk->
fitP[0]+target_trk->
Perig[2])/2.;
212 target_trk->
iniP[1]=target_trk->
cnstP[1]=(target_trk->
fitP[1]+target_trk->
Perig[3])/2.;
213 target_trk->
iniP[2]=target_trk->
cnstP[2]=(target_trk->
fitP[2]+target_trk->
Perig[4])/2.;
215 if(tmpCov[0]>1.e12 || tmpCov[2]>1.e12)
return -18;
216 if(Pointing){tmpCov[0] += target_trk->
Perig[0]*target_trk->
Perig[0]; tmpCov[2] += target_trk->
Perig[1]*target_trk->
Perig[1];}
217 tmpCov[0] += 0.0001*0.0001; tmpCov[2] += 0.0002*0.0002;
220 if (IERR)
return -15;
229 double dptot[4],VrtMomCov[21];
231 if (IERR)
return -13;
250 long int Iter, IERR, iv,
i;
256 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
266 double Chi2Old=0.,Chi2Cur=0.;
267 for(Iter=0; Iter<100; Iter++){
269 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
291 if(std::abs(Chi2Cur-Chi2Old)<0.01)
break;
292 if(Iter>10 && (Chi2Cur-Chi2Old)>0.)
break;
294 if(Chi2Cur>1.e6 && Iter>0)
return -1;
299 if( cnstRemnants > 10000.
e0)
return -2;
300 if(cnstRemnants==0. && Chi2Cur>3000. )
return -1;
301 if( Chi2Cur>50000.)
return -1;
306 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
314 double * fullMatrix =
new double[fullNPar*fullNPar];
315 double * iniCovMatrix =
new double[fullNPar*fullNPar];
for(
int ss=0;
ss<fullNPar*fullNPar;
ss++) iniCovMatrix[
ss]=0.;
316 double * fullLSide =
new double[fullNPar];
317 double * tmpLSide =
new double[fullNPar];
319 std::vector<VKVertex> listVCopy(cascadeEvent_.
cascadeNV);
324 long int NParCur, NStart;
329 int NFitIterationMax=100;
330 int badStepCountMax=5;
332 cnstRemnants=100000.;
double cnstProgr=1.;
double minCnstRemnants=100000.;
335 for(Iter=0; Iter<=NFitIterationMax; Iter++){
338 for( iv=0; iv<fullNPar*fullNPar; iv++)fullMatrix[iv]=0.;
339 for( iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
362 copyFullMtx( vk->
ader, NParCur, NParCur, fullMatrix, NStart, fullNPar);
366 if(Iter==NFitIterationMax || fullSTOP || badStepCount>=badStepCountMax)
370 for(
i=0;
i<NParCur;
i++) fullLSide[
i+NStart] = tmpLSide[
i];
375 if( (Chi2Cur>1.e8 && Iter>0) || IERR){
376 delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
377 if(Chi2Cur>1.e8)
return -1;
383 if( Iter>3 && cnstProgr>4. && getBack<5 && fullSTOP==0 ) {
386 if(IERR){
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
return IERR;}
387 cnstRemnants=100000.; Chi2Old+=10000;
388 if(Iter==NFitIterationMax )Iter-=2;
389 if(Iter==NFitIterationMax-1)Iter-=1;
390 getBack++; badStepCount=0;
394 if(cnstRemnants<minCnstRemnants)minCnstRemnants=cnstRemnants;
395 if(minCnstRemnants==cnstRemnants){
396 for( iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
411 int tstRet=
fixPseudoTrackPt(fullNPar, fullMatrix, fullLSide, cascadeEvent_ );
412 if( tstRet ){
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
return -1; }
416 if( Iter<NFitIterationMax && fullSTOP==0 && badStepCount<badStepCountMax){
417 IERR =
vkMSolve(fullMatrix, fullLSide, fullNPar);
418 if(IERR){
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
return IERR;}
420 cascadeEvent_.
fullCovMatrix.reset(
new double[fullNPar*fullNPar] );
422 if(IERR){
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
425 std::unique_ptr<double[]> newCov(
new double[fullNPar*fullNPar]);
437 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
444 Chi2Full += signift*signift;
447 Chi2Diff=Chi2Old-Chi2Full;
448 if(Chi2Diff<0. && cnstProgr>0.99) badStepCount++;
449 if(cnstRemnants==minCnstRemnants || cnstProgr<0.9) badStepCount=0;
452 bool badFullStep=
false;
453 if( Iter>5 && Chi2Full/Chi2Cur>1.2) badFullStep=
true;
454 if( Iter==NFitIterationMax || fullSTOP ) badFullStep=
false;
460 if(IERR){
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
return IERR;}
462 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
473 if(cnstRemnants>1.
e4&&Iter>10)
474 {
delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
return -2;}
476 if( Iter<NFitIterationMax && Iter>2){
477 if(std::abs(Chi2Diff)<0.001 && cnstRemnants/minCnstRemnants<10.){
478 NFitIterationMax=Iter+1;
482 if ( Iter>NFitIterationMax+50 )
break;
483 if(badStepCount>badStepCountMax)
break;
491 delete[] fullMatrix;
delete[] fullLSide;
delete[] tmpLSide;
delete[] iniCovMatrix;
495 if( cnstRemnants > 1.
e0)
return -2;
505 double aermd[6],tmpd[6];
520 int IERR=
cfdinv(tmpd, aermd, -3);
if (IERR) { IERR = -4;
return IERR; }
563 double vShift, tmpPer[5], tmpWgt[15], tmpCov[15], targetVertex[3];
567 for( iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
573 vShift = sqrt( (vk->
refV[0]-targetVertex[0])*(vk->
refV[0]-targetVertex[0])
574 +(vk->
refV[1]-targetVertex[1])*(vk->
refV[1]-targetVertex[1])
575 +(vk->
refV[2]-targetVertex[2])*(vk->
refV[2]-targetVertex[2]) );
577 double dirMom=(pvx->refIterV[0]-targetVertex[0])*pvx->fitMom[0]+
578 (pvx->refIterV[1]-targetVertex[1])*pvx->fitMom[1]+
579 (pvx->refIterV[2]-targetVertex[2])*pvx->fitMom[2];
580 if(dirMom<0.)
cfdcopy(pvx->refIterV,targetVertex,3);
582 bool insideGoodVolume=
false;
586 if(!insideGoodVolume) {
return -16; }
598 if(eig5<1.
e-15 || tmpCov[0]>1.e7) {
601 tmpCov[1]=0.;tmpCov[3]=0.;tmpCov[6]=0.;tmpCov[10]=0.;
602 tmpCov[4]=0.;tmpCov[7]=0.;tmpCov[11]=0.;
603 tmpCov[8]=0.;tmpCov[12]=0.;
605 tmpCov[0]=std::abs(tmpCov[0]); tmpCov[2]=std::abs(tmpCov[2]);tmpCov[5]=std::abs(tmpCov[5]);
606 tmpCov[9]=std::abs(tmpCov[9]); tmpCov[14]=std::abs(tmpCov[14]);
609 if(tmpCov[0]>1.e7){ IERR=-7;
return IERR; }
610 IERR=
cfInv5(tmpCov, tmpWgt);
if (IERR) IERR=
cfdinv(tmpCov, tmpWgt, 5);
if(IERR)
return -8;
612 if ( vShift > 100. ) trk->
setReference( tmpPer, tmpCov );
615 if ( vShift > 100. )vk->
setRefV(targetVertex);
645 for( iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
674 for(iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
686 std::vector< Vect3DF > & cVertices,
687 std::vector< std::vector<double> > & covVertices,
688 std::vector< std::vector< VectMOM> > & fittedParticles,
689 std::vector< std::vector<double> > & cascadeCovar,
690 std::vector<double> & particleChi2,
691 std::vector<double> & fullCovar)
695 fittedParticles.clear();
696 particleChi2.clear();
700 std::vector< std::vector<double> > cascadeCovarFit;
703 double vBx,vBy,vBz,pp2,
pt,invR;
704 int iv,
it,jt, NTRK, pnt;
708 double **DPhys =
new double*[PDIM];
for(
it=0;
it<PDIM;
it++) DPhys[
it] =
new double[ADIM];
709 for(
it=0;
it<PDIM;
it++)
for(jt=0;jt<ADIM;jt++)DPhys[
it][jt]=0.;
715 std::vector<VectMOM> momCollector;
716 std::vector<double> tmpCov(6);
719 for( iv=0; iv<cascadeEvent_.
cascadeNV; iv++){
724 cVertices.push_back(vrtPos);
726 momCollector.clear();
728 double **Deriv =
new double*[DIM];
for(
it=0;
it<DIM;
it++) Deriv[
it] =
new double[DIM];
729 for(
it=0;
it<DIM;
it++)
for(jt=0;jt<DIM;jt++)Deriv[
it][jt]=0.;
730 Deriv[0][0]=1.;Deriv[1][1]=1.;Deriv[2][2]=1.;
731 DPhys[pntPhys+0][cascadeEvent_.
matrixPnt[iv]+0]=1.;
732 DPhys[pntPhys+1][cascadeEvent_.
matrixPnt[iv]+1]=1.;
733 DPhys[pntPhys+2][cascadeEvent_.
matrixPnt[iv]+2]=1.;
738 prtMom.Px=pp[0]; prtMom.Py=pp[1]; prtMom.Pz=pp[2]; prtMom.E=pp[3];
739 momCollector.push_back( prtMom );
741 pp2 = pp[0]*pp[0] + pp[1]*pp[1] +pp[2]*pp[2];
742 pt = sqrt(pp[0]*pp[0] + pp[1]*pp[1]);
746 DPhys[pntPhys+pnt+0][cascadeEvent_.
matrixPnt[iv]+pnt+0]= Deriv[pnt+0][pnt+0]= 0 ;
747 DPhys[pntPhys+pnt+0][cascadeEvent_.
matrixPnt[iv]+pnt+1]= Deriv[pnt+0][pnt+1]= -pp[1];
748 DPhys[pntPhys+pnt+0][cascadeEvent_.
matrixPnt[iv]+pnt+2]= Deriv[pnt+0][pnt+2]= -pp[0]/invR;
750 DPhys[pntPhys+pnt+1][cascadeEvent_.
matrixPnt[iv]+pnt+0]= Deriv[pnt+1][pnt+0]= 0 ;
751 DPhys[pntPhys+pnt+1][cascadeEvent_.
matrixPnt[iv]+pnt+1]= Deriv[pnt+1][pnt+1]= pp[0];
752 DPhys[pntPhys+pnt+1][cascadeEvent_.
matrixPnt[iv]+pnt+2]= Deriv[pnt+1][pnt+2]= -pp[1]/invR;
754 DPhys[pntPhys+pnt+2][cascadeEvent_.
matrixPnt[iv]+pnt+0]= Deriv[pnt+2][pnt+0]= -pp2/
pt;
755 DPhys[pntPhys+pnt+2][cascadeEvent_.
matrixPnt[iv]+pnt+1]= Deriv[pnt+2][pnt+1]= 0 ;
756 DPhys[pntPhys+pnt+2][cascadeEvent_.
matrixPnt[iv]+pnt+2]= Deriv[pnt+2][pnt+2]= -pp[2]/invR;
759 fittedParticles.push_back(momCollector);
760 cascadeCovar.push_back(
transformCovar(DIM, Deriv, cascadeCovarFit[iv] ) );
761 for(
int kk=0;
kk<6;
kk++) tmpCov[
kk]=cascadeCovar[iv][
kk];
762 covVertices.push_back(tmpCov);
763 for(
it=0;
it<DIM;
it++)
delete[]Deriv[
it];
769 fullCovar.resize(PDIM*(PDIM+1)/2);
771 for(jt=0; jt<=
it; jt++){
773 for(itn=0; itn<ADIM; itn++)
for(jtn=0; jtn<ADIM; jtn++)
tmp += DPhys[
it][itn]*DPhys[jt][jtn]*cascadeEvent_.
fullCovMatrix[itn*ADIM+jtn];
777 for(
it=0;
it<PDIM;
it++)
delete[]DPhys[
it];