Logo ROOT   6.07/09
Reference Guide
mathcoreVectorFloatIO.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// \notebook -nodraw
4 /// Macro illustrating automatic dictionary generation and 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 #ifdef __MAKECINT__
35 #pragma link C++ class ROOT::Math::PxPyPzE4D<float>+;
36 #pragma link C++ class ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<float> >+;
37 #pragma link C++ typedef ROOT::Math::XYZTVectorF;
38 #endif
39 
40 using namespace ROOT::Math;
41 
42 void write(int n) {
43 
44  TRandom R;
46 
47  TFile f1("mathcoreVectorIO_F.root","RECREATE");
48 
49  // create tree
50  TTree t1("t1","Tree with new Float LorentzVector");
51 
52  XYZTVectorF *v1 = new XYZTVectorF();
53  t1.Branch("LV branch","ROOT::Math::XYZTVectorF",&v1);
54 
55  timer.Start();
56  for (int i = 0; i < n; ++i) {
57  double Px = R.Gaus(0,10);
58  double Py = R.Gaus(0,10);
59  double Pz = R.Gaus(0,10);
60  double E = R.Gaus(100,10);
61  v1->SetCoordinates(Px,Py,Pz,E);
62  t1.Fill();
63  }
64 
65  f1.Write();
66  timer.Stop();
67  std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
68  t1.Print();
69 }
70 
71 void read() {
72 
73  TRandom R;
75 
76  TFile f1("mathcoreVectorIO_F.root");
77 
78  // create tree
79  TTree *t1 = (TTree*)f1.Get("t1");
80 
81  XYZTVectorF *v1 = 0;
82  t1->SetBranchAddress("LV branch",&v1);
83 
84  timer.Start();
85  int n = (int) t1->GetEntries();
86  std::cout << " Tree Entries " << n << std::endl;
87  double etot=0;
88  for (int i = 0; i < n; ++i) {
89  t1->GetEntry(i);
90  etot += v1->E();
91  }
92 
93  timer.Stop();
94  std::cout << " Time for new Float Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
95  std::cout << " E average" << n<< " " << etot << " " << etot/double(n) << endl;
96 }
97 
98 void runIt() {
99 
100 #if defined(__CINT__) && !defined(__MAKECINT__)
101  gSystem->Load("libMathCore");
102  gSystem->Load("libPhysics");
103  using namespace ROOT::Math ;
104 
105  cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
106  cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
107  cout << "\t runIt()" << endl;
108 #endif
109  int nEvents = 100000;
110  write(nEvents);
111  read();
112 }
113 
114 void mathcoreVectorFloatIO() {
115 #if defined(__CINT__) && !defined(__MAKECINT__)
116  gSystem->Load("libMathCore");
117  gSystem->Load("libPhysics");
118  using namespace ROOT::Math ;
119 
120  cout << "This tutorial can run only using ACliC, you must run it by doing: " << endl;
121  cout << "\t .L tutorials/math/mathcoreVectorFloatIO.C+" << endl;
122  cout << "\t runIt()" << endl;
123 
124 #endif
125 }
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:830
Scalar E() const
return 4-th component (time, or energy for a 4-momentum vector)
double read(const std::string &file_name)
reading
Class describing a generic LorentzVector in the 4D space-time, using the specified coordinate system ...
Definition: LorentzVector.h:54
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 write(int n, const std::string &file_name, const std::string &vector_type, int compress=0)
writing
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:235
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5210
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1818
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
TLatex * t1
Definition: textangle.C:20
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
virtual void Print(Option_t *option="") const
Dump this text with its attributes.
Definition: TText.cxx:780
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7719
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
const int nEvents
Definition: testRooFit.cxx:42
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
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
Double_t E()
Definition: TMath.h:54
TF1 * f1
Definition: legend1.C:11
virtual Long64_t GetEntries() const
Definition: TTree.h:392
A TTree object has a header with a name and a title.
Definition: TTree.h:98
const Int_t n
Definition: legend1.C:16
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
Stopwatch class.
Definition: TStopwatch.h:30