37#include "GaudiKernel/NTuple.h"
61 patternList(useAllAmbig) {
78 unsigned int groupWord;
82 static const int layerWire[8][2] =
83 { { 0, -1}, { 0, 0}, { 1, 0}, { 2, -1},
84 { 2, 0}, { 3, -1}, { 3, 0}, { 3, 1} };
96 for (
int i = 0; i < nslay; i++) layArray[i] = slayer->
layer(i);
97 if(nslay != 4) layArray[3] = 0;
107 for (
int cellIndex = 0; cellIndex < layArray[1]->
nWires(); cellIndex++) {
109 double phi = layArray[1]->
getWire(cellIndex)->
phiE();
110 for(
int i = 0; i < 4; i++ ) {
111 wireIndex[i] = cellIndex;
112 if ( slayer->
slayNum() > 4)
continue;
113 if ( (slayer->
slayNum() > 9) && (i==3) )
break;
114 if ( i == 1 )
continue;
115 if ( i == 3 ) phi = layArray[2]->
getWire(wireIndex[2])->
phiE();
116 BesAngle tmp(phi - layArray[i]->phiEPOffset());
117 int wlow = (int)floor(layArray[i]->nWires() * tmp.
rad() / twopi );
118 int wbig = (int)ceil(layArray[i]->nWires() * tmp.
rad() / twopi );
129 wireIndex[i] =
mdcWrapWire(wireIndex[i], layArray[i]->nWires());
133 unsigned bitWord = 1u;
135 for (
int ii = 0; ii < 8; ii++) {
138 if(layArray[ layerWire[ii][0] ] == 0)
continue;
140 int laynum = layArray[ layerWire[ii][0] ]->
layNum();
141 int wire = wireIndex[ layerWire[ii][0] ] + layerWire[ii][1];
142 wire =
mdcWrapWire(wire, layArray[ layerWire[ii][0] ]->nWires());
145 if ( !usedHits.
get(thisHit)->deadHit() ) {
146 groupHits[ii] = thisHit;
147 groupWord |= bitWord;
154 if ( (ii == 2 && nGroup < 1) || (ii == 4 && nGroup < 2) )
break;
157 if (nGroup < 3)
continue;
160 int lastWire =
mdcWrapWire(cellIndex - 1, layArray[0]->nWires());
161 if (map->
hitWire(layArray[1]->
layNum(), lastWire) != 0) lPrevHit = 1;
166 int nPatt = patternList.
npatt4[groupWord];
168 if ((layArray[1]->layNum()==5) && (cellIndex ==46)) {
169 for(
int i=0;i<4;i++){
176 newSegs = tryPatterns(groupHits, groupWord, 4, lPrevHit, nPatt,
177 patternList.
allowedPatt4[groupWord], slayer, segs, usedHits, map,
186 for (
int i = 0; i < 8; i++) {
187 if (groupHits[i] != 0) {
188 if (usedHits.
get(groupHits[i])->usedSeg() == 0) nUnused++;
192 nPatt = patternList.
npatt3[groupWord];
195 newSegs = tryPatterns(groupHits, groupWord, 3, lPrevHit, nPatt,
196 patternList.
allowedPatt3[groupWord], slayer, segs, usedHits, map,
203 slayer = slayer->
next();
211 nSegs = segs.
count();
214 cout <<
"Number of Segs found: " << nSegs <<
"\n";
222MdcSegFinder::tryPatterns(
MdcHit *groupHits[8],
223 unsigned groupWord,
int nInPatt,
int lPrevHit,
int npatt,
233 patterns = patternList.
patt3;
236 patterns = patternList.
patt4;
249 for (
int iPatt = 0; iPatt < npatt; iPatt++) {
250 unsigned thisPat = patterns[ allowedPat[iPatt] ];
251 unsigned testWord = thisPat & groupWord;
253 if (
countbits(testWord) < nInPatt)
continue;
254 if (lPrevHit && nInPatt == 3 && (thisPat == 051 || thisPat == 0111))
259 unsigned bitadd = 1u;
261 for (
int ibit = 0; ibit < nbit; ibit++) {
263 if (bitadd & thisPat) {
265 if (layer == 0) cout <<
"huh?" << endl;
266 span.x[nhit] = layer->
rMid() - slay->
rad0();
267 spanHits[nhit] = groupHits[ibit];
269 if (nhit == nInPatt)
break;
278 std::map<int, MdcSeg*> bestTrialSegs;
280 int namb = 1 << nInPatt;
282 for (
int iamb = 0; iamb < namb; iamb++) {
285 if (ambigPatt[ allowedPat[iPatt] ][iamb] != 1)
continue;
290 for (ihit = 0; ihit < nInPatt; ihit++) {
291 spanAmbig[ihit] = ( (1u<<ihit) &iamb) ? 1: -1;
292 nUsed += usedHits.
get(spanHits[ihit])->usedAmbig( spanAmbig[ihit] );
294 if (nUsed >= nInPatt)
continue;
305 double phiIn =
mdcWrapAng(spanHits[nInPatt-1]->phi(),spanHits[0]->phi());
306 double dphidr = ( (spanHits[nInPatt-1]->
phi() + spanAmbig[nhit-1] *
307 spanHits[nInPatt-1]->
driftDist(t0,spanAmbig[nhit-1]) /
308 rOut) - (phiIn+ spanAmbig[0] * spanHits[0]->driftDist(t0,spanAmbig[0]) / rIn)) / (rOut - rIn);
310 double corr = sqrt( 1 + slay->
rad0() * slay->
rad0() * dphidr * dphidr );
313 std::cout<<__FILE__<<
" "<<__LINE__<<
" corr" <<corr<<
" phi_n "
314 <<spanHits[nInPatt-1]->
phi()<<
" dd_n "<< spanAmbig[nhit-1] *
315 spanHits[nInPatt-1]->
driftDist(t0,spanAmbig[nhit-1])
317 <<
" dd_in "<< spanAmbig[0] * spanHits[0]->
driftDist(t0,spanAmbig[0]) << std::endl;
321 bool crossAxis=
false;
328 for (ihit = 0; ihit < nInPatt; ihit++) {
329 ahit = spanHits[ihit];
330 double rinv = 1. / ahit->
layer()->
rMid();
331 double drift = ahit->
driftDist(t0,spanAmbig[ihit]);
332 span.y[ihit] = ahit->
phi() + spanAmbig[ihit] *
333 drift * (corr * rinv);
336 std::cout<<
" in segment finding ("<<ahit->
layernumber()<<
","<<ahit->
wirenumber()<<
")"<<
" |driftDist| "<< fabs(drift)<<
" ambig "<<spanAmbig[ihit]<<
" corr "<<corr<<
" rinv "<<rinv<<
" sigma "<<ahit->
sigma(drift,spanAmbig[ihit])<<std::endl;
340 span.sigma[ihit] = ahit->
sigma(fabs(drift), spanAmbig[ihit]) * (corr * rinv);
343 sigmaSum+= span.sigma[ihit];
345 driftHit[ihit]=drift;
349 if ( (span.y[ihit]!=0) && (!crossAxis)){
350 if ( (yOld / span.y[ihit]) < 0) crossAxis =
true;
365 for (
int ihit=0 ; ihit < nInPatt; ihit++){
367 if (
abs(span.y[ihit]) < 0.7)
break;
368 if (span.y[ihit] < 0)span.y[ihit]+=twopi;
375 if (5 == segs.
segPar()->
lPrint) std::cout<<
" first fit("<<nInPatt<<
")"<<std::endl;
378 span.intercept = temp.
posRad();
379 double t_segchi2 = span.chisq / (nInPatt - 2) ;
383 for(
int ii=0;ii<nInPatt;ii++){
384 std::cout<<__FILE__<<
" "<<__LINE__<<
" "<<ii <<
" span.x "<<setw(10)<<span.x[ii]<<
" y "<<setw(10)<<span.y[ii]<<
" sigma "<<setw(10)<<span.sigma[ii]<< std::endl;
396 std::cout<<__FILE__<<
" "<<__LINE__<<
" pattern "<< thisPat
397 <<
" nHit "<<nInPatt <<
" chi2/dof " << t_segchi2
398 <<
" chisq "<<span.chisq <<
" maxChisq="<<segs.
segPar()->
maxChisq <<std::endl;
399 for(
int jj=0; jj<nInPatt; jj++){
400 std::cout <<
"resid["<<jj<<
"] "<<span.resid[jj]<<
" sigma "<<span.sigma[jj]<<
" chisq add "<<span.resid[jj] * span.resid[jj] / (span.sigma[jj] * span.sigma[jj]) << std::endl;
405 std::cout<<__FILE__<<
" "<<__LINE__<<
"CUT! chi2/(N-2) " <<span.chisq / (nInPatt - 2) <<
" > "<< segs.
segPar()->
maxChisq<< std::endl;
406 for(std::map<int,MdcSeg*>::iterator
iter = bestTrialSegs.begin();
iter != bestTrialSegs.end();
iter++) {
407 cout<<
" bestTrialSeg nHit="<<
iter->first<<endl;
414 if(5 == segs.
segPar()->
lPrint) std::cout<<
" CUT by span.|slope| "<<span.slope<<
" > pi"<<std::endl;
418 int nInSeg = nInPatt;
424 corr = sqrt( 1 + slay->
rad0() * slay->
rad0() * dphidr * dphidr );
426 for (ihit = 0; ihit < nInSeg; ihit++) {
427 ahit = spanHits[ihit];
428 double rinv = 1. / ahit->
layer()->
rMid();
429 double drift = ahit->
driftDist(t0,spanAmbig[ihit]);
430 span.y[ihit] = ahit->
phi() + spanAmbig[ihit] *
431 drift * (corr * rinv);
438 span.sigma[ihit] = ahit->
sigma(fabs(drift), spanAmbig[ihit]) * (corr * rinv);
440 if ( (span.y[ihit]!=0) && (!crossAxis)){
441 if ( (yOld / span.y[ihit]) < 0) crossAxis =
true;
448 for (
int ihit=0 ; ihit < nInPatt; ihit++){
450 if (
abs(span.y[ihit]) < 0.7)
break;
451 if (span.y[ihit] < 0)span.y[ihit]+=twopi;
454 if (5 == segs.
segPar()->
lPrint) std::cout<<
" second fit("<<nInSeg<<
")"<<std::endl;
458 span.intercept = temp.
posRad();
462 std::cout<<__FILE__<<
" "<<__LINE__<<
"CUT! chi2/(N-2) " <<span.chisq / (nInPatt - 2) <<
" > "<< segs.
segPar()->
maxChisq<< std::endl;
463 for(std::map<int,MdcSeg*>::iterator
iter = bestTrialSegs.begin();
iter != bestTrialSegs.end();
iter++) {
464 cout<<
" bestTrialSeg nHit="<<
iter->first<<endl;
475 trialSeg->
setValues(nInPatt, nInSeg, spanHits, &span, slay, spanAmbig);
478 std::cout<<
" try: "<<std::endl;
480 std::cout<<
" "<<std::endl;
484 nInSeg = trialSeg->
addHits(&span, spanHits, map, corr);
489 for(std::map<int,MdcSeg*>::iterator
iter = bestTrialSegs.begin();
iter != bestTrialSegs.end();
iter++) {
490 cout<<
" bestTrialSeg nHit="<<
iter->first<<
" chi2 "<<
iter->second->chisq()<<endl;
492 std::cout<<
"trialSeg chisq "<<trialSeg->
chisq()<<std::endl;
493 cout <<
"segment phi: " <<
494 trialSeg->
phi() <<
" slope: " << trialSeg->
slope() <<
495 " nhit: " << trialSeg->
nHit() <<
" chi2 "<<trialSeg->
chisq() << endl;
497 for (
int i = 0; i < trialSeg->
nHit(); i++) {
498 int ambi = trialSeg->
hit(i)->
ambig();
501 cout <<
" ambig " <<ambi;
507 if (nInPatt == 4) trialSeg->
setFull();
510 double t_mcRadio = -1.;
511 double t_nAmbigRight= 0.;
513 for(
int ii=0;ii<trialSeg->
nHit();ii++){
528 for(
int ii=0;ii<trialSeg->
nHit();ii++){
532 if(t_mcRadio>-998.) t_mcRadio = t_nSame/nInSeg;
556 std::map<int,MdcSeg*>::iterator
iter = bestTrialSegs.find(trialSeg->
nHit());
557 if(
iter==bestTrialSegs.end()){
558 bestTrialSegs.insert(std::map<int, MdcSeg*>::value_type(trialSeg->
nHit(),trialSeg));
560 std::cout<<
" ----insert "<<trialSeg->
nHit()<<std::endl;
561 trialSeg->
plotSegAll(); std::cout<<
" \n "<<std::endl;
564 if(trialSeg->
chisq()<
iter->second->chisq()){
567 iter->second = trialSeg;
569 std::cout<<
" ----update "<<
iter->first<<std::endl;
570 trialSeg->
plotSegAll(); std::cout<<
" \n "<<std::endl;
574 std::cout<<
"DROP TrialSeg " <<std::endl;
575 trialSeg->
plotSegAll(); std::cout<<
" \n "<<std::endl;
598 trialSeg =
new MdcSeg(t0);
603 for(std::map<int,MdcSeg*>::iterator
iter = bestTrialSegs.begin();
iter != bestTrialSegs.end();
iter++) {
606 std::cout<<
" append bestTrialSeg of nHit " <<
iter->first <<std::endl;
607 iter->second->plotSeg(); std::cout<<std::endl;
611 bestTrialSegs.clear();
NTuple::Item< double > g_findSegMc
NTuple::Tuple * g_tupleFindSeg
NTuple::Item< int > g_findSegPat34
NTuple::Item< double > g_findSegAmbig
NTuple::Item< int > g_findSegPat
NTuple::Item< double > g_findSegChi2Refit
int haveDigiAmbig[43][288]
NTuple::Item< double > g_findSegIntercept
NTuple::Item< double > g_findSegChi2
NTuple::Item< double > g_findSegChi2Add
NTuple::Item< double > g_findSegSlope
NTuple::Item< int > g_findSegSl
NTuple::Item< int > g_findSegNhit
NTuple::Item< double > g_findSegMc
NTuple::Tuple * g_tupleFindSeg
NTuple::Item< int > g_findSegPat34
NTuple::Item< double > g_findSegAmbig
NTuple::Item< int > g_findSegPat
NTuple::Item< double > g_findSegChi2Refit
int haveDigiAmbig[43][288]
NTuple::Item< double > g_findSegIntercept
NTuple::Item< double > g_findSegChi2
NTuple::Item< double > g_findSegChi2Add
NTuple::Item< double > g_findSegSlope
NTuple::Item< int > g_findSegSl
NTuple::Item< int > g_findSegNhit
const MdcSuperLayer * firstSlay(void) const
MdcHit * hitWire(int lay, int wire) const
const MdcHit * mdcHit() const
unsigned layernumber() const
unsigned wirenumber() const
void print(std::ostream &o) const
double sigma(double, int, double, double, double) const
double driftDist(double, int, double, double, double) const
const MdcLayer * layer() const
MdcSWire * getWire(int wire) const
bool get(const K &theKey, V &theAnswer) const
MdcSegFinder(int useAllAmbig)
int createSegs(const MdcDetector *gm, MdcSegList &segs, const MdcMap< const MdcHit *, MdcSegUsage * > &usedHits, const MdcHitMap *map, double tbunch)
void append(MdcSeg *aSeg)
void resetSeed(const MdcDetector *gm)
void setValues(int nInPatt, int nhit, MdcHit *hits[], MdcLine *span, const MdcSuperLayer *slay, int ambig[])
int addHits(MdcLine *span, MdcHit *hits[], const MdcHitMap *, double corr)
void setPattern(unsigned thePatt)
MdcHitUse * hit(int i) const
void markHits(const MdcMap< const MdcHit *, MdcSegUsage * > &usedHits) const
const MdcSuperLayer * next(void) const
const MdcLayer * lastLayer(void) const
const MdcLayer * layer(int i) const
const MdcLayer * firstLayer(void) const
int countbits(unsigned word)
double mdcWrapAng(double phi1, double phi2)
int mdcWrapWire(int wireIn, int nCell)