CMS 3D CMS Logo

PatBasicFWLiteAnalyzer.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 
16 
21 
24 
25 int main(int argc, char* argv[]) {
26  // ----------------------------------------------------------------------
27  // First Part:
28  //
29  // * enable FWLite
30  // * book the histograms of interest
31  // * open the input file
32  // ----------------------------------------------------------------------
33 
34  // load framework libraries
35  gSystem->Load("libFWCoreFWLite");
37 
38  // only allow one argument for this simple example which should be the
39  // the python cfg file
40  if (argc < 2) {
41  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
42  return 0;
43  }
44  if (!edm::readPSetsFrom(argv[1])->existsAs<edm::ParameterSet>("process")) {
45  std::cout << " ERROR: ParametersSet 'process' is missing in your configuration file" << std::endl;
46  exit(0);
47  }
48  // get the python configuration
49  const edm::ParameterSet& process = edm::readPSetsFrom(argv[1])->getParameter<edm::ParameterSet>("process");
50  fwlite::InputSource inputHandler_(process);
51  fwlite::OutputFiles outputHandler_(process);
52 
53  // now get each parameter
54  const edm::ParameterSet& ana = process.getParameter<edm::ParameterSet>("muonAnalyzer");
55  edm::InputTag muons_(ana.getParameter<edm::InputTag>("muons"));
56 
57  // book a set of histograms
59  TFileDirectory dir = fs.mkdir("analyzeBasicPat");
60  TH1F* muonPt_ = dir.make<TH1F>("muonPt", "pt", 100, 0., 300.);
61  TH1F* muonEta_ = dir.make<TH1F>("muonEta", "eta", 100, -3., 3.);
62  TH1F* muonPhi_ = dir.make<TH1F>("muonPhi", "phi", 100, -5., 5.);
63 
64  // loop the events
65  int ievt = 0;
66  int maxEvents_(inputHandler_.maxEvents());
67  for (unsigned int iFile = 0; iFile < inputHandler_.files().size(); ++iFile) {
68  // open input file (can be located on castor)
69  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
70  if (inFile) {
71  // ----------------------------------------------------------------------
72  // Second Part:
73  //
74  // * loop the events in the input file
75  // * receive the collections of interest via fwlite::Handle
76  // * fill the histograms
77  // * after the loop close the input file
78  // ----------------------------------------------------------------------
79  fwlite::Event ev(inFile);
80  for (ev.toBegin(); !ev.atEnd(); ++ev, ++ievt) {
81  edm::EventBase const& event = ev;
82  // break loop if maximal number of events is reached
83  if (maxEvents_ > 0 ? ievt + 1 > maxEvents_ : false)
84  break;
85  // simple event counter
86  if (inputHandler_.reportAfter() != 0 ? (ievt > 0 && ievt % inputHandler_.reportAfter() == 0) : false)
87  std::cout << " processing event: " << ievt << std::endl;
88 
89  // Handle to the muon collection
91  event.getByLabel(muons_, muons);
92 
93  // loop muon collection and fill histograms
94  for (unsigned i = 0; i < muons->size(); ++i) {
95  muonPt_->Fill((*muons)[i].pt());
96  muonEta_->Fill((*muons)[i].eta());
97  muonPhi_->Fill((*muons)[i].phi());
98  }
99  }
100  // close input file
101  inFile->Close();
102  }
103  // break loop if maximal number of events is reached:
104  // this has to be done twice to stop the file loop as well
105  if (maxEvents_ > 0 ? ievt + 1 > maxEvents_ : false)
106  break;
107  }
108  return 0;
109 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
int main(int argc, char *argv[])
static void enable()
enable automatic library loading
std::unique_ptr< edm::ParameterSet > readPSetsFrom(std::string const &fileOrString)
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:27
def exit(msg="")