Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:26:17

0001 /** \class MuonServiceProxy
0002  *  Class to handle the services needed by the muon reconstruction
0003  *  This class avoid the EventSetup percolation.
0004  *  The update method is called each event in order to update the
0005  *  pointers.
0006  *
0007  *  \author N. Amapane - CERN <nicola.amapane@cern.ch>
0008  *  \author R. Bellan - INFN Torino <riccardo.bellan@cern.ch>
0009  *
0010  *  Modified by C. Calabria & A. Sharma
0011  *  Modified by D. Nash
0012  */
0013 
0014 // Class Header
0015 #include "RecoMuon/TrackingTools/interface/MuonServiceProxy.h"
0016 
0017 // Framework Headers
0018 #include "FWCore/Framework/interface/ConsumesCollector.h"
0019 #include "FWCore/Framework/interface/EventSetup.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include "FWCore/Utilities/interface/ESInputTag.h"
0023 
0024 #include <vector>
0025 
0026 // Constructor
0027 MuonServiceProxy::MuonServiceProxy(const edm::ParameterSet& par,
0028                                    edm::ConsumesCollector&& iC,
0029                                    UseEventSetupIn useEventSetupIn)
0030     : theTrackingGeometry(nullptr),
0031       theMGField(nullptr),
0032       theDetLayerGeometry(nullptr),
0033       theEventSetup(nullptr),
0034       theSchool(nullptr) {
0035   theMuonNavigationFlag = par.getUntrackedParameter<bool>("UseMuonNavigation", true);
0036 
0037   if (theMuonNavigationFlag) {
0038     theRPCLayer = par.getParameter<bool>("RPCLayers");
0039 
0040     if (par.existsAs<bool>("CSCLayers"))
0041       theCSCLayer = par.getParameter<bool>("CSCLayers");
0042     else
0043       theCSCLayer = true;
0044 
0045     if (par.existsAs<bool>("GEMLayers"))
0046       theGEMLayer = par.getParameter<bool>("GEMLayers");
0047     else
0048       theGEMLayer = false;
0049 
0050     if (par.existsAs<bool>("ME0Layers"))
0051       theME0Layer = par.getParameter<bool>("ME0Layers");
0052     else
0053       theME0Layer = false;
0054 
0055   } else {
0056     theRPCLayer = true;
0057     theCSCLayer = true;
0058     theGEMLayer = true;
0059     theME0Layer = true;
0060   }
0061 
0062   // load the propagators map
0063   std::vector<std::string> noPropagators;
0064   std::vector<std::string> propagatorNames =
0065       par.getUntrackedParameter<std::vector<std::string>>("Propagators", noPropagators);
0066 
0067   if (propagatorNames.empty())
0068     LogDebug("Muon|RecoMuon|MuonServiceProxy") << "NO propagator(s) selected!";
0069 
0070   for (auto const& propagatorName : propagatorNames) {
0071     thePropagators[propagatorName] = PropagatorInfo();
0072   }
0073   theCacheId_GTG = 0;
0074   theCacheId_MG = 0;
0075   theCacheId_DG = 0;
0076   theCacheId_P = 0;
0077   theChangeInTrackingComponentsRecord = false;
0078 
0079   // Declare the products we get from the EventSetup and initialize the tokens used to get them
0080   if (useEventSetupIn == UseEventSetupIn::Event || useEventSetupIn == UseEventSetupIn::RunAndEvent) {
0081     globalTrackingGeometryEventToken_ = iC.esConsumes<GlobalTrackingGeometry, GlobalTrackingGeometryRecord>();
0082     magneticFieldEventToken_ = iC.esConsumes<MagneticField, IdealMagneticFieldRecord>();
0083     muonDetLayerGeometryEventToken_ = iC.esConsumes<MuonDetLayerGeometry, MuonRecoGeometryRecord>();
0084     for (auto& element : thePropagators) {
0085       element.second.eventToken_ =
0086           iC.esConsumes<Propagator, TrackingComponentsRecord>(edm::ESInputTag("", element.first));
0087     }
0088   }
0089   if (useEventSetupIn == UseEventSetupIn::Run || useEventSetupIn == UseEventSetupIn::RunAndEvent) {
0090     globalTrackingGeometryRunToken_ =
0091         iC.esConsumes<GlobalTrackingGeometry, GlobalTrackingGeometryRecord, edm::Transition::BeginRun>();
0092     magneticFieldRunToken_ = iC.esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
0093     muonDetLayerGeometryRunToken_ =
0094         iC.esConsumes<MuonDetLayerGeometry, MuonRecoGeometryRecord, edm::Transition::BeginRun>();
0095     for (auto& element : thePropagators) {
0096       element.second.runToken_ = iC.esConsumes<Propagator, TrackingComponentsRecord, edm::Transition::BeginRun>(
0097           edm::ESInputTag("", element.first));
0098     }
0099   }
0100 }
0101 
0102 // Destructor
0103 MuonServiceProxy::~MuonServiceProxy() {
0104   if (theSchool)
0105     delete theSchool;
0106 }
0107 
0108 // Operations
0109 
0110 // update the services each event
0111 void MuonServiceProxy::update(const edm::EventSetup& setup, bool duringEvent) {
0112   const std::string metname = "Muon|RecoMuon|MuonServiceProxy";
0113 
0114   theEventSetup = &setup;
0115 
0116   // Global Tracking Geometry
0117   unsigned long long newCacheId_GTG = setup.get<GlobalTrackingGeometryRecord>().cacheIdentifier();
0118   if (newCacheId_GTG != theCacheId_GTG) {
0119     LogTrace(metname) << "GlobalTrackingGeometry changed!";
0120     theCacheId_GTG = newCacheId_GTG;
0121     if (duringEvent) {
0122       theTrackingGeometry = setup.getHandle(globalTrackingGeometryEventToken_);
0123     } else {
0124       theTrackingGeometry = setup.getHandle(globalTrackingGeometryRunToken_);
0125     }
0126   }
0127 
0128   // Magfield Field
0129   unsigned long long newCacheId_MG = setup.get<IdealMagneticFieldRecord>().cacheIdentifier();
0130   if (newCacheId_MG != theCacheId_MG) {
0131     LogTrace(metname) << "Magnetic Field changed!";
0132     theCacheId_MG = newCacheId_MG;
0133     if (duringEvent) {
0134       theMGField = setup.getHandle(magneticFieldEventToken_);
0135     } else {
0136       theMGField = setup.getHandle(magneticFieldRunToken_);
0137     }
0138   }
0139 
0140   // DetLayer Geometry
0141   unsigned long long newCacheId_DG = setup.get<MuonRecoGeometryRecord>().cacheIdentifier();
0142   if (newCacheId_DG != theCacheId_DG) {
0143     LogTrace(metname) << "Muon Reco Geometry changed!";
0144     theCacheId_DG = newCacheId_DG;
0145     if (duringEvent) {
0146       theDetLayerGeometry = setup.getHandle(muonDetLayerGeometryEventToken_);
0147     } else {
0148       theDetLayerGeometry = setup.getHandle(muonDetLayerGeometryRunToken_);
0149     }
0150     // MuonNavigationSchool should live until its validity expires, and then DELETE
0151     // the NavigableLayers (this is implemented in MuonNavigationSchool's dtor)
0152     if (theMuonNavigationFlag) {
0153       if (theSchool)
0154         delete theSchool;
0155       theSchool = new MuonNavigationSchool(&*theDetLayerGeometry, theRPCLayer, theCSCLayer, theGEMLayer, theME0Layer);
0156     }
0157   }
0158 
0159   // Propagators
0160   unsigned long long newCacheId_P = setup.get<TrackingComponentsRecord>().cacheIdentifier();
0161   if (newCacheId_P != theCacheId_P) {
0162     LogTrace(metname) << "Tracking Component changed!";
0163     theChangeInTrackingComponentsRecord = true;
0164     theCacheId_P = newCacheId_P;
0165     for (auto& element : thePropagators) {
0166       if (duringEvent) {
0167         element.second.esHandle_ = setup.getHandle(element.second.eventToken_);
0168       } else {
0169         element.second.esHandle_ = setup.getHandle(element.second.runToken_);
0170       }
0171     }
0172   } else
0173     theChangeInTrackingComponentsRecord = false;
0174 }
0175 
0176 // get the propagator
0177 edm::ESHandle<Propagator> MuonServiceProxy::propagator(std::string propagatorName) const {
0178   PropagatorMap::const_iterator prop = thePropagators.find(propagatorName);
0179 
0180   if (prop == thePropagators.end()) {
0181     edm::LogError("Muon|RecoMuon|MuonServiceProxy") << "MuonServiceProxy: propagator with name: " << propagatorName
0182                                                     << " not found! Please load it in the MuonServiceProxy.cff";
0183     return edm::ESHandle<Propagator>(nullptr);
0184   }
0185   return prop->second.esHandle_;
0186 }