CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/ElectroWeakAnalysis/Utilities/src/MuonWithPFIsoProducer.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 
00006 #include "MuonAnalysis/MomentumScaleCalibration/interface/MomentumScaleCorrector.h"
00007 #include "MuonAnalysis/MomentumScaleCalibration/interface/ResolutionFunction.h"
00008 
00009 //
00010 // class declaration
00011 //
00012 class MuonWithPFIsoProducer : public edm::EDProducer {
00013    public:
00014       explicit MuonWithPFIsoProducer(const edm::ParameterSet&);
00015       ~MuonWithPFIsoProducer();
00016 
00017    private:
00018       virtual void beginJob() ;
00019       virtual void produce(edm::Event&, const edm::EventSetup&);
00020       virtual void endJob() ;
00021 
00022       edm::InputTag muonTag_;
00023       edm::InputTag pfTag_;
00024 
00025       bool usePfMuonsOnly_;
00026 
00027       double trackIsoVeto_;
00028       double gammaIsoVeto_;
00029       double neutralHadronIsoVeto_;
00030 };
00031 
00032 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00033 #include "DataFormats/Common/interface/Handle.h"
00034 #include "DataFormats/Common/interface/View.h"
00035 #include "FWCore/Framework/interface/Event.h"
00036 #include "DataFormats/MuonReco/interface/Muon.h"
00037 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00038 #include "DataFormats/TrackReco/interface/Track.h"
00039 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00040 
00041 #include "DataFormats/GeometryVector/interface/VectorUtil.h"
00042 
00044 MuonWithPFIsoProducer::MuonWithPFIsoProducer(const edm::ParameterSet& pset) {
00045 
00046   // What is being produced
00047       produces<std::vector<reco::Muon> >();
00048 
00049   // Muon collection
00050       muonTag_ = pset.getUntrackedParameter<edm::InputTag> ("MuonTag", edm::InputTag("muons"));
00051 
00052   // PF candidate collection
00053       pfTag_ = pset.getUntrackedParameter<edm::InputTag> ("PFTag", edm::InputTag("particleFlow"));
00054 
00055   // Use only PF muons to get exact consistency with PfMET
00056       usePfMuonsOnly_ = pset.getUntrackedParameter<bool> ("UsePfMuonsOnly", false);
00057 
00058   // Veto cone
00059       trackIsoVeto_ = pset.getUntrackedParameter<double> ("TrackIsoVeto", 0.01);
00060       gammaIsoVeto_ = pset.getUntrackedParameter<double> ("GammaIsoVeto", 0.07);
00061       neutralHadronIsoVeto_ = pset.getUntrackedParameter<double> ("NeutralHadronIsoVeto", 0.1);
00062 
00063 } 
00064 
00066 MuonWithPFIsoProducer::~MuonWithPFIsoProducer(){
00067 }
00068 
00070 void MuonWithPFIsoProducer::beginJob() {
00071 }
00072 
00074 void MuonWithPFIsoProducer::endJob(){
00075 }
00076 
00078 void MuonWithPFIsoProducer::produce(edm::Event& ev, const edm::EventSetup& iSetup) {
00079 
00080       // Initialize pointer to new output muon collection
00081       std::auto_ptr<reco::MuonCollection> newmuons (new reco::MuonCollection);
00082 
00083       // Get Muon collection
00084       edm::Handle<edm::View<reco::Muon> > muonCollection;
00085       if (!ev.getByLabel(muonTag_, muonCollection)) {
00086             edm::LogError("") << ">>> Muon collection does not exist !!!";
00087             ev.put(newmuons);
00088             return;
00089       }
00090 
00091       // Get PFCandidate collection
00092       edm::Handle<edm::View<reco::PFCandidate> > pfCollection;
00093       if (!ev.getByLabel(pfTag_, pfCollection)) {
00094             edm::LogError("") << ">>> PFCandidate collection does not exist !!!";
00095             ev.put(newmuons);
00096             return;
00097       }
00098 
00099       // Loop over Pf candidates to find muons and collect deposits in veto, 
00100       // dR<0.3 and dR<0.5 cones. Interpret "track" as charged particles (e,mu,
00101       // chraged hadrons). Interpret "em" as photons and also as electromagnetic 
00102       // energy in HF. Interpret "had" as neutral hadrons and also as hadronic
00103       // energy in HF. Apply weights if requested at input level.
00104       // HO energies are not filled. Ditto for jet energies around the muon.
00105       unsigned int muonCollectionSize = muonCollection->size();
00106       unsigned int pfCollectionSize = pfCollection->size();
00107       for (unsigned int i=0; i<muonCollectionSize; i++) {
00108             edm::RefToBase<reco::Muon> mu = muonCollection->refAt(i);
00109 
00110             // Ask for PfMuon consistency if requested
00111             bool muonFound = false;
00112 
00113             // Starting bycloning this muon
00114             reco::Muon* newmu = mu->clone();
00115             reco::TrackRef tk = mu->innerTrack();
00116 
00117             // Set isolations
00118             reco::MuonIsolation iso03;
00119             reco::MuonIsolation iso05;
00120             // Loop on all candidates
00121             for (unsigned int j=0; j<pfCollectionSize; j++) {
00122                   edm::RefToBase<reco::PFCandidate> pf = pfCollection->refAt(j);
00123 
00124                   // Check the muon is in the PF collection when required
00125                   bool thisIsTheMuon = false;
00126                   if (tk.isNonnull() && pf->trackRef()==tk) {
00127                         thisIsTheMuon = true;
00128                         muonFound = true;
00129                   }
00130                          
00131                   // Get dR. Nothing to add if dR>0.5
00132                   double deltaR = Geom::deltaR(mu->momentum(),pf->momentum());
00133                   if (deltaR>0.5) continue;
00134 
00135                   // Fill "tracker" components
00136                   if (   pf->particleId()==reco::PFCandidate::h
00137                       || pf->particleId()==reco::PFCandidate::e
00138                       || pf->particleId()==reco::PFCandidate::mu ) {
00139                         if (deltaR<trackIsoVeto_ || thisIsTheMuon) {
00140                               iso05.trackerVetoPt += pf->pt();
00141                               iso03.trackerVetoPt += pf->pt();
00142                         } else {
00143                               iso05.sumPt += pf->pt();
00144                               iso05.nTracks++;
00145                               if (deltaR<0.3) {
00146                                     iso03.sumPt += pf->pt();
00147                                     iso03.nTracks++;
00148                               }
00149                         }
00150                   // Fill "em" components
00151                   } else if (   pf->particleId()==reco::PFCandidate::gamma 
00152                              || pf->particleId()==reco::PFCandidate::egamma_HF) {
00153                         if (deltaR<gammaIsoVeto_) {
00154                               iso05.emVetoEt += pf->pt();
00155                               iso03.emVetoEt += pf->pt();
00156                         } else {
00157                               iso05.emEt += pf->pt();
00158                               if (deltaR<0.3) iso03.emEt += pf->pt();
00159                         }
00160                   // Fill "had" components
00161                   } else if (   pf->particleId()==reco::PFCandidate::h0
00162                              || pf->particleId()==reco::PFCandidate::h_HF) {
00163                         if (deltaR<neutralHadronIsoVeto_) {
00164                               iso05.hadVetoEt += pf->pt();
00165                               iso03.hadVetoEt += pf->pt();
00166                         } else {
00167                               iso05.hadEt += pf->pt();
00168                               if (deltaR<0.3) iso03.hadEt += pf->pt();
00169                         }
00170                   }
00171             }
00172 
00173             // Do not take this muon (under explicit request) if it is not a PfMuon
00174             if (usePfMuonsOnly_ && (!muonFound)) continue;
00175 
00176             // Set this isolation information in the new muon
00177             newmu->setIsolation(iso03,iso05);
00178             
00179             // Add new muon to output collection
00180             newmuons->push_back(*newmu);
00181 
00182       }
00183 
00184       // Add output collection to event
00185       ev.put(newmuons);
00186 }
00187 
00188 DEFINE_FWK_MODULE(MuonWithPFIsoProducer);