77 void fillVertex(
VKVertex *vk,
int NTRK,
const long int *ich,
78 double xyz0[3],
double (*par0)[3],
double (*inp_Trk5)[5],
79 double (*inp_CovTrk5)[15]) {
83 double xyz[3] = {0., 0., 0.};
90 for (
int tk = 0; tk < NTRK; tk++) {
92 vk->
TrackList[tk] = std::make_unique<VKTrack>(
93 TrkID, &inp_Trk5[tk][0], &inp_CovTrk5[tk][0], vk, vrtForCFT.
wm[tk]);
96 vk->
tmpArr[tk]= std::make_unique< TWRK > ();
107 for(
int tk=0; tk<NTRK; tk++){
117 bool insideGoodVolume =
true;
124 return insideGoodVolume;
127 void protectCurvatureSign(
128 double ini,
double cur,
130 double x =
cur / ini;
135 Wgt[14] *= (0.7 * 0.7) / (
x *
x);
140 int i, jerr, tk,
it=0;
143 double chi2min, chi21s=11., chi22s=10., vShift;
144 double aermd[6],tmpd[6]={0.};
145 double tmpPer[5],tmpCov[15], tmpWgt[15];
146 double VrtMomCov[21],PartMom[4];
147 double cnstRemnants=0., iniCnstRem=1.e-12;
150 const double ConstraintAccuracy=1.e-6;
163 double zeroV[3]={0.,0.,0.};
169 std::vector<int>
index;
173 for(tk=0; tk<NTRK; tk++){
if( vrtForCFT.
indtrkmc[
ic][tk] )
index.push_back(tk); }
179 vk->
ConstraintList.emplace_back(std::make_unique<VKPointConstraint>( NTRK, vrtForCFT.
vrt, vk,
false));
182 vk->
ConstraintList.emplace_back(std::make_unique<VKPointConstraint>( NTRK, vrtForCFT.
vrt, vk,
true));
186 IERR=
cfdinv(tmpd, aermd, -3);
if (IERR) { IERR = -4;
return IERR; }
194 if( vrtForCFT.
Ap+vrtForCFT.
Bp+vrtForCFT.
Cp != 0.){
195 vk->
ConstraintList.emplace_back(std::make_unique<VKPlaneConstraint>( NTRK, vrtForCFT.
Ap, vrtForCFT.
Bp, vrtForCFT.
Cp, vrtForCFT.
Dp, vk));
218 bool extrapolationDone=
false;
219 bool forcedExtrapolation=
false;
221 double cnstRemnantsPrev=1.e20, Chi2Prev=0.;
int countBadStep=0;
225 savedExtrapVertices[
it-1].Set(newVrtXYZ);
230 extrapolationDone=
false;
231 bool insideGoodVolume=checkPosition(vk,newVrtXYZ);
234 if( forcedExtrapolation || vk->
truncatedStep || !insideGoodVolume){
235 insideGoodVolume=
false;
236 while( !insideGoodVolume ){
237 newVrtXYZ[0]=(2.*savedExtrapVertices[
it-1].X + savedExtrapVertices[
it-2].X)/3.;
238 newVrtXYZ[1]=(2.*savedExtrapVertices[
it-1].Y + savedExtrapVertices[
it-2].Y)/3.;
239 newVrtXYZ[2]=(2.*savedExtrapVertices[
it-1].Z + savedExtrapVertices[
it-2].Z)/3.;
240 savedExtrapVertices[
it-1].Set(newVrtXYZ);
241 insideGoodVolume=checkPosition(vk,newVrtXYZ);
242 if(!insideGoodVolume && savedExtrapVertices[
it-1].Dist3D(savedExtrapVertices[
it-2])<5.) {
return -11; }
245 extrapolationDone=
true;
246 forcedExtrapolation=
false;
247 double targV[3]={newVrtXYZ[0],newVrtXYZ[1],newVrtXYZ[2]};
248 for (tk = 0; tk < NTRK; ++tk) {
251 if(std::abs(tmpCov[14])<1.
e-20 || std::isnan(tmpCov[14])) {
return -7;}
254 if(eig5>0 && eig5<1.
e-15 ){
255 tmpCov[0]+=1.e-15; tmpCov[2]+=1.e-15; tmpCov[5]+=1.e-15; tmpCov[9]+=1.e-15; tmpCov[14]+=1.e-15;
256 }
else if(tmpCov[0]>1.e9 || eig5<0.) {
261 tmpCov[1]=0.;tmpCov[3]=0.;tmpCov[6]=0.;tmpCov[10]=0.;
262 tmpCov[4]=0.;tmpCov[7]=0.;tmpCov[11]=0.;
263 tmpCov[8]=0.;tmpCov[12]=0.;
265 tmpCov[0]=std::abs(tmpCov[0]); tmpCov[2]=std::abs(tmpCov[2]);tmpCov[5]=std::abs(tmpCov[5]);
266 tmpCov[9]=std::abs(tmpCov[9]); tmpCov[14]=std::abs(tmpCov[14]);
269 if(tmpCov[0]>1.e9){ IERR=-7;
return IERR; }
270 jerr=
cfInv5(tmpCov, tmpWgt);
271 if(jerr)jerr=
cfdinv(tmpCov, tmpWgt,5);
272 if(jerr){ IERR=-7;
return IERR; }
273 vk->
TrackList[tk]->setCurrent(tmpPer,tmpWgt);
282 for( tk=0; tk<NTRK; tk++) vk->
TrackList[tk]->restoreCurrentWgt();
289 for (tk = 0; tk < NTRK; ++tk){
295 for( tk=0; tk<NTRK; tk++){
305 if (IERR)
return IERR;
308 if(extrapolationDone && chi21s*10.<vk->
Chi2 &&
it>2 && vk->
Chi2>NTRK*5.){
309 double ddstep=savedExtrapVertices[
it-1].Dist3D(savedExtrapVertices[
it-2]);
312 forcedExtrapolation=
true;
317 chi22s = chi21s * 1.01 + 10.;
326 if(jerr!=0)
return -17;
327 cfdcopy( PartMom, &dparst[3], 3);
335 for( tk=0; tk<NTRK; tk++){
345 if ( IERR )
return IERR;
351 if (chi22s > 1e8) {
return -1; }
352 if (chi22s != chi22s) {
return -14; }
359 double chi2df = std::abs(chi21s - chi22s);
365 if((chi2df < PrecLimit) && (vShift < 0.001) && (cnstRemnants<ConstraintAccuracy)){
368 forcedExtrapolation=
true;
375 if ((chi2min*100. < chi22s) && (chi22s>
std::max( (2*NTRK-3)*10., 100.)) && (
it>5)){
377 IERR = -5;
return IERR;
385 cfdcopy( PartMom, &dparst[3], 3);
400 if(
it==1){
if(cnstRemnants>0.){iniCnstRem=cnstRemnants;}
else{iniCnstRem=1.e-12;} }
402 if(cnstRemnants > 0.1 && cnstRemnants/iniCnstRem > 0.1 ){
403 IERR = -6;
return IERR;
409 if(
it>5 && ((cnstRemnants>cnstRemnantsPrev)||cnstRemnants==0.) && chi22s>(Chi2Prev*1.0001)){
410 if(++countBadStep>5)
break;
411 }
else{countBadStep=0;}
412 Chi2Prev=chi22s; cnstRemnantsPrev=cnstRemnants;
418 if( cnstRemnants > ConstraintAccuracy ){
419 IERR = -6;
return IERR;
430 long int *ich,
double xyz0[3],
double (*par0)[3],
431 double (*inp_Trk5)[5],
double (*inp_CovTrk5)[15],
432 double xyzfit[3],
double (*parfs)[3],
double ptot[4],
433 double covf[21],
double &
chi2,
double *chi2tr)
436 std::fill_n(ptot,4,0.);
437 std::fill_n(covf,21,0.);
444 std::unique_ptr<VKVertex> MainVRT(
new VKVertex(*FitCONTROL));
445 fillVertex(MainVRT.get(), NTRK, ich, xyz0, par0, inp_Trk5, inp_CovTrk5);
449 int IERR = fitVertex( MainVRT.get());
450 if(IERR)
return IERR;
457 for (
int tk = 0; tk < NTRK; tk++) {
460 chi2tr[tk] = trk->
Chi2;
463 ptot[0]+=pp[0]; ptot[1]+=pp[1]; ptot[2]+=pp[2]; ptot[3]+=pp[3];
466 double vM2=(ptot[3]-ptot[2])*(ptot[3]+ptot[2]) - ptot[1]*ptot[1] - ptot[0]*ptot[0];
473 double dptot[5], VrtMomCov[21];
479 for(
int ti=0; ti<3+3*NTRK; ti++){
480 for(
int tj=0; tj<=ti; tj++){
484 int activeTrk[
vkalNTrkM]={0}; std::fill_n(activeTrk,NTRK,1);
485 double vrtMass=0., vrtMassError=0.;