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 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
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}
#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 ...
void IgnoreTObjectStreamer(Bool_t ignore=kTRUE)
When the class kIgnoreTObjectStreamer bit is set, the automatically generated Streamer will not call ...
Definition TClass.cxx:4841
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
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:880
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...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void Stop()
Stop the stopwatch.
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition TTree.cxx:5638
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8380
virtual Long64_t GetEntries() const
Definition TTree.h:463
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:93
auto * t1
Definition textangle.C:20