Skip to content

Commit

Permalink
Merge pull request cms-sw#572 from jpata/vhbbHeppy80X_heppy_8_0_19
Browse files Browse the repository at this point in the history
sync to cbernet/cmssw:heppy_8_0_19
  • Loading branch information
arizzi authored Dec 13, 2016
2 parents 3095fee + f0bc092 commit 9e815ce
Show file tree
Hide file tree
Showing 216 changed files with 12,987 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TreeAnalyzerNumpy( Analyzer ):

def __init__(self, cfg_ana, cfg_comp, looperName):
super(TreeAnalyzerNumpy,self).__init__(cfg_ana, cfg_comp, looperName)
self.outservicename = getattr(cfg_ana,"outservicename","outputfile")
self.outservicename = getattr(cfg_ana,"outservicename","PhysicsTools.HeppyCore.framework.services.tfile.TFileService_outputfile")
self.treename = getattr(cfg_ana,"treename","tree")


Expand Down
18 changes: 9 additions & 9 deletions PhysicsTools/Heppy/python/analyzers/core/autovars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class NTupleVariable:
- name, type, help, default: obvious
- function: a function that taken an object computes the value to fill (e.g. lambda event : len(event.goodVertices))
"""
def __init__(self, name, function, type=float, help="", default=-99, mcOnly=False, filler=None):
def __init__(self, name, function, the_type=float, help="", default=-99, mcOnly=False, filler=None):
self.name = name
self.function = function
self.type = type
self.the_type = the_type
self.help = help
self.default = default
self.mcOnly = mcOnly
Expand All @@ -24,7 +24,7 @@ def __call__(self,object):
return ret
def makeBranch(self,treeNumpy,isMC):
if self.mcOnly and not isMC: return
treeNumpy.var(self.name, type=self.type, default=self.default, title=self.help, filler=self.filler)
treeNumpy.var(self.name, the_type=self.the_type, default=self.default, title=self.help, filler=self.filler)
def fillBranch(self,treeNumpy,object,isMC):
if self.mcOnly and not isMC: return
treeNumpy.fill(self.name, self(object))
Expand Down Expand Up @@ -60,7 +60,7 @@ def ownVars(self,isMC):
lambda object, subvar=subvar, so=so : subvar(so(object)),
# ^-- lambda object : subvar(so(object)) doesn't work due to scoping, see
# http://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture-in-python/2295372#2295372
type = subvar.type, help = subvar.help, default = subvar.default, mcOnly = subvar.mcOnly,
the_type = subvar.the_type, help = subvar.help, default = subvar.default, mcOnly = subvar.mcOnly,
filler = subvar.filler))
self._subObjectVars[isMC] = subvars
vars += self._subObjectVars[isMC]
Expand Down Expand Up @@ -144,7 +144,7 @@ def makeBranches(self,treeNumpy,isMC):
for v in allvars:
h = v.help
if self.help: h = "%s for %s" % ( h if h else v.name, self.help )
treeNumpy.var("%s_%s" % (self.name, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
treeNumpy.var("%s_%s" % (self.name, v.name), the_type=v.the_type, default=v.default, title=h, filler=v.filler)
def fillBranches(self,treeNumpy,object,isMC):
if self.mcOnly and not isMC: return
if object is None:
Expand Down Expand Up @@ -209,7 +209,7 @@ def makeBranchesScalar(self,treeNumpy,isMC):
for i in xrange(1,self.maxlen+1):
h = v.help
if self.help: h = "%s for %s [%d]" % ( h if h else v.name, self.help, i-1 )
treeNumpy.var("%s%d_%s" % (self.name, i, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
treeNumpy.var("%s%d_%s" % (self.name, i, v.name), the_type=v.the_type, default=v.default, title=h, filler=v.filler)
def makeBranchesVector(self,treeNumpy,isMC):
if not isMC and self.objectType.mcOnly: return
treeNumpy.var("n"+self.name, int)
Expand All @@ -218,7 +218,7 @@ def makeBranchesVector(self,treeNumpy,isMC):
h = v.help
if self.help: h = "%s for %s" % ( h if h else v.name, self.help )
name="%s_%s" % (self.name, v.name) if v.name != "" else self.name
treeNumpy.vector(name, "n"+self.name, self.maxlen, type=v.type, default=v.default, title=h, filler=v.filler)
treeNumpy.vector(name, "n"+self.name, self.maxlen, the_type=v.the_type, default=v.default, title=h, filler=v.filler)
def fillBranchesScalar(self,treeNumpy,collection,isMC):
if not isMC and self.objectType.mcOnly: return
if self.filter != None: collection = [ o for o in collection if self.filter(o) ]
Expand Down Expand Up @@ -248,14 +248,14 @@ def __repr__(self):
def get_cpp_declaration(self, isMC):
s = []
for v in self.objectType.allVars(isMC):
s += ["{0} {1}__{2}[{3}];".format(v.type.__name__, self.name, v.name, self.maxlen)]
s += ["{0} {1}__{2}[{3}];".format(v.the_type.__name__, self.name, v.name, self.maxlen)]
return "\n".join(s)

def get_cpp_wrapper_class(self, isMC):
s = "class %s {\n" % self.name
s += "public:\n"
for v in self.objectType.allVars(isMC):
s += " {0} {1};\n".format(v.type.__name__, v.name)
s += " {0} {1};\n".format(v.the_type.__name__, v.name)
s += "};\n"
return s

Expand Down
6 changes: 3 additions & 3 deletions PhysicsTools/Heppy/python/analyzers/objects/LeptonAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def plausible(rec,gen):
leps = event.inclusiveLeptons if self.cfg_ana.match_inclusiveLeptons else event.selectedLeptons
match = matchObjectCollection3(leps,
event.genleps + event.gentauleps,
deltaRMax = 1.2, filter = plausible)
deltaRMax = 1.2, filter_func = plausible)
for lep in leps:
gen = match[lep]
lep.mcMatchId = (gen.sourceId if gen != None and hasattr(gen, "sourceId") else 0)
Expand All @@ -649,7 +649,7 @@ def isFromB(self,particle,bid=5, done={}):
def matchAnyLeptons(self, event):
event.anyLeptons = [ x for x in event.genParticles if x.status() == 1 and abs(x.pdgId()) in [11,13] ]
leps = event.inclusiveLeptons if hasattr(event, 'inclusiveLeptons') else event.selectedLeptons
match = matchObjectCollection3(leps, event.anyLeptons, deltaRMax = 0.3, filter = lambda x,y : abs(x.pdgId()) == abs(y.pdgId()))
match = matchObjectCollection3(leps, event.anyLeptons, deltaRMax = 0.3, filter_func = lambda x,y : abs(x.pdgId()) == abs(y.pdgId()))
for lep in leps:
gen = match[lep]
lep.mcMatchAny_gp = gen
Expand All @@ -675,7 +675,7 @@ def matchToPhotons(self, event):
leps = event.inclusiveLeptons if hasattr(event, 'inclusiveLeptons') else event.selectedLeptons
leps = [ l for l in leps if abs(l.pdgId()) == 11 ]
plausible = lambda rec, gen : 0.3*gen.pt() < rec.pt() and rec.pt() < 1.5*gen.pt()
match = matchObjectCollection3(leps, event.anyPho, deltaRMax = 0.3, filter = plausible)
match = matchObjectCollection3(leps, event.anyPho, deltaRMax = 0.3, filter_func = plausible)
for lep in leps:
gen = match[lep]
lep.mcPho = gen
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/python/analyzers/objects/METAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def makeMETs(self, event):
if self.cfg_ana.doMetNoPU: self.metNoPU = ROOT.pat.MET(self.handles['nopumet'].product()[0])
else:
self.met = self.handles['met'].product()[0]
if self.cfg_ana.doMetNoPU: self.metNoPU = self.handles['nopumet'].product()[0]
if self.cfg_ana.doMetNoPU:
self.metNoPU = self.handles['nopumet'].product()[0]

if self.recalibrateMET == "type1":
type1METCorr = getattr(event, 'type1METCorr'+self.jetAnalyzerPostFix)
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/Heppy/python/utils/miniAodFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def miniAodFiles():
]
elif (big,medium)==(8,0):
files=[
'/store/relval/CMSSW_8_0_19/RelValZMM_13/MINIAODSIM/80X_mcRun2_asymptotic_v17_gs7120p2-v1/00000/4E733BCE-656E-E611-AE16-0CC47A78A3F4.root',
'/store/relval/CMSSW_8_0_19/RelValZMM_13/MINIAODSIM/80X_mcRun2_asymptotic_v17_gs7120p2-v1/00000/B82838D0-656E-E611-BAFD-0CC47A7C35A4.root'
'/store/relval/CMSSW_8_0_21/RelValZMM_13/MINIAODSIM/80X_mcRun2_asymptotic_2016_TrancheIV_v6_Tr4GT_v6-v1/10000/50C4AE3D-C498-E611-B0DF-0025905B859E.root',
'/store/relval/CMSSW_8_0_21/RelValZMM_13/MINIAODSIM/80X_mcRun2_asymptotic_2016_TrancheIV_v6_Tr4GT_v6-v1/10000/DCC7483C-C498-E611-9270-0025905A48EC.root'
]
else:
raise ValueError('no mini AOD file defined for release '+cmsswRelease())
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/Heppy/test/simple_example_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
sel_jets = cfg.Analyzer(
Filter,
'jets',
output = 'jets',
input_objects = 'all_jets',
filter_func = lambda x : x.pt()>30
)
Expand Down
7 changes: 0 additions & 7 deletions PhysicsTools/HeppyCore/python/analyzers/CMSTestAnalyzer.py

This file was deleted.

34 changes: 34 additions & 0 deletions PhysicsTools/HeppyCore/python/analyzers/ChargedHadronsFromB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from PhysicsTools.HeppyCore.framework.analyzer import Analyzer
from PhysicsTools.HeppyCore.particles.genbrowser import GenBrowser
from PhysicsTools.HeppyCore.particles.pdgcodes import hasBottom

class ChargedHadronsFromB(Analyzer):

def process(self, event):
genptcs = event.gen_particles
bquarks = []
charged_hadrons = []
event.hadrons_from_b = []
for ptc in genptcs:
if abs(ptc.pdgid())==5:
bquarks.append(ptc)
elif ptc.q() and ptc.status()==1:
charged_hadrons.append(ptc)
if len(bquarks) == 0 or len(charged_hadrons) == 0:
return
event.genbrowser = GenBrowser(event.gen_particles,
event.gen_vertices)
event.hadrons_from_b = []
event.hadrons_not_from_b = []
for hadron in charged_hadrons:
ancestors = event.genbrowser.ancestors(hadron)
is_from_b = False
for ancestor in ancestors:
if hasBottom(ancestor.pdgid() ):
is_from_b = True
if is_from_b:
event.hadrons_from_b.append(hadron)
else:
event.hadrons_not_from_b.append(hadron)


23 changes: 23 additions & 0 deletions PhysicsTools/HeppyCore/python/analyzers/Counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from PhysicsTools.HeppyCore.framework.analyzer import Analyzer

class Counter(Analyzer):
'''Counts the number of objects in the input_objects collection
and skip the event if this number is strictly inferior to min_number
Example:
from PhysicsTools.HeppyCore.analyzers.Counter import Counter
gen_counter = cfg.Analyzer(
Counter,
input_objects = 'gen_particles_stable',
min_number = 1
)
* input_objects : the input collection
* min_number : the minimum amount of object in input_object to not skip the event
'''

def process(self, event):
input_collection = getattr(event, self.cfg_ana.input_objects)
return len(input_collection) >= self.cfg_ana.min_number
39 changes: 39 additions & 0 deletions PhysicsTools/HeppyCore/python/analyzers/EventFilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from PhysicsTools.HeppyCore.framework.analyzer import Analyzer
import collections

class EventFilter (Analyzer):
'''Filters events based on the contents of an input collection.
When an event is rejected by the EventFilter, the analyzers
placed after the filter in the sequence will not run.
Example:
To reject events with 1 lepton or more:
from PhysicsTools.HeppyCore.analyzers.EventFilter import EventFilter
lepton_filter = cfg.Analyzer(
EventFilter ,
'lepton_filter',
input_objects = 'leptons',
min_number = 1,
veto = True
)
* input_objects : the input collection.
* min_number : minimum number of objects in input_objects to trigger the filtering
* veto :
- if False: events are selected if there are >= min_number objects in input_objects
- if True: events are rejected if there are >= min_number objects in input_objects.
'''

def process(self, event):
input_collection = getattr(event, self.cfg_ana.input_objects)
if self.cfg_ana.veto:
return not len(input_collection) >= self.cfg_ana.min_number
else:
return len(input_collection) >= self.cfg_ana.min_number


69 changes: 69 additions & 0 deletions PhysicsTools/HeppyCore/python/analyzers/EventTextOutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from PhysicsTools.HeppyCore.framework.analyzer import Analyzer

import shelve

outfilename = 'particles.shv'

class SimpleParticle(object):
def __init__(self, ptc):
self.theta = ptc.theta()
self.phi = ptc.phi()
self.energy = ptc.e()
self.pdgid = ptc.pdgid()

def get_data(self):
return self.pdgid, self.theta, self.phi, self.energy

def __str__(self):
return 'particle: {id} : theta={theta}, phi={phi}, energy={energy}'.format(
id = self.pdgid,
theta = self.theta,
phi = self.phi,
energy = self.energy
)

class SimpleEvent(object):
def __init__(self, ievent, ptcs):
self.ievent = ievent
self.ptcs = map(SimpleParticle, ptcs)
self.data = dict(
ievent = ievent,
particles = [ptc.get_data() for ptc in self.ptcs]
)

def get_data(self):
return self.data

def __str__(self):
lines = ['event {iev}'.format(iev=self.ievent)]
lines.extend( map(str, self.ptcs) )
return '\n'.join(lines)



class EventTextOutput(Analyzer):

def beginLoop(self, setup):
super(EventTextOutput, self).beginLoop(setup)
self.events = []

def process(self, event):
ptcs = getattr(event, self.cfg_ana.particles )
self.events.append(SimpleEvent(event.iEv, ptcs).get_data())

def endLoop(self, setup):
super(EventTextOutput, self).endLoop(setup)
out = shelve.open('/'.join([self.dirName, outfilename]))
out['events'] = self.events
out.close()


if __name__ == '__main__':

import pprint
sh = shelve.open(outfilename)
events = sh['events']
for event in events:
print 'event', event['ievent']
for pdgid, theta, phi, energy in event['particles']:
print '\t', pdgid, theta, phi, energy
42 changes: 39 additions & 3 deletions PhysicsTools/HeppyCore/python/analyzers/Filter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
from PhysicsTools.HeppyCore.framework.analyzer import Analyzer
import collections

class Filter(Analyzer):
'''Filter objects from the input_objects collection
and store them in the output collection
Example:
from PhysicsTools.HeppyCore.analyzers.Filter import Filter
def is_lepton(ptc):
"""Returns true if the particle energy is larger than 5 GeV
and if its pdgid is +-11 (electrons) or +-13 (muons)
return ptc.e()> 5. and abs(ptc.pdgid()) in [11, 13]
leptons = cfg.Analyzer(
Filter,
'sel_leptons',
output = 'leptons',
input_objects = 'rec_particles',
filter_func = is_lepton
)
* input_objects : the input collection.
if a dictionary, the filtering function is applied to the dictionary values,
and not to the keys.
* output : the output collection
* filter_func : a function object.
IMPORTANT NOTE: lambda statements should not be used, as they
do not work in multiprocessing mode. looking for a solution...
'''

def process(self, event):
input_collection = getattr(event, self.cfg_ana.input_objects)
output_collection = [obj for obj in input_collection \
if self.cfg_ana.filter_func(obj)]
setattr(event, self.instance_label, output_collection)
output_collection = None
if isinstance(input_collection, collections.Mapping):
output_collection = dict( [(key, val) for key, val in input_collection.iteritems()
if self.cfg_ana.filter_func(val)] )
else:
output_collection = [obj for obj in input_collection \
if self.cfg_ana.filter_func(obj)]
setattr(event, self.cfg_ana.output, output_collection)
Loading

0 comments on commit 9e815ce

Please sign in to comment.