3 #include "cetlib_except/exception.h" 9 std::vector<int> &TPCClusterlist,
10 float &curvature_init,
17 unsigned int initialtpnTPCClusters,
21 size_t nTPCClusters = TPCClusterlist.size();
22 size_t firstTPCCluster = 0;
23 size_t farTPCCluster = TMath::Min(nTPCClusters-1, (
size_t) initialtpnTPCClusters);;
24 size_t intTPCCluster = farTPCCluster/2;
25 size_t lastTPCCluster = nTPCClusters-1;
27 float trackbeg[3] = {TPCClusters[TPCClusterlist[firstTPCCluster]].Position()[0],
28 TPCClusters[TPCClusterlist[firstTPCCluster]].Position()[1],
29 TPCClusters[TPCClusterlist[firstTPCCluster]].Position()[2]};
31 float tp1[3] = {TPCClusters[TPCClusterlist[intTPCCluster]].Position()[0],
32 TPCClusters[TPCClusterlist[intTPCCluster]].Position()[1],
33 TPCClusters[TPCClusterlist[intTPCCluster]].Position()[2]};
35 float tp2[3] = {TPCClusters[TPCClusterlist[farTPCCluster]].Position()[0],
36 TPCClusters[TPCClusterlist[farTPCCluster]].Position()[1],
37 TPCClusters[TPCClusterlist[farTPCCluster]].Position()[2]};
41 std::cout <<
"TPCCluster Dump in initial_trackpar_estimate: " <<
std::endl;
42 for (
size_t i=0;i<nTPCClusters;++i)
45 std::cout << i <<
" : " <<
46 TPCClusters[TPCClusterlist[ihf]].Position()[0] <<
" " <<
47 TPCClusters[TPCClusterlist[ihf]].Position()[1] <<
" " <<
48 TPCClusters[TPCClusterlist[ihf]].Position()[2] <<
std::endl;
53 std::cout <<
"first TPCCluster: " << firstTPCCluster <<
", inter TPCCluster: " << intTPCCluster <<
" " <<
" far TPCCluster: " << farTPCCluster <<
std::endl;
54 std::cout <<
"in the TPCCluster list: " << TPCClusterlist[firstTPCCluster] <<
" " << TPCClusterlist[intTPCCluster] <<
" " << TPCClusterlist[farTPCCluster] <<
std::endl;
55 std::cout <<
"First TPCCluster x, y, z: " << trackbeg[0] <<
" " << trackbeg[1] <<
" " << trackbeg[2] <<
std::endl;
56 std::cout <<
"Inter TPCCluster x, y, z: " << tp1[0] <<
" " << tp1[1] <<
" " << tp1[2] <<
std::endl;
57 std::cout <<
"Far TPCCluster x, y, z: " << tp2[0] <<
" " << tp2[1] <<
" " << tp2[2] <<
std::endl;
63 x_other_end = TPCClusters[TPCClusterlist[lastTPCCluster]].Position()[0];
68 curvature_init =
gar::rec::capprox(trackbeg[1],trackbeg[2],tp1[1],tp1[2],tp2[1],tp2[2],ycc,zcc);
80 std::vector<double> dtpcclusy;
81 std::vector<double> dtpcclusz;
82 for (
size_t i=0;i<nTPCClusters; ++i)
84 dtpcclusy.push_back(TPCClusters[TPCClusterlist[i]].Position()[1]);
85 dtpcclusz.push_back(TPCClusters[TPCClusterlist[i]].Position()[2]);
87 int ict = nTPCClusters;
88 ouchef(dtpcclusy.data(),dtpcclusz.data(),ict,dycc,dzcc,drcc,dchisq,ifail);
89 if (ifail == 0 && drcc != 0)
93 if (curvature_init < 0) drcc = -
std::abs(drcc);
98 curvature_init = 1.0/drcc;
102 phi_init = TMath::ATan2( trackbeg[2] - zcc, ycc - trackbeg[1] );
103 float phi2 = phi_init;
104 if (curvature_init<0) phi_init += TMath::Pi();
105 float radius_init = 10000;
106 if (curvature_init != 0) radius_init = 1.0/curvature_init;
108 float dx1 = tp2[0] -
xpos;
111 float dphi2 = TMath::ATan2(tp2[2]-zcc,ycc-tp2[1])-phi2;
112 if (dphi2 > TMath::Pi()) dphi2 -= 2.0*TMath::Pi();
113 if (dphi2 < -TMath::Pi()) dphi2 += 2.0*TMath::Pi();
114 lambda_init = TMath::ATan(1.0/((radius_init/dx1)*dphi2));
125 std::cout <<
"phi calc: dz, dy " << tp2[2]-trackbeg[2] <<
" " << tp2[1]-trackbeg[1] <<
std::endl;
126 std::cout <<
"initial curvature, phi, lambda: " << curvature_init <<
" " << phi_init <<
" " << lambda_init <<
std::endl;
136 float &xc,
float &yc)
147 float det = x3*y2-x2*y3;
148 if (TMath::Abs(det)<1
e-10){
152 float u = 0.5* (x2*(x2-
x3)+y2*(y2-y3))/det;
153 float x0 = x3*0.5-y3*u;
154 float y0 = y3*0.5+x3*u;
155 float c2 = 1/TMath::Sqrt(x0*x0+y0*y0);
171 std::vector<int> &hlf,
172 std::vector<int> &hlb,
174 float &lengthforwards,
175 float &lengthbackwards,
190 for (
size_t iTPCCluster=0; iTPCCluster < TPCClusters.size(); ++iTPCCluster)
192 for (
int i=0; i<3; ++i)
194 float c = TPCClusters[iTPCCluster].Position()[i];
207 ihex[i] = iTPCCluster;
212 ihex[i+3] = iTPCCluster;
221 for (
size_t i=0; i<6; ++i)
224 TVector3 poshc(TPCClusters[ihex[i]].Position());
225 for (
size_t iTPCCluster=0; iTPCCluster<TPCClusters.size(); ++iTPCCluster)
227 TVector3 hp(TPCClusters[iTPCCluster].Position());
228 sumd += (poshc - hp).Mag();
244 hlf.push_back(ihex[imax]);
245 TVector3 lpos(TPCClusters[hlf[0]].Position());
246 TVector3 lastpos=lpos;
250 TVector3 curdir(0,0,0);
251 bool havecurdir =
false;
254 for (
size_t inh=1;inh<TPCClusters.size();++inh)
258 for (
size_t jh=0;jh<TPCClusters.size();++jh)
261 for (
size_t kh=0;kh<hlf.size();++kh)
263 if (hlf[kh] == (
int) jh)
270 TVector3 hpos(TPCClusters[jh].Position());
275 float dtmp = (hpos-lastpos).Dot(curdir);
280 d= dtmp + 0.1 * ((hpos-lastpos).Cross(curdir)).Mag();
304 lengthforwards += (hpadd-lastpos).Mag();
306 if ( (hpadd-cdpos).Mag() > dmindir)
308 TVector3 cdcand(TPCClusters[hlf[cdposindex]].Position());
309 size_t itctmp = cdposindex;
310 for (
size_t itc=cdposindex+1; itc<hlf.size(); ++itc)
312 TVector3 cdcandt(TPCClusters[hlf[itc]].Position());
313 if ( (hpadd-cdcandt).Mag() < dmindir )
326 curdir = hpadd - cdcand;
327 curdir *= (1.0/curdir.Mag());
335 for (
size_t iTPCCluster=0; iTPCCluster<TPCClusters.size(); ++iTPCCluster)
337 printf(
"Sort compare: %5d %10.3f %10.3f %10.3f %5d %10.3f %10.3f %10.3f\n",
339 TPCClusters[iTPCCluster].Position()[0],
340 TPCClusters[iTPCCluster].Position()[1],
341 TPCClusters[iTPCCluster].Position()[2],
343 TPCClusters[hlf[iTPCCluster]].Position()[0],
344 TPCClusters[hlf[iTPCCluster]].Position()[1],
345 TPCClusters[hlf[iTPCCluster]].Position()[2]);
352 for (
size_t i=0; i< hlf.size(); ++i)
354 hlb.push_back(hlf[hlf.size()-1-i]);
356 lengthbackwards = lengthforwards;
360 for (
size_t iTPCCluster=0; iTPCCluster<TPCClusters.size(); ++iTPCCluster)
362 printf(
"Backward Sort compare: %5d %10.3f %10.3f %10.3f %5d %10.3f %10.3f %10.3f\n",
364 TPCClusters[iTPCCluster].Position()[0],
365 TPCClusters[iTPCCluster].Position()[1],
366 TPCClusters[iTPCCluster].Position()[2],
368 TPCClusters[hlb[iTPCCluster]].Position()[0],
369 TPCClusters[hlb[iTPCCluster]].Position()[1],
370 TPCClusters[hlb[iTPCCluster]].Position()[2]);
380 if (i == j)
return true;
383 int next = (link1.at(i) == -1) ? link2.at(i) : link1.at(i);
386 if (next == j)
return true;
388 next = (link1.at(next) == prev) ? link2.at(next) : link1.at(next);
400 std::vector<int> &hlf,
401 std::vector<int> &hlb,
403 float &lengthforwards,
404 float &lengthbackwards,
407 size_t nclus = TPCClusters.size();
412 if (nclus == 0)
return;
420 size_t ndists = nclus*(nclus-1)/2;
421 std::vector<float> dists(ndists);
422 std::vector<int> dsi(ndists);
423 std::vector<int> link1(nclus,-1);
424 std::vector<int> link2(nclus,-1);
425 for (
size_t i=0; i<nclus-1; ++i)
427 TVector3 iclus(TPCClusters.at(i).Position());
428 for (
size_t j=i+1; j<nclus; ++j)
430 TVector3 jclus(TPCClusters.at(j).Position());
431 float d = (iclus-jclus).Mag();
435 TMath::Sort( (
int) ndists, dists.data(), dsi.data(), false );
444 for (
size_t k=0;
k<ndists; ++
k)
448 size_t k2 = dsi.at(
k);
449 if (dists.at(k2) > dcut)
break;
451 TVector3 iclus(TPCClusters.at(i).Position());
452 TVector3 jclus(TPCClusters.at(j).Position());
454 if (link1.at(i) == -1)
456 if (link1.at(j) == -1)
463 if (link2.at(j) == -1)
465 TVector3 j2clus(TPCClusters.at(link1.at(j)).Position());
480 if (link2.at(i) == -1)
482 if (link1.at(j) == -1)
489 if (link2.at(j) == -1)
491 TVector3 j2clus(TPCClusters.at(link1.at(j)).Position());
516 std::vector<int> used(nclus,-1);
517 std::vector<std::vector<int> > clistv;
522 for (
size_t i=0; i<nclus; ++i)
524 int il1 = link1.at(i);
525 int il2 = link2.at(i);
527 if ( (il1 == -1 && il2 != -1) || (il1 != -1 && il2 == -1) )
542 for (
size_t i=ifirst; i<nclus; ++i)
544 if (used.at(i) == 1)
continue;
545 std::vector<int> clist;
550 int il1 = link1.at(idx);
551 int il2 = link2.at(idx);
554 if (il1 != -1) u1 = used.at(il1);
556 if (il2 != -1) u2 = used.at(il2);
558 while (u1 == -1 || u2 == -1)
560 idx = (u1 == -1) ? il1 : il2;
561 clist.push_back(idx);
566 if (il1 != -1) u1 = used.at(il1);
568 if (il2 != -1) u2 = used.at(il2);
570 clistv.push_back(clist);
577 for (
size_t ichain=0; ichain<clistv.size(); ++ichain)
579 size_t cursize = clistv.at(ichain).size();
586 if (ibest == -1)
return;
588 for (
size_t i=0; i<clistv.at(ibest).size(); ++i)
590 hlf.push_back(clistv[ibest].at(i));
593 TVector3 lastpoint(TPCClusters.at(hlf.at(i-1)).Position());
594 TVector3 curpoint(TPCClusters.at(hlf.at(i)).Position());
595 lengthforwards += (curpoint-lastpoint).Mag();
598 for (
size_t i=0; i< hlf.size(); ++i)
600 hlb.push_back(hlf[hlf.size()-1-i]);
602 lengthbackwards = lengthforwards;
619 size_t i = TMath::Min(i_in,j_in);
620 size_t j = TMath::Max(i_in,j_in);
621 size_t k = (n*(n-1)/2) - (n-i)*((n-i)-1)/2 + j - i - 1;
637 throw cet::exception(
"gar::rec::idx2ijsort2: k too big. ") << k <<
" " <<
n;
639 i = n - 2 - TMath::Floor(TMath::Sqrt( (
double) (-8*k + 4*n*(n-1)-7) )/2.0 - 0.5);
640 j = k + i + 1 - n*(n-1)/2 + (n-i)*((n-i)-1)/2;
644 double &rcirc,
double &chisq,
int &ifail)
693 const double eps = 1.0e-10;
695 const int nptmax=1000;
792 vlf[0] = x[npf-1] - x[0];
793 vlf[1] = y[npf-1] - y[0];
799 vmf[0] = x[ihalf-1] - x[0];
800 vmf[1] = y[ihalf-1] - y[0];
806 if (vlf[0]*vmf[0] + vlf[1]*vmf[1] < 0.)
goto label_12;
811 if (fabs(y[npf-1] - y[0]) > fabs(x[npf-1]-x[0]))
872 a2 = (fg-h2-d2)/fact;
874 a1 = (d2*(f+
g) - 2.*(p2+q2))/fact;
876 a0 = (d2*(h2-fg) + 2.*(p2*g + q2*f) - 4.*xd*yd*
h)/fact;
883 ya = a0 + xa*(a1 + xa*(a2 + xa*(xa-4.0)));
884 if (fabs(ya) > fabs(yb))
goto label_4;
885 if (iter >= itmax)
goto label_4;
886 dy = a1 + xa*(a22 + xa*(4.*xa - 12.));
888 if (fabs(xa-xb) <= eps)
goto label_5;
907 den = 1./sqrt(x1*x1 + y1*y1 + gam*det*det);
909 alf = -(xm*det +
x1)*den;
910 bet = -(ym*det + y1)*den;
912 gam = ((rm-gam)*det + 2.*(xm*x1 + ym*y1))*den*0.5;
916 gmm = gam + alf*xm + bet*ym + 0.5*cur*rm;
917 chisq = ((0.5*cur)*(0.5*cur)*d2 + cur*(alm*xd + bem*yd + gmm*gam0)
918 + alm*alm*x2 + bem*bem*y2 + 2.*alm*bem*xy + gmm*gmm) /rn;
bool sort2_check_cyclic(std::vector< int > &link1, std::vector< int > &link2, int i, int j)
void ouchef(double *x, double *y, int np, double &xcirccent, double &ycirccent, double &rcirc, double &chisq, int &ifail)
static constexpr double g
float capprox(float x1, float y1, float x2, float y2, float x3, float y3, float &xc, float &yc)
void sort_TPCClusters_along_track(const std::vector< gar::rec::TPCCluster > &TPCClusters, std::vector< int > &hlf, std::vector< int > &hlb, int printlevel, float &lengthforwards, float &lengthbackwards, float sorttransweight, float sortdistback)
void sort_TPCClusters_along_track2(const std::vector< gar::rec::TPCCluster > &TPCClusters, std::vector< int > &hlf, std::vector< int > &hlb, int printlevel, float &lengthforwards, float &lengthbackwards, float dcut)
size_t ij2idxsort2(size_t nclus, size_t i, size_t j)
void idx2ijsort2(size_t nclus, size_t idx, size_t &i, size_t &j)
cet::coded_exception< error, detail::translate > exception
QTextStream & endl(QTextStream &s)
int initial_trackpar_estimate(const std::vector< gar::rec::TPCCluster > &TPCClusters, std::vector< int > &TPCClusterlist, float &curvature_init, float &lambda_init, float &phi_init, float &xpos, float &ypos, float &zpos, float &x_other_end, unsigned int initialtpnTPCClusters, int printlevel)