Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mathcoreVectorCollection.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_math
3/// \notebook -js
4/// Example showing how to write and read a std vector of ROOT::Math LorentzVector in a ROOT tree.
5///
6/// In the write() function a variable number of track Vectors is generated
7/// according to a Poisson distribution with random momentum uniformly distributed
8/// in phi and eta.
9/// In the read() the vectors are read back and the content analysed and
10/// some information such as number of tracks per event or the track pt
11/// distributions are displayed in a canvas.
12///
13/// To execute the macro type in:
14///
15/// ~~~{.cpp}
16/// root[0]: .x mathcoreVectorCollection.C
17/// ~~~
18///
19/// \macro_image
20/// \macro_output
21/// \macro_code
22///
23/// \author Andras Zsenei
24
25#include "TRandom.h"
26#include "TStopwatch.h"
27#include "TSystem.h"
28#include "TFile.h"
29#include "TTree.h"
30#include "TH1D.h"
31#include "TCanvas.h"
32#include "TMath.h"
33
34#include <iostream>
35
36// CLING does not understand some files included by LorentzVector
37#include "Math/Vector3D.h"
38#include "Math/Vector4D.h"
39
40using namespace ROOT::Math;
41
42double write(int n) {
43 TRandom R;
45
46 TFile f1("mathcoreLV.root","RECREATE");
47
48 // create tree
49 TTree t1("t1","Tree with new LorentzVector");
50
51 std::vector<ROOT::Math::XYZTVector> tracks;
52 std::vector<ROOT::Math::XYZTVector> * pTracks = &tracks;
53 t1.Branch("tracks","std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > >",&pTracks);
54
55 double M = 0.13957; // set pi+ mass
56
57 timer.Start();
58 double sum = 0;
59 for (int i = 0; i < n; ++i) {
60 int nPart = R.Poisson(5);
61 pTracks->clear();
62 pTracks->reserve(nPart);
63 for (int j = 0; j < nPart; ++j) {
64 double px = R.Gaus(0,10);
65 double py = R.Gaus(0,10);
66 double pt = sqrt(px*px +py*py);
67 double eta = R.Uniform(-3,3);
68 double phi = R.Uniform(0.0 , 2*TMath::Pi() );
69 RhoEtaPhiVector vcyl( pt, eta, phi);
70 // set energy
71 double E = sqrt( vcyl.R()*vcyl.R() + M*M);
72 XYZTVector q( vcyl.X(), vcyl.Y(), vcyl.Z(), E);
73 // fill track vector
74 pTracks->push_back(q);
75 // evaluate sum of components to check
76 sum += q.x()+q.y()+q.z()+q.t();
77 }
78 t1.Fill();
79 }
80
81 f1.Write();
82 timer.Stop();
83 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
84
85 t1.Print();
86 return sum;
87}
88
89double read() {
90 TRandom R;
92
93 TH1D * h1 = new TH1D("h1","total event energy ",100,0,1000.);
96 TH1D * h4 = new TH1D("h4","Track Pt",100,0,100);
97 TH1D * h5 = new TH1D("h5","Track Eta",100,-5,5);
98 TH1D * h6 = new TH1D("h6","Track Cos(theta)",100,-1,1);
99
100 TFile f1("mathcoreLV.root");
101
102 // create tree
103 TTree *t1 = (TTree*)f1.Get("t1");
104
105 std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > > * pTracks = nullptr;
106 t1->SetBranchAddress("tracks",&pTracks);
107
108 timer.Start();
109 int n = (int) t1->GetEntries();
110 std::cout << " Tree Entries " << n << std::endl;
111 double sum=0;
112 for (int i = 0; i < n; ++i) {
113 t1->GetEntry(i);
114 int ntrk = pTracks->size();
115 h3->Fill(ntrk);
117 for (int j = 0; j < ntrk; ++j) {
118 XYZTVector v = (*pTracks)[j];
119 q += v;
120 h3->Fill(v.E());
121 h4->Fill(v.Pt());
122 h5->Fill(v.Eta());
123 h6->Fill(cos(v.Theta()));
124 sum += v.x() + v.y() + v.z() + v.t();
125 }
126 h1->Fill(q.E() );
127 h2->Fill(ntrk);
128 }
129
130 timer.Stop();
131 std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
132
133 TCanvas *c1 = new TCanvas("c1","demo of Trees",10,10,600,800);
134 c1->Divide(2,3);
135
136 c1->cd(1);
137 h1->Draw();
138 c1->cd(2);
139 h2->Draw();
140 c1->cd(3);
141 h3->Draw();
142 c1->cd(3);
143 h3->Draw();
144 c1->cd(4);
145 h4->Draw();
146 c1->cd(5);
147 h5->Draw();
148 c1->cd(6);
149 h6->Draw();
150
151 return sum;
152}
153
155
156 int nEvents = 10000;
157 double s1 = write(nEvents);
158 double s2 = read();
159
160 if (fabs(s1-s2) > s1*1.E-15 ) {
161 std::cout << "ERROR: Found difference in Vector when reading ( " << s1 << " != " << s2 << " diff = " << fabs(s1-s2) << " ) " << std::endl;
162 return -1;
163 }
164 return 0;
165}
166
167int main() {
169}
170
int main()
Definition Prototype.cxx:12
#define s1(x)
Definition RSha256.hxx:91
#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.
float * q
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:698
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3038
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
A TTree represents a columnar dataset.
Definition TTree.h:84
TPaveText * pt
return c1
Definition legend1.C:41
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
constexpr Double_t E()
Base of natural log: .
Definition TMath.h:94
constexpr Double_t Pi()
Definition TMath.h:38
auto * t1
Definition textangle.C:20
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345
void tracks()
Definition tracks.C:48