WCSim
printSizes.C
Go to the documentation of this file.
1 #include "TTree.h"
2 #include "TBranch.h"
3 #include "Riostream.h"
4 #include "TMemFile.h"
5 #include "TKey.h"
6 #include "TBranchRef.h"
7 #include "TSystem.h"
8 
9 #include "WCSimRootEvent.hh"
10 #include "WCSimRootGeom.hh"
11 #include "WCSimEnumerations.hh"
12 
13 using namespace std;
14 
15 //
16 // This macro can be used to get aggregate information on the size
17 // take on disk or in memory by the various branches in a TTree.
18 // For example:
19 /*
20 
21 root [] printTreeSummary(tree);
22 The TTree "T" takes 3764343 bytes on disk
23  Its branch "event" takes 3760313 bytes on disk
24 
25 root [] printBranchSummary(tree->GetBranch("event"));
26 The branch "event" takes 3760313 bytes on disk
27  Its sub-branch "TObject" takes 581 bytes on disk
28  Its sub-branch "fType[20]" takes 640 bytes on disk
29  Its sub-branch "fEventName" takes 855 bytes on disk
30  Its sub-branch "fNtrack" takes 506 bytes on disk
31  Its sub-branch "fNseg" takes 554 bytes on disk
32  Its sub-branch "fNvertex" takes 507 bytes on disk
33  Its sub-branch "fFlag" takes 420 bytes on disk
34  Its sub-branch "fTemperature" takes 738 bytes on disk
35  Its sub-branch "fMeasures[10]" takes 1856 bytes on disk
36  Its sub-branch "fMatrix[4][4]" takes 4563 bytes on disk
37  Its sub-branch "fClosestDistance" takes 2881 bytes on disk
38  Its sub-branch "fEvtHdr" takes 847 bytes on disk
39  Its sub-branch "fTracks" takes 3673982 bytes on disk
40  Its sub-branch "fHighPt" takes 59640 bytes on disk
41  Its sub-branch "fMuons" takes 1656 bytes on disk
42  Its sub-branch "fLastTrack" takes 785 bytes on disk
43  Its sub-branch "fWebHistogram" takes 596 bytes on disk
44  Its sub-branch "fH" takes 10076 bytes on disk
45  Its sub-branch "fTriggerBits" takes 1699 bytes on disk
46  Its sub-branch "fIsValid" takes 366 bytes on disk
47 
48  */
49 
50 Long64_t GetTotalSize(TBranch * b, bool ondisk, bool inclusive);
51 Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive);
52 
53 Long64_t GetBasketSize(TObjArray * branches, bool ondisk, bool inclusive) {
54  Long64_t result = 0;
55  size_t n = branches->GetEntries();
56  for( size_t i = 0; i < n; ++ i ) {
57  result += GetBasketSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, inclusive );
58  }
59  return result;
60 }
61 
62 Long64_t GetBasketSize(TBranch * b, bool ondisk, bool inclusive) {
63  Long64_t result = 0;
64  if (b) {
65  if (ondisk && b->GetZipBytes() > 0) {
66  result = b->GetZipBytes();
67  } else {
68  result = b->GetTotBytes();
69  }
70  if (inclusive) {
71  result += GetBasketSize(b->GetListOfBranches(), ondisk, true);
72  }
73  return result;
74  }
75  return result;
76 }
77 
78 Long64_t GetTotalSize( TBranch * br, bool ondisk, bool inclusive ) {
79  TMemFile f("buffer","CREATE");
80  if (br->GetTree()->GetCurrentFile()) {
81  f.SetCompressionSettings(br->GetTree()->GetCurrentFile()->GetCompressionSettings());
82  }
83  f.WriteObject(br,"thisbranch");
84  TKey* key = f.GetKey("thisbranch");
85  Long64_t size;
86  if (ondisk)
87  size = key->GetNbytes();
88  else
89  size = key->GetObjlen();
90  return GetBasketSize(br, ondisk, inclusive) + size;
91 }
92 
93 Long64_t GetTotalSize( TObjArray * branches, bool ondisk ) {
94  Long64_t result = 0;
95  size_t n = branches->GetEntries();
96  for( size_t i = 0; i < n; ++ i ) {
97  result += GetTotalSize( dynamic_cast<TBranch*>( branches->At( i ) ), ondisk, true );
98  cerr << "After " << branches->At( i )->GetName() << " " << result << endl;
99  }
100  return result;
101 }
102 
103 Long64_t GetTotalSize(TTree *t, bool ondisk) {
104  TKey *key = 0;
105  if (t->GetDirectory()) {
106  key = t->GetDirectory()->GetKey(t->GetName());
107  }
108  Long64_t ondiskSize = 0;
109  Long64_t totalSize = 0;
110  if (key) {
111  ondiskSize = key->GetNbytes();
112  totalSize = key->GetObjlen();
113  } else {
114  TMemFile f("buffer","CREATE");
115  if (t->GetCurrentFile()) {
116  f.SetCompressionSettings(t->GetCurrentFile()->GetCompressionSettings());
117  }
118  f.WriteTObject(t);
119  key = f.GetKey(t->GetName());
120  ondiskSize = key->GetNbytes();
121  totalSize = key->GetObjlen();
122  }
123  if (t->GetBranchRef() ) {
124  if (ondisk) {
125  ondiskSize += GetBasketSize(t->GetBranchRef(), true, true);
126  } else {
127  totalSize += GetBasketSize(t->GetBranchRef(), false, true);
128  }
129  }
130  if (ondisk) {
131  return ondiskSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ true, /* inclusive */ true);
132  } else {
133  return totalSize + GetBasketSize(t->GetListOfBranches(), /* ondisk */ false, /* inclusive */ true);
134  }
135 }
136 
137 Long64_t sizeOnDisk(TTree *t) {
138  // Return the size on disk on this TTree.
139 
140  return GetTotalSize(t, true);
141 }
142 
143 Long64_t sizeOnDisk(TBranch *branch, bool inclusive)
144 {
145  // Return the size on disk on this branch.
146  // If 'inclusive' is true, include also the size
147  // of all its sub-branches.
148 
149  return GetTotalSize(branch, true, inclusive);
150 }
151 
152 void printBranchSummary(TBranch *br)
153 {
154  cout << "The branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
155  size_t n = br->GetListOfBranches()->GetEntries();
156  for( size_t i = 0; i < n; ++ i ) {
157  TBranch *subbr = dynamic_cast<TBranch*>(br->GetListOfBranches()->At(i));
158  cout << " Its sub-branch \"" << subbr->GetName() << "\" takes " << sizeOnDisk(subbr,true) << " bytes on disk\n";
159  }
160 }
161 
162 void printTreeSummary(TTree *t)
163 {
164  cout << "The TTree \"" << t->GetName() << "\" takes " << sizeOnDisk(t) << " bytes on disk\n";
165  size_t n = t->GetListOfBranches()->GetEntries();
166  for( size_t i = 0; i < n; ++ i ) {
167  TBranch *br =dynamic_cast<TBranch*>(t->GetListOfBranches()->At(i));
168  cout << " Its branch \"" << br->GetName() << "\" takes " << sizeOnDisk(br,true) << " bytes on disk\n";
169  }
170 }
171 
172 const int ntrees = 3;
173 string wcsimtrees[ntrees] = {"wcsimGeoT", "wcsimT", "wcsimRootOptionsT"};
174 string wcsimbranches[ntrees] = {"wcsimrootgeom", "wcsimrootevent", "wcsimrootoptions"};
175 
176 TFile * OpenFile(const char * filename)
177 {
178  TFile *f = TFile::Open(filename);
179  if (!f->IsOpen()){
180  cout << "Error, could not open input file: " << filename << endl;
181  exit(-1);
182  }
183  return f;
184 }
185 
186 Long64_t GetWCSimTreeSize(TFile * f, int itree, bool verbose)
187 {
188  TTree *t;
189  f->GetObject(wcsimtrees[itree].c_str(), t);
190  if (!t) return 1;
192  if(verbose)
193  printBranchSummary(t->GetBranch(wcsimbranches[itree].c_str()));
194  return sizeOnDisk(t);
195 }
196 
197 int printSizes(const char *filename1="wcsimtest.root",
198  const char *filename2="../../WCSim_clean/verification-test-scripts/wcsimtest.root",
199  bool verbose = false)
200 {
201 
202  cout << "Your version of WCSim assumes file: " << filename1 << endl
203  << "Stable GitHub version of WCSim assumes file: " << filename2 << endl;
204  TFile * f1 = OpenFile(filename1);
205  TFile * f2 = OpenFile(filename2);
206  for(int itree = 0; itree < ntrees; itree++) {
207  Long64_t s1 = GetWCSimTreeSize(f1, itree, verbose);
208  Long64_t s2 = GetWCSimTreeSize(f2, itree, verbose);
209  if(s1 == s2)
210  cout << "Trees sizes are identical!" << endl;
211  else {
212  double change = (s1 - s2) * 100. / (double)s2;
213  cout << "Your WCSim version tree is " << change << "% " << (change > 0 ? "larger" : "smaller")
214  << " than the master GitHub version" << endl;
215  }
216  }//itree
217 
218  return 0;
219 }
Definition: json.hpp:5295
string filename
Definition: MakeKin.py:235
double t[MAX_N_ACTIVE_TUBES]
Definition: jhfNtuple.h:45