CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/MuonAnalysis/MomentumScaleCalibration/plugins/MuScleFitMuonProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    MuScleFitMuonProducer
00004 // Class:      MuScleFitMuonProducer
00005 //
00010 //
00011 // Original Author:  Marco De Mattia,40 3-B32,+41227671551,
00012 //         Created:  Tue Jun 22 13:50:22 CEST 2010
00013 // $Id: MuScleFitMuonProducer.cc,v 1.8 2010/12/13 11:23:42 demattia Exp $
00014 //
00015 //
00016 
00017 // system include files
00018 #include <memory>
00019 #include <string>
00020 
00021 // user include files
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/EDProducer.h"
00024 
00025 #include "FWCore/Framework/interface/Event.h"
00026 #include "FWCore/Framework/interface/MakerMacros.h"
00027 
00028 #include "FWCore/Utilities/interface/InputTag.h"
00029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00030 
00031 #include "DataFormats/MuonReco/interface/Muon.h"
00032 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00033 #include "DataFormats/Candidate/interface/LeafCandidate.h"
00034 
00035 #include "DataFormats/TrackReco/interface/Track.h"
00036 #include "DataFormats/PatCandidates/interface/Muon.h"
00037 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
00038 
00039 #include "MuonAnalysis/MomentumScaleCalibration/interface/Functions.h"
00040 
00041 #include "CondFormats/RecoMuonObjects/interface/MuScleFitDBobject.h"
00042 #include "CondFormats/DataRecord/interface/MuScleFitDBobjectRcd.h"
00043 
00044 #include "FWCore/Framework/interface/EventSetup.h"
00045 #include "FWCore/Framework/interface/ESHandle.h"
00046 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
00047 
00048 class MuScleFitMuonProducer : public edm::EDProducer {
00049    public:
00050       explicit MuScleFitMuonProducer(const edm::ParameterSet&);
00051       ~MuScleFitMuonProducer();
00052 
00053    private:
00054       virtual void beginJob() ;
00055       virtual void produce(edm::Event&, const edm::EventSetup&);
00056       virtual void endJob() ;
00057       template<class T> std::auto_ptr<T> applyCorrection(const edm::Handle<T> & allMuons);
00058 
00059   edm::InputTag theMuonLabel_;
00060   bool patMuons_;
00061   edm::ESHandle<MuScleFitDBobject> dbObject_;
00062   std::string dbObjectLabel_;
00063   unsigned long long dbObjectCacheId_;
00064   boost::shared_ptr<MomentumScaleCorrector> corrector_;
00065 };
00066 
00067 MuScleFitMuonProducer::MuScleFitMuonProducer(const edm::ParameterSet& iConfig) :
00068   theMuonLabel_( iConfig.getParameter<edm::InputTag>( "MuonLabel" ) ),
00069   patMuons_( iConfig.getParameter<bool>( "PatMuons" ) ),
00070   dbObjectLabel_( iConfig.getUntrackedParameter<std::string>("DbObjectLabel", "") ),
00071   dbObjectCacheId_(0)
00072 {
00073   if ( patMuons_ == true ) {
00074     produces<pat::MuonCollection>();
00075   } else {
00076     produces<reco::MuonCollection>();
00077   }
00078 }
00079 
00080 
00081 MuScleFitMuonProducer::~MuScleFitMuonProducer()
00082 {
00083 }
00084 
00085 
00086 template<class T>
00087 std::auto_ptr<T> MuScleFitMuonProducer::applyCorrection(const edm::Handle<T> & allMuons)
00088 {
00089   std::auto_ptr<T> pOut(new T);
00090 
00091   // Apply the correction and produce the new muons
00092   for( typename T::const_iterator muon = allMuons->begin(); muon != allMuons->end(); ++muon ) {
00093 
00094     //std::cout << "Pt before correction = " << muon->pt() << std::endl;
00095     double pt = (*corrector_)(*muon);
00096     //std::cout << "Pt after correction = " << pt << std::endl;
00097     double eta = muon->eta();
00098     double phi = muon->phi();
00099 
00100     typename T::value_type * newMuon = muon->clone();
00101     newMuon->setP4( reco::Particle::PolarLorentzVector( pt, eta, phi, muon->mass() ) );
00102 
00103     pOut->push_back(*newMuon);
00104   }
00105   return pOut;
00106 }
00107 
00108 // ------------ method called to produce the data  ------------
00109 void MuScleFitMuonProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00110 {
00111   unsigned long long dbObjectCacheId = iSetup.get<MuScleFitDBobjectRcd>().cacheIdentifier();
00112   if ( dbObjectCacheId != dbObjectCacheId_ ) {
00113     if ( dbObjectLabel_ != "" ) {
00114       iSetup.get<MuScleFitDBobjectRcd>().get(dbObjectLabel_, dbObject_);
00115     } else {
00116       iSetup.get<MuScleFitDBobjectRcd>().get(dbObject_);
00117     }
00118   }
00119 
00120   //std::cout << "identifiers size from dbObject = " << dbObject_->identifiers.size() << std::endl;
00121   //std::cout << "parameters size from dbObject = " << dbObject_->parameters.size() << std::endl;;
00122 
00123   // Create the corrector and set the parameters
00124   corrector_.reset(new MomentumScaleCorrector( dbObject_.product() ) );
00125 
00126   if( patMuons_ == true ) {
00127     edm::Handle<pat::MuonCollection> allMuons;
00128     iEvent.getByLabel (theMuonLabel_, allMuons);
00129     iEvent.put(applyCorrection(allMuons));
00130   }
00131   else {
00132     edm::Handle<reco::MuonCollection> allMuons;
00133     iEvent.getByLabel (theMuonLabel_, allMuons);
00134     iEvent.put(applyCorrection(allMuons));
00135   }
00136 
00137   // put into the Event
00138   // iEvent.put(pOut);
00139   // iEvent.put(applyCorrection(allMuons));
00140   
00141 /*  std::auto_ptr<reco::MuonCollection> pOut(new reco::MuonCollection);
00142 
00143   // Apply the correction and produce the new muons
00144   for( std::vector<reco::Muon>::const_iterator muon = allMuons->begin(); muon != allMuons->end(); ++muon ) {
00145 
00146     double pt = (*corrector_)(*muon);
00147     double eta = muon->eta();
00148     double phi = muon->phi();
00149 
00150     reco::Muon * newMuon = muon->clone();
00151     newMuon->setP4( reco::Particle::PolarLorentzVector( pt, eta, phi, muon->mass() ) );
00152 
00153     pOut->push_back(*newMuon);
00154   }
00155 */
00156 
00157 }
00158 
00159 // ------------ method called once each job just before starting event loop  ------------
00160 void 
00161 MuScleFitMuonProducer::beginJob()
00162 {
00163 }
00164 
00165 // ------------ method called once each job just after ending the event loop  ------------
00166 void 
00167 MuScleFitMuonProducer::endJob()
00168 {
00169 }
00170 
00171 //define this as a plug-in
00172 DEFINE_FWK_MODULE(MuScleFitMuonProducer);