Skip to content

Commit

Permalink
Merge pull request #6 from arizzi/heppy_7_2_2_patch2
Browse files Browse the repository at this point in the history
fix batch system stuff
  • Loading branch information
cbernet committed Dec 10, 2014
2 parents 7b1dd89 + 2dfcce1 commit 7a3dbb6
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 39 deletions.
41 changes: 25 additions & 16 deletions PhysicsTools/Heppy/python/analyzers/core/AutoFillTreeProducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def __init__(self, cfg_ana, cfg_comp, looperName):
## Declare how we store floats by default
self.tree.setDefaultFloatType("F"); # otherwise it's "D"

self.collections = {}
self.collections = {}
self.globalObjects = {}
self.globalVariables = {}
if hasattr(cfg_ana,"collections"):
self.collections=cfg_ana.collections
if hasattr(cfg_ana,"globalObjects"):
self.globalObjects=cfg_ana.globalObjects
if hasattr(cfg_ana,"globalVariables"):
self.globalVariables=cfg_ana.globalVariables
self.globalVariables = []
if hasattr(cfg_ana,"collections"):
self.collections=cfg_ana.collections
if hasattr(cfg_ana,"globalObjects"):
self.globalObjects=cfg_ana.globalObjects
if hasattr(cfg_ana,"globalVariables"):
self.globalVariables=cfg_ana.globalVariables

def declareHandles(self):
super(AutoFillTreeProducer, self).declareHandles()
Expand Down Expand Up @@ -74,11 +74,20 @@ def declareCoreVariables(self, tr, isMC):
else:
tr.vector('pdfWeight_%s' % pdf, nvals)

def declareVariables(self):
def declareVariables(self,setup):
isMC = self.cfg_comp.isMC
tree = self.tree
self.declareCoreVariables(tree, isMC)

if not hasattr(self.cfg_ana,"ignoreAnalyzerBookings") or not self.cfg_ana.ignoreAnalyzerBooking :
#import variables declared by the analyzers
if hasattr(setup,"globalVariables"):
self.globalVariables+=setup.globalVariables
if hasattr(setup,"globalObjects"):
self.globalObjects.update(setup.globalObjects)
if hasattr(setup,"collections"):
self.collections.update(setup.collections)

for v in self.globalVariables:
v.makeBranch(tree, isMC)
for o in self.globalObjects.itervalues():
Expand All @@ -103,18 +112,18 @@ def fillCoreVariables(self, tr, event, isMC):

if isMC:
## PU weights, check if a PU analyzer actually filled it
if hasattr(event,"nPU"):
tr.fill("nTrueInt", event.nPU)
tr.fill("puWeight", event.eventWeight)
else :
if hasattr(event,"nPU"):
tr.fill("nTrueInt", event.nPU)
tr.fill("puWeight", event.eventWeight)
else :
tr.fill("nTrueInt", -1)
tr.fill("puWeight", 1.0)
tr.fill("puWeight", 1.0)
tr.fill("genWeight", self.mchandles['GenInfo'].product().weight())
## PDF weights
if hasattr(event,"pdfWeights") :
for (pdf,nvals) in self.pdfWeights:
if len(event.pdfWeights[pdf]) != nvals:
if len(event.pdfWeights[pdf]) != nvals:
raise RuntimeError, "PDF lenght mismatch for %s, declared %d but the event has %d" % (pdf,nvals,event.pdfWeights[pdf])
if self.scalar:
for i,w in enumerate(event.pdfWeights[pdf]):
Expand Down
4 changes: 2 additions & 2 deletions PhysicsTools/Heppy/python/analyzers/core/TreeAnalyzerNumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def __init__(self, cfg_ana, cfg_comp, looperName):

def beginLoop(self, setup) :
super(TreeAnalyzerNumpy, self).beginLoop(setup)
self.declareVariables()
self.declareVariables(setup)

def declareVariables(self):
def declareVariables(self,setup):
print 'TreeAnalyzerNumpy.declareVariables : overload this function.'
pass

Expand Down
52 changes: 52 additions & 0 deletions PhysicsTools/Heppy/python/analyzers/core/TriggerBitAnalyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 *

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

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

def beginLoop(self, setup):
super(TriggerBitAnalyzer,self).beginLoop(setup)
self.triggerBitCheckers = []
for T, TL in self.cfg_ana.triggerBits.iteritems():
trigVec = ROOT.vector(ROOT.string)()
for TP in TL:
trigVec.push_back(TP)
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) )
self.triggerBitCheckers.append( (T, TriggerBitChecker(trigVec)) )

def process(self, event):
self.readCollections( event.input )
triggerResults = self.handles['TriggerResults'].product()
for T,TC in self.triggerBitCheckers:
outname="%s_%s"%(self.outprefix,T)
setattr(event,outname, TC.check(event.input.object(), triggerResults))

return True

12 changes: 11 additions & 1 deletion PhysicsTools/Heppy/test/example_autofill.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@
from PhysicsTools.Heppy.analyzers.objects.JetAnalyzer import JetAnalyzer
JetAna = JetAnalyzer.defaultConfig

sequence = [VertexAna,LepAna,TauAna,PhoAna,JetAna,treeProducer]
from PhysicsTools.Heppy.analyzers.core.TriggerBitAnalyzer import TriggerBitAnalyzer
TrigAna= cfg.Analyzer(
verbose=False,
class_object=TriggerBitAnalyzer,
triggerBits={'any','HLT_.*'},
# processName='HLT',
# outprefix='HLT'
)


sequence = [VertexAna,LepAna,TauAna,PhoAna,JetAna,TrigAna,treeProducer]


from PhysicsTools.Heppy.utils.miniAodFiles import miniAodFiles
Expand Down
32 changes: 20 additions & 12 deletions PhysicsTools/HeppyCore/python/framework/looper.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ def _build(self, cfg):
def _prepareOutput(self, name):
index = 0
tmpname = name
while True:
while True and index < 2000:
try:
# print 'mkdir', self.name
os.mkdir( tmpname )
break
except OSError:
index += 1
tmpname = '%s_%d' % (name, index)
if index == 2000:
raise ValueError( "More than 2000 output folder with same name or 2000 attempts failed, please clean-up, change name or check permissions")
return tmpname


Expand Down Expand Up @@ -189,16 +191,22 @@ def write(self):
import pickle
import sys
import os

cfgFileName = sys.argv[1]
pckfile = open( cfgFileName, 'r' )
config = pickle.load( pckfile )
comp = config.components[0]
events_class = config.events_class
looper = Looper( 'Loop', comp,
config.sequence,
config.services,
events_class,
nPrint = 5)
if len(sys.argv) == 2 :
cfgFileName = sys.argv[1]
pckfile = open( cfgFileName, 'r' )
config = pickle.load( pckfile )
comp = config.components[0]
events_class = config.events_class
elif len(sys.argv) == 3 :
cfgFileName = sys.argv[1]
file = open( cfgFileName, 'r' )
cfg = imp.load_source( 'cfg', cfgFileName, file)
compFileName = sys.argv[2]
pckfile = open( compFileName, 'r' )
comp = pickle.load( pckfile )
cfg.config.components=[comp]
events_class = cfg.config.events_class

looper = Looper( 'Loop', cfg.config,nPrint = 5)
looper.loop()
looper.write()
8 changes: 6 additions & 2 deletions PhysicsTools/HeppyCore/python/utils/batchmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,18 @@ def RunningMode(self, batch):
hostName = os.environ['HOSTNAME']
onLxplus = hostName.startswith('lxplus')
onPSI = hostName.startswith('t3ui' )
onPISA = re.match('.*gridui.*',hostName) or re.match('.*faiwn.*',hostName)
batchCmd = batch.split()[0]

if batchCmd == 'bsub':
if not onLxplus:
if not (onLxplus or onPISA) :
err = 'Cannot run %s on %s' % (batchCmd, hostName)
raise ValueError( err )
elif onPISA :
print 'running on LSF pisa : %s from %s' % (batchCmd, hostName)
return 'PISA'
else:
print 'running on LSF : %s from %s' % (batchCmd, hostName)
print 'running on LSF lxplus: %s from %s' % (batchCmd, hostName)
return 'LXPLUS'
elif batchCmd == "qsub":
if not onPSI:
Expand Down
47 changes: 41 additions & 6 deletions PhysicsTools/HeppyCore/scripts/heppy_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ def split(comps):
return splitComps


def batchScriptPISA( index, remoteDir=''):
'''prepare the LSF version of the batch script, to run on LSF'''
script = """#!/bin/bash
#BSUB -q cms
echo 'PWD:'
pwd
export VO_CMS_SW_DIR=/cvmfs/cms.cern.ch
source $VO_CMS_SW_DIR/cmsset_default.sh
echo 'environment:'
echo
env > local.env
env
# ulimit -v 3000000 # NO
echo 'copying job dir to worker'
###cd $CMSSW_BASE/src
eval `scramv1 runtime -sh`
#eval `scramv1 ru -sh`
# cd $LS_SUBCWD
# eval `scramv1 ru -sh`
##cd -
##cp -rf $LS_SUBCWD .
ls
echo `find . -type d | grep /`
echo 'running'
python $CMSSW_BASE/src/PhysicsTools/HeppyCore/python/framework/looper.py pycfg.py config.pck >& local.output
exit $?
#echo
#echo 'sending the job directory back'
#echo cp -r Loop/* $LS_SUBCWD
"""
return script


def batchScriptCERN( index, remoteDir=''):
'''prepare the LSF version of the batch script, to run on LSF'''
script = """#!/bin/bash
Expand All @@ -50,7 +83,7 @@ def batchScriptCERN( index, remoteDir=''):
ls
cd `find . -type d | grep /`
echo 'running'
python $CMSSW_BASE/src/PhysicsTools/HeppyCore/python/framework/looper.py config.pck
python $CMSSW_BASE/src/PhysicsTools/HeppyCore/python/framework/looper.py pycfg.py config.pck
echo
echo 'sending the job directory back'
cp -r Loop/* $LS_SUBCWD
Expand Down Expand Up @@ -135,7 +168,7 @@ def batchScriptPSI( index, jobDir, remoteDir=''):
cd `find . -type d | grep /`
echo 'running'
#python $CMSSW_BASE/src/CMGTools/RootTools/python/fwlite/looper.py config.pck
python {cmssw}/src/CMGTools/RootTools/python/fwlite/looper.py config.pck
python {cmssw}/src/CMGTools/RootTools/python/fwlite/looper.py pycfg.py config.pck
echo
{copy}
###########################################################################
Expand All @@ -155,7 +188,7 @@ def batchScriptLocal( remoteDir, index ):

script = """#!/bin/bash
echo 'running'
python $CMSSW_BASE/src/PhysicsTools/HeppyCore/python/framework/looper.py config.pck echo
python $CMSSW_BASE/src/PhysicsTools/HeppyCore/python/framework/looper.py pycfg.py config.pck echo
echo 'sending the job directory back'
mv Loop/* ./
"""
Expand All @@ -182,14 +215,16 @@ def PrepareJobUser(self, jobDir, value ):
scriptFile.write( batchScriptPSI ( value, jobDir, storeDir ) ) # storeDir not implemented at the moment
elif mode == 'LOCAL':
scriptFile.write( batchScriptLocal( storeDir, value) ) # watch out arguments are swapped (although not used)
elif mode == 'PISA' :
scriptFile.write( batchScriptPISA( storeDir, value) )
scriptFile.close()
os.system('chmod +x %s' % scriptFileName)

shutil.copyfile(cfgFileName, jobDir+'/pycfg.py')
jobConfig = copy.deepcopy(config)
jobConfig.components = [ components[value] ]
# jobConfig = copy.deepcopy(config)
# jobConfig.components = [ components[value] ]
cfgFile = open(jobDir+'/config.pck','w')
pickle.dump( jobConfig, cfgFile )
pickle.dump( components[value] , cfgFile )
# pickle.dump( cfo, cfgFile )
cfgFile.close()

Expand Down

0 comments on commit 7a3dbb6

Please sign in to comment.