ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mathcoreVectorCollection.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Example showing how to write and read a std vector of ROOT::Math LorentzVector in a ROOT tree.
4 /// In the write() function a variable number of track Vectors is generated
5 /// according to a Poisson distribution with random momentum uniformly distributed
6 /// in phi and eta.
7 /// In the read() the vectors are read back and the content analysed and
8 /// some information such as number of tracks per event or the track pt
9 /// distributions are displayed in a canvas.
10 ///
11 /// To execute the macro type in:
12 ///
13 /// ~~~ {.cpp}
14 /// root[0]: .x mathcoreVectorCollection.C
15 /// ~~~
16 ///
17 /// \macro_image
18 /// \macro_output
19 /// \macro_code
20 ///
21 /// \author Andras Zsenei
22 
23 #include "TRandom.h"
24 #include "TStopwatch.h"
25 #include "TSystem.h"
26 #include "TFile.h"
27 #include "TTree.h"
28 #include "TH1D.h"
29 #include "TCanvas.h"
30 #include "TMath.h"
31 
32 #include <iostream>
33 
34 // CINT does not understand some files included by LorentzVector
35 #include "Math/Vector3D.h"
36 #include "Math/Vector4D.h"
37 
38 using namespace ROOT::Math;
39 
40 double write(int n) {
41  TRandom R;
43 
44  TFile f1("mathcoreLV.root","RECREATE");
45 
46  // create tree
47  TTree t1("t1","Tree with new LorentzVector");
48 
49  std::vector<ROOT::Math::XYZTVector> tracks;
50  std::vector<ROOT::Math::XYZTVector> * pTracks = &tracks;
51  t1.Branch("tracks","std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > >",&pTracks);
52 
53  double M = 0.13957; // set pi+ mass
54 
55  timer.Start();
56  double sum = 0;
57  for (int i = 0; i < n; ++i) {
58  int nPart = R.Poisson(5);
59  pTracks->clear();
60  pTracks->reserve(nPart);
61  for (int j = 0; j < nPart; ++j) {
62  double px = R.Gaus(0,10);
63  double py = R.Gaus(0,10);
64  double pt = sqrt(px*px +py*py);
65  double eta = R.Uniform(-3,3);
66  double phi = R.Uniform(0.0 , 2*TMath::Pi() );
67  RhoEtaPhiVector vcyl( pt, eta, phi);
68  // set energy
69  double E = sqrt( vcyl.R()*vcyl.R() + M*M);
70  XYZTVector q( vcyl.X(), vcyl.Y(), vcyl.Z(), E);
71  // fill track vector
72  pTracks->push_back(q);
73  // evaluate sum of components to check
74  sum += q.x()+q.y()+q.z()+q.t();
75  }
76  t1.Fill();
77  }
78 
79  f1.Write();
80  timer.Stop();
81  std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
82 
83  t1.Print();
84  return sum;
85 }
86 
87  double read() {
88  TRandom R;
90 
91  TH1D * h1 = new TH1D("h1","total event energy ",100,0,1000.);
92  TH1D * h2 = new TH1D("h2","Number of track per event",21,-0.5,20.5);
93  TH1D * h3 = new TH1D("h3","Track Energy",100,0,200);
94  TH1D * h4 = new TH1D("h4","Track Pt",100,0,100);
95  TH1D * h5 = new TH1D("h5","Track Eta",100,-5,5);
96  TH1D * h6 = new TH1D("h6","Track Cos(theta)",100,-1,1);
97 
98  TFile f1("mathcoreLV.root");
99 
100  // create tree
101  TTree *t1 = (TTree*)f1.Get("t1");
102 
103  std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > > * pTracks = 0;
104  t1->SetBranchAddress("tracks",&pTracks);
105 
106  timer.Start();
107  int n = (int) t1->GetEntries();
108  std::cout << " Tree Entries " << n << std::endl;
109  double sum=0;
110  for (int i = 0; i < n; ++i) {
111  t1->GetEntry(i);
112  int ntrk = pTracks->size();
113  h3->Fill(ntrk);
114  XYZTVector q;
115  for (int j = 0; j < ntrk; ++j) {
116  XYZTVector v = (*pTracks)[j];
117  q += v;
118  h3->Fill(v.E());
119  h4->Fill(v.Pt());
120  h5->Fill(v.Eta());
121  h6->Fill(cos(v.Theta()));
122  sum += v.x() + v.y() + v.z() + v.t();
123  }
124  h1->Fill(q.E() );
125  h2->Fill(ntrk);
126  }
127 
128  timer.Stop();
129  std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
130 
131  TCanvas *c1 = new TCanvas("c1","demo of Trees",10,10,600,800);
132  c1->Divide(2,3);
133 
134  c1->cd(1);
135  h1->Draw();
136  c1->cd(2);
137  h2->Draw();
138  c1->cd(3);
139  h3->Draw();
140  c1->cd(3);
141  h3->Draw();
142  c1->cd(4);
143  h4->Draw();
144  c1->cd(5);
145  h5->Draw();
146  c1->cd(6);
147  h6->Draw();
148 
149  return sum;
150 }
151 
152 int mathcoreVectorCollection() {
153 
154  #if defined(__CINT__) && !defined(__MAKECINT__)
155 
156  gSystem->Load("libMathCore");
157  gSystem->Load("libPhysics");
158  // in CINT need to do that after having loading the library
159  using namespace ROOT::Math;
160 
161  cout << "This tutorial can run only using ACliC, compiling it by doing: " << endl;
162  cout << "\t .x tutorials/math/mathcoreVectorCollection.C+" << endl;
163  return 0;
164 #else
165  int nEvents = 10000;
166  double s1 = write(nEvents);
167  double s2 = read();
168 
169  if (fabs(s1-s2) > s1*1.E-15 ) {
170  std::cout << "ERROR: Found difference in Vector when reading ( " << s1 << " != " << s2 << " diff = " << fabs(s1-s2) << " ) " << std::endl;
171  return -1;
172  }
173  return 0;
174 #endif
175 }
176 
177 #ifndef __CINT__
178 int main() {
179  return mathcoreVectorCollection();
180 }
181 #endif
182 
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
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
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
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
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:5144
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
TLatex * t1
Definition: textangle.C:20
double cos(double)
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
TSocket * s1
Definition: hserv2.C:36
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:6495
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
Scalar Eta() const
pseudorapidity
const int nEvents
Definition: testRooFit.cxx:42
Class describing a generic displacement vector in 3 dimensions.
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
SVector< double, 2 > v
Definition: Dict.h:5
tuple main
Definition: hsum.py:20
Scalar Pt() const
return the transverse spatial component sqrt ( X**2 + Y**2 )
Double_t E()
Definition: TMath.h:54
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
Double_t Pi()
Definition: TMath.h:44
TH1F * s2
Definition: threadsh2.C:15
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1623
TF1 * f1
Definition: legend1.C:11
Scalar Theta() const
polar Angle
virtual Long64_t GetEntries() const
Definition: TTree.h:386
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual Int_t Poisson(Double_t mean)
Generates a random integer N according to a Poisson law.
Definition: TRandom.cxx:362
float * q
Definition: THbookFile.cxx:87
const Int_t n
Definition: legend1.C:16
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
Stopwatch class.
Definition: TStopwatch.h:30