Skip to content

Commit

Permalink
Merge pull request #7 from gpetruc/HeppyUpdatesFromCMG-heppy_7_2_2_pa…
Browse files Browse the repository at this point in the history
…tch2

Heppy updates from CMG
  • Loading branch information
cbernet committed Dec 16, 2014
2 parents 7a3dbb6 + 6f20bdf commit e7d9209
Show file tree
Hide file tree
Showing 45 changed files with 1,582 additions and 824 deletions.
76 changes: 0 additions & 76 deletions PhysicsTools/Heppy/interface/HemisphereViaKt.h

This file was deleted.

4 changes: 2 additions & 2 deletions PhysicsTools/Heppy/python/analyzers/core/Analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def beginLoop(self, setup):
self.declareHandles()


def process(self, iEvent, event ):
def process(self, event ):
'''Automatically called by Looper, for all analyzers.
each analyzer in the sequence will be passed the same event instance.
each analyzer can access, modify, and store event information, of any type.'''
print self.cfg_ana.name
self.readCollections( iEvent )
self.readCollections( event.input )

def readCollections(self, iEvent ):
'''You must call this function at the beginning of the process
Expand Down
10 changes: 5 additions & 5 deletions PhysicsTools/Heppy/python/analyzers/core/EventSelector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EventSelector( Analyzer ):
The process function of this analyzer returns False if the event number
is not in the toSelect list.
In this list, put actual CMS event numbers obtained by doing:
iEvent.eventAuxiliary().id().event()
event.input.eventAuxiliary().id().event()
not event processing number
in this python framework.
Expand All @@ -34,10 +34,10 @@ class EventSelector( Analyzer ):
with an other person at the event level.
"""

def process(self, iEvent, event):
run = iEvent.eventAuxiliary().id().run()
lumi = iEvent.eventAuxiliary().id().luminosityBlock()
eId = iEvent.eventAuxiliary().id().event()
def process(self, event):
run = event.input.eventAuxiliary().id().run()
lumi = event.input.eventAuxiliary().id().luminosityBlock()
eId = event.input.eventAuxiliary().id().event()
if eId in self.cfg_ana.toSelect:
# raise ValueError('found')
print 'Selecting', run, lumi, eId
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/Heppy/python/analyzers/core/PileUpAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def beginLoop(self, setup):
self.averages.add('vertexWeight', Average('vertexWeight') )


def process(self, iEvent, event):
self.readCollections( iEvent )
def process(self, event):
self.readCollections( event.input )
## if component is embed return (has no trigger obj)
if self.cfg_comp.isEmbed :
return True
Expand Down
17 changes: 10 additions & 7 deletions PhysicsTools/Heppy/python/analyzers/core/SkimAnalyzerCount.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SkimAnalyzerCount( Analyzer ):

def __init__(self, cfg_ana, cfg_comp, looperName):
super(SkimAnalyzerCount, self).__init__(cfg_ana, cfg_comp, looperName)
self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else True
self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else False

def declareHandles(self):
super(SkimAnalyzerCount, self).declareHandles()
Expand All @@ -27,10 +27,11 @@ def beginLoop(self, setup):
self.counters.addCounter('SkimReport')
self.count = self.counters.counter('SkimReport')
self.count.register('All Events')
self.count.register('Sum Weights')
if self.cfg_comp.isMC:
self.count.register('Sum Weights')

if not self.useLumiBlocks:
print 'Will actually count events instead of accessing lumi blocks'
#print 'Will actually count events instead of accessing lumi blocks'
return True

print 'Counting the total events before the skim by accessing luminosity blocks'
Expand All @@ -47,16 +48,18 @@ def beginLoop(self, setup):

if self.useLumiBlocks:
self.count.inc('All Events',totalEvents)
self.count.inc('Sum Weights',totalEvents)
if self.cfg_comp.isMC:
self.count.inc('Sum Weights',totalEvents)
print 'Done -> proceeding with the analysis'
else:
print 'Failed -> will have to actually count events (this can happen if the input dataset is not a CMG one)'



def process(self, iEvent, event):
def process(self, event):
if not self.useLumiBlocks:
self.readCollections( iEvent )
self.readCollections( event.input )
self.count.inc('All Events')
self.count.inc('Sum Weights', self.mchandles['GenInfo'].product().weight())
if self.cfg_comp.isMC:
self.count.inc('Sum Weights', self.mchandles['GenInfo'].product().weight())
return True
21 changes: 4 additions & 17 deletions PhysicsTools/Heppy/python/analyzers/core/TriggerBitAnalyzer.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
import operator
import itertools
import copy
from math import *
import ROOT
from ROOT.heppy import TriggerBitChecker

from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.HeppyCore.framework.event import Event
from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import *
from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import NTupleVariable

class TriggerBitAnalyzer( Analyzer ):
def __init__(self, cfg_ana, cfg_comp, looperName ):
super(TriggerBitAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
if hasattr(self.cfg_ana,"processName"):
self.processName = self.cfg_ana.processName
else :
self.processName = 'HLT'

if hasattr(self.cfg_ana,"outprefix"):
self.outprefix = self.cfg_ana.outprefix
else :
self.outprefix = self.processName
self.processName = getattr(self.cfg_ana,"processName","HLT")
self.outprefix = getattr(self.cfg_ana,"outprefix", self.processName)

def declareHandles(self):
super(TriggerBitAnalyzer, self).declareHandles()
Expand All @@ -38,7 +25,7 @@ def beginLoop(self, setup):
outname="%s_%s"%(self.outprefix,T)
if not hasattr(setup ,"globalVariables") :
setup.globalVariables = []
setup.globalVariables.append( NTupleVariable(outname, lambda ev : getattr(ev,outname), help="OR of %s"%TL) )
setup.globalVariables.append( NTupleVariable(outname, eval("lambda ev: ev.%s" % outname), help="OR of %s"%TL) )
self.triggerBitCheckers.append( (T, TriggerBitChecker(trigVec)) )

def process(self, event):
Expand Down
22 changes: 8 additions & 14 deletions PhysicsTools/Heppy/python/analyzers/core/TriggerBitFilter.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import operator
import itertools
import copy
from math import *

from ROOT import TriggerBitChecker
import ROOT
from ROOT.heppy import TriggerBitChecker

from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.HeppyCore.framework.event import Event
from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle

class TriggerBitFilter( Analyzer ):
def __init__(self, cfg_ana, cfg_comp, looperName ):
super(TriggerBitFilter,self).__init__(cfg_ana,cfg_comp,looperName)
self.processName = getattr(self.cfg_ana,"processName","HLT")
triggers = cfg_comp.triggers
self.autoAccept = True if len(triggers) == 0 else False
vetoTriggers = cfg_comp.vetoTriggers if hasattr(cfg_comp, 'vetoTriggers') else []
import ROOT
trigVec = ROOT.vector(ROOT.string)()
for t in triggers: trigVec.push_back(t)
self.mainFilter = TriggerBitChecker(trigVec)
Expand All @@ -29,17 +23,17 @@ def __init__(self, cfg_ana, cfg_comp, looperName ):

def declareHandles(self):
super(TriggerBitFilter, self).declareHandles()
self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','','HLT'), 'edm::TriggerResults' )
self.handles['TriggerResults'] = AutoHandle( ('TriggerResults','',self.processName), 'edm::TriggerResults' )

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

def process(self, iEvent, event):
def process(self, event):
if self.autoAccept: return True
self.readCollections( iEvent )
if not self.mainFilter.check(iEvent.object(), self.handles['TriggerResults'].product()):
self.readCollections( event.input )
if not self.mainFilter.check(event.input.object(), self.handles['TriggerResults'].product()):
return False
if self.vetoFilter != None and self.vetoFilter.check(iEvent.object(), self.handles['TriggerResults'].product()):
if self.vetoFilter != None and self.vetoFilter.check(event.input.object(), self.handles['TriggerResults'].product()):
return False
return True

1 change: 1 addition & 0 deletions PhysicsTools/Heppy/python/analyzers/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty
8 changes: 8 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/core/all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from PhysicsTools.Heppy.analyzers.core.AutoFillTreeProducer import AutoFillTreeProducer
from PhysicsTools.Heppy.analyzers.core.EventSelector import EventSelector
from PhysicsTools.Heppy.analyzers.core.JSONAnalyzer import JSONAnalyzer
from PhysicsTools.Heppy.analyzers.core.PileUpAnalyzer import PileUpAnalyzer
from PhysicsTools.Heppy.analyzers.core.SkimAnalyzerCount import SkimAnalyzerCount
from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy import TreeAnalyzerNumpy
from PhysicsTools.Heppy.analyzers.core.TriggerBitAnalyzer import TriggerBitAnalyzer
from PhysicsTools.Heppy.analyzers.core.TriggerBitFilter import TriggerBitFilter
20 changes: 16 additions & 4 deletions PhysicsTools/Heppy/python/analyzers/core/autovars.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,33 @@ def __init__(self,name,baseObjectTypes=[],mcOnly=[],variables=[]):
self.baseObjectTypes = baseObjectTypes
self.mcOnly = mcOnly
self.variables = variables
def ownVars(self,isMC):
"""Return only my vars, not including the ones from the bases"""
return [ v for v in self.variables if (isMC or not var.mcOnly) ]
def allVars(self,isMC):
"""Return all vars, including the base ones. Duplicate bases are not added twice"""
ret = []; names = {}
if not isMC and self.mcOnly: return []
for base in self.baseObjectTypes:
for base in self.allBases():
if not isMC and base.mcOnly: continue
for var in base.allVars(isMC):
for var in base.ownVars(isMC):
if var.name in names: raise RuntimeError, "Duplicate definition of variable %s from %s and %s" % (var.name, base.name, names[var.name])
names[var.name] = base.name
ret.append(var)
for var in self.variables:
if not isMC and var.mcOnly: continue
for var in self.ownVars(isMC):
if var.name in names: raise RuntimeError, "Duplicate definition of variable %s from %s and %s" % (var.name, self.name, names[var.name])
names[var.name] = self.name
ret.append(var)
return ret
def allBases(self):
ret = []
for b in self.baseObjectTypes:
if b not in ret:
ret.append(b)
for b2 in b.allBases():
if b2 not in ret:
ret.append(b2)
return ret
def removeVariable(self,name):
self.variables = [ v for v in self.variables if v.name != name]

Expand Down
63 changes: 63 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/gen/GenHeavyFlavourAnalyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.HeppyCore.utils.deltar import deltaR

class GenHeavyFlavourAnalyzer( Analyzer ):
"""
Makes:
event.genallbquarks, event.genallcquarks:
list of all b and c quarks (without doublecounting of b->b and c->c chains).
if status2Only == True, only status 2 ones are included.
event.allBPartons
all status 2 b-quarks, sorted by pt decreasingly
event.bPartons:
status 2 b-quarks passing a pt cut bquarkPtCut (default: 15)
Requires:
event.genParticles
"""

def __init__(self, cfg_ana, cfg_comp, looperName ):
super(GenHeavyFlavourAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
self.status2Only = cfg_ana.status2Only
self.bquarkPtCut = cfg_ana.bquarkPtCut

def declareHandles(self):
super(GenHeavyFlavourAnalyzer, self).declareHandles()

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

def makeBPartons(self,event):
event.allBPartons = [ q for q in event.genParticles if abs(q.pdgId()) == 5 and abs(q.status()) == 2 and abs(q.pt()) > self.bquarkPtCut ]
event.allBPartons.sort(key = lambda q : q.pt(), reverse = True)
event.bPartons = []
for q in event.allBPartons:
duplicate = False
for q2 in event.bPartons:
if deltaR(q.eta(),q.phi(),q2.eta(),q2.phi()) < 0.5:
duplicate = True
continue
if not duplicate: event.bPartons.append(q)

def process(self, event):
self.readCollections( event.input )

# if not MC, nothing to do
if not self.cfg_comp.isMC:
return True

status2f = (lambda p : p.status() == 2) if self.status2Only else (lambda p : True)
event.genallcquarks = [ p for p in event.genParticles if abs(p.pdgId()) == 5 and ( p.numberOfDaughters() == 0 or abs(p.daughter(0).pdgId()) != 5) and status2f(p) ]
event.genallbquarks = [ p for p in event.genParticles if abs(p.pdgId()) == 4 and ( p.numberOfDaughters() == 0 or abs(p.daughter(0).pdgId()) != 4) and status2f(p) ]

self.makeBPartons(event)

return True

import PhysicsTools.HeppyCore.framework.config as cfg
setattr(GenHeavyFlavourAnalyzer,"defaultConfig",
cfg.Analyzer(GenHeavyFlavourAnalyzer,
status2Only = False,
bquarkPtCut = 15.0,
)
)
Loading

0 comments on commit e7d9209

Please sign in to comment.