Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-15 05:17:37

0001 #include "TrackingTools/TrackRefitter/interface/TrackTransformerForGlobalCosmicMuons.h"
0002 
0003 #include "DataFormats/DetId/interface/DetId.h"
0004 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0005 #include "DataFormats/MuonDetId/interface/DTWireId.h"
0006 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0007 #include "DataFormats/MuonDetId/interface/RPCDetId.h"
0008 #include "DataFormats/TrackReco/interface/Track.h"
0009 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0010 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0015 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
0016 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0017 #include "TrackingTools/PatternTools/interface/TrajectorySmoother.h"
0018 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0019 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
0020 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
0021 
0022 using namespace std;
0023 using namespace edm;
0024 
0025 /// Constructor
0026 TrackTransformerForGlobalCosmicMuons::TrackTransformerForGlobalCosmicMuons(const ParameterSet& parameterSet,
0027                                                                            edm::ConsumesCollector iC)
0028     : theIOpropToken(iC.esConsumes(edm::ESInputTag("", "SmartPropagatorRK"))),
0029       theOIpropToken(iC.esConsumes(edm::ESInputTag("", "SmartPropagatorRKOpposite"))),
0030       thGlobTrackGeoToken(iC.esConsumes()),
0031       theMFToken(iC.esConsumes()),
0032       theIOFitterToken(iC.esConsumes(edm::ESInputTag("", "KFFitterForRefitInsideOut"))),
0033       theOIFitterToken(iC.esConsumes(edm::ESInputTag("", "KFSmootherForRefitInsideOut"))),
0034       theIOSmootherToken(iC.esConsumes(edm::ESInputTag("", "KFFitterForRefitOutsideIn"))),
0035       theOISmootherToken(iC.esConsumes(edm::ESInputTag("", "KFSmootherForRefitOutsideIn"))),
0036       theTkRecHitBuildToken(
0037           iC.esConsumes(edm::ESInputTag("", parameterSet.getParameter<string>("TrackerRecHitBuilder")))),
0038       theMuonRecHitBuildToken(
0039           iC.esConsumes(edm::ESInputTag("", parameterSet.getParameter<string>("MuonRecHitBuilder")))),
0040       theTopologyToken(iC.esConsumes()) {
0041   theRPCInTheFit = parameterSet.getParameter<bool>("RefitRPCHits");
0042   theCacheId_TC = theCacheId_GTG = theCacheId_MG = theCacheId_TRH = 0;
0043   theSkipStationDT = parameterSet.getParameter<int>("SkipStationDT");
0044   theSkipStationCSC = parameterSet.getParameter<int>("SkipStationCSC");
0045   theSkipWheelDT = parameterSet.getParameter<int>("SkipWheelDT");
0046   theTrackerSkipSystem = parameterSet.getParameter<int>("TrackerSkipSystem");
0047   theTrackerSkipSection =
0048       parameterSet.getParameter<int>("TrackerSkipSection");  //layer, wheel, or disk depending on the system
0049 }
0050 
0051 /// Destructor
0052 TrackTransformerForGlobalCosmicMuons::~TrackTransformerForGlobalCosmicMuons() {}
0053 
0054 void TrackTransformerForGlobalCosmicMuons::setServices(const EventSetup& setup) {
0055   const std::string metname = "Reco|TrackingTools|TrackTransformer";
0056 
0057   theFitterIO = setup.getHandle(theIOFitterToken);
0058   theFitterOI = setup.getHandle(theOIFitterToken);
0059   theSmootherIO = setup.getHandle(theIOSmootherToken);
0060   theSmootherOI = setup.getHandle(theOISmootherToken);
0061 
0062   unsigned long long newCacheId_TC = setup.get<TrackingComponentsRecord>().cacheIdentifier();
0063 
0064   if (newCacheId_TC != theCacheId_TC) {
0065     LogTrace(metname) << "Tracking Component changed!";
0066     theCacheId_TC = newCacheId_TC;
0067     thePropagatorIO = setup.getHandle(theIOpropToken);
0068     thePropagatorOI = setup.getHandle(theOIpropToken);
0069   }
0070 
0071   // Global Tracking Geometry
0072   unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
0073   if (newCacheId_GTG != theCacheId_GTG) {
0074     LogTrace(metname) << "GlobalTrackingGeometry changed!";
0075     theCacheId_GTG = newCacheId_GTG;
0076     theTrackingGeometry = setup.getHandle(thGlobTrackGeoToken);
0077   }
0078 
0079   // Magfield Field
0080   unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
0081   if (newCacheId_MG != theCacheId_MG) {
0082     LogTrace(metname) << "Magnetic Field changed!";
0083     theCacheId_MG = newCacheId_MG;
0084     theMGField = setup.getHandle(theMFToken);
0085   }
0086 
0087   // Transient Rechit Builders
0088   unsigned long long newCacheId_TRH = setup.get<TransientRecHitRecord>().cacheIdentifier();
0089   if (newCacheId_TRH != theCacheId_TRH) {
0090     theCacheId_TRH = newCacheId_TRH;
0091     LogTrace(metname) << "TransientRecHitRecord changed!";
0092     theTrackerRecHitBuilder = setup.getHandle(theTkRecHitBuildToken);
0093     theMuonRecHitBuilder = setup.getHandle(theMuonRecHitBuildToken);
0094   }
0095 
0096   //Retrieve tracker topology from geometry
0097   tTopo_ = &setup.getData(theTopologyToken);
0098 }
0099 
0100 TransientTrackingRecHit::ConstRecHitContainer TrackTransformerForGlobalCosmicMuons::getTransientRecHits(
0101     const reco::TransientTrack& track) const {
0102   TransientTrackingRecHit::ConstRecHitContainer tkHits;
0103   TransientTrackingRecHit::ConstRecHitContainer staHits;
0104 
0105   for (trackingRecHit_iterator hit = track.recHitsBegin(); hit != track.recHitsEnd(); ++hit) {
0106     if ((*hit)->isValid()) {
0107       if ((*hit)->geographicalId().det() == DetId::Tracker && TrackerKeep((*hit)->geographicalId())) {
0108         tkHits.push_back(theTrackerRecHitBuilder->build(&**hit));
0109       } else if ((*hit)->geographicalId().det() == DetId::Muon && MuonKeep((*hit)->geographicalId())) {
0110         if ((*hit)->geographicalId().subdetId() == 3 && !theRPCInTheFit) {
0111           LogTrace("Reco|TrackingTools|TrackTransformer") << "RPC Rec Hit discarged";
0112           continue;
0113         }
0114         staHits.push_back(theMuonRecHitBuilder->build(&**hit));
0115       }
0116     }
0117   }
0118 
0119   if (staHits.empty())
0120     return staHits;
0121 
0122   /*
0123   bool up = staHits.front()->globalPosition().y()>0 ? true : false;
0124 
0125   if(up){
0126     reverse(staHits.begin(),staHits.end());
0127     reverse(tkHits.begin(),tkHits.end());
0128   }
0129 */
0130   copy(staHits.begin(), staHits.end(), back_inserter(tkHits));
0131 
0132   for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator hit = tkHits.begin(); hit != tkHits.end(); ++hit) {
0133     DetId hitId = (*hit)->geographicalId();
0134     GlobalPoint glbpoint = trackingGeometry()->idToDet(hitId)->position();
0135 
0136     if (hitId.det() == DetId::Tracker) {
0137       if (hitId.subdetId() == StripSubdetector::TIB)
0138         LogTrace("TrackFitters") << glbpoint << " I am TIB " << tTopo_->tibLayer(hitId);
0139       else if (hitId.subdetId() == StripSubdetector::TOB)
0140         LogTrace("TrackFitters") << glbpoint << " I am TOB " << tTopo_->tobLayer(hitId);
0141       else if (hitId.subdetId() == StripSubdetector::TEC)
0142         LogTrace("TrackFitters") << glbpoint << " I am TEC " << tTopo_->tecWheel(hitId);
0143       else if (hitId.subdetId() == StripSubdetector::TID)
0144         LogTrace("TrackFitters") << glbpoint << " I am TID " << tTopo_->tidWheel(hitId);
0145       else if (hitId.subdetId() == (int)PixelSubdetector::PixelBarrel)
0146         LogTrace("TrackFitters") << glbpoint << " I am PixBar " << tTopo_->pxbLayer(hitId);
0147       else if (hitId.subdetId() == (int)PixelSubdetector::PixelEndcap)
0148         LogTrace("TrackFitters") << glbpoint << " I am PixFwd " << tTopo_->pxfDisk(hitId);
0149       else
0150         LogTrace("TrackFitters") << " UNKNOWN TRACKER HIT TYPE ";
0151     } else if (hitId.det() == DetId::Muon) {
0152       if (hitId.subdetId() == MuonSubdetId::DT)
0153         LogTrace("TrackFitters") << glbpoint << " I am DT " << DTWireId(hitId);
0154       else if (hitId.subdetId() == MuonSubdetId::CSC)
0155         LogTrace("TrackFitters") << glbpoint << " I am CSC " << CSCDetId(hitId);
0156       else if (hitId.subdetId() == MuonSubdetId::RPC)
0157         LogTrace("TrackFitters") << glbpoint << " I am RPC " << RPCDetId(hitId);
0158       else
0159         LogTrace("TrackFitters") << " UNKNOWN MUON HIT TYPE ";
0160     } else
0161       LogTrace("TrackFitters") << " UNKNOWN HIT TYPE ";
0162   }
0163 
0164   return tkHits;
0165 }
0166 
0167 /// the refitter used to refit the reco::Track
0168 ESHandle<TrajectoryFitter> TrackTransformerForGlobalCosmicMuons::fitter(bool up) const {
0169   if (up)
0170     return theFitterOI;
0171   else
0172     return theFitterIO;
0173 }
0174 
0175 /// the smoother used to smooth the trajectory which came from the refitting step
0176 ESHandle<TrajectorySmoother> TrackTransformerForGlobalCosmicMuons::smoother(bool up) const {
0177   if (up)
0178     return theSmootherOI;
0179   else
0180     return theSmootherIO;
0181 }
0182 
0183 ESHandle<Propagator> TrackTransformerForGlobalCosmicMuons::propagator(bool up) const {
0184   if (up)
0185     return thePropagatorIO;
0186   else
0187     return thePropagatorOI;
0188 }
0189 
0190 /// Convert Tracks into Trajectories
0191 vector<Trajectory> TrackTransformerForGlobalCosmicMuons::transform(const reco::Track& tr) const {
0192   const std::string metname = "Reco|TrackingTools|TrackTransformer";
0193 
0194   reco::TransientTrack track(tr, magneticField(), trackingGeometry());
0195 
0196   // Build the transient Rechits
0197   TransientTrackingRecHit::ConstRecHitContainer recHitsForReFit = getTransientRecHits(track);
0198 
0199   if (recHitsForReFit.size() < 2)
0200     return vector<Trajectory>();
0201 
0202   bool up = recHitsForReFit.back()->globalPosition().y() > 0 ? true : false;
0203   LogTrace(metname) << "Up ? " << up;
0204 
0205   PropagationDirection propagationDirection = up ? oppositeToMomentum : alongMomentum;
0206   TrajectoryStateOnSurface firstTSOS = up ? track.outermostMeasurementState() : track.innermostMeasurementState();
0207   unsigned int innerId = up ? track.track().outerDetId() : track.track().innerDetId();
0208 
0209   LogTrace(metname) << "Prop Dir: " << propagationDirection << " FirstId " << innerId << " firstTSOS " << firstTSOS;
0210 
0211   TrajectorySeed seed({}, {}, propagationDirection);
0212 
0213   if (recHitsForReFit.front()->geographicalId() != DetId(innerId)) {
0214     LogTrace(metname) << "Propagation occurring" << endl;
0215     firstTSOS = propagator(up)->propagate(firstTSOS, recHitsForReFit.front()->det()->surface());
0216     LogTrace(metname) << "Final destination: " << recHitsForReFit.front()->det()->surface().position() << endl;
0217     if (!firstTSOS.isValid()) {
0218       LogTrace(metname) << "Propagation error!" << endl;
0219       return vector<Trajectory>();
0220     }
0221   }
0222 
0223   vector<Trajectory> trajectories = fitter(up)->fit(seed, recHitsForReFit, firstTSOS);
0224 
0225   if (trajectories.empty()) {
0226     LogTrace(metname) << "No Track refitted!" << endl;
0227     return vector<Trajectory>();
0228   }
0229 
0230   Trajectory trajectoryBW = trajectories.front();
0231 
0232   vector<Trajectory> trajectoriesSM = smoother(up)->trajectories(trajectoryBW);
0233 
0234   if (trajectoriesSM.empty()) {
0235     LogTrace(metname) << "No Track smoothed!" << endl;
0236     return vector<Trajectory>();
0237   }
0238 
0239   return trajectoriesSM;
0240 }
0241 
0242 //
0243 // Selection for Tracker Hits
0244 //
0245 bool TrackTransformerForGlobalCosmicMuons::TrackerKeep(DetId id) const {
0246   if (id.det() != DetId::Tracker)
0247     return false;
0248   if (theTrackerSkipSystem < 0)
0249     return true;
0250   bool retVal = true;
0251 
0252   int layer = -999;
0253 
0254   if (id.subdetId() == theTrackerSkipSystem)
0255     layer = tTopo_->layer(id);
0256 
0257   if (theTrackerSkipSection > -998 && layer == theTrackerSkipSection)
0258     retVal = false;
0259 
0260   return retVal;
0261 }
0262 //
0263 // Selection for Muon hits
0264 //
0265 bool TrackTransformerForGlobalCosmicMuons::MuonKeep(DetId id) const {
0266   if (id.det() != DetId::Muon)
0267     return false;
0268   if (theSkipStationDT < 0 && theSkipStationCSC < 0)
0269     return true;
0270 
0271   int station = -999;
0272   int wheel = -999;
0273   bool isRPC = false;
0274   bool isDT = false;
0275   bool isCSC = false;
0276 
0277   if (id.subdetId() == MuonSubdetId::DT) {
0278     DTChamberId did(id.rawId());
0279     station = did.station();
0280     wheel = did.wheel();
0281     isDT = true;
0282   } else if (id.subdetId() == MuonSubdetId::CSC) {
0283     CSCDetId did(id.rawId());
0284     station = did.station();
0285     isCSC = true;
0286   } else if (id.subdetId() == MuonSubdetId::RPC) {
0287     RPCDetId rpcid(id.rawId());
0288     station = rpcid.station();
0289     isRPC = true;
0290   }
0291 
0292   if (isRPC && (station == theSkipStationCSC || station == theSkipStationDT))
0293     return false;
0294   if (isDT && station == theSkipStationDT)
0295     return false;
0296   if (isCSC && station == theSkipStationCSC)
0297     return false;
0298 
0299   if (isDT && theSkipWheelDT > -998 && wheel == theSkipWheelDT)
0300     return false;
0301 
0302   return true;
0303 }