]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/macros/MakeTreeStat.C
Add macro to check the size of the Tree
[u/mrichter/AliRoot.git] / TPC / macros / MakeTreeStat.C
1 /****************************************************************************
2   Macro to get the size of the Tree
3   As a improvment to  tree->Print() function, this algorithm
4   gives the size of all of the branchces and in addition print them 
5   sorted according total tree size (MEMORY USAGE if one event in tree)
6   or zip size (THE storage size on disk 
7
8   Printed statistic:
9   1. Order
10   2. TotSize (in memory) + fraction of total size
11   3. ZipSize (in memory) + fraction of zip size
12   4. Compression ratio 
13
14   Usage:
15   //  1. Enable macro
16   .L $ALICE_ROOT/TPC/macros/MakeTreeStat.C+
17   // 2. Open the tree (eg.)
18   TFile f("AliESDs.root");
19   TTree * tree = (TTree*)f.Get("esdTree");
20   // 3. Print statistic (sorting according secon argument - either zip Bytes (kTRUE or TotSize (kFALSE)
21   MakeStat(tree, kTRUE);
22
23   Origin: M.Ivanov, GSI, m.ivanov@gsi.de
24
25 ****************************************************************************/
26
27
28
29
30
31
32 #include "TFile.h"
33 #include "TTree.h"
34 #include "TBranch.h"
35 #include "TMath.h"
36 #include "TArrayF.h"
37 #include "TObjArray.h"
38 #include "TObjString.h"
39
40
41
42
43
44 TTree     *fTree;   // tree of interest
45 TObjArray aReport;  // array with branch statistic
46 TArrayF   totSize;  // total size for branch
47 TArrayF   zipSize;  // zip Size for branch
48 TArrayF   zipRatio; // zip Ratio for branch
49
50 void MakeStat(TTree *tree, Bool_t zipSort);
51
52
53
54 void PrintSorted(Bool_t zipSort){
55   //
56   //print statistic
57   //
58   Int_t entries   = aReport.GetEntries();
59   Int_t* indexes  = new Int_t[entries];
60   if (zipSort) TMath::Sort(entries,zipSize.GetArray(),indexes,kTRUE);
61   else{
62     TMath::Sort(entries,totSize.GetArray(),indexes,kTRUE);
63   }
64   Float_t zipBytes = fTree->GetZipBytes();
65   Float_t totBytes = fTree->GetTotBytes();
66   Float_t ratioT = 100.*zipBytes/totBytes;
67   for (Int_t i=entries-1; i>=0; i--){
68     Int_t ib = indexes[i];
69     Float_t ratio0= 100.*totSize[ib]/totBytes;
70     Float_t ratio1= 100.*zipSize[ib]/zipBytes;
71     if (i==0) 
72       printf("\n------------------------------------------------------------\n");
73
74     printf("%d  \t\t%5.f(%.2f\%)  \t\t%5.f(%.2f\%)  \t%.2f  \t%s\n",i, 
75            totSize[ib],ratio0, zipSize[ib],ratio1, 100.*zipRatio[ib], aReport.At(ib)->GetName());
76   }
77     
78
79 }
80
81
82 void AddToReport(const char * name, Float_t size[2], Float_t ratio){
83   //
84   // add branch info to array
85   //
86   aReport.AddLast(new TObjString(name));
87   Int_t entry = aReport.GetEntries();
88   if (totSize.GetSize()<entry){
89     totSize.Set(entry*2);
90     zipSize.Set(entry*2);
91     zipRatio.Set(entry*2);
92   }
93   totSize[entry-1]=Float_t(size[0]);
94   zipSize[entry-1]=Float_t(size[1]);
95   zipRatio[entry-1]=Float_t(ratio);
96 }
97
98
99
100
101
102 void MakeStat(const char *prefix, TBranch * branch, Float_t* size, Float_t mratio);
103
104
105
106 void MakeStat(TTree *tree, Bool_t zipSort){
107   //
108   // make recursve loop over tree branches
109   //
110   fTree= tree;
111   aReport.Clear();
112   TObjArray * array = tree->GetListOfBranches(); 
113   Float_t size[2]={0,0};
114   char * prefix ="";
115   Float_t mratio=tree->GetZipBytes()/float(tree->GetTotBytes());
116   for (Int_t i=0; i<array->GetEntries(); i++){
117     MakeStat(prefix,(TBranch*)array->At(i),size, mratio);
118   }  
119   Float_t ratio= (size[0]>0) ? size[1]/Float_t(size[0]): 0;
120   //  printf("Sum :\t%f\t%f\t%f\t%s.%s\t\n", float(size[0]), float(size[1]),ratio, prefix,tree->GetName());
121
122   AddToReport(tree->GetName(),size,ratio);
123   PrintSorted(zipSort);
124 }
125
126
127 void MakeStat(const char *prefix, TBranch * branch, Float_t *size, Float_t mratio){
128   //
129   // Recursive function to get size of the branches
130   // and ratios
131   //
132   TObjArray * array = branch->GetListOfBranches();
133   Float_t bsizeSum[2]={0,0};
134
135   if (!array || array->GetEntries()==0){
136     Float_t bsize[2] = {0,0};
137     bsize[0]=branch->GetTotalSize();
138     bsize[1]=branch->GetZipBytes();
139     if (bsize[1]>0){
140       Float_t ratio= (bsize[0]>0) ? bsize[1]/Float_t(bsize[0]): 0;
141       //      printf("Term:\t%f\t%f\t%f\t%s.%s\t\n",float(bsize[0]), float(bsize[1]),ratio, prefix,branch->GetName());
142       AddToReport(branch->GetName(),bsize,ratio);       
143       //branch->Print();
144       size[0]+=bsize[0];
145       size[1]+=bsize[1];
146     }else{
147       Float_t ratio= mratio;
148       //printf("Ter?:\t%f\t%f\t%f\t%s.%s\t\n",float(bsize[0]), float(-1),ratio, prefix,branch->GetName());
149       AddToReport(branch->GetName(),bsize,ratio);
150       //branch->Print();
151       size[0]+=bsize[0];
152       size[1]+=TMath::Nint(bsize[0]*mratio);
153     }
154
155     return;
156   }
157   for (Int_t i=0; i<array->GetEntries(); i++){
158     Float_t bsize[2] = {0,0};
159     TString str=prefix;
160     str+= branch->GetName();
161     MakeStat(str.Data(),(TBranch*)array->At(i), bsize, mratio);
162     bsizeSum[0]+=bsize[0];
163     bsizeSum[1]+=bsize[1];
164     size[0]+=bsize[0];
165     size[1]+=bsize[1];
166   } 
167   Float_t ratio= (size[0]>0) ? size[1]/Float_t(size[0]): 0;
168   //printf("Sum :\t%f\t%f\t%f\t%s.%s\t\n", float(bsizeSum[0]), float(bsizeSum[1]),ratio, prefix,branch->GetName()); 
169   AddToReport(branch->GetName(),bsizeSum,ratio);
170 }