Logo ROOT   6.10/09
Reference Guide
bill.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// \notebook -nodraw
4 /// Benchmark comparing row-wise and column-wise storage performance
5 ///
6 /// The test consists in writing/reading to/from keys or trees
7 /// To execute the benchmark:
8 /// ~~~
9 /// root -b -q bill.C or root -b -q bill.C++
10 /// ~~~
11 /// for example for N=10000, the following output is produced
12 /// on an 2.7 GHz Intel Core i7 (year 2011).
13 /// The names featuring a "t" are relative to trees, the faster, the better.
14 /// ~~~
15 /// billw0 : RT= 0.803 s, Cpu= 0.800 s, File size= 45608143 bytes, CX= 1
16 /// billr0 : RT= 0.388 s, Cpu= 0.390 s
17 /// billtw0 : RT= 0.336 s, Cpu= 0.310 s, File size= 45266881 bytes, CX= 1.00034
18 /// billtr0 : RT= 0.229 s, Cpu= 0.230 s
19 /// billw1 : RT= 1.671 s, Cpu= 1.670 s, File size= 16760526 bytes, CX= 2.72078
20 /// billr1 : RT= 0.667 s, Cpu= 0.680 s
21 /// billtw1 : RT= 0.775 s, Cpu= 0.770 s, File size= 9540884 bytes, CX= 4.74501
22 /// billtr1 : RT= 0.352 s, Cpu= 0.350 s
23 /// billtot : RT= 5.384 s, Cpu= 5.290 s
24 /// ******************************************************************
25 /// * ROOTMARKS =1763.9 * Root6.05/03 20150914/948
26 /// ******************************************************************
27 /// ~~~
28 /// \macro_code
29 /// \author Rene Brun
30 
31 #include "TFile.h"
32 #include "TSystem.h"
33 #include "TH1.h"
34 #include "TRandom.h"
35 #include "TStopwatch.h"
36 #include "TKey.h"
37 #include "TTree.h"
38 #include "TROOT.h"
39 
40 const Int_t N = 10000; //number of events to be processed
42 
43 
44 
45 void billw(const char *billname, Int_t compress) {
46  //write N histograms as keys
47  timer.Start();
48  TFile f(billname,"recreate","bill benchmark with keys",compress);
49  TH1F h("h","h",1000,-3,3);
50  h.FillRandom("gaus",50000);
51 
52  for (Int_t i=0;i<N;i++) {
53  char name[20];
54  sprintf(name,"h%d",i);
55  h.SetName(name);
56  h.Fill(2*gRandom->Rndm());
57  h.Write();
58  }
59  timer.Stop();
60  printf("billw%d : RT=%7.3f s, Cpu=%7.3f s, File size= %9d bytes, CX= %g\n",compress,timer.RealTime(),timer.CpuTime(),
61  (Int_t)f.GetBytesWritten(),f.GetCompressionFactor());
62 }
63 
64 void billr(const char *billname, Int_t compress) {
65  //read N histograms from keys
66  timer.Start();
67  TFile f(billname);
68  TIter next(f.GetListOfKeys());
69  TH1F *h;
71  TKey *key;
72  Int_t i=0;
73  TH1F *hmean = new TH1F("hmean","hist mean from keys",100,0,1);
74 
75  while ((key=(TKey*)next())) {
76  h = (TH1F*)key->ReadObj();
77  hmean->Fill(h->GetMean());
78  delete h;
79  i++;
80  }
81  timer.Stop();
82  printf("billr%d : RT=%7.3f s, Cpu=%7.3f s\n",compress,timer.RealTime(),timer.CpuTime());
83 }
84 
85 void billtw(const char *billtname, Int_t compress) {
86  //write N histograms to a Tree
87  timer.Start();
88  TFile f(billtname,"recreate","bill benchmark with trees",compress);
89  TH1F *h = new TH1F("h","h",1000,-3,3);
90  h->FillRandom("gaus",50000);
91  TTree *T = new TTree("T","test bill");
92  T->Branch("event","TH1F",&h,64000,0);
93  for (Int_t i=0;i<N;i++) {
94  char name[20];
95  sprintf(name,"h%d",i);
96  h->SetName(name);
97  h->Fill(2*gRandom->Rndm());
98  T->Fill();
99  }
100  T->Write();
101  delete T;
102  timer.Stop();
103  printf("billtw%d : RT=%7.3f s, Cpu=%7.3f s, File size= %9d bytes, CX= %g\n",compress,timer.RealTime(),timer.CpuTime(),
104  (Int_t)f.GetBytesWritten(),f.GetCompressionFactor());
105 }
106 
107 void billtr(const char *billtname, Int_t compress) {
108  //read N histograms from a tree
109  timer.Start();
110  TFile f(billtname);
111  TH1F *h = 0;
112  TTree *T = (TTree*)f.Get("T");
113  T->SetBranchAddress("event",&h);
114  TH1F *hmeant = new TH1F("hmeant","hist mean from tree",100,0,1);
116  for (Long64_t i=0;i<nentries;i++) {
117  T->GetEntry(i);
118  hmeant->Fill(h->GetMean());
119  }
120  timer.Stop();
121  printf("billtr%d : RT=%7.3f s, Cpu=%7.3f s\n",compress,timer.RealTime(),timer.CpuTime());
122 }
123 
124 void bill() {
125 
126  TString dir = gSystem->DirName(gSystem->UnixPathName(__FILE__));
127  TString bill = dir + "/bill.root";
128  TString billt = dir + "/billt.root";
129 
130  TStopwatch totaltimer;
131  totaltimer.Start();
132  for (Int_t compress=0;compress<2;compress++) {
133  billw(bill,compress);
134  billr(bill,compress);
135  billtw(billt,compress);
136  billtr(billt,compress);
137  }
138  gSystem->Unlink(bill);
139  gSystem->Unlink(billt);
140  totaltimer.Stop();
141  Double_t realtime = totaltimer.RealTime();
142  Double_t cputime = totaltimer.CpuTime();
143  printf("billtot : RT=%7.3f s, Cpu=%7.3f s\n",realtime,cputime);
144  //reference is a P IV 2.4 GHz
145  Float_t rootmarks = 600*(16.98 + 14.40)/(realtime + cputime);
146  printf("******************************************************************\n");
147  printf("* ROOTMARKS =%6.1f * Root%-8s %d/%d\n",rootmarks,gROOT->GetVersion(),gROOT->GetVersionDate(),gROOT->GetVersionTime());
148  printf("******************************************************************\n");
149 }
150 
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
long long Long64_t
Definition: RtypesCore.h:69
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
float Float_t
Definition: RtypesCore.h:53
double T(double x)
Definition: ChebyshevPol.h:34
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4383
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
TH1 * h
Definition: legend2.C:5
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
#define gROOT
Definition: TROOT.h:375
#define N
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition: TH1.cxx:6763
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5321
Basic string class.
Definition: TString.h:129
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1003
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition: TH1.cxx:1218
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1043
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1353
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:7873
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9163
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3294
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8073
double f(double x)
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
virtual Long64_t GetEntries() const
Definition: TTree.h:381
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:1660
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:730
A TTree object has a header with a name and a title.
Definition: TTree.h:78
Stopwatch class.
Definition: TStopwatch.h:28