Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
44 using namespace ROOT::Math;
45 
46 void 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 
75 void 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 
102 void 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 
118 void 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 }