BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
MdcxFindSegs.cxx
Go to the documentation of this file.
1#include "MdcxReco/MdcxFindSegs.h"
2#include "MdcxReco/MdcxHit.h"
3#include "MdcGeom/MdcDetector.h"
4#include "MdcTrkRecon/mdcWrapWire.h"
5#include "MdcGeom/BesAngle.h"
6#include "MdcxReco/MdcxParameters.h"
7
8using std::cout;
9using std::endl;
10using std::ostream;
11#include "AIDA/IHistogram1D.h"
12#include "AIDA/IHistogram2D.h"
13extern AIDA::IHistogram1D* g_csmax4;
14extern AIDA::IHistogram1D* g_csmax3;
15
16int MdcxFindSegs::wireGroups[11][288][16] = { { {0} } };
17
19 m_debug = debug;
20 gm = g;
21 dchitlist = hitlist;
23 process();
24}
25
27 m_debug = debug;
29 dchitlist = hitlist;
31 process();
32}
33
35
37 //Assure initWireGroups run only once
38 static bool alreadyInit = false;
39 if (alreadyInit) return;
40
41 //Make wire groups
42 int lastsl= gm->nSuper();
43 for (int sl = 0; sl < lastsl; sl++) { // loop over superLayers
44 const MdcLayer *layArray[4];
45 const MdcSuperLayer* slayer = gm->SuperLayer(sl);
46 int nslay = slayer->nLayers();
47 for (int i = 0; i < nslay; i++) layArray[i] = slayer->layer(i);
48 int cellMax = layArray[1]->nWires();
49 // loop over cells
50 for (int cellIndex = 0; cellIndex < cellMax ; cellIndex++) {
51 int wireIndex[4];
52 if (slayer->slayNum() > 4) {
53 wireIndex[0]= cellIndex;
54 wireIndex[1]= cellIndex+1;
55 wireIndex[2]= cellIndex;
56 wireIndex[3]= cellIndex+1;
57
58 wireIndex[1] = mdcWrapWire(wireIndex[1], layArray[1]->nWires());
59 wireIndex[3] = mdcWrapWire(wireIndex[3], layArray[3]->nWires());
60 } else {
61 wireIndex[1]= cellIndex+1;
62 //take No.2 wire as referenc wire
63 double phi = layArray[1]->getWire(wireIndex[1])->phiE();
64 for(int i = 0; i < 4; i++ ) {
65 if ( i == 1 ) continue;
66 //change reference wire
67 if ( i == 3 ) phi = layArray[2]->getWire(wireIndex[2])->phiE();
68 BesAngle tmp(phi - layArray[i]->phiEPOffset());//yzhang temp
69 if ( i==3 ) {
70 wireIndex[i]=(tmp==0)?1:(int)ceil(layArray[i]->nWires()*tmp.rad()/twopi);
71 }else{
72 wireIndex[i]=(tmp==0)?-1:(int)floor(layArray[i]->nWires()*tmp.rad()/twopi);
73 }
74 wireIndex[i] = mdcWrapWire(wireIndex[i], layArray[i]->nWires());
75 }
76 }
77 // set wireGroups
78 int* w = wireGroups[sl][cellIndex];
79 for (int i = 0; i < 4; i++) {
80 for (int j = 0; j < 4; j++) {
81 //wireGroups[sl][cellIndex][4*i+j] = wireIndex[i] + (j - 1);
82 w[4*i+j] = mdcWrapWire((wireIndex[i]+j-1), layArray[i]->nWires());
83 }
84 }
85 } //end loop cells
86 } //end loop superLayers
87
88 //Set alreadyInit flag
89 alreadyInit = true;
90 return;
91}
92
94 //if(3 == m_debug) cout <<" Event contains "<<dchitlist.length()<<" MdcxHit "<<endl;
95 // for last super layer use 44
96 int lflag[44][288] = { {0} };
97 int pflag[44][288] = { {0} };
98 int ix,iy,iyp,iyn,cellMax;
99
100 //yzhang 2009-10-20 do not use poison
101 int ipoison=0;
102 //number of last layer
103 int lastlay= gm->nLayer();
104
105 // tag hit's lfalg
106 int k=0;
107 while(dchitlist[k]){
108 MdcxHit* temp = dchitlist[k];
109 k++;
110 lflag[temp->Layer()][temp->WireNo()]=k;
111 }
112
113 // try poisoning
114 if (ipoison){
115 for (ix=0; ix<lastlay; ix++){
116 cellMax = gm->Layer(ix)->nWires();
117 // if have hits in left and right , poison this cell
118 for (iy=0; iy<cellMax; iy++){
119 iyp=iy+1; if (iyp == cellMax)iyp=0;
120 iyn=iy-1; if (iyn == -1)iyn=cellMax-1;
121 if ((lflag[ix][iyp] != 0) && (lflag[ix][iyn] != 0)) {
122 pflag[ix][iy] = 1;
123 lflag[ix][iy] = 0;
124 //g_poison->fill(ix,iy);
125 }
126 }
127 //for (iy=0; iy<cellMax; iy++) {if (pflag[ix][iy] != 0) lflag[ix][iy] = 0;}
128 }
129 } //FIXME (optimize ???)
130
131 float csmax_4 = MdcxParameters::csmax4;
132 float csmax_3 = MdcxParameters::csmax3;
133 int lastsl= gm->nSuper();//lastSLayNum;
134
135 // loop superlayer
136 for (sl = 0; sl < lastsl; sl++) {
137 int fsl=4*sl;
138 int l0=fsl, l1=fsl+1, l2=fsl+2, l3=fsl+3;
139 int iprt = 0;
140 //initialize superlayer pointer in layArray[]
141 const MdcLayer *layArray[4];
142 const MdcSuperLayer* slayer = gm->SuperLayer(sl);
143 int nslay = slayer->nLayers();
144 for (int i = 0; i < nslay; i++) layArray[i] = slayer->layer(i);
145 if(3 == m_debug){
146 cout<<"slayer No. "<<slayer->index()<<endl;//yzhang debug
147 }
148 //reference point (2,1)
149 cellMax = layArray[1]->nWires();
150
151 // loop over cells
152 for (int cellIndex=0; cellIndex<cellMax ; cellIndex++) {
153 int* w = wireGroups[sl][cellIndex];
154 unsigned int sig_mark = 0;
155 for (int ilayer = l0; ilayer <= l3; ilayer++) {
156 for (int iwire = 3; iwire >= 0; iwire--) {
157 if (lflag[ilayer][w[4*(ilayer-l0)+iwire]]) {
158 sig_mark |= 0x1;
159 }
160 sig_mark <<= 1;
161 }
162 }
163 sig_mark >>= 1;
164
165 int goodSegNo = 0;
166 int nPat = m_segPat.patt4_size;
167 int iPat = (sig_mark & 0x0200) ? 0 : 11;
168 for ( ; iPat < nPat; iPat++) {
169 int pat = m_segPat.patt4[iPat];
170 if ((pat & sig_mark) == pat) {
171 if(3 == m_debug) {
172 cout<< "pat " << std::hex << pat << std::dec << " with wire";
173 for (int tmpi = 0; tmpi < 4; tmpi++) {
174 cout<<"("<<l0+tmpi<<","<<w[4*tmpi+m_segPat.wirePat4[iPat][tmpi]-1]<<")";
175 }
176 cout << endl;
177 }
178 int w0 = lflag[l0][w[0 +m_segPat.wirePat4[iPat][0]-1]] - 1;
179 int w1 = lflag[l1][w[4 +m_segPat.wirePat4[iPat][1]-1]] - 1;
180 int w2 = lflag[l2][w[8 +m_segPat.wirePat4[iPat][2]-1]] - 1;
181 int w3 = lflag[l3][w[12+m_segPat.wirePat4[iPat][3]-1]] - 1;
182 int tw[4] = {w0, w1, w2, w3};
183
184 int namb = m_segPat.ambPat4_size[iPat];
185 for (int iamb = 0; iamb < namb; iamb++) {
186 int amb = m_segPat.ambigPatt4[iPat][iamb];
187 fithel = trial(w0, w1, w2, w3, amb);
188 if(3 == m_debug){
189 cout<<"chisq "<<fithel.Chisq()
190 <<" <? csmax4 "<<csmax_4<< endl;
191 if (fithel.Chisq() < csmax_4) {
192 cout<<"Accept this seg"<<endl;
193 }else{
194 cout<<"DROP this seg"<<endl;
195 }
196 }
197 if(g_csmax4) g_csmax4->fill(fithel.Chisq());
198 if (fithel.Chisq() < csmax_4) {
199 if (iprt) printseg(fithel, pat,amb);
200 appendseg(fithel, pat, amb);
201 goodSegNo++;
202 }
203 }
204 }
205 }
206 if (goodSegNo != 0) continue;
207 nPat = m_segPat.patt3_size;
208 iPat = (sig_mark & 0x0200) ? 0 : 14;
209 for ( ; iPat < nPat; iPat++) {
210 if (iPat > nPat-3) {
211 if ((iPat == nPat-2) && (sig_mark&0x2121 == 0x2121)) continue;
212 if ((iPat == nPat-1) && (sig_mark&0x2122 == 0x2122)) continue;
213 }
214 int pat = m_segPat.patt3[iPat];
215 if ((pat & sig_mark) == pat) {
216 if(3 == m_debug) {
217 cout<<"MdcxFindSegs: in pat "<<std::hex<<pat<<std::dec<<" with wire";
218 for (int tmpi = 0; tmpi < 4; tmpi++) {
219 if (m_segPat.wirePat3[iPat][tmpi] == 0) continue;
220 cout<< " (" << l0+tmpi << "," << w[4*tmpi+m_segPat.wirePat3[iPat][tmpi]-1] << ")";
221 }
222 cout<< endl;
223 }
224 int wn[3];
225 for (int iw = 0, iwp = 0; iwp < 4; iwp++) {
226 int wireNo = m_segPat.wirePat3[iPat][iwp];
227 if( wireNo == 0 ) continue;
228 wn[iw++] = lflag[l0+iwp][w[4*iwp+wireNo-1]] - 1;
229 }
230
231 int namb = m_segPat.ambPat3_size[iPat];
232 for (int iamb = 0; iamb < namb; iamb++) {
233 int amb = m_segPat.ambigPatt3[iPat][iamb];
234 fithel = trial(wn[0], wn[1], wn[2], amb);//FIXME
235 if(3 == m_debug){
236 cout<<"chisq "<<fithel.Chisq()
237 <<" <? csmax3 "<<csmax_3<< endl;
238 if (fithel.Chisq() < csmax_3) {
239 cout<<"Accept this seg"<<endl;
240 }else{
241 cout<<"DROP this seg"<<endl;
242 }
243 }
244 if(g_csmax3) g_csmax3->fill(fithel.Chisq());
245 if (fithel.Chisq() < csmax_3) {
246 if (iprt) printseg(fithel, pat, amb);
247 appendseg(fithel, pat ,amb);
248 }
249 }
250 }
251 }//end of nPat3
252 }
253 }
254
255 if(3 == m_debug){
256 cout << "MdcxFindSegs found " << MdcxSeglist.length() << " segs" << endl;
257 }
258 return;
259}
260
261void MdcxFindSegs::print(ostream &o, int pmax)const {
262 int mcheck=pmax; if (MdcxSeglist.length() < pmax)mcheck=MdcxSeglist.length();
263 o << " First " << mcheck << " Drift Chamber Segs:" << endl;
264 for(int i=0; i<mcheck; i++){
265 o << " Superlayer # " << MdcxSeglist[i]->SuperLayer();
266 o << " # of hits " << MdcxSeglist[i]->Nhits() << endl;
267 }
268} // end of print
269
270MdcxFittedHel MdcxFindSegs::trial(int i1, int i2, int i3, int i4, int amb) {
272 MdcxHit* t1 = dchitlist[i1]; MdcxHit* t2 = dchitlist[i2];
273 MdcxHit* t3 = dchitlist[i3]; MdcxHit* t4 = dchitlist[i4];
274 seg.append(t1); seg.append(t2); seg.append(t3); seg.append(t4);
275 double dx, dy, d0;
276 if (amb==0){dx=t4->xneg()-t1->xneg(); dy=t4->yneg()-t1->yneg(); d0=-t1->d();}
277 if (amb==1){dx=t4->xneg()-t1->xpos(); dy=t4->yneg()-t1->ypos(); d0= t1->d();}
278 if (amb==2){dx=t4->xpos()-t1->xneg(); dy=t4->ypos()-t1->yneg(); d0=-t1->d();}
279 if (amb==3){dx=t4->xpos()-t1->xpos(); dy=t4->ypos()-t1->ypos(); d0= t1->d();}
280 double phi0 = atan2(dy,dx); dx=t1->x(); dy=t1->y();
281 MdcxHel hel(d0,phi0,0.0,0.0,0.0,0.0,111,0,dx,dy);
282 hel.SetTurnFlag(1);
283 MdcxFittedHel temp(seg, hel, 1.0);
284 if (3 == m_debug) {
285 cout<<"trial4 amb "<<amb
286 << ": phi0 " << phi0 << " d0 " << d0
287 << " dx " << dx << " dy " << dy << endl;
288 //temp.FitPrint();
289 }
290 return temp;
291}
292
293MdcxFittedHel MdcxFindSegs::trial(int i1, int i2, int i3, int amb) {
295 MdcxHit* t1 = dchitlist[i1];
296 MdcxHit* t2 = dchitlist[i2];
297 MdcxHit* t3 = dchitlist[i3];
298 seg.append(t1); seg.append(t2); seg.append(t3);
299 double dx,dy,d0;
300 if (amb==0){dx=t3->xneg()-t1->xneg(); dy=t3->yneg()-t1->yneg(); d0=-t1->d();}
301 if (amb==1){dx=t3->xneg()-t1->xpos(); dy=t3->yneg()-t1->ypos(); d0= t1->d();}
302 if (amb==2){dx=t3->xpos()-t1->xneg(); dy=t3->ypos()-t1->yneg(); d0=-t1->d();}
303 if (amb==3){dx=t3->xpos()-t1->xpos(); dy=t3->ypos()-t1->ypos(); d0= t1->d();}
304
305 double phi0 = atan2(dy,dx); dx=t1->x(); dy=t1->y();
306 MdcxHel hel(d0,phi0,0.0,0.0,0.0,0.0,111,0,dx,dy);
307 hel.SetTurnFlag(1);
308 MdcxFittedHel temp(seg,hel,1.0);
309
310 if (3 == m_debug) {
311 cout<<"trial3 amb "<<amb
312 << ": phi0 " << phi0 << " d0 " << d0
313 << " dx " << dx << " dy " << dy << endl;
314 }
315 return temp;
316}
317
318void MdcxFindSegs::appendseg(MdcxFittedHel& fithel,int pat,int amb){
319 MdcxSeg *tempseg = new MdcxSeg(fithel, pat, amb);
320 MdcxSeglist.append(tempseg);
321}
322
323void MdcxFindSegs::printseg(MdcxFittedHel& fithel, int pat, int amb, int subtry){
324 cout << "Seg: pat " << pat << " amb " << amb << " subtry " << subtry
325 << " sl " << sl << " fail "
326 << fithel.Fail() << " chi2 " << fithel.Chisq() << endl;
327}
328
const int wireNo
double w
int mdcWrapWire(int wireIn, int nCell)
const MdcSuperLayer * SuperLayer(unsigned id) const
const MdcLayer * Layer(unsigned id) const
static MdcDetector * instance()
Definition: MdcDetector.cxx:21
MdcSWire * getWire(int wire) const
void appendseg(MdcxFittedHel &fithel, int pat, int amb)
MdcxFindSegs(MdcDetector *g, const HepAList< MdcxHit > &l, int m_debug=0)
virtual ~MdcxFindSegs()
void initWireGroups(void)
MdcxFittedHel trial(int i1, int i2, int i3, int i4, int amb)
void printseg(MdcxFittedHel &fithel, int pat, int amb, int subtry=0)
void print(std::ostream &o, int pmax=10) const
int Fail(float Probmin=0.0) const
float d(MdcxHel &hel) const
Definition: MdcxHit.cxx:160
#define ix(i)
Definition: minoreval.cpp:387