babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
disk_usage.cxx
Go to the documentation of this file.
1 #include <locale>
2 #include <iostream>
3 #include <iomanip>
4 #include <string>
5 #include <set>
6 #include <algorithm> // std::sort
7 
8 #include "TChain.h"
9 #include "TBranch.h"
10 #include "TLeaf.h"
11 #include "TString.h"
12 #include "utilities.hh"
13 
14 using namespace std;
15 
16 struct Variable{
17  TString name;
18  long zip_size, tot_size;
19 
20  bool operator<(const Variable &var) const{
21  // return (zip_size<var.zip_size)
22  // || (zip_size==var.zip_size && tot_size<var.tot_size)
23  // || (zip_size==var.zip_size && tot_size==var.tot_size && name<var.name);
24  return (name>var.name);
25  }
26 };
27 
28 class comma_numpunct : public numpunct<char>{
29 protected:
30  virtual char do_thousands_sep() const{
31  return ',';
32  }
33 
34  virtual string do_grouping() const{
35  return "\03";
36  }
37 };
38 
39 int findVar(const Variable &var, vector<Variable> &vars){
40  int index = -1;
41  for(size_t ivar=0; ivar<vars.size(); ivar++){
42  if(var.name == vars[ivar].name){
43  index = ivar;
44  break;
45  }
46  }
47 
48  return index;
49 }
50 
51 bool varCompare(const Variable &var1, const Variable &var2) {
52  return var1.zip_size > var2.zip_size;
53 }
54 
55 int main(int argc, char *argv[]){
56  cout.imbue(locale(locale(), new comma_numpunct));
57 
58  if(argc<2) {
59  cout<<endl<<"Format is: ./run/disk_usage.exe file <treename=tree>"<<endl<<endl;
60  return 1;
61  }
62 
63  TString nametree="tree", filename = argv[1];
64  if(argc>=3) nametree = argv[2];
65 
66  TChain chain(nametree);
67  if(!chain.Add(filename) || !chain.GetListOfLeaves()) {
68  cout<<endl<<"No tree found in "<<filename<<endl<<endl;
69  return 1;
70  }
71 
72  long zip_sum(0), tot_sum(0);
73  Ssiz_t max_length(0);
74  double nentries = chain.GetEntries();
75 
76  vector<Variable> vars, allvars;
77  set<Variable>::iterator it;
78 
79  for(int i = 0; i < chain.GetListOfLeaves()->GetSize(); ++i){
80  TBranch *b = static_cast<TLeaf*>(chain.GetListOfLeaves()->At(i))->GetBranch();
81  if(!b) continue;
82  Variable v;
83  v.name = b->GetName();
84  v.zip_size = b->GetZipBytes();
85  v.tot_size = b->GetTotBytes();
86 
87  allvars.push_back(v);
88  zip_sum += v.zip_size;
89  tot_sum += v.tot_size;
90  if(v.name.Length() > max_length) max_length = v.name.Length();
91 
92  if(v.name.First('_')>=0) v.name.Remove(v.name.First('_'), v.name.Length()-v.name.First('_'));
93  int index = findVar(v, vars);
94  if(index == -1) vars.push_back(v);
95  else {
96  vars[index].zip_size += v.zip_size;
97  vars[index].tot_size += v.tot_size;
98  }
99  } // Loop over branches
100 
101  int wbytes=10, wother=11;
102  TString sep = " ";
103  sort(vars.begin(), vars.end(), varCompare);
104  sort(allvars.begin(), allvars.end(), varCompare);
105  cout <<endl<<filename<<endl
106  << endl << "Tree \""<< nametree <<"\" occupies "<< zip_sum <<" bytes and has "<<addCommas(nentries)<<" entries ("
107  <<roundNumber(zip_sum,2,nentries*1024)<<" kB/event):" << endl;
108  cout <<endl<< setw(max_length) << "Branch name" << ' '
109  << setw(wbytes) << "Byte/ev" << ' '
110  << setw(wother) << "Frac. [%]" << ' '
111  << setw(wother) << "Cumulative" << sep
112  << setw(max_length) << "Branch group name" << ' '
113  << setw(wbytes) << "Byte/ev" << ' '
114  << setw(wother) << "Frac. [%]" << ' '
115  << setw(wother) << "Cumulative" << endl;
116  for(int ind=0; ind<(max_length+wbytes+2*wother+3); ind++) cout << "=";
117  cout << sep;
118  for(int ind=0; ind<(max_length+wbytes+2*wother+3); ind++) cout << "=";
119  cout << endl
120  << setw(max_length) << "Total" << ' '
121  << setw(wbytes) << roundNumber(zip_sum,1,nentries) << ' '
122  << setw(wother) << "100.00" << ' '
123  << setw(wother) << "-" << sep
124  << setw(max_length) << "Total" << ' '
125  << setw(wbytes) << roundNumber(zip_sum,1,nentries) << ' '
126  << setw(wother) << "100.00" << ' '
127  << setw(wother) << "-" << endl;
128  long running_total(0), tot2=0;
129  for(size_t ivar=0; ivar<allvars.size(); ivar++){
130  running_total += allvars[ivar].zip_size;
131  double this_frac = (100.0*allvars[ivar].zip_size)/zip_sum;
132  double tot_frac = (100.0*running_total)/zip_sum;
133  cout << setw(max_length) << allvars[ivar].name << " "
134  << setw(wbytes) << roundNumber(allvars[ivar].zip_size,2,nentries) << " "
135  << setw(wother) << roundNumber(this_frac,2) << " "
136  << setw(wother) << roundNumber(tot_frac,2) << sep;
137  if(ivar<vars.size()){
138  tot2 += vars[ivar].zip_size;
139  this_frac = (100.0*vars[ivar].zip_size)/zip_sum;
140  tot_frac = (100.0*tot2)/zip_sum;
141  cout << setw(max_length) << vars[ivar].name << " "
142  << setw(wbytes) << roundNumber(vars[ivar].zip_size,2,nentries) << " "
143  << setw(wother) << roundNumber(this_frac,2) << " "
144  << setw(wother) << roundNumber(tot_frac,2);
145 
146  }
147  cout<<endl;
148  } // Loop over all branches
149  cout<<endl;
150 }
long zip_size
Definition: disk_usage.cxx:18
STL namespace.
virtual string do_grouping() const
Definition: disk_usage.cxx:34
virtual char do_thousands_sep() const
Definition: disk_usage.cxx:30
bool operator<(const Variable &var) const
Definition: disk_usage.cxx:20
bool varCompare(const Variable &var1, const Variable &var2)
Definition: disk_usage.cxx:51
long tot_size
Definition: disk_usage.cxx:18
TString addCommas(double num)
Definition: utilities.cc:499
TString roundNumber(double num, int decimals, double denom=1.)
Definition: utilities.cc:478
int main(int argc, char *argv[])
Definition: disk_usage.cxx:55
tuple ind
Definition: resubmit.py:140
TString name
Definition: disk_usage.cxx:17
int findVar(const Variable &var, vector< Variable > &vars)
Definition: disk_usage.cxx:39