Logo ROOT  
Reference Guide
mathcoreVectorFloatIO.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook -nodraw
4/// Macro illustrating I/O with Lorentz Vectors of floats
5/// The dictionary for LorentzVector of float is not in the libMathCore, therefore
6/// is generated when parsed the file with CINT.
7///
8/// To run this macro you must do
9///
10/// ~~~{.cpp}
11/// root[0] .L mathcoreVectorFloatIO.C+
12/// root[1] runIt();
13/// ~~~
14///
15/// \macro_code
16///
17/// \author Lorenzo Moneta
18
19#include "TRandom.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
34// Now the dictionary contains the vector's with float types
35// No need to force dictionary generation
36// You need to run ACLIC with old ROOT version
37// and uncomment these lines below
38// #ifdef __MAKECINT__
39// #pragma link C++ class ROOT::Math::PxPyPzE4D<float>+;
40// #pragma link C++ class ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float> >+;
41// #pragma link C++ typedef ROOT::Math::XYZTVectorF;
42// #endif
43
44using namespace ROOT::Math;
45
46void write(int n) {
47
48 TRandom R;
49 TStopwatch timer;
50
51 TFile f1("mathcoreVectorIO_F.root","RECREATE");
52
53 // create tree
54 TTree t1("t1","Tree with new Float LorentzVector");
55
56 XYZTVectorF *v1 = new XYZTVectorF();
57 t1.Branch("LV branch","ROOT::Math::XYZTVectorF",&v1);
58
59 timer.Start();
60 for (int i = 0; i < n; ++i) {
61 double Px = R.Gaus(0,10);
62 double Py = R.Gaus(0,10);
63 double Pz = R.Gaus(0,10);
64 double E = R.Gaus(100,10);
65 v1->SetCoordinates(Px,Py,Pz,E);
66 t1.Fill();
67 }
68
69 f1.Write();
70 timer.Stop();
71 std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
72 t1.Print();
73}
74
75void read() {
76
77 TRandom R;
78 TStopwatch timer;
79
80 TFile f1("mathcoreVectorIO_F.root");
81
82 // create tree
83 TTree *t1 = (TTree*)f1.Get("t1");
84
85 XYZTVectorF *v1 = 0;
86 t1->SetBranchAddress("LV branch",&v1);
87
88 timer.Start();
89 int n = (int) t1->GetEntries();
90 std::cout << " Tree Entries " << n << std::endl;
91 double etot=0;
92 for (int i = 0; i < n; ++i) {
93 t1->GetEntry(i);
94 etot += v1->E();
95 }
96
97 timer.Stop();
98 std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
99 std::cout << " E average" << n<< " " << etot << " " << etot/double(n) << endl;
100}
101
102void runIt() {
103
104#if defined(__CINT__) && !defined(__MAKECINT__)
105 gSystem->Load("libMathCore");
106 gSystem->Load("libPhysics");
107 using namespace ROOT::Math ;
108
109 cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
110 cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
111 cout << "\t runIt()" << endl;
112#endif
113 int nEvents = 100000;
114 write(nEvents);
115 read();
116}
117
118void mathcoreVectorFloatIO() {
119#if defined(__CINT__) && !defined(__MAKECINT__)
120 gSystem->Load("libMathCore");
121 gSystem->Load("libPhysics");
122 using namespace ROOT::Math ;
123
124 cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
125 cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
126 cout << "\t runIt()" << endl;
127
128#endif
129}
double
Definition: Converters.cxx:921
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
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:796
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
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1850
A TTree represents a columnar dataset.
Definition: TTree.h:78
const Int_t n
Definition: legend1.C:16
TF1 * f1
Definition: legend1.C:11
LorentzVector< PxPyPzE4D< float > > XYZTVectorF
LorentzVector based on x,y,x,t (or px,py,pz,E) coordinates in float precision with metric (-,...
Definition: Vector4Dfwd.h:50
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:97
auto * t1
Definition: textangle.C:20