Fork me on GitHub

source: svn/trunk/modules/TreeWriter.cc@ 1343

Last change on this file since 1343 was 1325, checked in by Pavel Demin, 11 years ago

fix class map in TreeWriter

  • Property svn:keywords set to Id Revision Date
File size: 16.0 KB
RevLine 
[694]1
[815]2/** \class TreeWriter
3 *
4 * Fills ROOT tree branches.
5 *
6 * $Date: 2013-11-12 14:48:27 +0000 (Tue, 12 Nov 2013) $
7 * $Revision: 1325 $
8 *
9 *
10 * \author P. Demin - UCL, Louvain-la-Neuve
11 *
12 */
13
[694]14#include "modules/TreeWriter.h"
15
[703]16#include "classes/DelphesClasses.h"
17#include "classes/DelphesFactory.h"
[766]18#include "classes/DelphesFormula.h"
[694]19
20#include "ExRootAnalysis/ExRootResult.h"
[703]21#include "ExRootAnalysis/ExRootFilter.h"
22#include "ExRootAnalysis/ExRootClassifier.h"
[694]23#include "ExRootAnalysis/ExRootTreeBranch.h"
24
25#include "TROOT.h"
[703]26#include "TMath.h"
[694]27#include "TString.h"
[703]28#include "TFormula.h"
29#include "TRandom3.h"
30#include "TObjArray.h"
31#include "TDatabasePDG.h"
[694]32#include "TLorentzVector.h"
33
[988]34#include <algorithm>
[703]35#include <stdexcept>
[694]36#include <iostream>
[703]37#include <sstream>
[694]38
39using namespace std;
40
41//------------------------------------------------------------------------------
42
43TreeWriter::TreeWriter()
44{
45}
46
47//------------------------------------------------------------------------------
48
49TreeWriter::~TreeWriter()
50{
51}
52
53//------------------------------------------------------------------------------
54
55void TreeWriter::Init()
56{
57 fClassMap[GenParticle::Class()] = &TreeWriter::ProcessParticles;
[1325]58 fClassMap[Vertex::Class()] = &TreeWriter::ProcessVertices;
[694]59 fClassMap[Track::Class()] = &TreeWriter::ProcessTracks;
60 fClassMap[Tower::Class()] = &TreeWriter::ProcessTowers;
61 fClassMap[Photon::Class()] = &TreeWriter::ProcessPhotons;
62 fClassMap[Electron::Class()] = &TreeWriter::ProcessElectrons;
63 fClassMap[Muon::Class()] = &TreeWriter::ProcessMuons;
64 fClassMap[Jet::Class()] = &TreeWriter::ProcessJets;
65 fClassMap[MissingET::Class()] = &TreeWriter::ProcessMissingET;
[893]66 fClassMap[ScalarHT::Class()] = &TreeWriter::ProcessScalarHT;
[1114]67 fClassMap[Rho::Class()] = &TreeWriter::ProcessRho;
[1123]68 fClassMap[Weight::Class()] = &TreeWriter::ProcessWeight;
[694]69
70 TBranchMap::iterator itBranchMap;
71 map< TClass *, TProcessMethod >::iterator itClassMap;
72
73 // read branch configuration and
74 // import array with output from filter/classifier/jetfinder modules
75
76 ExRootConfParam param = GetParam("Branch");
77 Long_t i, size;
78 TString branchName, branchClassName, branchInputArray;
79 TClass *branchClass;
[895]80 TObjArray *array;
[694]81 ExRootTreeBranch *branch;
82
83 size = param.GetSize();
[703]84 for(i = 0; i < size/3; ++i)
[694]85 {
[703]86 branchInputArray = param[i*3].GetString();
87 branchName = param[i*3 + 1].GetString();
88 branchClassName = param[i*3 + 2].GetString();
[694]89
90 branchClass = gROOT->GetClass(branchClassName);
91
92 if(!branchClass)
93 {
94 cout << "** ERROR: cannot find class '" << branchClassName << "'" << endl;
95 continue;
96 }
97
98 itClassMap = fClassMap.find(branchClass);
99 if(itClassMap == fClassMap.end())
100 {
101 cout << "** ERROR: cannot create branch for class '" << branchClassName << "'" << endl;
102 continue;
103 }
104
105 array = ImportArray(branchInputArray);
106 branch = NewBranch(branchName, branchClass);
107
[895]108 fBranchMap.insert(make_pair(branch, make_pair(itClassMap->second, array)));
[694]109 }
110
111}
112
113//------------------------------------------------------------------------------
114
115void TreeWriter::Finish()
116{
117
118}
119
120//------------------------------------------------------------------------------
121
[930]122void TreeWriter::FillParticles(Candidate *candidate, TRefArray *array)
123{
124 TIter it1(candidate->GetCandidates());
125 it1.Reset();
126 array->Clear();
127 while((candidate = static_cast<Candidate*>(it1.Next())))
128 {
129 TIter it2(candidate->GetCandidates());
130
131 // particle
132 if(candidate->GetCandidates()->GetEntriesFast() == 0)
133 {
134 array->Add(candidate);
135 continue;
136 }
137
138 // track
139 candidate = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
140 if(candidate->GetCandidates()->GetEntriesFast() == 0)
141 {
142 array->Add(candidate);
143 continue;
144 }
145
146 // tower
147 it2.Reset();
148 while((candidate = static_cast<Candidate*>(it2.Next())))
149 {
150 array->Add(candidate->GetCandidates()->At(0));
151 }
152 }
153}
154
155//------------------------------------------------------------------------------
156
[895]157void TreeWriter::ProcessParticles(ExRootTreeBranch *branch, TObjArray *array)
[694]158{
[895]159 TIter iterator(array);
[694]160 Candidate *candidate = 0;
161 GenParticle *entry = 0;
[771]162 Double_t pt, signPz, cosTheta, eta, rapidity;
[694]163
164 // loop over all particles
[895]165 iterator.Reset();
166 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]167 {
168 const TLorentzVector &momentum = candidate->Momentum;
169 const TLorentzVector &position = candidate->Position;
170
171 entry = static_cast<GenParticle*>(branch->NewEntry());
172
[921]173 entry->SetBit(kIsReferenced);
174 entry->SetUniqueID(candidate->GetUniqueID());
[923]175
[694]176 pt = momentum.Pt();
[771]177 cosTheta = TMath::Abs(momentum.CosTheta());
[694]178 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]179 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
180 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]181
182 entry->PID = candidate->PID;
183
184 entry->Status = candidate->Status;
[1014]185 entry->IsPU = candidate->IsPU;
[694]186
187 entry->M1 = candidate->M1;
188 entry->M2 = candidate->M2;
189
[845]190 entry->D1 = candidate->D1;
191 entry->D2 = candidate->D2;
192
[694]193 entry->Charge = candidate->Charge;
194 entry->Mass = candidate->Mass;
195
196 entry->E = momentum.E();
197 entry->Px = momentum.Px();
198 entry->Py = momentum.Py();
199 entry->Pz = momentum.Pz();
200
201 entry->Eta = eta;
202 entry->Phi = momentum.Phi();
203 entry->PT = pt;
204
205 entry->Rapidity = rapidity;
206
207 entry->X = position.X();
208 entry->Y = position.Y();
209 entry->Z = position.Z();
210 entry->T = position.T();
211 }
212}
213
214//------------------------------------------------------------------------------
215
[1323]216void TreeWriter::ProcessVertices(ExRootTreeBranch *branch, TObjArray *array)
217{
218 TIter iterator(array);
219 Candidate *candidate = 0;
220 Vertex *entry = 0;
221
222 // loop over all vertices
223 iterator.Reset();
224 while((candidate = static_cast<Candidate*>(iterator.Next())))
225 {
226 const TLorentzVector &position = candidate->Position;
227
228 entry = static_cast<Vertex*>(branch->NewEntry());
229
230 entry->X = position.X();
231 entry->Y = position.Y();
232 entry->Z = position.Z();
233 entry->T = position.T();
234 }
235}
236
237//------------------------------------------------------------------------------
238
[895]239void TreeWriter::ProcessTracks(ExRootTreeBranch *branch, TObjArray *array)
[694]240{
[895]241 TIter iterator(array);
[694]242 Candidate *candidate = 0;
[920]243 Candidate *particle = 0;
[694]244 Track *entry = 0;
[771]245 Double_t pt, signz, cosTheta, eta, rapidity;
[694]246
[1323]247 // loop over all tracks
[895]248 iterator.Reset();
249 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]250 {
[920]251 const TLorentzVector &position = candidate->Position;
[694]252
[920]253 cosTheta = TMath::Abs(position.CosTheta());
254 signz = (position.Pz() >= 0.0) ? 1.0 : -1.0;
255 eta = (cosTheta == 1.0 ? signz*999.9 : position.Eta());
256 rapidity = (cosTheta == 1.0 ? signz*999.9 : position.Rapidity());
[694]257
258 entry = static_cast<Track*>(branch->NewEntry());
259
[936]260 entry->SetBit(kIsReferenced);
261 entry->SetUniqueID(candidate->GetUniqueID());
262
[703]263 entry->PID = candidate->PID;
264
265 entry->Charge = candidate->Charge;
266
[694]267 entry->EtaOuter = eta;
[920]268 entry->PhiOuter = position.Phi();
[696]269
[920]270 entry->XOuter = position.X();
271 entry->YOuter = position.Y();
272 entry->ZOuter = position.Z();
[988]273
[920]274 const TLorentzVector &momentum = candidate->Momentum;
[694]275
276 pt = momentum.Pt();
[771]277 cosTheta = TMath::Abs(momentum.CosTheta());
[694]278 signz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]279 eta = (cosTheta == 1.0 ? signz*999.9 : momentum.Eta());
280 rapidity = (cosTheta == 1.0 ? signz*999.9 : momentum.Rapidity());
[694]281
282 entry->Eta = eta;
283 entry->Phi = momentum.Phi();
284 entry->PT = pt;
[988]285
[920]286 particle = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
287 const TLorentzVector &initialPosition = particle->Position;
288
289 entry->X = initialPosition.X();
290 entry->Y = initialPosition.Y();
291 entry->Z = initialPosition.Z();
[988]292
[920]293 entry->Particle = particle;
[694]294 }
295}
296
297//------------------------------------------------------------------------------
298
[895]299void TreeWriter::ProcessTowers(ExRootTreeBranch *branch, TObjArray *array)
[694]300{
[895]301 TIter iterator(array);
[694]302 Candidate *candidate = 0;
303 Tower *entry = 0;
[771]304 Double_t pt, signPz, cosTheta, eta, rapidity;
[694]305
[1323]306 // loop over all towers
[895]307 iterator.Reset();
308 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]309 {
310 const TLorentzVector &momentum = candidate->Momentum;
311
312 pt = momentum.Pt();
[771]313 cosTheta = TMath::Abs(momentum.CosTheta());
[694]314 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]315 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
316 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]317
318 entry = static_cast<Tower*>(branch->NewEntry());
319
[936]320 entry->SetBit(kIsReferenced);
321 entry->SetUniqueID(candidate->GetUniqueID());
322
[694]323 entry->Eta = eta;
324 entry->Phi = momentum.Phi();
325 entry->ET = pt;
[696]326 entry->E = momentum.E();
327 entry->Eem = candidate->Eem;
328 entry->Ehad = candidate->Ehad;
[898]329 entry->Edges[0] = candidate->Edges[0];
330 entry->Edges[1] = candidate->Edges[1];
331 entry->Edges[2] = candidate->Edges[2];
332 entry->Edges[3] = candidate->Edges[3];
[988]333
[930]334 FillParticles(candidate, &entry->Particles);
[694]335 }
336}
337
338//------------------------------------------------------------------------------
339
[895]340void TreeWriter::ProcessPhotons(ExRootTreeBranch *branch, TObjArray *array)
[694]341{
[895]342 TIter iterator(array);
[694]343 Candidate *candidate = 0;
344 Photon *entry = 0;
[771]345 Double_t pt, signPz, cosTheta, eta, rapidity;
[694]346
[1066]347 array->Sort();
348
349 // loop over all photons
[895]350 iterator.Reset();
351 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]352 {
[930]353 TIter it1(candidate->GetCandidates());
[694]354 const TLorentzVector &momentum = candidate->Momentum;
355
356 pt = momentum.Pt();
[771]357 cosTheta = TMath::Abs(momentum.CosTheta());
[694]358 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]359 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
360 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]361
362 entry = static_cast<Photon*>(branch->NewEntry());
363
364 entry->Eta = eta;
365 entry->Phi = momentum.Phi();
366 entry->PT = pt;
[923]367 entry->E = momentum.E();
368
[988]369 entry->EhadOverEem = candidate->Eem > 0.0 ? candidate->Ehad/candidate->Eem : 999.9;
370
[930]371 FillParticles(candidate, &entry->Particles);
[694]372 }
373}
374
375//------------------------------------------------------------------------------
376
[895]377void TreeWriter::ProcessElectrons(ExRootTreeBranch *branch, TObjArray *array)
[694]378{
[895]379 TIter iterator(array);
[694]380 Candidate *candidate = 0;
381 Electron *entry = 0;
[771]382 Double_t pt, signPz, cosTheta, eta, rapidity;
[694]383
[1066]384 array->Sort();
385
386 // loop over all electrons
[895]387 iterator.Reset();
388 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]389 {
390 const TLorentzVector &momentum = candidate->Momentum;
391
392 pt = momentum.Pt();
[771]393 cosTheta = TMath::Abs(momentum.CosTheta());
[694]394 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]395 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
396 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]397
398 entry = static_cast<Electron*>(branch->NewEntry());
399
400 entry->Eta = eta;
401 entry->Phi = momentum.Phi();
402 entry->PT = pt;
403
404 entry->Charge = candidate->Charge;
[920]405
[988]406 entry->EhadOverEem = 0.0;
407
[920]408 entry->Particle = candidate->GetCandidates()->At(0);
[694]409 }
410}
411
412//------------------------------------------------------------------------------
413
[895]414void TreeWriter::ProcessMuons(ExRootTreeBranch *branch, TObjArray *array)
[694]415{
[895]416 TIter iterator(array);
[694]417 Candidate *candidate = 0;
418 Muon *entry = 0;
[771]419 Double_t pt, signPz, cosTheta, eta, rapidity;
[694]420
[1066]421 array->Sort();
422
423 // loop over all muons
[895]424 iterator.Reset();
425 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]426 {
427 const TLorentzVector &momentum = candidate->Momentum;
428
429 pt = momentum.Pt();
[771]430 cosTheta = TMath::Abs(momentum.CosTheta());
[694]431 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]432 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
433 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]434
435 entry = static_cast<Muon*>(branch->NewEntry());
436
[939]437 entry->SetBit(kIsReferenced);
438 entry->SetUniqueID(candidate->GetUniqueID());
439
[694]440 entry->Eta = eta;
441 entry->Phi = momentum.Phi();
442 entry->PT = pt;
443
444 entry->Charge = candidate->Charge;
[920]445
446 entry->Particle = candidate->GetCandidates()->At(0);
[694]447 }
448}
449
450//------------------------------------------------------------------------------
451
[895]452void TreeWriter::ProcessJets(ExRootTreeBranch *branch, TObjArray *array)
[694]453{
[895]454 TIter iterator(array);
[936]455 Candidate *candidate = 0, *constituent = 0;
[694]456 Jet *entry = 0;
[771]457 Double_t pt, signPz, cosTheta, eta, rapidity;
[988]458 Double_t ecalEnergy, hcalEnergy;
[694]459
[1066]460 array->Sort();
461
[694]462 // loop over all jets
[895]463 iterator.Reset();
464 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]465 {
[936]466 TIter itConstituents(candidate->GetCandidates());
[694]467 const TLorentzVector &momentum = candidate->Momentum;
468
469 pt = momentum.Pt();
[771]470 cosTheta = TMath::Abs(momentum.CosTheta());
[694]471 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]472 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
473 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]474
475 entry = static_cast<Jet*>(branch->NewEntry());
476
477 entry->Eta = eta;
478 entry->Phi = momentum.Phi();
479 entry->PT = pt;
480
481 entry->Mass = momentum.M();
[696]482
[900]483 entry->DeltaEta = candidate->DeltaEta;
484 entry->DeltaPhi = candidate->DeltaPhi;
[696]485
486 entry->BTag = candidate->BTag;
[926]487 entry->TauTag = candidate->TauTag;
[900]488
[926]489 entry->Charge = candidate->Charge;
490
[936]491 itConstituents.Reset();
492 entry->Constituents.Clear();
[988]493 ecalEnergy = 0.0;
494 hcalEnergy = 0.0;
[936]495 while((constituent = static_cast<Candidate*>(itConstituents.Next())))
496 {
497 entry->Constituents.Add(constituent);
[988]498 ecalEnergy += constituent->Eem;
499 hcalEnergy += constituent->Ehad;
[936]500 }
[988]501
502 entry->EhadOverEem = ecalEnergy > 0.0 ? hcalEnergy/ecalEnergy : 999.9;
503
[930]504 FillParticles(candidate, &entry->Particles);
[694]505 }
506}
507
508//------------------------------------------------------------------------------
509
[895]510void TreeWriter::ProcessMissingET(ExRootTreeBranch *branch, TObjArray *array)
[694]511{
512 Candidate *candidate = 0;
513 MissingET *entry = 0;
514
[893]515 // get the first entry
[895]516 if((candidate = static_cast<Candidate*>(array->At(0))))
[694]517 {
518 const TLorentzVector &momentum = candidate->Momentum;
519
520 entry = static_cast<MissingET*>(branch->NewEntry());
521
[1321]522 entry->Eta = (-momentum).Eta();
[1092]523 entry->Phi = (-momentum).Phi();
[893]524 entry->MET = momentum.Pt();
[694]525 }
526}
527
528//------------------------------------------------------------------------------
529
[895]530void TreeWriter::ProcessScalarHT(ExRootTreeBranch *branch, TObjArray *array)
[893]531{
532 Candidate *candidate = 0;
533 ScalarHT *entry = 0;
534
535 // get the first entry
[895]536 if((candidate = static_cast<Candidate*>(array->At(0))))
[893]537 {
538 const TLorentzVector &momentum = candidate->Momentum;
539
540 entry = static_cast<ScalarHT*>(branch->NewEntry());
541
542 entry->HT = momentum.Pt();
543 }
544}
545
546//------------------------------------------------------------------------------
547
[1114]548void TreeWriter::ProcessRho(ExRootTreeBranch *branch, TObjArray *array)
549{
[1321]550 TIter iterator(array);
[1114]551 Candidate *candidate = 0;
552 Rho *entry = 0;
553
[1321]554 // loop over all rho
555 iterator.Reset();
556 while((candidate = static_cast<Candidate*>(iterator.Next())))
[1114]557 {
558 const TLorentzVector &momentum = candidate->Momentum;
559
560 entry = static_cast<Rho*>(branch->NewEntry());
561
[1115]562 entry->Rho = momentum.E();
[1321]563 entry->Edges[0] = candidate->Edges[0];
564 entry->Edges[1] = candidate->Edges[1];
[1114]565 }
566}
567
568//------------------------------------------------------------------------------
569
[1123]570void TreeWriter::ProcessWeight(ExRootTreeBranch *branch, TObjArray *array)
571{
572 Candidate *candidate = 0;
573 Weight *entry = 0;
574
575 // get the first entry
576 if((candidate = static_cast<Candidate*>(array->At(0))))
577 {
578 const TLorentzVector &momentum = candidate->Momentum;
579
580 entry = static_cast<Weight*>(branch->NewEntry());
581
582 entry->Weight = momentum.E();
583 }
584}
585
586//------------------------------------------------------------------------------
587
[694]588void TreeWriter::Process()
589{
590 TBranchMap::iterator itBranchMap;
591 ExRootTreeBranch *branch;
592 TProcessMethod method;
[895]593 TObjArray *array;
[694]594
595 for(itBranchMap = fBranchMap.begin(); itBranchMap != fBranchMap.end(); ++itBranchMap)
596 {
597 branch = itBranchMap->first;
598 method = itBranchMap->second.first;
[895]599 array = itBranchMap->second.second;
[694]600
[895]601 (this->*method)(branch, array);
[694]602 }
603}
604
605//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.