Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:01

0001 #include <map>
0002 #include <string>
0003 
0004 #include "TH1.h"
0005 
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0012 
0013 #include "DataFormats/PatCandidates/interface/Electron.h"
0014 #include "DataFormats/PatCandidates/interface/Muon.h"
0015 #include "DataFormats/PatCandidates/interface/Jet.h"
0016 #include "DataFormats/PatCandidates/interface/MET.h"
0017 
0018 class PatTopSelectionAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0019 public:
0020   /// default constructor
0021   explicit PatTopSelectionAnalyzer(const edm::ParameterSet&);
0022   /// default destructor
0023   ~PatTopSelectionAnalyzer() override;
0024 
0025 private:
0026   /// everything that needs to be done before the event loop
0027   void beginJob() override;
0028   /// everything that needs to be done during the event loop
0029   void analyze(const edm::Event&, const edm::EventSetup&) override;
0030   /// everything that needs to be done after the event loop
0031   void endJob() override;
0032 
0033   /// check if histogram was booked
0034   bool booked(const std::string histName) const { return hists_.find(histName) != hists_.end(); };
0035   /// fill histogram if it had been booked before
0036   void fill(const std::string histName, double value) const {
0037     if (booked(histName))
0038       hists_.find(histName)->second->Fill(value);
0039   };
0040 
0041   // simple map to contain all histograms;
0042   // histograms are booked in the beginJob()
0043   // method
0044   std::map<std::string, TH1F*> hists_;
0045 
0046   // input tags
0047   edm::EDGetTokenT<edm::View<pat::Electron> > elecsToken_;
0048   edm::EDGetTokenT<edm::View<pat::Muon> > muonsToken_;
0049   edm::EDGetTokenT<edm::View<pat::Jet> > jetsToken_;
0050   edm::EDGetTokenT<edm::View<pat::MET> > metToken_;
0051 };
0052 
0053 PatTopSelectionAnalyzer::PatTopSelectionAnalyzer(const edm::ParameterSet& iConfig)
0054     : hists_(),
0055       elecsToken_(consumes<edm::View<pat::Electron> >(iConfig.getUntrackedParameter<edm::InputTag>("elecs"))),
0056       muonsToken_(consumes<edm::View<pat::Muon> >(iConfig.getUntrackedParameter<edm::InputTag>("muons"))),
0057       jetsToken_(consumes<edm::View<pat::Jet> >(iConfig.getUntrackedParameter<edm::InputTag>("jets"))),
0058       metToken_(consumes<edm::View<pat::MET> >(iConfig.getUntrackedParameter<edm::InputTag>("met"))) {
0059   usesResource(TFileService::kSharedResource);
0060 }
0061 
0062 PatTopSelectionAnalyzer::~PatTopSelectionAnalyzer() {}
0063 
0064 void PatTopSelectionAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0065   // get electron collection
0066   edm::Handle<edm::View<pat::Electron> > elecs;
0067   iEvent.getByToken(elecsToken_, elecs);
0068 
0069   // get muon collection
0070   edm::Handle<edm::View<pat::Muon> > muons;
0071   iEvent.getByToken(muonsToken_, muons);
0072 
0073   // get jet collection
0074   edm::Handle<edm::View<pat::Jet> > jets;
0075   iEvent.getByToken(jetsToken_, jets);
0076 
0077   // get met collection
0078   edm::Handle<edm::View<pat::MET> > met;
0079   iEvent.getByToken(metToken_, met);
0080 
0081   // fill yield
0082   fill("yield", 0.5);
0083 
0084   // fill quantities for leading elec and elec multiplicity
0085   fill("elecMult", elecs->size());
0086   if (elecs->begin() != elecs->end()) {
0087     fill("elecIso", (elecs->begin()->trackIso() + elecs->begin()->caloIso()) / elecs->begin()->pt());
0088     fill("elecPt", elecs->begin()->pt());
0089   }
0090 
0091   // fill quantities for leading muon and muon multiplicity
0092   fill("muonMult", muons->size());
0093   if (muons->begin() != muons->end()) {
0094     fill("muonIso", (muons->begin()->trackIso() + muons->begin()->caloIso()) / muons->begin()->pt());
0095     fill("muonPt", muons->begin()->pt());
0096   }
0097 
0098   // fill quantities for leading jets and jet multiplicity
0099   // jet pt is corrected up to L3Absolute
0100   fill("jetMult", jets->size());
0101   if (!jets->empty())
0102     fill("jet0Pt", (*jets)[0].pt());
0103   if (jets->size() > 1)
0104     fill("jet1Pt", (*jets)[1].pt());
0105   if (jets->size() > 2)
0106     fill("jet2Pt", (*jets)[2].pt());
0107   if (jets->size() > 3)
0108     fill("jet3Pt", (*jets)[3].pt());
0109 
0110   // fill MET
0111   fill("met", met->empty() ? 0 : (*met)[0].et());
0112 }
0113 
0114 void PatTopSelectionAnalyzer::beginJob() {
0115   // register to the TFileService
0116   edm::Service<TFileService> fs;
0117 
0118   // book histograms:
0119   hists_["yield"] = fs->make<TH1F>("yield", "electron multiplicity", 1, 0., 1.);
0120   hists_["elecMult"] = fs->make<TH1F>("elecMult", "electron multiplicity", 10, 0., 10.);
0121   hists_["elecIso"] = fs->make<TH1F>("elecIso", "electron isolation", 20, 0., 1.);
0122   hists_["elecPt"] = fs->make<TH1F>("elecPt", "electron pt", 30, 0., 150.);
0123   hists_["muonMult"] = fs->make<TH1F>("muonMult", "muon multiplicity", 10, 0., 10.);
0124   hists_["muonIso"] = fs->make<TH1F>("muonIso", "muon isolation", 20, 0., 1.);
0125   hists_["muonPt"] = fs->make<TH1F>("muonPt", "muon pt", 30, 0., 150.);
0126   hists_["jetMult"] = fs->make<TH1F>("jetMult", "jet multiplicity", 15, 0., 15.);
0127   hists_["jet0Pt"] = fs->make<TH1F>("jet0Pt", "1. leading jet pt", 50, 0., 250.);
0128   hists_["jet1Pt"] = fs->make<TH1F>("jet1Pt", "1. leading jet pt", 50, 0., 250.);
0129   hists_["jet2Pt"] = fs->make<TH1F>("jet2Pt", "1. leading jet pt", 50, 0., 200.);
0130   hists_["jet3Pt"] = fs->make<TH1F>("jet3Pt", "1. leading jet pt", 50, 0., 200.);
0131   hists_["met"] = fs->make<TH1F>("met", "missing E_{T}", 25, 0., 200.);
0132 }
0133 
0134 void PatTopSelectionAnalyzer::endJob() {}
0135 
0136 #include "FWCore/Framework/interface/MakerMacros.h"
0137 DEFINE_FWK_MODULE(PatTopSelectionAnalyzer);