Logo ROOT   6.18/05
Reference Guide
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 mathcore Lorentz Vectors in a Tree and comparison with a TLorentzVector.
5/// A ROOT tree is written and read in both using either a XYZTVector or a TLorentzVector.
6///
7/// To execute the macro type in:
8///
9/// ~~~{.cpp}
10/// root[0] .x mathcoreVectorIO.C
11/// ~~~
12///
13/// \macro_output
14/// \macro_code
15///
16/// \author Lorenzo Moneta
17
18
19#include "TRandom2.h"
20#include "TStopwatch.h"
21#include "TSystem.h"
22#include "TFile.h"
23#include "TTree.h"
24#include "TH1D.h"
25#include "TCanvas.h"
26
27#include <iostream>
28
29#include "TLorentzVector.h"
30
31#include "Math/Vector4D.h"
32
33using namespace ROOT::Math;
34
35void write(int n) {
36 TRandom2 R;
37 TStopwatch timer;
38
39
40 R.SetSeed(1);
41 timer.Start();
42 double s = 0;
43 for (int i = 0; i < n; ++i) {
44 s += R.Gaus(0,10);
45 s += R.Gaus(0,10);
46 s += R.Gaus(0,10);
47 s += R.Gaus(100,10);
48 }
49
50 timer.Stop();
51 std::cout << s/double(n) << std::endl;
52 std::cout << " Time for Random gen " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
53
54
55 TFile f1("mathcoreVectorIO_1.root","RECREATE");
56
57 // create tree
58 TTree t1("t1","Tree with new LorentzVector");
59
60 XYZTVector *v1 = new XYZTVector();
61 t1.Branch("LV branch","ROOT::Math::XYZTVector",&v1);
62
63 R.SetSeed(1);
64 timer.Start();
65 for (int i = 0; i < n; ++i) {
66 double Px = R.Gaus(0,10);
67 double Py = R.Gaus(0,10);
68 double Pz = R.Gaus(0,10);
69 double E = R.Gaus(100,10);
70 v1->SetCoordinates(Px,Py,Pz,E);
71 t1.Fill();
72 }
73
74 f1.Write();
75 timer.Stop();
76 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
77
78 t1.Print();
79
80 // create tree with old LV
81
82 TFile f2("mathcoreVectorIO_2.root","RECREATE");
83 TTree t2("t2","Tree with TLorentzVector");
84
85 TLorentzVector * v2 = new TLorentzVector();
86 TLorentzVector::Class()->IgnoreTObjectStreamer();
87 TVector3::Class()->IgnoreTObjectStreamer();
88
89 t2.Branch("TLV branch","TLorentzVector",&v2,16000,2);
90
91 R.SetSeed(1);
92 timer.Start();
93 for (int i = 0; i < n; ++i) {
94 double Px = R.Gaus(0,10);
95 double Py = R.Gaus(0,10);
96 double Pz = R.Gaus(0,10);
97 double E = R.Gaus(100,10);
98 v2->SetPxPyPzE(Px,Py,Pz,E);
99 t2.Fill();
100 }
101
102 f2.Write();
103 timer.Stop();
104 std::cout << " Time for old Vector " << timer.RealTime() << " " << timer.CpuTime() << endl;
105 t2.Print();
106}
107
108
109void read() {
110
111 TRandom R;
112 TStopwatch timer;
113
114 TFile f1("mathcoreVectorIO_1.root");
115
116 // create tree
117 TTree *t1 = (TTree*)f1.Get("t1");
118
119 XYZTVector *v1 = 0;
120 t1->SetBranchAddress("LV branch",&v1);
121
122 timer.Start();
123 int n = (int) t1->GetEntries();
124 std::cout << " Tree Entries " << n << std::endl;
125 double etot=0;
126 for (int i = 0; i < n; ++i) {
127 t1->GetEntry(i);
128 etot += v1->Px();
129 etot += v1->Py();
130 etot += v1->Pz();
131 etot += v1->E();
132 }
133 timer.Stop();
134 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
135
136 std::cout << " TOT average : n = " << n << "\t " << etot/double(n) << endl;
137
138 // create tree with old LV
139 TFile f2("mathcoreVectorIO_2.root");
140 TTree *t2 = (TTree*)f2.Get("t2");
141
142 TLorentzVector * v2 = 0;
143 t2->SetBranchAddress("TLV branch",&v2);
144
145 timer.Start();
146 n = (int) t2->GetEntries();
147 std::cout << " Tree Entries " << n << std::endl;
148 etot = 0;
149 for (int i = 0; i < n; ++i) {
150 t2->GetEntry(i);
151 etot += v2->Px();
152 etot += v2->Py();
153 etot += v2->Pz();
154 etot += v2->E();
155 }
156
157 timer.Stop();
158 std::cout << " Time for old Vector " << timer.RealTime() << " " << timer.CpuTime() << endl;
159 std::cout << " TOT average:\t" << etot/double(n) << endl;
160}
161
162void mathcoreVectorIO() {
163
164 int nEvents = 100000;
165 write(nEvents);
166 read();
167}
void Class()
Definition: Class.C:29
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
Scalar E() const
return 4-th component (time, or energy for a 4-momentum vector)
Scalar Py() const
spatial Y component
Scalar Pz() const
spatial Z component
LorentzVector< CoordSystem > & SetCoordinates(const Scalar src[])
Set internal data based on an array of 4 Scalar numbers.
Scalar Px() const
spatial X component
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
Double_t Px() const
Double_t Pz() const
Double_t Py() const
void SetPxPyPzE(Double_t px, Double_t py, Double_t pz, Double_t e)
Double_t E() const
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:785
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
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
A TTree represents a columnar dataset.
Definition: TTree.h:71
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:8049
virtual Long64_t GetEntries() const
Definition: TTree.h:402
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5422
const Int_t n
Definition: legend1.C:16
TF1 * f1
Definition: legend1.C:11
LorentzVector< PxPyPzE4D< double > > XYZTVector
LorentzVector based on x,y,x,t (or px,py,pz,E) coordinates in double precision with metric (-,...
Definition: Vector4Dfwd.h:33
static constexpr double s
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:97
auto * t1
Definition: textangle.C:20