CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/PhysicsTools/PatExamples/plugins/PatMCMatchingExtended.cc

Go to the documentation of this file.
00001 #include <map>
00002 #include <string>
00003 
00004 #include "TH1.h"
00005 
00006 #include "FWCore/Framework/interface/Event.h"
00007 #include "FWCore/Framework/interface/EDAnalyzer.h"
00008 #include "FWCore/Utilities/interface/InputTag.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 #include "FWCore/ServiceRegistry/interface/Service.h"
00011 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00012 #include "Math/VectorUtil.h"
00013 
00014 
00015 class PatMCMatchingExtended : public edm::EDAnalyzer {
00016 
00017 public:
00019   explicit PatMCMatchingExtended(const edm::ParameterSet&);
00021   ~PatMCMatchingExtended();
00022   
00023 private:
00024 
00025   virtual void beginJob() ;
00026   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00027   virtual void endJob() ;
00028   
00029   // simple map to contain all histograms; 
00030   // histograms are booked in the beginJob() 
00031   // method
00032   std::map<std::string,TH1F*> histContainer_; 
00033 
00034   // input tags  
00035   edm::InputTag muonSrc_;
00036 
00037   //counts how often a genParticle with different charge gives a match
00038   unsigned int diffCharge;
00039 
00040   //how many muons have no match
00041   unsigned int noMatch;
00042 
00043   //how many muons have no status 1 or 3 match, but decay in flight
00044   unsigned int decayInFlight;
00045 
00046   //count the number of muons in all events
00047   unsigned int numberMuons;
00048 
00049 };
00050 
00051 
00052 #include "DataFormats/PatCandidates/interface/Muon.h"
00053 
00054 
00055 PatMCMatchingExtended::PatMCMatchingExtended(const edm::ParameterSet& iConfig):
00056   histContainer_(),
00057   muonSrc_(iConfig.getUntrackedParameter<edm::InputTag>("muonSrc"))
00058 {
00059 }
00060 
00061 PatMCMatchingExtended::~PatMCMatchingExtended()
00062 {
00063 }
00064 
00065 void
00066 PatMCMatchingExtended::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00067 {
00068 
00069   // get muon collection
00070   edm::Handle<edm::View<pat::Muon> > muons;
00071   iEvent.getByLabel(muonSrc_,muons);
00072 
00073   for(edm::View<pat::Muon>::const_iterator muon=muons->begin(); muon!=muons->end(); ++muon){
00074     if(muon->genParticleById(0,1).isNonnull() ){
00075       histContainer_["DR_status1Match"]->Fill( ROOT::Math::VectorUtil::DeltaR(muon->p4() , (muon->genParticleById(0,1) )->p4() ) ); 
00076       histContainer_["DPt_status1Match"]->Fill(muon->pt() - (muon->genParticleById(0,1) )->pt() );
00077     }
00078     if(muon->genParticleById(0,3).isNonnull() ){
00079       histContainer_["DR_status3Match"]->Fill( ROOT::Math::VectorUtil::DeltaR(muon->p4() , (muon->genParticleById(0,3) )->p4() ) );
00080       histContainer_["DPt_status3Match"]->Fill(muon->pt() - (muon->genParticleById(0,3) )->pt() );
00081     }
00082     if(muon->genParticleById(0,-1).isNonnull() ){
00083       histContainer_["DR_defaultMatch"]->Fill( ROOT::Math::VectorUtil::DeltaR(muon->p4() , (muon->genParticleById(0,-1) )->p4() ) );
00084       histContainer_["DPt_defaultMatch"]->Fill(muon->pt() - (muon->genParticleById(0,-1) )->pt() );
00085     }
00086     if(muon->genParticleById(0,1).isNull() && muon->genParticleById(0,3).isNull() && muon->genParticleById(0,-1).isNull()) noMatch++;
00087     if(muon->genParticleById(0,1).isNull() && muon->genParticleById(0,3).isNull() && muon->genParticleById(0,-1).isNonnull())decayInFlight++;
00088     
00089     
00090     
00091     if( muon->genParticleById(-13,0, 1).isNonnull() ){
00092       diffCharge++;
00093       std::cout<<" DIFF CHARGE!!! charge gen: "<< muon->genParticleById(-13,0, true)->charge()<< " charge reco: "<< muon->charge()<<std::endl;
00094     }
00095     numberMuons++;  
00096   }
00097   
00098 }
00099 
00100 void 
00101 PatMCMatchingExtended::beginJob()
00102 {
00103   // register to the TFileService
00104   edm::Service<TFileService> fs;
00105   
00106   // book histograms:
00107   //DR
00108   histContainer_["DR_defaultMatch"  ]=fs->make<TH1F>("DR_defaultMatch",   "DR_defaultMatch",     100, 0,  0.02);
00109   histContainer_["DR_status1Match"  ]=fs->make<TH1F>("DR_status1Match",   "DR_status1Match",     100, 0,  0.02);
00110   histContainer_["DR_status3Match"  ]=fs->make<TH1F>("DR_status3Match",   "DR_status3Match",     100, 0,  0.02);
00111   //DPT
00112   histContainer_["DPt_defaultMatch"  ]=fs->make<TH1F>("DPt_defaultMatch",   "DPt_defaultMatch",     10, 0,  1.2);
00113   histContainer_["DPt_status1Match"  ]=fs->make<TH1F>("DPt_status1Match",   "DPt_status1Match",     10, 0,  1.2);
00114   histContainer_["DPt_status3Match"  ]=fs->make<TH1F>("DPt_status3Match",   "DPt_status3Match",     10, 0,  1.2);
00115   //some counters
00116   diffCharge=0;
00117   noMatch=0;
00118   decayInFlight=0;
00119   numberMuons=0;
00120 
00121 }
00122 
00123 void 
00124 PatMCMatchingExtended::endJob() 
00125 {
00126     std::cout<<"diffcharge: "<< diffCharge <<std::endl;
00127     std::cout<<"noMatch: "<<  noMatch <<std::endl;
00128     std::cout<<"decayInFlight: "<< decayInFlight <<std::endl;
00129     std::cout<<"numberMuons: "<< numberMuons <<std::endl;
00130 }
00131 
00132 #include "FWCore/Framework/interface/MakerMacros.h"
00133 DEFINE_FWK_MODULE(PatMCMatchingExtended);
00134 
00135