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
57  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file());
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 }
TFileService.h
TFileDirectory::mkdir
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileDirectory.cc:112
main
int main(int argc, char *argv[])
Definition: FWLiteWithSelectorUtils.cc:20
Handle.h
cmsBatch.argv
argv
Definition: cmsBatch.py:279
WSelector
Example class of an EventSelector to apply a simple W Boson selection.
Definition: WSelector.h:16
dir2webdir.argc
argc
Definition: dir2webdir.py:39
edm::readPSetsFrom
std::unique_ptr< edm::ParameterSet > readPSetsFrom(std::string const &fileOrString)
Definition: ParameterSetReader.cc:23
TFileDirectory::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileDirectory.h:53
gather_cfg.cout
cout
Definition: gather_cfg.py:144
TFileDirectory
Definition: TFileDirectory.h:24
reco::LeafCandidate::pt
double pt() const final
transverse momentum
Definition: LeafCandidate.h:146
pat::Muon
Analysis-level muon class.
Definition: Muon.h:51
fwlite::InputSource
Definition: InputSource.h:19
align_cfg.TFileService
TFileService
Definition: align_cfg.py:143
InputSource.h
corrVsCorr.selection
selection
main part
Definition: corrVsCorr.py:100
ParameterSetReader.h
WSelector.h
fwlite::OutputFiles
Definition: OutputFiles.h:19
FWLiteEnabler::enable
static void enable()
enable automatic library loading
Definition: FWLiteEnabler.cc:46
OutputFiles.h
edm::ParameterSet
Definition: ParameterSet.h:47
reco::LeafCandidate::eta
double eta() const final
momentum pseudorapidity
Definition: LeafCandidate.h:152
LaserDQM_cfg.process
process
Definition: LaserDQM_cfg.py:3
dumpRecoGeometry_cfg.Muon
Muon
Definition: dumpRecoGeometry_cfg.py:190
pat::strbitset
Definition: strbitset.h:23
fwlite::Event
Definition: Event.h:87
reco::LeafCandidate::phi
double phi() const final
momentum azimuthal angle
Definition: LeafCandidate.h:148
fwlite::OutputFiles::file
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:27
Event.h
ev
bool ev
Definition: Hydjet2Hadronizer.cc:95
edm::EventBase
Definition: EventBase.h:46
fwlite::TFileService
Definition: TFileService.h:12
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
ParameterSet.h
event
Definition: event.py:1
FWLiteEnabler.h