# Tai Sakuma <tai.sakuma@cern.ch>
from Branch import Branch
from Events import Events
from BranchAddressManager import BranchAddressManager

##____________________________________________________________________________||
branchAddressManager = BranchAddressManager()

##____________________________________________________________________________||
class BEvents(Events):
    def __init__(self, tree, maxEvents = -1):
        super(BEvents, self).__init__(tree, maxEvents)
        tree.SetBranchStatus('*', 0)
        self.branches = { }

    def __getattr__(self, name):
        if name in self.branches: return self.branches[name]
        itsArray, itsCountArray = branchAddressManager.getArrays(self.tree, name)
        if itsArray is None: raise AttributeError("'" + str(self) + "' has no attribute '" + name + "'")
        self.branches[name] = Branch(name, itsArray, itsCountArray)
        if self.iEvent >= 0: self.tree.GetEntry(self.iEvent)
        return self.branches[name]

##____________________________________________________________________________||

@@ -132,16 +132,88 @@
 sManager
+%0Afrom BranchAddressManagerForVector import BranchAddressManagerForVector
 %0A%0A##____
@@ -332,16 +332,80 @@
 anager()
+%0AbranchAddressManagerForVector = BranchAddressManagerForVector()
 %0A%0A##____
@@ -775,64 +775,34 @@
     
-itsArray, itsCountArray = branchAddressManager.getArrays
+branch = self._buildBranch
 (sel
@@ -826,24 +826,22 @@
      if 
-itsArray
+branch
  is None
@@ -952,45 +952,14 @@
 %5D = 
-B
+b
 ranch
-(name, itsArray, itsCountArray)
 %0A   
@@ -1052,16 +1052,439 @@
 %5Bname%5D%0A%0A
+    def _buildBranch(self, tree, name):%0A        itsArray, itsCountArray = branchAddressManager.getArrays(tree, name)%0A        if itsArray is not None:%0A            branch = Branch(name, itsArray, itsCountArray)%0A            return branch%0A        itsVector = branchAddressManagerForVector.getVector(tree, name)%0A        if itsVector is not None:%0A            return itsVector # this can be used at a branch%0A        return None%0A%0A%0A
 ##______

use BranchAddressManagerForVector in BEvents