BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MdcxFindSegs.cxx
Go to the documentation of this file.
1#include "MdcxReco/MdcxFindSegs.h"
2#include "MdcGeom/BesAngle.h"
3#include "MdcGeom/MdcDetector.h"
4#include "MdcTrkRecon/mdcWrapWire.h"
5#include "MdcxReco/MdcxHit.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
26MdcxFindSegs::MdcxFindSegs( const HepAList<MdcxHit>& hitlist, int debug ) {
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++ )
44 { // loop over superLayers
45 const MdcLayer* layArray[4];
46 const MdcSuperLayer* slayer = gm->SuperLayer( sl );
47 int nslay = slayer->nLayers();
48 for ( int i = 0; i < nslay; i++ ) layArray[i] = slayer->layer( i );
49 int cellMax = layArray[1]->nWires();
50 // loop over cells
51 for ( int cellIndex = 0; cellIndex < cellMax; cellIndex++ )
52 {
53 int wireIndex[4];
54 if ( slayer->slayNum() > 4 )
55 {
56 wireIndex[0] = cellIndex;
57 wireIndex[1] = cellIndex + 1;
58 wireIndex[2] = cellIndex;
59 wireIndex[3] = cellIndex + 1;
60
61 wireIndex[1] = mdcWrapWire( wireIndex[1], layArray[1]->nWires() );
62 wireIndex[3] = mdcWrapWire( wireIndex[3], layArray[3]->nWires() );
63 }
64 else
65 {
66 wireIndex[1] = cellIndex + 1;
67 // take No.2 wire as referenc wire
68 double phi = layArray[1]->getWire( wireIndex[1] )->phiE();
69 for ( int i = 0; i < 4; i++ )
70 {
71 if ( i == 1 ) continue;
72 // change reference wire
73 if ( i == 3 ) phi = layArray[2]->getWire( wireIndex[2] )->phiE();
74 BesAngle tmp( phi - layArray[i]->phiEPOffset() ); // yzhang temp
75 if ( i == 3 )
76 {
77 wireIndex[i] =
78 ( tmp == 0 ) ? 1 : (int)ceil( layArray[i]->nWires() * tmp.rad() / twopi );
79 }
80 else
81 {
82 wireIndex[i] =
83 ( tmp == 0 ) ? -1 : (int)floor( layArray[i]->nWires() * tmp.rad() / twopi );
84 }
85 wireIndex[i] = mdcWrapWire( wireIndex[i], layArray[i]->nWires() );
86 }
87 }
88 // set wireGroups
89 int* w = wireGroups[sl][cellIndex];
90 for ( int i = 0; i < 4; i++ )
91 {
92 for ( int j = 0; j < 4; j++ )
93 {
94 // wireGroups[sl][cellIndex][4*i+j] = wireIndex[i] + (j - 1);
95 w[4 * i + j] = mdcWrapWire( ( wireIndex[i] + j - 1 ), layArray[i]->nWires() );
96 }
97 }
98 } // end loop cells
99 } // end loop superLayers
100
101 // Set alreadyInit flag
102 alreadyInit = true;
103 return;
104}
105
107 // if(3 == m_debug) cout <<" Event contains "<<dchitlist.length()<<" MdcxHit "<<endl;
108 // for last super layer use 44
109 int lflag[44][288] = { { 0 } };
110 int pflag[44][288] = { { 0 } };
111 int ix, iy, iyp, iyn, cellMax;
112
113 // yzhang 2009-10-20 do not use poison
114 int ipoison = 0;
115 // number of last layer
116 int lastlay = gm->nLayer();
117
118 // tag hit's lfalg
119 int k = 0;
120 while ( dchitlist[k] )
121 {
122 MdcxHit* temp = dchitlist[k];
123 k++;
124 lflag[temp->Layer()][temp->WireNo()] = k;
125 }
126
127 // try poisoning
128 if ( ipoison )
129 {
130 for ( ix = 0; ix < lastlay; ix++ )
131 {
132 cellMax = gm->Layer( ix )->nWires();
133 // if have hits in left and right , poison this cell
134 for ( iy = 0; iy < cellMax; iy++ )
135 {
136 iyp = iy + 1;
137 if ( iyp == cellMax ) iyp = 0;
138 iyn = iy - 1;
139 if ( iyn == -1 ) iyn = cellMax - 1;
140 if ( ( lflag[ix][iyp] != 0 ) && ( lflag[ix][iyn] != 0 ) )
141 {
142 pflag[ix][iy] = 1;
143 lflag[ix][iy] = 0;
144 // g_poison->fill(ix,iy);
145 }
146 }
147 // for (iy=0; iy<cellMax; iy++) {if (pflag[ix][iy] != 0) lflag[ix][iy] = 0;}
148 }
149 } // FIXME (optimize ???)
150
151 float csmax_4 = MdcxParameters::csmax4;
152 float csmax_3 = MdcxParameters::csmax3;
153 int lastsl = gm->nSuper(); // lastSLayNum;
154
155 // loop superlayer
156 for ( sl = 0; sl < lastsl; sl++ )
157 {
158 int fsl = 4 * sl;
159 int l0 = fsl, l1 = fsl + 1, l2 = fsl + 2, l3 = fsl + 3;
160 int iprt = 0;
161 // initialize superlayer pointer in layArray[]
162 const MdcLayer* layArray[4];
163 const MdcSuperLayer* slayer = gm->SuperLayer( sl );
164 int nslay = slayer->nLayers();
165 for ( int i = 0; i < nslay; i++ ) layArray[i] = slayer->layer( i );
166 if ( 3 == m_debug )
167 {
168 cout << "slayer No. " << slayer->index() << endl; // yzhang debug
169 }
170 // reference point (2,1)
171 cellMax = layArray[1]->nWires();
172
173 // loop over cells
174 for ( int cellIndex = 0; cellIndex < cellMax; cellIndex++ )
175 {
176 int* w = wireGroups[sl][cellIndex];
177 unsigned int sig_mark = 0;
178 for ( int ilayer = l0; ilayer <= l3; ilayer++ )
179 {
180 for ( int iwire = 3; iwire >= 0; iwire-- )
181 {
182 if ( lflag[ilayer][w[4 * ( ilayer - l0 ) + iwire]] ) { sig_mark |= 0x1; }
183 sig_mark <<= 1;
184 }
185 }
186 sig_mark >>= 1;
187
188 int goodSegNo = 0;
189 int nPat = m_segPat.patt4_size;
190 int iPat = ( sig_mark & 0x0200 ) ? 0 : 11;
191 for ( ; iPat < nPat; iPat++ )
192 {
193 int pat = m_segPat.patt4[iPat];
194 if ( ( pat & sig_mark ) == pat )
195 {
196 if ( 3 == m_debug )
197 {
198 // cout << "pat " << std::hex << pat << std::dec << " with wire";
199 cout << "pat " << pat << " with wire";
200 for ( int tmpi = 0; tmpi < 4; tmpi++ )
201 {
202 cout << "(" << l0 + tmpi << ","
203 << w[4 * tmpi + m_segPat.wirePat4[iPat][tmpi] - 1] << ")";
204 }
205 cout << endl;
206 }
207 int w0 = lflag[l0][w[0 + m_segPat.wirePat4[iPat][0] - 1]] - 1;
208 int w1 = lflag[l1][w[4 + m_segPat.wirePat4[iPat][1] - 1]] - 1;
209 int w2 = lflag[l2][w[8 + m_segPat.wirePat4[iPat][2] - 1]] - 1;
210 int w3 = lflag[l3][w[12 + m_segPat.wirePat4[iPat][3] - 1]] - 1;
211 int tw[4] = { w0, w1, w2, w3 };
212
213 int namb = m_segPat.ambPat4_size[iPat];
214 for ( int iamb = 0; iamb < namb; iamb++ )
215 {
216 int amb = m_segPat.ambigPatt4[iPat][iamb];
217 fithel = trial( w0, w1, w2, w3, amb );
218 if ( 3 == m_debug )
219 {
220 cout << "chisq " << fithel.Chisq() << " <? csmax4 " << csmax_4 << endl;
221 if ( fithel.Chisq() < csmax_4 ) { cout << "Accept this seg" << endl; }
222 else { cout << "DROP this seg" << endl; }
223 }
224 if ( g_csmax4 ) g_csmax4->fill( fithel.Chisq() );
225 if ( fithel.Chisq() < csmax_4 )
226 {
227 if ( iprt ) printseg( fithel, pat, amb );
228 appendseg( fithel, pat, amb );
229 goodSegNo++;
230 }
231 }
232 }
233 }
234 if ( goodSegNo != 0 ) continue;
235 nPat = m_segPat.patt3_size;
236 iPat = ( sig_mark & 0x0200 ) ? 0 : 14;
237 for ( ; iPat < nPat; iPat++ )
238 {
239 if ( iPat > nPat - 3 )
240 {
241 if ( ( iPat == nPat - 2 ) && ( sig_mark & 0x2121 == 0x2121 ) ) continue;
242 if ( ( iPat == nPat - 1 ) && ( sig_mark & 0x2122 == 0x2122 ) ) continue;
243 }
244 int pat = m_segPat.patt3[iPat];
245 if ( ( pat & sig_mark ) == pat )
246 {
247 if ( 3 == m_debug )
248 {
249 // cout << "MdcxFindSegs: in pat " << std::hex << pat << std::dec << " with wire";
250 cout << "MdcxFindSegs: in pat " << pat << " with wire";
251 for ( int tmpi = 0; tmpi < 4; tmpi++ )
252 {
253 if ( m_segPat.wirePat3[iPat][tmpi] == 0 ) continue;
254 cout << " (" << l0 + tmpi << ","
255 << w[4 * tmpi + m_segPat.wirePat3[iPat][tmpi] - 1] << ")";
256 }
257 cout << endl;
258 }
259 int wn[3];
260 for ( int iw = 0, iwp = 0; iwp < 4; iwp++ )
261 {
262 int wireNo = m_segPat.wirePat3[iPat][iwp];
263 if ( wireNo == 0 ) continue;
264 wn[iw++] = lflag[l0 + iwp][w[4 * iwp + wireNo - 1]] - 1;
265 }
266
267 int namb = m_segPat.ambPat3_size[iPat];
268 for ( int iamb = 0; iamb < namb; iamb++ )
269 {
270 int amb = m_segPat.ambigPatt3[iPat][iamb];
271 fithel = trial( wn[0], wn[1], wn[2], amb ); // FIXME
272 if ( 3 == m_debug )
273 {
274 cout << "chisq " << fithel.Chisq() << " <? csmax3 " << csmax_3 << endl;
275 if ( fithel.Chisq() < csmax_3 ) { cout << "Accept this seg" << endl; }
276 else { cout << "DROP this seg" << endl; }
277 }
278 if ( g_csmax3 ) g_csmax3->fill( fithel.Chisq() );
279 if ( fithel.Chisq() < csmax_3 )
280 {
281 if ( iprt ) printseg( fithel, pat, amb );
282 appendseg( fithel, pat, amb );
283 }
284 }
285 }
286 } // end of nPat3
287 }
288 }
289
290 if ( 3 == m_debug )
291 { cout << "MdcxFindSegs found " << MdcxSeglist.length() << " segs" << endl; }
292 return;
293}
294
295void MdcxFindSegs::print( ostream& o, int pmax ) const {
296 int mcheck = pmax;
297 if ( MdcxSeglist.length() < pmax ) mcheck = MdcxSeglist.length();
298 o << " First " << mcheck << " Drift Chamber Segs:" << endl;
299 for ( int i = 0; i < mcheck; i++ )
300 {
301 o << " Superlayer # " << MdcxSeglist[i]->SuperLayer();
302 o << " # of hits " << MdcxSeglist[i]->Nhits() << endl;
303 }
304} // end of print
305
306MdcxFittedHel MdcxFindSegs::trial( int i1, int i2, int i3, int i4, int amb ) {
308 MdcxHit* t1 = dchitlist[i1];
309 MdcxHit* t2 = dchitlist[i2];
310 MdcxHit* t3 = dchitlist[i3];
311 MdcxHit* t4 = dchitlist[i4];
312 seg.append( t1 );
313 seg.append( t2 );
314 seg.append( t3 );
315 seg.append( t4 );
316 double dx, dy, d0;
317 dx = dy = d0 = 0.;
318 if ( amb == 0 )
319 {
320 dx = t4->xneg() - t1->xneg();
321 dy = t4->yneg() - t1->yneg();
322 d0 = -t1->d();
323 }
324 if ( amb == 1 )
325 {
326 dx = t4->xneg() - t1->xpos();
327 dy = t4->yneg() - t1->ypos();
328 d0 = t1->d();
329 }
330 if ( amb == 2 )
331 {
332 dx = t4->xpos() - t1->xneg();
333 dy = t4->ypos() - t1->yneg();
334 d0 = -t1->d();
335 }
336 if ( amb == 3 )
337 {
338 dx = t4->xpos() - t1->xpos();
339 dy = t4->ypos() - t1->ypos();
340 d0 = t1->d();
341 }
342 double phi0 = atan2( dy, dx );
343 dx = t1->x();
344 dy = t1->y();
345 MdcxHel hel( d0, phi0, 0.0, 0.0, 0.0, 0.0, 111, 0, dx, dy );
346 hel.SetTurnFlag( 1 );
347 MdcxFittedHel temp( seg, hel, 1.0 );
348 if ( 3 == m_debug )
349 {
350 cout << "trial4 amb " << amb << ": phi0 " << phi0 << " d0 " << d0 << " dx " << dx << " dy "
351 << dy << endl;
352 // temp.FitPrint();
353 }
354 return temp;
355}
356
357MdcxFittedHel MdcxFindSegs::trial( int i1, int i2, int i3, int amb ) {
359 MdcxHit* t1 = dchitlist[i1];
360 MdcxHit* t2 = dchitlist[i2];
361 MdcxHit* t3 = dchitlist[i3];
362 seg.append( t1 );
363 seg.append( t2 );
364 seg.append( t3 );
365 double dx = 0., dy = 0., d0 = 0.;// add initialize 2025-05-15
366 if ( amb == 0 )
367 {
368 dx = t3->xneg() - t1->xneg();
369 dy = t3->yneg() - t1->yneg();
370 d0 = -t1->d();
371 }
372 if ( amb == 1 )
373 {
374 dx = t3->xneg() - t1->xpos();
375 dy = t3->yneg() - t1->ypos();
376 d0 = t1->d();
377 }
378 if ( amb == 2 )
379 {
380 dx = t3->xpos() - t1->xneg();
381 dy = t3->ypos() - t1->yneg();
382 d0 = -t1->d();
383 }
384 if ( amb == 3 )
385 {
386 dx = t3->xpos() - t1->xpos();
387 dy = t3->ypos() - t1->ypos();
388 d0 = t1->d();
389 }
390
391 double phi0 = atan2( dy, dx );
392 dx = t1->x();
393 dy = t1->y();
394 MdcxHel hel( d0, phi0, 0.0, 0.0, 0.0, 0.0, 111, 0, dx, dy );
395 hel.SetTurnFlag( 1 );
396 MdcxFittedHel temp( seg, hel, 1.0 );
397
398 if ( 3 == m_debug )
399 {
400 cout << "trial3 amb " << amb << ": phi0 " << phi0 << " d0 " << d0 << " dx " << dx << " dy "
401 << dy << endl;
402 }
403 return temp;
404}
405
406void MdcxFindSegs::appendseg( MdcxFittedHel& fithel, int pat, int amb ) {
407 MdcxSeg* tempseg = new MdcxSeg( fithel, pat, amb );
408 MdcxSeglist.append( tempseg );
409}
410
411void MdcxFindSegs::printseg( MdcxFittedHel& fithel, int pat, int amb, int subtry ) {
412 cout << "Seg: pat " << pat << " amb " << amb << " subtry " << subtry << " sl " << sl
413 << " fail " << fithel.Fail() << " chi2 " << fithel.Chisq() << endl;
414}
const int wireNo
double w
AIDA::IHistogram1D * g_csmax3
AIDA::IHistogram1D * g_csmax4
static MdcDetector * instance()
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
float d(MdcxHel &hel) const
Definition MdcxHit.cxx:155
#define ix(i)