CMS 3D CMS Logo

GlobalMuonRefitter.cc
Go to the documentation of this file.
1 
17 
18 //---------------
19 // C++ Headers --
20 //---------------
21 
22 #include <iostream>
23 #include <iomanip>
24 #include <algorithm>
25 
26 //-------------------------------
27 // Collaborating Class Headers --
28 //-------------------------------
29 
31 
41 
42 
57 
67 
68 using namespace std;
69 using namespace edm;
70 
71 //----------------
72 // Constructors --
73 //----------------
74 
78  theCosmicFlag(par.getParameter<bool>("PropDirForCosmics")),
79  theDTRecHitLabel(par.getParameter<InputTag>("DTRecSegmentLabel")),
80  theCSCRecHitLabel(par.getParameter<InputTag>("CSCRecSegmentLabel")),
81  theGEMRecHitLabel(par.getParameter<InputTag>("GEMRecHitLabel")),
82  theME0RecHitLabel(par.getParameter<InputTag>("ME0RecHitLabel")),
83  theService(service) {
84 
85  theCategory = par.getUntrackedParameter<string>("Category", "Muon|RecoMuon|GlobalMuon|GlobalMuonRefitter");
86 
87  theHitThreshold = par.getParameter<int>("HitThreshold");
88  theDTChi2Cut = par.getParameter<double>("Chi2CutDT");
89  theCSCChi2Cut = par.getParameter<double>("Chi2CutCSC");
90  theRPCChi2Cut = par.getParameter<double>("Chi2CutRPC");
91  theGEMChi2Cut = par.getParameter<double>("Chi2CutGEM");
92  theME0Chi2Cut = par.getParameter<double>("Chi2CutME0");
93 
94  // Refit direction
95  string refitDirectionName = par.getParameter<string>("RefitDirection");
96 
97  if (refitDirectionName == "insideOut" ) theRefitDirection = insideOut;
98  else if (refitDirectionName == "outsideIn" ) theRefitDirection = outsideIn;
99  else
100  throw cms::Exception("TrackTransformer constructor")
101  <<"Wrong refit direction chosen in TrackTransformer ParameterSet"
102  << "\n"
103  << "Possible choices are:"
104  << "\n"
105  << "RefitDirection = insideOut or RefitDirection = outsideIn";
106 
107  theFitterName = par.getParameter<string>("Fitter");
108  thePropagatorName = par.getParameter<string>("Propagator");
109 
110  theSkipStation = par.getParameter<int>("SkipStation");
111  theTrackerSkipSystem = par.getParameter<int>("TrackerSkipSystem");
112  theTrackerSkipSection = par.getParameter<int>("TrackerSkipSection");//layer, wheel, or disk depending on the system
113 
114  theTrackerRecHitBuilderName = par.getParameter<string>("TrackerRecHitBuilder");
115  theMuonRecHitBuilderName = par.getParameter<string>("MuonRecHitBuilder");
116 
117  theRPCInTheFit = par.getParameter<bool>("RefitRPCHits");
118 
119  theDYTthrs = par.getParameter< std::vector<int> >("DYTthrs");
120  theDYTselector = par.existsAs<int>("DYTselector")?par.getParameter<int>("DYTselector"):1;
121  theDYTupdator = par.existsAs<bool>("DYTupdator")?par.getParameter<bool>("DYTupdator"):false;
122  theDYTuseAPE = par.existsAs<bool>("DYTuseAPE")?par.getParameter<bool>("DYTuseAPE"):false;
123  dytInfo = new reco::DYTInfo();
124 
125  if (par.existsAs<double>("RescaleErrorFactor")) {
126  theRescaleErrorFactor = par.getParameter<double>("RescaleErrorFactor");
127  edm::LogWarning("GlobalMuonRefitter") << "using error rescale factor " << theRescaleErrorFactor;
128  }
129  else
130  theRescaleErrorFactor = 1000.;
131 
132  theCacheId_TRH = 0;
139 }
140 
141 //--------------
142 // Destructor --
143 //--------------
144 
146  delete dytInfo;
147 }
148 
149 
150 //
151 // set Event
152 //
154 
155  theEvent = &event;
156  event.getByToken(theDTRecHitToken, theDTRecHits);
157  event.getByToken(theCSCRecHitToken, theCSCRecHits);
158  event.getByToken(theGEMRecHitToken, theGEMRecHits);
159  event.getByToken(theME0RecHitToken, theME0RecHits);
160  event.getByToken(CSCSegmentsToken, CSCSegments);
161  event.getByToken(all4DSegmentsToken, all4DSegments);
162 }
163 
164 
166 
168  theService->eventSetup().get<TrajectoryFitter::Record>().get(theFitterName,aFitter);
169  theFitter = aFitter->clone();
170 
171 
172  // Transient Rechit Builders
173  unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
174  if ( newCacheId_TRH != theCacheId_TRH ) {
175  LogDebug(theCategory) << "TransientRecHitRecord changed!";
179  }
180  theFitter->setHitCloner(&hitCloner);
181 
182 }
183 
184 
185 //
186 // build a combined tracker-muon trajectory
187 //
189  const int theMuonHitsOption,
190  const TrackerTopology *tTopo) const {
191  LogTrace(theCategory) << " *** GlobalMuonRefitter *** option " << theMuonHitsOption << endl;
192 
193  ConstRecHitContainer allRecHitsTemp; // all muon rechits temp
194 
195  reco::TransientTrack track(globalTrack,&*(theService->magneticField()),theService->trackingGeometry());
196 
197  auto tkbuilder = static_cast<TkTransientTrackingRecHitBuilder const *>(theTrackerRecHitBuilder.product());
198 
199  for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit)
200  if ((*hit)->isValid()) {
201  if ((*hit)->geographicalId().det() == DetId::Tracker)
202  allRecHitsTemp.push_back((**hit).cloneForFit(*tkbuilder->geometry()->idToDet( (**hit).geographicalId() ) ) );
203  else if ((*hit)->geographicalId().det() == DetId::Muon) {
204  if ((*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit) {
205  LogTrace(theCategory) << "RPC Rec Hit discarged";
206  continue;
207  }
208  allRecHitsTemp.push_back(theMuonRecHitBuilder->build(&**hit));
209  }
210  }
211  vector<Trajectory> refitted = refit(globalTrack,track,allRecHitsTemp,theMuonHitsOption, tTopo);
212  return refitted;
213 }
214 
215 //
216 // build a combined tracker-muon trajectory
217 //
220  const TransientTrackingRecHit::ConstRecHitContainer& allRecHitsTemp,
221  const int theMuonHitsOption,
222  const TrackerTopology *tTopo) const {
223 
224  // MuonHitsOption: 0 - tracker only
225  // 1 - include all muon hits
226  // 2 - include only first muon hit(s)
227  // 3 - include only selected muon hits
228  // 4 - redo pattern recognition with dynamic truncation
229 
230  vector<int> stationHits(4,0);
231  map<DetId, int> hitMap;
232 
233  ConstRecHitContainer allRecHits; // all muon rechits
234  ConstRecHitContainer fmsRecHits; // only first muon rechits
235  ConstRecHitContainer selectedRecHits; // selected muon rechits
236  ConstRecHitContainer DYTRecHits; // rec hits from dynamic truncation algorithm
237 
238  LogTrace(theCategory) << " *** GlobalMuonRefitter *** option " << theMuonHitsOption << endl;
239  LogTrace(theCategory) << " Track momentum before refit: " << globalTrack.pt() << endl;
240  LogTrace(theCategory) << " Hits size before : " << allRecHitsTemp.size() << endl;
241 
242  allRecHits = getRidOfSelectStationHits(allRecHitsTemp, tTopo);
243  // printHits(allRecHits);
244  LogTrace(theCategory) << " Hits size: " << allRecHits.size() << endl;
245 
246  vector <Trajectory> outputTraj;
247 
248  if ((theMuonHitsOption == 1) || (theMuonHitsOption == 3) || (theMuonHitsOption == 4) ) {
249  // refit the full track with all muon hits
250  vector <Trajectory> globalTraj = transform(globalTrack, track, allRecHits);
251 
252  if (globalTraj.empty()) {
253  LogTrace(theCategory) << "No trajectory from the TrackTransformer!" << endl;
254  return vector<Trajectory>();
255  }
256 
257  LogTrace(theCategory) << " Initial trajectory state: "
258  << globalTraj.front().lastMeasurement().updatedState().freeState()->parameters() << endl;
259 
260  if (theMuonHitsOption == 1 )
261  outputTraj.push_back(globalTraj.front());
262 
263  if (theMuonHitsOption == 3 ) {
264  checkMuonHits(globalTrack, allRecHits, hitMap);
265  selectedRecHits = selectMuonHits(globalTraj.front(),hitMap);
266  LogTrace(theCategory) << " Selected hits size: " << selectedRecHits.size() << endl;
267  outputTraj = transform(globalTrack, track, selectedRecHits);
268  }
269 
270  if (theMuonHitsOption == 4 ) {
271  //
272  // DYT 2.0
273  //
275  dytRefit.setProd(all4DSegments, CSCSegments);
276  dytRefit.setSelector(theDYTselector);
277  dytRefit.setThr(theDYTthrs);
278  dytRefit.setUpdateState(theDYTupdator);
279  dytRefit.setUseAPE(theDYTuseAPE);
280  DYTRecHits = dytRefit.filter(globalTraj.front());
281  dytInfo->CopyFrom(dytRefit.getDYTInfo());
282  if ((DYTRecHits.size() > 1) && (DYTRecHits.front()->globalPosition().mag() > DYTRecHits.back()->globalPosition().mag()))
283  stable_sort(DYTRecHits.begin(),DYTRecHits.end(),RecHitLessByDet(alongMomentum));
284  outputTraj = transform(globalTrack, track, DYTRecHits);
285  }
286 
287  } else if (theMuonHitsOption == 2 ) {
288  getFirstHits(globalTrack, allRecHits, fmsRecHits);
289  outputTraj = transform(globalTrack, track, fmsRecHits);
290  }
291 
292 
293  if (!outputTraj.empty()) {
294  LogTrace(theCategory) << "Refitted pt: " << outputTraj.front().firstMeasurement().updatedState().globalParameters().momentum().perp() << endl;
295  return outputTraj;
296  } else {
297  LogTrace(theCategory) << "No refitted Tracks... " << endl;
298  return vector<Trajectory>();
299  }
300 
301 }
302 
303 
304 //
305 //
306 //
309  map<DetId, int> &hitMap) const {
310 
311  LogTrace(theCategory) << " GlobalMuonRefitter::checkMuonHits " << endl;
312 
313  float coneSize = 20.0;
314 
315  // loop through all muon hits and calculate the maximum # of hits in each chamber
316  for (ConstRecHitContainer::const_iterator imrh = all.begin(); imrh != all.end(); imrh++ ) {
317 
318  if ( (*imrh != nullptr ) && !(*imrh)->isValid() ) continue;
319 
320  int detRecHits = 0;
321  MuonRecHitContainer dRecHits;
322 
323  DetId id = (*imrh)->geographicalId();
324  DetId chamberId;
325 
326  // Skip tracker hits
327  if (id.det()!=DetId::Muon) continue;
328 
329  if ( id.subdetId() == MuonSubdetId::DT ) {
330  DTChamberId did(id.rawId());
331  chamberId=did;
332 
333  if ((*imrh)->dimension()>1) {
334  std::vector <const TrackingRecHit*> hits2d = (*imrh)->recHits();
335  for (std::vector <const TrackingRecHit*>::const_iterator hit2d = hits2d.begin(); hit2d!= hits2d.end(); hit2d++) {
336  if ((*hit2d)->dimension()>1) {
337  std::vector <const TrackingRecHit*> hits1d = (*hit2d)->recHits();
338  for (std::vector <const TrackingRecHit*>::const_iterator hit1d = hits1d.begin(); hit1d!= hits1d.end(); hit1d++) {
339  DetId id1 = (*hit1d)->geographicalId();
340  DTLayerId lid(id1.rawId());
341  // Get the 1d DT RechHits from this layer
342  DTRecHitCollection::range dRecHits = theDTRecHits->get(lid);
343  int layerHits=0;
344  for (DTRecHitCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
345  double rhitDistance = fabs(ir->localPosition().x()-(**hit1d).localPosition().x());
346  if ( rhitDistance < coneSize ) layerHits++;
347  LogTrace(theCategory) << " " << (ir)->localPosition() << " " << (**hit1d).localPosition()
348  << " Distance: " << rhitDistance << " recHits: " << layerHits << " SL: " << lid.superLayer() << endl;
349  }
350  if (layerHits>detRecHits) detRecHits=layerHits;
351  }
352  } else {
353  DTLayerId lid(id.rawId());
354  // Get the 1d DT RechHits from this layer
355  DTRecHitCollection::range dRecHits = theDTRecHits->get(lid);
356  for (DTRecHitCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
357  double rhitDistance = fabs(ir->localPosition().x()-(**imrh).localPosition().x());
358  if ( rhitDistance < coneSize ) detRecHits++;
359  LogTrace(theCategory)<< " " << (ir)->localPosition() << " " << (**imrh).localPosition()
360  << " Distance: " << rhitDistance << " recHits: " << detRecHits << endl;
361  }
362  }
363  }
364 
365  } else {
366  DTLayerId lid(id.rawId());
367 
368  // Get the 1d DT RechHits from this layer
369  DTRecHitCollection::range dRecHits = theDTRecHits->get(lid);
370 
371  for (DTRecHitCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
372  double rhitDistance = fabs(ir->localPosition().x()-(**imrh).localPosition().x());
373  if ( rhitDistance < coneSize ) detRecHits++;
374  LogTrace(theCategory) << " " << (ir)->localPosition() << " " << (**imrh).localPosition()
375  << " Distance: " << rhitDistance << " recHits: " << detRecHits << endl;
376  }
377  }
378  }// end of if DT
379  else if ( id.subdetId() == MuonSubdetId::CSC ) {
380  CSCDetId did(id.rawId());
381  chamberId=did.chamberId();
382 
383  if ((*imrh)->recHits().size()>1) {
384  std::vector <const TrackingRecHit*> hits2d = (*imrh)->recHits();
385  for (std::vector <const TrackingRecHit*>::const_iterator hit2d = hits2d.begin(); hit2d!= hits2d.end(); hit2d++) {
386  DetId id1 = (*hit2d)->geographicalId();
387  CSCDetId lid(id1.rawId());
388 
389  // Get the CSC Rechits from this layer
390  CSCRecHit2DCollection::range dRecHits = theCSCRecHits->get(lid);
391  int layerHits=0;
392 
393  for (CSCRecHit2DCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
394  double rhitDistance = (ir->localPosition()-(**hit2d).localPosition()).mag();
395  if ( rhitDistance < coneSize ) layerHits++;
396  LogTrace(theCategory) << ir->localPosition() << " " << (**hit2d).localPosition()
397  << " Distance: " << rhitDistance << " recHits: " << layerHits << endl;
398  }
399  if (layerHits>detRecHits) detRecHits=layerHits;
400  }
401  } else {
402  // Get the CSC Rechits from this layer
403  CSCRecHit2DCollection::range dRecHits = theCSCRecHits->get(did);
404 
405  for (CSCRecHit2DCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
406  double rhitDistance = (ir->localPosition()-(**imrh).localPosition()).mag();
407  if ( rhitDistance < coneSize ) detRecHits++;
408  LogTrace(theCategory) << ir->localPosition() << " " << (**imrh).localPosition()
409  << " Distance: " << rhitDistance << " recHits: " << detRecHits << endl;
410  }
411  }
412  } //end of CSC if
413  else if ( id.subdetId() == MuonSubdetId::GEM ) {
414  GEMDetId did(id.rawId());
415  chamberId=did.chamberId();
416 
417  if ((*imrh)->recHits().size()>1) {
418  std::vector <const TrackingRecHit*> hits2d = (*imrh)->recHits();
419  for (std::vector <const TrackingRecHit*>::const_iterator hit2d = hits2d.begin(); hit2d!= hits2d.end(); hit2d++) {
420  DetId id1 = (*hit2d)->geographicalId();
421  GEMDetId lid(id1.rawId());
422 
423  // Get the GEM Rechits from this layer
424  GEMRecHitCollection::range dRecHits = theGEMRecHits->get(lid);
425  int layerHits=0;
426 
427  for (GEMRecHitCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
428  double rhitDistance = (ir->localPosition()-(**hit2d).localPosition()).mag();
429  if ( rhitDistance < coneSize ) layerHits++;
430  LogTrace(theCategory) << ir->localPosition() << " " << (**hit2d).localPosition()
431  << " Distance: " << rhitDistance << " recHits: " << layerHits << endl;
432  }
433  if (layerHits>detRecHits) detRecHits=layerHits;
434  }
435  } else {
436  // Get the GEM Rechits from this layer
437  GEMRecHitCollection::range dRecHits = theGEMRecHits->get(did);
438 
439  for (GEMRecHitCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
440  double rhitDistance = (ir->localPosition()-(**imrh).localPosition()).mag();
441  if ( rhitDistance < coneSize ) detRecHits++;
442  LogTrace(theCategory) << ir->localPosition() << " " << (**imrh).localPosition()
443  << " Distance: " << rhitDistance << " recHits: " << detRecHits << endl;
444  }
445  }
446  } //end of GEM if
447  else if ( id.subdetId() == MuonSubdetId::ME0 ) {
448  ME0DetId did(id.rawId());
449  chamberId=did.chamberId();
450 
451  if ((*imrh)->recHits().size()>1) {
452  std::vector <const TrackingRecHit*> hits2d = (*imrh)->recHits();
453  for (std::vector <const TrackingRecHit*>::const_iterator hit2d = hits2d.begin(); hit2d!= hits2d.end(); hit2d++) {
454  DetId id1 = (*hit2d)->geographicalId();
455  ME0DetId lid(id1.rawId());
456 
457  // Get the ME0 Rechits from this layer
458  ME0SegmentCollection::range dRecHits = theME0RecHits->get(lid);
459  int layerHits=0;
460 
461  for (ME0SegmentCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
462  double rhitDistance = (ir->localPosition()-(**hit2d).localPosition()).mag();
463  if ( rhitDistance < coneSize ) layerHits++;
464  LogTrace(theCategory) << ir->localPosition() << " " << (**hit2d).localPosition()
465  << " Distance: " << rhitDistance << " recHits: " << layerHits << endl;
466  }
467  if (layerHits>detRecHits) detRecHits=layerHits;
468  }
469  } else {
470  // Get the ME0 Rechits from this layer
471  ME0SegmentCollection::range dRecHits = theME0RecHits->get(did);
472 
473  for (ME0SegmentCollection::const_iterator ir = dRecHits.first; ir != dRecHits.second; ir++ ) {
474  double rhitDistance = (ir->localPosition()-(**imrh).localPosition()).mag();
475  if ( rhitDistance < coneSize ) detRecHits++;
476  LogTrace(theCategory) << ir->localPosition() << " " << (**imrh).localPosition()
477  << " Distance: " << rhitDistance << " recHits: " << detRecHits << endl;
478  }
479  }
480  } //end of ME0 if
481  else {
482  if ( id.subdetId() != MuonSubdetId::RPC ) LogError(theCategory)<<" Wrong Hit Type ";
483  continue;
484  }
485 
486  map<DetId,int>::iterator imap=hitMap.find(chamberId);
487  if (imap!=hitMap.end()) {
488  if (detRecHits>imap->second) imap->second=detRecHits;
489  } else hitMap[chamberId]=detRecHits;
490 
491  } // end of loop over muon rechits
492 
493  for (map<DetId,int>::iterator imap=hitMap.begin(); imap!=hitMap.end(); imap++ )
494  LogTrace(theCategory) << " Station " << imap->first.rawId() << ": " << imap->second <<endl;
495 
496  LogTrace(theCategory) << "CheckMuonHits: "<<all.size();
497 
498  // check order of muon measurements
499  if ( (all.size() > 1) &&
500  ( all.front()->globalPosition().mag() >
501  all.back()->globalPosition().mag() ) ) {
502  LogTrace(theCategory)<< "reverse order: ";
503  stable_sort(all.begin(),all.end(),RecHitLessByDet(alongMomentum));
504  }
505 }
506 
507 
508 //
509 // Get the hits from the first muon station (containing hits)
510 //
513  ConstRecHitContainer& first) const {
514 
515  LogTrace(theCategory) << " GlobalMuonRefitter::getFirstHits\nall rechits length:" << all.size() << endl;
516  first.clear();
517 
518  int station_to_keep = 999;
519  vector<int> stations;
520  for (ConstRecHitContainer::const_iterator ihit = all.begin(); ihit != all.end(); ++ihit) {
521 
522  int station = 0;
523  bool use_it = true;
524  DetId id = (*ihit)->geographicalId();
525  unsigned raw_id = id.rawId();
526  if (!(*ihit)->isValid()) station = -1;
527  else {
528  if (id.det() == DetId::Muon) {
529  switch (id.subdetId()) {
530  case MuonSubdetId::DT: station = DTChamberId(raw_id).station(); break;
531  case MuonSubdetId::CSC: station = CSCDetId(raw_id).station(); break;
532  case MuonSubdetId::GEM: station = GEMDetId(raw_id).station(); break;
533  case MuonSubdetId::ME0: station = ME0DetId(raw_id).station(); break;
534  case MuonSubdetId::RPC: station = RPCDetId(raw_id).station(); use_it = false; break;
535  }
536  }
537  }
538 
539 
540  if (use_it && station > 0 && station < station_to_keep) station_to_keep = station;
541  stations.push_back(station);
542  LogTrace(theCategory) << "rawId: " << raw_id << " station = " << station << " station_to_keep is now " << station_to_keep;
543  }
544 
545  if (station_to_keep <= 0 || station_to_keep > 4 || stations.size() != all.size())
546  LogInfo(theCategory) << "failed to getFirstHits (all muon hits are outliers/bad ?)! station_to_keep = "
547  << station_to_keep << " stations.size " << stations.size() << " all.size " << all.size();
548 
549  for (unsigned i = 0; i < stations.size(); ++i)
550  if (stations[i] >= 0 && stations[i] <= station_to_keep) first.push_back(all[i]);
551 
552  return;
553 }
554 
555 
556 //
557 // select muon hits compatible with trajectory;
558 // check hits in chambers with showers
559 //
562  const map<DetId, int> &hitMap) const {
563 
564  ConstRecHitContainer muonRecHits;
565  const double globalChi2Cut = 200.0;
566 
567  vector<TrajectoryMeasurement> muonMeasurements = traj.measurements();
568 
569  // loop through all muon hits and skip hits with bad chi2 in chambers with high occupancy
570  for (std::vector<TrajectoryMeasurement>::const_iterator im = muonMeasurements.begin(); im != muonMeasurements.end(); im++ ) {
571 
572  if ( !(*im).recHit()->isValid() ) continue;
573  if ( (*im).recHit()->det()->geographicalId().det() != DetId::Muon ) {
574  // if ( ( chi2ndf < globalChi2Cut ) )
575  muonRecHits.push_back((*im).recHit());
576  continue;
577  }
578  const MuonTransientTrackingRecHit* immrh = dynamic_cast<const MuonTransientTrackingRecHit*>((*im).recHit().get());
579 
580  DetId id = immrh->geographicalId();
581  DetId chamberId;
582  int threshold = 0;
583  double chi2Cut = 0.0;
584 
585  // get station of hit if it is in DT
586  if ( (*immrh).isDT() ) {
587  DTChamberId did(id.rawId());
588  chamberId = did;
589  threshold = theHitThreshold;
590  chi2Cut = theDTChi2Cut;
591  }
592  // get station of hit if it is in CSC
593  else if ( (*immrh).isCSC() ) {
594  CSCDetId did(id.rawId());
595  chamberId = did.chamberId();
596  threshold = theHitThreshold;
597  chi2Cut = theCSCChi2Cut;
598  }
599  // get station of hit if it is in GEM
600  else if ( (*immrh).isGEM() ) {
601  GEMDetId did(id.rawId());
602  chamberId = did.chamberId();
603  threshold = theHitThreshold;
604  chi2Cut = theGEMChi2Cut;
605  }
606  // get station of hit if it is in ME0
607  else if ( (*immrh).isME0() ) {
608  ME0DetId did(id.rawId());
609  chamberId = did.chamberId();
610  threshold = theHitThreshold;
611  chi2Cut = theME0Chi2Cut;
612  }
613  // get station of hit if it is in RPC
614  else if ( (*immrh).isRPC() ) {
615  RPCDetId rpcid(id.rawId());
616  chamberId = rpcid;
617  threshold = theHitThreshold;
618  chi2Cut = theRPCChi2Cut;
619  } else
620  continue;
621 
622  double chi2ndf = (*im).estimate()/(*im).recHit()->dimension();
623 
624  bool keep = true;
625  map<DetId,int>::const_iterator imap=hitMap.find(chamberId);
626  if ( imap!=hitMap.end() )
627  if (imap->second>threshold) keep = false;
628 
629  if ( (keep || (chi2ndf<chi2Cut)) && (chi2ndf<globalChi2Cut) ) {
630  muonRecHits.push_back((*im).recHit());
631  } else {
633  << "Skip hit: " << id.rawId() << " chi2="
634  << chi2ndf << " ( threshold: " << chi2Cut << ") Det: "
635  << imap->second << endl;
636  }
637  }
638 
639  // check order of rechits
640  reverse(muonRecHits.begin(),muonRecHits.end());
641  return muonRecHits;
642 }
643 
644 
645 //
646 // print RecHits
647 //
649 
650  LogTrace(theCategory) << "Used RecHits: " << hits.size();
651  for (ConstRecHitContainer::const_iterator ir = hits.begin(); ir != hits.end(); ir++ ) {
652  if ( !(*ir)->isValid() ) {
653  LogTrace(theCategory) << "invalid RecHit";
654  continue;
655  }
656 
657  const GlobalPoint& pos = (*ir)->globalPosition();
658 
660  << "r = " << sqrt(pos.x() * pos.x() + pos.y() * pos.y())
661  << " z = " << pos.z()
662  << " dimension = " << (*ir)->dimension()
663  << " det = " << (*ir)->det()->geographicalId().det()
664  << " subdet = " << (*ir)->det()->subDetector()
665  << " raw id = " << (*ir)->det()->geographicalId().rawId();
666  }
667 
668 }
669 
670 
671 //
672 // add Trajectory* to TrackCand if not already present
673 //
676 
677  if (!recHits.empty()){
678  ConstRecHitContainer::const_iterator frontHit = recHits.begin();
679  ConstRecHitContainer::const_iterator backHit = recHits.end() - 1;
680  while( !(*frontHit)->isValid() && frontHit != backHit) {frontHit++;}
681  while( !(*backHit)->isValid() && backHit != frontHit) {backHit--;}
682 
683  double rFirst = (*frontHit)->globalPosition().mag();
684  double rLast = (*backHit) ->globalPosition().mag();
685 
686  if(rFirst < rLast) return insideOut;
687  else if(rFirst > rLast) return outsideIn;
688  else {
689  LogError(theCategory) << "Impossible determine the rechits order" <<endl;
690  return undetermined;
691  }
692  } else {
693  LogError(theCategory) << "Impossible determine the rechits order" <<endl;
694  return undetermined;
695  }
696 }
697 
698 
699 //
700 // Convert Tracks into Trajectories with a given set of hits
701 //
702 vector<Trajectory> GlobalMuonRefitter::transform(const reco::Track& newTrack,
704  const TransientTrackingRecHit::ConstRecHitContainer& urecHitsForReFit) const {
705 
706  TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = urecHitsForReFit;
707  LogTrace(theCategory) << "GlobalMuonRefitter::transform: " << recHitsForReFit.size() << " hits:";
708  printHits(recHitsForReFit);
709 
710  if(recHitsForReFit.size() < 2) return vector<Trajectory>();
711 
712  // Check the order of the rechits
713  RefitDirection recHitsOrder = checkRecHitsOrdering(recHitsForReFit);
714 
715  LogTrace(theCategory) << "checkRecHitsOrdering() returned " << recHitsOrder
716  << ", theRefitDirection is " << theRefitDirection
717  << " (insideOut == " << insideOut << ", outsideIn == " << outsideIn << ")";
718 
719  // Reverse the order in the case of inconsistency between the fit direction and the rechit order
720  if(theRefitDirection != recHitsOrder) reverse(recHitsForReFit.begin(),recHitsForReFit.end());
721 
722  // Even though we checked the rechits' ordering above, we may have
723  // already flipped them elsewhere (getFirstHits() is such a
724  // culprit). Use the global positions of the states and the desired
725  // refit direction to find the starting TSOS.
726  TrajectoryStateOnSurface firstTSOS, lastTSOS;
727  unsigned int innerId; //UNUSED: outerId;
728  bool order_swapped = track.outermostMeasurementState().globalPosition().mag() < track.innermostMeasurementState().globalPosition().mag();
729  bool inner_is_first;
730  LogTrace(theCategory) << "order swapped? " << order_swapped;
731 
732  // Fill the starting state, depending on the ordering above.
733  if ((theRefitDirection == insideOut && !order_swapped) || (theRefitDirection == outsideIn && order_swapped)) {
734  innerId = newTrack.innerDetId();
735  //UNUSED: outerId = newTrack.outerDetId();
736  firstTSOS = track.innermostMeasurementState();
737  lastTSOS = track.outermostMeasurementState();
738  inner_is_first = true;
739  }
740  else {
741  innerId = newTrack.outerDetId();
742  //UNUSED: outerId = newTrack.innerDetId();
743  firstTSOS = track.outermostMeasurementState();
744  lastTSOS = track.innermostMeasurementState();
745  inner_is_first = false;
746  }
747 
748  LogTrace(theCategory) << "firstTSOS: inner_is_first? " << inner_is_first
749  << " globalPosition is " << firstTSOS.globalPosition()
750  << " innerId is " << innerId;
751 
752  if(!firstTSOS.isValid()){
753  LogWarning(theCategory) << "Error wrong initial state!" << endl;
754  return vector<Trajectory>();
755  }
756 
757  firstTSOS.rescaleError(theRescaleErrorFactor);
758 
759  // This is the only way to get a TrajectorySeed with settable propagation direction
760  PTrajectoryStateOnDet garbage1;
762 
763  // These lines cause the code to ignore completely what was set
764  // above, and force propDir for tracks from collisions!
765 // if(propDir == alongMomentum && theRefitDirection == outsideIn) propDir=oppositeToMomentum;
766 // if(propDir == oppositeToMomentum && theRefitDirection == insideOut) propDir=alongMomentum;
767 
768  const TrajectoryStateOnSurface& tsosForDir = inner_is_first ? lastTSOS : firstTSOS;
770  LogTrace(theCategory) << "propDir based on firstTSOS x dot p is " << propDir
771  << " (alongMomentum == " << alongMomentum << ", oppositeToMomentum == " << oppositeToMomentum << ")";
772 
773  // Additional propagation diretcion determination logic for cosmic muons
774  if (theCosmicFlag) {
775  PropagationDirection propDir_first = (firstTSOS.globalPosition().basicVector().dot(firstTSOS.globalMomentum().basicVector()) > 0) ? alongMomentum : oppositeToMomentum;
776  PropagationDirection propDir_last = (lastTSOS .globalPosition().basicVector().dot(lastTSOS .globalMomentum().basicVector()) > 0) ? alongMomentum : oppositeToMomentum;
777  LogTrace(theCategory) << "propDir_first " << propDir_first << ", propdir_last " << propDir_last
778  << " : they " << (propDir_first == propDir_last ? "agree" : "disagree");
779 
780  int y_count = 0;
781  for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator it = recHitsForReFit.begin(); it != recHitsForReFit.end(); ++it) {
782  if ((*it)->globalPosition().y() > 0) ++y_count;
783  else --y_count;
784  }
785 
786  PropagationDirection propDir_ycount = alongMomentum;
787  if (y_count > 0) {
788  if (theRefitDirection == insideOut) propDir_ycount = oppositeToMomentum;
789  else if (theRefitDirection == outsideIn) propDir_ycount = alongMomentum;
790  }
791  else {
792  if (theRefitDirection == insideOut) propDir_ycount = alongMomentum;
793  else if (theRefitDirection == outsideIn) propDir_ycount = oppositeToMomentum;
794  }
795 
796  LogTrace(theCategory) << "y_count = " << y_count
797  << "; based on geometrically-outermost TSOS, propDir is " << propDir << ": "
798  << (propDir == propDir_ycount ? "agrees" : "disagrees")
799  << " with ycount determination";
800 
801  if (propDir_first != propDir_last) {
802  LogTrace(theCategory) << "since first/last disagreed, using y_count propDir";
803  propDir = propDir_ycount;
804  }
805  }
806 
807  TrajectorySeed seed(garbage1,garbage2,propDir);
808 
809  if(recHitsForReFit.front()->geographicalId() != DetId(innerId)){
810  LogDebug(theCategory)<<"Propagation occured"<<endl;
811  LogTrace(theCategory) << "propagating firstTSOS at " << firstTSOS.globalPosition()
812  << " to first rechit with surface pos " << recHitsForReFit.front()->det()->surface().toGlobal(LocalPoint(0,0,0));
813  firstTSOS = theService->propagator(thePropagatorName)->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
814  if(!firstTSOS.isValid()){
815  LogDebug(theCategory)<<"Propagation error!"<<endl;
816  return vector<Trajectory>();
817  }
818  }
819 
820 /*
821  cout << " GlobalMuonRefitter : theFitter " << propDir << endl;
822  cout << " First TSOS: "
823  << firstTSOS.globalPosition() << " p="
824  << firstTSOS.globalMomentum() << " = "
825  << firstTSOS.globalMomentum().mag() << endl;
826 
827  cout << " Starting seed: "
828  << " nHits= " << seed.nHits()
829  << " tsos: "
830  << seed.startingState().parameters().position() << " p="
831  << seed.startingState().parameters().momentum() << endl;
832 
833  cout << " RecHits: "
834  << recHitsForReFit.size() << endl;
835 */
836 
837  vector<Trajectory> trajectories = theFitter->fit(seed,recHitsForReFit,firstTSOS);
838 
839  if(trajectories.empty()){
840  LogDebug(theCategory) << "No Track refitted!" << endl;
841  return vector<Trajectory>();
842  }
843  return trajectories;
844 }
845 
846 
847 //
848 // Remove Selected Station Rec Hits
849 //
851  const TrackerTopology *tTopo) const
852 {
854  ConstRecHitContainer::const_iterator it = hits.begin();
855  for (; it!=hits.end(); it++) {
856 
857  DetId id = (*it)->geographicalId();
858 
859  //Check that this is a Muon hit that we're toying with -- else pass on this because the hacker is a moron / not careful
860 
861  if (id.det() == DetId::Tracker && theTrackerSkipSystem > 0) {
862  int layer = -999;
863  int disk = -999;
864  int wheel = -999;
865  if ( id.subdetId() == theTrackerSkipSystem){
866  // continue; //caveat that just removes the whole system from refitting
867 
868  if (theTrackerSkipSystem == PXB) {
869 
870  layer = tTopo->pxbLayer(id);
871  }
872  if (theTrackerSkipSystem == TIB) {
873 
874  layer = tTopo->tibLayer(id);
875  }
876 
877  if (theTrackerSkipSystem == TOB) {
878 
879  layer = tTopo->tobLayer(id);
880  }
881  if (theTrackerSkipSystem == PXF) {
882 
883  disk = tTopo->pxfDisk(id);
884  }
885  if (theTrackerSkipSystem == TID) {
886 
887  wheel = tTopo->tidWheel(id);
888  }
889  if (theTrackerSkipSystem == TEC) {
890 
891  wheel = tTopo->tecWheel(id);
892  }
893  if (theTrackerSkipSection >= 0 && layer == theTrackerSkipSection) continue;
894  if (theTrackerSkipSection >= 0 && disk == theTrackerSkipSection) continue;
895  if (theTrackerSkipSection >= 0 && wheel == theTrackerSkipSection) continue;
896  }
897  }
898 
899  if (id.det() == DetId::Muon && theSkipStation) {
900  int station = -999;
901  //UNUSED: int wheel = -999;
902  if ( id.subdetId() == MuonSubdetId::DT ) {
903  DTChamberId did(id.rawId());
904  station = did.station();
905  //UNUSED: wheel = did.wheel();
906  } else if ( id.subdetId() == MuonSubdetId::CSC ) {
907  CSCDetId did(id.rawId());
908  station = did.station();
909  } else if ( id.subdetId() == MuonSubdetId::GEM ) {
910  GEMDetId did(id.rawId());
911  station = did.station();
912  } else if ( id.subdetId() == MuonSubdetId::ME0 ) {
913  ME0DetId did(id.rawId());
914  station = did.station();
915  } else if ( id.subdetId() == MuonSubdetId::RPC ) {
916  RPCDetId rpcid(id.rawId());
917  station = rpcid.station();
918  }
919  if(station == theSkipStation) continue;
920  }
921  results.push_back(*it);
922  }
923  return results;
924 }
925 
#define LogDebug(id)
std::string theMuonRecHitBuilderName
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void printHits(const ConstRecHitContainer &) const
print all RecHits of a trajectory
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
edm::Handle< DTRecHitCollection > theDTRecHits
void setProd(const edm::Handle< DTRecSegment4DCollection > &DTSegProd, const edm::Handle< CSCSegmentCollection > &CSCSegProd)
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:32
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
const MuonServiceProxy * theService
unsigned int tibLayer(const DetId &id) const
RefitDirection theRefitDirection
edm::ESHandle< TransientTrackingRecHitBuilder > theTrackerRecHitBuilder
edm::Handle< CSCSegmentCollection > CSCSegments
ME0DetId chamberId() const
Return the corresponding ChamberId (mask layers)
Definition: ME0DetId.h:67
std::string thePropagatorName
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
unsigned int pxfDisk(const DetId &id) const
static const int GEM
Definition: MuonSubdetId.h:15
T y() const
Definition: PV3DBase.h:63
edm::Handle< GEMRecHitCollection > theGEMRecHits
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
GlobalPoint globalPosition() const
void setServices(const edm::EventSetup &)
set the services needed by the TrackTransformer
unsigned int tidWheel(const DetId &id) const
void CopyFrom(const DYTInfo &)
copy from another DYTInfo
Definition: DYTInfo.cc:15
PropagationDirection
TransientTrackingRecHit::ConstRecHitContainer ConstRecHitContainer
edm::EDGetTokenT< CSCSegmentCollection > CSCSegmentsToken
int station() const
Definition: ME0DetId.h:76
std::unique_ptr< TrajectoryFitter > theFitter
edm::ESHandle< TransientTrackingRecHitBuilder > theMuonRecHitBuilder
edm::Handle< ME0SegmentCollection > theME0RecHits
void checkMuonHits(const reco::Track &, ConstRecHitContainer &, std::map< DetId, int > &) const
check muon RecHits, calculate chamber occupancy and select hits to be used in the final fit ...
TrajectoryStateOnSurface innermostMeasurementState() const
std::vector< int > theDYTthrs
GEMDetId chamberId() const
Return the corresponding ChamberId.
Definition: GEMDetId.h:85
void getFirstHits(const reco::Track &, ConstRecHitContainer &, ConstRecHitContainer &) const
get the RecHits in the tracker and the first muon chamber with hits
const edm::Event * theEvent
uint32_t rawId() const
get the raw id
Definition: DetId.h:44
DataContainer const & measurements() const
Definition: Trajectory.h:196
const int keep
virtual std::unique_ptr< TrajectoryFitter > clone() const =0
static const int ME0
Definition: MuonSubdetId.h:16
static const int CSC
Definition: MuonSubdetId.h:13
T mag() const
Definition: PV3DBase.h:67
edm::Handle< DTRecSegment4DCollection > all4DSegments
ConstRecHitContainer selectMuonHits(const Trajectory &, const std::map< DetId, int > &) const
select muon hits compatible with trajectory; check hits in chambers with showers
ConstRecHitContainer getRidOfSelectStationHits(const ConstRecHitContainer &hits, const TrackerTopology *tTopo) const
T sqrt(T t)
Definition: SSEVec.h:18
double pt() const
track transverse momentum
Definition: TrackBase.h:621
T z() const
Definition: PV3DBase.h:64
MuonTransientTrackingRecHit::MuonRecHitContainer MuonRecHitContainer
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Definition: Track.h:94
int station() const
Station id : the station is the pair of chambers at same disk.
Definition: GEMDetId.h:64
edm::EDGetTokenT< ME0SegmentCollection > theME0RecHitToken
CSCDetId chamberId() const
Definition: CSCDetId.h:53
edm::Handle< CSCRecHit2DCollection > theCSCRecHits
RefitDirection checkRecHitsOrdering(const ConstRecHitContainer &) const
edm::EDGetTokenT< DTRecHitCollection > theDTRecHitToken
edm::EDGetTokenT< CSCRecHit2DCollection > theCSCRecHitToken
TrajectoryStateOnSurface outermostMeasurementState() const
TransientTrackingRecHit::ConstRecHitContainer filter(const Trajectory &)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
#define LogTrace(id)
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
edm::InputTag theDTRecHitLabel
std::vector< ConstRecHitPointer > ConstRecHitContainer
unsigned int pxbLayer(const DetId &id) const
edm::InputTag theGEMRecHitLabel
virtual void setEvent(const edm::Event &)
pass the Event to the algo at each event
Definition: DetId.h:18
edm::InputTag theME0RecHitLabel
GlobalMuonRefitter(const edm::ParameterSet &, const MuonServiceProxy *, edm::ConsumesCollector &)
constructor with Parameter Set and MuonServiceProxy
edm::EDGetTokenT< DTRecSegment4DCollection > all4DSegmentsToken
const T & get() const
Definition: EventSetup.h:59
reco::DYTInfo getDYTInfo()
reco::DYTInfo * dytInfo
HLT enums.
GlobalVector globalMomentum() const
static const int RPC
Definition: MuonSubdetId.h:14
int station() const
Definition: CSCDetId.h:86
static const int DT
Definition: MuonSubdetId.h:12
virtual ~GlobalMuonRefitter()
destructor
DetId geographicalId() const
unsigned long long theCacheId_TRH
int station() const
Return the station number.
Definition: DTChamberId.h:51
T x() const
Definition: PV3DBase.h:62
std::string theTrackerRecHitBuilderName
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
unsigned int tecWheel(const DetId &id) const
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
Definition: Track.h:99
T const * product() const
Definition: ESHandle.h:86
void setThr(const std::vector< int > &)
std::vector< Trajectory > refit(const reco::Track &globalTrack, const int theMuonHitsOption, const TrackerTopology *tTopo) const
build combined trajectory from sta Track and tracker RecHits
unsigned int tobLayer(const DetId &id) const
Definition: event.py:1
edm::InputTag theCSCRecHitLabel
edm::EDGetTokenT< GEMRecHitCollection > theGEMRecHitToken
std::vector< Trajectory > transform(const reco::Track &newTrack, const reco::TransientTrack track, const TransientTrackingRecHit::ConstRecHitContainer &recHitsForReFit) const
refit the track with a new set of RecHits
T dot(const Basic3DVector &rh) const
Scalar product, or "dot" product, with a vector of same type.
int station() const
Definition: RPCDetId.h:96