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