Logo ROOT   6.14/05
Reference Guide
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 
22 void write()
23 {
24 
25  TFile *f = TFile::Open("hvector.root","RECREATE");
26 
27  if (!f) { return; }
28 
29  // Create one histograms
30  TH1F *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  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
47 
48  gRandom->SetSeed();
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;
63  Float_t random = gRandom->Rndm(1);
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) hpx->Draw();
75  c1->Modified();
76  c1->Update();
77  if (gSystem->ProcessEvents())
78  break;
79  }
80  t->Fill();
81  }
82  f->Write();
83 
84  delete f;
85 }
86 
87 
88 void read()
89 {
90 
91  TFile *f = TFile::Open("hvector.root","READ");
92 
93  if (!f) { return; }
94 
95  TTree *t; f->GetObject("tvec",t);
96 
97  std::vector<float> *vpx = 0;
98 
99  // Create a new canvas.
100  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
101 
102  const Int_t kUPDATE = 1000;
103 
104  TBranch *bvpx = 0;
105  t->SetBranchAddress("vpx",&vpx,&bvpx);
106 
107 
108  // Create one histograms
109  TH1F *h = new TH1F("h","This is the px distribution",100,-4,4);
110  h->SetFillColor(48);
111 
112  for (Int_t i = 0; i < 25000; i++) {
113 
114  Long64_t tentry = t->LoadTree(i);
115  bvpx->GetEntry(tentry);
116 
117  for (UInt_t j = 0; j < vpx->size(); ++j) {
118 
119  h->Fill(vpx->at(j));
120 
121  }
122  if (i && (i%kUPDATE) == 0) {
123  if (i == kUPDATE) h->Draw();
124  c1->Modified();
125  c1->Update();
126  if (gSystem->ProcessEvents())
127  break;
128  }
129  }
130 
131  // Since we passed the address of a local variable we need
132  // to remove it.
134 }
135 
136 
137 void hvector()
138 {
139  gBenchmark->Start("hvector");
140 
141  write();
142  read();
143 
144  gBenchmark->Show("hvector");
145 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:424
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:481
long long Long64_t
Definition: RtypesCore.h:69
float Float_t
Definition: RtypesCore.h:53
return c1
Definition: legend1.C:41
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4374
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:285
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:41
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7982
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:589
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:6190
void GetObject(const char *namecycle, T *&ptr)
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:533
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2336
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
unsigned int UInt_t
Definition: RtypesCore.h:42
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:1310
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
#define h(i)
Definition: RSha256.hxx:106
The Canvas class.
Definition: TCanvas.h:31
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7714
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:1711
A TTree object has a header with a name and a title.
Definition: TTree.h:70
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2248
A TTree is a list of TBranches.
Definition: TBranch.h:62
void Modified(Bool_t flag=1)
Definition: TPad.h:414