CMS 3D CMS Logo

DTMeantimerPatternReco.cc
Go to the documentation of this file.
1 
8 /* This Class Header */
10 
11 /* Collaborating Class Header */
27 
28 /* C++ Headers */
29 #include <iterator>
30 using namespace std;
32 
33 /* ====================================================================== */
34 
35 typedef std::vector<std::shared_ptr<DTHitPairForFit>> hitCont;
36 typedef hitCont::const_iterator hitIter;
37 
41  theFitter(new DTLinearFit()),
42  theAlgoName("DTMeantimerPatternReco"),
43  theDTGeometryToken(cc.esConsumes()) {
44  theMaxAllowedHits = pset.getParameter<unsigned int>("MaxAllowedHits"); // 100
45  theAlphaMaxTheta = pset.getParameter<double>("AlphaMaxTheta"); // 0.1 ;
46  theAlphaMaxPhi = pset.getParameter<double>("AlphaMaxPhi"); // 1.0 ;
47  theMaxChi2 = pset.getParameter<double>("MaxChi2"); // 8.0 ;
48  debug = pset.getUntrackedParameter<bool>("debug");
51 }
52 
55  delete theUpdator;
56  delete theCleaner;
57  delete theFitter;
58 }
59 
60 /* Operations */
62  const std::vector<DTRecHit1DPair>& pairs) {
64  std::vector<std::shared_ptr<DTHitPairForFit>> hitsForFit = initHits(sl, pairs);
65 
66  vector<DTSegmentCand*> candidates = buildSegments(sl, hitsForFit);
67 
68  vector<DTSegmentCand*>::const_iterator cand = candidates.begin();
69  while (cand < candidates.end()) {
70  DTSLRecSegment2D* segment = (**cand);
71  theUpdator->update(segment, true);
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  return result;
81 }
82 
84  // Get the DT Geometry
87 }
88 
89 vector<std::shared_ptr<DTHitPairForFit>> DTMeantimerPatternReco::initHits(const DTSuperLayer* sl,
90  const std::vector<DTRecHit1DPair>& hits) {
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  vector<DTSegmentCand*> result;
102 
103  if (debug) {
104  cout << "buildSegments: " << sl->id() << " nHits " << hits.size() << endl;
105  for (hitIter hit = hits.begin(); hit != hits.end(); ++hit)
106  cout << **hit << " wire: " << (*hit)->id() << " DigiTime: " << (*hit)->digiTime() << endl;
107  }
108 
109  if (hits.size() > theMaxAllowedHits) {
110  if (debug) {
111  cout << "Warning: this SuperLayer " << sl->id() << " has too many hits : " << hits.size() << " max allowed is "
112  << theMaxAllowedHits << endl;
113  cout << "Skipping segment reconstruction... " << endl;
114  }
115  return result;
116  }
117 
118  GlobalPoint IP;
119  float DAlphaMax;
120  if ((sl->id()).superlayer() == 2) // Theta SL
121  DAlphaMax = theAlphaMaxTheta;
122  else // Phi SL
123  DAlphaMax = theAlphaMaxPhi;
124 
125  // get two hits in different layers and see if there are other hits
126  // compatible with them
127  for (hitCont::const_iterator firstHit = hits.begin(); firstHit != hits.end(); ++firstHit) {
128  for (hitCont::const_reverse_iterator lastHit = hits.rbegin(); (*lastHit) != (*firstHit); ++lastHit) {
129  // a geometrical sensibility cut for the two hits
130  if (!geometryFilter((*firstHit)->id(), (*lastHit)->id()))
131  continue;
132 
133  // create a set of hits for the fit (only the hits between the two selected ones)
134  hitCont hitsForFit;
135  for (hitCont::const_iterator tmpHit = firstHit + 1; (*tmpHit) != (*lastHit); tmpHit++)
136  if ((geometryFilter((*tmpHit)->id(), (*lastHit)->id())) && (geometryFilter((*tmpHit)->id(), (*firstHit)->id())))
137  hitsForFit.push_back(*tmpHit);
138 
139  for (int firstLR = 0; firstLR < 2; ++firstLR) {
140  for (int lastLR = 0; lastLR < 2; ++lastLR) {
141  // TODO move the global transformation in the DTHitPairForFit class
142  // when it will be moved I will able to remove the sl from the input parameter
143  GlobalPoint gposFirst = sl->toGlobal((*firstHit)->localPosition(codes[firstLR]));
144  GlobalPoint gposLast = sl->toGlobal((*lastHit)->localPosition(codes[lastLR]));
145  GlobalVector gvec = gposLast - gposFirst;
146  GlobalVector gvecIP = gposLast - IP;
147 
148  // difference in angle measured
149  float DAlpha = fabs(gvec.theta() - gvecIP.theta());
150  if (DAlpha > DAlphaMax)
151  continue;
152 
153  // if(debug) {
154  // cout << "Selected hit pair:" << endl;
155  // cout << " First " << *(*firstHit) << " Layer Id: " << (*firstHit)->id().layerId() << " Side: " << firstLR << " DigiTime: " << (*firstHit)->digiTime() << endl;
156  // cout << " Last " << *(*lastHit) << " Layer Id: " << (*lastHit)->id().layerId() << " Side: " << lastLR << " DigiTime: " << (*lastHit)->digiTime() << endl;
157  // }
158 
160  auto segCand = std::make_unique<DTSegmentCand>(pointSet, sl);
161  segCand->add(*firstHit, codes[firstLR]);
162  segCand->add(*lastHit, codes[lastLR]);
163 
164  // run hit adding/segment building
165  maxfound = 3;
166  addHits(segCand.get(), hitsForFit, result);
167  }
168  }
169  }
170  }
171 
172  // now I have a couple of segment hypotheses, should check for ghosts
173  if (debug) {
174  cout << "Result (before cleaning): " << result.size() << endl;
175  for (vector<DTSegmentCand*>::const_iterator seg = result.begin(); seg != result.end(); ++seg)
176  cout << *(*seg) << endl;
177  }
178 
180 
181  if (debug) {
182  cout << "Result (after cleaning): " << result.size() << endl;
183  for (vector<DTSegmentCand*>::const_iterator seg = result.begin(); seg != result.end(); ++seg)
184  cout << *(*seg) << endl;
185  }
186 
187  return result;
188 }
189 
191  const vector<std::shared_ptr<DTHitPairForFit>>& hits,
192  vector<DTSegmentCand*>& result) {
193  double chi2l, chi2r, t0l, t0r;
194  bool foundSomething = false;
195 
196  if (debug)
197  cout << " DTMeantimerPatternReco::addHit " << endl
198  << " Picked " << segCand->nHits() << " hits, " << hits.size() << " left." << endl;
199 
200  if (segCand->nHits() + hits.size() < maxfound)
201  return;
202 
203  // loop over the remaining hits
204  for (hitCont::const_iterator hit = hits.begin(); hit != hits.end(); ++hit) {
205  // if (debug) {
206  // cout << " Trying B: " << **hit<< " wire: " << (*hit)->id() << endl;
207  // printPattern(assHits,*hit);
208  // }
209 
212 
213  segCand->add(lhit);
214  bool left_ok = (fitWithT0(segCand, false) ? true : false);
215  chi2l = segCand->chi2();
216  t0l = segCand->t0();
217  segCand->removeHit(lhit);
218 
219  segCand->add(rhit);
220  bool right_ok = (fitWithT0(segCand, false) ? true : false);
221  chi2r = segCand->chi2();
222  t0r = segCand->t0();
223  segCand->removeHit(rhit);
224 
225  if (debug) {
226  int nHits = segCand->nHits() + 1;
227  cout << " Left: t0= " << t0l << " chi2/nHits= " << chi2l << "/" << nHits << " ok: " << left_ok << endl;
228  cout << " Right: t0= " << t0r << " chi2/nHits= " << chi2r << "/" << nHits << " ok: " << right_ok << endl;
229  }
230 
231  if (!left_ok && !right_ok)
232  continue;
233 
234  foundSomething = true;
235 
236  // prepare the hit set for the next search, start from the other side
237  hitCont hitsForFit;
238  for (hitCont::const_iterator tmpHit = hit + 1; tmpHit != hits.end(); tmpHit++)
239  if (geometryFilter((*tmpHit)->id(), (*hit)->id()))
240  hitsForFit.push_back(*tmpHit);
241 
242  reverse(hitsForFit.begin(), hitsForFit.end());
243 
244  // choose only one - left or right
245  if (segCand->nHits() > 3 && left_ok && right_ok) {
246  if (chi2l < chi2r - 0.1)
247  right_ok = false;
248  else if (chi2r < chi2l - 0.1)
249  left_ok = false;
250  }
251 
252  if (left_ok) {
253  segCand->add(lhit);
254  addHits(segCand, hitsForFit, result);
255  segCand->removeHit(lhit);
256  }
257 
258  if (right_ok) {
259  segCand->add(rhit);
260  addHits(segCand, hitsForFit, result);
261  segCand->removeHit(rhit);
262  }
263  }
264 
265  if (foundSomething)
266  return;
267  // if we didn't find any new hits compatible with the current candidate, we proceed to check and store the candidate
268 
269  // If we already have a segment with more hits from this hit pair - don't save this one.
270  if (segCand->nHits() < maxfound)
271  return;
272 
273  // Check if semgent Ok, calculate chi2
274  bool seg_ok = (fitWithT0(segCand, debug) ? true : false);
275  if (!seg_ok)
276  return;
277 
278  if (!segCand->good()) {
279  // if (debug) cout << " Segment not good() - skipping" << endl;
280  return;
281  }
282 
283  if (segCand->nHits() > maxfound)
284  maxfound = segCand->nHits();
285  if (debug)
286  cout << endl << " Seg t0= " << segCand->t0() << endl << *segCand << endl;
287 
288  if (checkDoubleCandidates(result, segCand)) {
289  result.push_back(new DTSegmentCand(*segCand));
290  if (debug)
291  cout << " Result is now " << result.size() << endl;
292  } else {
293  if (debug)
294  cout << " Exists - skipping" << endl;
295  }
296 }
297 
299  // return true;
300 
301  const int layerLowerCut[4] = {0, -1, -2, -2};
302  const int layerUpperCut[4] = {0, 2, 2, 3};
303  // const int layerLowerCut[4]={0,-2,-4,-5};
304  // const int layerUpperCut[4]={0, 3, 4, 6};
305 
306  // deal only with hits that are in the same SL
307  if (first.layerId().superlayerId().superLayer() != second.layerId().superlayerId().superLayer())
308  return true;
309 
310  int deltaLayer = abs(first.layerId().layer() - second.layerId().layer());
311 
312  // drop hits in the same layer
313  if (!deltaLayer)
314  return false;
315 
316  // protection against unexpected layer numbering
317  if (deltaLayer > 3) {
318  cout << "*** WARNING! DT Layer numbers differ by more than 3! for hits: " << endl;
319  cout << " " << first << endl;
320  cout << " " << second << endl;
321  return false;
322  }
323 
324  // accept only hits in cells "not too far away"
325  int deltaWire = first.wire() - second.wire();
326  if (second.layerId().layer() % 2 == 0)
327  deltaWire = -deltaWire; // yet another trick to get it right...
328  if ((deltaWire <= layerLowerCut[deltaLayer]) || (deltaWire >= layerUpperCut[deltaLayer]))
329  return false;
330 
331  return true;
332 }
333 
335  // perform the 3 parameter fit on the segment candidate
336  theUpdator->fit(seg, true, fitdebug);
337  double chi2 = seg->chi2();
338 
339  // Sanity check - drop segment candidates with a failed 3-par fit.
340  // (this includes segments with hits after the calculated t0 correction ending up
341  // beyond the chamber walls or on the other side of the wire)
342  if (chi2 == -1.)
343  return nullptr;
344 
345  // at this point we keep all 3-hit segments that passed the above check
346  if (seg->nHits() == 3)
347  return seg;
348 
349  // for segments with no t0 information we impose a looser chi2 cut
350  if (seg->t0() == 0) {
351  if (chi2 < 100.)
352  return seg;
353  else
354  return nullptr;
355  }
356 
357  // cut on chi2/ndof of the segment candidate
358  if ((chi2 / (seg->nHits() - 3) < theMaxChi2))
359  return seg;
360  else
361  return nullptr;
362 }
363 
365  for (vector<DTSegmentCand*>::iterator cand = cands.begin(); cand != cands.end(); ++cand) {
366  if (*(*cand) == *seg)
367  return false;
368  if (((*cand)->nHits() >= seg->nHits()) && ((*cand)->chi2ndof() < seg->chi2ndof()))
369  if ((*cand)->nSharedHitPairs(*seg) > int(seg->nHits() - 2))
370  return false;
371  }
372  return true;
373 }
374 
375 void DTMeantimerPatternReco::printPattern(vector<DTSegmentCand::AssPoint>& assHits, const DTHitPairForFit* hit) {
376  char mark[26] = {". . . . . . . . . . . . "};
377  int wire[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
378 
379  for (vector<DTSegmentCand::AssPoint>::const_iterator assHit = assHits.begin(); assHit != assHits.end(); ++assHit) {
380  int lay =
381  (((*assHit).first)->id().superlayerId().superLayer() - 1) * 4 + ((*assHit).first)->id().layerId().layer() - 1;
382  wire[lay] = ((*assHit).first)->id().wire();
383  if ((*assHit).second == DTEnums::Left)
384  mark[lay * 2] = 'L';
385  else
386  mark[lay * 2] = 'R';
387  }
388 
389  int lay = ((*hit).id().superlayerId().superLayer() - 1) * 4 + (*hit).id().layerId().layer() - 1;
390  wire[lay] = (*hit).id().wire();
391  mark[lay * 2] = '*';
392 
393  cout << " " << mark << endl << " ";
394  for (int i = 0; i < 12; i++)
395  if (wire[i])
396  cout << setw(2) << wire[i];
397  else
398  cout << " ";
399  cout << endl;
400 }
std::pair< std::shared_ptr< DTHitPairForFit >, DTEnums::DTCellSide > AssPoint
Definition: DTSegmentCand.h:36
std::vector< std::shared_ptr< DTHitPairForFit > > initHits(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
mark
Definition: cuy.py:1010
edm::OwnVector< DTSLRecSegment2D > reconstruct(const DTSuperLayer *sl, const std::vector< DTRecHit1DPair > &hits) override
this function is called in the producer
void setES(const edm::EventSetup &setup) override
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
const edm::ESGetToken< DTGeometry, MuonGeometryRecord > theDTGeometryToken
DTCellSide
Which side of the DT cell.
Definition: DTEnums.h:15
std::vector< DTSegmentCand * > clean(const std::vector< DTSegmentCand *> &inputCands) const
do the cleaning
void addHits(DTSegmentCand *segCand, const std::vector< std::shared_ptr< DTHitPairForFit >> &hits, std::vector< DTSegmentCand *> &result)
virtual unsigned int nHits() const
Definition: DTSegmentCand.h:58
std::vector< std::shared_ptr< DTHitPairForFit > > hitCont
void update(DTRecSegment4D *seg, const bool calcT0, bool allow3par) const
recompute hits position and refit the segment4D
virtual double chi2ndof() const
the chi2/NDOF of the fit
Definition: DTSegmentCand.h:64
U second(std::pair< T, U > const &p)
virtual bool good() const
DTSegmentCand * fitWithT0(DTSegmentCand *seg, const bool fitdebug)
std::vector< DTSegmentCand * > buildSegments(const DTSuperLayer *sl, const std::vector< std::shared_ptr< DTHitPairForFit >> &hits)
void setES(const edm::EventSetup &setup)
set the setup
virtual void add(AssPoint newHit)
add hits to the hit list.
bool fit(DTSegmentCand *seg, bool allow3par, const bool fitdebug) const
bool checkDoubleCandidates(std::vector< DTSegmentCand *> &segs, DTSegmentCand *seg)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual double chi2() const
the chi2 (NOT chi2/NDOF) of the fit
Definition: DTSegmentCand.h:61
~DTMeantimerPatternReco() override
Destructor.
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
unsigned int id
void printPattern(std::vector< DTSegmentCand::AssPoint > &assHits, const DTHitPairForFit *hit)
std::set< AssPoint, AssPointLessZ > AssPointCont
Definition: DTSegmentCand.h:38
hitCont::const_iterator hitIter
DTMeantimerPatternReco(const edm::ParameterSet &pset, edm::ConsumesCollector cc)
Constructor.
bool geometryFilter(const DTWireId first, const DTWireId second) const
virtual void removeHit(AssPoint hit)
remove hit from the candidate
virtual double t0() const
the t0 of the segment
Definition: DTSegmentCand.h:67
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
DTSuperLayerId id() const
Return the DetId of this SL.
Definition: DTSuperLayer.cc:34
Geom::Theta< T > theta() const
Definition: PV3DBase.h:72
edm::ESHandle< DTGeometry > theDTGeometry