ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hvector.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
4 /// Write and read STL vectors in a tree.
5 /// \macro_image
6 /// \macro_code
7 /// \author The ROOT Team
8 
9 #include <vector>
10 
11 #include "TFile.h"
12 #include "TTree.h"
13 #include "TCanvas.h"
14 #include "TFrame.h"
15 #include "TH1F.h"
16 #include "TBenchmark.h"
17 #include "TRandom.h"
18 #include "TSystem.h"
19 
20 void write()
21 {
22 
23  TFile *f = TFile::Open("hvector.root","RECREATE");
24 
25  if (!f) { return; }
26 
27  // Create one histograms
28  TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
29  hpx->SetFillColor(48);
30 
31  std::vector<float> vpx;
32  std::vector<float> vpy;
33  std::vector<float> vpz;
34  std::vector<float> vrand;
35 
36  // Create a TTree
37  TTree *t = new TTree("tvec","Tree with vectors");
38  t->Branch("vpx",&vpx);
39  t->Branch("vpy",&vpy);
40  t->Branch("vpz",&vpz);
41  t->Branch("vrand",&vrand);
42 
43  // Create a new canvas.
44  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
45 
46  gRandom->SetSeed();
47  const Int_t kUPDATE = 1000;
48  for (Int_t i = 0; i < 25000; i++) {
49  Int_t npx = (Int_t)(gRandom->Rndm(1)*15);
50 
51  vpx.clear();
52  vpy.clear();
53  vpz.clear();
54  vrand.clear();
55 
56  for (Int_t j = 0; j < npx; ++j) {
57 
58  Float_t px,py,pz;
59  gRandom->Rannor(px,py);
60  pz = px*px + py*py;
61  Float_t random = gRandom->Rndm(1);
62 
63  hpx->Fill(px);
64 
65  vpx.emplace_back(px);
66  vpy.emplace_back(py);
67  vpz.emplace_back(pz);
68  vrand.emplace_back(random);
69 
70  }
71  if (i && (i%kUPDATE) == 0) {
72  if (i == kUPDATE) hpx->Draw();
73  c1->Modified();
74  c1->Update();
75  if (gSystem->ProcessEvents())
76  break;
77  }
78  t->Fill();
79  }
80  f->Write();
81 
82  delete f;
83 }
84 
85 
86 void read()
87 {
88 
89  TFile *f = TFile::Open("hvector.root","READ");
90 
91  if (!f) { return; }
92 
93  TTree *t; f->GetObject("tvec",t);
94 
95  std::vector<float> *vpx = 0;
96 
97  // Create a new canvas.
98  TCanvas *c1 = new TCanvas("c1","Dynamic Filling Example",200,10,700,500);
99 
100  const Int_t kUPDATE = 1000;
101 
102  TBranch *bvpx = 0;
103  t->SetBranchAddress("vpx",&vpx,&bvpx);
104 
105 
106  // Create one histograms
107  TH1F *h = new TH1F("h","This is the px distribution",100,-4,4);
108  h->SetFillColor(48);
109 
110  for (Int_t i = 0; i < 25000; i++) {
111 
112  Long64_t tentry = t->LoadTree(i);
113  bvpx->GetEntry(tentry);
114 
115  for (UInt_t j = 0; j < vpx->size(); ++j) {
116 
117  h->Fill(vpx->at(j));
118 
119  }
120  if (i && (i%kUPDATE) == 0) {
121  if (i == kUPDATE) h->Draw();
122  c1->Modified();
123  c1->Update();
124  if (gSystem->ProcessEvents())
125  break;
126  }
127  }
128 
129  // Since we passed the address of a local variable we need
130  // to remove it.
132 }
133 
134 
135 void hvector()
136 {
137  gBenchmark->Start("hvector");
138 
139  write();
140  read();
141 
142  gBenchmark->Show("hvector");
143 }
double read(const std::string &file_name)
reading
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:420
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:460
Float_t pz
Definition: hprod.C:33
long long Long64_t
Definition: RtypesCore.h:69
double write(int n, const std::string &file_name, const std::string &vector_type, int compress=0)
writing
float Float_t
Definition: RtypesCore.h:53
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4306
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
Float_t py
Definition: hprod.C:33
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
TFile * f
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:3851
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7510
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
Definition: TTree.cxx:5785
void GetObject(const char *namecycle, T *&ptr)
TThread * t[5]
Definition: threadsh1.C:13
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
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:2248
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
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:1199
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
The Canvas class.
Definition: TCanvas.h:48
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Definition: TTree.cxx:7266
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:1623
Float_t px
Definition: hprod.C:33
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
A TTree is a list of TBranches.
Definition: TBranch.h:58
void Modified(Bool_t flag=1)
Definition: TPad.h:407