Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ AutoDict_*
# Output dirs in signal and bkg
Signal/outdir_*
Background/outdir_*

# Lint and formatting
*.pylintrc
65 changes: 64 additions & 1 deletion Background/RunBackgroundScripts.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Script for running background fitting jobs for flashggFinalFit
import os, sys
import os
import sys
from optparse import OptionParser
from collections import OrderedDict as od

# Import tools
from tools.submissionTools import *
from commonTools import *
from commonObjects import *
from CollectModels import collect_models, create_empty_json

def get_options():
parser = OptionParser()
Expand All @@ -15,6 +17,12 @@ def get_options():
parser.add_option('--mode', dest='mode', default='std', help="Which script to run. Options: ['fTestOnly','fTestParallel','bkgPlotsOnly']")
parser.add_option('--jobOpts', dest='jobOpts', default='', help="Additional options to add to job submission. For Condor separate individual options with a colon (specify all within quotes e.g. \"option_xyz = abc+option_123 = 456\")")
parser.add_option('--printOnly', dest='printOnly', default=False, action="store_true", help="Dry run: print submission files only")
parser.add_option('--fitType', dest='fitType', default='mgg', help="Fit type: mgg, mjj or 2D. (default: mgg)")
parser.add_option('--mggLow', dest='mggLow', default=100, type='int', help="Lower mgg fit range (default: 100)")
parser.add_option('--mggHigh', dest='mggHigh', default=180, type='int', help="Upper mgg fit range (default: 180)")
parser.add_option('--mjjLow', dest='mjjLow', default=80, type='int', help="Lower mjj fit range (default: 80)")
parser.add_option('--mjjHigh', dest='mjjHigh', default=190, type='int', help="Upper mjj fit range (default: 190)")
parser.add_option('--noClean', dest='noClean', default=False, action="store_true", help="Do not clean up old ROOT files and plots. (default: False)")
return parser.parse_args()

(opt,args) = get_options()
Expand Down Expand Up @@ -49,6 +57,12 @@ def leave():
options['mode'] = opt.mode
options['jobOpts'] = opt.jobOpts
options['printOnly'] = opt.printOnly
options['fitType'] = opt.fitType
options['mggLow'] = opt.mggLow
options['mggHigh'] = opt.mggHigh
options['mjjLow'] = opt.mjjLow
options['mjjHigh'] = opt.mjjHigh
options['noClean'] = opt.noClean

# Delete copy of file
os.system("rm config.py")
Expand All @@ -66,6 +80,32 @@ def leave():
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RUNNING BACKGROUND SCRIPTS (END) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
sys.exit(1)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Remove existing root files
if options["fitType"] == "2D" and options["mode"] == "fTestParallel":
pattern = f"{bwd__}/outdir_{options['ext']}/CMS-*.root"
elif options["fitType"] == "mgg" and options["mode"] == "fTestParallel":
pattern = f"{bwd__}/outdir_{options['ext']}/CMS-HGG*.root"
elif options["fitType"] == "mjj" and options["mode"] == "fTestParallel":
pattern = f"{bwd__}/outdir_{options['ext']}/CMS-HBB*.root"
else:
print(" --> Invalid fitType. Exiting to avoid accidental deletion.")
sys.exit(1)
root_files = glob.glob(pattern)

if len(co.bwd__) < 5: # change this number if needed
print(" --> Directory name too short. Exiting to avoid accidental deletion.")
leave()
if len(options["ext"]) == 0:
print(" --> Extension name blank. Exiting to avoid accidental deletion.")
leave()

if len(root_files) > 0:
print(" --> Removing existing root files")
for f in root_files:
print(f" --> Removing {f}")
os.remove(f)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# If cat == auto: extract list of categories from datafile
if options['cats'] == 'auto':
Expand All @@ -83,6 +123,7 @@ def leave():
print(" --> Extension: %s"%options['ext'])
print(" --> Category offset: %g"%options['catOffset'])
print(" --> Year: %s ::: Corresponds to intLumi = %s fb^-1"%(options['year'],options['lumi']))
print(" --> Fit type: %s"%options['fitType'])
print("")
print(" --> Job information:")
print(" * Batch: %s"%options['batch'])
Expand All @@ -97,13 +138,35 @@ def leave():

# Write submission files: style depends on batch system
writeSubFiles(options)
# if options['fitType'] == "mgg" or options['fitType'] == "2D":
# writeSubFilesMgg(options)
# if options['fitType'] == "mjj" or options['fitType'] == "2D":
# writeSubFilesMjj(options)
print(" --> Finished writing submission scripts")

# Submit scripts to batch system
if not options['printOnly']:
submitFiles(options)
# if options["fitType"] == "mgg" or options['fitType'] == "2D":
# submitFilesMgg(options)
# if options["fitType"] == "mjj" or options['fitType'] == "2D":
# submitFilesMjj(options)
else:
print(" --> Running with printOnly option. Will not submit scripts")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 2D Model Construction
output_path_base = "%s/outdir_%s/"%(bwd__,options['ext'])
if options['fitType'] == "2D":
print(" --> Running 2D model construction")
if options['mode'] == "fTestParallel":
collect_models(
json_file=f"{output_path_base}/models.json",
output_path=f"{output_path_base}/CMS-2D_multipdf_{options['ext']}_%CAT.root",
ws_type="bkg-nonres",
no_clear=options['noClean'],
)
print(" --> Finished collecting models")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
leave()
35 changes: 30 additions & 5 deletions Background/runBackgroundScripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ BATCH=""
QUEUE=""
YEAR="2016"
CATOFFSET=0
FITTYPE="mgg" # mgg or mjj
MASSLOW=100
MASSHIGH=180
BINWIDTH=2

usage(){
echo "The script runs background scripts:"
Expand All @@ -43,14 +47,18 @@ echo "--isData) specified in fb^-{1} (default $DATA)) "
echo "--unblind) specified in fb^-{1} (default $UNBLIND)) "
echo "--batch) which batch system to use (None (''),HTCONDOR,IC) (default '$BATCH')) "
echo "--queue) queue to submit jobs to (specific to batch))"
echo "--fitType) mgg or mjj (default $FITTYPE)) "
echo "--massLow) lower fit range (default $MASSLOW)) "
echo "--massHigh) upper fit range (default $MASSHIGH)) "
echo "--binWidth) bin width for plots (default $BINWIDTH)) "
}


#------------------------------ parsing


# options may be followed by one colon to indicate they have a required argument
if ! options=$(getopt -u -o hi:p:f: -l help,inputFile:,procs:,flashggCats:,ext:,catOffset:,fTestOnly,pseudoDataOnly,bkgPlotsOnly,pseudoDataDat:,sigFile:,seed:,intLumi:,year:,unblind,isData,batch:,queue: -- "$@")
if ! options=$(getopt -u -o hi:p:f: -l help,inputFile:,procs:,flashggCats:,ext:,catOffset:,fTestOnly,pseudoDataOnly,bkgPlotsOnly,pseudoDataDat:,sigFile:,seed:,intLumi:,year:,unblind,isData,batch:,queue:,fitType:,massLow:,massHigh:,binWidth: -- "$@")
then
# something went wrong, getopt will put out an error message for us
exit 1
Expand Down Expand Up @@ -78,6 +86,10 @@ case $1 in
--unblind) UNBLIND=1;;
--batch) BATCH=$2; shift;;
--queue) QUEUE=$2; shift;;
--fitType) FITTYPE=$2; shift;;
--massLow) MASSLOW=$2; shift;;
--massHigh) MASSHIGH=$2; shift;;
--binWidth) BINWIDTH=$2; shift;;

(--) shift; break;;
(-*) usage; echo "$0: error - unrecognized option $1" 1>&2; usage >> /dev/stderr; exit 1;;
Expand Down Expand Up @@ -134,6 +146,7 @@ echo "--> Create fake data by fitting simulations, throwing toys and adding data
echo "--> generating $INTLUMI fb^{-1} of pseudodata."
echo "--------------------------------------"

# TODO: Add binWidth arg?
echo " ./bin/pseudodataMaker -i $PSEUDODATADAT --pseudodata 1 --plotdir $OUTDIR/pseudoData -f $CATS --seed $SEED --intLumi $INTLUMI "
./bin/pseudodataMaker -i $PSEUDODATADAT --pseudodata 1 --plotdir $OUTDIR/pseudoData -f $CATS --seed $SEED --intLumi $INTLUMI -y $OUTDIR/pseudoData/yields_pseudodata.txt
FILE=$OUTDIR/pseudoData/pseudoWS.root
Expand All @@ -159,8 +172,14 @@ if [ $ISDATA == 1 ]; then
OPT=" --isData 1"
fi

echo " ./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HGG_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET"
./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HGG_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET
if [ $FITTYPE == "mgg" ]; then
echo " ./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HGG_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET --massLow $MASSLOW --massHigh $MASSHIGH --fitType $FITTYPE --binWidth $BINWIDTH"
./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HGG_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET --massLow $MASSLOW --massHigh $MASSHIGH --fitType $FITTYPE --binWidth $BINWIDTH
fi
if [ $FITTYPE == "mjj" ]; then
echo " ./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HBB_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET --massLow $MASSLOW --massHigh $MASSHIGH --fitType $FITTYPE --binWidth $BINWIDTH"
./bin/fTest -i $FILE --saveMultiPdf $OUTDIR/CMS-HBB_multipdf_$EXT_$CATS.root -D $OUTDIR/bkgfTest$DATAEXT -f $CATS $OPT --year $YEAR --catOffset $CATOFFSET --massLow $MASSLOW --massHigh $MASSHIGH --fitType $FITTYPE --binWidth $BINWIDTH
fi

OPT=""
fi
Expand All @@ -180,8 +199,14 @@ fi
if [ $UNBLIND == 1 ]; then
OPT=" --unblind"
fi
echo "./scripts/subBkgPlots.py -b CMS-HGG_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L 100 -H 180 -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR"
./scripts/subBkgPlots.py -b CMS-HGG_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L 100 -H 180 -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR
if [ $FITTYPE == "mgg" ]; then # TODO: Add binWidth arg?
echo "./scripts/subBkgPlots.py -b CMS-HGG_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L $MASSLOW -H $MASSHIGH -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR"
./scripts/subBkgPlots.py -b CMS-HGG_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L $MASSLOW -H $MASSHIGH -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR
fi
if [ $FITTYPE == "mjj" ]; then # TODO: Add binWidth arg?
echo "./scripts/subBkgPlots.py -b CMS-HBB_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L $MASSLOW -H $MASSHIGH -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR"
./scripts/subBkgPlots.py -b CMS-HBB_multipdf_$EXT.root -d $OUTDIR/bkgPlots$DATAEXT -S 13 --isMultiPdf --useBinnedData --doBands --massStep 1 $SIG -L $MASSLOW -H $MASSHIGH -f $CATS -l $CATS --intLumi $INTLUMI $OPT --batch $BATCH -q $QUEUE --year $YEAR
fi

# FIX THIS FOR CONDOR:
#continueLoop=1
Expand Down
Loading