7 def __init__(self, name, title, defaultFloatType="D", defaultIntType="I"):
10 self.
tree = TTree(name, title)
24 for branch
in tree.GetListOfBranches():
25 name = branch.GetName()
26 typeName = branch.GetListOfLeaves()[0].GetTypeName()
28 if typeName ==
'Int_t':
32 def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None):
33 """Backend function used to create scalar and vector branches.
34 Users should call "var" and "vector", not this function directly."""
35 if storageType ==
"default":
38 if storageType ==
"F":
39 selfmap[varName]=numpy.zeros(len,numpy.float32)
40 self.tree.Branch(varName,selfmap[varName],varName+postfix+
'/F')
41 elif storageType ==
"D":
42 selfmap[varName]=numpy.zeros(len,numpy.float64)
43 self.tree.Branch(varName,selfmap[varName],varName+postfix+
'/D')
45 raise RuntimeError(
'Unknown storage type %s for branch %s' % (storageType, varName))
57 if storageType
not in dtypes:
58 raise RuntimeError(
'Unknown storage type %s for branch %s' % (storageType, varName))
59 selfmap[varName]=numpy.zeros(len,dtypes[storageType])
60 self.tree.Branch(varName,selfmap[varName],varName+postfix+
'/'+storageType)
62 raise RuntimeError(
'Unknown type %s for branch %s' % (type, varName))
64 self.tree.GetBranch(varName).SetTitle(title)
66 def var(self, varName,type=float, default=-99, title=None, storageType="default", filler=None ):
67 if type
in [int, float]:
68 self.
branch_(self.
vars, varName, type, 1, title=title, storageType=storageType)
70 elif __builtins__[
'type'](type) == str:
72 self.
vars[varName] = getattr(ROOT,type)()
73 if type
in [
"TLorentzVector" ]:
74 self.tree.Branch(varName+
".", type, self.
vars[varName], 8000,-1)
76 self.tree.Branch(varName+
".", type, self.
vars[varName])
78 raise RuntimeError(
"Error: when brancing with an object, filler should be set to a function that takes as argument an object instance and a value, and set the instance to the value (as otherwise python assignment of objects changes the address as well)")
81 raise RuntimeError(
'Unknown type %s for branch %s: it is not int, float or a string' % (type, varName))
84 def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None ):
85 """either lenvar is a string, and maxlen an int (variable size array), or lenvar is an int and maxlen is not specified (fixed array)"""
86 if type
in [int, float]:
87 if __builtins__[
'type'](lenvar) == int:
88 self.
branch_(self.
vecvars, varName, type, lenvar, postfix=
"[%d]" % lenvar, title=title, storageType=storageType)
90 if maxlen ==
None: RuntimeError,
'You must specify a maxlen if making a dynamic array';
91 self.
branch_(self.
vecvars, varName, type, maxlen, postfix=
"[%s]" % lenvar, title=title, storageType=storageType)
92 elif __builtins__[
'type'](type) == str:
93 self.
vecvars[varName] = ROOT.TClonesArray(type,(lenvar
if __builtins__[
'type'](lenvar) == int
else maxlen))
94 if type
in [
"TLorentzVector" ]:
95 self.tree.Branch(varName+
".", self.
vecvars[varName], 32000, -1)
97 self.tree.Branch(varName+
".", self.
vecvars[varName])
99 raise RuntimeError(
"Error: when brancing with an object, filler should be set to a function that takes as argument an object instance and a value, and set the instance to the value (as otherwise python assignment of objects changes the address as well)")
104 for name,value
in self.vars.items():
109 for name,value
in self.vecvars.items():
110 if isinstance(value, numpy.ndarray):
113 if isinstance(value, ROOT.TObject)
and value.ClassName() ==
"TClonesArray":
114 value.ExpandCreateFast(0)
116 def fill(self, varName, value ):
117 if isinstance(self.
vars[varName], numpy.ndarray):
118 self.
vars[varName][0]=value
124 if isinstance(a, numpy.ndarray):
125 for (i,v)
in enumerate(values):
128 if isinstance(a, ROOT.TObject)
and a.ClassName() ==
"TClonesArray":
129 a.ExpandCreateFast(len(values))
131 for (i,v)
in enumerate(values):