10 """Branch containing an individual variable (either of the event or of an object), created with a name and a function to compute it
11 - name, type, help, default: obvious
12 - function: a function that taken an object computes the value to fill (e.g. lambda event : len(event.goodVertices))
14 def __init__(self, name, function, type=float, help="", default=-99, mcOnly=False, filler=None):
26 if self.
mcOnly and not isMC:
return
29 if self.
mcOnly and not isMC:
return
30 treeNumpy.fill(self.
name, self(object))
32 return "<NTupleVariable[%s]>" % self.
name
36 """Type defining a collection of variables associated to a single object. Contans NTupleVariable and NTupleSubObject"""
37 def __init__(self,name,baseObjectTypes=[],mcOnly=[],variables=[]):
44 if issubclass(v.__class__,NTupleSubObject):
45 self.subObjects.append(v)
47 self.variables.append(v)
50 """Return only my vars, not including the ones from the bases"""
51 vars = [ v
for v
in self.
variables if (isMC
or not v.mcOnly) ]
56 if so.mcOnly
and not isMC:
continue
57 for subvar
in so.objectType.allVars(isMC):
60 lambda object, subvar=subvar, so=so : subvar(so(object)),
63 type = subvar.type, help = subvar.help, default = subvar.default, mcOnly = subvar.mcOnly,
64 filler = subvar.filler))
69 """Return all vars, including the base ones. Duplicate bases are not added twice"""
71 if not isMC
and self.
mcOnly:
return []
73 if not isMC
and base.mcOnly:
continue
74 for var
in base.ownVars(isMC):
75 if var.name
in names:
raise RuntimeError(
"Duplicate definition of variable %s from %s and %s" % (var.name, base.name, names[var.name]))
76 names[var.name] = base.name
79 if var.name
in names:
raise RuntimeError(
"Duplicate definition of variable %s from %s and %s" % (var.name, self.
name, names[var.name]))
80 names[var.name] = self.
name
88 for b2
in b.allBases():
93 currentnames = [v.name
for v
in self.
allVars(
True)]
96 if var.name
in uniquenewvars:
raise RuntimeError(
"Duplicate definition of variable %s while adding variables to object type %s" % (var.name,self.
name))
97 uniquenewvars.append(var.name)
98 if var.name
not in currentnames:
99 self.variables.append(var)
101 raise RuntimeError(
"Variable %s is already present in object type %s" % (var.name,self.
name))
103 currentnames = [v.name
for v
in self.
subObjects]
106 if ob.name
in uniquenewobjs:
raise RuntimeError(
"Duplicate definition of sub-object %s while adding it to object type %s" % (ob.name,self.
name))
107 uniquenewobjs.append(ob.name)
108 if ob.name
not in currentnames:
109 self.subObjects.append(ob)
111 raise RuntimeError(
"Sub-object %s is already present in object type %s" % (ob.name,self.
name))
112 self._subObjectVars.clear()
114 mynewvars = self.
allVars(
False)
118 return "<NTupleObjectType[%s]>" % self.
name
124 """Type to add a sub-object within an NTupleObjectType, given a name (used as prefix), a function to extract the sub-object and NTupleObjectType to define tye type"""
125 def __init__(self,name,function,objectType,mcOnly=False):
134 """Type defining a set of branches associated to a single object (i.e. an instance of NTupleObjectType)"""
135 def __init__(self, name, objectType, help="", mcOnly=False):
141 if not isMC
and self.
mcOnly:
return
142 allvars = self.objectType.allVars(isMC)
145 if self.
help: h =
"%s for %s" % ( h
if h
else v.name, self.
help )
146 treeNumpy.var(
"%s_%s" % (self.
name, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
148 if self.
mcOnly and not isMC:
return
149 allvars = self.objectType.allVars(isMC)
151 treeNumpy.fill(
"%s_%s" % (self.
name, v.name),
v(object))
153 return "<NTupleObject[%s]>" % self.
name
157 """Type defining a set of branches associated to a list of objects (i.e. an instance of NTupleObjectType)"""
158 def __init__(self, name, objectType, maxlen, help="", mcOnly=False, sortAscendingBy=None, sortDescendingBy=None, filter=None):
163 if objectType.mcOnly
and mcOnly ==
False:
167 if sortAscendingBy !=
None and sortDescendingBy !=
None:
168 raise RuntimeError(
"Cannot specify two sort conditions")
173 if not isMC
and self.objectType.mcOnly:
return
174 treeNumpy.var(
"n"+self.
name, int)
175 allvars = self.objectType.allVars(isMC)
177 for i
in xrange(1,self.
maxlen+1):
179 if self.
help: h =
"%s for %s [%d]" % ( h
if h
else v.name, self.
help, i-1 )
180 treeNumpy.var(
"%s%d_%s" % (self.
name, i, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
182 if not isMC
and self.objectType.mcOnly:
return
183 treeNumpy.var(
"n"+self.
name, int)
184 allvars = self.objectType.allVars(isMC)
187 if self.
help: h =
"%s for %s" % ( h
if h
else v.name, self.
help )
188 name=
"%s_%s" % (self.
name, v.name)
if v.name !=
"" else self.
name
189 treeNumpy.vector(name,
"n"+self.
name, self.
maxlen, type=v.type, default=v.default, title=h, filler=v.filler)
191 if not isMC
and self.objectType.mcOnly:
return
192 if self.
filter !=
None: collection = [ o
for o
in collection
if self.
filter(o) ]
196 treeNumpy.fill(
"n"+self.
name, num)
197 allvars = self.objectType.allVars(isMC)
198 for i
in xrange(num):
201 treeNumpy.fill(
"%s%d_%s" % (self.
name, i+1, v.name),
v(o))
203 if not isMC
and self.objectType.mcOnly:
return
204 if self.
filter !=
None: collection = [ o
for o
in collection
if self.
filter(o) ]
208 treeNumpy.fill(
"n"+self.
name, num)
209 allvars = self.objectType.allVars(isMC)
211 name=
"%s_%s" % (self.
name, v.name)
if v.name !=
"" else self.
name
212 treeNumpy.vfill(name, [
v(collection[i])
for i
in xrange(num) ])
214 return "<NTupleCollection[%s]>" % self.
name
218 for v
in self.objectType.allVars(isMC):
219 s += [
"{0} {1}__{2}[{3}];".
format(v.type.__name__, self.
name, v.name, self.
maxlen)]
223 s =
"class %s {\n" % self.
name
225 for v
in self.objectType.allVars(isMC):
226 s +=
" {0} {1};\n".
format(v.type.__name__, v.name)
231 s =
"class %s:\n" % self.
name
232 s +=
" def __init__(self, tree, n):\n"
233 for v
in self.objectType.allVars(isMC):
235 s +=
" self.{0} = tree.{1}_{2}[n];\n".
format(v.name, self.
name, v.name)
237 s +=
" self.{0} = tree.{0}[n];\n".
format(self.
name)
239 s +=
" @staticmethod\n"
240 s +=
" def make_array(event):\n"
241 s +=
" return [{0}(event.input, i) for i in range(event.input.n{0})]\n".
format(self.
name)
static std::string join(char **cmd)
def get_cpp_wrapper_class