# Tai Sakuma <tai.sakuma@cern.ch>
from ..mkdir_p import mkdir_p
from ..listToAlignedText import listToAlignedText
import os
import ROOT

##__________________________________________________________________||
class TblTreeEntries(object):
    def __init__(self, analyzerName, fileName, treeName, outPath, columnName = 'n'):
        self.analyzerName = analyzerName
        self.fileName = fileName
        self.treeName = treeName
        self.outPath = outPath
        self._rows = [['component', columnName]]

    def begin(self): pass

    def read(self, component):
        inputPath = os.path.join(getattr(component, self.analyzerName).path, self.fileName)
        file = ROOT.TFile.Open(inputPath)
        tree = file.Get(self.treeName)

        row = [component.name, tree.GetEntries()]
        self._rows.append(row)

    def end(self):
        f = self._open(self.outPath)
        f.write(listToAlignedText(self._rows))
        self._close(f)

    def _open(self, path):
        mkdir_p(os.path.dirname(path))
        return open(path, 'w')

    def _close(self, file): file.close()

##__________________________________________________________________||

@@ -299,29 +299,11 @@
 Path
-, columnName = 'n'
 ):%0A
+
     
@@ -458,16 +458,29 @@
 rows = %5B
+%0A            
 %5B'compon
@@ -489,18 +489,130 @@
 t', 
-columnName
+'n',%0A             'size', 'uncompressed_size', 'compression_factor',%0A             'analyzer', 'file', 'tree',%0A            
 %5D%5D%0A%0A
@@ -855,48 +855,708 @@
     
-row = %5Bcomponent.name, tree.GetEntries()
+size = tree.GetDirectory().GetKey(tree.GetName()).GetNbytes()%0A        size += tree.GetZipBytes()%0A        size /= 1024.0*1024.0 # MB%0A        uncompressed_size = tree.GetDirectory().GetKey(tree.GetName()).GetKeylen()%0A        uncompressed_size += tree.GetTotBytes()%0A        uncompressed_size /= 1024.0*1024.0 # MB%0A        compression_factor = uncompressed_size/size if size %3E 0 else 0%0A%0A        size = round(size, 3)%0A        uncompressed_size = round(uncompressed_size, 3)%0A        compression_factor = round(compression_factor, 3)%0A%0A        row = %5Bcomponent.name, tree.GetEntries(),%0A               size, uncompressed_size, compression_factor,%0A               self.analyzerName, self.fileName, self.treeName
 %5D%0A  

add sizes to TblTreeEntries