Fork me on GitHub

Ticket #639: Example3.C

File Example3.C, 4.8 KB (added by Jordan Dull, 9 years ago)
Line 
1/*
2 This macro shows how to access the particle-level reference for reconstructed objects.
3 It is also shown how to loop over the jet constituents.
4
5 root -l examples/Example3.C'("delphes_output.root")'
6*/
7using namespace TMath;
8//------------------------------------------------------------------------------
9
10struct TestPlots
11{
12 TH1 *fJetPT;
13 TH1 *fdeltaR;
14
15 TH1 *fTowerPT;
16 TH1 *fTowerE;
17 TH1 *fdeltaE;
18
19};
20
21//------------------------------------------------------------------------------
22
23class ExRootResult;
24class ExRootTreeReader;
25
26//------------------------------------------------------------------------------
27
28void BookHistograms(ExRootResult *result, TestPlots *plots)
29{
30 TLegend *legend;
31 TPaveText *comment;
32
33 plots->fTowerPT = result->AddHist1D(
34 "TowerPt", "Pt",
35 "Pt", "number of towers",
36 100, 1, 4000);
37
38 plots->fTowerE = result->AddHist1D(
39 "TowerE", "Energy",
40 "Energy", "number of towers",
41 100, 1, 4000);
42
43 plots->fJetPT = result->AddHist1D(
44 "jet_pt", "Jet Pt",
45 "Jet Pt", "number of jets",
46 100, 2900, 5000);
47
48 plots->fdeltaR=result->AddHist1D(
49 "deltaR", "Tower Distance from Jet",
50 "Delta R", "number of towers",
51 100, .001, 5);
52
53 plots->fdeltaE=result->AddHist1D(
54 "deltaE", "Energy Difference Between Jet and Towers",
55 "Delta E", "Frequency",
56 100, -1000, 4000);
57
58}
59
60//------------------------------------------------------------------------------
61
62void AnalyseEvents(ExRootTreeReader *treeReader, TestPlots *plots)
63{
64 TClonesArray *branchParticle = treeReader->UseBranch("Particle");
65 TClonesArray *branchTower = treeReader->UseBranch("Tower");
66 TClonesArray *branchJet = treeReader->UseBranch("Jet");
67
68 Long64_t allEntries = treeReader->GetEntries();
69
70 cout << "** Chain contains " << allEntries << " events" << endl;
71
72 GenParticle *particle;
73 Tower *tower;
74 Jet *jet;
75 TObject *object;
76
77 TLorentzVector momentum;
78
79 Float_t Eem, Ehad;
80 Bool_t skip;
81
82 Long64_t entry;
83
84 Int_t i, j, pdgCode;
85
86 // Loop over all events
87 for(entry = 0; entry < allEntries; ++entry)
88 {
89 // Load selected branches with data from specified event
90 treeReader->ReadEntry(entry);
91
92 // cout << "-- New event -- " << endl;
93 // Loop over all jets in event
94 for(i = 0; i < branchJet->GetEntriesFast(); ++i)
95 {
96 jet = (Jet*) branchJet->At(i);
97
98 momentum.SetPxPyPzE(0.0, 0.0, 0.0, 0.0);
99
100 // Loop over all jet's constituents if jetPt>3000
101 if (jet->PT>3000) {
102
103 cout<<"Jet pt: "<<jet->PT<<", eta: "<<jet->Eta<<", phi: "<<jet->Phi<<", Energy:"<<jet->P4().Energy()<<endl;
104
105 double particleE=0;
106 double towerE=0;
107 for(j = 0; j < jet->Constituents.GetEntriesFast(); ++j)
108 {
109 object = jet->Constituents.At(j);
110
111 // Check if the constituent is accessible
112 if(object == 0) continue;
113
114 if(object->IsA() == GenParticle::Class())
115 {
116 particle = (GenParticle*) object;
117 // cout << " GenPart pt: " << particle->PT << ", eta: " << particle->Eta << ", phi: " << particle->Phi << endl;
118 momentum += particle->P4();
119 particleE +=particle->E;
120
121 }
122
123
124
125 else if(object->IsA() == Tower::Class())
126 {
127 tower = (Tower*) object;
128 //cout << " Tower pt: " << tower->ET << ", eta: " << tower->Eta << ", phi: " << tower->Phi << endl;
129 momentum += tower->P4();
130 plots->fTowerPT->Fill(tower->P4().Perp());
131 plots->fTowerE->Fill(tower->E);
132
133 double dEta=abs(jet->Eta-tower->Eta);
134 double dPhi=abs(jet->Phi-tower->Phi);
135 //if (dPhi>Pi()) { dPhi=Pi()-dPhi; }
136 double dR=Sqrt(Power(dEta,2)+Power(dPhi,2));
137 plots->fdeltaR->Fill(dR);
138 towerE +=tower->E;
139
140 }
141
142 }
143
144 cout<<"Particle Energy Sum: "<<particleE<<endl;
145 cout<<"Tower Energy Sum: "<<towerE<<endl;
146 plots->fdeltaE->Fill(jet->P4().Energy()-(towerE+particleE));
147 }
148 if (jet->PT>3000) {
149 plots->fJetPT->Fill(jet->PT);
150 }
151 }
152 }
153}
154
155//------------------------------------------------------------------------------
156
157void PrintHistograms(ExRootResult *result, TestPlots *plots)
158{
159 result->Print("png");
160}
161
162//------------------------------------------------------------------------------
163
164void Example3()
165{
166 gSystem->Load("libDelphes");
167
168 const char* inputFile="tev14_pythia8_qcd_pt2500_001.root";
169
170 TChain *chain = new TChain("Delphes");
171 chain->Add(inputFile);
172
173 ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
174 ExRootResult *result = new ExRootResult();
175
176 TestPlots *plots = new TestPlots;
177
178 BookHistograms(result, plots);
179
180
181 AnalyseEvents(treeReader, plots);
182
183 PrintHistograms(result, plots);
184
185 result->Write("results.root");
186
187 cout << "** Exiting..." << endl;
188
189 delete plots;
190 delete result;
191 delete treeReader;
192 delete chain;
193}
194
195//------------------------------------------------------------------------------