8 def __init__(self, name, title, defaultFloatType="D", defaultIntType="I"):
11 self.
tree = TTree(name, title)
25 for branch
in tree.GetListOfBranches():
26 name = branch.GetName()
27 typeName = branch.GetListOfLeaves()[0].GetTypeName()
29 if typeName ==
'Int_t':
33 def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None):
34 """Backend function used to create scalar and vector branches.
35 Users should call "var" and "vector", not this function directly."""
36 if storageType ==
"default":
39 if storageType ==
"F":
40 selfmap[varName]=numpy.zeros(len,numpy.float32)
41 self.
tree.Branch(varName,selfmap[varName],varName+postfix+
'/F')
42 elif storageType ==
"D":
43 selfmap[varName]=numpy.zeros(len,numpy.float64)
44 self.
tree.Branch(varName,selfmap[varName],varName+postfix+
'/D')
46 raise RuntimeError(
'Unknown storage type %s for branch %s' % (storageType, varName))
58 if storageType
not in dtypes:
59 raise RuntimeError(
'Unknown storage type %s for branch %s' % (storageType, varName))
60 selfmap[varName]=numpy.zeros(len,dtypes[storageType])
61 self.
tree.Branch(varName,selfmap[varName],varName+postfix+
'/'+storageType)
63 raise RuntimeError(
'Unknown type %s for branch %s' % (type, varName))
65 self.
tree.GetBranch(varName).SetTitle(title)
67 def var(self, varName,type=float, default=-99, title=None, storageType="default", filler=None ):
68 if type
in [int, float]:
69 self.
branch_(self.
vars, varName, type, 1, title=title, storageType=storageType)
71 elif __builtins__[
'type'](type) == str:
73 self.
vars[varName] = getattr(ROOT,type)()
74 if type
in [
"TLorentzVector" ]:
75 self.
tree.Branch(varName+
".", type, self.
vars[varName], 8000,-1)
77 self.
tree.Branch(varName+
".", type, self.
vars[varName])
79 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)")
82 raise RuntimeError(
'Unknown type %s for branch %s: it is not int, float or a string' % (type, varName))
85 def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None ):
86 """either lenvar is a string, and maxlen an int (variable size array), or lenvar is an int and maxlen is not specified (fixed array)"""
87 if type
in [int, float]:
88 if __builtins__[
'type'](lenvar) == int:
89 self.
branch_(self.
vecvars, varName, type, lenvar, postfix=
"[%d]" % lenvar, title=title, storageType=storageType)
91 if maxlen ==
None: RuntimeError,
'You must specify a maxlen if making a dynamic array';
92 self.
branch_(self.
vecvars, varName, type, maxlen, postfix=
"[%s]" % lenvar, title=title, storageType=storageType)
93 elif __builtins__[
'type'](type) == str:
94 self.
vecvars[varName] = ROOT.TClonesArray(type,(lenvar
if __builtins__[
'type'](lenvar) == int
else maxlen))
95 if type
in [
"TLorentzVector" ]:
96 self.
tree.Branch(varName+
".", self.
vecvars[varName], 32000, -1)
98 self.
tree.Branch(varName+
".", self.
vecvars[varName])
100 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)")
105 for name,value
in six.iteritems(self.
vars):
110 for name,value
in six.iteritems(self.
vecvars):
111 if isinstance(value, numpy.ndarray):
114 if isinstance(value, ROOT.TObject)
and value.ClassName() ==
"TClonesArray":
115 value.ExpandCreateFast(0)
117 def fill(self, varName, value ):
118 if isinstance(self.
vars[varName], numpy.ndarray):
119 self.
vars[varName][0]=value
125 if isinstance(a, numpy.ndarray):
126 for (i,v)
in enumerate(values):
129 if isinstance(a, ROOT.TObject)
and a.ClassName() ==
"TClonesArray":
130 a.ExpandCreateFast(len(values))
132 for (i,v)
in enumerate(values):