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 int main(int argc, char* argv[]) {
26  // ----------------------------------------------------------------------
27  // First Part:
28  //
29  // * enable FWLite
30  // * book the histograms of interest
31  // * open the input file
32  // ----------------------------------------------------------------------
33 
34  // load framework libraries
35  gSystem->Load("libFWCoreFWLite");
37 
38  // only allow one argument for this simple example which should be the
39  // the python cfg file
40  if (argc < 2) {
41  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
42  return 0;
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;
46  exit(0);
47  }
48  // get the python configuration
49  const edm::ParameterSet& process = edm::readPSetsFrom(argv[1])->getParameter<edm::ParameterSet>("process");
50  fwlite::InputSource inputHandler_(process);
51  fwlite::OutputFiles outputHandler_(process);
52 
53  // now get each parameter
54  const edm::ParameterSet& ana = process.getParameter<edm::ParameterSet>("muonAnalyzer");
55  edm::InputTag muons_(ana.getParameter<edm::InputTag>("muons"));
56 
57  // book a set of histograms
58  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file());
59  TFileDirectory dir = fs.mkdir("analyzeBasicPat");
60  TH1F* muonPt_ = dir.make<TH1F>("muonPt", "pt", 100, 0., 300.);
61  TH1F* muonEta_ = dir.make<TH1F>("muonEta", "eta", 100, -3., 3.);
62  TH1F* muonPhi_ = dir.make<TH1F>("muonPhi", "phi", 100, -5., 5.);
63 
64  // loop the events
65  int ievt = 0;
66  int maxEvents_(inputHandler_.maxEvents());
67  for (unsigned int iFile = 0; iFile < inputHandler_.files().size(); ++iFile) {
68  // open input file (can be located on castor)
69  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
70  if (inFile) {
71  // ----------------------------------------------------------------------
72  // Second Part:
73  //
74  // * loop the events in the input file
75  // * receive the collections of interest via fwlite::Handle
76  // * fill the histograms
77  // * after the loop close the input file
78  // ----------------------------------------------------------------------
79  fwlite::Event ev(inFile);
80  for (ev.toBegin(); !ev.atEnd(); ++ev, ++ievt) {
81  edm::EventBase const& event = ev;
82  // break loop if maximal number of events is reached
83  if (maxEvents_ > 0 ? ievt + 1 > maxEvents_ : false)
84  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)
106  break;
107  }
108  return 0;
109 }
TFileService.h
PDWG_BPHSkim_cff.muons
muons
Definition: PDWG_BPHSkim_cff.py:47
TFileDirectory::mkdir
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileDirectory.cc:112
Handle.h
cmsBatch.argv
argv
Definition: cmsBatch.py:279
mps_fire.i
i
Definition: mps_fire.py:355
dir2webdir.argc
argc
Definition: dir2webdir.py:39
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
edm::readPSetsFrom
std::unique_ptr< edm::ParameterSet > readPSetsFrom(std::string const &fileOrString)
Definition: ParameterSetReader.cc:23
Muon.h
gather_cfg.cout
cout
Definition: gather_cfg.py:144
TFileDirectory
Definition: TFileDirectory.h:24
fwlite::InputSource
Definition: InputSource.h:19
align_cfg.TFileService
TFileService
Definition: align_cfg.py:143
edm::Handle
Definition: AssociativeIterator.h:50
InputSource.h
PVValHelper::eta
Definition: PVValidationHelpers.h:69
ParameterSetReader.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:36
PVValHelper::phi
Definition: PVValidationHelpers.h:68
LaserDQM_cfg.process
process
Definition: LaserDQM_cfg.py:3
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
main
int main(int argc, char *argv[])
Definition: PatBasicFWLiteAnalyzer.cc:25
fwlite::Event
Definition: Event.h:87
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
edm::InputTag
Definition: InputTag.h:15
FWLiteEnabler.h
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23