CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/PhysicsTools/PatExamples/plugins/PatBasicAnalyzer.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 
00013 class PatBasicAnalyzer : public edm::EDAnalyzer {
00014 
00015 public:
00017   explicit PatBasicAnalyzer(const edm::ParameterSet&);
00019   ~PatBasicAnalyzer();
00020   
00021 private:
00023   virtual void beginJob() ;
00025   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   // plot number of towers per jet
00034   TH1F* jetTowers_;
00035 
00036   // input tags  
00037   edm::InputTag photonSrc_;
00038   edm::InputTag elecSrc_;
00039   edm::InputTag muonSrc_;
00040   edm::InputTag tauSrc_;
00041   edm::InputTag jetSrc_;
00042   edm::InputTag metSrc_;
00043 };
00044 
00045 #include "DataFormats/PatCandidates/interface/Electron.h"
00046 #include "DataFormats/PatCandidates/interface/Photon.h"
00047 #include "DataFormats/PatCandidates/interface/Muon.h"
00048 #include "DataFormats/PatCandidates/interface/Tau.h"
00049 #include "DataFormats/PatCandidates/interface/Jet.h"
00050 #include "DataFormats/PatCandidates/interface/MET.h"
00051 
00052 PatBasicAnalyzer::PatBasicAnalyzer(const edm::ParameterSet& iConfig):
00053   histContainer_(),
00054   photonSrc_(iConfig.getUntrackedParameter<edm::InputTag>("photonSrc")),
00055   elecSrc_(iConfig.getUntrackedParameter<edm::InputTag>("electronSrc")),
00056   muonSrc_(iConfig.getUntrackedParameter<edm::InputTag>("muonSrc")),
00057   tauSrc_(iConfig.getUntrackedParameter<edm::InputTag>("tauSrc" )),
00058   jetSrc_(iConfig.getUntrackedParameter<edm::InputTag>("jetSrc" )),
00059   metSrc_(iConfig.getUntrackedParameter<edm::InputTag>("metSrc" ))
00060 {
00061 }
00062 
00063 PatBasicAnalyzer::~PatBasicAnalyzer()
00064 {
00065 }
00066 
00067 void
00068 PatBasicAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00069 {
00070   // get electron collection
00071   edm::Handle<edm::View<pat::Electron> > electrons;
00072   iEvent.getByLabel(elecSrc_,electrons);
00073 
00074   // get muon collection
00075   edm::Handle<edm::View<pat::Muon> > muons;
00076   iEvent.getByLabel(muonSrc_,muons);
00077 
00078   // get tau collection  
00079   edm::Handle<edm::View<pat::Tau> > taus;
00080   iEvent.getByLabel(tauSrc_,taus);
00081 
00082   // get jet collection
00083   edm::Handle<edm::View<pat::Jet> > jets;
00084   iEvent.getByLabel(jetSrc_,jets);
00085 
00086   // get met collection  
00087   edm::Handle<edm::View<pat::MET> > mets;
00088   iEvent.getByLabel(metSrc_,mets);
00089   
00090   // get photon collection  
00091   edm::Handle<edm::View<pat::Photon> > photons;
00092   iEvent.getByLabel(photonSrc_,photons);
00093     
00094   // loop over jets
00095   size_t nJets=0;
00096   for(edm::View<pat::Jet>::const_iterator jet=jets->begin(); jet!=jets->end(); ++jet){
00097     if(jet->pt()>50){
00098       ++nJets;
00099     }
00100     // uncomment the following line to fill the 
00101     // jetTowers_ histogram
00102     // jetTowers_->Fill(jet->getCaloConstituents().size());
00103   }
00104   histContainer_["jets"]->Fill(nJets);
00105 
00106   // do something similar for the other candidates
00107   histContainer_["photons"]->Fill(photons->size() );
00108   histContainer_["elecs" ]->Fill(electrons->size());
00109   histContainer_["muons"]->Fill(muons->size() );
00110   histContainer_["taus" ]->Fill(taus->size()  );
00111   histContainer_["met"  ]->Fill(mets->empty() ? 0 : (*mets)[0].et());
00112 }
00113 
00114 void 
00115 PatBasicAnalyzer::beginJob()
00116 {
00117   // register to the TFileService
00118   edm::Service<TFileService> fs;
00119   
00120   // book histograms:
00121   // uncomment the following line to book the jetTowers_ histogram
00122   //jetTowers_= fs->make<TH1F>("jetTowers", "towers per jet",   90, 0,  90); 
00123   histContainer_["photons"]=fs->make<TH1F>("photons", "photon multiplicity",   10, 0,  10);
00124   histContainer_["elecs"  ]=fs->make<TH1F>("elecs",   "electron multiplicity", 10, 0,  10);
00125   histContainer_["muons"  ]=fs->make<TH1F>("muons",   "muon multiplicity",     10, 0,  10);
00126   histContainer_["taus"   ]=fs->make<TH1F>("taus",    "tau multiplicity",      10, 0,  10);
00127   histContainer_["jets"   ]=fs->make<TH1F>("jets",    "jet multiplicity",      10, 0,  10);
00128   histContainer_["met"    ]=fs->make<TH1F>("met",     "missing E_{T}",         20, 0, 100);
00129 }
00130 
00131 void 
00132 PatBasicAnalyzer::endJob() 
00133 {
00134 }
00135 
00136 #include "FWCore/Framework/interface/MakerMacros.h"
00137 DEFINE_FWK_MODULE(PatBasicAnalyzer);