Fork me on GitHub

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

Last change on this file since 1150 was 1123, checked in by Pavel Demin, 12 years ago

add Weight branch and Weighter module

  • Property svn:keywords set to Id Revision Date
File size: 15.2 KB
Line 
1
2/** \class TreeWriter
3 *
4 * Fills ROOT tree branches.
5 *
6 * $Date: 2013-05-26 00:00:39 +0000 (Sun, 26 May 2013) $
7 * $Revision: 1123 $
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[Track::Class()] = &TreeWriter::ProcessTracks;
59 fClassMap[Tower::Class()] = &TreeWriter::ProcessTowers;
60 fClassMap[Photon::Class()] = &TreeWriter::ProcessPhotons;
61 fClassMap[Electron::Class()] = &TreeWriter::ProcessElectrons;
62 fClassMap[Muon::Class()] = &TreeWriter::ProcessMuons;
63 fClassMap[Jet::Class()] = &TreeWriter::ProcessJets;
64 fClassMap[MissingET::Class()] = &TreeWriter::ProcessMissingET;
65 fClassMap[ScalarHT::Class()] = &TreeWriter::ProcessScalarHT;
66 fClassMap[Rho::Class()] = &TreeWriter::ProcessRho;
67 fClassMap[Weight::Class()] = &TreeWriter::ProcessWeight;
68
69 TBranchMap::iterator itBranchMap;
70 map< TClass *, TProcessMethod >::iterator itClassMap;
71
72 // read branch configuration and
73 // import array with output from filter/classifier/jetfinder modules
74
75 ExRootConfParam param = GetParam("Branch");
76 Long_t i, size;
77 TString branchName, branchClassName, branchInputArray;
78 TClass *branchClass;
79 TObjArray *array;
80 ExRootTreeBranch *branch;
81
82 size = param.GetSize();
83 for(i = 0; i < size/3; ++i)
84 {
85 branchInputArray = param[i*3].GetString();
86 branchName = param[i*3 + 1].GetString();
87 branchClassName = param[i*3 + 2].GetString();
88
89 branchClass = gROOT->GetClass(branchClassName);
90
91 if(!branchClass)
92 {
93 cout << "** ERROR: cannot find class '" << branchClassName << "'" << endl;
94 continue;
95 }
96
97 itClassMap = fClassMap.find(branchClass);
98 if(itClassMap == fClassMap.end())
99 {
100 cout << "** ERROR: cannot create branch for class '" << branchClassName << "'" << endl;
101 continue;
102 }
103
104 array = ImportArray(branchInputArray);
105 branch = NewBranch(branchName, branchClass);
106
107 fBranchMap.insert(make_pair(branch, make_pair(itClassMap->second, array)));
108 }
109
110}
111
112//------------------------------------------------------------------------------
113
114void TreeWriter::Finish()
115{
116
117}
118
119//------------------------------------------------------------------------------
120
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
156void TreeWriter::ProcessParticles(ExRootTreeBranch *branch, TObjArray *array)
157{
158 TIter iterator(array);
159 Candidate *candidate = 0;
160 GenParticle *entry = 0;
161 Double_t pt, signPz, cosTheta, eta, rapidity;
162
163 // loop over all particles
164 iterator.Reset();
165 while((candidate = static_cast<Candidate*>(iterator.Next())))
166 {
167 const TLorentzVector &momentum = candidate->Momentum;
168 const TLorentzVector &position = candidate->Position;
169
170 entry = static_cast<GenParticle*>(branch->NewEntry());
171
172 entry->SetBit(kIsReferenced);
173 entry->SetUniqueID(candidate->GetUniqueID());
174
175 pt = momentum.Pt();
176 cosTheta = TMath::Abs(momentum.CosTheta());
177 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
178 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
179 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
180
181 entry->PID = candidate->PID;
182
183 entry->Status = candidate->Status;
184 entry->IsPU = candidate->IsPU;
185
186 entry->M1 = candidate->M1;
187 entry->M2 = candidate->M2;
188
189 entry->D1 = candidate->D1;
190 entry->D2 = candidate->D2;
191
192 entry->Charge = candidate->Charge;
193 entry->Mass = candidate->Mass;
194
195 entry->E = momentum.E();
196 entry->Px = momentum.Px();
197 entry->Py = momentum.Py();
198 entry->Pz = momentum.Pz();
199
200 entry->Eta = eta;
201 entry->Phi = momentum.Phi();
202 entry->PT = pt;
203
204 entry->Rapidity = rapidity;
205
206 entry->X = position.X();
207 entry->Y = position.Y();
208 entry->Z = position.Z();
209 entry->T = position.T();
210 }
211}
212
213//------------------------------------------------------------------------------
214
215void TreeWriter::ProcessTracks(ExRootTreeBranch *branch, TObjArray *array)
216{
217 TIter iterator(array);
218 Candidate *candidate = 0;
219 Candidate *particle = 0;
220 Track *entry = 0;
221 Double_t pt, signz, cosTheta, eta, rapidity;
222
223 // loop over all jets
224 iterator.Reset();
225 while((candidate = static_cast<Candidate*>(iterator.Next())))
226 {
227 const TLorentzVector &position = candidate->Position;
228
229 cosTheta = TMath::Abs(position.CosTheta());
230 signz = (position.Pz() >= 0.0) ? 1.0 : -1.0;
231 eta = (cosTheta == 1.0 ? signz*999.9 : position.Eta());
232 rapidity = (cosTheta == 1.0 ? signz*999.9 : position.Rapidity());
233
234 entry = static_cast<Track*>(branch->NewEntry());
235
236 entry->SetBit(kIsReferenced);
237 entry->SetUniqueID(candidate->GetUniqueID());
238
239 entry->PID = candidate->PID;
240
241 entry->Charge = candidate->Charge;
242
243 entry->EtaOuter = eta;
244 entry->PhiOuter = position.Phi();
245
246 entry->XOuter = position.X();
247 entry->YOuter = position.Y();
248 entry->ZOuter = position.Z();
249
250 const TLorentzVector &momentum = candidate->Momentum;
251
252 pt = momentum.Pt();
253 cosTheta = TMath::Abs(momentum.CosTheta());
254 signz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
255 eta = (cosTheta == 1.0 ? signz*999.9 : momentum.Eta());
256 rapidity = (cosTheta == 1.0 ? signz*999.9 : momentum.Rapidity());
257
258 entry->Eta = eta;
259 entry->Phi = momentum.Phi();
260 entry->PT = pt;
261
262 particle = static_cast<Candidate*>(candidate->GetCandidates()->At(0));
263 const TLorentzVector &initialPosition = particle->Position;
264
265 entry->X = initialPosition.X();
266 entry->Y = initialPosition.Y();
267 entry->Z = initialPosition.Z();
268
269 entry->Particle = particle;
270 }
271}
272
273//------------------------------------------------------------------------------
274
275void TreeWriter::ProcessTowers(ExRootTreeBranch *branch, TObjArray *array)
276{
277 TIter iterator(array);
278 Candidate *candidate = 0;
279 Tower *entry = 0;
280 Double_t pt, signPz, cosTheta, eta, rapidity;
281
282 // loop over all jets
283 iterator.Reset();
284 while((candidate = static_cast<Candidate*>(iterator.Next())))
285 {
286 const TLorentzVector &momentum = candidate->Momentum;
287
288 pt = momentum.Pt();
289 cosTheta = TMath::Abs(momentum.CosTheta());
290 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
291 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
292 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
293
294 entry = static_cast<Tower*>(branch->NewEntry());
295
296 entry->SetBit(kIsReferenced);
297 entry->SetUniqueID(candidate->GetUniqueID());
298
299 entry->Eta = eta;
300 entry->Phi = momentum.Phi();
301 entry->ET = pt;
302 entry->E = momentum.E();
303 entry->Eem = candidate->Eem;
304 entry->Ehad = candidate->Ehad;
305 entry->Edges[0] = candidate->Edges[0];
306 entry->Edges[1] = candidate->Edges[1];
307 entry->Edges[2] = candidate->Edges[2];
308 entry->Edges[3] = candidate->Edges[3];
309
310 FillParticles(candidate, &entry->Particles);
311 }
312}
313
314//------------------------------------------------------------------------------
315
316void TreeWriter::ProcessPhotons(ExRootTreeBranch *branch, TObjArray *array)
317{
318 TIter iterator(array);
319 Candidate *candidate = 0;
320 Photon *entry = 0;
321 Double_t pt, signPz, cosTheta, eta, rapidity;
322
323 array->Sort();
324
325 // loop over all photons
326 iterator.Reset();
327 while((candidate = static_cast<Candidate*>(iterator.Next())))
328 {
329 TIter it1(candidate->GetCandidates());
330 const TLorentzVector &momentum = candidate->Momentum;
331
332 pt = momentum.Pt();
333 cosTheta = TMath::Abs(momentum.CosTheta());
334 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
335 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
336 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
337
338 entry = static_cast<Photon*>(branch->NewEntry());
339
340 entry->Eta = eta;
341 entry->Phi = momentum.Phi();
342 entry->PT = pt;
343 entry->E = momentum.E();
344
345 entry->EhadOverEem = candidate->Eem > 0.0 ? candidate->Ehad/candidate->Eem : 999.9;
346
347 FillParticles(candidate, &entry->Particles);
348 }
349}
350
351//------------------------------------------------------------------------------
352
353void TreeWriter::ProcessElectrons(ExRootTreeBranch *branch, TObjArray *array)
354{
355 TIter iterator(array);
356 Candidate *candidate = 0;
357 Electron *entry = 0;
358 Double_t pt, signPz, cosTheta, eta, rapidity;
359
360 array->Sort();
361
362 // loop over all electrons
363 iterator.Reset();
364 while((candidate = static_cast<Candidate*>(iterator.Next())))
365 {
366 const TLorentzVector &momentum = candidate->Momentum;
367
368 pt = momentum.Pt();
369 cosTheta = TMath::Abs(momentum.CosTheta());
370 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
371 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
372 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
373
374 entry = static_cast<Electron*>(branch->NewEntry());
375
376 entry->Eta = eta;
377 entry->Phi = momentum.Phi();
378 entry->PT = pt;
379
380 entry->Charge = candidate->Charge;
381
382 entry->EhadOverEem = 0.0;
383
384 entry->Particle = candidate->GetCandidates()->At(0);
385 }
386}
387
388//------------------------------------------------------------------------------
389
390void TreeWriter::ProcessMuons(ExRootTreeBranch *branch, TObjArray *array)
391{
392 TIter iterator(array);
393 Candidate *candidate = 0;
394 Muon *entry = 0;
395 Double_t pt, signPz, cosTheta, eta, rapidity;
396
397 array->Sort();
398
399 // loop over all muons
400 iterator.Reset();
401 while((candidate = static_cast<Candidate*>(iterator.Next())))
402 {
403 const TLorentzVector &momentum = candidate->Momentum;
404
405 pt = momentum.Pt();
406 cosTheta = TMath::Abs(momentum.CosTheta());
407 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
408 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
409 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
410
411 entry = static_cast<Muon*>(branch->NewEntry());
412
413 entry->SetBit(kIsReferenced);
414 entry->SetUniqueID(candidate->GetUniqueID());
415
416 entry->Eta = eta;
417 entry->Phi = momentum.Phi();
418 entry->PT = pt;
419
420 entry->Charge = candidate->Charge;
421
422 entry->Particle = candidate->GetCandidates()->At(0);
423 }
424}
425
426//------------------------------------------------------------------------------
427
428void TreeWriter::ProcessJets(ExRootTreeBranch *branch, TObjArray *array)
429{
430 TIter iterator(array);
431 Candidate *candidate = 0, *constituent = 0;
432 Jet *entry = 0;
433 Double_t pt, signPz, cosTheta, eta, rapidity;
434 Double_t ecalEnergy, hcalEnergy;
435
436 array->Sort();
437
438 // loop over all jets
439 iterator.Reset();
440 while((candidate = static_cast<Candidate*>(iterator.Next())))
441 {
442 TIter itConstituents(candidate->GetCandidates());
443 const TLorentzVector &momentum = candidate->Momentum;
444
445 pt = momentum.Pt();
446 cosTheta = TMath::Abs(momentum.CosTheta());
447 signPz = (momentum.Pz() >= 0.0) ? 1.0 : -1.0;
448 eta = (cosTheta == 1.0 ? signPz*999.9 : momentum.Eta());
449 rapidity = (cosTheta == 1.0 ? signPz*999.9 : momentum.Rapidity());
450
451 entry = static_cast<Jet*>(branch->NewEntry());
452
453 entry->Eta = eta;
454 entry->Phi = momentum.Phi();
455 entry->PT = pt;
456
457 entry->Mass = momentum.M();
458
459 entry->DeltaEta = candidate->DeltaEta;
460 entry->DeltaPhi = candidate->DeltaPhi;
461
462 entry->BTag = candidate->BTag;
463 entry->TauTag = candidate->TauTag;
464
465 entry->Charge = candidate->Charge;
466
467 itConstituents.Reset();
468 entry->Constituents.Clear();
469 ecalEnergy = 0.0;
470 hcalEnergy = 0.0;
471 while((constituent = static_cast<Candidate*>(itConstituents.Next())))
472 {
473 entry->Constituents.Add(constituent);
474 ecalEnergy += constituent->Eem;
475 hcalEnergy += constituent->Ehad;
476 }
477
478 entry->EhadOverEem = ecalEnergy > 0.0 ? hcalEnergy/ecalEnergy : 999.9;
479
480 FillParticles(candidate, &entry->Particles);
481 }
482}
483
484//------------------------------------------------------------------------------
485
486void TreeWriter::ProcessMissingET(ExRootTreeBranch *branch, TObjArray *array)
487{
488 Candidate *candidate = 0;
489 MissingET *entry = 0;
490
491 // get the first entry
492 if((candidate = static_cast<Candidate*>(array->At(0))))
493 {
494 const TLorentzVector &momentum = candidate->Momentum;
495
496 entry = static_cast<MissingET*>(branch->NewEntry());
497
498 entry->Phi = (-momentum).Phi();
499 entry->MET = momentum.Pt();
500 }
501}
502
503//------------------------------------------------------------------------------
504
505void TreeWriter::ProcessScalarHT(ExRootTreeBranch *branch, TObjArray *array)
506{
507 Candidate *candidate = 0;
508 ScalarHT *entry = 0;
509
510 // get the first entry
511 if((candidate = static_cast<Candidate*>(array->At(0))))
512 {
513 const TLorentzVector &momentum = candidate->Momentum;
514
515 entry = static_cast<ScalarHT*>(branch->NewEntry());
516
517 entry->HT = momentum.Pt();
518 }
519}
520
521//------------------------------------------------------------------------------
522
523void TreeWriter::ProcessRho(ExRootTreeBranch *branch, TObjArray *array)
524{
525 Candidate *candidate = 0;
526 Rho *entry = 0;
527
528 // get the first entry
529 if((candidate = static_cast<Candidate*>(array->At(0))))
530 {
531 const TLorentzVector &momentum = candidate->Momentum;
532
533 entry = static_cast<Rho*>(branch->NewEntry());
534
535 entry->Rho = momentum.E();
536 }
537}
538
539//------------------------------------------------------------------------------
540
541void TreeWriter::ProcessWeight(ExRootTreeBranch *branch, TObjArray *array)
542{
543 Candidate *candidate = 0;
544 Weight *entry = 0;
545
546 // get the first entry
547 if((candidate = static_cast<Candidate*>(array->At(0))))
548 {
549 const TLorentzVector &momentum = candidate->Momentum;
550
551 entry = static_cast<Weight*>(branch->NewEntry());
552
553 entry->Weight = momentum.E();
554 }
555}
556
557//------------------------------------------------------------------------------
558
559void TreeWriter::Process()
560{
561 TBranchMap::iterator itBranchMap;
562 ExRootTreeBranch *branch;
563 TProcessMethod method;
564 TObjArray *array;
565
566 for(itBranchMap = fBranchMap.begin(); itBranchMap != fBranchMap.end(); ++itBranchMap)
567 {
568 branch = itBranchMap->first;
569 method = itBranchMap->second.first;
570 array = itBranchMap->second.second;
571
572 (this->*method)(branch, array);
573 }
574}
575
576//------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.