Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:25:26

0001 // Implementation of template class: JetPlotsExample
0002 // Description:  Example of simple EDAnalyzer for jets.
0003 // Author: K. Kousouris
0004 // Date:  25 - August - 2008
0005 #include "RecoJets/JetAnalyzers/interface/JetPlotsExample.h"
0006 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
0007 #include "DataFormats/JetReco/interface/PFJetCollection.h"
0008 #include "DataFormats/JetReco/interface/GenJetCollection.h"
0009 #include "DataFormats/JetReco/interface/CaloJet.h"
0010 #include "DataFormats/JetReco/interface/PFJet.h"
0011 #include "DataFormats/JetReco/interface/GenJet.h"
0012 #include "DataFormats/JetReco/interface/JPTJet.h"
0013 #include "DataFormats/JetReco/interface/JPTJetCollection.h"
0014 
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include <TFile.h>
0018 #include <cmath>
0019 using namespace edm;
0020 using namespace reco;
0021 using namespace std;
0022 ////////////////////////////////////////////////////////////////////////////////////////
0023 template <class Jet>
0024 JetPlotsExample<Jet>::JetPlotsExample(edm::ParameterSet const& cfg) {
0025   JetAlgorithm = cfg.getParameter<std::string>("JetAlgorithm");
0026   HistoFileName = cfg.getParameter<std::string>("HistoFileName");
0027   NJets = cfg.getParameter<int>("NJets");
0028 }
0029 ////////////////////////////////////////////////////////////////////////////////////////
0030 template <class Jet>
0031 void JetPlotsExample<Jet>::beginJob() {
0032   TString hname;
0033   m_file = new TFile(HistoFileName.c_str(), "RECREATE");
0034   /////////// Booking histograms //////////////////////////
0035   hname = "JetPt";
0036   m_HistNames1D[hname] = new TH1F(hname, hname, 100, 0, 1000);
0037   hname = "JetEta";
0038   m_HistNames1D[hname] = new TH1F(hname, hname, 120, -6, 6);
0039   hname = "JetPhi";
0040   m_HistNames1D[hname] = new TH1F(hname, hname, 100, -M_PI, M_PI);
0041   hname = "NumberOfJets";
0042   m_HistNames1D[hname] = new TH1F(hname, hname, 100, 0, 100);
0043 }
0044 ////////////////////////////////////////////////////////////////////////////////////////
0045 template <class Jet>
0046 void JetPlotsExample<Jet>::analyze(edm::Event const& evt, edm::EventSetup const& iSetup) {
0047   /////////// Get the jet collection //////////////////////
0048   Handle<JetCollection> jets;
0049   evt.getByLabel(JetAlgorithm, jets);
0050   typename JetCollection::const_iterator i_jet;
0051   int index = 0;
0052   TString hname;
0053   /////////// Count the jets in the event /////////////////
0054   hname = "NumberOfJets";
0055   FillHist1D(hname, jets->size());
0056   /////////// Fill Histograms for the leading NJet jets ///
0057   for (i_jet = jets->begin(); i_jet != jets->end() && index < NJets; ++i_jet) {
0058     hname = "JetPt";
0059     FillHist1D(hname, i_jet->pt());
0060     hname = "JetEta";
0061     FillHist1D(hname, i_jet->eta());
0062     hname = "JetPhi";
0063     FillHist1D(hname, i_jet->phi());
0064     index++;
0065   }
0066 }
0067 ////////////////////////////////////////////////////////////////////////////////////////
0068 template <class Jet>
0069 void JetPlotsExample<Jet>::endJob() {
0070   /////////// Write Histograms in output ROOT file ////////
0071   if (m_file != nullptr) {
0072     m_file->cd();
0073     for (std::map<TString, TH1*>::iterator hid = m_HistNames1D.begin(); hid != m_HistNames1D.end(); hid++)
0074       hid->second->Write();
0075     delete m_file;
0076     m_file = nullptr;
0077   }
0078 }
0079 ////////////////////////////////////////////////////////////////////////////////////////
0080 template <class Jet>
0081 void JetPlotsExample<Jet>::FillHist1D(const TString& histName, const Double_t& value) {
0082   std::map<TString, TH1*>::iterator hid = m_HistNames1D.find(histName);
0083   if (hid == m_HistNames1D.end())
0084     std::cout << "%fillHist -- Could not find histogram with name: " << histName << std::endl;
0085   else
0086     hid->second->Fill(value);
0087 }
0088 /////////// Register Modules ////////
0089 #include "FWCore/Framework/interface/MakerMacros.h"
0090 /////////// Calo Jet Instance ////////
0091 typedef JetPlotsExample<CaloJet> CaloJetPlotsExample;
0092 DEFINE_FWK_MODULE(CaloJetPlotsExample);
0093 /////////// Cen Jet Instance ////////
0094 typedef JetPlotsExample<GenJet> GenJetPlotsExample;
0095 DEFINE_FWK_MODULE(GenJetPlotsExample);
0096 /////////// PF Jet Instance ////////
0097 typedef JetPlotsExample<PFJet> PFJetPlotsExample;
0098 DEFINE_FWK_MODULE(PFJetPlotsExample);
0099 /////////// JPT Jet Instance ////////
0100 typedef JetPlotsExample<JPTJet> JPTJetPlotsExample;
0101 DEFINE_FWK_MODULE(JPTJetPlotsExample);