29#include "CLHEP/Alist/AList.h"
37#include "AIDA/IHistogram1D.h"
55 for (
int i = 0; i <
nDeep; i++) {
94 if (printit) cout <<endl<<
"MdcSegGrouper::nextGroup starting group finder, nply = " <<
nPlyFilled << endl;
96 bool incrementNext =
true;
101 if(printit) std::cout<<
" reach end of list, break." << iply<< std::endl;
106 if(printit) std::cout<<
" leaveGap " <<std::endl;
108 if (iply ==
nPlyFilled - 1 && incrementNext) {
123 incrementNext =
false;
129 incrementNext =
true;
131 if(printit) { std::cout<<
"reach end of segs "<<std::endl; }
141 if(printit) { std::cout<<endl<<
" All done! "<<std::endl; }
152 std::cout<<
"ply " << iply<<
" seg "<<
currentSeg[iply]<<
": ";
156 std::cout<<
" ct " << info->
ct();
163 std::cout<< std::endl<<
" Used??";
166 if(printit) { std::cout<<
" segUsed! SKIP "<<std::endl; }
177 if(invalid){ std::cout<<
" invalid "<< std::endl;
178 }
else{ std::cout<<
" KEEP 1 "<< std::endl; }
180 if (invalid)
continue;
182 if(printit) std::cout<<
" KEEP 2 "<< std::endl;
200 if(lBad) std::cout<<
" incompWithGroup Bad! restart" << std::endl;
203 if (lBad)
goto restart;
209 if (printit) std::cout<<
"nextGoup() found " << nFound <<
" segment(s)"<<std::endl;
228 if (
nNull == 0)
return 1;
233 for (
int igap = 0; igap <
nNull; igap++) {
238 int inext = igap + 1;
240 if (inext >=
nNull)
return 1;
255 for (
int j = 0; j <
nNull; j++) {
271 TrkContext& context,
double t0,
double maxSegChisqO,
int combineByFitHits) {
275 if (3 ==
_debug) std::cout<<std::endl<<
"=====MdcSegGrouper::combineSegs=====" <<std::endl;
276 bool lSeed = (seed != 0);
280 double qualBest = -1000.;
286 double chiBest = 9999.;
291 segGroup =
new MdcSeg * [nToUse];
292 segGroupBest =
new MdcSeg * [nToUse];
300 if ((3 ==
_debug)&&lSeed) {
301 std::cout<<
"seed segment: ";
303 std::cout<< std::endl;
309 double seedAngle[2] = {0.,0.};
315 int iprint = ( 3 ==
_debug);
317 while ( (nInGroup =
nextGroup(segGroup, iprint)) != 0) {
320 segGroup[nToUse-1] = seed;
323 if (nInGroup < 0)
continue;
324 if (nInGroup < 2)
break;
325 if (nInGroup < nSegBest)
break;
328 cout << endl <<
"-----found a segment group by nextGroup()----- nInGroup "<<nInGroup<<
" nToUse "<<nToUse<<endl;
329 for(
int ii=0; ii<nToUse; ii++){
if(segGroup[ii]) {segGroup[ii]->
plotSegAll(); cout<<endl;} }
338 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
340 if (combineByFitHits == 0){
341 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
348 if (tkFit != 0) chisq =
calcParByHits(segGroup, nToUse, par, qual,nSegFit, param, Bz);
352 chisq =
calcParBySegs(segGroup, seedAngle, nToUse, qual, nSegFit, param);
358 if (chisq < 0.)
continue;
359 double chiDof = chisq/(2.*nSegFit - 2.);
367 std::cout<< endl<<
"chisq "<<chisq<<
" nSegFit " << nSegFit<<
" chiDof "<<chiDof<<std::endl;
368 if(chiDof > maxSegChisqO) {
369 cout <<
"***** DROP this group by chiDof "<<chiDof<<
" > maxSegChisqO:"<<maxSegChisqO<<endl;
371 cout <<
"***** KEEP this group by chiDof "<<chiDof<<
" <= maxSegChisqO:"<<maxSegChisqO<<endl;
375 if (chiDof > maxSegChisqO)
continue;
378 if (qual > qualBest) {
382 paramBest[0] = param[0];
383 paramBest[1] = param[1];
385 for (
int i = 0; i < nToUse; i++) {
386 segGroupBest[i] = segGroup[i];
388 if (3 ==
_debug && 11 ==
_debug) std::cout<<
"Keep as best group. param: phi0/z0 "
389 <<paramBest[0]<<
" cpa/ct "<<paramBest[1]<< std::endl;
395 std::cout<< endl<<
"-----Parameter best group----- "<<std::endl;
396 std::cout<<
"paramBest "<<paramBest[0]<<
" "<<paramBest[1]<<
" chiBest " << chiBest<< std::endl;
399 trk =
storePar(trk, paramBest, chiBest, context, t0);
402 delete [] segGroupBest;
413 double smallRad = 1000.;
423 if(3 ==
_debug) std::cout<< endl<<
"-----transferHits for this segGroup----- " <<std::endl;
424 for (
int i = 0; i < nSegs; i++) {
425 if (segGroup[i] == 0)
continue;
427 cout << i <<
" "; segGroup[i]->
plotSegAll(); cout<< endl;
430 for (
int ihit = 0; ihit < segGroup[i]->
nHit(); ihit++) {
433 double radius = layer->
rMid();
434 if (radius < smallRad) {
440 if (radius > bigRad && !trk->
hasCurled()) {
452 if (theHits == 0)
return;
466 for(
int islayer=0; islayer<11; islayer++){
467 for(
int i=0; i<
segList[islayer].length(); i++){
468 segList[islayer][i]->plotSegAll();
469 std::cout<< std::endl;
476 int nToUse,
double& qual,
int& nSegFit,
double param[2]) {
478 if (11 ==
_debug) std::cout<<
"-----calculate group param by segment param-----"<<std::endl;
479 double wgtmat[3], wgtinv[3];
481 double temvec[2], diff[2];
484 wgtmat[0] = wgtmat[1] = wgtmat[2] = wgtpar[0] = wgtpar[1] = 0.0;
487 for (iPly = 0; iPly < nToUse; iPly++) {
491 if (segGroup[iPly] == 0)
continue;
495 for (
int i = 0; i < 3; i++) wgtmat[i] += (segInfo->
inverr())[i];
496 for (
int k = 0; k < 2; k++) {
497 param[k] = segInfo->
par(k);
500 param[k] =
mdcWrapAng(seedAngle[k], param[k]);
505 wgtpar[0] += temvec[0];
506 wgtpar[1] += temvec[1];
508 std::cout<<0<<
": param "<<param[0]<<
" inverr "<< segInfo->
inverr()[0]<<
" par*W "<<temvec[0]<<std::endl;
509 std::cout<<1<<
": param "<<param[1]<<
" "<<1/param[1]<<
" inverr "<< segInfo->
inverr()[1]<<
" par*W "<<temvec[1]<<std::endl;
510 std::cout<<
" " <<std::endl;
512 nhit += segGroup[iPly]->
nHit();
517 if (error && (11 ==
_debug)) {
518 cout <<
"ErrMsg(warning) "
519 <<
"failed matrix inversion in MdcTrackList::combineSegs" << endl;
525 std::cout<<
" param of wgtinv * wgtpar" << std::endl;
526 std::cout<<0<<
": param "<<param[0]<< std::endl;
527 std::cout<<1<<
": param "<<param[1]<<
" "<<1/param[1]<< std::endl;
528 std::cout<<
" " <<std::endl;
534 if(11 ==
_debug)cout<<endl<<
"-- Calculate track & chisq for this group "<<endl;
539 for (iPly = 0; iPly < nToUse; iPly++) {
540 if (segGroup[iPly] == 0)
continue;
542 for (
int j = 0; j < 2; j++) {
547 temPar = segInfo->
par(j);
550 std::cout<<
" segPar"<<j<<
" "<<temPar<< std::endl;
552 diff[j] = temPar - param[j];
556 std::cout<<
"inverr " <<segInfo->
inverr()[0]<<
" "
557 <<segInfo->
inverr()[1] <<
" "<<segInfo->
inverr()[2] << std::endl;
558 std::cout<<
"errmat " <<segInfo->
errmat()[0]<<
" "
559 <<segInfo->
errmat()[1] <<
" "<<segInfo->
errmat()[2] << std::endl;
560 std::cout<<
"diff " <<diff[0]<<
" "<<diff[1]<<
" temvec "<<temvec[0]<<
" "<<temvec[1]<< std::endl;
561 std::cout<< std::endl;
565 chisq += diff[0] * temvec[0] + diff[1] * temvec[1];
568 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;
572 double chiDof = chisq/(2.*nSegFit - 2.);
574 cout <<
"segment:"<<endl<<
" chiDof "<<chiDof<<
" chisq "<< chisq <<
" nhit " << nhit <<
" phi0/z0 " <<
575 param[0] <<
" cpa/cot " << param[1] <<
" qual "<<(2. * nhit - chiDof) <<endl;
578 qual = 2. * nhit - chiDof;
589 if (11 ==
_debug ) debug =
true;
590 if (debug) std::cout<<
"-----calculate group param by hit-----"<<std::endl;
595 if (debug) std::cout<<
"nToUse="<<nToUse<<std::endl;
596 for (
int iPly = 0; iPly < nToUse; iPly++) {
597 if (segGroup[iPly] == 0)
continue;
601 if(debug) std::cout<<
"nHit in segment="<<segGroup[iPly]->
nHit()<<std::endl;
602 for (
int ii=0,iHit=0; ii<segGroup[iPly]->
nHit(); ii++){
607 int szCode = segInfo->
zPosition(*(segGroup[iPly]->hit(iHit)),par,&span,iHit,segGroup[iPly]->
bunchTime(),Bz);
608 if(debug)std::cout<<
" szCode "<<szCode;
609 if(szCode>0&&debug) std::cout<< iHit<<
" s "<< span.x[iHit]<<
" z "<<span.y[iHit] <<
" sigma "<<span.sigma[iHit];
610 if(debug)std::cout<<std::endl;
612 spanFit.
x[nHit]=span.x[iHit];
613 spanFit.
y[nHit]=span.y[iHit];
615 spanFit.
sigma[nHit]=1.;
616 if(debug)std::cout<< std::endl;
622 if(debug)std::cout<< __FILE__ <<
" " << __LINE__ <<
" nHit "<< nHit<<std::endl;
623 if (nHit>0) spanFit.
fit(nHit);
627 param[1] = spanFit.
slope;
628 if(debug)std::cout<<
"nHit "<<nHit<<
" intercept(z0) "<<param[0]<<
" slope(ct) " << param[1] <<std::endl;
630 qual = 2.*nHit - spanFit.
chisq/(spanFit.
nPoint - 2);
631 if(debug)std::cout<<
"chisq "<<spanFit.
chisq<<
" qual "<<qual<<std::endl;
633 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)