Fork me on GitHub

source: svn/trunk/modules/TreeWriter.cc

Last change on this file was 1372, checked in by Pavel Demin, 11 years ago

several minor corrections

  • Property svn:keywords set to Id Revision Date
File size: 18.8 KB
Line 
1
2/** \class TreeWriter
3 *
4 * Fills ROOT tree branches.
5 *
6 * $Date: 2014-04-17 08:54:17 +0000 (Thu, 17 Apr 2014) $
7 * $Revision: 1372 $
8 *
9 *
10 * \author P. Demin - UCL, Louvain-la-Neuve
11 *
12 */
13
14#include "modules/TreeWriter.h"
15
16#include "classes/DelphesClasses.h"
17#include "classes/DelphesFactory.h"
18#include "classes/DelphesFormula.h"
19
20#include "ExRootAnalysis/ExRootResult.h"
21#include "ExRootAnalysis/ExRootFilter.h"
22#include "ExRootAnalysis/ExRootClassifier.h"
23#include "ExRootAnalysis/ExRootTreeBranch.h"
24
25#include "TROOT.h"
26#include "TMath.h"
27#include "TString.h"
28#include "TFormula.h"
29#include "TRandom3.h"
30#include "TObjArray.h"
31#include "TDatabasePDG.h"
32#include "TLorentzVector.h"
33
34#include <algorithm>
35#include <stdexcept>
36#include <iostream>
37#include <sstream>
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;
58 fClassMap[Vertex::Class()] = &TreeWriter::ProcessVertices;
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;
66 fClassMap[ScalarHT::Class()] = &TreeWriter::ProcessScalarHT;
67 fClassMap[Rho::Class()] = &TreeWriter::ProcessRho;
68 fClassMap[Weight::Class()] = &TreeWriter::ProcessWeight;
69 fClassMap[HectorHit::Class()] = &TreeWriter::ProcessHectorHit;
70
71 TBranchMap::iterator itBranchMap;
72 map< TClass *, TProcessMethod >::iterator itClassMap;
73
74 // read branch configuration and
75 // import array with output from filter/classifier/jetfinder modules
76
77 ExRootConfParam param = GetParam("Branch");
78 Long_t i, size;
79 TString branchName, branchClassName, branchInputArray;
80 TClass *branchClass;
81 TObjArray *array;
82 ExRootTreeBranch *branch;
83
84 size = param.GetSize();
85 for(i = 0; i < size/3; ++i)
86 {
87 branchInputArray = param[i*3].GetString();
88 branchName = param[i*3 + 1].GetString();
89 branchClassName = param[i*3 + 2].GetString();
90
91 branchClass = gROOT->GetClass(branchClassName);
92
93 if(!branchClass)
94 {
95 cout << "** ERROR: cannot find class '" << branchClassName << "'" << endl;
96 continue;
97 }
98
99 itClassMap = fClassMap.find(branchClass);
100 if(itClassMap == fClassMap.end())
101 {
102 cout << "** ERROR: cannot create branch for class '" << branchClassName << "'" << endl;
103 continue;
104 }
105
106 array = ImportArray(branchInputArray);
107 branch = NewBranch(branchName, branchClass);
108
109 fBranchMap.insert(make_pair(branch, make_pair(itClassMap->second, array)));
110 }
111
112}
113
114//------------------------------------------------------------------------------
115
116void TreeWriter::Finish()
117{
118}
119
120//------------------------------------------------------------------------------
121
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
157void TreeWriter::ProcessParticles(ExRootTreeBranch *branch, TObjArray *array)
158{
159 TIter iterator(array);
160 Candidate *candidate = 0;
161 GenParticle *entry = 0;
162 Double_t pt, signPz, cosTheta, eta, rapidity;
163
164 const Double_t c_light = 2.99792458E8;
165
166 // loop over all particles
167 iterator.Reset();
168 while((candidate = static_cast<Candidate*>(iterator.Next())))
169 {
170 const TLorentzVector &momentum = candidate->Momentum;
171 const TLorentzVector &position = candidate->Position;
172
173 entry = static_cast<GenParticle*>(branch->NewEntry());
174
175 entry->SetBit(kIsReferenced);
176 entry->SetUniqueID(candidate->GetUniqueID());
177
178 pt = momentum.Pt();
179 cosTheta = TMath::Abs(momentum.CosTheta());
180 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
181 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
182 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
183
184 entry->PID = candidate->PID;
185
186 entry->Status = candidate->Status;
187 entry->IsPU = candidate->IsPU;
188
189 entry->M1 = candidate->M1;
190 entry->M2 = candidate->M2;
191
192 entry->D1 = candidate->D1;
193 entry->D2 = candidate->D2;
194
195 entry->Charge = candidate->Charge;
196 entry->Mass = candidate->Mass;
197
198 entry->E = momentum.E();
199 entry->Px = momentum.Px();
200 entry->Py = momentum.Py();
201 entry->Pz = momentum.Pz();
202
203 entry->Eta = eta;
204 entry->Phi = momentum.Phi();
205 entry->PT = pt;
206
207 entry->Rapidity = rapidity;
208
209 entry->X = position.X();
210 entry->Y = position.Y();
211 entry->Z = position.Z();
212 entry->T = position.T()*1.0E-3/c_light;
213 }
214}
215
216//------------------------------------------------------------------------------
217
218void TreeWriter::ProcessVertices(ExRootTreeBranch *branch, TObjArray *array)
219{
220 TIter iterator(array);
221 Candidate *candidate = 0;
222 Vertex *entry = 0;
223
224 const Double_t c_light = 2.99792458E8;
225
226 // loop over all vertices
227 iterator.Reset();
228 while((candidate = static_cast<Candidate*>(iterator.Next())))
229 {
230 const TLorentzVector &position = candidate->Position;
231
232 entry = static_cast<Vertex*>(branch->NewEntry());
233
234 entry->X = position.X();
235 entry->Y = position.Y();
236 entry->Z = position.Z();
237 entry->T = position.T()*1.0E-3/c_light;
238 }
239}
240
241//------------------------------------------------------------------------------
242
243void TreeWriter::ProcessTracks(ExRootTreeBranch *branch, TObjArray *array)
244{
245 TIter iterator(array);
246 Candidate *candidate = 0;
247 Candidate *particle = 0;
248 Track *entry = 0;
249 Double_t pt, signz, cosTheta, eta, rapidity;
250 const Double_t c_light = 2.99792458E8;
251
252 // loop over all tracks
253 iterator.Reset();
254 while((candidate = static_cast<Candidate*>(iterator.Next())))
255 {
256 const TLorentzVector &position = candidate->Position;
257
258 cosTheta = TMath::Abs(position.CosTheta());
259 signz = (position.Pz() >= 0.0) ? 1.0 : -1.0;
260 eta = (cosTheta == 1.0 ? signz*999.9 : position.Eta());
261 rapidity = (cosTheta == 1.0 ? signz*999.9 : position.Rapidity());
262
263 entry = static_cast<Track*>(branch->NewEntry());
264
265 entry->SetBit(kIsReferenced);
266 entry->SetUniqueID(candidate->GetUniqueID());
267
268 entry->PID = candidate->PID;
269
270 entry->Charge = candidate->Charge;
271
272 entry->EtaOuter = eta;
273 entry->PhiOuter = position.Phi();
274
275 entry->XOuter = position.X();
276 entry->YOuter = position.Y();
277 entry->ZOuter = position.Z();
278 entry->TOuter = position.T()*1.0E-3/c_light;
279
280 entry->Dxy = candidate->Dxy;
281 entry->SDxy = candidate->SDxy ;
282 entry->Xd = candidate->Xd;
283 entry->Yd = candidate->Yd;
284 entry->Zd = candidate->Zd;
285
286 const TLorentzVector &momentum = candidate->Momentum;
287
288 pt = momentum.Pt();
289 cosTheta = TMath::Abs(momentum.CosTheta());
290 signz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
291 eta = (cosTheta == 1.0 ? signz*999.9 : momentum.Eta());
292 rapidity = (cosTheta == 1.0 ? signz*999.9 : momentum.Rapidity());
293
294 entry->Eta = eta;
295 entry->Phi = momentum.Phi();
296 entry->PT = pt;
297
298 particle = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
299 const TLorentzVector &initialPosition = particle->Position;
300
301 entry->X = initialPosition.X();
302 entry->Y = initialPosition.Y();
303 entry->Z = initialPosition.Z();
304 entry->T = initialPosition.T()*1.0E-3/c_light;
305
306 entry->Particle = particle;
307 }
308}
309
310//------------------------------------------------------------------------------
311
312void TreeWriter::ProcessTowers(ExRootTreeBranch *branch, TObjArray *array)
313{
314 TIter iterator(array);
315 Candidate *candidate = 0;
316 Tower *entry = 0;
317 Double_t pt, signPz, cosTheta, eta, rapidity;
318 const Double_t c_light = 2.99792458E8;
319
320 // loop over all towers
321 iterator.Reset();
322 while((candidate = static_cast<Candidate*>(iterator.Next())))
323 {
324 const TLorentzVector &momentum = candidate->Momentum;
325 const TLorentzVector &position = candidate->Position;
326
327 pt = momentum.Pt();
328 cosTheta = TMath::Abs(momentum.CosTheta());
329 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
330 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
331 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
332
333 entry = static_cast<Tower*>(branch->NewEntry());
334
335 entry->SetBit(kIsReferenced);
336 entry->SetUniqueID(candidate->GetUniqueID());
337
338 entry->Eta = eta;
339 entry->Phi = momentum.Phi();
340 entry->ET = pt;
341 entry->E = momentum.E();
342 entry->Eem = candidate->Eem;
343 entry->Ehad = candidate->Ehad;
344 entry->Edges[0] = candidate->Edges[0];
345 entry->Edges[1] = candidate->Edges[1];
346 entry->Edges[2] = candidate->Edges[2];
347 entry->Edges[3] = candidate->Edges[3];
348
349 entry->T = position.T()*1.0E-3/c_light;
350
351 FillParticles(candidate, &entry->Particles);
352 }
353}
354
355//------------------------------------------------------------------------------
356
357void TreeWriter::ProcessPhotons(ExRootTreeBranch *branch, TObjArray *array)
358{
359 TIter iterator(array);
360 Candidate *candidate = 0;
361 Photon *entry = 0;
362 Double_t pt, signPz, cosTheta, eta, rapidity;
363 const Double_t c_light = 2.99792458E8;
364
365 array->Sort();
366
367 // loop over all photons
368 iterator.Reset();
369 while((candidate = static_cast<Candidate*>(iterator.Next())))
370 {
371 TIter it1(candidate->GetCandidates());
372 const TLorentzVector &momentum = candidate->Momentum;
373 const TLorentzVector &position = candidate->Position;
374
375
376 pt = momentum.Pt();
377 cosTheta = TMath::Abs(momentum.CosTheta());
378 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
379 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
380 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
381
382 entry = static_cast<Photon*>(branch->NewEntry());
383
384 entry->Eta = eta;
385 entry->Phi = momentum.Phi();
386 entry->PT = pt;
387 entry->E = momentum.E();
388
389 entry->T = position.T()*1.0E-3/c_light;
390
391 entry->EhadOverEem = candidate->Eem > 0.0 ? candidate->Ehad/candidate->Eem : 999.9;
392
393 FillParticles(candidate, &entry->Particles);
394 }
395}
396
397//------------------------------------------------------------------------------
398
399void TreeWriter::ProcessElectrons(ExRootTreeBranch *branch, TObjArray *array)
400{
401 TIter iterator(array);
402 Candidate *candidate = 0;
403 Electron *entry = 0;
404 Double_t pt, signPz, cosTheta, eta, rapidity;
405 const Double_t c_light = 2.99792458E8;
406
407 array->Sort();
408
409 // loop over all electrons
410 iterator.Reset();
411 while((candidate = static_cast<Candidate*>(iterator.Next())))
412 {
413 const TLorentzVector &momentum = candidate->Momentum;
414 const TLorentzVector &position = candidate->Position;
415
416 pt = momentum.Pt();
417 cosTheta = TMath::Abs(momentum.CosTheta());
418 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
419 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
420 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
421
422 entry = static_cast<Electron*>(branch->NewEntry());
423
424 entry->Eta = eta;
425 entry->Phi = momentum.Phi();
426 entry->PT = pt;
427
428 entry->T = position.T()*1.0E-3/c_light;
429
430 entry->Charge = candidate->Charge;
431
432 entry->EhadOverEem = 0.0;
433
434 entry->Particle = candidate->GetCandidates()->At(0);
435 }
436}
437
438//------------------------------------------------------------------------------
439
440void TreeWriter::ProcessMuons(ExRootTreeBranch *branch, TObjArray *array)
441{
442 TIter iterator(array);
443 Candidate *candidate = 0;
444 Muon *entry = 0;
445 Double_t pt, signPz, cosTheta, eta, rapidity;
446
447 const Double_t c_light = 2.99792458E8;
448
449 array->Sort();
450
451 // loop over all muons
452 iterator.Reset();
453 while((candidate = static_cast<Candidate*>(iterator.Next())))
454 {
455 const TLorentzVector &momentum = candidate->Momentum;
456 const TLorentzVector &position = candidate->Position;
457
458
459 pt = momentum.Pt();
460 cosTheta = TMath::Abs(momentum.CosTheta());
461 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
462 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
463 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
464
465 entry = static_cast<Muon*>(branch->NewEntry());
466
467 entry->SetBit(kIsReferenced);
468 entry->SetUniqueID(candidate->GetUniqueID());
469
470 entry->Eta = eta;
471 entry->Phi = momentum.Phi();
472 entry->PT = pt;
473
474 entry->T = position.T()*1.0E-3/c_light;
475
476 entry->Charge = candidate->Charge;
477
478 entry->Particle = candidate->GetCandidates()->At(0);
479 }
480}
481
482//------------------------------------------------------------------------------
483
484void TreeWriter::ProcessJets(ExRootTreeBranch *branch, TObjArray *array)
485{
486 TIter iterator(array);
487 Candidate *candidate = 0, *constituent = 0;
488 Jet *entry = 0;
489 Double_t pt, signPz, cosTheta, eta, rapidity;
490 Double_t ecalEnergy, hcalEnergy;
491 const Double_t c_light = 2.99792458E8;
492
493 array->Sort();
494
495 // loop over all jets
496 iterator.Reset();
497 while((candidate = static_cast<Candidate*>(iterator.Next())))
498 {
499 TIter itConstituents(candidate->GetCandidates());
500
501 const TLorentzVector &momentum = candidate->Momentum;
502 const TLorentzVector &position = candidate->Position;
503
504 pt = momentum.Pt();
505 cosTheta = TMath::Abs(momentum.CosTheta());
506 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
507 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
508 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
509
510 entry = static_cast<Jet*>(branch->NewEntry());
511
512 entry->Eta = eta;
513 entry->Phi = momentum.Phi();
514 entry->PT = pt;
515
516 entry->T = position.T()*1.0E-3/c_light;
517
518 entry->Mass = momentum.M();
519
520 entry->DeltaEta = candidate->DeltaEta;
521 entry->DeltaPhi = candidate->DeltaPhi;
522
523 entry->BTag = candidate->BTag;
524 entry->TauTag = candidate->TauTag;
525
526 entry->Charge = candidate->Charge;
527
528 itConstituents.Reset();
529 entry->Constituents.Clear();
530 ecalEnergy = 0.0;
531 hcalEnergy = 0.0;
532 while((constituent = static_cast<Candidate*>(itConstituents.Next())))
533 {
534 entry->Constituents.Add(constituent);
535 ecalEnergy += constituent->Eem;
536 hcalEnergy += constituent->Ehad;
537 }
538
539 entry->EhadOverEem = ecalEnergy > 0.0 ? hcalEnergy/ecalEnergy : 999.9;
540
541 //--- Pile-Up Jet ID variables ----
542
543 entry->NCharged = candidate->NCharged;
544 entry->NNeutrals = candidate->NNeutrals;
545 entry->Beta = candidate->Beta;
546 entry->BetaStar = candidate->BetaStar;
547 entry->MeanSqDeltaR = candidate->MeanSqDeltaR;
548 entry->PTD = candidate->PTD;
549 entry->FracPt[0] = candidate->FracPt[0];
550 entry->FracPt[1] = candidate->FracPt[1];
551 entry->FracPt[2] = candidate->FracPt[2];
552 entry->FracPt[3] = candidate->FracPt[3];
553 entry->FracPt[4] = candidate->FracPt[4];
554
555 //--- N-subjettiness variables ----
556
557 entry->Tau1 = candidate->Tau[0];
558 entry->Tau2 = candidate->Tau[1];
559 entry->Tau3 = candidate->Tau[2];
560 entry->Tau4 = candidate->Tau[3];
561 entry->Tau5 = candidate->Tau[4];
562
563 FillParticles(candidate, &entry->Particles);
564 }
565}
566
567//------------------------------------------------------------------------------
568
569void TreeWriter::ProcessMissingET(ExRootTreeBranch *branch, TObjArray *array)
570{
571 Candidate *candidate = 0;
572 MissingET *entry = 0;
573
574 // get the first entry
575 if((candidate = static_cast<Candidate*>(array->At(0))))
576 {
577 const TLorentzVector &momentum = candidate->Momentum;
578
579 entry = static_cast<MissingET*>(branch->NewEntry());
580
581 entry->Eta = (-momentum).Eta();
582 entry->Phi = (-momentum).Phi();
583 entry->MET = momentum.Pt();
584 }
585}
586
587//------------------------------------------------------------------------------
588
589void TreeWriter::ProcessScalarHT(ExRootTreeBranch *branch, TObjArray *array)
590{
591 Candidate *candidate = 0;
592 ScalarHT *entry = 0;
593
594 // get the first entry
595 if((candidate = static_cast<Candidate*>(array->At(0))))
596 {
597 const TLorentzVector &momentum = candidate->Momentum;
598
599 entry = static_cast<ScalarHT*>(branch->NewEntry());
600
601 entry->HT = momentum.Pt();
602 }
603}
604
605//------------------------------------------------------------------------------
606
607void TreeWriter::ProcessRho(ExRootTreeBranch *branch, TObjArray *array)
608{
609 TIter iterator(array);
610 Candidate *candidate = 0;
611 Rho *entry = 0;
612
613 // loop over all rho
614 iterator.Reset();
615 while((candidate = static_cast<Candidate*>(iterator.Next())))
616 {
617 const TLorentzVector &momentum = candidate->Momentum;
618
619 entry = static_cast<Rho*>(branch->NewEntry());
620
621 entry->Rho = momentum.E();
622 entry->Edges[0] = candidate->Edges[0];
623 entry->Edges[1] = candidate->Edges[1];
624 }
625}
626
627//------------------------------------------------------------------------------
628
629void TreeWriter::ProcessWeight(ExRootTreeBranch *branch, TObjArray *array)
630{
631 Candidate *candidate = 0;
632 Weight *entry = 0;
633
634 // get the first entry
635 if((candidate = static_cast<Candidate*>(array->At(0))))
636 {
637 const TLorentzVector &momentum = candidate->Momentum;
638
639 entry = static_cast<Weight*>(branch->NewEntry());
640
641 entry->Weight = momentum.E();
642 }
643}
644
645//------------------------------------------------------------------------------
646
647void TreeWriter::ProcessHectorHit(ExRootTreeBranch *branch, TObjArray *array)
648{
649 TIter iterator(array);
650 Candidate *candidate = 0;
651 HectorHit *entry = 0;
652
653 // loop over all roman pot hits
654 iterator.Reset();
655 while((candidate = static_cast<Candidate*>(iterator.Next())))
656 {
657 const TLorentzVector &position = candidate->Position;
658 const TLorentzVector &momentum = candidate->Momentum;
659
660 entry = static_cast<HectorHit*>(branch->NewEntry());
661
662 entry->E = momentum.E();
663
664 entry->Tx = momentum.Px();
665 entry->Ty = momentum.Py();
666
667 entry->T = position.T();
668
669 entry->X = position.X();
670 entry->Y = position.Y();
671 entry->S = position.Z();
672
673 entry->Particle = candidate->GetCandidates()->At(0);
674 }
675}
676
677//------------------------------------------------------------------------------
678
679void TreeWriter::Process()
680{
681 TBranchMap::iterator itBranchMap;
682 ExRootTreeBranch *branch;
683 TProcessMethod method;
684 TObjArray *array;
685
686 for(itBranchMap = fBranchMap.begin(); itBranchMap != fBranchMap.end(); ++itBranchMap)
687 {
688 branch = itBranchMap->first;
689 method = itBranchMap->second.first;
690 array = itBranchMap->second.second;
691
692 (this->*method)(branch, array);
693 }
694}
695
696//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.