1 from builtins
import range
2 from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy
import TreeAnalyzerNumpy
3 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
14 def __init__(self, cfg_ana, cfg_comp, looperName):
15 super(AutoFillTreeProducer,self).
__init__(cfg_ana, cfg_comp, looperName)
18 self.
scalar =
not self.cfg_ana.vectorTree
21 if not getattr(self.cfg_ana,
'saveTLorentzVectors',
False):
22 fourVectorType.removeVariable(
"p4")
28 if hasattr(cfg_ana,
"collections"):
30 if hasattr(cfg_ana,
"globalObjects"):
32 if hasattr(cfg_ana,
"globalVariables"):
36 super(AutoFillTreeProducer, self).
beginLoop(setup)
41 self.mchandles[
'GenInfo'] = AutoHandle( (
'generator',
'',
''),
'GenEventInfoProduct' )
43 if isinstance(v, tuple)
and isinstance(v[0], AutoHandle):
44 self.handles[k] = v[0]
47 """Here we declare the variables that we always want and that are hard-coded"""
48 tr.var(
'run', int, storageType=
"i")
49 tr.var(
'lumi', int, storageType=
"i")
50 tr.var(
'evt', int, storageType=
"l")
63 tr.var(
'intLumi', int, storageType=
"i")
76 if hasattr(self.cfg_ana,
"PDFWeights")
and len(self.cfg_ana.PDFWeights) > 0:
80 for i
in range(nvals): tr.var(
'pdfWeight_%s_%d' % (pdf,i))
82 tr.vector(
'pdfWeight_%s' % pdf, nvals)
85 isMC = self.cfg_comp.isMC
89 if not hasattr(self.cfg_ana,
"ignoreAnalyzerBookings")
or not self.cfg_ana.ignoreAnalyzerBookings :
91 if hasattr(setup,
"globalVariables"):
93 if hasattr(setup,
"globalObjects"):
95 if hasattr(setup,
"collections"):
99 v.makeBranch(tree, isMC)
101 o.makeBranches(tree, isMC)
103 if isinstance(c, tuple): c = c[-1]
105 c.makeBranchesScalar(tree, isMC)
107 c.makeBranchesVector(tree, isMC)
110 """Here we fill the variables that we always want and that are hard-coded"""
111 tr.fill(
'run', event.input.eventAuxiliary().
id().
run())
112 tr.fill(
'lumi',event.input.eventAuxiliary().
id().luminosityBlock())
113 tr.fill(
'evt', event.input.eventAuxiliary().
id().
event())
114 tr.fill(
'isData', 0
if isMC
else 1)
121 tr.fill(
'intLumi', getattr(self.cfg_comp,
'intLumi',1.0))
125 tr.fill(
'xsec', getattr(self.cfg_comp,
'xSection',1.0))
127 if hasattr(event,
"nPU"):
128 tr.fill(
"nTrueInt", event.nPU)
129 tr.fill(
"puWeight", event.puWeight)
131 tr.fill(
"nTrueInt", -1)
132 tr.fill(
"puWeight", 1.0)
134 tr.fill(
"genWeight", self.mchandles[
'GenInfo'].product().
weight())
136 if hasattr(event,
"pdfWeights") :
138 if len(event.pdfWeights[pdf]) != nvals:
139 raise RuntimeError(
"PDF lenght mismatch for %s, declared %d but the event has %d" % (pdf,nvals,event.pdfWeights[pdf]))
141 for i,w
in enumerate(event.pdfWeights[pdf]):
142 tr.fill(
'pdfWeight_%s_%d' % (pdf,i), w)
144 tr.vfill(
'pdfWeight_%s' % pdf, event.pdfWeights[pdf])
147 if hasattr(self.cfg_ana,
"filter") :
148 if not self.cfg_ana.
filter(event) :
150 self.readCollections( event.input)
154 isMC = self.cfg_comp.isMC
155 if resetFirst: self.tree.
reset()
160 if not isMC
and v.mcOnly:
continue
161 v.fillBranch(self.tree, event, isMC)
164 if not isMC
and o.mcOnly:
continue
165 o.fillBranches(self.tree, getattr(event, on), isMC)
168 if isinstance(c, tuple)
and isinstance(c[0], AutoHandle):
169 if not isMC
and c[-1].mcOnly:
continue
170 objects = self.handles[cn].product()
171 setattr(event, cn, [objects[i]
for i
in range(objects.size())])
173 if not isMC
and c.mcOnly:
continue
175 c.fillBranchesScalar(self.tree, getattr(event, cn), isMC)
177 c.fillBranchesVector(self.tree, getattr(event, cn), isMC)
179 self.tree.tree.Fill()
183 This function produces a string that contains a Python wrapper for the event.
184 The wrapper is automatically generated based on the collections and allows the full
185 event contents to be accessed from subsequent Analyzers using e.g.
187 leps = event.selLeptons #is of type selLeptons
190 One just needs to add the EventAnalyzer to the sequence.
193 isMC = self.cfg_comp.isMC
197 anclass +=
"from PhysicsTools.HeppyCore.framework.analyzer import Analyzer\n"
198 anclass +=
"class EventAnalyzer(Analyzer):\n"
199 anclass +=
" def __init__(self, cfg_ana, cfg_comp, looperName):\n"
200 anclass +=
" super(EventAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)\n"
202 anclass +=
" def process(self, event):\n"
205 classes += coll.get_py_wrapper_class(isMC)
206 anclass +=
" event.{0} = {0}.make_array(event)\n".
format(coll.name)
208 return classes +
"\n" + anclass