CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/ElectroWeakAnalysis/Utilities/src/DistortedMuonProducerFromDB.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include "FWCore/Framework/interface/Frameworkfwd.h"
00003 #include "FWCore/Framework/interface/MakerMacros.h"
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 
00007 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
00008 #include "MuonAnalysis/MomentumScaleCalibration/interface/ResolutionFunction.h"
00009 
00010 //
00011 // class declaration
00012 //
00013 class DistortedMuonProducerFromDB : public edm::EDProducer {
00014    public:
00015       explicit DistortedMuonProducerFromDB(const edm::ParameterSet&);
00016       ~DistortedMuonProducerFromDB();
00017 
00018    private:
00019       virtual void beginRun(edm::Run&, const edm::EventSetup&) ;
00020       virtual void produce(edm::Event&, const edm::EventSetup&);
00021       virtual void endJob() ;
00022 
00023       edm::InputTag muonTag_;
00024       edm::InputTag genMatchMapTag_;
00025 
00026       std::string dbScaleLabel_;
00027       std::string dbDataResolutionLabel_;
00028       std::string dbMCResolutionLabel_;
00029 
00030       std::auto_ptr<MomentumScaleCorrector> momCorrector_;
00031       std::auto_ptr<ResolutionFunction> momResolutionData_;
00032       std::auto_ptr<ResolutionFunction> momResolutionMC_;
00033 };
00034 
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 #include "DataFormats/Common/interface/Handle.h"
00037 #include "DataFormats/Common/interface/View.h"
00038 #include "DataFormats/MuonReco/interface/Muon.h"
00039 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00040 
00041 #include <CLHEP/Random/RandGauss.h>
00042 
00043 #include "FWCore/Framework/interface/ESHandle.h"
00044 #include "FWCore/Framework/interface/EventSetup.h"
00045 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
00046 #include "CondFormats/DataRecord/interface/MuScleFitDBobjectRcd.h"
00047 #include "MuonAnalysis/MomentumScaleCalibration/interface/BaseFunction.h"
00048 #include "MuonAnalysis/MomentumScaleCalibration/interface/Functions.h"
00049 
00051 DistortedMuonProducerFromDB::DistortedMuonProducerFromDB(const edm::ParameterSet& pset) {
00052 
00053   // What is being produced
00054       produces<std::vector<reco::Muon> >();
00055 
00056   // Input products
00057       muonTag_ = pset.getUntrackedParameter<edm::InputTag> ("MuonTag", edm::InputTag("muons"));
00058       dbScaleLabel_ = pset.getUntrackedParameter<std::string> ("DBScaleLabel", "scale");
00059       dbDataResolutionLabel_ = pset.getUntrackedParameter<std::string> ("DBDataResolutionLabel", "datareso");
00060       dbMCResolutionLabel_ = pset.getUntrackedParameter<std::string> ("DBMCResolutionLabel", "mcreso");
00061 
00062 } 
00063 
00065 DistortedMuonProducerFromDB::~DistortedMuonProducerFromDB(){
00066 }
00067 
00069 void DistortedMuonProducerFromDB::beginRun(edm::Run&, const edm::EventSetup& iSetup) {
00070       edm::ESHandle<MuScleFitDBobject> dbObject1;
00071       iSetup.get<MuScleFitDBobjectRcd>().get(dbScaleLabel_,dbObject1);
00072       momCorrector_.reset(new MomentumScaleCorrector(dbObject1.product()));
00073 
00074       LogTrace("") << ">>> Using database for momentum scale corrections !!";
00075 
00076       edm::ESHandle<MuScleFitDBobject> dbObject2;
00077       iSetup.get<MuScleFitDBobjectRcd>().get(dbDataResolutionLabel_, dbObject2);
00078       momResolutionData_.reset(new ResolutionFunction(dbObject2.product()));
00079 
00080       edm::ESHandle<MuScleFitDBobject> dbObject3;
00081       iSetup.get<MuScleFitDBobjectRcd>().get(dbMCResolutionLabel_, dbObject3);
00082       momResolutionMC_.reset(new ResolutionFunction(dbObject3.product()));
00083 
00084       LogTrace("") << ">>> Using database for momentum resolution corrections !!";
00085 }
00086 
00088 void DistortedMuonProducerFromDB::endJob(){
00089 }
00090 
00092 void DistortedMuonProducerFromDB::produce(edm::Event& ev, const edm::EventSetup& iSetup) {
00093 
00094       if (ev.isRealData()) return;
00095 
00096       // Muon collection
00097       edm::Handle<edm::View<reco::Muon> > muonCollection;
00098       if (!ev.getByLabel(muonTag_, muonCollection)) {
00099             edm::LogError("") << ">>> Muon collection does not exist !!!";
00100             return;
00101       }
00102       unsigned int muonCollectionSize = muonCollection->size();
00103 
00104       std::auto_ptr<reco::MuonCollection> newmuons (new reco::MuonCollection);
00105 
00106       for (unsigned int i=0; i<muonCollectionSize; i++) {
00107             edm::RefToBase<reco::Muon> mu = muonCollection->refAt(i);
00108 
00109             // Set shift
00110             double shift = (*momCorrector_)(*mu) - mu->pt();
00111             LogTrace("") << "\tmomentumScaleShift= " << shift << " [GeV]"; 
00112 
00113             // Set resolutions
00114             double sigma = pow(momResolutionData_->sigmaPt(*mu),2) -
00115                               pow(momResolutionMC_->sigmaPt(*mu),2);
00116             if (sigma>0.) sigma = sqrt(sigma); else sigma = 0.;
00117             LogTrace("") << "\tPt additional smearing= " << sigma << " [GeV]"; 
00118 
00119             // Gaussian Random number for smearing
00120             double rndg = CLHEP::RandGauss::shoot();
00121             
00122             // New muon
00123             double ptmu = mu->pt();
00124             ptmu += shift + sigma*rndg;
00125             reco::Muon* newmu = mu->clone();
00126             newmu->setP4 (
00127                   reco::Particle::PolarLorentzVector (
00128                         ptmu, mu->eta(), mu->phi(), mu->mass()
00129                   )
00130             );
00131             newmuons->push_back(*newmu);
00132 
00133       }
00134 
00135       ev.put(newmuons);
00136 }
00137 
00138 DEFINE_FWK_MODULE(DistortedMuonProducerFromDB);