mathcoreVectorCollection.C: Example showing how to write and read a std vector of ROOT::Math LorentzVector in a ROOT tree.
//
// Example showing how to write and read a std vector of ROOT::Math LorentzVector in a ROOT tree.
// In the write() function a variable number of track Vectors is generated
// according to a Poisson distribution with random momentum uniformly distributed
// in phi and eta.
// In the read() the vectors are read back and the content analyzed and
// some information such as number of tracks per event or the track pt
// distributions are displayed in a canvas.
//
// To execute the macro type in:
//
// root[0]: .x mathcoreVectorCollection.C
//Author: Andras Zsenei
#include "TRandom.h"
#include "TStopwatch.h"
#include "TSystem.h"
#include "TFile.h"
#include "TTree.h"
#include "TH1D.h"
#include "TCanvas.h"
#include "TMath.h"
#include <iostream>
// CINT does not understand some files included by LorentzVector
#include "Math/Vector3D.h"
#include "Math/Vector4D.h"
using namespace ROOT::Math;
double write(int n) {
TRandom R;
TStopwatch timer;
TFile f1("mathcoreLV.root","RECREATE");
// create tree
TTree t1("t1","Tree with new LorentzVector");
std::vector<ROOT::Math::XYZTVector> tracks;
std::vector<ROOT::Math::XYZTVector> * pTracks = &tracks;
t1.Branch("tracks","std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > >",&pTracks);
double M = 0.13957; // set pi+ mass
timer.Start();
double sum = 0;
for (int i = 0; i < n; ++i) {
int nPart = R.Poisson(5);
pTracks->clear();
pTracks->reserve(nPart);
for (int j = 0; j < nPart; ++j) {
double px = R.Gaus(0,10);
double py = R.Gaus(0,10);
double pt = sqrt(px*px +py*py);
double eta = R.Uniform(-3,3);
double phi = R.Uniform(0.0 , 2*TMath::Pi() );
RhoEtaPhiVector vcyl( pt, eta, phi);
// set energy
double E = sqrt( vcyl.R()*vcyl.R() + M*M);
XYZTVector q( vcyl.X(), vcyl.Y(), vcyl.Z(), E);
// fill track vector
pTracks->push_back(q);
// evaluate sum of components to check
sum += q.x()+q.y()+q.z()+q.t();
}
t1.Fill();
}
f1.Write();
timer.Stop();
std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
t1.Print();
return sum;
}
double read() {
TRandom R;
TStopwatch timer;
TH1D * h1 = new TH1D("h1","total event energy ",100,0,1000.);
TH1D * h2 = new TH1D("h2","Number of track per event",21,-0.5,20.5);
TH1D * h3 = new TH1D("h3","Track Energy",100,0,200);
TH1D * h4 = new TH1D("h4","Track Pt",100,0,100);
TH1D * h5 = new TH1D("h5","Track Eta",100,-5,5);
TH1D * h6 = new TH1D("h6","Track Cos(theta)",100,-1,1);
TFile f1("mathcoreLV.root");
// create tree
TTree *t1 = (TTree*)f1.Get("t1");
std::vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > > * pTracks = 0;
t1->SetBranchAddress("tracks",&pTracks);
timer.Start();
int n = (int) t1->GetEntries();
std::cout << " Tree Entries " << n << std::endl;
double sum=0;
for (int i = 0; i < n; ++i) {
t1->GetEntry(i);
int ntrk = pTracks->size();
h3->Fill(ntrk);
XYZTVector q;
for (int j = 0; j < ntrk; ++j) {
XYZTVector v = (*pTracks)[j];
q += v;
h3->Fill(v.E());
h4->Fill(v.Pt());
h5->Fill(v.Eta());
h6->Fill(cos(v.Theta()));
sum += v.x() + v.y() + v.z() + v.t();
}
h1->Fill(q.E() );
h2->Fill(ntrk);
}
timer.Stop();
std::cout << " Time for new Vector " << timer.RealTime() << " " << timer.CpuTime() << std::endl;
TCanvas *c1 = new TCanvas("c1","demo of Trees",10,10,600,800);
c1->Divide(2,3);
c1->cd(1);
h1->Draw();
c1->cd(2);
h2->Draw();
c1->cd(3);
h3->Draw();
c1->cd(3);
h3->Draw();
c1->cd(4);
h4->Draw();
c1->cd(5);
h5->Draw();
c1->cd(6);
h6->Draw();
return sum;
}
int mathcoreVectorCollection() {
#if defined(__CINT__) && !defined(__MAKECINT__)
gSystem->Load("libMathCore");
gSystem->Load("libPhysics");
// in CINT need to do that after having loading the library
using namespace ROOT::Math;
cout << "This tutorial can run only using ACliC, compiling it by doing: " << endl;
cout << "\t .x tutorials/math/mathcoreVectorCollection.C+" << endl;
//gROOT->ProcessLine(".x tutorials/math/mathcoreVectorCollection.C+");
return 0;
#else
int nEvents = 10000;
double s1 = write(nEvents);
double s2 = read();
if (fabs(s1-s2) > s1*1.E-15 ) {
std::cout << "ERROR: Found difference in Vector when reading ( " << s1 << " != " << s2 << " diff = " << fabs(s1-s2) << " ) " << std::endl;
return -1;
}
return 0;
#endif
}
#ifndef __CINT__
int main() {
return mathcoreVectorCollection();
}
#endif
mathcoreVectorCollection.C:1 mathcoreVectorCollection.C:2 mathcoreVectorCollection.C:3 mathcoreVectorCollection.C:4 mathcoreVectorCollection.C:5 mathcoreVectorCollection.C:6 mathcoreVectorCollection.C:7 mathcoreVectorCollection.C:8 mathcoreVectorCollection.C:9 mathcoreVectorCollection.C:10 mathcoreVectorCollection.C:11 mathcoreVectorCollection.C:12 mathcoreVectorCollection.C:13 mathcoreVectorCollection.C:14 mathcoreVectorCollection.C:15 mathcoreVectorCollection.C:16 mathcoreVectorCollection.C:17 mathcoreVectorCollection.C:18 mathcoreVectorCollection.C:19 mathcoreVectorCollection.C:20 mathcoreVectorCollection.C:21 mathcoreVectorCollection.C:22 mathcoreVectorCollection.C:23 mathcoreVectorCollection.C:24 mathcoreVectorCollection.C:25 mathcoreVectorCollection.C:26 mathcoreVectorCollection.C:27 mathcoreVectorCollection.C:28 mathcoreVectorCollection.C:29 mathcoreVectorCollection.C:30 mathcoreVectorCollection.C:31 mathcoreVectorCollection.C:32 mathcoreVectorCollection.C:33 mathcoreVectorCollection.C:34 mathcoreVectorCollection.C:35 mathcoreVectorCollection.C:36 mathcoreVectorCollection.C:37 mathcoreVectorCollection.C:38 mathcoreVectorCollection.C:39 mathcoreVectorCollection.C:40 mathcoreVectorCollection.C:41 mathcoreVectorCollection.C:42 mathcoreVectorCollection.C:43 mathcoreVectorCollection.C:44 mathcoreVectorCollection.C:45 mathcoreVectorCollection.C:46 mathcoreVectorCollection.C:47 mathcoreVectorCollection.C:48 mathcoreVectorCollection.C:49 mathcoreVectorCollection.C:50 mathcoreVectorCollection.C:51 mathcoreVectorCollection.C:52 mathcoreVectorCollection.C:53 mathcoreVectorCollection.C:54 mathcoreVectorCollection.C:55 mathcoreVectorCollection.C:56 mathcoreVectorCollection.C:57 mathcoreVectorCollection.C:58 mathcoreVectorCollection.C:59 mathcoreVectorCollection.C:60 mathcoreVectorCollection.C:61 mathcoreVectorCollection.C:62 mathcoreVectorCollection.C:63 mathcoreVectorCollection.C:64 mathcoreVectorCollection.C:65 mathcoreVectorCollection.C:66 mathcoreVectorCollection.C:67 mathcoreVectorCollection.C:68 mathcoreVectorCollection.C:69 mathcoreVectorCollection.C:70 mathcoreVectorCollection.C:71 mathcoreVectorCollection.C:72 mathcoreVectorCollection.C:73 mathcoreVectorCollection.C:74 mathcoreVectorCollection.C:75 mathcoreVectorCollection.C:76 mathcoreVectorCollection.C:77 mathcoreVectorCollection.C:78 mathcoreVectorCollection.C:79 mathcoreVectorCollection.C:80 mathcoreVectorCollection.C:81 mathcoreVectorCollection.C:82 mathcoreVectorCollection.C:83 mathcoreVectorCollection.C:84 mathcoreVectorCollection.C:85 mathcoreVectorCollection.C:86 mathcoreVectorCollection.C:87 mathcoreVectorCollection.C:88 mathcoreVectorCollection.C:89 mathcoreVectorCollection.C:90 mathcoreVectorCollection.C:91 mathcoreVectorCollection.C:92 mathcoreVectorCollection.C:93 mathcoreVectorCollection.C:94 mathcoreVectorCollection.C:95 mathcoreVectorCollection.C:96 mathcoreVectorCollection.C:97 mathcoreVectorCollection.C:98 mathcoreVectorCollection.C:99 mathcoreVectorCollection.C:100 mathcoreVectorCollection.C:101 mathcoreVectorCollection.C:102 mathcoreVectorCollection.C:103 mathcoreVectorCollection.C:104 mathcoreVectorCollection.C:105 mathcoreVectorCollection.C:106 mathcoreVectorCollection.C:107 mathcoreVectorCollection.C:108 mathcoreVectorCollection.C:109 mathcoreVectorCollection.C:110 mathcoreVectorCollection.C:111 mathcoreVectorCollection.C:112 mathcoreVectorCollection.C:113 mathcoreVectorCollection.C:114 mathcoreVectorCollection.C:115 mathcoreVectorCollection.C:116 mathcoreVectorCollection.C:117 mathcoreVectorCollection.C:118 mathcoreVectorCollection.C:119 mathcoreVectorCollection.C:120 mathcoreVectorCollection.C:121 mathcoreVectorCollection.C:122 mathcoreVectorCollection.C:123 mathcoreVectorCollection.C:124 mathcoreVectorCollection.C:125 mathcoreVectorCollection.C:126 mathcoreVectorCollection.C:127 mathcoreVectorCollection.C:128 mathcoreVectorCollection.C:129 mathcoreVectorCollection.C:130 mathcoreVectorCollection.C:131 mathcoreVectorCollection.C:132 mathcoreVectorCollection.C:133 mathcoreVectorCollection.C:134 mathcoreVectorCollection.C:135 mathcoreVectorCollection.C:136 mathcoreVectorCollection.C:137 mathcoreVectorCollection.C:138 mathcoreVectorCollection.C:139 mathcoreVectorCollection.C:140 mathcoreVectorCollection.C:141 mathcoreVectorCollection.C:142 mathcoreVectorCollection.C:143 mathcoreVectorCollection.C:144 mathcoreVectorCollection.C:145 mathcoreVectorCollection.C:146 mathcoreVectorCollection.C:147 mathcoreVectorCollection.C:148 mathcoreVectorCollection.C:149 mathcoreVectorCollection.C:150 mathcoreVectorCollection.C:151 mathcoreVectorCollection.C:152 mathcoreVectorCollection.C:153 mathcoreVectorCollection.C:154 mathcoreVectorCollection.C:155 mathcoreVectorCollection.C:156 mathcoreVectorCollection.C:157 mathcoreVectorCollection.C:158 mathcoreVectorCollection.C:159 mathcoreVectorCollection.C:160 mathcoreVectorCollection.C:161 mathcoreVectorCollection.C:162 mathcoreVectorCollection.C:163 mathcoreVectorCollection.C:164 mathcoreVectorCollection.C:165 mathcoreVectorCollection.C:166 mathcoreVectorCollection.C:167 mathcoreVectorCollection.C:168 mathcoreVectorCollection.C:169 mathcoreVectorCollection.C:170 mathcoreVectorCollection.C:171 mathcoreVectorCollection.C:172 mathcoreVectorCollection.C:173 mathcoreVectorCollection.C:174 mathcoreVectorCollection.C:175 mathcoreVectorCollection.C:176 mathcoreVectorCollection.C:177 mathcoreVectorCollection.C:178 mathcoreVectorCollection.C:179 mathcoreVectorCollection.C:180 mathcoreVectorCollection.C:181 mathcoreVectorCollection.C:182 mathcoreVectorCollection.C:183 mathcoreVectorCollection.C:184 mathcoreVectorCollection.C:185 mathcoreVectorCollection.C:186 mathcoreVectorCollection.C:187 mathcoreVectorCollection.C:188 mathcoreVectorCollection.C:189 mathcoreVectorCollection.C:190 mathcoreVectorCollection.C:191 mathcoreVectorCollection.C:192 mathcoreVectorCollection.C:193 mathcoreVectorCollection.C:194 mathcoreVectorCollection.C:195 mathcoreVectorCollection.C:196 mathcoreVectorCollection.C:197 mathcoreVectorCollection.C:198 mathcoreVectorCollection.C:199 mathcoreVectorCollection.C:200 mathcoreVectorCollection.C:201 mathcoreVectorCollection.C:202 mathcoreVectorCollection.C:203