ROOT   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
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
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;
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;
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);
167}
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Definition: LorentzVector.h:59
void IgnoreTObjectStreamer(Bool_t ignore=kTRUE)
When the class kIgnoreTObjectStreamer bit is set, the automatically generated Streamer will not call ...
Definition: TClass.cxx:4830
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
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:875
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:79
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Definition: TTree.cxx:5612
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:8354
virtual Long64_t GetEntries() const
Definition: TTree.h:459
static TClass * Class()
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:46
static constexpr double s
constexpr Double_t E()
Base of natural log: .
Definition: TMath.h:93
auto * t1
Definition: textangle.C:20