Logo ROOT  
Reference Guide
printSizes.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
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///
38/// \macro_code
39///
40/// \author
41
42#include "TTree.h"
43#include "TBranch.h"
44#include "Riostream.h"
45#include "TMemFile.h"
46#include "TKey.h"
47#include "TBranchRef.h"
48
49Long64_t GetTotalSize(TBranch * b, bool ondisk, bool inclusive);
50Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive);
51
52void printSizes() {
53}
54
55Long64_t GetBasketSize(TObjArray * branches, bool ondisk, bool inclusive) {
56 Long64_t result = 0;
57 size_t n = branches->GetEntries();
58 for( size_t i = 0; i < n; ++ i ) {
59 result += GetBasketSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, inclusive );
60 }
61 return result;
62}
63
64Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive) {
65 Long64_t result = 0;
66 if (b) {
67 if (ondisk && b->GetZipBytes() > 0) {
68 result = b->GetZipBytes();
69 } else {
70 result = b->GetTotBytes();
71 }
72 if (inclusive) {
73 result += GetBasketSize(b->GetListOfBranches(), ondisk, true);
74 }
75 return result;
76 }
77 return result;
78}
79
80Long64_t GetTotalSize( TBranch * br, bool ondisk, bool inclusive ) {
81 TMemFile f("buffer","CREATE");
82 if (br->GetTree()->GetCurrentFile()) {
83 f.SetCompressionSettings(br->GetTree()->GetCurrentFile()->GetCompressionSettings());
84 }
85 f.WriteObject(br,"thisbranch");
86 TKey* key = f.GetKey("thisbranch");
87 Long64_t size;
88 if (ondisk)
89 size = key->GetNbytes();
90 else
91 size = key->GetObjlen();
92 return GetBasketSize(br, ondisk, inclusive) + size;
93}
94
95Long64_t GetTotalSize( TObjArray * branches, bool ondisk ) {
96 Long64_t result = 0;
97 size_t n = branches->GetEntries();
98 for( size_t i = 0; i < n; ++ i ) {
99 result += GetTotalSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, true );
100 cerr << "After " << branches->At( i )->GetName() << " " << result << endl;
101 }
102 return result;
103}
104
105Long64_t GetTotalSize(TTree *t, bool ondisk) {
106 TKey *key = 0;
107 if (t->GetDirectory()) {
108 key = t->GetDirectory()->GetKey(t->GetName());
109 }
110 Long64_t ondiskSize = 0;
111 Long64_t totalSize = 0;
112 if (key) {
113 ondiskSize = key->GetNbytes();
114 totalSize = key->GetObjlen();
115 } else {
116 TMemFile f("buffer","CREATE");
117 if (t->GetCurrentFile()) {
118 f.SetCompressionSettings(t->GetCurrentFile()->GetCompressionSettings());
119 }
120 f.WriteTObject(t);
121 key = f.GetKey(t->GetName());
122 ondiskSize = key->GetNbytes();
123 totalSize = key->GetObjlen();
124 }
125 if (t->GetBranchRef() ) {
126 if (ondisk) {
127 ondiskSize += GetBasketSize(t->GetBranchRef(), true, true);
128 } else {
129 totalSize += GetBasketSize(t->GetBranchRef(), false, true);
130 }
131 }
132 if (ondisk) {
133 return ondiskSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ true, /* inclusive */ true);
134 } else {
135 return totalSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ false, /* inclusive */ true);
136 }
137}
138
139Long64_t sizeOnDisk(TTree *t) {
140 // Return the size on disk on this TTree.
141
142 return GetTotalSize(t, true);
143}
144
145Long64_t sizeOnDisk(TBranch *branch, bool inclusive)
146{
147 // Return the size on disk on this branch.
148 // If 'inclusive' is true, include also the size
149 // of all its sub-branches.
150
151 return GetTotalSize(branch, true, inclusive);
152}
153
154void printBranchSummary(TBranch *br)
155{
156 cout << "The branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
157 size_t n = br->GetListOfBranches()->GetEntries();
158 for( size_t i = 0; i < n; ++ i ) {
159 TBranch *subbr = dynamic_cast<TBranch*>(br->GetListOfBranches()->At(i));
160 cout << " Its sub-branch \"" << subbr->GetName() << "\" takes " << sizeOnDisk(subbr,true) << " bytes on disk\n";
161 }
162}
163
164void printTreeSummary(TTree *t)
165{
166 cout << "The TTree \"" << t->GetName() << "\" takes " << sizeOnDisk(t) << " bytes on disk\n";
167 size_t n = t->GetListOfBranches()->GetEntries();
168 for( size_t i = 0; i < n; ++ i ) {
169 TBranch *br =dynamic_cast<TBranch*>(t->GetListOfBranches()->At(i));
170 cout << " Its branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
171 }
172}
173
#define b(i)
Definition: RSha256.hxx:100
#define f(i)
Definition: RSha256.hxx:104
long long Long64_t
Definition: RtypesCore.h:69
A TTree is a list of TBranches.
Definition: TBranch.h:91
TTree * GetTree() const
Definition: TBranch.h:250
TObjArray * GetListOfBranches()
Definition: TBranch.h:244
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:158
Int_t GetCompressionSettings() const
Definition: TFile.h:394
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
Int_t GetObjlen() const
Definition: TKey.h:84
Int_t GetNbytes() const
Definition: TKey.h:83
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:19
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
An array of TObjects.
Definition: TObjArray.h:37
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:522
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
A TTree represents a columnar dataset.
Definition: TTree.h:72
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition: TTree.cxx:5338
TDirectory * GetDirectory() const
Definition: TTree.h:449
virtual TBranchRef * GetBranchRef() const
Definition: TTree.h:437
virtual TObjArray * GetListOfBranches()
Definition: TTree.h:475
const Int_t n
Definition: legend1.C:16