Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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///
6/// The dictionary for LorentzVector of float is not in the libMathCore, therefore
7/// is generated when parsed the file with CLING.
8///
9/// To run this macro you must do
10///
11/// ~~~{.cpp}
12/// root[0] .L mathcoreVectorFloatIO.C+
13/// root[1] runIt();
14/// ~~~
15///
16/// \macro_code
17///
18/// \author Lorenzo Moneta
19
20#include "TRandom.h"
21#include "TStopwatch.h"
22#include "TSystem.h"
23#include "TFile.h"
24#include "TTree.h"
25#include "TH1D.h"
26#include "TCanvas.h"
27
28#include <iostream>
29
30#include "TLorentzVector.h"
31
32#include "Math/Vector4D.h"
33
34
35// Now the dictionary contains the vector's with float types
36// No need to force dictionary generation
37// You need to run ACLIC with old ROOT version
38// and uncomment these lines below
39// #ifdef __MAKECINT__
40// #pragma link C++ class ROOT::Math::PxPyPzE4D<float>+;
41// #pragma link C++ class ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float> >+;
42// #pragma link C++ typedef ROOT::Math::XYZTVectorF;
43// #endif
44
45using namespace ROOT::Math;
46
47void write(int n) {
48
49 TRandom R;
51
52 TFile f1("mathcoreVectorIO_F.root","RECREATE");
53
54 // create tree
55 TTree t1("t1","Tree with new Float LorentzVector");
56
57 XYZTVectorF *v1 = new XYZTVectorF();
58 t1.Branch("LV branch","ROOT::Math::XYZTVectorF",&v1);
59
60 timer.Start();
61 for (int i = 0; i < n; ++i) {
62 double Px = R.Gaus(0,10);
63 double Py = R.Gaus(0,10);
64 double Pz = R.Gaus(0,10);
65 double E = R.Gaus(100,10);
66 v1->SetCoordinates(Px,Py,Pz,E);
67 t1.Fill();
68 }
69
70 f1.Write();
71 timer.Stop();
72 std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
73 t1.Print();
74}
75
76void read() {
77
78 TRandom R;
80
81 TFile f1("mathcoreVectorIO_F.root");
82
83 // create tree
84 TTree *t1 = (TTree*)f1.Get("t1");
85
86 XYZTVectorF *v1 = 0;
87 t1->SetBranchAddress("LV branch",&v1);
88
89 timer.Start();
90 int n = (int) t1->GetEntries();
91 std::cout << " Tree Entries " << n << std::endl;
92 double etot=0;
93 for (int i = 0; i < n; ++i) {
94 t1->GetEntry(i);
95 etot += v1->E();
96 }
97
98 timer.Stop();
99 std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
100 std::cout << " E average" << n<< " " << etot << " " << etot/double(n) << endl;
101}
102
103void runIt() {
104
105#if defined(__CINT__) && !defined(__MAKECINT__)
106 gSystem->Load("libMathCore");
107 gSystem->Load("libPhysics");
108 using namespace ROOT::Math ;
109
110 cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
111 cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
112 cout << "\t runIt()" << endl;
113#endif
114 int nEvents = 100000;
115 write(nEvents);
116 read();
117}
118
120#if defined(__CINT__) && !defined(__MAKECINT__)
121 gSystem->Load("libMathCore");
122 gSystem->Load("libPhysics");
123 using namespace ROOT::Math ;
124
125 cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
126 cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
127 cout << "\t runIt()" << endl;
128
129#endif
130}
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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:964
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Stopwatch class.
Definition TStopwatch.h:28
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1869
A TTree represents a columnar dataset.
Definition TTree.h:84
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