CMS 3D CMS Logo

TopElecFWLiteAnalyzer.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <cstdlib>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 
10 
15 
16 int main(int argc, char* argv[]) {
17  if (argc < 3) {
18  // -------------------------------------------------
19  std::cerr << "ERROR:: "
20  << "Wrong number of arguments! Please specify:" << std::endl
21  << " * filepath" << std::endl
22  << " * process name" << std::endl;
23  // -------------------------------------------------
24  return -1;
25  }
26 
27  // load framework libraries
28  gSystem->Load("libFWCoreFWLite");
30 
31  // set nice style for histograms
32  setNiceStyle();
33 
34  // define some histograms
35  TH1I* noElecs = new TH1I("noElecs", "N_{Elecs}", 10, 0, 10);
36  TH1F* ptElecs = new TH1F("ptElecs", "pt_{Elecs}", 100, 0., 300.);
37  TH1F* enElecs = new TH1F("enElecs", "energy_{Elecs}", 100, 0., 300.);
38  TH1F* etaElecs = new TH1F("etaElecs", "eta_{Elecs}", 100, -3., 3.);
39  TH1F* phiElecs = new TH1F("phiElecs", "phi_{Elecs}", 100, -5., 5.);
40 
41  // -------------------------------------------------
42  std::cout << "open file: " << argv[1] << std::endl;
43  // -------------------------------------------------
44  TFile* inFile = TFile::Open(argv[1]);
45  TTree* events_ = nullptr;
46  if (inFile)
47  inFile->GetObject("Events", events_);
48  if (events_ == nullptr) {
49  // -------------------------------------------------
50  std::cerr << "ERROR:: "
51  << "Unable to retrieve TTree Events!" << std::endl
52  << " Eighter wrong file name or the the tree doesn't exists" << std::endl;
53  // -------------------------------------------------
54  return -1;
55  }
56 
57  // acess branch of elecs
58  char elecName[50];
59  sprintf(elecName, "patElectrons_selectedPatElectrons__%s.obj", argv[2]);
60  TBranch* elecs_ = events_->GetBranch(elecName);
61  assert(elecs_ != nullptr);
62 
63  // loop over events and fill histograms
64  std::vector<pat::Electron> elecs;
65  int nevt = events_->GetEntries();
66 
67  // -------------------------------------------------
68  std::cout << "start looping " << nevt << " events..." << std::endl;
69  // -------------------------------------------------
70  for (int evt = 0; evt < nevt; ++evt) {
71  // set branch address
72  elecs_->SetAddress(&elecs);
73  // get event
74  elecs_->GetEntry(evt);
75  events_->GetEntry(evt, 0);
76 
77  // -------------------------------------------------
78  if (evt > 0 && !(evt % 10))
79  std::cout << " processing event: " << evt << std::endl;
80  // -------------------------------------------------
81 
82  // fill histograms
83  noElecs->Fill(elecs.size());
84  for (unsigned idx = 0; idx < elecs.size(); ++idx) {
85  // fill histograms
86  ptElecs->Fill(elecs[idx].pt());
87  enElecs->Fill(elecs[idx].energy());
88  etaElecs->Fill(elecs[idx].eta());
89  phiElecs->Fill(elecs[idx].phi());
90  }
91  }
92  // -------------------------------------------------
93  std::cout << "close file" << std::endl;
94  // -------------------------------------------------
95  inFile->Close();
96 
97  // save histograms to file
98  TFile outFile("analyzeElecs.root", "recreate");
99  outFile.mkdir("analyzeElec");
100  outFile.cd("analyzeElec");
101  noElecs->Write();
102  ptElecs->Write();
103  enElecs->Write();
104  etaElecs->Write();
105  phiElecs->Write();
106  outFile.Close();
107 
108  // free allocated space
109  delete noElecs;
110  delete ptElecs;
111  delete enElecs;
112  delete etaElecs;
113  delete phiElecs;
114 
115  return 0;
116 }
RootPostScript.h
RootHistograms.h
cmsBatch.argv
argv
Definition: cmsBatch.py:279
NiceStyle.cc
dir2webdir.argc
argc
Definition: dir2webdir.py:39
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
gather_cfg.cout
cout
Definition: gather_cfg.py:144
cms::cuda::assert
assert(be >=bs)
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
setNiceStyle
void setNiceStyle()
Definition: NiceStyle.cc:3
PVValHelper::eta
Definition: PVValidationHelpers.h:69
singleTopDQM_cfi.elecs
elecs
Definition: singleTopDQM_cfi.py:41
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
L1TdeCSCTF_cfi.outFile
outFile
Definition: L1TdeCSCTF_cfi.py:5
FWLiteEnabler::enable
static void enable()
enable automatic library loading
Definition: FWLiteEnabler.cc:46
PVValHelper::phi
Definition: PVValidationHelpers.h:68
main
int main(int argc, char *argv[])
Definition: TopElecFWLiteAnalyzer.cc:16
RootSystem.h
nevt
int nevt
Definition: ReggeGribovPartonMCHadronizer.h:66
Electron.h
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
FWLiteEnabler.h