Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mathcoreVectorIO.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook -nodraw
4/// Example of I/O of a GenVector Lorentz Vectors in a Tree and comparison with legacy TLorentzVector.
5///
6/// A ROOT tree is written and read in both using either a XYZTVector or a TLorentzVector.
7///
8/// To execute the macro type in:
9///
10/// ~~~{.cpp}
11/// root[0] .x mathcoreVectorIO.C
12/// ~~~
13///
14/// \macro_output
15/// \macro_code
16///
17/// \author Lorenzo Moneta
18
19
20#include "TRandom2.h"
21#include "TStopwatch.h"
22#include "TSystem.h"
23#include "TFile.h"
24#include "TTree.h"
25#include "TH1D.h"
26#include "TCanvas.h"
27
28#include <iostream>
29
30#include "TLorentzVector.h"
31
32#include "Math/Vector4D.h"
33
34using namespace ROOT::Math;
35
36void write(int n) {
37 TRandom2 R;
39
40
41 R.SetSeed(1);
42 timer.Start();
43 double s = 0;
44 for (int i = 0; i < n; ++i) {
45 s += R.Gaus(0,10);
46 s += R.Gaus(0,10);
47 s += R.Gaus(0,10);
48 s += R.Gaus(100,10);
49 }
50
51 timer.Stop();
52 std::cout << s/double(n) << std::endl;
53 std::cout << " Time for Random gen " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
54
55
56 TFile f1("mathcoreVectorIO_1.root","RECREATE");
57
58 // create tree
59 TTree t1("t1","Tree with new LorentzVector");
60
61 XYZTVector *v1 = new XYZTVector();
62 t1.Branch("LV branch","ROOT::Math::XYZTVector",&v1);
63
64 R.SetSeed(1);
65 timer.Start();
66 for (int i = 0; i < n; ++i) {
67 double Px = R.Gaus(0,10);
68 double Py = R.Gaus(0,10);
69 double Pz = R.Gaus(0,10);
70 double E = R.Gaus(100,10);
71 v1->SetCoordinates(Px,Py,Pz,E);
72 t1.Fill();
73 }
74
75 f1.Write();
76 timer.Stop();
77 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
78
79 t1.Print();
80
81 // create tree with old LV
82
83 TFile f2("mathcoreVectorIO_2.root","RECREATE");
84 TTree t2("t2","Tree with TLorentzVector");
85
87 TLorentzVector::Class()->IgnoreTObjectStreamer();
88 TVector3::Class()->IgnoreTObjectStreamer();
89
90 t2.Branch("TLV branch","TLorentzVector",&v2,16000,2);
91
92 R.SetSeed(1);
93 timer.Start();
94 for (int i = 0; i < n; ++i) {
95 double Px = R.Gaus(0,10);
96 double Py = R.Gaus(0,10);
97 double Pz = R.Gaus(0,10);
98 double E = R.Gaus(100,10);
99 v2->SetPxPyPzE(Px,Py,Pz,E);
100 t2.Fill();
101 }
102
103 f2.Write();
104 timer.Stop();
105 std::cout << " Time for old Vector " << timer.RealTime() << " " << timer.CpuTime() << endl;
106 t2.Print();
107}
108
109
110void read() {
111
112 TRandom R;
114
115 TFile f1("mathcoreVectorIO_1.root");
116
117 // create tree
118 TTree *t1 = (TTree*)f1.Get("t1");
119
120 XYZTVector *v1 = 0;
121 t1->SetBranchAddress("LV branch",&v1);
122
123 timer.Start();
124 int n = (int) t1->GetEntries();
125 std::cout << " Tree Entries " << n << std::endl;
126 double etot=0;
127 for (int i = 0; i < n; ++i) {
128 t1->GetEntry(i);
129 etot += v1->Px();
130 etot += v1->Py();
131 etot += v1->Pz();
132 etot += v1->E();
133 }
134 timer.Stop();
135 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
136
137 std::cout << " TOT average : n = " << n << "\t " << etot/double(n) << endl;
138
139 // create tree with old LV
140 TFile f2("mathcoreVectorIO_2.root");
141 TTree *t2 = (TTree*)f2.Get("t2");
142
143 TLorentzVector * v2 = 0;
144 t2->SetBranchAddress("TLV branch",&v2);
145
146 timer.Start();
147 n = (int) t2->GetEntries();
148 std::cout << " Tree Entries " << n << std::endl;
149 etot = 0;
150 for (int i = 0; i < n; ++i) {
151 t2->GetEntry(i);
152 etot += v2->Px();
153 etot += v2->Py();
154 etot += v2->Pz();
155 etot += v2->E();
156 }
157
158 timer.Stop();
159 std::cout << " Time for old Vector " << timer.RealTime() << " " << timer.CpuTime() << endl;
160 std::cout << " TOT average:\t" << etot/double(n) << endl;
161}
162
163void mathcoreVectorIO() {
164
165 int nEvents = 100000;
166 write(nEvents);
167 read();
168}
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
static TClass * Class()
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:964
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition TRandom2.h:27
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Stopwatch class.
Definition TStopwatch.h:28
A TTree represents a columnar dataset.
Definition TTree.h:89
static TClass * Class()
const Int_t n
Definition legend1.C:16
TF1 * f1
Definition legend1.C:11
constexpr Double_t E()
Base of natural log: .
Definition TMath.h:94
auto * t1
Definition textangle.C:20