1 from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy
import TreeAnalyzerNumpy
2 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
13 def __init__(self, cfg_ana, cfg_comp, looperName):
14 super(AutoFillTreeProducer,self).
__init__(cfg_ana, cfg_comp, looperName)
17 self.
scalar =
not self.cfg_ana.vectorTree
20 if not getattr(self.cfg_ana,
'saveTLorentzVectors',
False):
21 fourVectorType.removeVariable(
"p4")
27 if hasattr(cfg_ana,
"collections"):
29 if hasattr(cfg_ana,
"globalObjects"):
31 if hasattr(cfg_ana,
"globalVariables"):
35 super(AutoFillTreeProducer, self).
beginLoop(setup)
40 self.mchandles[
'GenInfo'] = AutoHandle( (
'generator',
'',
''),
'GenEventInfoProduct' )
41 for k,v
in self.collections.iteritems():
42 if type(v) == tuple
and isinstance(v[0], AutoHandle):
43 self.handles[k] = v[0]
46 """Here we declare the variables that we always want and that are hard-coded"""
47 tr.var(
'run', int, storageType=
"i")
48 tr.var(
'lumi', int, storageType=
"i")
49 tr.var(
'evt', int, storageType=
"i")
67 tr.var(
"nTrueInt",int)
72 if hasattr(self.cfg_ana,
"PDFWeights")
and len(self.cfg_ana.PDFWeights) > 0:
73 self.pdfWeights = self.cfg_ana.PDFWeights
74 for (pdf,nvals)
in self.pdfWeights:
76 for i
in range(nvals): tr.var(
'pdfWeight_%s_%d' % (pdf,i))
78 tr.vector(
'pdfWeight_%s' % pdf, nvals)
81 isMC = self.cfg_comp.isMC
85 if not hasattr(self.cfg_ana,
"ignoreAnalyzerBookings")
or not self.cfg_ana.ignoreAnalyzerBookings :
87 if hasattr(setup,
"globalVariables"):
89 if hasattr(setup,
"globalObjects"):
90 self.globalObjects.update(setup.globalObjects)
91 if hasattr(setup,
"collections"):
92 self.collections.update(setup.collections)
95 v.makeBranch(tree, isMC)
96 for o
in self.globalObjects.itervalues():
97 o.makeBranches(tree, isMC)
98 for c
in self.collections.itervalues():
99 if type(c) == tuple: c = c[-1]
101 c.makeBranchesScalar(tree, isMC)
103 c.makeBranchesVector(tree, isMC)
106 """Here we fill the variables that we always want and that are hard-coded"""
107 tr.fill(
'run', event.input.eventAuxiliary().id().
run())
108 tr.fill(
'lumi',event.input.eventAuxiliary().id().luminosityBlock())
109 tr.fill(
'evt', event.input.eventAuxiliary().id().
event())
110 tr.fill(
'isData', 0
if isMC
else 1)
118 tr.fill(
'xsec', getattr(self.cfg_comp,
'xSection',1.0))
120 if hasattr(event,
"nPU"):
121 tr.fill(
"nTrueInt", event.nPU)
122 tr.fill(
"puWeight", event.eventWeight)
124 tr.fill(
"nTrueInt", -1)
125 tr.fill(
"puWeight", 1.0)
127 tr.fill(
"genWeight", self.mchandles[
'GenInfo'].product().
weight())
129 if hasattr(event,
"pdfWeights") :
130 for (pdf,nvals)
in self.pdfWeights:
131 if len(event.pdfWeights[pdf]) != nvals:
132 raise RuntimeError,
"PDF lenght mismatch for %s, declared %d but the event has %d" % (pdf,nvals,event.pdfWeights[pdf])
134 for i,w
in enumerate(event.pdfWeights[pdf]):
135 tr.fill(
'pdfWeight_%s_%d' % (pdf,i), w)
137 tr.vfill(
'pdfWeight_%s' % pdf, event.pdfWeights[pdf])
140 if hasattr(self.cfg_ana,
"filter") :
141 if not self.cfg_ana.filter(event) :
143 self.readCollections( event.input)
147 isMC = self.cfg_comp.isMC
148 if resetFirst: self.tree.reset()
153 if not isMC
and v.mcOnly:
continue
154 v.fillBranch(self.tree, event, isMC)
156 for on, o
in self.globalObjects.iteritems():
157 if not isMC
and o.mcOnly:
continue
158 o.fillBranches(self.tree, getattr(event, on), isMC)
160 for cn, c
in self.collections.iteritems():
161 if type(c) == tuple
and isinstance(c[0], AutoHandle):
162 if not isMC
and c[-1].mcOnly:
continue
163 objects = self.handles[cn].product()
164 setattr(event, cn, [objects[i]
for i
in xrange(objects.size())])
166 if not isMC
and c.mcOnly:
continue
168 c.fillBranchesScalar(self.tree, getattr(event, cn), isMC)
170 c.fillBranchesVector(self.tree, getattr(event, cn), isMC)
172 self.tree.tree.Fill()
176 This function produces a string that contains a Python wrapper for the event.
177 The wrapper is automatically generated based on the collections and allows the full
178 event contents to be accessed from subsequent Analyzers using e.g.
180 leps = event.selLeptons #is of type selLeptons
183 One just needs to add the EventAnalyzer to the sequence.
186 isMC = self.cfg_comp.isMC
190 anclass +=
"from PhysicsTools.HeppyCore.framework.analyzer import Analyzer\n"
191 anclass +=
"class EventAnalyzer(Analyzer):\n"
192 anclass +=
" def __init__(self, cfg_ana, cfg_comp, looperName):\n"
193 anclass +=
" super(EventAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)\n"
195 anclass +=
" def process(self, event):\n"
197 for cname, coll
in self.collections.items():
198 classes += coll.get_py_wrapper_class(isMC)
199 anclass +=
" event.{0} = {0}.make_array(event)\n".
format(coll.name)
201 return classes +
"\n" + anclass
def declareVariables
cross section
scalar
Read whether we want vectors or flat trees.
collections
Read whether we want 4-vectors.