00001 #include "MdcxReco/MdcxFindSegs.h"
00002 #include "MdcxReco/MdcxHit.h"
00003 #include "MdcGeom/MdcDetector.h"
00004 #include "MdcTrkRecon/mdcWrapWire.h"
00005 #include "MdcGeom/BesAngle.h"
00006 #include "MdcxReco/MdcxParameters.h"
00007
00008 using std::cout;
00009 using std::endl;
00010 using std::ostream;
00011 #include "AIDA/IHistogram1D.h"
00012 #include "AIDA/IHistogram2D.h"
00013 extern AIDA::IHistogram1D* g_csmax4;
00014 extern AIDA::IHistogram1D* g_csmax3;
00015
00016 int MdcxFindSegs::wireGroups[11][288][16] = { { {0} } };
00017
00018 MdcxFindSegs::MdcxFindSegs(MdcDetector* g, const HepAList<MdcxHit> &hitlist, int debug){
00019 m_debug = debug;
00020 gm = g;
00021 dchitlist = hitlist;
00022 initWireGroups();
00023 process();
00024 }
00025
00026 MdcxFindSegs::MdcxFindSegs(const HepAList<MdcxHit> &hitlist, int debug){
00027 m_debug = debug;
00028 gm = MdcDetector::instance();
00029 dchitlist = hitlist;
00030 initWireGroups();
00031 process();
00032 }
00033
00034 MdcxFindSegs::~MdcxFindSegs() { KillList(); }
00035
00036 void MdcxFindSegs::initWireGroups(void) {
00037
00038 static bool alreadyInit = false;
00039 if (alreadyInit) return;
00040
00041
00042 int lastsl= gm->nSuper();
00043 for (int sl = 0; sl < lastsl; sl++) {
00044 const MdcLayer *layArray[4];
00045 const MdcSuperLayer* slayer = gm->SuperLayer(sl);
00046 int nslay = slayer->nLayers();
00047 for (int i = 0; i < nslay; i++) layArray[i] = slayer->layer(i);
00048 int cellMax = layArray[1]->nWires();
00049
00050 for (int cellIndex = 0; cellIndex < cellMax ; cellIndex++) {
00051 int wireIndex[4];
00052 if (slayer->slayNum() > 4) {
00053 wireIndex[0]= cellIndex;
00054 wireIndex[1]= cellIndex+1;
00055 wireIndex[2]= cellIndex;
00056 wireIndex[3]= cellIndex+1;
00057
00058 wireIndex[1] = mdcWrapWire(wireIndex[1], layArray[1]->nWires());
00059 wireIndex[3] = mdcWrapWire(wireIndex[3], layArray[3]->nWires());
00060 } else {
00061 wireIndex[1]= cellIndex+1;
00062
00063 double phi = layArray[1]->getWire(wireIndex[1])->phiE();
00064 for(int i = 0; i < 4; i++ ) {
00065 if ( i == 1 ) continue;
00066
00067 if ( i == 3 ) phi = layArray[2]->getWire(wireIndex[2])->phiE();
00068 BesAngle tmp(phi - layArray[i]->phiEPOffset());
00069 if ( i==3 ) {
00070 wireIndex[i]=(tmp==0)?1:(int)ceil(layArray[i]->nWires()*tmp.rad()/twopi);
00071 }else{
00072 wireIndex[i]=(tmp==0)?-1:(int)floor(layArray[i]->nWires()*tmp.rad()/twopi);
00073 }
00074 wireIndex[i] = mdcWrapWire(wireIndex[i], layArray[i]->nWires());
00075 }
00076 }
00077
00078 int* w = wireGroups[sl][cellIndex];
00079 for (int i = 0; i < 4; i++) {
00080 for (int j = 0; j < 4; j++) {
00081
00082 w[4*i+j] = mdcWrapWire((wireIndex[i]+j-1), layArray[i]->nWires());
00083 }
00084 }
00085 }
00086 }
00087
00088
00089 alreadyInit = true;
00090 return;
00091 }
00092
00093 void MdcxFindSegs::process() {
00094
00095
00096 int lflag[44][288] = { {0} };
00097 int pflag[44][288] = { {0} };
00098 int ix,iy,iyp,iyn,cellMax;
00099
00100
00101 int ipoison=0;
00102
00103 int lastlay= gm->nLayer();
00104
00105
00106 int k=0;
00107 while(dchitlist[k]){
00108 MdcxHit* temp = dchitlist[k];
00109 k++;
00110 lflag[temp->Layer()][temp->WireNo()]=k;
00111 }
00112
00113
00114 if (ipoison){
00115 for (ix=0; ix<lastlay; ix++){
00116 cellMax = gm->Layer(ix)->nWires();
00117
00118 for (iy=0; iy<cellMax; iy++){
00119 iyp=iy+1; if (iyp == cellMax)iyp=0;
00120 iyn=iy-1; if (iyn == -1)iyn=cellMax-1;
00121 if ((lflag[ix][iyp] != 0) && (lflag[ix][iyn] != 0)) {
00122 pflag[ix][iy] = 1;
00123 lflag[ix][iy] = 0;
00124
00125 }
00126 }
00127
00128 }
00129 }
00130
00131 float csmax_4 = MdcxParameters::csmax4;
00132 float csmax_3 = MdcxParameters::csmax3;
00133 int lastsl= gm->nSuper();
00134
00135
00136 for (sl = 0; sl < lastsl; sl++) {
00137 int fsl=4*sl;
00138 int l0=fsl, l1=fsl+1, l2=fsl+2, l3=fsl+3;
00139 int iprt = 0;
00140
00141 const MdcLayer *layArray[4];
00142 const MdcSuperLayer* slayer = gm->SuperLayer(sl);
00143 int nslay = slayer->nLayers();
00144 for (int i = 0; i < nslay; i++) layArray[i] = slayer->layer(i);
00145 if(3 == m_debug){
00146 cout<<"slayer No. "<<slayer->index()<<endl;
00147 }
00148
00149 cellMax = layArray[1]->nWires();
00150
00151
00152 for (int cellIndex=0; cellIndex<cellMax ; cellIndex++) {
00153 int* w = wireGroups[sl][cellIndex];
00154 unsigned int sig_mark = 0;
00155 for (int ilayer = l0; ilayer <= l3; ilayer++) {
00156 for (int iwire = 3; iwire >= 0; iwire--) {
00157 if (lflag[ilayer][w[4*(ilayer-l0)+iwire]]) {
00158 sig_mark |= 0x1;
00159 }
00160 sig_mark <<= 1;
00161 }
00162 }
00163 sig_mark >>= 1;
00164
00165 int goodSegNo = 0;
00166 int nPat = m_segPat.patt4_size;
00167 int iPat = (sig_mark & 0x0200) ? 0 : 11;
00168 for ( ; iPat < nPat; iPat++) {
00169 int pat = m_segPat.patt4[iPat];
00170 if ((pat & sig_mark) == pat) {
00171 if(3 == m_debug) {
00172 cout<< "pat " << std::hex << pat << std::dec << " with wire";
00173 for (int tmpi = 0; tmpi < 4; tmpi++) {
00174 cout<<"("<<l0+tmpi<<","<<w[4*tmpi+m_segPat.wirePat4[iPat][tmpi]-1]<<")";
00175 }
00176 cout << endl;
00177 }
00178 int w0 = lflag[l0][w[0 +m_segPat.wirePat4[iPat][0]-1]] - 1;
00179 int w1 = lflag[l1][w[4 +m_segPat.wirePat4[iPat][1]-1]] - 1;
00180 int w2 = lflag[l2][w[8 +m_segPat.wirePat4[iPat][2]-1]] - 1;
00181 int w3 = lflag[l3][w[12+m_segPat.wirePat4[iPat][3]-1]] - 1;
00182 int tw[4] = {w0, w1, w2, w3};
00183
00184 int namb = m_segPat.ambPat4_size[iPat];
00185 for (int iamb = 0; iamb < namb; iamb++) {
00186 int amb = m_segPat.ambigPatt4[iPat][iamb];
00187 fithel = trial(w0, w1, w2, w3, amb);
00188 if(3 == m_debug){
00189 cout<<"chisq "<<fithel.Chisq()
00190 <<" <? csmax4 "<<csmax_4<< endl;
00191 if (fithel.Chisq() < csmax_4) {
00192 cout<<"Accept this seg"<<endl;
00193 }else{
00194 cout<<"DROP this seg"<<endl;
00195 }
00196 }
00197 if(g_csmax4) g_csmax4->fill(fithel.Chisq());
00198 if (fithel.Chisq() < csmax_4) {
00199 if (iprt) printseg(fithel, pat,amb);
00200 appendseg(fithel, pat, amb);
00201 goodSegNo++;
00202 }
00203 }
00204 }
00205 }
00206 if (goodSegNo != 0) continue;
00207 nPat = m_segPat.patt3_size;
00208 iPat = (sig_mark & 0x0200) ? 0 : 14;
00209 for ( ; iPat < nPat; iPat++) {
00210 if (iPat > nPat-3) {
00211 if ((iPat == nPat-2) && (sig_mark&0x2121 == 0x2121)) continue;
00212 if ((iPat == nPat-1) && (sig_mark&0x2122 == 0x2122)) continue;
00213 }
00214 int pat = m_segPat.patt3[iPat];
00215 if ((pat & sig_mark) == pat) {
00216 if(3 == m_debug) {
00217 cout<<"MdcxFindSegs: in pat "<<std::hex<<pat<<std::dec<<" with wire";
00218 for (int tmpi = 0; tmpi < 4; tmpi++) {
00219 if (m_segPat.wirePat3[iPat][tmpi] == 0) continue;
00220 cout<< " (" << l0+tmpi << "," << w[4*tmpi+m_segPat.wirePat3[iPat][tmpi]-1] << ")";
00221 }
00222 cout<< endl;
00223 }
00224 int wn[3];
00225 for (int iw = 0, iwp = 0; iwp < 4; iwp++) {
00226 int wireNo = m_segPat.wirePat3[iPat][iwp];
00227 if( wireNo == 0 ) continue;
00228 wn[iw++] = lflag[l0+iwp][w[4*iwp+wireNo-1]] - 1;
00229 }
00230
00231 int namb = m_segPat.ambPat3_size[iPat];
00232 for (int iamb = 0; iamb < namb; iamb++) {
00233 int amb = m_segPat.ambigPatt3[iPat][iamb];
00234 fithel = trial(wn[0], wn[1], wn[2], amb);
00235 if(3 == m_debug){
00236 cout<<"chisq "<<fithel.Chisq()
00237 <<" <? csmax3 "<<csmax_3<< endl;
00238 if (fithel.Chisq() < csmax_3) {
00239 cout<<"Accept this seg"<<endl;
00240 }else{
00241 cout<<"DROP this seg"<<endl;
00242 }
00243 }
00244 if(g_csmax3) g_csmax3->fill(fithel.Chisq());
00245 if (fithel.Chisq() < csmax_3) {
00246 if (iprt) printseg(fithel, pat, amb);
00247 appendseg(fithel, pat ,amb);
00248 }
00249 }
00250 }
00251 }
00252 }
00253 }
00254
00255 if(3 == m_debug){
00256 cout << "MdcxFindSegs found " << MdcxSeglist.length() << " segs" << endl;
00257 }
00258 return;
00259 }
00260
00261 void MdcxFindSegs::print(ostream &o, int pmax)const {
00262 int mcheck=pmax; if (MdcxSeglist.length() < pmax)mcheck=MdcxSeglist.length();
00263 o << " First " << mcheck << " Drift Chamber Segs:" << endl;
00264 for(int i=0; i<mcheck; i++){
00265 o << " Superlayer # " << MdcxSeglist[i]->SuperLayer();
00266 o << " # of hits " << MdcxSeglist[i]->Nhits() << endl;
00267 }
00268 }
00269
00270 MdcxFittedHel MdcxFindSegs::trial(int i1, int i2, int i3, int i4, int amb) {
00271 HepAList<MdcxHit> seg;
00272 MdcxHit* t1 = dchitlist[i1]; MdcxHit* t2 = dchitlist[i2];
00273 MdcxHit* t3 = dchitlist[i3]; MdcxHit* t4 = dchitlist[i4];
00274 seg.append(t1); seg.append(t2); seg.append(t3); seg.append(t4);
00275 double dx, dy, d0;
00276 if (amb==0){dx=t4->xneg()-t1->xneg(); dy=t4->yneg()-t1->yneg(); d0=-t1->d();}
00277 if (amb==1){dx=t4->xneg()-t1->xpos(); dy=t4->yneg()-t1->ypos(); d0= t1->d();}
00278 if (amb==2){dx=t4->xpos()-t1->xneg(); dy=t4->ypos()-t1->yneg(); d0=-t1->d();}
00279 if (amb==3){dx=t4->xpos()-t1->xpos(); dy=t4->ypos()-t1->ypos(); d0= t1->d();}
00280 double phi0 = atan2(dy,dx); dx=t1->x(); dy=t1->y();
00281 MdcxHel hel(d0,phi0,0.0,0.0,0.0,0.0,111,0,dx,dy);
00282 hel.SetTurnFlag(1);
00283 MdcxFittedHel temp(seg, hel, 1.0);
00284 if (3 == m_debug) {
00285 cout<<"trial4 amb "<<amb
00286 << ": phi0 " << phi0 << " d0 " << d0
00287 << " dx " << dx << " dy " << dy << endl;
00288
00289 }
00290 return temp;
00291 }
00292
00293 MdcxFittedHel MdcxFindSegs::trial(int i1, int i2, int i3, int amb) {
00294 HepAList<MdcxHit> seg;
00295 MdcxHit* t1 = dchitlist[i1];
00296 MdcxHit* t2 = dchitlist[i2];
00297 MdcxHit* t3 = dchitlist[i3];
00298 seg.append(t1); seg.append(t2); seg.append(t3);
00299 double dx,dy,d0;
00300 if (amb==0){dx=t3->xneg()-t1->xneg(); dy=t3->yneg()-t1->yneg(); d0=-t1->d();}
00301 if (amb==1){dx=t3->xneg()-t1->xpos(); dy=t3->yneg()-t1->ypos(); d0= t1->d();}
00302 if (amb==2){dx=t3->xpos()-t1->xneg(); dy=t3->ypos()-t1->yneg(); d0=-t1->d();}
00303 if (amb==3){dx=t3->xpos()-t1->xpos(); dy=t3->ypos()-t1->ypos(); d0= t1->d();}
00304
00305 double phi0 = atan2(dy,dx); dx=t1->x(); dy=t1->y();
00306 MdcxHel hel(d0,phi0,0.0,0.0,0.0,0.0,111,0,dx,dy);
00307 hel.SetTurnFlag(1);
00308 MdcxFittedHel temp(seg,hel,1.0);
00309
00310 if (3 == m_debug) {
00311 cout<<"trial3 amb "<<amb
00312 << ": phi0 " << phi0 << " d0 " << d0
00313 << " dx " << dx << " dy " << dy << endl;
00314 }
00315 return temp;
00316 }
00317
00318 void MdcxFindSegs::appendseg(MdcxFittedHel& fithel,int pat,int amb){
00319 MdcxSeg *tempseg = new MdcxSeg(fithel, pat, amb);
00320 MdcxSeglist.append(tempseg);
00321 }
00322
00323 void MdcxFindSegs::printseg(MdcxFittedHel& fithel, int pat, int amb, int subtry){
00324 cout << "Seg: pat " << pat << " amb " << amb << " subtry " << subtry
00325 << " sl " << sl << " fail "
00326 << fithel.Fail() << " chi2 " << fithel.Chisq() << endl;
00327 }
00328