CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTMeantimerPatternReco.cc
Go to the documentation of this file.
1 
8 /* This Class Header */
10 
11 /* Collaborating Class Header */
25 
26 /* C++ Headers */
27 #include <iterator>
28 using namespace std;
30 
31 /* ====================================================================== */
32 
33 
36 DTRecSegment2DBaseAlgo(pset), theAlgoName("DTMeantimerPatternReco")
37 {
38 
39  theMaxAllowedHits = pset.getParameter<unsigned int>("MaxAllowedHits"); // 100
40  theAlphaMaxTheta = pset.getParameter<double>("AlphaMaxTheta");// 0.1 ;
41  theAlphaMaxPhi = pset.getParameter<double>("AlphaMaxPhi");// 1.0 ;
42  theMaxT0 = pset.getParameter<double>("MaxT0");
43  theMinT0 = pset.getParameter<double>("MinT0");
44  theMaxChi2 = pset.getParameter<double>("MaxChi2");// 8.0 ;
45  debug = pset.getUntrackedParameter<bool>("debug");
46  theUpdator = new DTSegmentUpdator(pset);
47  theCleaner = new DTSegmentCleaner(pset);
48 }
49 
52  delete theUpdator;
53  delete theCleaner;
54 }
55 
56 /* Operations */
59  const std::vector<DTRecHit1DPair>& pairs){
60 
62  vector<DTHitPairForFit*> hitsForFit = initHits(sl, pairs);
63 
64  vector<DTSegmentCand*> candidates = buildSegments(sl, hitsForFit);
65 
66  vector<DTSegmentCand*>::const_iterator cand=candidates.begin();
67  while (cand<candidates.end()) {
68 
69  DTSLRecSegment2D *segment = (**cand);
70 
71  theUpdator->update(segment);
72 
73  if (debug)
74  cout<<"Reconstructed 2D segments "<<*segment<<endl;
75  result.push_back(segment);
76 
77  delete *(cand++); // delete the candidate!
78  }
79 
80  for (vector<DTHitPairForFit*>::iterator it = hitsForFit.begin(), ed = hitsForFit.end();
81  it != ed; ++it) delete *it;
82 
83  return result;
84 }
85 
87  // Get the DT Geometry
88  setup.get<MuonGeometryRecord>().get(theDTGeometry);
89  theUpdator->setES(setup);
90 }
91 
92 vector<DTHitPairForFit*>
94  const std::vector<DTRecHit1DPair>& hits){
95 
96  vector<DTHitPairForFit*> result;
97  for (vector<DTRecHit1DPair>::const_iterator hit=hits.begin();
98  hit!=hits.end(); ++hit) {
99  result.push_back(new DTHitPairForFit(*hit, *sl, theDTGeometry));
100  }
101  return result;
102 }
103 
104 vector<DTSegmentCand*>
106  const std::vector<DTHitPairForFit*>& hits){
107 
108  typedef vector<DTHitPairForFit*> hitCont;
109  typedef hitCont::const_iterator hitIter;
110  vector<DTSegmentCand*> result;
112 
113  if(debug) {
114  cout << "buildSegments: " << sl->id() << " nHits " << hits.size() << endl;
115  for (hitIter hit=hits.begin(); hit!=hits.end(); ++hit) cout << **hit<< " wire: " << (*hit)->id() << " DigiTime: " << (*hit)->digiTime() << endl;
116  }
117 
118  // 10-Mar-2004 SL
119  // put a protection against heavily populated chambers, for which the segment
120  // building could lead to infinite memory usage...
121  if (hits.size() > theMaxAllowedHits ) {
122  if(debug) {
123  cout << "Warning: this SuperLayer " << sl->id() << " has too many hits : "
124  << hits.size() << " max allowed is " << theMaxAllowedHits << endl;
125  cout << "Skipping segment reconstruction... " << endl;
126  }
127  return result;
128  }
129 
130  GlobalPoint IP;
131  float DAlphaMax;
132  if ((sl->id()).superlayer()==2) // Theta SL
133  DAlphaMax=theAlphaMaxTheta;
134  else // Phi SL
135  DAlphaMax=theAlphaMaxPhi;
136 
137  vector<AssPoint> usedHits;
138 
139  // get two hits in different layers and see if there are other hits
140  // compatible with them
141  for (hitCont::const_iterator firstHit=hits.begin(); firstHit!=hits.end();
142  ++firstHit) {
143  for (hitCont::const_reverse_iterator lastHit=hits.rbegin();
144  (*lastHit)!=(*firstHit); ++lastHit) {
145 
146  // a geometrical sensibility cut for the two hits
147  if (!geometryFilter((*firstHit)->id(),(*lastHit)->id())) continue;
148 
149  // create a set of hits for the fit (only the hits between the two selected ones)
150  hitCont hitsForFit;
151  for (hitCont::const_iterator tmpHit=firstHit+1; (*tmpHit)!=(*lastHit); tmpHit++)
152  if ((geometryFilter((*tmpHit)->id(),(*lastHit)->id()))
153  && (geometryFilter((*tmpHit)->id(),(*firstHit)->id()))) hitsForFit.push_back(*tmpHit);
154 
155  for (int firstLR=0; firstLR<2; ++firstLR) {
156  for (int lastLR=0; lastLR<2; ++lastLR) {
157 
158  // TODO move the global transformation in the DTHitPairForFit class
159  // when it will be moved I will able to remove the sl from the input parameter
160  GlobalPoint gposFirst=sl->toGlobal( (*firstHit)->localPosition(codes[firstLR]) );
161  GlobalPoint gposLast= sl->toGlobal( (*lastHit)->localPosition(codes[lastLR]) );
162  GlobalVector gvec=gposLast-gposFirst;
163  GlobalVector gvecIP=gposLast-IP;
164 
165  // difference in angle measured
166  float DAlpha=fabs(gvec.theta()-gvecIP.theta());
167  if (DAlpha>DAlphaMax) continue;
168 
169  if(debug) {
170  cout << "Selected hit pair:" << endl;
171  cout << " First " << *(*firstHit) << " Layer Id: " << (*firstHit)->id().layerId() << " Side: " << firstLR << " DigiTime: " << (*firstHit)->digiTime() << endl;
172  cout << " Last " << *(*lastHit) << " Layer Id: " << (*lastHit)->id().layerId() << " Side: " << lastLR << " DigiTime: " << (*lastHit)->digiTime() << endl;
173  }
174 
175  vector<AssPoint> assHits;
176  // create a candidate hit list
177  assHits.push_back(AssPoint(*firstHit,codes[firstLR]));
178  assHits.push_back(AssPoint(*lastHit,codes[lastLR]));
179 
180  // run hit adding/segment building
181  maxfound = 3;
182  addHits(sl,assHits,hitsForFit,result, usedHits);
183  }
184  }
185  }
186  }
187 
188  // now I have a couple of segment hypotheses, should check for ghost
189  if (debug) {
190  cout << "Result (before cleaning): " << result.size() << endl;
191  for (vector<DTSegmentCand*>::const_iterator seg=result.begin(); seg!=result.end(); ++seg)
192  cout << *(*seg) << endl;
193  }
194  result = theCleaner->clean(result);
195  if (debug) {
196  cout << "Result (after cleaning): " << result.size() << endl;
197  for (vector<DTSegmentCand*>::const_iterator seg=result.begin();
198  seg!=result.end(); ++seg)
199  cout << *(*seg) << endl;
200  }
201 
202  return result;
203 }
204 
205 
206 
207 void
208 DTMeantimerPatternReco::addHits(const DTSuperLayer* sl, vector<AssPoint>& assHits, const vector<DTHitPairForFit*>& hits,
209  vector<DTSegmentCand*> &result, vector<AssPoint>& usedHits) {
210 
211  typedef vector<DTHitPairForFit*> hitCont;
212  double chi2l,chi2r,t0_corrl,t0_corrr;
213  bool foundSomething = false;
214 
215  if (debug)
216  cout << "DTMeantimerPatternReco::addHit " << endl << " Picked " << assHits.size() << " hits, " << hits.size() << " left." << endl;
217 
218  if (assHits.size()+hits.size()<maxfound) return;
219 
220  // loop over the remaining hits
221  for (hitCont::const_iterator hit=hits.begin(); hit!=hits.end(); ++hit) {
222  if (debug)
223  cout << " Trying B: " << **hit<< " wire: " << (*hit)->id() << endl;
224 
225  assHits.push_back(AssPoint(*hit, DTEnums::Left));
226  bool left_ok=fitWithT0(assHits, chi2l, t0_corrl,0);
227  assHits.pop_back();
228 
229  assHits.push_back(AssPoint(*hit, DTEnums::Right));
230  bool right_ok=fitWithT0(assHits, chi2r, t0_corrr,0);
231  assHits.pop_back();
232 
233  if (debug) {
234  int nHits=assHits.size()+1;
235  cout << " Left: t0_corr = " << t0_corrl << "ns chi2/nHits = " << chi2l << "/" << nHits << " ok: " << left_ok << endl;
236  cout << " Right: t0_corr = " << t0_corrr << "ns chi2/nHits = " << chi2r << "/" << nHits << " ok: " << right_ok << endl;
237  }
238 
239  if (!left_ok && !right_ok) continue;
240 
241  foundSomething = true;
242 
243  // prepare the hit set for the next search, start from the other side
244  hitCont hitsForFit;
245  for (hitCont::const_iterator tmpHit=hit+1; tmpHit!=hits.end(); tmpHit++)
246  if (geometryFilter((*tmpHit)->id(),(*hit)->id())) hitsForFit.push_back(*tmpHit);
247 
248  reverse(hitsForFit.begin(),hitsForFit.end());
249 
250  // choose only one - left or right
251  if (assHits.size()>3 && left_ok && right_ok) {
252  if (chi2l<chi2r-0.1) right_ok=false; else
253  if (chi2r<chi2l-0.1) left_ok=false;
254  }
255  if (left_ok) {
256  assHits.push_back(AssPoint(*hit, DTEnums::Left));
257  addHits(sl,assHits,hitsForFit,result,usedHits);
258  assHits.pop_back();
259  }
260  if (right_ok) {
261  assHits.push_back(AssPoint(*hit, DTEnums::Right));
262  addHits(sl,assHits,hitsForFit,result,usedHits);
263  assHits.pop_back();
264  }
265  }
266 
267  if (foundSomething) return;
268 
269  // If we already have a segment with more hits from this hit pair - don't save this one.
270  if (assHits.size()<maxfound) return;
271 
272  // Check if semgent Ok, calculate chi2
273  if (!fitWithT0(assHits, chi2l, t0_corrl,debug)) return;
274 
275  // If no more iterations - store the current segment
276 
277  DTSegmentCand::AssPointCont pointsSet;
278  pointsSet.insert(assHits.begin(),assHits.end());
279  DTSegmentCand* seg = new DTSegmentCand(pointsSet,sl);
280  theUpdator->fit(seg);
281 
282  if (seg) {
283  for (vector<AssPoint>::const_iterator hiti = assHits.begin()+1; hiti != assHits.end()-1; hiti++)
284  usedHits.push_back(*hiti);
285 
286  if (assHits.size()>maxfound) maxfound = assHits.size();
287  seg->setChi2(chi2l); // we need to set the chi^2 so that the cleaner can pick the best segments
288  if (debug) cout << "Segment built: " << *seg<< endl;
289  if (checkDoubleCandidates(result,seg)) {
290  result.push_back(seg);
291  if (debug) cout << " Result is now " << result.size() << endl;
292  } else {
293  if (debug) cout << " Exists - skipping" << endl;
294  delete seg;
295  }
296  }
297 }
298 
299 
300 bool
302 {
303 // return true;
304 
305  const int layerLowerCut[4]={0,-1,-2,-2};
306  const int layerUpperCut[4]={0, 2, 2, 3};
307 // const int layerLowerCut[4]={0,-2,-4,-5};
308 // const int layerUpperCut[4]={0, 3, 4, 6};
309 
310  // deal only with hits that are in the same SL
311  if (first.layerId().superlayerId().superLayer()!=second.layerId().superlayerId().superLayer())
312  return true;
313 
314  int deltaLayer=abs(first.layerId().layer()-second.layerId().layer());
315 
316  // drop hits in the same layer
317  if (!deltaLayer) return false;
318 
319  // protection against unexpected layer numbering
320  if (deltaLayer>3) {
321  cout << "*** WARNING! DT Layer numbers differ by more than 3! for hits: " << endl;
322  cout << " " << first << endl;
323  cout << " " << second << endl;
324  return false;
325  }
326 
327  // accept only hits in cells "not too far away"
328  int deltaWire=first.wire()-second.wire();
329  if (second.layerId().layer()%2==0) deltaWire=-deltaWire; // yet another trick to get it right...
330  if ((deltaWire<=layerLowerCut[deltaLayer]) || (deltaWire>=layerUpperCut[deltaLayer])) return false;
331 
332  return true;
333 }
334 
335 
336 bool
337 DTMeantimerPatternReco::fitWithT0(const vector<AssPoint> &assHits, double &chi2, double &t0_corr, const bool fitdebug)
338 {
339  typedef vector < pair<double,double> > hitCoord;
340  double a,b,coordError,x,y;
341  double sx=0,sy=0,sxy=0,sxx=0,ssx=0,ssy=0,s=0,ss=0;
342  int leftHits=0,rightHits=0;
343 
344  if (assHits.size()<3) return false;
345 
346  // I'm assuming the single hit error is the same for all hits...
347  coordError=((*(assHits.begin())).first)->localPositionError().xx();
348 
349  for (vector<AssPoint>::const_iterator assHit=assHits.begin(); assHit!=assHits.end(); ++assHit) {
350  if (coordError!=((*(assHits.begin())).first)->localPositionError().xx())
351  cout << " DTMeantimerPatternReco: Warning! Hit errors different within one segment!" << endl;
352 
353  x=((*assHit).first)->localPosition((*assHit).second).z();
354  y=((*assHit).first)->localPosition((*assHit).second).x();
355 
356  sx+=x;
357  sy+=y;
358  sxy+=x*y;
359  sxx+=x*x;
360  s++;
361 
362  if ((*assHit).second==DTEnums::Left) {
363  leftHits++;
364  ssx+=x;
365  ssy+=y;
366  ss++;
367  } else {
368  rightHits++;
369  ssx-=x;
370  ssy-=y;
371  ss--;
372  }
373  }
374 
375  if (fitdebug && debug)
376  cout << " DTMeantimerPatternReco::fitWithT0 Left hits: " << leftHits << " Right hits: " << rightHits << endl;
377 
378  if (leftHits && rightHits) {
379 
380  double delta = ss*ss*sxx+s*sx*sx+s*ssx*ssx-s*s*sxx-2*ss*sx*ssx;
381  t0_corr=0.;
382 
383  if (delta) {
384  a=(ssy*s*ssx+sxy*ss*ss+sy*sx*s-sy*ss*ssx-ssy*sx*ss-sxy*s*s)/delta;
385  b=(ssx*sy*ssx+sxx*ssy*ss+sx*sxy*s-sxx*sy*s-ssx*sxy*ss-sx*ssy*ssx)/delta;
386  t0_corr=(ssx*s*sxy+sxx*ss*sy+sx*sx*ssy-sxx*s*ssy-sx*ss*sxy-ssx*sx*sy)/delta;
387  } else return false;
388  } else {
389  double d = s*sxx - sx*sx;
390  b = (sxx*sy- sx*sxy)/ d;
391  a = (s*sxy - sx*sy) / d;
392  t0_corr=0;
393  }
394 
395 // Calculate the chi^2 of the hits AFTER t0 correction
396 
397  double chi,chi2_not0;
398  chi2=0;
399  chi2_not0=0;
400 
401  for (vector<AssPoint>::const_iterator assHit=assHits.begin(); assHit!=assHits.end(); ++assHit) {
402  x=((*assHit).first)->localPosition((*assHit).second).z();
403  y=((*assHit).first)->localPosition((*assHit).second).x();
404 
405  chi=y-a*x-b;
406  chi2_not0+=chi*chi/coordError;
407 
408  if ((*assHit).second==DTEnums::Left) chi-=t0_corr;
409  else chi+=t0_corr;
410 
411  chi2+=chi*chi/coordError;
412  }
413 
414  // For 3-hit segments ignore timing information
415  if (assHits.size()<4) {
416  chi2=chi2_not0;
417 // if (chi2<theMaxChi2) return true;
418  if (chi2<200.) return true;
419  else return false;
420  }
421 
422  t0_corr/=-0.00543; // convert drift distance to time
423 
424  if (debug && fitdebug)
425  {
426  cout << " t0_corr = " << t0_corr << "ns chi2/nHits = " << chi2 << "/" << assHits.size() << endl;
427 
428  if (((chi2/(assHits.size()-2)<theMaxChi2) && (t0_corr<theMaxT0) && (t0_corr>theMinT0)) || (assHits.size()==4)) {
429  cout << " a = " << a << " b = " << b << endl;
430  for (vector<AssPoint>::const_iterator assHit=assHits.begin(); assHit!=assHits.end(); ++assHit) {
431  x=((*assHit).first)->localPosition((*assHit).second).z();
432  y=((*assHit).first)->localPosition((*assHit).second).x();
433  cout << " z= " << x << " x= " << y;
434  if ((*assHit).second==DTEnums::Left) cout << " x_corr= " << y+t0_corr*0.00543;
435  else cout << " x_corr= " << y-t0_corr*0.00543;
436  cout << " seg= " << a*x+b << endl;
437  }
438  }
439  }
440 
441  if ((chi2/(assHits.size()-2)<theMaxChi2) && (t0_corr<theMaxT0) && (t0_corr>theMinT0)) return true;
442  else return false;
443 }
444 
445 void
446 DTMeantimerPatternReco::rawFit(double &a, double &b, const vector< pair<double,double> > &hits) {
447 
448  double s=0,sx=0,sy=0,x,y;
449  double sxx=0,sxy=0;
450 
451  a=b=0;
452  if (hits.size()==0) return;
453 
454  if (hits.size()==1) {
455  b=(*(hits.begin())).second;
456  } else {
457  for (unsigned int i = 0; i != hits.size(); i++) {
458  x=hits[i].first;
459  y=hits[i].second;
460  sy += y;
461  sxy+= x*y;
462  s += 1.;
463  sx += x;
464  sxx += x*x;
465  }
466  // protect against a vertical line???
467 
468  double d = s*sxx - sx*sx;
469  b = (sxx*sy- sx*sxy)/ d;
470  a = (s*sxy - sx*sy) / d;
471  }
472 }
473 
474 bool
475 DTMeantimerPatternReco::checkDoubleCandidates(vector<DTSegmentCand*>& cands,
476  DTSegmentCand* seg) {
477  for (vector<DTSegmentCand*>::iterator cand=cands.begin();
478  cand!=cands.end(); ++cand) {
479  if (*(*cand)==*seg) return false;
480  if (((*cand)->nHits()>=seg->nHits()) && ((*cand)->chi2ndof()<seg->chi2ndof()))
481  if ((*cand)->nSharedHitPairs(*seg)>int(seg->nHits()-2)) return false;
482  }
483  return true;
484 }
dbl * delta
Definition: mlp_gen.cc:36
T getParameter(std::string const &) const
std::pair< DTHitPairForFit *, DTEnums::DTCellSide > AssPoint
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void update(DTRecSegment4D *seg, const bool calcT0=false) const
recompute hits position and refit the segment4D
virtual double chi2ndof() const
the chi2/NDOF of the fit
Definition: DTSegmentCand.h:66
bool checkDoubleCandidates(std::vector< DTSegmentCand * > &segs, DTSegmentCand *seg)
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:47
DTCellSide
Which side of the DT cell.
Definition: DTEnums.h:15
virtual void setChi2(double &chi2)
set chi2
virtual ~DTMeantimerPatternReco()
Destructor.
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
std::vector< DTSegmentCand * > clean(const std::vector< DTSegmentCand * > &inputCands) const
do the cleaning
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:59
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
U second(std::pair< T, U > const &p)
DTSuperLayerId id() const
Return the DetId of this SL.
Definition: DTSuperLayer.cc:36
void push_back(D *&d)
Definition: OwnVector.h:273
virtual void setES(const edm::EventSetup &setup)
void setES(const edm::EventSetup &setup)
set the setup
std::vector< DTHitPairForFit * > initHits(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits)
tuple result
Definition: query.py:137
tuple IP
Definition: listHistos.py:76
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int superLayer() const
Return the superlayer number.
bool fit(DTSegmentCand *seg) const
bool fitWithT0(const std::vector< AssPoint > &assHits, double &chi2, double &t0_corr, const bool fitdebug)
bool first
Definition: L1TdeRCT.cc:79
unsigned int id
int wire() const
Return the wire number.
Definition: DTWireId.h:56
std::set< AssPoint, AssPointLessZ > AssPointCont
Definition: DTSegmentCand.h:39
void rawFit(double &a, double &b, const std::vector< std::pair< double, double > > &hits)
virtual unsigned int nHits() const
Definition: DTSegmentCand.h:60
std::vector< DTSegmentCand * > buildSegments(const DTSuperLayer *sl, const std::vector< DTHitPairForFit * > &hits)
const T & get() const
Definition: EventSetup.h:55
double b
Definition: hdecay.h:120
bool geometryFilter(const DTWireId first, const DTWireId second) const
virtual edm::OwnVector< DTSLRecSegment2D > reconstruct(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits)
this function is called in the producer
void addHits(const DTSuperLayer *sl, std::vector< AssPoint > &assHits, const std::vector< DTHitPairForFit * > &hits, std::vector< DTSegmentCand * > &result, std::vector< AssPoint > &usedHits)
DTLayerId layerId() const
Return the corresponding LayerId.
Definition: DTWireId.h:62
DTMeantimerPatternReco(const edm::ParameterSet &pset)
Constructor.
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
Definition: DDAxes.h:10
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
edm::ESHandle< DTGeometry > theDTGeometry