1 from builtins
import range
2 from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy
import TreeAnalyzerNumpy
3 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
15 def __init__(self, cfg_ana, cfg_comp, looperName):
16 super(AutoFillTreeProducer,self).
__init__(cfg_ana, cfg_comp, looperName)
19 self.
scalar =
not self.cfg_ana.vectorTree
22 if not getattr(self.cfg_ana,
'saveTLorentzVectors',
False):
23 fourVectorType.removeVariable(
"p4")
29 if hasattr(cfg_ana,
"collections"):
31 if hasattr(cfg_ana,
"globalObjects"):
33 if hasattr(cfg_ana,
"globalVariables"):
37 super(AutoFillTreeProducer, self).
beginLoop(setup)
42 self.mchandles[
'GenInfo'] = AutoHandle( (
'generator',
'',
''),
'GenEventInfoProduct' )
44 if isinstance(v, tuple)
and isinstance(v[0], AutoHandle):
45 self.handles[k] = v[0]
48 """Here we declare the variables that we always want and that are hard-coded"""
49 tr.var(
'run', int, storageType=
"i")
50 tr.var(
'lumi', int, storageType=
"i")
51 tr.var(
'evt', int, storageType=
"l")
64 tr.var(
'intLumi', int, storageType=
"i")
77 if hasattr(self.cfg_ana,
"PDFWeights")
and len(self.cfg_ana.PDFWeights) > 0:
81 for i
in range(nvals): tr.var(
'pdfWeight_%s_%d' % (pdf,i))
83 tr.vector(
'pdfWeight_%s' % pdf, nvals)
86 isMC = self.cfg_comp.isMC
90 if not hasattr(self.cfg_ana,
"ignoreAnalyzerBookings")
or not self.cfg_ana.ignoreAnalyzerBookings :
92 if hasattr(setup,
"globalVariables"):
94 if hasattr(setup,
"globalObjects"):
96 if hasattr(setup,
"collections"):
100 v.makeBranch(tree, isMC)
102 o.makeBranches(tree, isMC)
104 if isinstance(c, tuple): c = c[-1]
106 c.makeBranchesScalar(tree, isMC)
108 c.makeBranchesVector(tree, isMC)
111 """Here we fill the variables that we always want and that are hard-coded"""
112 tr.fill(
'run', event.input.eventAuxiliary().
id().
run())
113 tr.fill(
'lumi',event.input.eventAuxiliary().
id().luminosityBlock())
114 tr.fill(
'evt', event.input.eventAuxiliary().
id().
event())
115 tr.fill(
'isData', 0
if isMC
else 1)
122 tr.fill(
'intLumi', getattr(self.cfg_comp,
'intLumi',1.0))
126 tr.fill(
'xsec', getattr(self.cfg_comp,
'xSection',1.0))
128 if hasattr(event,
"nPU"):
129 tr.fill(
"nTrueInt", event.nPU)
130 tr.fill(
"puWeight", event.puWeight)
132 tr.fill(
"nTrueInt", -1)
133 tr.fill(
"puWeight", 1.0)
135 tr.fill(
"genWeight", self.mchandles[
'GenInfo'].product().
weight())
137 if hasattr(event,
"pdfWeights") :
139 if len(event.pdfWeights[pdf]) != nvals:
140 raise RuntimeError(
"PDF lenght mismatch for %s, declared %d but the event has %d" % (pdf,nvals,event.pdfWeights[pdf]))
142 for i,w
in enumerate(event.pdfWeights[pdf]):
143 tr.fill(
'pdfWeight_%s_%d' % (pdf,i), w)
145 tr.vfill(
'pdfWeight_%s' % pdf, event.pdfWeights[pdf])
148 if hasattr(self.cfg_ana,
"filter") :
149 if not self.cfg_ana.
filter(event) :
151 self.readCollections( event.input)
155 isMC = self.cfg_comp.isMC
156 if resetFirst: self.tree.
reset()
161 if not isMC
and v.mcOnly:
continue
162 v.fillBranch(self.tree, event, isMC)
165 if not isMC
and o.mcOnly:
continue
166 o.fillBranches(self.tree, getattr(event, on), isMC)
169 if isinstance(c, tuple)
and isinstance(c[0], AutoHandle):
170 if not isMC
and c[-1].mcOnly:
continue
171 objects = self.handles[cn].product()
172 setattr(event, cn, [objects[i]
for i
in range(objects.size())])
174 if not isMC
and c.mcOnly:
continue
176 c.fillBranchesScalar(self.tree, getattr(event, cn), isMC)
178 c.fillBranchesVector(self.tree, getattr(event, cn), isMC)
180 self.tree.tree.Fill()
184 This function produces a string that contains a Python wrapper for the event.
185 The wrapper is automatically generated based on the collections and allows the full
186 event contents to be accessed from subsequent Analyzers using e.g.
188 leps = event.selLeptons #is of type selLeptons
191 One just needs to add the EventAnalyzer to the sequence.
194 isMC = self.cfg_comp.isMC
198 anclass +=
"from PhysicsTools.HeppyCore.framework.analyzer import Analyzer\n"
199 anclass +=
"class EventAnalyzer(Analyzer):\n"
200 anclass +=
" def __init__(self, cfg_ana, cfg_comp, looperName):\n"
201 anclass +=
" super(EventAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)\n"
203 anclass +=
" def process(self, event):\n"
206 classes += coll.get_py_wrapper_class(isMC)
207 anclass +=
" event.{0} = {0}.make_array(event)\n".
format(coll.name)
209 return classes +
"\n" + anclass