File indexing completed on 2024-04-06 12:22:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <memory>
0018 #include <string>
0019
0020
0021 #include "CondFormats/DataRecord/interface/MuScleFitDBobjectRcd.h"
0022 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
0023 #include "DataFormats/Candidate/interface/LeafCandidate.h"
0024 #include "DataFormats/MuonReco/interface/Muon.h"
0025 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0026 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0027 #include "DataFormats/PatCandidates/interface/Muon.h"
0028 #include "DataFormats/TrackReco/interface/Track.h"
0029 #include "FWCore/Framework/interface/stream/EDProducer.h"
0030 #include "FWCore/Framework/interface/ESHandle.h"
0031 #include "FWCore/Framework/interface/Event.h"
0032 #include "FWCore/Framework/interface/EventSetup.h"
0033 #include "FWCore/Framework/interface/Frameworkfwd.h"
0034 #include "FWCore/Framework/interface/MakerMacros.h"
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/Utilities/interface/InputTag.h"
0037 #include "MuonAnalysis/MomentumScaleCalibration/interface/Functions.h"
0038 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
0039
0040 class MuScleFitMuonProducer : public edm::stream::EDProducer<> {
0041 public:
0042 explicit MuScleFitMuonProducer(const edm::ParameterSet&);
0043 ~MuScleFitMuonProducer() override;
0044
0045 private:
0046 void produce(edm::Event&, const edm::EventSetup&) override;
0047 template <class T>
0048 std::unique_ptr<T> applyCorrection(const edm::Handle<T>& allMuons);
0049 const edm::ESGetToken<MuScleFitDBobject, MuScleFitDBobjectRcd> muToken_;
0050 const edm::InputTag theMuonLabel_;
0051 const edm::EDGetTokenT<pat::MuonCollection> thePatMuonToken_;
0052 const edm::EDGetTokenT<reco::MuonCollection> theRecoMuonToken_;
0053 const bool patMuons_;
0054
0055 edm::ESHandle<MuScleFitDBobject> dbObject_;
0056 unsigned long long dbObjectCacheId_;
0057 std::shared_ptr<MomentumScaleCorrector> corrector_;
0058 };
0059
0060 MuScleFitMuonProducer::MuScleFitMuonProducer(const edm::ParameterSet& iConfig)
0061 : muToken_(esConsumes(edm::ESInputTag("", iConfig.getUntrackedParameter<std::string>("DbObjectLabel", "")))),
0062 theMuonLabel_(iConfig.getParameter<edm::InputTag>("MuonLabel")),
0063 thePatMuonToken_(mayConsume<pat::MuonCollection>(theMuonLabel_)),
0064 theRecoMuonToken_(mayConsume<reco::MuonCollection>(theMuonLabel_)),
0065 patMuons_(iConfig.getParameter<bool>("PatMuons")),
0066 dbObjectCacheId_(0) {
0067 if (patMuons_ == true) {
0068 produces<pat::MuonCollection>();
0069 } else {
0070 produces<reco::MuonCollection>();
0071 }
0072 }
0073
0074 MuScleFitMuonProducer::~MuScleFitMuonProducer() = default;
0075
0076 template <class T>
0077 std::unique_ptr<T> MuScleFitMuonProducer::applyCorrection(const edm::Handle<T>& allMuons) {
0078 std::unique_ptr<T> pOut(new T);
0079
0080
0081 for (typename T::const_iterator muon = allMuons->begin(); muon != allMuons->end(); ++muon) {
0082
0083 double pt = (*corrector_)(*muon);
0084
0085 double eta = muon->eta();
0086 double phi = muon->phi();
0087
0088 typename T::value_type* newMuon = muon->clone();
0089 newMuon->setP4(reco::Particle::PolarLorentzVector(pt, eta, phi, muon->mass()));
0090
0091 pOut->push_back(*newMuon);
0092 }
0093 return pOut;
0094 }
0095
0096
0097 void MuScleFitMuonProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0098 unsigned long long dbObjectCacheId = iSetup.get<MuScleFitDBobjectRcd>().cacheIdentifier();
0099 if (dbObjectCacheId != dbObjectCacheId_) {
0100 dbObject_ = iSetup.getHandle(muToken_);
0101 }
0102
0103
0104
0105
0106
0107 corrector_.reset(new MomentumScaleCorrector(dbObject_.product()));
0108
0109 if (patMuons_ == true) {
0110 edm::Handle<pat::MuonCollection> allMuons;
0111 iEvent.getByToken(thePatMuonToken_, allMuons);
0112 iEvent.put(applyCorrection(allMuons));
0113 } else {
0114 edm::Handle<reco::MuonCollection> allMuons;
0115 iEvent.getByToken(theRecoMuonToken_, allMuons);
0116 iEvent.put(applyCorrection(allMuons));
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 }
0139
0140
0141 DEFINE_FWK_MODULE(MuScleFitMuonProducer);