__author__ = 'marcusmorgenstern'
__mail__ = ''

import os
from ROOT import TFile


class FileHandle(object):
    def __init__(self, fName, sPath = './'):
        self.fName = fName
        self.path = sPath
        self.absFName = os.path.join(self.path, self.fName)
        self.open()

    def open(self):
        if not os.path.exists(self.absFName):
            raise ValueError("File " + os.path.join(self.path, self.fName) + " does not exist.")

        self.tfile = TFile.Open(os.path.join(self.path, self.fName), 'READ')

    def get_objects(self):
        objects = []
        for obj in self.tfile.GetListOfKeys():
            objects.append(self.tfile.Get(obj.GetName()))
        return objects

    def get_objects_by_type(self, typename):
        obj = self.get_objects()
        obj = filter(lambda t: t.InheritsFrom(typename), obj)
        return obj

    def get_object_by_name(self, obj_name):
        obj = self.tfile.Get(obj_name)
        if not obj.__nonzero__():
            raise ValueError("Object " + obj_name + " does not exist in file " + os.path.join(self.path, self.fName))
        self.release_object_from_file(obj)
        return obj

    def release_object_from_file(self, obj):
        obj.SetDirectory(0)

@@ -74,17 +74,595 @@
 t TFile%0A
-%0A
+from PyAnalysisTools.base import _logger, InvalidInputError%0A%0A_memoized = %7B%7D%0A%0A%0Adef get_id_tuple(f, args, kwargs, mark=object()):%0A    l = %5Bid(f)%5D%0A    for arg in args:%0A        l.append(id(arg))%0A    l.append(id(mark))%0A    for k, v in kwargs:%0A        l.append(k)%0A        l.append(id(v))%0A    return tuple(l)%0A%0A%0Adef memoize(f):%0A    %22%22%22%0A    Some basic memoizer%0A    %22%22%22%0A    def memoized(*args, **kwargs):%0A        key = get_id_tuple(f, args, kwargs)%0A        if key not in _memoized:%0A            _memoized%5Bkey%5D = f(*args, **kwargs)%0A        return _memoized%5Bkey%5D%0A    return memoized%0A%0A@memoize
 %0Aclass F
@@ -708,22 +708,23 @@
 f, f
-N
+ile_n
 ame, 
-sPath = 
+path=
 './'
@@ -744,17 +744,25 @@
 lf.f
-N
+ile_n
 ame = f
-N
+ile_n
 ame%0A
@@ -785,10 +785,9 @@
 h = 
-sP
+p
 ath%0A
@@ -836,25 +836,29 @@
 path, self.f
-N
+ile_n
 ame)%0A       
@@ -869,16 +869,60 @@
 f.open()
+%0A        self.process = self.parse_process()
 %0A%0A    de
@@ -1046,25 +1046,29 @@
 path, self.f
-N
+ile_n
 ame) + %22 doe
@@ -1141,25 +1141,29 @@
 path, self.f
-N
+ile_n
 ame), 'READ'
@@ -1165,16 +1165,105 @@
 READ')%0A%0A
+    def parse_process(self):%0A        return self.file_name.split(%22-%22)%5B-1%5D.split(%22.%22)%5B0%5D%0A%0A
     def 
@@ -1819,17 +1819,21 @@
 , self.f
-N
+ile_n
 ame))%0A  
@@ -1834,24 +1834,25 @@
 e))%0A        
+#
 self.release
@@ -1902,16 +1902,1068 @@
     def 
+fetch_and_link_hist_to_tree(self, tree_name, hist, var_name, cut_string=%22%22):%0A        tree = self.get_object_by_name(tree_name)%0A        _logger.debug(%22Parsed tree %25s from file %25s containing %25i entries%22 %25 (tree_name, self.file_name, tree.GetEntries()))%0A        if cut_string is None:%0A            cut_string = %22%22%0A        n_selected_events = tree.Project(hist.GetName(), var_name, cut_string)%0A        _logger.debug(%22Selected %25i events from tree %25s for distribution %25s and cut %25s.%22 %25(n_selected_events,%0A                                                                                          tree_name,%0A                                                                                          var_name,%0A                                                                                          cut_string))%0A        if n_selected_events == -1:%0A            _logger.error(%22Unable to project %25s from tree %25s with cut %25s%22 %25 (var_name, tree_name, cut_string))%0A            raise RuntimeError(%22TTree::Project failed%22)%0A        return hist%0A%0A    @staticmethod%0A    def 
 release_
@@ -2979,22 +2979,16 @@
 om_file(
-self, 
 obj):%0A  

implement process parsing