BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MdcSegList.cxx
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2// File and Version Information:
3// $Id: MdcSegList.cxx,v 1.10 2012/10/13 09:39:26 zhangy Exp $
4//
5// Description:
6//
7//
8// Environment:
9// Software developed for the BaBar Detector at the SLAC B-Factory.
10//
11// Authors:
12// Zhang Yao(zhangyao@ihep.ac.cn) Migrate to BESIII
13//
14// Copyright (C) 1996 The Board of Trustees of
15// The Leland Stanford Junior University. All Rights Reserved.
16//------------------------------------------------------------------------
17#include "MdcTrkRecon/MdcSegList.h"
18#include "MdcData/MdcHit.h"
19#include "MdcData/MdcHitUse.h"
20#include "MdcGeom/BesAngle.h"
21#include "MdcGeom/Constants.h"
22#include "MdcGeom/MdcDetector.h"
23#include "MdcGeom/MdcSuperLayer.h"
24#include "MdcTrkRecon/GmsList.h"
25#include "MdcTrkRecon/MdcSeg.h"
26#include "MdcTrkRecon/mdcWrapWire.h"
27// yzhang hist cut
28#include "AIDA/IHistogram1D.h"
29extern AIDA::IHistogram1D* g_phiDiff;
30extern AIDA::IHistogram1D* g_slopeDiff;
31// zhangy hist cut
32const double _twopi = Constants::twoPi;
33
34/**************************************************************************/
35MdcSegList::MdcSegList( int nSlayer, const MdcSegParams segP ) {
36 /**************************************************************************/
37 segParam = segP; // bit-wise copy
38 MdcSeg::setParam( &segParam );
39 zeroSeed();
40 segList = new GmsList[nSlayer];
41 _nsupers = nSlayer;
42}
43
44/**************************************************************************/
46 /**************************************************************************/
47 delete[] segList;
48}
49
50/**************************************************************************/
51void MdcSegList::zeroSeed() {
52 /**************************************************************************/
53 seedSeg[0] = seedSeg[1] = seedSeg[2] = 0;
54 seedSlay[0] = seedSlay[1] = seedSlay[2] = 0;
55}
56/**************************************************************************/
58 /**************************************************************************/
59
60 for ( int iview = -1; iview <= 1; iview++ )
61 {
62 int viewIndex = iview + 1;
63 /*
64 if(0 == iview){
65 seedSlay[viewIndex] = gm->firstSlayInView(iview);
66 }else{
67 seedSlay[viewIndex] = gm->lastSlayInView(iview);
68 }
69 */
70 seedSlay[viewIndex] = gm->lastSlayInView( iview );
71 if ( seedSlay[viewIndex] != 0 )
72 seedSeg[viewIndex] = (MdcSeg*)oneList( seedSlay[viewIndex]->index() )->first();
73 }
74}
75/**************************************************************************/
76void MdcSegList::plot() const {
77 /**************************************************************************/
78
79 MdcSeg* aSeg;
80 for ( int i = 0; i < _nsupers; i++ )
81 {
82 std::cout << " ---------------MdcSeg of Slayer " << i << "-------------" << std::endl;
83
84 aSeg = (MdcSeg*)segList[i].first();
85 while ( aSeg != 0 )
86 {
87 aSeg->plotSegAll();
88 std::cout << endl;
89 aSeg = (MdcSeg*)aSeg->next();
90 }
91 }
92}
93
94/****************************************************************************/
96 /****************************************************************************/
97
98 zeroSeed();
99 MdcSeg *aSeg, *bSeg;
100 for ( int i = 0; i < _nsupers; i++ )
101 {
102 aSeg = (MdcSeg*)segList[i].first();
103 while ( aSeg != 0 )
104 {
105 bSeg = (MdcSeg*)aSeg->next();
106 segList[i].remove( aSeg );
107 delete aSeg;
108 aSeg = bSeg;
109 }
110 }
111 return;
112}
113
114//****************************************************************************
115void MdcSegList::sortByPhi() {
116 //****************************************************************************
117 // Sort the list of segments by phi. Insertion sort, good only for small n
118
119 for ( int isuper = 0; isuper < _nsupers; isuper++ )
120 {
121
122 if ( segList[isuper].first() == 0 ) continue;
123 MdcSeg* aseg = (MdcSeg*)segList[isuper].first()->next();
124 for ( int j = 1; j < (int)segList[isuper].count(); j++ )
125 {
126 double phiSeg = aseg->phi();
127 MdcSeg* nextseg = (MdcSeg*)aseg->next();
128
129 MdcSeg* bseg = (MdcSeg*)aseg->prev();
130 if ( phiSeg < bseg->phi() )
131 {
132 while ( bseg != 0 && phiSeg < bseg->phi() ) { bseg = (MdcSeg*)bseg->prev(); }
133 segList[isuper].moveAfter( aseg, bseg ); // insert aseg after bseg
134 }
135
136 aseg = nextseg;
137 }
138 }
139 return;
140}
141
142//****************************************************************************
143MdcSeg* MdcSegList::getSeed( int iview, int goodOnly ) {
144 //***************************************************************************
145
146 // Find a suitable segment for starting a track. If goodOnly=1, look for
147 // isolated, unambiguous segments; unambiguous is
148 // defined as being the only segment within a band in phiseg (typically
149 // 2 cell widths wide), or as differing only slightly from nearby
150 // segments in phi and slope. When these have been exhausted, take any
151 // unused segment. Return pointer to seed seg, or 0 if none found.
152
153 int viewIndex = iview + 1;
154 if ( seedSlay[viewIndex] == 0 ) return 0;
155 while ( 1 )
156 {
157 // if(seedSeg[viewIndex] != 0){
158 // seedSeg[viewIndex]->plotSeg(0,0);//yzhang debug
159 // std::cout<<__FILE__<<" "<<__LINE__<<" goodOnly "<<goodOnly<< std::endl;
160 //}
161 if ( seedSeg[viewIndex] == 0 )
162 {
163 // seedSlay[viewIndex] = seedSlay[viewIndex]->nextInView();//yzhang temp
164 seedSlay[viewIndex] = seedSlay[viewIndex]->prevInView(); // yzhang del
165 if ( seedSlay[viewIndex] == 0 ) return 0;
166 seedSeg[viewIndex] = (MdcSeg*)oneList( seedSlay[viewIndex]->index() )->first();
167 } // We have a segment; is it a viable seed?
168 else if ( seedSeg[viewIndex]->segUsed() )
169 {
170 // std::cout<<__FILE__<<" segUsed " << std::endl;
171 seedSeg[viewIndex] = (MdcSeg*)seedSeg[viewIndex]->next();
172 }
173 else if ( seedSeg[viewIndex]->segAmbig() && goodOnly )
174 {
175 // std::cout<<__FILE__<<" segAmbig && goodOnly" << std::endl;
176 seedSeg[viewIndex] = (MdcSeg*)seedSeg[viewIndex]->next();
177 // yzhang delete 09-10-10
178 // delete (!seedSeg[viewIndex]->segAmbig() && !goodOnly)
179 } // yzhang add 09-09-28
180 else if ( !seedSeg[viewIndex]->segFull() && goodOnly )
181 {
182 // std::cout<<__FILE__<<" segFull && goodOnly" << std::endl;
183 seedSeg[viewIndex] = (MdcSeg*)seedSeg[viewIndex]->next();
184 // zhangy add
185 }
186 else
187 {
188 // Reject segments with a lot of used hits
189 int nused = 0;
190 int navail = seedSeg[viewIndex]->nHit();
191 for ( int ihit = 0; ihit < seedSeg[viewIndex]->nHit(); ihit++ )
192 {
193 MdcHitUse* ahit = seedSeg[viewIndex]->hit( ihit );
194 if ( ahit->mdcHit()->usedHit() ) nused++;
195 }
196 // Minimum 2 unused hits (but allow special-purpose segs w/ 0 hits)
197 if ( navail - nused < 2 && navail > 0 )
198 { seedSeg[viewIndex] = (MdcSeg*)seedSeg[viewIndex]->next(); }
199 else
200 {
201 // std::cout<<__FILE__<<" Reject segments with a lot of used hits" << std::endl;
202 break;
203 }
204 }
205 /*
206 if(seedSeg[viewIndex]!=0){
207 seedSeg[viewIndex]->plotSeg();
208 std::cout<< __FILE__ << " " << __LINE__ << " goodOnly "
209 <<goodOnly<<" viewIndex "<<viewIndex<< " full "<<seedSeg[viewIndex]->segFull()
210 <<std::endl;
211 }
212 */
213 }
214
215 // We have a seed
216 MdcSeg* theSeed = seedSeg[viewIndex];
217 seedSeg[viewIndex] = (MdcSeg*)seedSeg[viewIndex]->next();
218
219 return theSeed;
220}
221//****************************************************************************
222void MdcSegList::tagAmbig() {
223 //***************************************************************************
224 // Go through the list of segments and tag ones that will not make good
225 // seeds -- i.e. ones that overlap with each other
226
227 for ( int isuper = 0; isuper < _nsupers; isuper++ )
228 {
229 const GmsList* thisSegList = &segList[isuper];
230 if ( thisSegList->count() < 2 ) return;
231
232 // Bunch some declarations:
233 double width, diff;
234 double thisphi, nextphi;
235 double nisocell = 2;
236 // width = cell width in phi for this superlayer
237 // diff = width of band about the track = nisocell * width
238
239 // Calculate road width for this slayer.
240 const MdcLayer* aLayer = ( (MdcSeg*)thisSegList->first() )->superlayer()->firstLayer();
241 width = _twopi / aLayer->nWires();
242 diff = nisocell * width;
243
244 for ( MdcSeg* startSeg = (MdcSeg*)thisSegList->first(); startSeg != 0;
245 startSeg = (MdcSeg*)startSeg->next() )
246 {
247 MdcSeg* nextSeg = (MdcSeg*)startSeg->next();
248 if ( nextSeg == 0 ) nextSeg = (MdcSeg*)thisSegList->first();
249
250 thisphi = startSeg->phi();
251 nextphi = nextSeg->phi();
252 if ( nextphi < thisphi ) nextphi += _twopi;
253
254 if ( nextphi - thisphi < diff )
255 {
256 startSeg->setAmbig();
257 nextSeg->setAmbig();
258 }
259
260 } // end loop over segments
261 } // end loop over superlayers
262
263 return;
264}
265//**************************************************************************
266// void MdcSegList::newParams(const MdcSegParams &segPar) {
267//**************************************************************************
268// segParam = segPar;
269//}
270/****************************************************************************/
272 /****************************************************************************/
273 segList[aSeg->superlayer()->index()].append( aSeg );
274}
275/****************************************************************************/
276void MdcSegList::massageSegs() {
277 /****************************************************************************/
278 sortByPhi();
279 // Delete duplicates only if there are too many segments
280 bool drop = ( segPar()->dropDups && count() > 200 ); // yzhang FIXME
281 deleteDups( drop );
282 tagAmbig();
283}
284/****************************************************************************/
285void MdcSegList::deleteDups( bool ldrop ) {
286 /****************************************************************************/
287 // Removes segments that have almost identical parameters. A bit risky,
288 // perhaps, but what kind of life would you have if you never took
289 // any risks?
290
291 // On second thought, offer the option of just marking them as used.
292
293 // Takes pairs of segments, and if they have the same slope and phi
294 // (within cuts), selects better, using nHit and chisq. Note that 2
295 // identical segs may be separated in list by non-identical
296
297 bool printit = ( 5 == segPar()->lPrint );
298
299 if ( printit ) std::cout << " =======MdcSegList::deleteDups()===== " << std::endl;
300
301 for ( int isuper = 0; isuper < _nsupers; isuper++ )
302 {
303 if ( isuper == 10 ) continue; // yzhang add 2012-09-18
304 GmsList* thisSegList = &segList[isuper];
305 if ( thisSegList->count() < 2 ) continue;
306 if ( 5 == segPar()->lPrint || 13 == segPar()->lPrint )
307 { std::cout << endl << " -----slayer " << isuper << "-----" << std::endl; }
308
309 double thisphi, nextphi;
310 double slopediff = segPar()->slopeDiffDrop; // FIXME
311
312 // Calculate road width for this slayer.
313 const MdcLayer* aLayer = ( (MdcSeg*)thisSegList->first() )->superlayer()->firstLayer();
314 double width = _twopi / aLayer->nWires();
315 double phidiff = segPar()->phiDiffDropMult * width; // FIXME
316
317 int lWrapped = 0;
318
319 MdcSeg* startSeg = (MdcSeg*)thisSegList->first();
320 while ( startSeg != 0 )
321 {
322 if ( thisSegList->count() < 2 ) break;
323 if ( lWrapped ) break;
324 thisphi = startSeg->phi();
325 MdcSeg* nextSeg = startSeg;
326 if ( 5 == segPar()->lPrint || 13 == segPar()->lPrint )
327 {
328 startSeg->plotSeg();
329 std::cout << std::endl;
330 }
331
332 while ( 1 )
333 { // loop over nextSeg
334 nextSeg = (MdcSeg*)nextSeg->next();
335 if ( nextSeg == 0 )
336 {
337 nextSeg = (MdcSeg*)thisSegList->first();
338 if ( lWrapped == 1 ) break; // cluster covers 2pi; run, screaming
339 lWrapped = 1;
340 }
341 if ( nextSeg == startSeg ) break; // yzhang add 2011-06-16
342 nextphi = nextSeg->phi();
343
344 if ( nextphi < thisphi ) nextphi += _twopi;
345
346 if ( printit )
347 {
348 std::cout << std::endl << "----start ";
349 startSeg->plotSeg();
350 std::cout << std::endl;
351 std::cout << " next ";
352 nextSeg->plotSeg();
353 std::cout << std::endl;
354 std::cout << " phi diff " << nextphi - thisphi << " cut phidiff " << phidiff;
355 std::cout << " slope diff " << nextSeg->slope() - startSeg->slope()
356 << " cut slopdiff " << slopediff << std::endl;
357 }
358
359 if ( g_phiDiff )
360 { g_phiDiff->fill( ( nextphi - thisphi ) / width ); } // yzhang hist cut
361 if ( g_slopeDiff )
362 { g_slopeDiff->fill( nextSeg->slope() - startSeg->slope() ); } // yzhang hist cut
363
364 if ( nextphi - thisphi > phidiff )
365 {
366 if ( printit ) { std::cout << " --not same phi" << std::endl; }
367 // Done with this segment (aSeg)
368 startSeg = (MdcSeg*)startSeg->next();
369 break;
370 }
371 else if ( fabs( nextSeg->slope() - startSeg->slope() ) > slopediff )
372 {
373 if ( printit )
374 {
375 std::cout << " --attached, but not identical -- move on to next nextSeg "
376 << std::endl;
377 }
378 // attached, but not identical -- move on to next nextSeg
379 continue;
380 }
381 else
382 {
383 // identical; choose better
384 int firstBetter = 0;
385 assert( startSeg->nHit() > 2 );
386 double chiFirst = startSeg->chisq() / ( startSeg->nHit() - 2 );
387 assert( nextSeg->nHit() > 2 );
388 double chiNext = nextSeg->chisq() / ( nextSeg->nHit() - 2 );
389 double theDiff;
390 int cdiff = (int)nextSeg->nHit() - (int)startSeg->nHit();
391 theDiff = ( chiFirst - chiNext ) + 2. * ( cdiff );
392 if ( theDiff < 0. ) firstBetter = 1;
393 if ( printit )
394 {
395 std::cout << " --identical; choose better " << std::endl;
396 std::cout << " chi start " << chiFirst << " chi next " << chiNext << " cdiff "
397 << cdiff << std::endl;
398 }
399
400 if ( firstBetter )
401 {
402 if ( printit ) { std::cout << " startSeg better,"; }
403 MdcSeg* tempSeg = nextSeg;
404 nextSeg = (MdcSeg*)nextSeg->next();
405 if ( ldrop )
406 {
407 if ( printit ) { std::cout << " delete nextSeg" << std::endl; }
408 thisSegList->remove( tempSeg );
409 delete tempSeg;
410 }
411 else
412 {
413 if ( printit ) { std::cout << " nextSeg->setUsed(true) " << std::endl; }
414 tempSeg->setUsed();
415 }
416 if ( nextSeg == 0 )
417 {
418 nextSeg = (MdcSeg*)thisSegList->first();
419 if ( lWrapped == 1 ) break; // cluster covers 2pi; run, screaming
420 lWrapped = 1;
421 }
422 }
423 else
424 {
425 if ( printit ) { std::cout << " nextSeg better,"; }
426 MdcSeg* tempSeg = startSeg;
427 startSeg = (MdcSeg*)startSeg->next();
428 if ( ldrop )
429 {
430 if ( printit ) { std::cout << " delete startSeg" << std::endl; }
431 thisSegList->remove( tempSeg );
432 delete tempSeg;
433 }
434 else
435 {
436 if ( printit ) { std::cout << " startSeg->setUsed(true) " << std::endl; }
437 tempSeg->setUsed();
438 }
439 break;
440 }
441
442 } // end if identical
443 } // end loop over nextSeg
444
445 } // end primary loop over segments
446 } // end loop over superlayers
447}
448
449void MdcSegList::setPlot( int lPlt ) { MdcSeg::segPar()->lPlot = lPlt; }
450
451//-------------------------------------------------------------------------
452const GmsList* MdcSegList::oneList( int slayIndex ) const {
453 //-------------------------------------------------------------------------
454 return &segList[slayIndex];
455}
456
457//-------------------------------------------------------------------------
458int MdcSegList::count() const {
459 //-------------------------------------------------------------------------
460 int cnt = 0;
461 for ( int i = 0; i < _nsupers; i++ ) cnt += segList[i].count();
462 return cnt;
463}
AIDA::IHistogram1D * g_phiDiff
AIDA::IHistogram1D * g_slopeDiff
const double _twopi
GmsList & remove(GmsListLink *)
Definition GmsList.cxx:104
const MdcSuperLayer * lastSlayInView(int iview) const
const MdcHit * mdcHit() const
Definition MdcHitUse.cxx:62
int count() const
void append(MdcSeg *aSeg)
MdcSegList(int nSupers, const MdcSegParams segParms)
const GmsList * oneList(int slayIndex) const
MdcSeg * getSeed(int iview, int goodOnly)
void resetSeed(const MdcDetector *gm)
void plot() const
void setPlot(int lPlt)
void destroySegs()
void plotSeg() const
Definition MdcSeg.cxx:198
int nHit() const
Definition MdcSeg.cxx:372
void append(MdcHitUse *)
Definition MdcSeg.cxx:359
void plotSegAll() const
Definition MdcSeg.cxx:157