GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BenchTree.C
Go to the documentation of this file.
1 
2 #ifndef __CINT__
3 
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 
11 #include <TStopwatch.h>
12 #include <TFile.h>
13 #include <TTree.h>
14 #include <Riostream.h>
15 #endif
16 
17 #define BUFSIZE 32*1024
18 
19 Float_t write_sys(const char *filename = "data.tmp")
20 {
21  int nbblock = 30000; char buf[BUFSIZE];
22 
23  printf("Writing %d Mbytes (%d blocks) with the system function write \n",nbblock*BUFSIZE/(1024*1024),nbblock);
24 
25  int id = open(filename,O_WRONLY | O_CREAT);
26  if ( id == -1 ) {
27  printf("Cannot open file \n");
28  return 0.0;
29  }
30 
31  // write blocks and compute the needed time
32  TStopwatch watch;
33  watch.Start();
34  for (int i = 0; i < nbblock; i++ ) write(id,buf,BUFSIZE);
35  watch.Stop();
36 
37  printf(" --> Writing rate %f MB/s \n",nbblock*BUFSIZE/(1024*1024*watch.RealTime()));
38 
39  close(id);
40 
41  return nbblock*BUFSIZE/(1024*1024*watch.RealTime());
42 }
43 
44 Float_t read_sys(const char *filename = "data.tmp")
45 {
46  int nbblock = 30000; char buf[BUFSIZE];
47 
48  printf("Reading %d Mbytes (%d blocks) with the system function read \n",nbblock*BUFSIZE/(1024*1024),nbblock);
49  int id = open(filename,O_RDONLY);
50  if ( id == -1 ) {
51  printf("Cannot open file \n");
52  return 0.0;
53  }
54 
55  // read blocks and compute the needed time
56  TStopwatch watch;
57  watch.Start();
58  for (int i = 0; i < nbblock; i++ ) read(id,buf,BUFSIZE);
59  watch.Stop();
60 
61  printf(" --> Reading rate %f MB/s \n",nbblock*BUFSIZE/(1024*1024*watch.RealTime()));
62  close(id);
63 
64  return nbblock*BUFSIZE/(1024*1024*watch.RealTime());
65 }
66 
67 Float_t write_tree(const char *filename = "data.root", Int_t nbevents = 10000000, Int_t compression = 0)
68 {
69  printf("Writing %d events in a root tree with compression level %d \n",nbevents,compression);
70 
71  TFile *fileout = new TFile(filename,"recreate");
72  if ( fileout->IsZombie() ) {
73  printf("Cannot open file \n");
74  return 0.0;
75  }
76  fileout->SetCompressionLevel(compression);
77 
78  Double_t wbytes = 0.0;
79  Int_t nbgamma; Int_t e[1000];
80  for (Int_t i = 1; i < 1000; i++) e[i] = 200*i;
81 
82  TTree *treeout;
83  treeout = new TTree("TEST","TEST");
84  treeout->Branch("mult",&nbgamma,"nbgamma/I"); treeout->Branch("e",e,"e[nbgamma]/I");
85 
86  // write events and compute the needed time
87  TStopwatch watch;
88  watch.Start();
89  for (int i = 0; i < nbevents; i++ ) {
90  nbgamma = 2 + i % 5; wbytes += 4.0; wbytes += nbgamma * 4.0;
91  treeout->Fill();
92  }
93  watch.Stop();
94 
95  printf(" --> Writing rate %f MB/s [%f] \n",wbytes/(1024*1024*watch.RealTime()),wbytes/(1024*1024));
96 
97  fileout->Close(); delete fileout;
98 
99  return wbytes;
100 }
101 
102 Float_t read_tree(const char *filename = "data.root", Double_t bytes = 10000000)
103 {
104  printf("Reading events from a root tree \n");
105 
106  TFile *filein = new TFile(filename,"READ");
107  if ( filein->IsZombie() ) {
108  printf("Cannot open file \n");
109  return 0.0;
110  }
111 
112  Int_t nbgamma; Int_t e[1000]; // up to 1000 energies ..should be ok
113 
114  TTree *treein;
115  treein = (TTree *)filein->Get("TEST");
116  if ( treein == NULL ) return 0;
117  else {
118  treein->SetBranchAddress("mult",&nbgamma);
119  treein->SetBranchAddress("e",&e);
120  }
121 
122  // write events and compute the needed time
123  TStopwatch watch;
124  watch.Start();
125  treein->Draw("e","","goff");
126  watch.Stop();
127 
128  cout << " --> Reading rate " << bytes / (1024*1024*watch.RealTime()) << " MB/s"<< endl ;
129 
130  filein->Close(); delete filein; return bytes / (1024*1024*watch.RealTime()) ;
131 }
132 
133 void BenchTree()
134 {
135  const char *fname1 = "testio.tmp";
136  printf("\n************ Basic I/O from the system ************ \n");
137  write_sys(fname1); read_sys(fname1);
138 
139  printf("\n************ ROOT/TTree I/O ************ \n");
140  const char *fname2 = "testio.root";
141  Double_t wbytes;
142  wbytes = write_tree(fname2,10000000,0); read_tree(fname2,wbytes);
143  wbytes = write_tree(fname2,10000000,1); read_tree(fname2,wbytes);
144  wbytes = write_tree(fname2,10000000,2); read_tree(fname2,wbytes);
145  wbytes = write_tree(fname2,10000000,8); read_tree(fname2,wbytes);
146  wbytes = write_tree(fname2,10000000,9); read_tree(fname2,wbytes);
147 }
148 
149 
150 
151 
152 
153 
printf("******************************************************************** \n")
Float_t write_sys(const char *filename="data.tmp")
Definition: BenchTree.C:19
Float_t read_sys(const char *filename="data.tmp")
Definition: BenchTree.C:44
Float_t write_tree(const char *filename="data.root", Int_t nbevents=10000000, Int_t compression=0)
Definition: BenchTree.C:67
ADF::LogMessage & endl(ADF::LogMessage &log)
void BenchTree()
Definition: BenchTree.C:133
Float_t read_tree(const char *filename="data.root", Double_t bytes=10000000)
Definition: BenchTree.C:102
#define BUFSIZE
Definition: BenchTree.C:17