Logo ROOT   6.12/07
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 
33 using namespace ROOT::Math;
34 
35 void 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 
109 void 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 
162 void mathcoreVectorIO() {
163 
164  int nEvents = 100000;
165  write(nEvents);
166  read();
167 }
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
void SetPxPyPzE(Double_t px, Double_t py, Double_t pz, Double_t e)
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Definition: LorentzVector.h:48
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
Scalar Px() const
spatial X component
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:256
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:27
virtual void SetSeed(ULong_t seed=0)
Set the generator seed.
Definition: TRandom2.cxx:120
LorentzVector< CoordSystem > & SetCoordinates(const Scalar src[])
Set internal data based on an array of 4 Scalar numbers.
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
Double_t Pz() const
void Class()
Definition: Class.C:29
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
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
Scalar E() const
return 4-th component (time, or energy for a 4-momentum vector)
TLorentzVector is a general four-vector class, which can be used either for the description of positi...
Scalar Py() const
spatial Y component
constexpr Double_t E()
Definition: TMath.h:74
auto * t1
Definition: textangle.C:20
Double_t Py() const
static constexpr double s
Double_t Px() const
TF1 * f1
Definition: legend1.C:11
Scalar Pz() const
spatial Z component
Double_t E() const
constexpr Double_t R()
Definition: TMath.h:213
const Int_t n
Definition: legend1.C:16
Stopwatch class.
Definition: TStopwatch.h:28