Skip to content

Commit

Permalink
Merge pull request #258 from gpetruc/heppyOptimizations
Browse files Browse the repository at this point in the history
Heppy optimizations and an improved time report
  • Loading branch information
gpetruc committed Feb 13, 2015
2 parents 8de30f1 + 475bc9e commit 6520f24
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
8 changes: 8 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/core/autovars.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def makeBranch(self,treeNumpy,isMC):
def fillBranch(self,treeNumpy,object,isMC):
if self.mcOnly and not isMC: return
treeNumpy.fill(self.name, self(object))
def __repr__(self):
return "<NTupleVariable[%s]>" % self.name

class NTupleObjectType:
def __init__(self,name,baseObjectTypes=[],mcOnly=[],variables=[]):
Expand Down Expand Up @@ -60,6 +62,8 @@ def allBases(self):
return ret
def removeVariable(self,name):
self.variables = [ v for v in self.variables if v.name != name]
def __repr__(self):
return "<NTupleObjectType[%s]>" % self.name

class NTupleObject:
def __init__(self, name, objectType, help="", mcOnly=False):
Expand All @@ -79,6 +83,8 @@ def fillBranches(self,treeNumpy,object,isMC):
allvars = self.objectType.allVars(isMC)
for v in allvars:
treeNumpy.fill("%s_%s" % (self.name, v.name), v(object))
def __repr__(self):
return "<NTupleObject[%s]>" % self.name


class NTupleCollection:
Expand Down Expand Up @@ -137,5 +143,7 @@ def fillBranchesVector(self,treeNumpy,collection,isMC):
for v in allvars:
name="%s_%s" % (self.name, v.name) if v.name != "" else self.name
treeNumpy.vfill(name, [ v(collection[i]) for i in xrange(num) ])
def __repr__(self):
return "<NTupleCollection[%s]>" % self.name


21 changes: 8 additions & 13 deletions PhysicsTools/Heppy/python/analyzers/gen/GeneratorAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def makeMCInfo(self, event):
verbose = getattr(self.cfg_ana, 'verbose', False)
rawGenParticles = self.mchandles['genParticles'].product()
good = []; keymap = {};
allGenParticles = []
for rawIndex,p in enumerate(rawGenParticles):
if self.makeAllGenParticles: allGenParticles.append(p)
id = abs(p.pdgId())
status = p.status()
# particles must be status > 2, except for prompt leptons, photons, neutralinos
Expand Down Expand Up @@ -134,15 +136,15 @@ def makeMCInfo(self, event):
print "Error keying %d: motherIndex %d, ancestor.pdgId %d, good[gp.motherIndex].pdgId() %d " % (igp, gp.motherIndex, ancestor.pdgId(), good[gp.motherIndex].pdgId())
break
ancestor = None if ancestor.numberOfMothers() == 0 else ancestor.motherRef(0)
if abs(gp.pdgId()) not in {1,2,3,4,5,11,12,13,14,15,16,21}:
if abs(gp.pdgId()) not in [1,2,3,4,5,11,12,13,14,15,16,21]:
gp.sourceId = gp.pdgId()
if gp.motherIndex != -1:
ancestor = good[gp.motherIndex]
if ancestor.sourceId != 99 and (ancestor.mass() > gp.mass() or gp.sourceId == 99):
gp.sourceId = ancestor.sourceId
event.generatorSummary = good
# add the ID of the mother to be able to recreate last decay chains
for ip,p in enumerate(event.generatorSummary):
for ip,p in enumerate(good):
moms = realGenMothers(p)
if len(moms)==0:
p.motherId = 0
Expand All @@ -164,15 +166,8 @@ def makeMCInfo(self, event):
print "\n\n"

if self.makeAllGenParticles:
event.genParticles = []
for rawIndex,p in enumerate(rawGenParticles):
if rawIndex in keymap:
gp = event.generatorSummary[keymap[rawIndex]]
else:
gp = p
gp.rawIndex = rawIndex
gp.genSummaryIndex = -1
event.genParticles.append(gp)
event.genParticles = allGenParticles

if self.makeSplittedGenLists:
event.genHiggsBosons = []
event.genVBosons = []
Expand Down Expand Up @@ -225,11 +220,11 @@ def process(self, event):
setattr(GeneratorAnalyzer,"defaultConfig",
cfg.Analyzer(GeneratorAnalyzer,
# BSM particles that can appear with status <= 2 and should be kept
stableBSMParticleIds = { 1000022 },
stableBSMParticleIds = [ 1000022 ],
# Particles of which we want to save the pre-FSR momentum (a la status 3).
# Note that for quarks and gluons the post-FSR doesn't make sense,
# so those should always be in the list
savePreFSRParticleIds = { 1,2,3,4,5, 11,12,13,14,15,16, 21 },
savePreFSRParticleIds = [ 1,2,3,4,5, 11,12,13,14,15,16, 21 ],
# Make also the list of all genParticles, for other analyzers to handle
makeAllGenParticles = True,
# Make also the splitted lists
Expand Down
25 changes: 13 additions & 12 deletions PhysicsTools/HeppyCore/python/framework/looper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ def __init__( self, name,
if len(self.cfg_comp.files)==0:
errmsg = 'please provide at least an input file in the files attribute of this component\n' + str(self.cfg_comp)
raise ValueError( errmsg )
if hasattr(config,"preprocessor") and config.preprocessor is not None :
self.cfg_comp = config.preprocessor.run(self.cfg_comp,self.outDir,firstEvent,nEvents)
if hasattr(self.cfg_comp,"options"):
print self.cfg_comp.files,self.cfg_comp.options
self.events = config.events_class(self.cfg_comp.files, tree_name,options=self.cfg_comp.options)
else :
self.events = config.events_class(self.cfg_comp.files, tree_name)

self.events = config.events_class(self.cfg_comp.files, tree_name)
if hasattr(self.cfg_comp, 'fineSplit'):
fineSplitIndex, fineSplitFactor = self.cfg_comp.fineSplit
if fineSplitFactor > 1:
Expand Down Expand Up @@ -192,10 +185,19 @@ def loop(self):
allev = max([x['events'] for x in self.timeReport])
warning = self.logger.warning
warning("\n ---- TimeReport (all times in ms; first evt is skipped) ---- ")
warning("%9s %9s %9s %9s %s" % ("processed","all evts","time/proc", " time/all", "analyer"))
warning("%9s %9s %9s %9s %s" % ("---------","--------","---------", "---------", "-------------"))
warning("%9s %9s %9s %9s %6s %s" % ("processed","all evts","time/proc", " time/all", " [%] ", "analyer"))
warning("%9s %9s %9s %9s %6s %s" % ("---------","--------","---------", "---------", " -----", "-------------"))
sumtime = sum(rep['time'] for rep in self.timeReport)
passev = self.timeReport[-1]['events']
for ana,rep in zip(self.analyzers,self.timeReport):
warning( "%9d %9d %10.2f %10.2f %s" % ( rep['events'], allev, 1000*rep['time']/(rep['events']-1) if rep['events']>1 else 0, 1000*rep['time']/(allev-1) if allev > 1 else 0, ana.name))
timePerProcEv = rep['time']/(rep['events']-1) if rep['events'] > 1 else 0
timePerAllEv = rep['time']/(allev-1) if allev > 1 else 0
fracAllEv = rep['time']/sumtime
warning( "%9d %9d %10.2f %10.2f %5.1f%% %s" % ( rep['events'], allev, 1000*timePerProcEv, 1000*timePerAllEv, 100.0*fracAllEv, ana.name))
totPerProcEv = sumtime/(passev-1) if passev > 1 else 0
totPerAllEv = sumtime/(allev-1) if allev > 1 else 0
warning("%9s %9s %9s %9s %s" % ("---------","--------","---------", "---------", "-------------"))
warning("%9d %9d %10.2f %10.2f %5.1f%% %s" % ( passev, allev, 1000*totPerProcEv, 1000*totPerAllEv, 100.0, "TOTAL"))
warning("")

def process(self, iEv ):
Expand Down Expand Up @@ -232,7 +234,6 @@ def write(self):
self.setup.close()



if __name__ == '__main__':

import pickle
Expand Down
5 changes: 4 additions & 1 deletion PhysicsTools/HeppyCore/python/utils/deltar.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def matchObjectCollection3 ( objects, matchCollection, deltaRMax = 0.3, filter =
return dict( zip(objects, [None]*len(objects)) )
# build all possible combinations

allPairs = [(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection if filter(object,match) ]
objectCoords = [ (o.eta(),o.phi(),o) for o in objects ]
matchdCoords = [ (o.eta(),o.phi(),o) for o in matchCollection ]
allPairs = [(deltaR2 (oeta, ophi, meta, mphi), (object, match)) for (oeta,ophi,object) in objectCoords for (meta,mphi,match) in matchdCoords if abs(oeta-meta)<=deltaRMax and filter(object,match) ]
#allPairs = [(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection if filter(object,match) ]
allPairs.sort ()
#
# to flag already matched objects
Expand Down

0 comments on commit 6520f24

Please sign in to comment.