Fork me on GitHub

Ticket #1369: jet_constits.C

File jet_constits.C, 5.0 KB (added by flyingMoggy, 6 years ago)
Line 
1/*
2This macro shows how to access the particle-level reference for reconstructed objects.
3It is also shown how to loop over the jet constituents.
4
5root -l examples/jet_constits.C'("delphes_output.root")'
6*/
7
8#ifdef __CLING__
9R__LOAD_LIBRARY(libDelphes)
10#include "classes/DelphesClasses.h"
11#include "external/ExRootAnalysis/ExRootTreeReader.h"
12#include "external/ExRootAnalysis/ExRootResult.h"
13#else
14class ExRootTreeReader;
15class ExRootResult;
16#endif
17
18void AnalyseEvents(ExRootTreeReader *treeReader)
19{
20 {//output files have a scope?!?
21 ofstream jetOut("jetPts.csv");
22 ofstream trackOut("trackPts.csv");
23 ofstream towerOut("towerPts.csv");
24 ofstream genOut("genPts.csv");
25 TClonesArray *branchParticle = treeReader->UseBranch("Particle");
26 //TClonesArray *branchElectron = treeReader->UseBranch("Electron");
27 //TClonesArray *branchPhoton = treeReader->UseBranch("Photon");
28 //TClonesArray *branchMuon = treeReader->UseBranch("Muon");
29 //~~~~~ Uncommenting this give tracks
30 TClonesArray *branchEFlowTrack = treeReader->UseBranch("EFlowTrack");
31 //~~~~~ Uncommenting this give towers
32 TClonesArray *branchEFlowPhoton = treeReader->UseBranch("EFlowPhoton");
33 //~~~~~ Uncommenting this give towers
34 TClonesArray *branchEFlowNeutralHadron = treeReader->UseBranch("EFlowNeutralHadron");
35 //~~~~~~~ Why are the existance of unused pointers changing the output?
36 TClonesArray *branchJet = treeReader->UseBranch("Jet");
37
38 Long64_t allEntries = treeReader->GetEntries();
39
40 cout << "** Chain contains " << allEntries << " events" << endl;
41
42 GenParticle *particle;
43 //Electron *electron;
44 //Photon *photon;
45 //Muon *muon;
46
47 Track *track;
48 Tower *tower;
49
50 Jet *jet;
51 TObject *object;
52
53 TLorentzVector momentum;
54
55 //Float_t Eem, Ehad;
56 //Bool_t skip;
57
58 Long64_t entry;
59
60 Int_t i, j; //, pdgCode;
61
62 // Loop over all events
63 for(entry = 0; entry < allEntries; ++entry)
64 {
65 // Load selected branches with data from specified event
66 treeReader->ReadEntry(entry);
67
68 // // Loop over all electrons in event
69 // for(i = 0; i < branchElectron->GetEntriesFast(); ++i)
70 // {
71 // electron = (Electron*) branchElectron->At(i);
72 // particle = (GenParticle*) electron->Particle.GetObject();
73
74 // }
75
76 // // Loop over all photons in event
77 // for(i = 0; i < branchPhoton->GetEntriesFast(); ++i)
78 // {
79 // photon = (Photon*) branchPhoton->At(i);
80
81 // // skip photons with references to multiple particles
82 // if(photon->Particles.GetEntriesFast() != 1) continue;
83
84 // particle = (GenParticle*) photon->Particles.At(0);
85 //
86 // }
87
88 // // Loop over all muons in event
89 // for(i = 0; i < branchMuon->GetEntriesFast(); ++i)
90 // {
91 // muon = (Muon*) branchMuon->At(i);
92 // particle = (GenParticle*) muon->Particle.GetObject();
93
94 // }
95
96 cout << "-- New event -- " << endl;
97
98 // Loop over all jets in event
99 for(i = 0; i < branchJet->GetEntriesFast(); ++i)
100 {
101 jet = (Jet*) branchJet->At(i);
102
103 momentum.SetPxPyPzE(0.0, 0.0, 0.0, 0.0);
104
105 cout<<"Looping over jet constituents. Jet pt: "<<jet->PT<<", eta: "<<jet->Eta<<", phi: "<<jet->Phi<<endl;
106 jetOut << jet->PT;
107
108 // Loop over all jet's constituents
109 for(j = 0; j < jet->Constituents.GetEntriesFast(); ++j)
110 {
111 object = jet->Constituents.At(j);
112
113 // Check if the constituent is accessible
114 if(object == 0) continue;
115
116 if(object->IsA() == GenParticle::Class())
117 {
118 particle = (GenParticle*) object;
119 cout << " GenPart pt: " << particle->PT << ", eta: " << particle->Eta << ", phi: " << particle->Phi << endl;
120 momentum += particle->P4();
121 genOut << particle->PT << ",";
122 }
123 else if(object->IsA() == Track::Class())
124 {
125 track = (Track*) object;
126 cout << " Track pt: " << track->PT << ", eta: " << track->Eta << ", phi: " << track->Phi << endl;
127 momentum += track->P4();
128 trackOut << track->PT << ",";
129 }
130 else if(object->IsA() == Tower::Class())
131 {
132 tower = (Tower*) object;
133 cout << " Tower pt: " << tower->ET << ", eta: " << tower->Eta << ", phi: " << tower->Phi << endl;
134 momentum += tower->P4();
135 towerOut << tower->ET << ",";
136 }
137 }
138 jetOut << endl;
139 towerOut << endl;
140 genOut << endl;
141 trackOut << endl;
142 }
143 }
144 }
145}
146
147//------------------------------------------------------------------------------
148
149void jet_constits(const char *inputFile)
150{
151 gSystem->Load("libDelphes");
152
153 TChain *chain = new TChain("Delphes");
154 chain->Add(inputFile);
155
156 ExRootTreeReader *treeReader = new ExRootTreeReader(chain);
157
158 AnalyseEvents(treeReader);
159
160
161 cout << "** Exiting..." << endl;
162
163 delete treeReader;
164 delete chain;
165}
166
167//------------------------------------------------------------------------------