Fork me on GitHub

Ticket #1173: Example2.C

File Example2.C, 2.8 KB (added by Goko, 7 years ago)
Line 
1/*
2Write this to the Terminal
3root -l examples/Example2.C'("delphes_output.root")'
4*/
5
6#include "TH1.h"
7#include "TSystem.h"
8
9#ifdef __CLING__
10R__LOAD_LIBRARY(libDelphes)
11#include "classes/DelphesClasses.h"
12#include "external/ExRootAnalysis/ExRootTreeReader.h"
13#include "external/ExRootAnalysis/ExRootResult.h"
14#endif
15
16//------------------------------------------------------------------------------
17
18struct MyPlots
19{
20 TH1 *ParticlePT;
21};
22
23//------------------------------------------------------------------------------
24
25class ExRootResult;
26class ExRootTreeReader;
27
28//------------------------------------------------------------------------------
29
30void BookHistograms(ExRootResult *result, MyPlots *plots)
31{
32 THStack *stack;
33 TLegend *legend;
34 TPaveText *comment;
35
36
37 // book more histograms
38
39 plots->fParticlePT = result->AddHist1D(
40 "electron_pt", "electron P_{T}",
41 "electron P_{T}, GeV/c", "number of electrons",
42 50, 0.0, 100.0);
43
44 // book general comment
45
46 comment = result->AddComment(0.64, 0.86, 0.98, 0.98);
47 comment->AddText("demonstration plot");
48 comment->AddText("produced by Example2.C");
49
50 result->Attach(plots->fParticlePT, comment);
51
52}
53
54//------------------------------------------------------------------------------
55
56void AnalyseEvents(ExRootTreeReader *treeReader, MyPlots *plots)
57{
58 TClonesArray *branchParticle = treeReader->UseBranch("Particle");
59
60 Long64_t allEntries = treeReader->GetEntries();
61
62 cout << "** Chain contains " << allEntries << " events" << endl;
63
64 Particle *particle;
65
66 Long64_t entry;
67
68 Int_t i;
69
70 // Loop over all events
71 for(entry = 0; entry < allEntries; ++entry)
72 {
73 // Load selected branches with data from specified event
74 treeReader->ReadEntry(entry);
75
76 // Loop over all electrons in event
77 for(i = 0; i < branchParticle->GetEntriesFast(); ++i)
78 {
79 particle = (Particle*) branchParticle->At(i);
80 plots->fParticlePT->Fill(particle->PT);
81 }
82 }
83}
84
85//------------------------------------------------------------------------------
86
87void PrintHistograms(ExRootResult *result, MyPlots *plots)
88{
89 result->Print("png");
90}
91
92//------------------------------------------------------------------------------
93
94void Example2(const char *inputFile)
95{
96 gSystem->Load("libDelphes");
97
98 TChain *chain = new TChain("Delphes");
99 chain->Add(inputFile);
100
101 ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
102 ExRootResult *result = new ExRootResult();
103
104 MyPlots *plots = new MyPlots;
105
106 BookHistograms(result, plots);
107
108 AnalyseEvents(treeReader, plots);
109
110 PrintHistograms(result, plots);
111
112 // write into a root file.
113
114 result->Write("result.root");
115
116 cout << "** Exiting..." << endl;
117
118 delete plots;
119 delete result;
120 delete treeReader;
121 delete chain;
122}
123
124//------------------------------------------------------------------------------