Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree121_hvector.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook
4/// Write and read STL vectors in a tree.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author The ROOT Team
10
11#include <vector>
12
13#include "TFile.h"
14#include "TTree.h"
15#include "TCanvas.h"
16#include "TFrame.h"
17#include "TH1F.h"
18#include "TBenchmark.h"
19#include "TRandom.h"
20#include "TSystem.h"
21
22void write_vector()
23{
24 auto f = TFile::Open("hvector.root","RECREATE");
25
26 if (!f)
27 return;
28
29 // Create one histograms
30 auto hpx = new TH1F("hpx","This is the px distribution", 100, -4, 4);
31 hpx->SetFillColor(48);
32
33 std::vector<float> vpx;
34 std::vector<float> vpy;
35 std::vector<float> vpz;
36 std::vector<float> vrand;
37
38 // Create a TTree
39 TTree *t = new TTree("tvec", "Tree with vectors");
40 t->Branch("vpx", &vpx);
41 t->Branch("vpy", &vpy);
42 t->Branch("vpz", &vpz);
43 t->Branch("vrand", &vrand);
44
45 // Create a new canvas.
46 auto c1 = new TCanvas("c1", "Dynamic Filling Example", 200, 10, 700, 500);
47
49 const Int_t kUPDATE = 1000;
50 for (Int_t i = 0; i < 25000; i++) {
51 Int_t npx = (Int_t)(gRandom->Rndm(1) * 15);
52
53 vpx.clear();
54 vpy.clear();
55 vpz.clear();
56 vrand.clear();
57
58 for (Int_t j = 0; j < npx; ++j) {
59
60 Float_t px,py,pz;
61 gRandom->Rannor(px, py);
62 pz = px * px + py * py;
64
65 hpx->Fill(px);
66
67 vpx.emplace_back(px);
68 vpy.emplace_back(py);
69 vpz.emplace_back(pz);
70 vrand.emplace_back(random);
71
72 }
73 if (i && (i%kUPDATE) == 0) {
74 if (i == kUPDATE)
75 hpx->Draw();
76 c1->Modified();
77 c1->Update();
79 break;
80 }
81 t->Fill();
82 }
83 f->Write();
84
85 delete f;
86}
87
88
89void read_vector()
90{
91 auto f = TFile::Open("hvector.root", "READ");
92
93 if (!f)
94 return;
95
96 auto t = f->Get<TTree>("tvec");
97
98 std::vector<float> *vpx = nullptr;
99
100 // Create a new canvas.
101 auto c1 = new TCanvas("c1", "Dynamic Filling Example", 200, 10, 700, 500);
102
103 const Int_t kUPDATE = 1000;
104
105 TBranch *bvpx = nullptr;
106 t->SetBranchAddress("vpx", &vpx, &bvpx);
107
108
109 // Create one histograms
110 auto h = new TH1F("h", "This is the px distribution", 100, -4, 4);
111 h->SetFillColor(48);
112
113 for (Int_t i = 0; i < 25000; i++) {
114
115 Long64_t tentry = t->LoadTree(i);
116 bvpx->GetEntry(tentry);
117
118 for (UInt_t j = 0; j < vpx->size(); ++j) {
119
120 h->Fill(vpx->at(j));
121
122 }
123 if (i && (i%kUPDATE) == 0) {
124 if (i == kUPDATE)
125 h->Draw();
126 c1->Modified();
127 c1->Update();
128 if (gSystem->ProcessEvents())
129 break;
130 }
131 }
132
133 // Since we passed the address of a local variable we need
134 // to remove it.
136}
137
138
139void tree121_hvector()
140{
141 gBenchmark->Start("hvector");
142 write_vector();
143 read_vector();
144 gBenchmark->Show("hvector");
145}
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:69
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
A TTree is a list of TBranches.
Definition TBranch.h:93
The Canvas class.
Definition TCanvas.h:23
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:4131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
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:507
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:416
A TTree represents a columnar dataset.
Definition TTree.h:84
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4610
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8486
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:365
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition TTree.cxx:6483
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition TTree.cxx:8162
return c1
Definition legend1.C:41