CMS 3D CMS Logo

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