Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TMemFile.h"
45#include "TKey.h"
46#include "TBranchRef.h"
47
48#include <iostream>
49
50Long64_t GetTotalSize(TBranch * b, bool ondisk, bool inclusive);
51Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive);
52
53void printSizes() {
54}
55
56Long64_t GetBasketSize(TObjArray * branches, bool ondisk, bool inclusive) {
57 Long64_t result = 0;
58 size_t n = branches->GetEntries();
59 for( size_t i = 0; i < n; ++ i ) {
60 result += GetBasketSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, inclusive );
61 }
62 return result;
63}
64
65Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive) {
66 Long64_t result = 0;
67 if (b) {
68 if (ondisk && b->GetZipBytes() > 0) {
69 result = b->GetZipBytes();
70 } else {
71 result = b->GetTotBytes();
72 }
73 if (inclusive) {
74 result += GetBasketSize(b->GetListOfBranches(), ondisk, true);
75 }
76 return result;
77 }
78 return result;
79}
80
81Long64_t GetTotalSize( TBranch * br, bool ondisk, bool inclusive ) {
82 TMemFile f("buffer","CREATE");
83 if (br->GetTree()->GetCurrentFile()) {
84 f.SetCompressionSettings(br->GetTree()->GetCurrentFile()->GetCompressionSettings());
85 }
86 f.WriteObject(br,"thisbranch");
87 TKey* key = f.GetKey("thisbranch");
89 if (ondisk)
90 size = key->GetNbytes();
91 else
92 size = key->GetObjlen();
93 return GetBasketSize(br, ondisk, inclusive) + size;
94}
95
96Long64_t GetTotalSize( TObjArray * branches, bool ondisk ) {
97 Long64_t result = 0;
98 size_t n = branches->GetEntries();
99 for( size_t i = 0; i < n; ++ i ) {
100 result += GetTotalSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, true );
101 cerr << "After " << branches->At( i )->GetName() << " " << result << endl;
102 }
103 return result;
104}
105
106Long64_t GetTotalSize(TTree *t, bool ondisk) {
107 TKey *key = 0;
108 if (t->GetDirectory()) {
109 key = t->GetDirectory()->GetKey(t->GetName());
110 }
113 if (key) {
114 ondiskSize = key->GetNbytes();
115 totalSize = key->GetObjlen();
116 } else {
117 TMemFile f("buffer","CREATE");
118 if (t->GetCurrentFile()) {
119 f.SetCompressionSettings(t->GetCurrentFile()->GetCompressionSettings());
120 }
121 f.WriteTObject(t);
122 key = f.GetKey(t->GetName());
123 ondiskSize = key->GetNbytes();
124 totalSize = key->GetObjlen();
125 }
126 if (t->GetBranchRef() ) {
127 if (ondisk) {
128 ondiskSize += GetBasketSize(t->GetBranchRef(), true, true);
129 } else {
130 totalSize += GetBasketSize(t->GetBranchRef(), false, true);
131 }
132 }
133 if (ondisk) {
134 return ondiskSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ true, /* inclusive */ true);
135 } else {
136 return totalSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ false, /* inclusive */ true);
137 }
138}
139
141 // Return the size on disk on this TTree.
142
143 return GetTotalSize(t, true);
144}
145
147{
148 // Return the size on disk on this branch.
149 // If 'inclusive' is true, include also the size
150 // of all its sub-branches.
151
152 return GetTotalSize(branch, true, inclusive);
153}
154
156{
157 cout << "The branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
158 size_t n = br->GetListOfBranches()->GetEntries();
159 for( size_t i = 0; i < n; ++ i ) {
160 TBranch *subbr = dynamic_cast<TBranch*>(br->GetListOfBranches()->At(i));
161 cout << " Its sub-branch \"" << subbr->GetName() << "\" takes " << sizeOnDisk(subbr,true) << " bytes on disk\n";
162 }
163}
164
165void printTreeSummary(TTree *t)
166{
167 cout << "The TTree \"" << t->GetName() << "\" takes " << sizeOnDisk(t) << " bytes on disk\n";
168 size_t n = t->GetListOfBranches()->GetEntries();
169 for( size_t i = 0; i < n; ++ i ) {
170 TBranch *br =dynamic_cast<TBranch*>(t->GetListOfBranches()->At(i));
171 cout << " Its branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
172 }
173}
174
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
A TTree is a list of TBranches.
Definition TBranch.h:93
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition TDirectory.h:222
Int_t GetCompressionSettings() const
Definition TFile.h:489
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
Int_t GetObjlen() const
Definition TKey.h:89
Int_t GetNbytes() const
Definition TKey.h:88
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition TMemFile.h:27
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
An array of TObjects.
Definition TObjArray.h:31
A TTree represents a columnar dataset.
Definition TTree.h:89
TFile * GetCurrentFile() const
Return pointer to the current file.
Definition TTree.cxx:5582
TDirectory * GetDirectory() const
Definition TTree.h:517
virtual TBranchRef * GetBranchRef() const
Definition TTree.h:505
virtual TObjArray * GetListOfBranches()
Definition TTree.h:583
const Int_t n
Definition legend1.C:16