CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DTCombinatorialExtendedPatternReco.cc
Go to the documentation of this file.
1 
7 /* This Class Header */
9 
10 /* Collaborating Class Header */
25 
26 /* C++ Headers */
27 #include <iterator>
28 using namespace std;
30 
31 /* ====================================================================== */
32 
36  : DTRecSegment2DBaseAlgo(pset),
37  theAlgoName("DTCombinatorialExtendedPatternReco"),
38  theDTGeometryToken(cc.esConsumes()) {
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  debug = pset.getUntrackedParameter<bool>("debug"); //true;
43  theUpdator = new DTSegmentUpdator(pset, cc);
44  theCleaner = new DTSegmentCleaner(pset);
45  string theHitAlgoName = pset.getParameter<string>("recAlgo");
46  usePairs = !(theHitAlgoName == "DTNoDriftAlgo");
47 }
48 
51 
52 /* Operations */
54  const DTSuperLayer* sl, const std::vector<DTRecHit1DPair>& pairs) {
55  if (debug)
56  cout << "DTCombinatorialExtendedPatternReco::reconstruct" << endl;
57  theTriedPattern.clear();
59  vector<std::shared_ptr<DTHitPairForFit>> hitsForFit = initHits(sl, pairs);
60 
61  vector<DTSegmentCand*> candidates = buildSegments(sl, hitsForFit);
62 
63  vector<DTSegmentCand*>::const_iterator cand = candidates.begin();
64  while (cand < candidates.end()) {
65  DTSLRecSegment2D* segment = (**cand);
66 
67  theUpdator->update(segment, false);
68 
69  result.push_back(segment);
70 
71  if (debug) {
72  cout << "Reconstructed 2D extended segments " << result.back() << endl;
73  }
74 
75  delete *(cand++); // delete the candidate!
76  }
77 
78  return result;
79 }
80 
82  // Get the DT Geometry
84  theUpdator->setES(setup);
85 }
86 
88 
89 vector<std::shared_ptr<DTHitPairForFit>> DTCombinatorialExtendedPatternReco::initHits(
90  const DTSuperLayer* sl, const std::vector<DTRecHit1DPair>& hits) {
91  vector<std::shared_ptr<DTHitPairForFit>> result;
92  for (vector<DTRecHit1DPair>::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
93  result.push_back(std::make_shared<DTHitPairForFit>(*hit, *sl, theDTGeometry));
94  }
95  return result;
96 }
97 
99  const DTSuperLayer* sl, const std::vector<std::shared_ptr<DTHitPairForFit>>& hits) {
100  typedef vector<std::shared_ptr<DTHitPairForFit>> hitCont;
101  typedef hitCont::const_iterator hitIter;
102  vector<DTSegmentCand*> result;
103 
104  if (debug) {
105  cout << "DTCombinatorialExtendedPatternReco::buildSegments: " << sl->id() << " nHits " << hits.size() << endl;
106  for (vector<std::shared_ptr<DTHitPairForFit>>::const_iterator hit = hits.begin(); hit != hits.end(); ++hit)
107  cout << **hit << endl;
108  }
109 
110  // 10-Mar-2004 SL
111  // put a protection against heavily populated chambers, for which the segment
112  // building could lead to infinite memory usage...
113  if (hits.size() > theMaxAllowedHits) {
114  if (debug) {
115  cout << "Warning: this SuperLayer " << sl->id() << " has too many hits : " << hits.size() << " max allowed is "
116  << theMaxAllowedHits << endl;
117  cout << "Skipping segment reconstruction... " << endl;
118  }
119  return result;
120  }
121 
123  // compatible with them
124  for (hitCont::const_iterator firstHit = hits.begin(); firstHit != hits.end(); ++firstHit) {
125  for (hitCont::const_reverse_iterator lastHit = hits.rbegin(); (*lastHit) != (*firstHit); ++lastHit) {
126  // hits must nor in the same nor in adiacent layers
127  if (fabs((*lastHit)->id().layerId() - (*firstHit)->id().layerId()) <= 1)
128  continue;
129  if (debug) {
130  cout << "Selected these two hits pair " << endl;
131  cout << "First " << *(*firstHit) << " Layer Id: " << (*firstHit)->id().layerId() << endl;
132  cout << "Last " << *(*lastHit) << " Layer Id: " << (*lastHit)->id().layerId() << endl;
133  }
134 
135  GlobalPoint IP;
136  float DAlphaMax;
137  if ((sl->id()).superlayer() == 2) // Theta SL
138  DAlphaMax = theAlphaMaxTheta;
139  else // Phi SL
140  DAlphaMax = theAlphaMaxPhi;
141 
143  for (int firstLR = 0; firstLR < 2; ++firstLR) {
144  for (int lastLR = 0; lastLR < 2; ++lastLR) {
145  // TODO move the global transformation in the DTHitPairForFit class
146  // when it will be moved I will able to remove the sl from the input parameter
147  GlobalPoint gposFirst = sl->toGlobal((*firstHit)->localPosition(codes[firstLR]));
148  GlobalPoint gposLast = sl->toGlobal((*lastHit)->localPosition(codes[lastLR]));
149 
150  GlobalVector gvec = gposLast - gposFirst;
151  GlobalVector gvecIP = gposLast - IP;
152 
153  // difference in angle measured
154  float DAlpha = fabs(gvec.theta() - gvecIP.theta());
155 
156  // cout << "DAlpha " << DAlpha << endl;
157  if (DAlpha < DAlphaMax) {
158  // create a segment hypotesis
159  // I don't need a true segment, just direction and position
160  LocalPoint posIni = (*firstHit)->localPosition(codes[firstLR]);
161  LocalVector dirIni = ((*lastHit)->localPosition(codes[lastLR]) - posIni).unit();
162 
163  // search for other compatible hits, with or without the L/R solved
164  vector<DTSegmentCand::AssPoint> assHits = findCompatibleHits(posIni, dirIni, hits);
165  if (debug)
166  cout << "compatible hits " << assHits.size() << endl;
167 
168  // here return an extended candidate (which _has_ the original
169  // segment)
170  DTSegmentExtendedCand* seg = buildBestSegment(assHits, sl);
171 
172  if (seg) {
173  if (debug)
174  cout << "segment " << *seg << endl;
175 
176  // check if the chi2 and #hits are ok
177  if (!seg->good()) { // good is reimplmented in extended segment
178  delete seg;
179  } else {
180  // remove duplicated segments
181  if (checkDoubleCandidates(result, seg)) {
182  // add to the vector of hypotesis
183  // still work with extended segments
184  result.push_back(seg);
185  if (debug)
186  cout << "result is now " << result.size() << endl;
187  } else { // delete it!
188  delete seg;
189  if (debug)
190  cout << "already existing" << endl;
191  }
192  }
193  }
194  }
195  }
196  }
197  }
198  }
199  if (debug) {
200  for (vector<DTSegmentCand*>::const_iterator seg = result.begin(); seg != result.end(); ++seg)
201  cout << *(*seg) << endl;
202  }
203 
204  // now I have a couple of segment hypotesis, should check for ghost
205  // still with extended candidates
206  result = theCleaner->clean(result);
207  if (debug) {
208  cout << "result no ghost " << result.size() << endl;
209  for (vector<DTSegmentCand*>::const_iterator seg = result.begin(); seg != result.end(); ++seg)
210  cout << *(*seg) << endl;
211  }
212 
213  // here, finally, I have to return the set of _original_ segments, not the
214  // extended ones.
215  return result;
216 }
217 
219  const LocalPoint& posIni, const LocalVector& dirIni, const vector<std::shared_ptr<DTHitPairForFit>>& hits) {
220  if (debug)
221  cout << "Pos: " << posIni << " Dir: " << dirIni << endl;
222  vector<DTSegmentCand::AssPoint> result;
223 
224  // counter to early-avoid double counting in hits pattern
225  vector<int> tried;
226  int nCompatibleHits = 0;
227 
228  typedef vector<std::shared_ptr<DTHitPairForFit>> hitCont;
229  typedef hitCont::const_iterator hitIter;
230  for (hitIter hit = hits.begin(); hit != hits.end(); ++hit) {
231  pair<bool, bool> isCompatible = (*hit)->isCompatible(posIni, dirIni);
232  if (debug)
233  cout << "isCompatible " << isCompatible.first << " " << isCompatible.second << endl;
234 
235  // if only one of the two is compatible, then the LR is assigned,
236  // otherwise is undefined
237 
238  DTEnums::DTCellSide lrcode;
239  if (isCompatible.first && isCompatible.second) {
240  usePairs ? lrcode = DTEnums::undefLR : lrcode = DTEnums::Left; // if not usePairs then only use single side
241  tried.push_back(3);
242  nCompatibleHits++;
243  } else if (isCompatible.first) {
244  lrcode = DTEnums::Left;
245  tried.push_back(2);
246  nCompatibleHits++;
247  } else if (isCompatible.second) {
248  lrcode = DTEnums::Right;
249  tried.push_back(1);
250  nCompatibleHits++;
251  } else {
252  tried.push_back(0);
253  continue; // neither is compatible
254  }
255  result.push_back(DTSegmentCand::AssPoint(*hit, lrcode));
256  }
257 
258  // check if too few associated hits or pattern already tried
259  if (nCompatibleHits < 3 || find(theTriedPattern.begin(), theTriedPattern.end(), tried) == theTriedPattern.end()) {
260  theTriedPattern.push_back(tried);
261  } else {
262  if (debug) {
263  vector<vector<int>>::const_iterator t = find(theTriedPattern.begin(), theTriedPattern.end(), tried);
264  cout << "Already tried";
265  copy((*t).begin(), (*t).end(), ostream_iterator<int>(std::cout));
266  cout << endl;
267  }
268  // empty the result vector
269  result.clear();
270  }
271  return result;
272 }
273 
275  const DTSuperLayer* sl) {
276  if (debug)
277  cout << "DTCombinatorialExtendedPatternReco::buildBestSegment " << hits.size() << endl;
278  if (hits.size() < 3) {
279  //cout << "buildBestSegment: hits " << hits.size()<< endl;
280  return nullptr; // a least 3 point
281  }
282 
283  // hits with defined LR
284  vector<DTSegmentCand::AssPoint> points;
285 
286  // without: I store both L and R, a deque since I need front insertion and
287  // deletion
288  deque<std::shared_ptr<DTHitPairForFit>> pointsNoLR;
289 
290  // first add only the hits with LR assigned
291  for (vector<DTSegmentCand::AssPoint>::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
292  if ((*hit).second != DTEnums::undefLR) {
293  points.push_back(*hit);
294  } else { // then also for the undef'd one
295  pointsNoLR.push_back((*hit).first);
296  }
297  }
298 
299  if (debug) {
300  cout << "points " << points.size() << endl;
301  cout << "pointsNoLR " << pointsNoLR.size() << endl;
302  }
303 
304  // build all possible candidates using L/R ambiguity
305  vector<DTSegmentCand*> candidates;
306 
307  buildPointsCollection(points, pointsNoLR, candidates, sl);
308 
309  // here I try to add the external clusters and build a set of "extended
310  // segment candidate
311  vector<DTSegmentExtendedCand*> extendedCands = extendCandidates(candidates, sl);
312  if (debug)
313  cout << "extended candidates " << extendedCands.size() << endl;
314 
315  // so now I have build a given number of segments, I should find the best one,
316  // by #hits and chi2.
317  vector<DTSegmentExtendedCand*>::const_iterator bestCandIter = extendedCands.end();
318  double minChi2 = 999999.;
319  unsigned int maxNumHits = 0;
320  for (vector<DTSegmentExtendedCand*>::const_iterator iter = extendedCands.begin(); iter != extendedCands.end();
321  ++iter) {
322  if ((*iter)->nHits() == maxNumHits && (*iter)->chi2() < minChi2) {
323  minChi2 = (*iter)->chi2();
324  bestCandIter = iter;
325  } else if ((*iter)->nHits() > maxNumHits) {
326  maxNumHits = (*iter)->nHits();
327  minChi2 = (*iter)->chi2();
328  bestCandIter = iter;
329  }
330  }
331 
332  // delete all candidates but the best one!
333  for (vector<DTSegmentExtendedCand*>::iterator iter = extendedCands.begin(); iter != extendedCands.end(); ++iter)
334  if (iter != bestCandIter)
335  delete (*iter);
336 
337  // return the best candate if any
338  if (bestCandIter != extendedCands.end()) {
339  return (*bestCandIter);
340  }
341  return nullptr;
342 }
343 
344 void DTCombinatorialExtendedPatternReco::buildPointsCollection(vector<DTSegmentCand::AssPoint>& points,
345  deque<std::shared_ptr<DTHitPairForFit>>& pointsNoLR,
346  vector<DTSegmentCand*>& candidates,
347  const DTSuperLayer* sl) {
348  if (debug) {
349  cout << "DTCombinatorialExtendedPatternReco::buildPointsCollection " << endl;
350  cout << "points: " << points.size() << " NOLR: " << pointsNoLR.size() << endl;
351  }
352  if (!pointsNoLR.empty()) { // still unassociated points!
353  std::shared_ptr<DTHitPairForFit> unassHit = pointsNoLR.front();
354  // try with the right
355  if (debug)
356  cout << "Right hit" << endl;
357  points.push_back(DTSegmentCand::AssPoint(unassHit, DTEnums::Right));
358  pointsNoLR.pop_front();
359  buildPointsCollection(points, pointsNoLR, candidates, sl);
360  pointsNoLR.push_front((unassHit));
361  points.pop_back();
362 
363  // try with the left
364  if (debug)
365  cout << "Left hit" << endl;
366  points.push_back(DTSegmentCand::AssPoint(unassHit, DTEnums::Left));
367  pointsNoLR.pop_front();
368  buildPointsCollection(points, pointsNoLR, candidates, sl);
369  pointsNoLR.push_front((unassHit));
370  points.pop_back();
371  } else { // all associated
372 
373  if (debug) {
374  cout << "The Hits were" << endl;
375  copy(points.begin(), points.end(), ostream_iterator<DTSegmentCand::AssPoint>(std::cout));
376  cout << "----" << endl;
377  cout << "All associated " << endl;
378  }
379  DTSegmentCand::AssPointCont pointsSet;
380 
381  // for (vector<DTSegmentCand::AssPoint>::const_iterator point=points.begin();
382  // point!=points.end(); ++point)
383  pointsSet.insert(points.begin(), points.end());
384 
385  if (debug) {
386  cout << "The Hits are" << endl;
387  copy(pointsSet.begin(), pointsSet.end(), ostream_iterator<DTSegmentCand::AssPoint>(std::cout));
388  cout << "----" << endl;
389  }
390 
391  DTSegmentCand* newCand = new DTSegmentCand(pointsSet, sl);
392  if (theUpdator->fit(newCand, false, false))
393  candidates.push_back(newCand);
394  else
395  delete newCand; // bad seg, too few hits
396  }
397 }
398 
400  for (vector<DTSegmentCand*>::iterator cand = cands.begin(); cand != cands.end(); ++cand)
401  if (*(*cand) == *seg)
402  return false;
403  return true;
404 }
405 
406 vector<DTSegmentExtendedCand*> DTCombinatorialExtendedPatternReco::extendCandidates(vector<DTSegmentCand*>& candidates,
407  const DTSuperLayer* sl) {
408  if (debug)
409  cout << "extendCandidates " << candidates.size() << endl;
410  vector<DTSegmentExtendedCand*> result;
411 
412  // in case of phi SL just return
413  if (sl->id().superLayer() != 2) {
414  for (vector<DTSegmentCand*>::const_iterator cand = candidates.begin(); cand != candidates.end(); ++cand) {
415  DTSegmentExtendedCand* extendedCand = new DTSegmentExtendedCand(*cand);
416  // and delete the original candidate
417  delete *cand;
418  result.push_back(extendedCand);
419  }
420  return result;
421  }
422 
423  // first I have to select the cluster which are compatible with the actual
424  // candidate, namely +/-1 sector/station/wheel
425  vector<DTSegmentExtendedCand::DTSLRecClusterForFit> clustersWithPos;
426  if (debug)
427  cout << "AllClustersWithPos " << theClusters.size() << endl;
428  if (debug)
429  cout << "SL: " << sl->id() << endl;
430  for (vector<DTSLRecCluster>::const_iterator clus = theClusters.begin(); clus != theClusters.end(); ++clus) {
431  if (debug)
432  cout << "CLUS: " << (*clus).superLayerId() << endl;
433  if ((*clus).superLayerId().superLayer() == 2 && closeSL(sl->id(), (*clus).superLayerId())) {
434  // and then get their pos in the actual SL frame
435  const DTSuperLayer* clusSl = theDTGeometry->superLayer((*clus).superLayerId());
436  LocalPoint pos = sl->toLocal(clusSl->toGlobal((*clus).localPosition()));
437  //LocalError err=sl->toLocal(clusSl->toGlobal((*clus).localPositionError()));
438  LocalError err = (*clus).localPositionError();
439  clustersWithPos.push_back(DTSegmentExtendedCand::DTSLRecClusterForFit(*clus, pos, err));
440  }
441  }
442  if (debug)
443  cout << "closeClustersWithPos " << clustersWithPos.size() << endl;
444 
445  for (vector<DTSegmentCand*>::const_iterator cand = candidates.begin(); cand != candidates.end(); ++cand) {
446  // create an extended candidate
447  DTSegmentExtendedCand* extendedCand = new DTSegmentExtendedCand(*cand);
448  // and delete the original candidate
449  delete *cand;
450  // do this only for theta SL
451  if (extendedCand->superLayer()->id().superLayer() == 2) {
452  // first check compatibility between cand and clusForFit
453  for (vector<DTSegmentExtendedCand::DTSLRecClusterForFit>::const_iterator exClus = clustersWithPos.begin();
454  exClus != clustersWithPos.end();
455  ++exClus) {
456  if (extendedCand->isCompatible(*exClus)) {
457  if (debug)
458  cout << "is compatible " << endl;
459  // add compatible cluster
460  extendedCand->addClus(*exClus);
461  }
462  }
463  // fit the segment
464  if (debug)
465  cout << "extended cands nHits: " << extendedCand->nHits() << endl;
466  if (theUpdator->fit(extendedCand, false, false)) {
467  // add to result
468  result.push_back(extendedCand);
469  } else {
470  cout << "Bad fit" << endl;
471  delete extendedCand;
472  }
473  } else { // Phi SuperLayer
474  result.push_back(extendedCand);
475  }
476  }
477 
478  return result;
479 }
480 
482  if (id1 == id2)
483  return false;
484  if (abs(id1.wheel() - id2.wheel()) > 1)
485  return false;
486  // take into account also sector 13 and 14
487  int sec1 = (id1.sector() == 13) ? 4 : id1.sector();
488  sec1 = (sec1 == 14) ? 10 : sec1;
489  int sec2 = (id2.sector() == 13) ? 4 : id2.sector();
490  sec2 = (sec2 == 14) ? 10 : sec2;
491  // take into account also sector 1/12
492  if (abs(sec1 - sec2) > 1 && abs(sec1 - sec2) != 11)
493  return false;
494  //if (abs(id1.station()-id2.station())>1 ) return false;
495  return true;
496 }
std::pair< std::shared_ptr< DTHitPairForFit >, DTEnums::DTCellSide > AssPoint
Definition: DTSegmentCand.h:36
edm::OwnVector< DTSLRecSegment2D > reconstruct(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits) override
this function is called in the producer
void setClusters(const std::vector< DTSLRecCluster > &clusters)
T getUntrackedParameter(std::string const &, T const &) const
reference back()
Definition: OwnVector.h:431
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
std::vector< DTSegmentCand * > buildSegments(const DTSuperLayer *sl, const std::vector< std::shared_ptr< DTHitPairForFit >> &hits)
DTCellSide
Which side of the DT cell.
Definition: DTEnums.h:15
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:58
std::vector< DTSegmentCand * > clean(const std::vector< DTSegmentCand * > &inputCands) const
do the cleaning
bool checkDoubleCandidates(std::vector< DTSegmentCand * > &segs, DTSegmentCand *seg)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< std::shared_ptr< DTHitPairForFit > > hitCont
bool good() const override
tuple result
Definition: mps_fire.py:311
Geom::Theta< T > theta() const
Definition: PV3DBase.h:72
DTSuperLayerId id() const
Return the DetId of this SL.
Definition: DTSuperLayer.cc:34
void push_back(D *&d)
Definition: OwnVector.h:326
void setES(const edm::EventSetup &setup)
set the setup
DTCombinatorialExtendedPatternReco(const edm::ParameterSet &pset, edm::ConsumesCollector)
Constructor.
unsigned int nHits() const override
void buildPointsCollection(std::vector< DTSegmentCand::AssPoint > &points, std::deque< std::shared_ptr< DTHitPairForFit >> &pointsNoLR, std::vector< DTSegmentCand * > &candidates, const DTSuperLayer *sl)
std::vector< std::shared_ptr< DTHitPairForFit > > initHits(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits)
std::vector< std::vector< int > > theTriedPattern
tuple IP
Definition: listHistos.py:76
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int superLayer() const
Return the superlayer number.
void addClus(const DTSegmentExtendedCand::DTSLRecClusterForFit &clus)
std::vector< DTSegmentExtendedCand * > extendCandidates(std::vector< DTSegmentCand * > &candidates, const DTSuperLayer *sl)
void setES(const edm::EventSetup &setup) override
const DTSuperLayer * superLayer() const
the super layer on which relies
Definition: DTSegmentCand.h:76
bool isCompatible(const DTSegmentExtendedCand::DTSLRecClusterForFit &clus)
edm::ESGetToken< DTGeometry, MuonGeometryRecord > theDTGeometryToken
std::set< AssPoint, AssPointLessZ > AssPointCont
Definition: DTSegmentCand.h:38
hitCont::const_iterator hitIter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool closeSL(const DTSuperLayerId &id1, const DTSuperLayerId &id2)
int sector() const
Definition: DTChamberId.h:49
tuple cout
Definition: gather_cfg.py:144
void update(DTRecSegment4D *seg, const bool calcT0, bool allow3par) const
recompute hits position and refit the segment4D
DTSegmentExtendedCand * buildBestSegment(std::vector< DTSegmentCand::AssPoint > &assHits, const DTSuperLayer *sl)
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:157
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:39
std::vector< DTSegmentCand::AssPoint > findCompatibleHits(const LocalPoint &pos, const LocalVector &dir, const std::vector< std::shared_ptr< DTHitPairForFit >> &hits)
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
bool fit(DTSegmentCand *seg, bool allow3par, const bool fitdebug) const
Basic3DVector unit() const