Logo ROOT  
Reference Guide
hvector.C File Reference

Detailed Description

View in nbviewer Open in SWAN Write and read STL vectors in a tree.

#include <vector>
#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TFrame.h"
#include "TH1F.h"
#include "TBenchmark.h"
#include "TRandom.h"
#include "TSystem.h"
void write()
{
TFile *f = TFile::Open("hvector.root","RECREATE");
if (!f) { return; }
// Create one histograms
TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
hpx->SetFillColor(48);
std::vector<float> vpx;
std::vector<float> vpy;
std::vector<float> vpz;
std::vector<float> vrand;
// Create a TTree
TTree *t = new TTree("tvec","Tree with vectors");
t->Branch("vpx",&vpx);
t->Branch("vpy",&vpy);
t->Branch("vpz",&vpz);
t->Branch("vrand",&vrand);
// Create a new canvas.
TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
const Int_t kUPDATE = 1000;
for (Int_t i = 0; i < 25000; i++) {
Int_t npx = (Int_t)(gRandom->Rndm(1)*15);
vpx.clear();
vpy.clear();
vpz.clear();
vrand.clear();
for (Int_t j = 0; j < npx; ++j) {
Float_t px,py,pz;
gRandom->Rannor(px,py);
pz = px*px + py*py;
Float_t random = gRandom->Rndm(1);
hpx->Fill(px);
vpx.emplace_back(px);
vpy.emplace_back(py);
vpz.emplace_back(pz);
vrand.emplace_back(random);
}
if (i && (i%kUPDATE) == 0) {
if (i == kUPDATE) hpx->Draw();
c1->Modified();
c1->Update();
break;
}
t->Fill();
}
f->Write();
delete f;
}
void read()
{
TFile *f = TFile::Open("hvector.root","READ");
if (!f) { return; }
TTree *t; f->GetObject("tvec",t);
std::vector<float> *vpx = 0;
// Create a new canvas.
TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
const Int_t kUPDATE = 1000;
TBranch *bvpx = 0;
t->SetBranchAddress("vpx",&vpx,&bvpx);
// Create one histograms
TH1F *h = new TH1F("h","This is the px distribution",100,-4,4);
h->SetFillColor(48);
for (Int_t i = 0; i < 25000; i++) {
Long64_t tentry = t->LoadTree(i);
bvpx->GetEntry(tentry);
for (UInt_t j = 0; j < vpx->size(); ++j) {
h->Fill(vpx->at(j));
}
if (i && (i%kUPDATE) == 0) {
if (i == kUPDATE) h->Draw();
c1->Modified();
c1->Update();
break;
}
}
// Since we passed the address of a local variable we need
// to remove it.
}
void hvector()
{
gBenchmark->Start("hvector");
write();
read();
gBenchmark->Show("hvector");
}
Author
The ROOT Team

Definition in file hvector.C.

TH1F.h
f
#define f(i)
Definition: RSha256.hxx:122
TBenchmark::Start
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TTree
Definition: TTree.h:79
TTree::SetBranchAddress
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:8205
TRandom.h
Float_t
float Float_t
Definition: RtypesCore.h:57
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3946
Int_t
int Int_t
Definition: RtypesCore.h:45
TTree::Fill
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4527
TCanvas.h
TTree.h
TFile.h
TRandom::Rannor
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:489
TBranch
Definition: TBranch.h:89
TSystem.h
h
#define h(i)
Definition: RSha256.hxx:124
TTree::Branch
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition: TTree.h:349
TTree::LoadTree
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6366
gBenchmark
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
TBranch::GetEntry
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1582
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TRandom::SetSeed
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:597
TBenchmark.h
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TRandom::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:541
TFile
Definition: TFile.h:54
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TCanvas
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
TFrame.h
kUPDATE
@ kUPDATE
Definition: TFitParametersDialog.cxx:51
TTree::ResetBranchAddresses
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7937
TBenchmark::Show
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
TSystem::ProcessEvents
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:417
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
c1
return c1
Definition: legend1.C:41