ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
printSizes.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 ///
4 /// This macro can be used to get aggregate information on the size
5 /// take on disk or in memory by the various branches in a TTree.
6 ///
7 /// For example:
8 /// ~~~{.cpp}
9 /// root [] printTreeSummary(tree);
10 /// The TTree "T" takes 3764343 bytes on disk
11 /// Its branch "event" takes 3760313 bytes on disk
12 /// ~~~
13 /// ~~~{.cpp}
14 /// root [] printBranchSummary(tree->GetBranch("event"));
15 /// The branch "event" takes 3760313 bytes on disk
16 /// Its sub-branch "TObject" takes 581 bytes on disk
17 /// Its sub-branch "fType[20]" takes 640 bytes on disk
18 /// Its sub-branch "fEventName" takes 855 bytes on disk
19 /// Its sub-branch "fNtrack" takes 506 bytes on disk
20 /// Its sub-branch "fNseg" takes 554 bytes on disk
21 /// Its sub-branch "fNvertex" takes 507 bytes on disk
22 /// Its sub-branch "fFlag" takes 420 bytes on disk
23 /// Its sub-branch "fTemperature" takes 738 bytes on disk
24 /// Its sub-branch "fMeasures[10]" takes 1856 bytes on disk
25 /// Its sub-branch "fMatrix[4][4]" takes 4563 bytes on disk
26 /// Its sub-branch "fClosestDistance" takes 2881 bytes on disk
27 /// Its sub-branch "fEvtHdr" takes 847 bytes on disk
28 /// Its sub-branch "fTracks" takes 3673982 bytes on disk
29 /// Its sub-branch "fHighPt" takes 59640 bytes on disk
30 /// Its sub-branch "fMuons" takes 1656 bytes on disk
31 /// Its sub-branch "fLastTrack" takes 785 bytes on disk
32 /// Its sub-branch "fWebHistogram" takes 596 bytes on disk
33 /// Its sub-branch "fH" takes 10076 bytes on disk
34 /// Its sub-branch "fTriggerBits" takes 1699 bytes on disk
35 /// Its sub-branch "fIsValid" takes 366 bytes on disk
36 /// ~~~
37 /// \macro_code
38 /// \author xxx
39 
40 #include "TTree.h"
41 #include "TBranch.h"
42 #include "Riostream.h"
43 #include "TMemFile.h"
44 #include "TKey.h"
45 #include "TBranchRef.h"
46 
47 Long64_t GetTotalSize(TBranch * b, bool ondisk, bool inclusive);
48 Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive);
49 
50 Long64_t GetBasketSize(TObjArray * branches, bool ondisk, bool inclusive) {
51  Long64_t result = 0;
52  size_t n = branches->GetEntries();
53  for( size_t i = 0; i < n; ++ i ) {
54  result += GetBasketSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, inclusive );
55  }
56  return result;
57 }
58 
59 Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive) {
60  Long64_t result = 0;
61  if (b) {
62  if (ondisk && b->GetZipBytes() > 0) {
63  result = b->GetZipBytes();
64  } else {
65  result = b->GetTotBytes();
66  }
67  if (inclusive) {
68  result += GetBasketSize(b->GetListOfBranches(), ondisk, true);
69  }
70  return result;
71  }
72  return result;
73 }
74 
75 Long64_t GetTotalSize( TBranch * br, bool ondisk, bool inclusive ) {
76  TMemFile f("buffer","CREATE");
77  if (br->GetTree()->GetCurrentFile()) {
79  }
80  f.WriteObject(br,"thisbranch");
81  TKey* key = f.GetKey("thisbranch");
82  Long64_t size;
83  if (ondisk)
84  size = key->GetNbytes();
85  else
86  size = key->GetObjlen();
87  return GetBasketSize(br, ondisk, inclusive) + size;
88 }
89 
90 Long64_t GetTotalSize( TObjArray * branches, bool ondisk ) {
91  Long64_t result = 0;
92  size_t n = branches->GetEntries();
93  for( size_t i = 0; i < n; ++ i ) {
94  result += GetTotalSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, true );
95  cerr << "After " << branches->At( i )->GetName() << " " << result << endl;
96  }
97  return result;
98 }
99 
100 Long64_t GetTotalSize(TTree *t, bool ondisk) {
101  TKey *key = 0;
102  if (t->GetDirectory()) {
103  key = t->GetDirectory()->GetKey(t->GetName());
104  }
105  Long64_t ondiskSize = 0;
106  Long64_t totalSize = 0;
107  if (key) {
108  ondiskSize = key->GetNbytes();
109  totalSize = key->GetObjlen();
110  } else {
111  TMemFile f("buffer","CREATE");
112  if (t->GetCurrentFile()) {
114  }
115  f.WriteTObject(t);
116  key = f.GetKey(t->GetName());
117  ondiskSize = key->GetNbytes();
118  totalSize = key->GetObjlen();
119  }
120  if (t->GetBranchRef() ) {
121  if (ondisk) {
122  ondiskSize += GetBasketSize(t->GetBranchRef(), true, true);
123  } else {
124  totalSize += GetBasketSize(t->GetBranchRef(), false, true);
125  }
126  }
127  if (ondisk) {
128  return ondiskSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ true, /* inclusive */ true);
129  } else {
130  return totalSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ false, /* inclusive */ true);
131  }
132 }
133 
134 Long64_t sizeOnDisk(TTree *t) {
135  // Return the size on disk on this TTree.
136 
137  return GetTotalSize(t, true);
138 }
139 
140 Long64_t sizeOnDisk(TBranch *branch, bool inclusive)
141 {
142  // Return the size on disk on this branch.
143  // If 'inclusive' is true, include also the size
144  // of all its sub-branches.
145 
146  return GetTotalSize(branch, true, inclusive);
147 }
148 
149 void printBranchSummary(TBranch *br)
150 {
151  cout << "The branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
152  size_t n = br->GetListOfBranches()->GetEntries();
153  for( size_t i = 0; i < n; ++ i ) {
154  TBranch *subbr = dynamic_cast<TBranch*>(br->GetListOfBranches()->At(i));
155  cout << " Its sub-branch \"" << subbr->GetName() << "\" takes " << sizeOnDisk(subbr,true) << " bytes on disk\n";
156  }
157 }
158 
159 void printTreeSummary(TTree *t)
160 {
161  cout << "The TTree \"" << t->GetName() << "\" takes " << sizeOnDisk(t) << " bytes on disk\n";
162  size_t n = t->GetListOfBranches()->GetEntries();
163  for( size_t i = 0; i < n; ++ i ) {
164  TBranch *br =dynamic_cast<TBranch*>(t->GetListOfBranches()->At(i));
165  cout << " Its branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
166  }
167 }
168 
169 void printSizes() {}
An array of TObjects.
Definition: TObjArray.h:39
long long Long64_t
Definition: RtypesCore.h:69
Long64_t GetTotBytes(Option_t *option="") const
Return total number of bytes in the branch (excluding current buffer) if option ="*" includes all sub...
Definition: TBranch.cxx:1618
virtual TKey * GetKey(const char *name, Short_t cycle=9999) const
Return pointer to key with name,cycle.
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition: TTree.cxx:4987
TFile * f
virtual Int_t WriteTObject(const TObject *obj, const char *name=0, Option_t *option="", Int_t bufsize=0)
Write object obj to this directory.
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:156
virtual TObjArray * GetListOfBranches()
Definition: TTree.h:409
Int_t GetNbytes() const
Definition: TKey.h:88
Long64_t GetZipBytes(Option_t *option="") const
Return total number of zip bytes in the branch if option ="*" includes all sub-branches of this branc...
Definition: TBranch.cxx:1636
TObjArray * GetListOfBranches()
Definition: TBranch.h:177
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
Int_t WriteObject(const T *obj, const char *name, Option_t *option="", Int_t bufsize=0)
Definition: TDirectory.h:204
TDirectory * GetDirectory() const
Definition: TTree.h:385
virtual TBranchRef * GetBranchRef() const
Definition: TTree.h:374
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
TTree * GetTree() const
Definition: TBranch.h:183
Int_t GetCompressionSettings() const
Definition: TFile.h:363
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
Int_t GetObjlen() const
Definition: TKey.h:89
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
virtual void SetCompressionSettings(Int_t settings=1)
Used to specify the compression level and algorithm.
Definition: TFile.cxx:2150
A TTree object has a header with a name and a title.
Definition: TTree.h:98
double result[121]
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Int_t n
Definition: legend1.C:16