DESY Hbb Analysis Framework
ToolsForNtupleExample.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <vector>
4 
5 #include "TFile.h"
6 #include "TFileCollection.h"
7 #include "TChain.h"
8 #include "TH1.h"
9 
10 #include "Analysis/Tools/interface/JetCollection.h"
11 #include "Analysis/Tools/interface/Muons.h"
12 
13 using namespace std;
14 using namespace analysis;
15 using namespace analysis::tools;
16 
17 
18 void PrintCrossSections(TChain *);
19 void PrintGeneratorFilter(TChain *);
20 
21 // =============================================================================================
22 int main(int argc, char * argv[])
23 {
24  TH1::SetDefaultSumw2(); // proper treatment of errors when scaling histograms
25 
26  // Input files list
27  std::string inputList = "rootFileList.txt";
28  TFileCollection fc("dum","",inputList.c_str());
29 
30 // =============================================================================================
31  // Acces to Metadata (outside the event loop, no friendship with event-like trees)
32  TChain * t_xsection = new TChain("MonteCarloStudies/Metadata/CrossSections");
33  t_xsection -> AddFileInfoList((TCollection*) fc.GetList());
34  PrintCrossSections(t_xsection);
35 
36  TChain * t_genfilter = new TChain("MonteCarloStudies/Metadata/GeneratorFilter");
37  t_genfilter -> AddFileInfoList((TCollection*) fc.GetList());
38  PrintGeneratorFilter(t_genfilter);
39 // =============================================================================================
40 
41 // =============================================================================================
42  // EVENT stuff
43  // Main event tree
44  TChain * t_Event = new TChain("MonteCarloStudies/Events/EventInfo");
45  t_Event->AddFileInfoList((TCollection*) fc.GetList());
46 
47  // Other event trees
48  TChain * t_Jets = new TChain("MonteCarloStudies/Events/slimmedJetsPuppi");
49  TChain * t_Muons = new TChain("MonteCarloStudies/Events/slimmedMuons");
50  t_Jets -> AddFileInfoList((TCollection*) fc.GetList());
51  t_Muons -> AddFileInfoList((TCollection*) fc.GetList());
52 
53  // Friendship (DON'T FORGET!!!)
54  t_Event -> AddFriend(t_Jets);
55  t_Event -> AddFriend(t_Muons);
56 
57  // Create objects collections
58  JetCollection jets (t_Jets);
59  Muons muons(t_Muons);
60 
61  // HISTOGRAMS
62  std::map<std::string, TH1F*> h1;
63  // JETS
64  h1["h_jet_N"] = new TH1F("h_jet_N" , "", 20, 0., 20.);
65  h1["h_jet_Pt"] = new TH1F("h_jet_Pt" , "", 200, 0., 1000.);
66  h1["h_jet_Eta"] = new TH1F("h_jet_Eta", "", 100, -5, 5.);
67  h1["h_jet_Phi"] = new TH1F("h_jet_Phi", "", 100, 3.2, 3.2);
68  h1["h_jet_Btag"] = new TH1F("h_jet_Btag", "", 100, 0., 1.);
69  h1["h_jet_Flavour"] = new TH1F("h_jet_Flavour", "", 40, -10., 30.);
70  h1["h_jet_IdLoose"] = new TH1F("h_jet_IdLoose", "", 2, 0, 2);
71  h1["h_jet_IdTight"] = new TH1F("h_jet_IdTight", "", 2, 0, 2);
72  // MUONS
73  h1["h_muon_N"] = new TH1F("h_muon_N" , "", 20, 0., 20.);
74  h1["h_muon_Pt"] = new TH1F("h_muon_Pt" , "", 200, 0., 1000.);
75  h1["h_muon_Eta"] = new TH1F("h_muon_Eta", "", 100, -5, 5.);
76  h1["h_muon_Phi"] = new TH1F("h_muon_Phi", "", 100, 3.2, 3.2);
77 
78 
79  // Number of loop events
80  int nEvents = t_Event->GetEntries();
81 
82  int eventCounter = 0;
83 
84  for ( int i = 0 ; i < nEvents ; ++i )
85  {
86 // if ( i%10000 == 0 ) std::cout << "Processed " << i << " events" << "\xd";
87  ++eventCounter;
88 
89  t_Event->GetEntry(i);
90 
91 
92  h1["h_jet_N"] -> Fill(jets.size());
93  for ( int i = 0; i < jets.size(); ++i )
94  {
95  Jet jet = jets.at(i);
96  h1["h_jet_Pt"] -> Fill(jet.pt());
97  h1["h_jet_Eta"] -> Fill(jet.eta());
98  h1["h_jet_Phi"] -> Fill(jet.phi());
99  h1["h_jet_Btag"] -> Fill(jet.btag("btag_csvivf"));
100  h1["h_jet_Flavour"] -> Fill(jet.flavour());
101  h1["h_jet_IdLoose"] -> Fill(jet.idLoose());
102  h1["h_jet_IdTight"] -> Fill(jet.idTight());
103  }
104 
105  h1["h_muon_N"] -> Fill(muons.size());
106  for ( int i = 0; i < muons.size(); ++i )
107  {
108  Muon muon = muons.at(i);
109  h1["h_muon_Pt"] -> Fill(muon.pt());
110  h1["h_muon_Eta"] -> Fill(muon.eta());
111  h1["h_muon_Phi"] -> Fill(muon.phi());
112  }
113 
114  }
115 
116  TFile * outFile = new TFile("ExampleNtupleHistograms.root","RECREATE");
117  outFile -> mkdir("Jets","Jets");
118  outFile -> mkdir("Muons","Muons");
119 
120  for ( auto& ih1 : h1 )
121  {
122  if ( ih1.first.find("h_jet_") != std::string::npos )
123  outFile -> cd("Jets");
124  if ( ih1.first.find("h_muon_") != std::string::npos )
125  outFile -> cd("Muons");
126  ih1.second -> Write();
127  }
128  outFile -> Close();
129 
130 
131 //
132 }
133 
134 void PrintCrossSections(TChain * tree)
135 {
136  // Cross Sections - print on the screen
137  std::cout << "=======================================================" << std::endl;
138  std::cout << " CROSS SECTIONS" << std::endl;
139  std::cout << "=======================================================" << std::endl;
140 
141  TObjArray * xsecBranches = tree->GetListOfBranches();
142  std::map<std::string, double> xsections;
143  for ( int i = 0 ; i < xsecBranches->GetEntries() ; ++i )
144  {
145  std::string branch = xsecBranches->At(i)->GetName();
146  if ( branch == "run" ) continue;
147  xsections[branch] = 0;
148  tree -> SetBranchAddress(branch.c_str(), &xsections[branch]);
149  }
150  tree -> GetEntry(0);
151  for ( auto& xs : xsections )
152  {
153  std::cout << xs.first << " = " << xs.second << " pb " << std::endl;
154  }
155  std::cout << "=======================================================" << std::endl;
156  std::cout << std::endl;
157  std::cout << std::endl;
158 
159 }
160 
161 void PrintGeneratorFilter(TChain * tree)
162 {
163  std::cout << "=======================================================" << std::endl;
164  std::cout << " GENERATOR FILTER" << std::endl;
165  std::cout << "=======================================================" << std::endl;
166 
167  unsigned int nEvtTot;
168  unsigned int nEvtFlt;
169  unsigned int sEvtTot = 0;
170  unsigned int sEvtFlt = 0;
171 
172  tree -> SetBranchAddress("nEventsTotal", &nEvtTot);
173  tree -> SetBranchAddress("nEventsFiltered", &nEvtFlt);
174 
175  for ( int i = 0; i < tree->GetEntries(); ++i )
176  {
177  tree -> GetEntry(i);
178  sEvtTot += nEvtTot;
179  sEvtFlt += nEvtFlt;
180  }
181  float genFilterEff = float(sEvtFlt)/sEvtTot;
182 
183  std::cout << "Total generated events = " << sEvtTot << std::endl;
184  std::cout << "Filtered generated events = " << sEvtFlt << std::endl;
185  std::cout << "Generator Filter Efficiency = " << genFilterEff << std::endl;
186 
187  std::cout << "=======================================================" << std::endl;
188  std::cout << std::endl;
189  std::cout << std::endl;
190 }
float eta() const
returns the pseudorapidity
Definition: Candidate.cc:134
bool idTight() const
returns if jet has id tight working point
Definition: Jet.cc:61
bool idLoose() const
returns if jet has id loose working point
Definition: Jet.cc:60
float phi() const
returns the azimuthal angle
Definition: Candidate.cc:135
int flavour() const
returns the flavour with the Hadron definition (=0 for data)
Definition: Jet.cc:58
void PrintGeneratorFilter(TChain *)
float pt() const
returns the transverse momentum
Definition: Candidate.cc:133
float btag(const std::string &) const
returns the btag value of btag_csvivf
Definition: Jet.cc:57
void PrintCrossSections(TChain *)
int main(int argc, char *argv[])