CMS 3D CMS Logo

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