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