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():
95 return "<NTupleObjectType[%s]>" % self.
name
101 """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"""
102 def __init__(self,name,function,objectType,mcOnly=False):
111 """Type defining a set of branches associated to a single object (i.e. an instance of NTupleObjectType)"""
112 def __init__(self, name, objectType, help="", mcOnly=False):
118 if not isMC
and self.
mcOnly:
return
119 allvars = self.objectType.allVars(isMC)
122 if self.
help: h =
"%s for %s" % ( h
if h
else v.name, self.
help )
123 treeNumpy.var(
"%s_%s" % (self.
name, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
125 if self.
mcOnly and not isMC:
return
126 allvars = self.objectType.allVars(isMC)
128 treeNumpy.fill(
"%s_%s" % (self.
name, v.name),
v(object))
130 return "<NTupleObject[%s]>" % self.
name
134 """Type defining a set of branches associated to a list of objects (i.e. an instance of NTupleObjectType)"""
135 def __init__(self, name, objectType, maxlen, help="", mcOnly=False, sortAscendingBy=None, sortDescendingBy=None, filter=None):
140 if objectType.mcOnly
and mcOnly ==
False:
144 if sortAscendingBy !=
None and sortDescendingBy !=
None:
145 raise RuntimeError,
"Cannot specify two sort conditions"
150 if not isMC
and self.objectType.mcOnly:
return
151 treeNumpy.var(
"n"+self.
name, int)
152 allvars = self.objectType.allVars(isMC)
154 for i
in xrange(1,self.
maxlen+1):
156 if self.
help: h =
"%s for %s [%d]" % ( h
if h
else v.name, self.
help, i-1 )
157 treeNumpy.var(
"%s%d_%s" % (self.
name, i, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
159 if not isMC
and self.objectType.mcOnly:
return
160 treeNumpy.var(
"n"+self.
name, int)
161 allvars = self.objectType.allVars(isMC)
164 if self.
help: h =
"%s for %s" % ( h
if h
else v.name, self.
help )
165 name=
"%s_%s" % (self.
name, v.name)
if v.name !=
"" else self.
name
166 treeNumpy.vector(name,
"n"+self.
name, self.
maxlen, type=v.type, default=v.default, title=h, filler=v.filler)
168 if not isMC
and self.objectType.mcOnly:
return
169 if self.
filter !=
None: collection = [ o
for o
in collection
if self.
filter(o) ]
173 treeNumpy.fill(
"n"+self.
name, num)
174 allvars = self.objectType.allVars(isMC)
175 for i
in xrange(num):
178 treeNumpy.fill(
"%s%d_%s" % (self.
name, i+1, v.name),
v(o))
180 if not isMC
and self.objectType.mcOnly:
return
181 if self.
filter !=
None: collection = [ o
for o
in collection
if self.
filter(o) ]
185 treeNumpy.fill(
"n"+self.
name, num)
186 allvars = self.objectType.allVars(isMC)
188 name=
"%s_%s" % (self.
name, v.name)
if v.name !=
"" else self.
name
189 treeNumpy.vfill(name, [
v(collection[i])
for i
in xrange(num) ])
191 return "<NTupleCollection[%s]>" % self.
name
195 for v
in self.objectType.allVars(isMC):
196 s += [
"{0} {1}__{2}[{3}];".
format(v.type.__name__, self.
name, v.name, self.
maxlen)]
200 s =
"class %s {\n" % self.
name
202 for v
in self.objectType.allVars(isMC):
203 s +=
" {0} {1};\n".
format(v.type.__name__, v.name)
208 s =
"class %s:\n" % self.
name
209 s +=
" def __init__(self, tree, n):\n"
210 for v
in self.objectType.allVars(isMC):
212 s +=
" self.{0} = tree.{1}_{2}[n];\n".
format(v.name, self.
name, v.name)
214 s +=
" self.{0} = tree.{0}[n];\n".
format(self.
name)
216 s +=
" @staticmethod\n"
217 s +=
" def make_array(event):\n"
218 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