CMS 3D CMS Logo

TopMuonFWLiteAnalyzer.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 
11 
16 
17 int main(int argc, char* argv[]) {
18  if (argc < 3) {
19  // -------------------------------------------------
20  std::cerr << "ERROR:: "
21  << "Wrong number of arguments! Please specify:" << std::endl
22  << " * filepath" << std::endl
23  << " * process name" << std::endl;
24  // -------------------------------------------------
25  return -1;
26  }
27 
28  // load framework libraries
29  gSystem->Load("libFWCoreFWLite");
31 
32  // set nice style for histograms
33  setNiceStyle();
34 
35  // define some histograms
36  TH1I* noMuons = new TH1I("noMuons", "N(Muon)", 10, 0, 10);
37  TH1I* noLepts = new TH1I("noLepts", "N(Lepton)", 10, 0, 10);
38  TH1F* ptMuons = new TH1F("ptMuons", "pt_{Muons}", 100, 0., 300.);
39  TH1F* enMuons = new TH1F("enMuons", "energy_{Muons}", 100, 0., 300.);
40  TH1F* etaMuons = new TH1F("etaMuons", "eta_{Muons}", 100, -3., 3.);
41  TH1F* phiMuons = new TH1F("phiMuons", "phi_{Muons}", 100, -5., 5.);
42 
43  // -------------------------------------------------
44  std::cout << "open file: " << argv[1] << std::endl;
45  // -------------------------------------------------
46  TFile* inFile = TFile::Open(argv[1]);
47  TTree* events_ = nullptr;
48  if (inFile)
49  inFile->GetObject("Events", events_);
50  if (events_ == nullptr) {
51  // -------------------------------------------------
52  std::cerr << "ERROR:: "
53  << "Unable to retrieve TTree Events!" << std::endl
54  << " Eighter wrong file name or the the tree doesn't exists" << std::endl;
55  // -------------------------------------------------
56  return -1;
57  }
58 
59  // acess branch of muons and elecs
60  char muonName[50];
61  sprintf(muonName, "patMuons_selectedPatMuons__%s.obj", argv[2]);
62  TBranch* muons_ = events_->GetBranch(muonName);
63  assert(muons_ != nullptr);
64  char elecName[50];
65  sprintf(elecName, "patElectrons_selectedPatElectrons__%s.obj", argv[2]);
66  TBranch* elecs_ = events_->GetBranch(elecName);
67  assert(elecs_ != nullptr);
68 
69  // loop over events and fill histograms
70  std::vector<pat::Muon> muons;
71  std::vector<pat::Electron> elecs;
72  int nevt = events_->GetEntries();
73 
74  // -------------------------------------------------
75  std::cout << "start looping " << nevt << " events..." << std::endl;
76  // -------------------------------------------------
77  for (int evt = 0; evt < nevt; ++evt) {
78  // set branch address
79  muons_->SetAddress(&muons);
80  elecs_->SetAddress(&elecs);
81  // get event
82  muons_->GetEntry(evt);
83  elecs_->GetEntry(evt);
84  events_->GetEntry(evt, 0);
85 
86  // -------------------------------------------------
87  if (evt > 0 && !(evt % 10))
88  std::cout << " processing event: " << evt << std::endl;
89  // -------------------------------------------------
90 
91  // fill histograms
92  noMuons->Fill(muons.size());
93  noLepts->Fill(muons.size() + elecs.size());
94  for (unsigned idx = 0; idx < muons.size(); ++idx) {
95  // fill histograms
96  ptMuons->Fill(muons[idx].pt());
97  enMuons->Fill(muons[idx].energy());
98  etaMuons->Fill(muons[idx].eta());
99  phiMuons->Fill(muons[idx].phi());
100  }
101  }
102  // -------------------------------------------------
103  std::cout << "close file" << std::endl;
104  // -------------------------------------------------
105  inFile->Close();
106 
107  // save histograms to file
108  TFile outFile("analyzeMuons.root", "recreate");
109  outFile.mkdir("analyzeMuon");
110  outFile.cd("analyzeMuon");
111  noMuons->Write();
112  noLepts->Write();
113  ptMuons->Write();
114  enMuons->Write();
115  etaMuons->Write();
116  phiMuons->Write();
117  outFile.Close();
118 
119  // free allocated space
120  delete noMuons;
121  delete noLepts;
122  delete ptMuons;
123  delete enMuons;
124  delete etaMuons;
125  delete phiMuons;
126 
127  return 0;
128 }
PDWG_BPHSkim_cff.muons
muons
Definition: PDWG_BPHSkim_cff.py:47
RootPostScript.h
RootHistograms.h
cmsBatch.argv
argv
Definition: cmsBatch.py:279
b2gDoubleLeptonHLTEventValidation_cfi.ptMuons
ptMuons
Definition: b2gDoubleLeptonHLTEventValidation_cfi.py:15
NiceStyle.cc
dir2webdir.argc
argc
Definition: dir2webdir.py:39
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
Muon.h
gather_cfg.cout
cout
Definition: gather_cfg.py:144
cms::cuda::assert
assert(be >=bs)
charmTagsComputerCvsB_cfi.idx
idx
Definition: charmTagsComputerCvsB_cfi.py:108
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
b2gDoubleLeptonHLTEventValidation_cfi.etaMuons
etaMuons
Definition: b2gDoubleLeptonHLTEventValidation_cfi.py:16
PVValHelper::phi
Definition: PVValidationHelpers.h:68
beam_dqm_sourceclient-live_cfg.cerr
cerr
Definition: beam_dqm_sourceclient-live_cfg.py:17
nano_cff.muonName
muonName
Definition: nano_cff.py:54
RootSystem.h
main
int main(int argc, char *argv[])
Definition: TopMuonFWLiteAnalyzer.cc:17
nevt
int nevt
Definition: ReggeGribovPartonMCHadronizer.h:66
Electron.h
FWLiteEnabler.h