CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
autovars.py
Go to the documentation of this file.
1 #!/bin/env python
2 #
3 # Objects to be used with AutoFillTreeProducer
4 #
5 # the variable declaration contains both the booking information and a function to fill the variable
6 #
7 # TODO: more documentation needed here!
8 
10  def __init__(self, name, function, type=float, help="", default=-99, mcOnly=False, filler=None):
11  self.name = name
12  self.function = function
13  self.type = type
14  self.help = help
15  self.default = default
16  self.mcOnly = mcOnly
17  self.filler = filler
18  def __call__(self,object):
19  ret = self.function(object)
20  return ret
21  def makeBranch(self,treeNumpy,isMC):
22  if self.mcOnly and not isMC: return
23  treeNumpy.var(self.name, type=self.type, default=self.default, title=self.help, filler=self.filler)
24  def fillBranch(self,treeNumpy,object,isMC):
25  if self.mcOnly and not isMC: return
26  treeNumpy.fill(self.name, self(object))
27 
29  def __init__(self,name,baseObjectTypes=[],mcOnly=[],variables=[]):
30  self.name = name
31  self.baseObjectTypes = baseObjectTypes
32  self.mcOnly = mcOnly
33  self.variables = variables
34  def ownVars(self,isMC):
35  """Return only my vars, not including the ones from the bases"""
36  return [ v for v in self.variables if (isMC or not v.mcOnly) ]
37  def allVars(self,isMC):
38  """Return all vars, including the base ones. Duplicate bases are not added twice"""
39  ret = []; names = {}
40  if not isMC and self.mcOnly: return []
41  for base in self.allBases():
42  if not isMC and base.mcOnly: continue
43  for var in base.ownVars(isMC):
44  if var.name in names: raise RuntimeError, "Duplicate definition of variable %s from %s and %s" % (var.name, base.name, names[var.name])
45  names[var.name] = base.name
46  ret.append(var)
47  for var in self.ownVars(isMC):
48  if var.name in names: raise RuntimeError, "Duplicate definition of variable %s from %s and %s" % (var.name, self.name, names[var.name])
49  names[var.name] = self.name
50  ret.append(var)
51  return ret
52  def allBases(self):
53  ret = []
54  for b in self.baseObjectTypes:
55  if b not in ret:
56  ret.append(b)
57  for b2 in b.allBases():
58  if b2 not in ret:
59  ret.append(b2)
60  return ret
61  def removeVariable(self,name):
62  self.variables = [ v for v in self.variables if v.name != name]
63 
65  def __init__(self, name, objectType, help="", mcOnly=False):
66  self.name = name
67  self.objectType = objectType
68  self.mcOnly = mcOnly
69  self.help = ""
70  def makeBranches(self,treeNumpy,isMC):
71  if not isMC and self.mcOnly: return
72  allvars = self.objectType.allVars(isMC)
73  for v in allvars:
74  h = v.help
75  if self.help: h = "%s for %s" % ( h if h else v.name, self.help )
76  treeNumpy.var("%s_%s" % (self.name, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
77  def fillBranches(self,treeNumpy,object,isMC):
78  if self.mcOnly and not isMC: return
79  allvars = self.objectType.allVars(isMC)
80  for v in allvars:
81  treeNumpy.fill("%s_%s" % (self.name, v.name), v(object))
82 
83 
85  def __init__(self, name, objectType, maxlen, help="", mcOnly=False, sortAscendingBy=None, sortDescendingBy=None, filter=None):
86  self.name = name
87  self.objectType = objectType
88  self.maxlen = maxlen
89  self.help = help
90  if objectType.mcOnly and mcOnly == False:
91  print "collection %s is set to mcOnly since the type %s is mcOnly" % (name, objectType.name)
92  mcOnly = True
93  self.mcOnly = mcOnly
94  if sortAscendingBy != None and sortDescendingBy != None:
95  raise RuntimeError, "Cannot specify two sort conditions"
96  self.filter = filter
97  self.sortAscendingBy = sortAscendingBy
98  self.sortDescendingBy = sortDescendingBy
99  def makeBranchesScalar(self,treeNumpy,isMC):
100  if not isMC and self.objectType.mcOnly: return
101  treeNumpy.var("n"+self.name, int)
102  allvars = self.objectType.allVars(isMC)
103  for v in allvars:
104  for i in xrange(1,self.maxlen+1):
105  h = v.help
106  if self.help: h = "%s for %s [%d]" % ( h if h else v.name, self.help, i-1 )
107  treeNumpy.var("%s%d_%s" % (self.name, i, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
108  def makeBranchesVector(self,treeNumpy,isMC):
109  if not isMC and self.objectType.mcOnly: return
110  treeNumpy.var("n"+self.name, int)
111  allvars = self.objectType.allVars(isMC)
112  for v in allvars:
113  h = v.help
114  if self.help: h = "%s for %s" % ( h if h else v.name, self.help )
115  name="%s_%s" % (self.name, v.name) if v.name != "" else self.name
116  treeNumpy.vector(name, "n"+self.name, self.maxlen, type=v.type, default=v.default, title=h, filler=v.filler)
117  def fillBranchesScalar(self,treeNumpy,collection,isMC):
118  if not isMC and self.objectType.mcOnly: return
119  if self.filter != None: collection = [ o for o in collection if self.filter(o) ]
120  if self.sortAscendingBy != None: collection = sorted(collection, key=self.sortAscendingBy)
121  if self.sortDescendingBy != None: collection = sorted(collection, key=self.sortDescendingBy, reverse=True)
122  num = min(self.maxlen,len(collection))
123  treeNumpy.fill("n"+self.name, num)
124  allvars = self.objectType.allVars(isMC)
125  for i in xrange(num):
126  o = collection[i]
127  for v in allvars:
128  treeNumpy.fill("%s%d_%s" % (self.name, i+1, v.name), v(o))
129  def fillBranchesVector(self,treeNumpy,collection,isMC):
130  if not isMC and self.objectType.mcOnly: return
131  if self.filter != None: collection = [ o for o in collection if self.filter(o) ]
132  if self.sortAscendingBy != None: collection = sorted(collection, key=self.sortAscendingBy)
133  if self.sortDescendingBy != None: collection = sorted(collection, key=self.sortDescendingBy, reverse=True)
134  num = min(self.maxlen,len(collection))
135  treeNumpy.fill("n"+self.name, num)
136  allvars = self.objectType.allVars(isMC)
137  for v in allvars:
138  name="%s_%s" % (self.name, v.name) if v.name != "" else self.name
139  treeNumpy.vfill(name, [ v(collection[i]) for i in xrange(num) ])
140 
141 
T min(T a, T b)
Definition: MathUtil.h:58