Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-10 02:51:17

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/Photon.h"
0015 #include "DataFormats/PatCandidates/interface/Muon.h"
0016 #include "DataFormats/PatCandidates/interface/Tau.h"
0017 #include "DataFormats/PatCandidates/interface/Jet.h"
0018 #include "DataFormats/PatCandidates/interface/MET.h"
0019 
0020 class PatBasicAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0021 public:
0022   /// default constructor
0023   explicit PatBasicAnalyzer(const edm::ParameterSet&);
0024   /// default destructor
0025   ~PatBasicAnalyzer() override;
0026 
0027 private:
0028   /// everything that needs to be done before the event loop
0029   void beginJob() override;
0030   /// everything that needs to be done during the event loop
0031   void analyze(const edm::Event&, const edm::EventSetup&) override;
0032   /// everything that needs to be done after the event loop
0033   void endJob() override;
0034 
0035   // simple map to contain all histograms;
0036   // histograms are booked in the beginJob()
0037   // method
0038   std::map<std::string, TH1F*> histContainer_;
0039   // plot number of towers per jet
0040   TH1F* jetTowers_;
0041 
0042   // input tokens
0043   edm::EDGetTokenT<edm::View<pat::Photon>> photonSrcToken_;
0044   edm::EDGetTokenT<edm::View<pat::Electron>> elecSrcToken_;
0045   edm::EDGetTokenT<edm::View<pat::Muon>> muonSrcToken_;
0046   edm::EDGetTokenT<edm::View<pat::Tau>> tauSrcToken_;
0047   edm::EDGetTokenT<edm::View<pat::Jet>> jetSrcToken_;
0048   edm::EDGetTokenT<edm::View<pat::MET>> metSrcToken_;
0049 };
0050 
0051 PatBasicAnalyzer::PatBasicAnalyzer(const edm::ParameterSet& iConfig)
0052     : histContainer_(),
0053       photonSrcToken_(consumes<edm::View<pat::Photon>>(iConfig.getUntrackedParameter<edm::InputTag>("photonSrc"))),
0054       elecSrcToken_(consumes<edm::View<pat::Electron>>(iConfig.getUntrackedParameter<edm::InputTag>("electronSrc"))),
0055       muonSrcToken_(consumes<edm::View<pat::Muon>>(iConfig.getUntrackedParameter<edm::InputTag>("muonSrc"))),
0056       tauSrcToken_(consumes<edm::View<pat::Tau>>(iConfig.getUntrackedParameter<edm::InputTag>("tauSrc"))),
0057       jetSrcToken_(consumes<edm::View<pat::Jet>>(iConfig.getUntrackedParameter<edm::InputTag>("jetSrc"))),
0058       metSrcToken_(consumes<edm::View<pat::MET>>(iConfig.getUntrackedParameter<edm::InputTag>("metSrc"))) {
0059   usesResource(TFileService::kSharedResource);
0060 }
0061 
0062 PatBasicAnalyzer::~PatBasicAnalyzer() {}
0063 
0064 void PatBasicAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0065   // get electron collection
0066   edm::Handle<edm::View<pat::Electron>> electrons;
0067   iEvent.getByToken(elecSrcToken_, electrons);
0068 
0069   // get muon collection
0070   edm::Handle<edm::View<pat::Muon>> muons;
0071   iEvent.getByToken(muonSrcToken_, muons);
0072 
0073   // get tau collection
0074   edm::Handle<edm::View<pat::Tau>> taus;
0075   iEvent.getByToken(tauSrcToken_, taus);
0076 
0077   // get jet collection
0078   edm::Handle<edm::View<pat::Jet>> jets;
0079   iEvent.getByToken(jetSrcToken_, jets);
0080 
0081   // get met collection
0082   edm::Handle<edm::View<pat::MET>> mets;
0083   iEvent.getByToken(metSrcToken_, mets);
0084 
0085   // get photon collection
0086   edm::Handle<edm::View<pat::Photon>> photons;
0087   iEvent.getByToken(photonSrcToken_, photons);
0088 
0089   // loop over jets
0090   size_t nJets = 0;
0091   for (edm::View<pat::Jet>::const_iterator jet = jets->begin(); jet != jets->end(); ++jet) {
0092     if (jet->pt() > 50) {
0093       ++nJets;
0094     }
0095     // uncomment the following line to fill the
0096     // jetTowers_ histogram
0097     // jetTowers_->Fill(jet->getCaloConstituents().size());
0098   }
0099   histContainer_["jets"]->Fill(nJets);
0100 
0101   // do something similar for the other candidates
0102   histContainer_["photons"]->Fill(photons->size());
0103   histContainer_["elecs"]->Fill(electrons->size());
0104   histContainer_["muons"]->Fill(muons->size());
0105   histContainer_["taus"]->Fill(taus->size());
0106   histContainer_["met"]->Fill(mets->empty() ? 0 : (*mets)[0].et());
0107 }
0108 
0109 void PatBasicAnalyzer::beginJob() {
0110   // register to the TFileService
0111   edm::Service<TFileService> fs;
0112 
0113   // book histograms:
0114   // uncomment the following line to book the jetTowers_ histogram
0115   //jetTowers_= fs->make<TH1F>("jetTowers", "towers per jet",   90, 0,  90);
0116   histContainer_["photons"] = fs->make<TH1F>("photons", "photon multiplicity", 10, 0, 10);
0117   histContainer_["elecs"] = fs->make<TH1F>("elecs", "electron multiplicity", 10, 0, 10);
0118   histContainer_["muons"] = fs->make<TH1F>("muons", "muon multiplicity", 10, 0, 10);
0119   histContainer_["taus"] = fs->make<TH1F>("taus", "tau multiplicity", 10, 0, 10);
0120   histContainer_["jets"] = fs->make<TH1F>("jets", "jet multiplicity", 10, 0, 10);
0121   histContainer_["met"] = fs->make<TH1F>("met", "missing E_{T}", 20, 0, 100);
0122 }
0123 
0124 void PatBasicAnalyzer::endJob() {}
0125 
0126 #include "FWCore/Framework/interface/MakerMacros.h"
0127 DEFINE_FWK_MODULE(PatBasicAnalyzer);