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 
26 int main(int argc, char* argv[])
27 {
28  // ----------------------------------------------------------------------
29  // First Part:
30  //
31  // * enable FWLite
32  // * book the histograms of interest
33  // * open the input file
34  // ----------------------------------------------------------------------
35 
36  // load framework libraries
37  gSystem->Load( "libFWCoreFWLite" );
39 
40  // only allow one argument for this simple example which should be the
41  // the python cfg file
42  if ( argc < 2 ) {
43  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
44  return 0;
45  }
46  if( !edm::readPSetsFrom(argv[1])->existsAs<edm::ParameterSet>("process") ){
47  std::cout << " ERROR: ParametersSet 'process' is missing in your configuration file" << std::endl; exit(0);
48  }
49  // get the python configuration
50  const edm::ParameterSet& process = edm::readPSetsFrom(argv[1])->getParameter<edm::ParameterSet>("process");
51  fwlite::InputSource inputHandler_(process); fwlite::OutputFiles outputHandler_(process);
52 
53 
54  // now get each parameter
55  const edm::ParameterSet& ana = process.getParameter<edm::ParameterSet>("muonAnalyzer");
56  edm::InputTag muons_( ana.getParameter<edm::InputTag>("muons") );
57 
58  // book a set of histograms
59  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file().c_str());
60  TFileDirectory dir = fs.mkdir("analyzeBasicPat");
61  TH1F* muonPt_ = dir.make<TH1F>("muonPt", "pt", 100, 0.,300.);
62  TH1F* muonEta_ = dir.make<TH1F>("muonEta","eta", 100, -3., 3.);
63  TH1F* muonPhi_ = dir.make<TH1F>("muonPhi","phi", 100, -5., 5.);
64 
65  // loop the events
66  int ievt=0;
67  int maxEvents_( inputHandler_.maxEvents() );
68  for(unsigned int iFile=0; iFile<inputHandler_.files().size(); ++iFile){
69  // open input file (can be located on castor)
70  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
71  if( inFile ){
72  // ----------------------------------------------------------------------
73  // Second Part:
74  //
75  // * loop the events in the input file
76  // * receive the collections of interest via fwlite::Handle
77  // * fill the histograms
78  // * after the loop close the input file
79  // ----------------------------------------------------------------------
80  fwlite::Event ev(inFile);
81  for(ev.toBegin(); !ev.atEnd(); ++ev, ++ievt){
82  edm::EventBase const & event = ev;
83  // break loop if maximal number of events is reached
84  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) 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) break;
106  }
107  return 0;
108 }
T getParameter(std::string const &) const
Event const & toBegin() override
Go to the very first Event.
Definition: Event.cc:240
std::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
bool ev
int main(int argc, char *argv[])
bool atEnd() const override
Definition: Event.cc:283
static void enable()
enable automatic library loading
T * make(const Args &...args) const
make new ROOT object
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
dbl *** dir
Definition: mlp_gen.cc:35
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:29