Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-27 02:53:22

0001 // -*- C++ -*-
0002 //
0003 // Package:    TrackerToMuonPropagator
0004 // Class:      TrackerToMuonPropagator
0005 //
0006 /**\class TrackerToMuonPropagator TrackerToMuonPropagator.cc Alignment/TrackerToMuonPropagator/src/TrackerToMuonPropagator.cc
0007 
0008  Description: <one line class summary>
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Jim Pivarski
0015 //         Created:  Wed Dec 12 13:31:55 CST 2007
0016 // $Id: TrackerToMuonPropagator.cc,v 1.4 2010/01/04 15:36:54 mussgill Exp $
0017 //
0018 //
0019 
0020 // system include files
0021 #include <memory>
0022 
0023 // user include files
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/stream/EDProducer.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/EventSetup.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 #include "FWCore/Framework/interface/ConsumesCollector.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 
0032 // products
0033 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0034 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0035 
0036 // references
0037 #include "DataFormats/MuonReco/interface/Muon.h"
0038 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0039 #include "DataFormats/TrackReco/interface/Track.h"
0040 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0041 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
0042 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0043 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0044 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
0045 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
0046 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
0047 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0048 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0049 #include "MagneticField/Engine/interface/MagneticField.h"
0050 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0051 #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
0052 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0053 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHitBuilder.h"
0054 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0055 #include "Geometry/CommonDetUnit/interface/GlobalTrackingGeometry.h"
0056 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
0057 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
0058 
0059 //
0060 // class decleration
0061 //
0062 
0063 class TrackerToMuonPropagator : public edm::stream::EDProducer<> {
0064 public:
0065   explicit TrackerToMuonPropagator(const edm::ParameterSet&);
0066   ~TrackerToMuonPropagator() override;
0067 
0068 private:
0069   void produce(edm::Event&, const edm::EventSetup&) override;
0070 
0071   // ----------member data ---------------------------
0072 
0073   // es tokens
0074   const edm::ESGetToken<Propagator, TrackingComponentsRecord> m_esTokenProp;
0075   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> m_esTokenTk;
0076   const edm::ESGetToken<DTGeometry, MuonGeometryRecord> m_esTokenDT;
0077   const edm::ESGetToken<CSCGeometry, MuonGeometryRecord> m_esTokenCSC;
0078   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> m_esTokenMF;
0079   const edm::ESGetToken<GlobalTrackingGeometry, GlobalTrackingGeometryRecord> m_esTokenGTGeo;
0080 
0081   const edm::InputTag m_globalMuons, m_globalMuonTracks;
0082   const bool m_refitTracker;
0083 
0084   const edm::EDGetTokenT<reco::MuonCollection> muonToken_;
0085   const edm::EDGetTokenT<reco::TrackCollection> trackToken_;
0086 
0087   TrackTransformer* m_trackTransformer;
0088 };
0089 
0090 //
0091 // constants, enums and typedefs
0092 //
0093 
0094 //
0095 // static data member definitions
0096 //
0097 
0098 //
0099 // constructors and destructor
0100 //
0101 TrackerToMuonPropagator::TrackerToMuonPropagator(const edm::ParameterSet& iConfig)
0102     : m_esTokenProp(esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("propagator")))),
0103       m_esTokenTk(esConsumes()),
0104       m_esTokenDT(esConsumes()),
0105       m_esTokenCSC(esConsumes()),
0106       m_esTokenMF(esConsumes()),
0107       m_esTokenGTGeo(esConsumes()),
0108       m_globalMuons(iConfig.getParameter<edm::InputTag>("globalMuons")),
0109       m_globalMuonTracks(iConfig.getParameter<edm::InputTag>("globalMuonTracks")),
0110       m_refitTracker(iConfig.getParameter<bool>("refitTrackerTrack")),
0111       muonToken_(consumes<reco::MuonCollection>(m_globalMuons)),
0112       trackToken_(consumes<reco::TrackCollection>(m_globalMuonTracks)) {
0113   if (m_refitTracker)
0114     m_trackTransformer =
0115         new TrackTransformer(iConfig.getParameter<edm::ParameterSet>("trackerTrackTransformer"), consumesCollector());
0116   else
0117     m_trackTransformer = nullptr;
0118 
0119   produces<std::vector<Trajectory>>();
0120   produces<TrajTrackAssociationCollection>();
0121 }
0122 
0123 TrackerToMuonPropagator::~TrackerToMuonPropagator() {
0124   // do anything here that needs to be done at desctruction time
0125   // (e.g. close files, deallocate resources etc.)
0126 }
0127 
0128 //
0129 // member functions
0130 //
0131 
0132 // ------------ method called to produce the data  ------------
0133 void TrackerToMuonPropagator::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0134   if (m_trackTransformer)
0135     m_trackTransformer->setServices(iSetup);
0136 
0137   const edm::Handle<reco::MuonCollection>& globalMuons = iEvent.getHandle(muonToken_);
0138   const edm::Handle<reco::TrackCollection>& globalMuonTracks = iEvent.getHandle(trackToken_);
0139 
0140   const Propagator* propagator = &iSetup.getData(m_esTokenProp);
0141   const TrackerGeometry* trackerGeometry = &iSetup.getData(m_esTokenTk);
0142   const DTGeometry* dtGeometry = &iSetup.getData(m_esTokenDT);
0143   const CSCGeometry* cscGeometry = &iSetup.getData(m_esTokenCSC);
0144   const MagneticField* magneticField = &iSetup.getData(m_esTokenMF);
0145   const GlobalTrackingGeometry* globalGeometry = &iSetup.getData(m_esTokenGTGeo);
0146 
0147   // Create these factories once per event
0148 
0149   MuonTransientTrackingRecHitBuilder muonTransBuilder;
0150 
0151   // Create a collection of Trajectories, to put in the Event
0152   auto trajectoryCollection = std::make_unique<std::vector<Trajectory>>();
0153 
0154   // Remember which trajectory is associated with which track
0155   std::map<edm::Ref<std::vector<Trajectory>>::key_type, edm::Ref<reco::TrackCollection>::key_type> reference_map;
0156   edm::Ref<std::vector<Trajectory>>::key_type trajCounter = 0;
0157 
0158   for (reco::MuonCollection::const_iterator globalMuon = globalMuons->begin(); globalMuon != globalMuons->end();
0159        ++globalMuon) {
0160     // get the counter for this global muon (that's why we needed to extract the collection explicitly
0161     edm::Ref<reco::TrackCollection>::key_type trackCounter = 0;
0162     reco::TrackCollection::const_iterator globalMuonTrack = globalMuonTracks->begin();
0163     for (; globalMuonTrack != globalMuonTracks->end(); ++globalMuonTrack) {
0164       trackCounter++;
0165       if (fabs(globalMuon->combinedMuon()->phi() - globalMuonTrack->phi()) < 1e-10 &&
0166           fabs(globalMuon->combinedMuon()->eta() - globalMuonTrack->eta()) < 1e-10)
0167         break;
0168     }
0169     if (globalMuonTrack == globalMuonTracks->end()) {
0170       throw cms::Exception("BadConfig") << "The tracks label doesn't correspond to the same objects as the muons label"
0171                                         << std::endl;
0172     }
0173 
0174     TrajectoryStateOnSurface tracker_tsos;
0175     DetId outerDetId;
0176     if (m_refitTracker) {
0177       std::vector<Trajectory> trackerTrajectories = m_trackTransformer->transform(*globalMuon->track());
0178       if (trackerTrajectories.size() == 1) {
0179         const Trajectory trackerTrajectory = *(trackerTrajectories.begin());
0180 
0181         // surprisingly, firstMeasurement() corresponds to the outermost state of the tracker
0182         tracker_tsos = trackerTrajectory.firstMeasurement().forwardPredictedState();
0183         outerDetId = trackerTrajectory.firstMeasurement().recHit()->geographicalId();
0184       } else
0185         continue;
0186     } else {
0187       // get information about the outermost tracker hit
0188       GlobalPoint outerPosition(globalMuon->track()->outerPosition().x(),
0189                                 globalMuon->track()->outerPosition().y(),
0190                                 globalMuon->track()->outerPosition().z());
0191       GlobalVector outerMomentum(globalMuon->track()->outerMomentum().x(),
0192                                  globalMuon->track()->outerMomentum().y(),
0193                                  globalMuon->track()->outerMomentum().z());
0194       int charge = globalMuon->track()->charge();
0195       const reco::Track::CovarianceMatrix outerStateCovariance = globalMuon->track()->outerStateCovariance();
0196       outerDetId = DetId(globalMuon->track()->outerDetId());
0197 
0198       // construct the information necessary to make a TrajectoryStateOnSurface
0199       GlobalTrajectoryParameters globalTrajParams(outerPosition, outerMomentum, charge, magneticField);
0200       CurvilinearTrajectoryError curviError(outerStateCovariance);
0201       FreeTrajectoryState tracker_state(globalTrajParams, curviError);
0202 
0203       // starting point for propagation into the muon system
0204       tracker_tsos =
0205           TrajectoryStateOnSurface(globalTrajParams, curviError, trackerGeometry->idToDet(outerDetId)->surface());
0206     }
0207 
0208     TrajectoryStateOnSurface last_tsos = tracker_tsos;
0209 
0210     // loop over the muon hits, keeping track of the successful extrapolations
0211     edm::OwnVector<TrackingRecHit> muonHits;
0212     std::vector<TrajectoryStateOnSurface> TSOSes;
0213     for (auto const& hit : globalMuon->combinedMuon()->recHits()) {
0214       DetId id = hit->geographicalId();
0215 
0216       TrajectoryStateOnSurface extrapolation;
0217       bool extrapolated = false;
0218       if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::DT) {
0219         extrapolation = propagator->propagate(last_tsos, dtGeometry->idToDet(id)->surface());
0220         extrapolated = true;
0221       } else if (id.det() == DetId::Muon && id.subdetId() == MuonSubdetId::CSC) {
0222         extrapolation = propagator->propagate(last_tsos, cscGeometry->idToDet(id)->surface());
0223         extrapolated = true;
0224       }
0225 
0226       if (extrapolated && extrapolation.isValid()) {
0227         muonHits.push_back(hit->clone());
0228         TSOSes.push_back(extrapolation);
0229       }
0230     }  // end loop over standAloneMuon hits
0231 
0232     // if it has any successful extrapolations, make them into a Trajectory
0233     if (!muonHits.empty()) {
0234       PTrajectoryStateOnDet const& PTraj = trajectoryStateTransform::persistentState(tracker_tsos, outerDetId.rawId());
0235       TrajectorySeed trajectorySeed(PTraj, muonHits, alongMomentum);
0236       Trajectory trajectory(trajectorySeed, alongMomentum);
0237 
0238       for (unsigned int i = 0; i < muonHits.size(); i++) {
0239         TrajectoryMeasurement::ConstRecHitPointer hitPtr(muonTransBuilder.build(&(muonHits[i]), globalGeometry));
0240         TrajectoryStateOnSurface TSOS = TSOSes[i];
0241         trajectory.push(TrajectoryMeasurement(TSOS, TSOS, TSOS, hitPtr));
0242       }  // end filling Trajectory
0243 
0244       trajectoryCollection->push_back(trajectory);
0245 
0246       // Remember which Trajectory is associated with which Track
0247       trajCounter++;
0248       reference_map[trajCounter] = trackCounter;
0249 
0250     }  // end if we have some good extrapolations
0251 
0252   }  // end loop over globalMuons
0253 
0254   unsigned int numTrajectories = trajectoryCollection->size();
0255 
0256   // insert the trajectories into the Event
0257   edm::OrphanHandle<std::vector<Trajectory>> ohTrajs = iEvent.put(std::move(trajectoryCollection));
0258 
0259   // create the trajectory <-> track association map
0260   auto trajTrackMap = std::make_unique<TrajTrackAssociationCollection>();
0261 
0262   for (trajCounter = 0; trajCounter < numTrajectories; trajCounter++) {
0263     edm::Ref<reco::TrackCollection>::key_type trackCounter = reference_map[trajCounter];
0264 
0265     trajTrackMap->insert(edm::Ref<std::vector<Trajectory>>(ohTrajs, trajCounter),
0266                          edm::Ref<reco::TrackCollection>(globalMuonTracks, trackCounter));
0267   }
0268   // and put it in the Event, also
0269   iEvent.put(std::move(trajTrackMap));
0270 }
0271 
0272 //define this as a plug-in
0273 DEFINE_FWK_MODULE(TrackerToMuonPropagator);