29#include "CLHEP/Alist/AList.h"
37#include "AIDA/IHistogram1D.h"
55 for (
int i = 0; i <
nDeep; i++) {
95 if (printit) cout <<endl<<
"MdcSegGrouper::nextGroup starting group finder, nply = " <<
nPlyFilled << endl;
97 bool incrementNext =
true;
102 if(printit) std::cout<<
" reach end of list, break." << iply<< std::endl;
107 if(printit) std::cout<<
" leaveGap " <<std::endl;
109 if (iply ==
nPlyFilled - 1 && incrementNext) {
124 incrementNext =
false;
130 incrementNext =
true;
132 if(printit) { std::cout<<
"reach end of segs "<<std::endl; }
142 if(printit) { std::cout<<endl<<
" All done! "<<std::endl; }
153 std::cout<<
"ply " << iply<<
" seg "<<
currentSeg[iply]<<
": ";
157 std::cout<<
" ct " << info->
ct();
164 std::cout<< std::endl<<
" Used??";
167 if(printit) { std::cout<<
" segUsed! SKIP "<<std::endl; }
178 if(invalid){ std::cout<<
" invalid "<< std::endl;
179 }
else{ std::cout<<
" KEEP 1 "<< std::endl; }
181 if (invalid)
continue;
183 if(printit) std::cout<<
" KEEP 2 "<< std::endl;
201 if(lBad) std::cout<<
" incompWithGroup Bad! restart" << std::endl;
204 if (lBad)
goto restart;
210 if (printit) std::cout<<
"nextGoup() found " << nFound <<
" segment(s)"<<std::endl;
229 if (
nNull == 0)
return 1;
234 for (
int igap = 0; igap <
nNull; igap++) {
239 int inext = igap + 1;
241 if (inext >=
nNull)
return 1;
256 for (
int j = 0; j <
nNull; j++) {
272 TrkContext& context,
double t0,
double maxSegChisqO,
int combineByFitHits) {
276 if (3 ==
_debug) std::cout<<std::endl<<
"=====MdcSegGrouper::combineSegs=====" <<std::endl;
277 bool lSeed = (seed != 0);
281 double qualBest = -1000.;
287 double chiBest = 9999.;
292 segGroup =
new MdcSeg * [nToUse];
293 segGroupBest =
new MdcSeg * [nToUse];
301 if ((3 ==
_debug)&&lSeed) {
302 std::cout<<
"seed segment: ";
304 std::cout<< std::endl;
310 double seedAngle[2] = {0.,0.};
321 segGroup[nToUse-1] = seed;
324 if (nInGroup < 0)
continue;
325 if (!
_noInner && nInGroup < 2)
break;
326 if (nInGroup < nSegBest)
break;
329 cout << endl <<
"-----found a segment group by nextGroup()----- nInGroup "<<nInGroup<<
" nToUse "<<nToUse<<endl;
330 for(
int ii=0; ii<nToUse; ii++){
if(segGroup[ii]) {segGroup[ii]->
plotSegAll(); cout<<endl;} }
339 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
341 if (combineByFitHits == 0){
342 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
349 if (tkFit != 0) chisq =
calcParByHits(segGroup, nToUse, par, qual,nSegFit, param, Bz);
353 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
358 if (chisq < 0.)
continue;
363 else chiDof = chisq/(2.*nSegFit - 2.);
371 if(
_noInner) std::cout<<endl<<
" no inner chiDof = chisq "<<endl;
372 std::cout<< endl<<
"chisq "<<chisq<<
" nSegFit " << nSegFit<<
" chiDof "<<chiDof<<std::endl;
373 if(chiDof > maxSegChisqO) {
374 cout <<
"***** DROP this group by chiDof "<<chiDof<<
" > maxSegChisqO:"<<maxSegChisqO<<endl;
376 cout <<
"***** KEEP this group by chiDof "<<chiDof<<
" <= maxSegChisqO:"<<maxSegChisqO<<endl;
380 if (chiDof > maxSegChisqO)
continue;
383 if (qual > qualBest) {
387 paramBest[0] = param[0];
388 paramBest[1] = param[1];
390 for (
int i = 0; i < nToUse; i++) {
391 segGroupBest[i] = segGroup[i];
393 if (3 ==
_debug && 11 ==
_debug) std::cout<<
"Keep as best group. param: phi0/z0 "
394 <<paramBest[0]<<
" cpa/ct "<<paramBest[1]<< std::endl;
400 std::cout<< endl<<
"-----Parameter best group----- "<<std::endl;
401 std::cout<<
"paramBest "<<paramBest[0]<<
" "<<paramBest[1]<<
" chiBest " << chiBest<< std::endl;
404 trk =
storePar(trk, paramBest, chiBest, context, t0);
407 delete [] segGroupBest;
418 double smallRad = 1000.;
428 if(3 ==
_debug) std::cout<< endl<<
"-----transferHits for this segGroup----- " <<std::endl;
429 for (
int i = 0; i < nSegs; i++) {
430 if (segGroup[i] == 0)
continue;
432 cout << i <<
" "; segGroup[i]->
plotSegAll(); cout<< endl;
435 for (
int ihit = 0; ihit < segGroup[i]->
nHit(); ihit++) {
438 double radius = layer->
rMid();
439 if (radius < smallRad) {
445 if (radius > bigRad && !trk->
hasCurled()) {
457 if (theHits == 0)
return;
471 for(
int islayer=0; islayer<11; islayer++){
472 for(
int i=0; i<
segList[islayer].length(); i++){
473 segList[islayer][i]->plotSegAll();
474 std::cout<< std::endl;
481 int nToUse,
double& qual,
int& nSegFit,
double param[2]) {
483 if (11 ==
_debug) std::cout<<
"-----calculate group param by segment param-----"<<std::endl;
484 double wgtmat[3], wgtinv[3];
486 double temvec[2], diff[2];
489 wgtmat[0] = wgtmat[1] = wgtmat[2] = wgtpar[0] = wgtpar[1] = 0.0;
492 for (iPly = 0; iPly < nToUse; iPly++) {
496 if (segGroup[iPly] == 0)
continue;
500 for (
int i = 0; i < 3; i++) wgtmat[i] += (segInfo->
inverr())[i];
501 for (
int k = 0; k < 2; k++) {
502 param[k] = segInfo->
par(k);
505 param[k] =
mdcWrapAng(seedAngle[k], param[k]);
510 wgtpar[0] += temvec[0];
511 wgtpar[1] += temvec[1];
513 std::cout<<0<<
": param "<<param[0]<<
" inverr "<< segInfo->
inverr()[0]<<
" par*W "<<temvec[0]<<std::endl;
514 std::cout<<1<<
": param "<<param[1]<<
" "<<1/param[1]<<
" inverr "<< segInfo->
inverr()[1]<<
" par*W "<<temvec[1]<<std::endl;
515 std::cout<<
" " <<std::endl;
517 nhit += segGroup[iPly]->
nHit();
522 if (error && (11 ==
_debug)) {
523 cout <<
"ErrMsg(warning) "
524 <<
"failed matrix inversion in MdcTrackList::combineSegs" << endl;
530 std::cout<<
" param of wgtinv * wgtpar" << std::endl;
531 std::cout<<0<<
": param "<<param[0]<< std::endl;
532 std::cout<<1<<
": param "<<param[1]<<
" "<<1/param[1]<< std::endl;
533 std::cout<<
" " <<std::endl;
539 if(11 ==
_debug)cout<<endl<<
"-- Calculate track & chisq for this group "<<endl;
544 for (iPly = 0; iPly < nToUse; iPly++) {
545 if (segGroup[iPly] == 0)
continue;
547 for (
int j = 0; j < 2; j++) {
552 temPar = segInfo->
par(j);
555 std::cout<<
" segPar"<<j<<
" "<<temPar<< std::endl;
557 diff[j] = temPar - param[j];
561 std::cout<<
"inverr " <<segInfo->
inverr()[0]<<
" "
562 <<segInfo->
inverr()[1] <<
" "<<segInfo->
inverr()[2] << std::endl;
563 std::cout<<
"errmat " <<segInfo->
errmat()[0]<<
" "
564 <<segInfo->
errmat()[1] <<
" "<<segInfo->
errmat()[2] << std::endl;
565 std::cout<<
"diff " <<diff[0]<<
" "<<diff[1]<<
" temvec "<<temvec[0]<<
" "<<temvec[1]<< std::endl;
566 std::cout<< std::endl;
570 chisq += diff[0] * temvec[0] + diff[1] * temvec[1];
573 std::cout<<iPly<<
" chi2Add:"<<diff[0] * temvec[0] + diff[1] * temvec[1]<<
" diff0 "<< diff[0]<<
" vec0 "<<temvec[0]<<
" diff1 "<< diff[1]<<
" vec1 "<<temvec[1] << std::endl;
580 else chiDof = chisq/(2.*nSegFit - 2.);
583 if(
_noInner) cout<<
" no inner chiDof = chisq"<<endl;
584 cout <<
"segment:"<<endl<<
" chiDof "<<chiDof<<
" chisq "<< chisq <<
" nhit " << nhit <<
" phi0/z0 " <<
585 param[0] <<
" cpa/cot " << param[1] <<
" qual "<<(2. * nhit - chiDof) <<endl;
588 qual = 2. * nhit - chiDof;
599 if (11 ==
_debug ) debug =
true;
600 if (debug) std::cout<<
"-----calculate group param by hit-----"<<std::endl;
605 if (debug) std::cout<<
"nToUse="<<nToUse<<std::endl;
606 for (
int iPly = 0; iPly < nToUse; iPly++) {
607 if (segGroup[iPly] == 0)
continue;
611 if(debug) std::cout<<
"nHit in segment="<<segGroup[iPly]->
nHit()<<std::endl;
612 for (
int ii=0,iHit=0; ii<segGroup[iPly]->
nHit(); ii++){
617 int szCode = segInfo->
zPosition(*(segGroup[iPly]->hit(iHit)),par,&span,iHit,segGroup[iPly]->
bunchTime(),Bz);
618 if(debug)std::cout<<
" szCode "<<szCode;
619 if(szCode>0&&debug) std::cout<< iHit<<
" s "<< span.x[iHit]<<
" z "<<span.y[iHit] <<
" sigma "<<span.sigma[iHit];
620 if(debug)std::cout<<std::endl;
622 spanFit.
x[nHit]=span.x[iHit];
623 spanFit.
y[nHit]=span.y[iHit];
625 spanFit.
sigma[nHit]=1.;
626 if(debug)std::cout<< std::endl;
632 if(debug)std::cout<< __FILE__ <<
" " << __LINE__ <<
" nHit "<< nHit<<std::endl;
633 if (nHit>0) spanFit.
fit(nHit);
637 param[1] = spanFit.
slope;
638 if(debug)std::cout<<
"nHit "<<nHit<<
" intercept(z0) "<<param[0]<<
" slope(ct) " << param[1] <<std::endl;
640 qual = 2.*nHit - spanFit.
chisq/(spanFit.
nPoint - 2);
641 if(debug)std::cout<<
"chisq "<<spanFit.
chisq<<
" qual "<<qual<<std::endl;
643 return spanFit.
chisq;
AIDA::IHistogram1D * g_maxSegChisqSt
AIDA::IHistogram1D * g_maxSegChisqAx
AIDA::IHistogram1D * g_maxSegChisqSt
AIDA::IHistogram1D * g_maxSegChisqAx
const MdcHit * mdcHit() const
unsigned layernumber() const
unsigned wirenumber() const
const MdcLayer * layer() const
int combineSegs(MdcTrack *&, MdcSeg *seed, TrkContext &, double trackT0, double maxSegChisqO, int combineByFitHits=0)
virtual void resetComb(const MdcSeg *seed)=0
MdcSegGrouper(const MdcDetector *gm, int nDeep, int debug)
HepAList< MdcSeg > * segList
virtual int incompWithGroup(MdcSeg **segGroup, const MdcSeg *testSeg, int iply)=0
int nextGroup(MdcSeg **segGroup, bool printit)
double calcParBySegs(MdcSeg **segGroup, double seedAngle[2], int nToUse, double &qual, int &nSegFit, double param[2])
void transferHits(MdcTrack *track, int nSegs, MdcSeg **segGroup)
double calcParByHits(MdcSeg **segGroup, int nToUse, const TrkExchangePar &par, double &qual, int &nSegFit, double param[2], double Bz)
virtual MdcTrack * storePar(MdcTrack *trk, double parms[2], double chisq, TrkContext &, double trackT0)=0
HepAList< MdcSeg > ** combList
int zPosition(MdcHitUse &hitUse, const TrkRecoTrk &track, MdcLine *span, int hitFit, double t0) const
virtual bool parIsAngle(int i) const =0
const double * errmat() const
const double * inverr() const
MdcSegInfo * info() const
MdcHitUse * hit(int i) const
void setFirstLayer(const MdcLayer *l)
const MdcLayer * firstLayer() const
const MdcLayer * lastLayer() const
void setLastLayer(const MdcLayer *l)
virtual TrkExchangePar helix(double fltL) const =0
TrkHitOnTrk * appendHit(const TrkHitUse &theHit)
void setFltLen(double flt)
const BField & bField() const
const TrkFit * fitResult() const
int mdcTwoInv(double matrix[3], double invmat[3])
void mdcTwoVec(const double matrix[3], const double invect[2], double outvect[2])
double mdcWrapAng(double phi1, double phi2)