CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/Alignment/CommonAlignmentMonitor/plugins/TrackerToMuonPropagator.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    TrackerToMuonPropagator
00004 // Class:      TrackerToMuonPropagator
00005 // 
00013 //
00014 // Original Author:  Jim Pivarski
00015 //         Created:  Wed Dec 12 13:31:55 CST 2007
00016 // $Id: TrackerToMuonPropagator.cc,v 1.5 2011/12/22 20:10:49 innocent Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/Frameworkfwd.h"
00026 #include "FWCore/Framework/interface/EDProducer.h"
00027 #include "FWCore/Framework/interface/Event.h"
00028 #include "FWCore/Framework/interface/EventSetup.h"
00029 #include "FWCore/Framework/interface/MakerMacros.h"
00030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00031 
00032 // products
00033 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00034 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00035 
00036 // references
00037 #include "DataFormats/MuonReco/interface/Muon.h"
00038 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00039 #include "DataFormats/TrackReco/interface/Track.h"
00040 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00041 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00042 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00043 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00044 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
00045 #include "DataFormats/MuonDetId/interface/MuonSubdetId.h"
00046 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00047 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00048 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00049 #include "MagneticField/Engine/interface/MagneticField.h"
00050 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00051 #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
00052 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00053 #include "RecoMuon/TransientTrackingRecHit/interface/MuonTransientTrackingRecHitBuilder.h"
00054 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00055 #include "Geometry/CommonDetUnit/interface/GlobalTrackingGeometry.h"
00056 #include "Geometry/Records/interface/GlobalTrackingGeometryRecord.h"
00057 #include "TrackingTools/TrackRefitter/interface/TrackTransformer.h"
00058 
00059 //
00060 // class decleration
00061 //
00062 
00063 class TrackerToMuonPropagator : public edm::EDProducer {
00064    public:
00065       explicit TrackerToMuonPropagator(const edm::ParameterSet&);
00066       ~TrackerToMuonPropagator();
00067 
00068    private:
00069       virtual void beginJob() ;
00070       virtual void produce(edm::Event&, const edm::EventSetup&);
00071       virtual void endJob() ;
00072       
00073       // ----------member data ---------------------------
00074 
00075       edm::InputTag m_globalMuons, m_globalMuonTracks;
00076       std::string m_propagator;
00077 
00078       bool m_refitTracker;
00079       TrackTransformer *m_trackTransformer;
00080 };
00081 
00082 //
00083 // constants, enums and typedefs
00084 //
00085 
00086 //
00087 // static data member definitions
00088 //
00089 
00090 //
00091 // constructors and destructor
00092 //
00093 TrackerToMuonPropagator::TrackerToMuonPropagator(const edm::ParameterSet& iConfig)
00094 {
00095    m_globalMuons = iConfig.getParameter<edm::InputTag>("globalMuons");
00096    m_globalMuonTracks = iConfig.getParameter<edm::InputTag>("globalMuonTracks");
00097    m_propagator = iConfig.getParameter<std::string>("propagator");
00098    m_refitTracker = iConfig.getParameter<bool>("refitTrackerTrack");
00099    if (m_refitTracker) {
00100       m_trackTransformer = new TrackTransformer(iConfig.getParameter<edm::ParameterSet>("trackerTrackTransformer"));
00101    }
00102    else m_trackTransformer = NULL;
00103   
00104    produces<std::vector<Trajectory> >();
00105    produces<TrajTrackAssociationCollection>();
00106 }
00107 
00108 
00109 TrackerToMuonPropagator::~TrackerToMuonPropagator()
00110 {
00111  
00112    // do anything here that needs to be done at desctruction time
00113    // (e.g. close files, deallocate resources etc.)
00114 
00115 }
00116 
00117 
00118 //
00119 // member functions
00120 //
00121 
00122 // ------------ method called to produce the data  ------------
00123 void
00124 TrackerToMuonPropagator::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00125 {
00126    if (m_trackTransformer) m_trackTransformer->setServices(iSetup);
00127 
00128    edm::Handle<reco::MuonCollection> globalMuons;
00129    iEvent.getByLabel(m_globalMuons, globalMuons);
00130 
00131    edm::Handle<reco::TrackCollection> globalMuonTracks;
00132    iEvent.getByLabel(m_globalMuonTracks, globalMuonTracks);
00133 
00134    edm::ESHandle<Propagator> propagator;
00135    iSetup.get<TrackingComponentsRecord>().get(m_propagator, propagator);
00136 
00137    edm::ESHandle<TrackerGeometry> trackerGeometry;
00138    iSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometry);
00139 
00140    edm::ESHandle<DTGeometry> dtGeometry;
00141    iSetup.get<MuonGeometryRecord>().get(dtGeometry);
00142 
00143    edm::ESHandle<CSCGeometry> cscGeometry;
00144    iSetup.get<MuonGeometryRecord>().get(cscGeometry);
00145 
00146    edm::ESHandle<MagneticField> magneticField;
00147    iSetup.get<IdealMagneticFieldRecord>().get(magneticField);
00148 
00149    edm::ESHandle<GlobalTrackingGeometry> globalGeometry;
00150    iSetup.get<GlobalTrackingGeometryRecord>().get(globalGeometry);
00151 
00152    // Create these factories once per event
00153    
00154    MuonTransientTrackingRecHitBuilder muonTransBuilder;
00155 
00156    // Create a collection of Trajectories, to put in the Event
00157    std::auto_ptr<std::vector<Trajectory> > trajectoryCollection(new std::vector<Trajectory>);
00158 
00159    // Remember which trajectory is associated with which track
00160    std::map<edm::Ref<std::vector<Trajectory> >::key_type, edm::Ref<reco::TrackCollection>::key_type> reference_map;
00161    edm::Ref<std::vector<Trajectory> >::key_type trajCounter = 0;
00162 
00163    for (reco::MuonCollection::const_iterator globalMuon = globalMuons->begin();  globalMuon != globalMuons->end();  ++globalMuon) {
00164 
00165       // get the counter for this global muon (that's why we needed to extract the collection explicitly
00166       edm::Ref<reco::TrackCollection>::key_type trackCounter = 0;
00167       reco::TrackCollection::const_iterator globalMuonTrack = globalMuonTracks->begin();
00168       for (; globalMuonTrack != globalMuonTracks->end();  ++globalMuonTrack) {
00169          trackCounter++;
00170          if (fabs(globalMuon->combinedMuon()->phi() - globalMuonTrack->phi()) < 1e-10  &&
00171              fabs(globalMuon->combinedMuon()->eta() - globalMuonTrack->eta()) < 1e-10) break;
00172       }
00173       if (globalMuonTrack == globalMuonTracks->end()) {
00174          throw cms::Exception("BadConfig") << "The tracks label doesn't correspond to the same objects as the muons label" << std::endl;
00175       }
00176 
00177       TrajectoryStateOnSurface tracker_tsos;
00178       DetId outerDetId;
00179       if (m_refitTracker) {
00180          std::vector<Trajectory> trackerTrajectories = m_trackTransformer->transform(*globalMuon->track());
00181          if (trackerTrajectories.size() == 1) {
00182             const Trajectory trackerTrajectory = *(trackerTrajectories.begin());
00183 
00184             // surprisingly, firstMeasurement() corresponds to the outermost state of the tracker
00185             tracker_tsos = trackerTrajectory.firstMeasurement().forwardPredictedState();
00186             outerDetId = trackerTrajectory.firstMeasurement().recHit()->geographicalId();
00187          }
00188          else continue;
00189       }
00190       else {
00191          // get information about the outermost tracker hit
00192          GlobalPoint outerPosition(globalMuon->track()->outerPosition().x(), globalMuon->track()->outerPosition().y(), globalMuon->track()->outerPosition().z());
00193          GlobalVector outerMomentum(globalMuon->track()->outerMomentum().x(), globalMuon->track()->outerMomentum().y(), globalMuon->track()->outerMomentum().z());
00194          int charge = globalMuon->track()->charge();
00195          const reco::Track::CovarianceMatrix outerStateCovariance = globalMuon->track()->outerStateCovariance();
00196          outerDetId = DetId(globalMuon->track()->outerDetId());
00197 
00198          // construct the information necessary to make a TrajectoryStateOnSurface
00199          GlobalTrajectoryParameters globalTrajParams(outerPosition, outerMomentum, charge, &(*magneticField));
00200          CurvilinearTrajectoryError curviError(outerStateCovariance);
00201          FreeTrajectoryState tracker_state(globalTrajParams, curviError);
00202 
00203          // starting point for propagation into the muon system
00204          tracker_tsos = TrajectoryStateOnSurface(globalTrajParams, curviError, trackerGeometry->idToDet(outerDetId)->surface());
00205       }
00206 
00207       TrajectoryStateOnSurface last_tsos = tracker_tsos;
00208 
00209       // loop over the muon hits, keeping track of the successful extrapolations
00210       edm::OwnVector<TrackingRecHit> muonHits;
00211       std::vector<TrajectoryStateOnSurface> TSOSes;
00212       for (trackingRecHit_iterator hit = globalMuon->combinedMuon()->recHitsBegin();  hit != globalMuon->combinedMuon()->recHitsEnd();  ++hit) {
00213          DetId id = (*hit)->geographicalId();
00214 
00215          TrajectoryStateOnSurface extrapolation;
00216          bool extrapolated = false;
00217          if (id.det() == DetId::Muon  &&  id.subdetId() == MuonSubdetId::DT) {
00218             extrapolation = propagator->propagate(last_tsos, dtGeometry->idToDet(id)->surface());
00219             extrapolated = true;
00220          }
00221          else if (id.det() == DetId::Muon  &&  id.subdetId() == MuonSubdetId::CSC) {
00222             extrapolation = propagator->propagate(last_tsos, cscGeometry->idToDet(id)->surface());
00223             extrapolated = true;
00224          }
00225          
00226          if (extrapolated  &&  extrapolation.isValid()) {
00227             muonHits.push_back((*hit)->clone());
00228             TSOSes.push_back(extrapolation);
00229          }
00230       } // end loop over standAloneMuon hits
00231          
00232       // if it has any successful extrapolations, make them into a Trajectory
00233       if (muonHits.size() > 0) {
00234          PTrajectoryStateOnDet const & PTraj = trajectoryStateTransform::persistentState(tracker_tsos, outerDetId.rawId());
00235          TrajectorySeed trajectorySeed(PTraj, muonHits, alongMomentum);
00236          Trajectory trajectory(trajectorySeed, alongMomentum);
00237 
00238          for (unsigned int i = 0;  i < muonHits.size();  i++) {
00239             TrajectoryMeasurement::ConstRecHitPointer hitPtr(muonTransBuilder.build(&(muonHits[i]), globalGeometry));
00240             TrajectoryStateOnSurface TSOS = TSOSes[i];
00241             trajectory.push(TrajectoryMeasurement(TSOS, TSOS, TSOS, hitPtr));
00242          } // end filling Trajectory
00243 
00244          trajectoryCollection->push_back(trajectory);
00245 
00246          // Remember which Trajectory is associated with which Track
00247          trajCounter++;
00248          reference_map[trajCounter] = trackCounter;
00249 
00250       } // end if we have some good extrapolations
00251 
00252    } // end loop over globalMuons
00253 
00254    unsigned int numTrajectories = trajectoryCollection->size();
00255 
00256    // insert the trajectories into the Event
00257    edm::OrphanHandle<std::vector<Trajectory> > ohTrajs = iEvent.put(trajectoryCollection);
00258 
00259    // create the trajectory <-> track association map
00260    std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap(new TrajTrackAssociationCollection());
00261 
00262    for (trajCounter = 0;  trajCounter < numTrajectories;  trajCounter++) {
00263       edm::Ref<reco::TrackCollection>::key_type trackCounter = reference_map[trajCounter];
00264 
00265       trajTrackMap->insert(edm::Ref<std::vector<Trajectory> >(ohTrajs, trajCounter), edm::Ref<reco::TrackCollection>(globalMuonTracks, trackCounter));
00266    }
00267    // and put it in the Event, also
00268    iEvent.put(trajTrackMap);
00269 }
00270 
00271 // ------------ method called once each job just before starting event loop  ------------
00272 void 
00273 TrackerToMuonPropagator::beginJob()
00274 {
00275 }
00276 
00277 // ------------ method called once each job just after ending the event loop  ------------
00278 void 
00279 TrackerToMuonPropagator::endJob() {
00280 }
00281 
00282 //define this as a plug-in
00283 DEFINE_FWK_MODULE(TrackerToMuonPropagator);