CMS 3D CMS Logo

Functions
PatBasicFWLiteAnalyzer.cc File Reference
#include <memory>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include <TH1F.h>
#include <TROOT.h>
#include <TFile.h>
#include <TSystem.h>
#include "DataFormats/FWLite/interface/Event.h"
#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/FWLite/interface/FWLiteEnabler.h"
#include "DataFormats/FWLite/interface/InputSource.h"
#include "DataFormats/FWLite/interface/OutputFiles.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/PythonParameterSet/interface/MakeParameterSets.h"
#include "DataFormats/PatCandidates/interface/Muon.h"
#include "PhysicsTools/FWLite/interface/TFileService.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file PatBasicFWLiteAnalyzer.cc.

References fwlite::Event::atEnd(), gather_cfg::cout, dir, FWLiteEnabler::enable(), PVValHelper::eta, ev, cmsRelvalreport::exit, fwlite::OutputFiles::file(), edm::ParameterSet::getParameter(), mps_fire::i, TFileDirectory::make(), TFileDirectory::mkdir(), nano_cff::muons, PVValHelper::phi, LaserDQM_cfg::process, EnergyCorrector::pt, edm::readPSetsFrom(), align_cfg::TFileService, and fwlite::Event::toBegin().

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());
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
std::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
bool ev
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