Fork me on GitHub

Ticket #725: test.C

File test.C, 5.4 KB (added by Michele Selvaggi, 9 years ago)
Line 
1/*
2Simple macro showing how to access branches from the delphes output root file,
3loop over events, store histograms in a root file and print them as image files.
4
5root -l examples/Example2.C'("delphes_output.root")'
6*/
7
8#include "TH1.h"
9#include "TSystem.h"
10
11#ifdef __CLING__
12//R__LOAD_LIBRARY(libDelphes)
13#include "classes/DelphesClasses.h"
14#include "external/ExRootAnalysis/ExRootTreeReader.h"
15#include "external/ExRootAnalysis/ExRootResult.h"
16#endif
17
18//------------------------------------------------------------------------------
19
20struct MyPlots
21{
22 TH1 *fJetPT[2];
23 TH1 *fJetM;
24 TH1 *fMissingET;
25 TH1 *fElectronPT;
26};
27
28//------------------------------------------------------------------------------
29
30class ExRootResult;
31class ExRootTreeReader;
32
33//------------------------------------------------------------------------------
34
35void BookHistograms(ExRootResult *result, MyPlots *plots)
36{
37 THStack *stack;
38 TLegend *legend;
39 TPaveText *comment;
40
41 // book 2 histograms for PT of 1st and 2nd leading jets
42
43 plots->fJetPT[0] = result->AddHist1D(
44 "jet_pt_0", "leading jet P_{T}",
45 "jet P_{T}, GeV/c", "number of jets",
46 50, 0.0, 100.0);
47
48 plots->fJetPT[1] = result->AddHist1D(
49 "jet_pt_1", "2nd leading jet P_{T}",
50 "jet P_{T}, GeV/c", "number of jets",
51 50, 0.0, 100.0);
52
53 plots->fJetPT[0]->SetLineColor(kRed);
54 plots->fJetPT[1]->SetLineColor(kBlue);
55
56 // book 1 stack of 2 histograms
57
58 stack = result->AddHistStack("jet_pt_all", "1st and 2nd jets P_{T}");
59 stack->Add(plots->fJetPT[0]);
60 stack->Add(plots->fJetPT[1]);
61
62 // book legend for stack of 2 histograms
63
64 legend = result->AddLegend(0.72, 0.86, 0.98, 0.98);
65 legend->AddEntry(plots->fJetPT[0], "leading jet", "l");
66 legend->AddEntry(plots->fJetPT[1], "second jet", "l");
67
68 // attach legend to stack (legend will be printed over stack in .eps file)
69
70 result->Attach(stack, legend);
71
72 // book more histograms
73
74 plots->fElectronPT = result->AddHist1D(
75 "electron_pt", "electron P_{T}",
76 "electron P_{T}, GeV/c", "number of electrons",
77 50, 0.0, 100.0);
78
79 plots->fMissingET = result->AddHist1D(
80 "missing_et", "Missing E_{T}",
81 "Missing E_{T}, GeV", "number of events",
82 60, 0.0, 30.0);
83
84 plots->fJetM = result->AddHist1D(
85 "m", "m",
86 "mass, GeV", "number of events",
87 40, 0.0, 200.0);
88
89
90 // book general comment
91
92 comment = result->AddComment(0.64, 0.86, 0.98, 0.98);
93 comment->AddText("demonstration plot");
94 comment->AddText("produced by Example2.C");
95
96 // attach comment to single histograms
97
98 result->Attach(plots->fJetPT[0], comment);
99 result->Attach(plots->fJetPT[1], comment);
100 result->Attach(plots->fElectronPT, comment);
101 result->Attach(plots->fJetM, comment);
102
103 // show histogram statisics for MissingET
104 plots->fMissingET->SetStats();
105}
106
107//------------------------------------------------------------------------------
108
109void AnalyseEvents(ExRootTreeReader *treeReader, MyPlots *plots)
110{
111 TClonesArray *branchJet = treeReader->UseBranch("Jet");
112 TClonesArray *branchElectron = treeReader->UseBranch("Electron");
113 TClonesArray *branchMissingET = treeReader->UseBranch("MissingET");
114
115 Long64_t allEntries = treeReader->GetEntries();
116
117 cout << "** Chain contains " << allEntries << " events" << endl;
118
119 //Jet *jet[2];
120 Jet *jet;
121 MissingET *met;
122 Electron *electron;
123
124 Long64_t entry;
125
126 Int_t i, np, nm;
127
128
129 np = 0;
130 nm = 0;
131 // Loop over all events
132 for(entry = 0; entry < allEntries; ++entry)
133 {
134 // Load selected branches with data from specified event
135 treeReader->ReadEntry(entry);
136
137 // Analyse two leading jets
138 /* if(branchJet->GetEntriesFast() >= 2)
139 {
140 jet[0] = (Jet*) branchJet->At(0);
141 jet[1] = (Jet*) branchJet->At(1);
142
143 plots->fJetPT[0]->Fill(jet[0]->PT);
144 plots->fJetPT[1]->Fill(jet[1]->PT);
145
146 if(TMath::Abs(jet[0]->Eta) > 2.5) continue;
147 if(TMath::Abs(jet[1]->Eta) > 2.5) continue;
148
149 plots->fJetM->Fill(((jet[0]->P4()) + (jet[1]->P4())).M());
150 }
151*/
152
153 for(i=0; i<branchJet->GetEntriesFast(); i++)
154 {
155 jet = (Jet*) branchJet->At(i);
156
157 if(!jet->TauTag) continue;
158
159 if(jet->Charge>0) np++;
160 if(jet->Charge<0) nm++;
161
162 }
163
164 // Analyse missing ET
165 if(branchMissingET->GetEntriesFast() > 0)
166 {
167 met = (MissingET*) branchMissingET->At(0);
168 plots->fMissingET->Fill(met->MET);
169 }
170
171 // Loop over all electrons in event
172 for(i = 0; i < branchElectron->GetEntriesFast(); ++i)
173 {
174 electron = (Electron*) branchElectron->At(i);
175 plots->fElectronPT->Fill(electron->PT);
176 }
177 }
178
179 cout<<"tau + : "<<np<<endl;
180 cout<<"tau - : "<<nm<<endl;
181
182}
183
184//------------------------------------------------------------------------------
185
186void PrintHistograms(ExRootResult *result, MyPlots *plots)
187{
188 result->Print("png");
189}
190
191//------------------------------------------------------------------------------
192
193void test(const char *inputFile)
194{
195 gSystem->Load("libDelphes");
196
197 TChain *chain = new TChain("Delphes");
198 chain->Add(inputFile);
199
200 ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
201 ExRootResult *result = new ExRootResult();
202
203 MyPlots *plots = new MyPlots;
204
205 BookHistograms(result, plots);
206
207 AnalyseEvents(treeReader, plots);
208
209 PrintHistograms(result, plots);
210
211 result->Write("results.root");
212
213 cout << "** Exiting..." << endl;
214
215 delete plots;
216 delete result;
217 delete treeReader;
218 delete chain;
219}
220
221//------------------------------------------------------------------------------