CMS 3D CMS Logo

FWLiteWithSelectorUtils.cc
Go to the documentation of this file.
1 #include <TH1F.h>
2 #include <TROOT.h>
3 #include <TFile.h>
4 #include <TSystem.h>
5 
9 
14 
15 #include "TStopwatch.h"
18 //#include "PhysicsTools/FWLite/interface/WSelectorFast.h"
19 
20 
21 int main(int argc, char* argv[])
22 {
23  // define what muon you are using; this is necessary as FWLite is not
24  // capable of reading edm::Views
25  using reco::Muon;
26 
27  // ----------------------------------------------------------------------
28  // First Part:
29  //
30  // * enable FWLite
31  // * book the histograms of interest
32  // * open the input file
33  // ----------------------------------------------------------------------
34 
35  // load framework libraries
36  gSystem->Load( "libFWCoreFWLite" );
38 
39  if ( argc < 2 ) {
40  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
41  return 0;
42  }
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; exit(0);
46  }
47  // get the python configuration
48  const edm::ParameterSet& process = edm::readPSetsFrom(argv[1])->getParameter<edm::ParameterSet>("process");
49  fwlite::InputSource inputHandler_(process); fwlite::OutputFiles outputHandler_(process);
50 
51  // initialize the W selector
53  WSelector wSelector( selection ); pat::strbitset wSelectorReturns = wSelector.getBitTemplate();
54 
55  // book a set of histograms
56  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file().c_str());
57  TFileDirectory theDir = fs.mkdir("analyzeBasicPat");
58  TH1F* muonPt_ = theDir.make<TH1F>("muonPt", "pt", 100, 0.,300.);
59  TH1F* muonEta_ = theDir.make<TH1F>("muonEta","eta", 100, -3., 3.);
60  TH1F* muonPhi_ = theDir.make<TH1F>("muonPhi","phi", 100, -5., 5.);
61 
62  // start a CPU timer
63  TStopwatch timer; timer.Start();
64 
65  // loop the events
66  int ievt=0;
67  unsigned int nEventsAnalyzed = 0;
68  int maxEvents_( inputHandler_.maxEvents() );
69  for(unsigned int iFile=0; iFile<inputHandler_.files().size(); ++iFile){
70  // open input file (can be located on castor)
71  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
72  if( inFile ){
73  // ----------------------------------------------------------------------
74  // Second Part:
75  //
76  // * loop the events in the input file
77  // * receive the collections of interest via fwlite::Handle
78  // * fill the histograms
79  // * after the loop close the input file
80  // ----------------------------------------------------------------------
81  fwlite::Event ev(inFile);
82  for(ev.toBegin(); !ev.atEnd(); ++ev, ++ievt){
83  edm::EventBase const & event = ev;
84  // break loop if maximal number of events is reached
85  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
86  // simple event counter
87  if(inputHandler_.reportAfter()!=0 ? (ievt>0 && ievt%inputHandler_.reportAfter()==0) : false)
88  std::cout << " processing event: " << ievt << std::endl;
89 
90  if ( wSelector(event, wSelectorReturns ) ) {
91  pat::Muon const & wMuon = wSelector.wMuon();
92  muonPt_ ->Fill( wMuon.pt() );
93  muonEta_->Fill( wMuon.eta() );
94  muonPhi_->Fill( wMuon.phi() );
95  }
96  ++nEventsAnalyzed;
97  }
98  // close input file
99  inFile->Close();
100  }
101  // break loop if maximal number of events is reached:
102  // this has to be done twice to stop the file loop as well
103  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
104  }
105  // stop CPU timer
106  timer.Stop();
107 
108  // print selector
109  wSelector.print(std::cout);
110 
111  // print some timing statistics
112  double rtime = timer.RealTime();
113  double ctime = timer.CpuTime ();
114  // timing printouts
115  printf("Analyzed events: %d \n",nEventsAnalyzed);
116  printf("RealTime=%f seconds, CpuTime=%f seconds\n",rtime,ctime);
117  printf("%4.2f events / RealTime second .\n", (double)nEventsAnalyzed/rtime);
118  printf("%4.2f events / CpuTime second .\n", (double)nEventsAnalyzed/ctime);
119  return 0;
120 }
T getParameter(std::string const &) const
virtual double pt() const final
transverse momentum
virtual double eta() const final
momentum pseudorapidity
std::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
selection
main part
Definition: corrVsCorr.py:98
bool ev
virtual double phi() const final
momentum azimuthal angle
Event const & toBegin()
Go to the very first Event.
Definition: Event.cc:240
static void enable()
enable automatic library loading
T * make(const Args &...args) const
make new ROOT object
int main(int argc, char *argv[])
virtual bool atEnd() const
Definition: Event.cc:283
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Example class of an EventSelector to apply a simple W Boson selection.
Definition: WSelector.h:15
Analysis-level muon class.
Definition: Muon.h:49
Definition: event.py:1
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:29