Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-19 23:36:10

0001 // -*- C++ -*-
0002 //
0003 // Package:    MuScleFitMuonProducer
0004 // Class:      MuScleFitMuonProducer
0005 //
0006 /**
0007  * Produce a new muon collection with corrected Pt. <br>
0008  * It is also possible to apply a smearing to the muons Pt.
0009  */
0010 //
0011 // Original Author:  Marco De Mattia,40 3-B32,+41227671551,
0012 //         Created:  Tue Jun 22 13:50:22 CEST 2010
0013 //
0014 //
0015 
0016 // system include files
0017 #include <memory>
0018 #include <string>
0019 
0020 // user include files
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   // Apply the correction and produce the new muons
0081   for (typename T::const_iterator muon = allMuons->begin(); muon != allMuons->end(); ++muon) {
0082     //std::cout << "Pt before correction = " << muon->pt() << std::endl;
0083     double pt = (*corrector_)(*muon);
0084     //std::cout << "Pt after correction = " << pt << std::endl;
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 // ------------ method called to produce the data  ------------
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   //std::cout << "identifiers size from dbObject = " << dbObject_->identifiers.size() << std::endl;
0104   //std::cout << "parameters size from dbObject = " << dbObject_->parameters.size() << std::endl;;
0105 
0106   // Create the corrector and set the parameters
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   // put into the Event
0120   // iEvent.put(std::move(pOut));
0121   // iEvent.put(applyCorrection(allMuons);
0122 
0123   /*  std::unique_ptr<reco::MuonCollection> pOut(new reco::MuonCollection);
0124 
0125   // Apply the correction and produce the new muons
0126   for( std::vector<reco::Muon>::const_iterator muon = allMuons->begin(); muon != allMuons->end(); ++muon ) {
0127 
0128     double pt = (*corrector_)(*muon);
0129     double eta = muon->eta();
0130     double phi = muon->phi();
0131 
0132     reco::Muon * newMuon = muon->clone();
0133     newMuon->setP4( reco::Particle::PolarLorentzVector( pt, eta, phi, muon->mass() ) );
0134 
0135     pOut->push_back(*newMuon);
0136   }
0137 */
0138 }
0139 
0140 //define this as a plug-in
0141 DEFINE_FWK_MODULE(MuScleFitMuonProducer);