CMS 3D CMS Logo

PatBasicFWLiteJetAnalyzer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <vector>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 
8 #include <TH1F.h>
9 #include <TROOT.h>
10 #include <TFile.h>
11 #include <TSystem.h>
12 
20 
21 int main(int argc, char* argv[]) {
22  // ----------------------------------------------------------------------
23  // First Part:
24  //
25  // * enable FWLite
26  // * book the histograms of interest
27  // * open the input file
28  // ----------------------------------------------------------------------
29 
30  // load framework libraries
31  gSystem->Load("libFWCoreFWLite");
33 
34  // only allow one argument for this simple example which should be the
35  // the python cfg file
36  if (argc < 2) {
37  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
38  return 0;
39  }
40 
41  // get the python configuration
42  ProcessDescImpl builder(argv[1]);
43  const edm::ParameterSet& fwliteParameters =
44  builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("FWLiteParams");
45 
46  // now get each parameter
47  std::string input_(fwliteParameters.getParameter<std::string>("inputFile"));
48  std::string output_(fwliteParameters.getParameter<std::string>("outputFile"));
49  edm::InputTag jets_(fwliteParameters.getParameter<edm::InputTag>("jets"));
50 
51  // book a set of histograms
53  TFileDirectory theDir = fs.mkdir("analyzeBasicPat");
54  TH1F* jetPt_ = theDir.make<TH1F>("jetPt", "pt", 100, 0., 300.);
55  TH1F* jetEta_ = theDir.make<TH1F>("jetEta", "eta", 100, -3., 3.);
56  TH1F* jetPhi_ = theDir.make<TH1F>("jetPhi", "phi", 100, -5., 5.);
57  TH1F* disc_ = theDir.make<TH1F>("disc", "Discriminant", 100, 0.0, 10.0);
58  TH1F* constituentPt_ = theDir.make<TH1F>("constituentPt", "Constituent pT", 100, 0, 300.0);
59 
60  // open input file (can be located on castor)
61  TFile* inFile = TFile::Open(input_.c_str());
62 
63  // ----------------------------------------------------------------------
64  // Second Part:
65  //
66  // * loop the events in the input file
67  // * receive the collections of interest via fwlite::Handle
68  // * fill the histograms
69  // * after the loop close the input file
70  // ----------------------------------------------------------------------
71 
72  // loop the events
73  unsigned int iEvent = 0;
74  fwlite::Event ev(inFile);
75  for (ev.toBegin(); !ev.atEnd(); ++ev, ++iEvent) {
76  edm::EventBase const& event = ev;
77 
78  // break loop after end of file is reached
79  // or after 1000 events have been processed
80  if (iEvent == 1000)
81  break;
82 
83  // simple event counter
84  if (iEvent > 0 && iEvent % 1 == 0) {
85  std::cout << " processing event: " << iEvent << std::endl;
86  }
87 
88  // Handle to the jet collection
91  event.getByLabel(jets_, jets);
92 
93  // loop jet collection and fill histograms
94  for (unsigned i = 0; i < jets->size(); ++i) {
95  // basic kinematics
96  jetPt_->Fill((*jets)[i].pt());
97  jetEta_->Fill((*jets)[i].eta());
98  jetPhi_->Fill((*jets)[i].phi());
99  // access tag infos
100  reco::SecondaryVertexTagInfo const* svTagInfos = (*jets)[i].tagInfoSecondaryVertex("secondaryVertex");
101  if (svTagInfos != nullptr) {
102  if (svTagInfos->nVertices() > 0) {
103  disc_->Fill(svTagInfos->flightDistance(0).value());
104  }
105  }
106  // access calo towers
107  std::vector<reco::PFCandidatePtr> const& pfConstituents = (*jets)[i].getPFConstituents();
108  for (std::vector<reco::PFCandidatePtr>::const_iterator ibegin = pfConstituents.begin(),
109  iend = pfConstituents.end(),
110  iconstituent = ibegin;
111  iconstituent != iend;
112  ++iconstituent) {
113  constituentPt_->Fill((*iconstituent)->pt());
114  }
115  }
116  }
117  // close input file
118  inFile->Close();
119 
120  // ----------------------------------------------------------------------
121  // Third Part:
122  //
123  // * never forget to free the memory of objects you created
124  // ----------------------------------------------------------------------
125 
126  // in this example there is nothing to do
127 
128  // that's it!
129  return 0;
130 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
int main(int argc, char *argv[])
T * make(const Args &...args) const
make new ROOT object
int iEvent
Definition: GenABIO.cc:224
static void enable()
enable automatic library loading
std::unique_ptr< edm::ProcessDesc > processDesc() const