Fork me on GitHub

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

Last change on this file since 1350 was 1348, checked in by Michele Selvaggi, 11 years ago

added PileUpJetID module from Seth Senz

  • Property svn:keywords set to Id Revision Date
File size: 17.6 KB
RevLine 
[694]1
[815]2/** \class TreeWriter
3 *
4 * Fills ROOT tree branches.
5 *
6 * $Date: 2014-01-03 15:41:28 +0000 (Fri, 03 Jan 2014) $
7 * $Revision: 1348 $
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 }
[1345]97
[694]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
[930]121void TreeWriter::FillParticles(Candidate *candidate, TRefArray *array)
122{
123 TIter it1(candidate->GetCandidates());
124 it1.Reset();
125 array->Clear();
126 while((candidate = static_cast<Candidate*>(it1.Next())))
127 {
128 TIter it2(candidate->GetCandidates());
129
130 // particle
131 if(candidate->GetCandidates()->GetEntriesFast() == 0)
132 {
133 array->Add(candidate);
134 continue;
135 }
136
137 // track
138 candidate = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
139 if(candidate->GetCandidates()->GetEntriesFast() == 0)
140 {
141 array->Add(candidate);
142 continue;
143 }
144
145 // tower
146 it2.Reset();
147 while((candidate = static_cast<Candidate*>(it2.Next())))
148 {
149 array->Add(candidate->GetCandidates()->At(0));
150 }
151 }
152}
153
154//------------------------------------------------------------------------------
155
[895]156void TreeWriter::ProcessParticles(ExRootTreeBranch *branch, TObjArray *array)
[694]157{
[895]158 TIter iterator(array);
[694]159 Candidate *candidate = 0;
160 GenParticle *entry = 0;
[771]161 Double_t pt, signPz, cosTheta, eta, rapidity;
[1345]162
163 const Double_t c_light = 2.99792458E8;
164
[694]165 // loop over all particles
[895]166 iterator.Reset();
167 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]168 {
169 const TLorentzVector &momentum = candidate->Momentum;
170 const TLorentzVector &position = candidate->Position;
171
172 entry = static_cast<GenParticle*>(branch->NewEntry());
173
[921]174 entry->SetBit(kIsReferenced);
175 entry->SetUniqueID(candidate->GetUniqueID());
[923]176
[694]177 pt = momentum.Pt();
[771]178 cosTheta = TMath::Abs(momentum.CosTheta());
[694]179 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]180 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
181 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]182
183 entry->PID = candidate->PID;
184
185 entry->Status = candidate->Status;
[1014]186 entry->IsPU = candidate->IsPU;
[694]187
188 entry->M1 = candidate->M1;
189 entry->M2 = candidate->M2;
190
[845]191 entry->D1 = candidate->D1;
192 entry->D2 = candidate->D2;
193
[694]194 entry->Charge = candidate->Charge;
195 entry->Mass = candidate->Mass;
196
197 entry->E = momentum.E();
198 entry->Px = momentum.Px();
199 entry->Py = momentum.Py();
200 entry->Pz = momentum.Pz();
201
202 entry->Eta = eta;
203 entry->Phi = momentum.Phi();
204 entry->PT = pt;
205
206 entry->Rapidity = rapidity;
207
208 entry->X = position.X();
209 entry->Y = position.Y();
210 entry->Z = position.Z();
[1345]211 entry->T = position.T()*1.0E-3/c_light;
[694]212 }
213}
214
215//------------------------------------------------------------------------------
216
[1323]217void TreeWriter::ProcessVertices(ExRootTreeBranch *branch, TObjArray *array)
218{
219 TIter iterator(array);
220 Candidate *candidate = 0;
221 Vertex *entry = 0;
[1345]222
223 const Double_t c_light = 2.99792458E8;
[1323]224
225 // loop over all vertices
226 iterator.Reset();
227 while((candidate = static_cast<Candidate*>(iterator.Next())))
228 {
229 const TLorentzVector &position = candidate->Position;
230
231 entry = static_cast<Vertex*>(branch->NewEntry());
232
233 entry->X = position.X();
234 entry->Y = position.Y();
235 entry->Z = position.Z();
[1345]236 entry->T = position.T()*1.0E-3/c_light;
[1323]237 }
238}
239
240//------------------------------------------------------------------------------
241
[895]242void TreeWriter::ProcessTracks(ExRootTreeBranch *branch, TObjArray *array)
[694]243{
[895]244 TIter iterator(array);
[694]245 Candidate *candidate = 0;
[920]246 Candidate *particle = 0;
[694]247 Track *entry = 0;
[771]248 Double_t pt, signz, cosTheta, eta, rapidity;
[1345]249 const Double_t c_light = 2.99792458E8;
250
[1323]251 // loop over all tracks
[895]252 iterator.Reset();
253 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]254 {
[920]255 const TLorentzVector &position = candidate->Position;
[694]256
[920]257 cosTheta = TMath::Abs(position.CosTheta());
258 signz = (position.Pz() >= 0.0) ? 1.0 : -1.0;
259 eta = (cosTheta == 1.0 ? signz*999.9 : position.Eta());
260 rapidity = (cosTheta == 1.0 ? signz*999.9 : position.Rapidity());
[694]261
262 entry = static_cast<Track*>(branch->NewEntry());
263
[936]264 entry->SetBit(kIsReferenced);
265 entry->SetUniqueID(candidate->GetUniqueID());
266
[703]267 entry->PID = candidate->PID;
268
269 entry->Charge = candidate->Charge;
270
[694]271 entry->EtaOuter = eta;
[920]272 entry->PhiOuter = position.Phi();
[696]273
[920]274 entry->XOuter = position.X();
275 entry->YOuter = position.Y();
276 entry->ZOuter = position.Z();
[1345]277 entry->TOuter = position.T()*1.0E-3/c_light;
[988]278
[920]279 const TLorentzVector &momentum = candidate->Momentum;
[694]280
281 pt = momentum.Pt();
[771]282 cosTheta = TMath::Abs(momentum.CosTheta());
[694]283 signz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]284 eta = (cosTheta == 1.0 ? signz*999.9 : momentum.Eta());
285 rapidity = (cosTheta == 1.0 ? signz*999.9 : momentum.Rapidity());
[694]286
287 entry->Eta = eta;
288 entry->Phi = momentum.Phi();
289 entry->PT = pt;
[988]290
[920]291 particle = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
292 const TLorentzVector &initialPosition = particle->Position;
293
294 entry->X = initialPosition.X();
295 entry->Y = initialPosition.Y();
296 entry->Z = initialPosition.Z();
[1345]297 entry->T = initialPosition.T()*1.0E-3/c_light;
[988]298
[920]299 entry->Particle = particle;
[694]300 }
301}
302
303//------------------------------------------------------------------------------
304
[895]305void TreeWriter::ProcessTowers(ExRootTreeBranch *branch, TObjArray *array)
[694]306{
[895]307 TIter iterator(array);
[694]308 Candidate *candidate = 0;
309 Tower *entry = 0;
[771]310 Double_t pt, signPz, cosTheta, eta, rapidity;
[1345]311 const Double_t c_light = 2.99792458E8;
312
[1323]313 // loop over all towers
[895]314 iterator.Reset();
315 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]316 {
317 const TLorentzVector &momentum = candidate->Momentum;
[1345]318 const TLorentzVector &position = candidate->Position;
319
[694]320 pt = momentum.Pt();
[771]321 cosTheta = TMath::Abs(momentum.CosTheta());
[694]322 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]323 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
324 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]325
326 entry = static_cast<Tower*>(branch->NewEntry());
327
[936]328 entry->SetBit(kIsReferenced);
329 entry->SetUniqueID(candidate->GetUniqueID());
330
[694]331 entry->Eta = eta;
332 entry->Phi = momentum.Phi();
333 entry->ET = pt;
[696]334 entry->E = momentum.E();
335 entry->Eem = candidate->Eem;
336 entry->Ehad = candidate->Ehad;
[898]337 entry->Edges[0] = candidate->Edges[0];
338 entry->Edges[1] = candidate->Edges[1];
339 entry->Edges[2] = candidate->Edges[2];
340 entry->Edges[3] = candidate->Edges[3];
[1345]341
342 entry->T = position.T()*1.0E-3/c_light;
343
[930]344 FillParticles(candidate, &entry->Particles);
[694]345 }
346}
347
348//------------------------------------------------------------------------------
349
[895]350void TreeWriter::ProcessPhotons(ExRootTreeBranch *branch, TObjArray *array)
[694]351{
[895]352 TIter iterator(array);
[694]353 Candidate *candidate = 0;
354 Photon *entry = 0;
[771]355 Double_t pt, signPz, cosTheta, eta, rapidity;
[1345]356 const Double_t c_light = 2.99792458E8;
357
[1066]358 array->Sort();
359
360 // loop over all photons
[895]361 iterator.Reset();
362 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]363 {
[930]364 TIter it1(candidate->GetCandidates());
[694]365 const TLorentzVector &momentum = candidate->Momentum;
[1345]366 const TLorentzVector &position = candidate->Position;
367
[694]368
369 pt = momentum.Pt();
[771]370 cosTheta = TMath::Abs(momentum.CosTheta());
[694]371 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]372 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
373 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]374
375 entry = static_cast<Photon*>(branch->NewEntry());
376
377 entry->Eta = eta;
378 entry->Phi = momentum.Phi();
379 entry->PT = pt;
[923]380 entry->E = momentum.E();
[1345]381
382 entry->T = position.T()*1.0E-3/c_light;
383
[988]384 entry->EhadOverEem = candidate->Eem > 0.0 ? candidate->Ehad/candidate->Eem : 999.9;
385
[930]386 FillParticles(candidate, &entry->Particles);
[694]387 }
388}
389
390//------------------------------------------------------------------------------
391
[895]392void TreeWriter::ProcessElectrons(ExRootTreeBranch *branch, TObjArray *array)
[694]393{
[895]394 TIter iterator(array);
[694]395 Candidate *candidate = 0;
396 Electron *entry = 0;
[771]397 Double_t pt, signPz, cosTheta, eta, rapidity;
[1345]398 const Double_t c_light = 2.99792458E8;
399
[1066]400 array->Sort();
401
402 // loop over all electrons
[895]403 iterator.Reset();
404 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]405 {
406 const TLorentzVector &momentum = candidate->Momentum;
[1345]407 const TLorentzVector &position = candidate->Position;
408
[694]409 pt = momentum.Pt();
[771]410 cosTheta = TMath::Abs(momentum.CosTheta());
[694]411 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]412 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
413 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]414
415 entry = static_cast<Electron*>(branch->NewEntry());
416
417 entry->Eta = eta;
418 entry->Phi = momentum.Phi();
419 entry->PT = pt;
[1345]420
421 entry->T = position.T()*1.0E-3/c_light;
422
[694]423 entry->Charge = candidate->Charge;
[920]424
[988]425 entry->EhadOverEem = 0.0;
426
[920]427 entry->Particle = candidate->GetCandidates()->At(0);
[694]428 }
429}
430
431//------------------------------------------------------------------------------
432
[895]433void TreeWriter::ProcessMuons(ExRootTreeBranch *branch, TObjArray *array)
[694]434{
[895]435 TIter iterator(array);
[694]436 Candidate *candidate = 0;
437 Muon *entry = 0;
[771]438 Double_t pt, signPz, cosTheta, eta, rapidity;
[1345]439
440 const Double_t c_light = 2.99792458E8;
441
[1066]442 array->Sort();
443
444 // loop over all muons
[895]445 iterator.Reset();
446 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]447 {
448 const TLorentzVector &momentum = candidate->Momentum;
[1345]449 const TLorentzVector &position = candidate->Position;
450
[694]451
452 pt = momentum.Pt();
[771]453 cosTheta = TMath::Abs(momentum.CosTheta());
[694]454 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]455 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
456 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]457
458 entry = static_cast<Muon*>(branch->NewEntry());
459
[939]460 entry->SetBit(kIsReferenced);
461 entry->SetUniqueID(candidate->GetUniqueID());
462
[694]463 entry->Eta = eta;
464 entry->Phi = momentum.Phi();
465 entry->PT = pt;
466
[1345]467 entry->T = position.T()*1.0E-3/c_light;
468
469 // cout<<entry->PT<<","<<entry->T<<endl;
470
[694]471 entry->Charge = candidate->Charge;
[920]472
473 entry->Particle = candidate->GetCandidates()->At(0);
[694]474 }
475}
476
477//------------------------------------------------------------------------------
478
[895]479void TreeWriter::ProcessJets(ExRootTreeBranch *branch, TObjArray *array)
[694]480{
[895]481 TIter iterator(array);
[936]482 Candidate *candidate = 0, *constituent = 0;
[694]483 Jet *entry = 0;
[771]484 Double_t pt, signPz, cosTheta, eta, rapidity;
[988]485 Double_t ecalEnergy, hcalEnergy;
[1345]486 const Double_t c_light = 2.99792458E8;
487
[1066]488 array->Sort();
489
[694]490 // loop over all jets
[895]491 iterator.Reset();
492 while((candidate = static_cast<Candidate*>(iterator.Next())))
[694]493 {
[936]494 TIter itConstituents(candidate->GetCandidates());
[1345]495
[694]496 const TLorentzVector &momentum = candidate->Momentum;
[1345]497 const TLorentzVector &position = candidate->Position;
498
[694]499 pt = momentum.Pt();
[771]500 cosTheta = TMath::Abs(momentum.CosTheta());
[694]501 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
[771]502 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
503 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
[694]504
505 entry = static_cast<Jet*>(branch->NewEntry());
506
507 entry->Eta = eta;
508 entry->Phi = momentum.Phi();
509 entry->PT = pt;
510
[1345]511 entry->T = position.T()*1.0E-3/c_light;
512
[694]513 entry->Mass = momentum.M();
[696]514
[900]515 entry->DeltaEta = candidate->DeltaEta;
516 entry->DeltaPhi = candidate->DeltaPhi;
[696]517
518 entry->BTag = candidate->BTag;
[926]519 entry->TauTag = candidate->TauTag;
[900]520
[926]521 entry->Charge = candidate->Charge;
522
[936]523 itConstituents.Reset();
524 entry->Constituents.Clear();
[988]525 ecalEnergy = 0.0;
526 hcalEnergy = 0.0;
[936]527 while((constituent = static_cast<Candidate*>(itConstituents.Next())))
528 {
529 entry->Constituents.Add(constituent);
[988]530 ecalEnergy += constituent->Eem;
531 hcalEnergy += constituent->Ehad;
[936]532 }
[988]533
534 entry->EhadOverEem = ecalEnergy > 0.0 ? hcalEnergy/ecalEnergy : 999.9;
[1348]535
536 //--- Pile-Up Jet ID variables ----
[988]537
[1348]538 entry->NCharged = candidate->NCharged;
539 entry->NNeutrals = candidate->NNeutrals;
540 entry->Beta = candidate->Beta;
541 entry->BetaStar = candidate->BetaStar;
542 entry->MeanSqDeltaR = candidate->MeanSqDeltaR;
543 entry->PTD = candidate->PTD;
544 entry->FracPt[0] = candidate->FracPt[0];
545 entry->FracPt[1] = candidate->FracPt[1];
546 entry->FracPt[2] = candidate->FracPt[2];
547 entry->FracPt[3] = candidate->FracPt[3];
548 entry->FracPt[4] = candidate->FracPt[4];
549
[930]550 FillParticles(candidate, &entry->Particles);
[694]551 }
552}
553
554//------------------------------------------------------------------------------
555
[895]556void TreeWriter::ProcessMissingET(ExRootTreeBranch *branch, TObjArray *array)
[694]557{
558 Candidate *candidate = 0;
559 MissingET *entry = 0;
560
[893]561 // get the first entry
[895]562 if((candidate = static_cast<Candidate*>(array->At(0))))
[694]563 {
564 const TLorentzVector &momentum = candidate->Momentum;
565
566 entry = static_cast<MissingET*>(branch->NewEntry());
567
[1321]568 entry->Eta = (-momentum).Eta();
[1092]569 entry->Phi = (-momentum).Phi();
[893]570 entry->MET = momentum.Pt();
[694]571 }
572}
573
574//------------------------------------------------------------------------------
575
[895]576void TreeWriter::ProcessScalarHT(ExRootTreeBranch *branch, TObjArray *array)
[893]577{
578 Candidate *candidate = 0;
579 ScalarHT *entry = 0;
580
581 // get the first entry
[895]582 if((candidate = static_cast<Candidate*>(array->At(0))))
[893]583 {
584 const TLorentzVector &momentum = candidate->Momentum;
585
586 entry = static_cast<ScalarHT*>(branch->NewEntry());
587
588 entry->HT = momentum.Pt();
589 }
590}
591
592//------------------------------------------------------------------------------
593
[1114]594void TreeWriter::ProcessRho(ExRootTreeBranch *branch, TObjArray *array)
595{
[1321]596 TIter iterator(array);
[1114]597 Candidate *candidate = 0;
598 Rho *entry = 0;
599
[1321]600 // loop over all rho
601 iterator.Reset();
602 while((candidate = static_cast<Candidate*>(iterator.Next())))
[1114]603 {
604 const TLorentzVector &momentum = candidate->Momentum;
605
606 entry = static_cast<Rho*>(branch->NewEntry());
607
[1115]608 entry->Rho = momentum.E();
[1321]609 entry->Edges[0] = candidate->Edges[0];
610 entry->Edges[1] = candidate->Edges[1];
[1114]611 }
612}
613
614//------------------------------------------------------------------------------
615
[1123]616void TreeWriter::ProcessWeight(ExRootTreeBranch *branch, TObjArray *array)
617{
618 Candidate *candidate = 0;
619 Weight *entry = 0;
620
621 // get the first entry
622 if((candidate = static_cast<Candidate*>(array->At(0))))
623 {
624 const TLorentzVector &momentum = candidate->Momentum;
625
626 entry = static_cast<Weight*>(branch->NewEntry());
627
628 entry->Weight = momentum.E();
629 }
630}
631
632//------------------------------------------------------------------------------
633
[694]634void TreeWriter::Process()
635{
636 TBranchMap::iterator itBranchMap;
637 ExRootTreeBranch *branch;
638 TProcessMethod method;
[895]639 TObjArray *array;
[694]640
641 for(itBranchMap = fBranchMap.begin(); itBranchMap != fBranchMap.end(); ++itBranchMap)
642 {
643 branch = itBranchMap->first;
644 method = itBranchMap->second.first;
[895]645 array = itBranchMap->second.second;
[694]646
[895]647 (this->*method)(branch, array);
[694]648 }
649}
650
651//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.