Skip to content

Commit

Permalink
Merge pull request cms-sw#58 from gpetruc/cmg-heppy-7412-22sep15
Browse files Browse the repository at this point in the history
@arizzi I merged the PR from CMG, so you can take my heppy_7_4_12 branch and send me a new PR with the commits you were just telling me about. After I have it, I'll port the whole thing to 7_5_X. 
Then, I will do a PR to CMSSW for both 7_4_X and 7_5_X
  • Loading branch information
cbernet committed Oct 1, 2015
2 parents 3e798c8 + ab5d5d0 commit a15e01b
Show file tree
Hide file tree
Showing 38 changed files with 1,045 additions and 316 deletions.
2 changes: 1 addition & 1 deletion PhysicsTools/Heppy/interface/IsolationComputer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IsolationComputer {
selfVetoNone=0, selfVetoAll=1, selfVetoFirst=2
};
/// Initialize with the list of packed candidates (note: clears also all vetos)
void setPackedCandidates(const std::vector<pat::PackedCandidate> & all, int fromPV_thresh=1, float dz_thresh=9999., bool also_leptons=false) ;
void setPackedCandidates(const std::vector<pat::PackedCandidate> & all, int fromPV_thresh=1, float dz_thresh=9999., float dxy_thresh=9999., bool also_leptons=false) ;


/// veto footprint from this candidate, for the isolation of all candidates and also for calculation of neutral weights (if used)
Expand Down
16 changes: 11 additions & 5 deletions PhysicsTools/Heppy/python/analyzers/core/AutoFillTreeProducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ def __init__(self, cfg_ana, cfg_comp, looperName):
self.globalObjects = {}
self.globalVariables = []
if hasattr(cfg_ana,"collections"):
self.collections=cfg_ana.collections
self.collections.update(cfg_ana.collections)
if hasattr(cfg_ana,"globalObjects"):
self.globalObjects=cfg_ana.globalObjects
self.globalObjects.update(cfg_ana.globalObjects)
if hasattr(cfg_ana,"globalVariables"):
self.globalVariables=cfg_ana.globalVariables
self.globalVariables=cfg_ana.globalVariables[:]

def beginLoop(self, setup) :
super(AutoFillTreeProducer, self).beginLoop(setup)
Expand All @@ -46,7 +46,7 @@ def declareCoreVariables(self, tr, isMC):
"""Here we declare the variables that we always want and that are hard-coded"""
tr.var('run', int, storageType="i")
tr.var('lumi', int, storageType="i")
tr.var('evt', int, storageType="i")
tr.var('evt', int, storageType="l")
tr.var('isData', int)

# self.triggerBitCheckers = []
Expand All @@ -57,7 +57,10 @@ def declareCoreVariables(self, tr, isMC):
# trigVec.push_back(TP)
# tr.var( 'HLT_'+T, int )
# self.triggerBitCheckers.append( (T, TriggerBitChecker(trigVec)) )


if not isMC:
tr.var('intLumi', int, storageType="i")

if isMC:
## cross section
tr.var('xsec', float)
Expand Down Expand Up @@ -113,6 +116,9 @@ def fillCoreVariables(self, tr, event, isMC):
# for T,TC in self.triggerBitCheckers:
# tr.fill("HLT_"+T, TC.check(event.object(), triggerResults))

if not isMC:
tr.fill('intLumi', getattr(self.cfg_comp,'intLumi',1.0))

if isMC:
## xsection, if available
tr.fill('xsec', getattr(self.cfg_comp,'xSection',1.0))
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/Heppy/python/analyzers/core/AutoHandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, label, type, mayFail=False, fallbackLabel=None, lazy=True,dis
self.type = type
self.mayFail = mayFail
self.lazy = lazy
self.isLoaded = False
self.autoDisable = disableAtFirstFail;
self.disabled= False
Handle.__init__(self, self.type)
Expand Down
14 changes: 13 additions & 1 deletion PhysicsTools/Heppy/python/analyzers/core/JSONAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from FWCore.PythonUtilities.LumiList import LumiList
from PhysicsTools.Heppy.utils.rltinfo import RLTInfo

from DataFormats.FWLite import Lumis

class JSONAnalyzer( Analyzer ):
'''Apply a json filter, and creates an RLTInfo TTree.
Expand Down Expand Up @@ -36,6 +37,7 @@ def __init__(self, cfg_ana, cfg_comp, looperName):
else:
self.lumiList = None

self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else False

self.rltInfo = RLTInfo()

Expand All @@ -46,6 +48,15 @@ def beginLoop(self, setup):
self.count.register('All Lumis')
self.count.register('Passed Lumis')

if self.useLumiBlocks and not self.cfg_comp.isMC and not self.lumiList is None:
lumis = Lumis(self.cfg_comp.files)
for lumi in lumis:
lumiid = lumi.luminosityBlockAuxiliary().id()
run, lumi = lumiid.run(), lumiid.luminosityBlock()
if self.lumiList.contains(run,lumi):
self.rltInfo.add('dummy', run, lumi)


def process(self, event):
self.readCollections( event.input )
evid = event.input.eventAuxiliary().id()
Expand All @@ -66,7 +77,8 @@ def process(self, event):
self.count.inc('All Lumis')
if self.lumiList.contains(run,lumi):
self.count.inc('Passed Lumis')
self.rltInfo.add('dummy', run, lumi)
if not self.useLumiBlocks:
self.rltInfo.add('dummy', run, lumi)
return True
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class TriggerBitAnalyzer( Analyzer ):
def __init__(self, cfg_ana, cfg_comp, looperName ):
super(TriggerBitAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
self.processName = getattr(self.cfg_ana,"processName","HLT")
self.fallbackName = getattr(self.cfg_ana,"fallbackProcessName",None)
self.outprefix = getattr(self.cfg_ana,"outprefix", self.processName)
self.unrollbits = ( hasattr(self.cfg_ana,"unrollbits") and self.cfg_ana.unrollbits )


def declareHandles(self):
super(TriggerBitAnalyzer, self).declareHandles()
self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','',self.processName), 'edm::TriggerResults' )
fallback = ('TriggerResults','',self.fallbackName) if self.fallbackName else None
self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','',self.processName), 'edm::TriggerResults', fallbackLabel=fallback )

def beginLoop(self, setup):
super(TriggerBitAnalyzer,self).beginLoop(setup)
Expand Down Expand Up @@ -76,6 +78,7 @@ def process(self, event):
setattr(TriggerBitAnalyzer,"defaultEventFlagsConfig",cfg.Analyzer(
TriggerBitAnalyzer, name="EventFlags",
processName = 'PAT',
fallbackProcessName = 'RECO',
outprefix = 'Flag',
triggerBits = {
"HBHENoiseFilter" : [ "Flag_HBHENoiseFilter" ],
Expand Down
79 changes: 79 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/core/TriggerMatchAnalyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ROOT

from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import NTupleVariable
from PhysicsTools.HeppyCore.utils.deltar import matchObjectCollection, matchObjectCollection3
import PhysicsTools.HeppyCore.framework.config as cfg

class TriggerMatchAnalyzer( Analyzer ):
def __init__(self, cfg_ana, cfg_comp, looperName ):
super(TriggerMatchAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
self.processName = getattr(self.cfg_ana,"processName","PAT")
self.unpackPathNames = getattr(self.cfg_ana,"unpackPathNames",True)
self.label = self.cfg_ana.label
self.trgObjSelectors = []
self.trgObjSelectors.extend(getattr(self.cfg_ana,"trgObjSelectors",[]))
self.collToMatch = getattr(self.cfg_ana,"collToMatch",None)
self.collMatchSelectors = []
self.collMatchSelectors.extend(getattr(self.cfg_ana,"collMatchSelectors",[]))
self.collMatchDRCut = getattr(self.cfg_ana,"collMatchDRCut",0.3)
if self.collToMatch and not hasattr(self.cfg_ana,"univoqueMatching"): raise RuntimeError, "Please specify if the matching to trigger objects should be 1-to-1 or 1-to-many"
self.match1To1 = getattr(self.cfg_ana,"univoqueMatching",True)

def declareHandles(self):
super(TriggerMatchAnalyzer, self).declareHandles()
self.handles['TriggerBits'] = AutoHandle( ('TriggerResults','','HLT'), 'edm::TriggerResults' )
self.handles['TriggerObjects'] = AutoHandle( ('selectedPatTrigger','',self.processName), 'std::vector<pat::TriggerObjectStandAlone>' )

def beginLoop(self, setup):
super(TriggerMatchAnalyzer,self).beginLoop(setup)

def process(self, event):
self.readCollections( event.input )
triggerBits = self.handles['TriggerBits'].product()
allTriggerObjects = self.handles['TriggerObjects'].product()
names = event.input.object().triggerNames(triggerBits)
for ob in allTriggerObjects: ob.unpackPathNames(names)
triggerObjects = [ob for ob in allTriggerObjects if False not in [sel(ob) for sel in self.trgObjSelectors]]

setattr(event,'trgObjects_'+self.label,triggerObjects)

if self.collToMatch:
tcoll = getattr(event,self.collToMatch)
doubleandselector = lambda lep,ob: False if False in [sel(lep,ob) for sel in self.collMatchSelectors] else True
pairs = matchObjectCollection3(tcoll,triggerObjects,deltaRMax=self.collMatchDRCut,filter=doubleandselector) if self.match1To1 else matchObjectCollection(tcoll,triggerObjects,self.collMatchDRCut,filter=doubleandselector)
for lep in tcoll: setattr(lep,'matchedTrgObj'+self.label,pairs[lep])

if self.verbose:
print 'Verbose debug for triggerMatchAnalyzer %s'%self.label
for ob in getattr(event,'trgObjects_'+self.label):
types = ", ".join([str(f) for f in ob.filterIds()])
filters = ", ".join([str(f) for f in ob.filterLabels()])
paths = ", ".join([("%s***" if f in set(ob.pathNames(True)) else "%s")%f for f in ob.pathNames()]) # asterisks indicate final paths fired by this object, see pat::TriggerObjectStandAlone class
print 'Trigger object: pt=%.2f, eta=%.2f, phi=%.2f, collection=%s, type_ids=%s, filters=%s, paths=%s'%(ob.pt(),ob.eta(),ob.phi(),ob.collection(),types,filters,paths)
if self.collToMatch:
for lep in tcoll:
mstring = 'None'
if getattr(lep,'matchedTrgObj'+self.label):
mstring = 'trigger obj with pt=%.2f, eta=%.2f, phi=%.2f, collection=%s'%(ob.pt(),ob.eta(),ob.phi(),ob.collection())
print 'Lepton pt=%.2f, eta=%.2f, phi=%.2f matched to %s'%(lep.pt(),lep.eta(),lep.phi(),mstring)

return True


setattr(TriggerMatchAnalyzer,"defaultConfig",cfg.Analyzer(
TriggerMatchAnalyzer, name="TriggerMatchAnalyzerDefault",
label='DefaultTrigObjSelection',
processName = 'PAT',
unpackPathNames = True,
trgObjSelectors = [],
collToMatch = None,
collMatchSelectors = [],
collMatchDRCut = 0.3,
univoqueMatching = True,
verbose = False
)
)


1 change: 1 addition & 0 deletions PhysicsTools/Heppy/python/analyzers/core/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy import TreeAnalyzerNumpy
from PhysicsTools.Heppy.analyzers.core.TriggerBitAnalyzer import TriggerBitAnalyzer
from PhysicsTools.Heppy.analyzers.core.TriggerBitFilter import TriggerBitFilter
from PhysicsTools.Heppy.analyzers.core.TriggerMatchAnalyzer import TriggerMatchAnalyzer
23 changes: 23 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/core/autovars.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ def allBases(self):
if b2 not in ret:
ret.append(b2)
return ret
def addVariables(self,newvars):
currentnames = [v.name for v in self.allVars(True)] # require no conflict with all variables, including mcOnly ones
uniquenewvars = []
for var in newvars:
if var.name in uniquenewvars: raise RuntimeError, "Duplicate definition of variable %s while adding variables to object type %s" % (var.name,self.name)
uniquenewvars.append(var.name)
if var.name not in currentnames:
self.variables.append(var)
else:
raise RuntimeError, "Variable %s is already present in object type %s" % (var.name,self.name)
def addSubObjects(self,sos):
currentnames = [v.name for v in self.subObjects]
uniquenewobjs = []
for ob in sos:
if ob.name in uniquenewobjs: raise RuntimeError, "Duplicate definition of sub-object %s while adding it to object type %s" % (ob.name,self.name)
uniquenewobjs.append(ob.name)
if ob.name not in currentnames:
self.subObjects.append(ob)
else:
raise RuntimeError, "Sub-object %s is already present in object type %s" % (ob.name,self.name)
self._subObjectVars.clear() # clear and update cache of subobj variables
mynewvars = self.allVars(True)
mynewvars = self.allVars(False)
def removeVariable(self,name):
self.variables = [ v for v in self.variables if v.name != name]
def __repr__(self):
Expand Down
Loading

0 comments on commit a15e01b

Please sign in to comment.