plotDirectory = os.path.join(plot_directory, "fit", str(args.year), fit, dirName) cardFile = os.path.join( cache_directory, "analysis", str(args.year) if args.year != "combined" else "COMBINED", args.carddir, args.cardfile + ".txt") logger.info("Plotting from cardfile %s" % cardFile) # replace the combineResults object by the substituted card object Results = CombineResults(cardFile=cardFile, plotDirectory=plotDirectory, year=args.year, isSearch=False) nuisances = [ p for p in Results.getPulls().keys() if p != "r" and not "prop" in p ] #plotBins = [10,11,12,13,14,15,16,17,18,19,20,21] plotBins = [34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45] # get region histograms if args.year == "combined": hists = Results.getRegionHistos(postFit=args.postFit, nuisances=nuisances, plotBins=plotBins) channels = ["dc_2016", "dc_2017", "dc_2018"] # hists_tmp = Results.getRegionHistos( postFit=args.postFit, nuisances=nuisances, plotBins=plotBins ) # for i, dir in enumerate(Results.channels): # print i, dir # if i == 0: # hists = {key:hist.Clone(str(i)+dir+key) for key, hist in hists_tmp[dir].iteritems() if key not in nuisances}
# logger import Analysis.Tools.logger as logger import RootTools.core.logger as logger_rt logger = logger.get_logger(args.logLevel, logFile=None) logger_rt = logger_rt.get_logger(args.logLevel, logFile=None) plotDirectory = "/mnt/hephy/cms/lukas.lechner/www/TTGammaEFT/pullPlot/combined/v1/" cardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/cardFiles/defaultSetup/observed/SR3M3_SR4pM3_VG3_VG4p_misDY3_misDY4p_addDYSF_incl_splitScale.txt" Results = CombineResults(cardFile=cardFile, plotDirectory=plotDirectory, year="combined", bkgOnly=False, isSearch=False) pulls = Results.getPulls(postFit=True) print pulls["JEC_FlavorQCD"] print pulls["ISR"] print pulls["FSR"] #sys.exit() print pulls["WGamma_normalization"] print 1 + pulls["ZGamma_normalization"] print pulls["MisID_normalization_2016"] print pulls["MisID_normalization_2017"] print pulls["MisID_normalization_2018"] #sys.exit() nuisances = [ { "tWgamma_normalization": "W#gamma normalization" },
def wrapper(): c = cardFileWriter.cardFileWriter() c.releaseLocation = combineReleaseLocation txtcards = {} shapecards = {} # get the seperated cards for year in years: baseDir = os.path.join(cache_directory, "analysis", str(year), "limits") if args.parameters: baseDir = os.path.join(baseDir, "withbkg" if args.withbkg else "withoutbkg") limitDir = os.path.join(baseDir, "cardFiles", args.label, "expected" if args.expected else "observed") if args.parameters: cardFileNameTxt = os.path.join(limitDir, "_".join(regionNames), eft + ".txt") else: cardFileNameTxt = os.path.join(limitDir, "_".join(regionNames) + ".txt") cardFileNameShape = cardFileNameTxt.replace(".txt", "_shapeCard.txt") #print cardFileName if not os.path.isfile(cardFileNameTxt): #raise IOError("File %s doesn't exist!"%cardFileName) print "File %s doesn't exist!" % cardFileNameTxt return txtcards[year] = cardFileNameTxt shapecards[year] = cardFileNameShape baseDir = baseDir.replace('2018', 'COMBINED') limitDir = limitDir.replace('2018', 'COMBINED') if not os.path.isdir(limitDir): os.makedirs(limitDir) txtcombinedCard = c.combineCards(txtcards, txtFileOnly=bool(args.parameters)) shapecombinedCard = c.combineCards(shapecards, txtFileOnly=bool(args.parameters)) combinedCard = txtcombinedCard if args.useTxt else shapecombinedCard print combinedCard if args.parameters: cardFileNameTxt = os.path.join(limitDir, "_".join(regionNames), eft + ".txt") else: cardFileNameTxt = os.path.join(limitDir, "_".join(regionNames) + ".txt") cardFileNameShape = cardFileNameTxt.replace(".txt", "_shape.root") cardFileName = cardFileNameTxt logger.info("File %s found. Reusing." % cardFileName) cardFileNameShape = cardFileNameShape.replace('.root', 'Card.txt') cardFileName = cardFileNameTxt if args.useTxt else cardFileNameShape if args.parameters: options = "--expectSignal=1 --setParameters r=1" nll = c.calcNLL(fname=cardFileName, options=options) nllCache.add(sEFTConfig, nll, overwrite=True) print nll print sEFTConfig res = c.calcLimit(cardFileName, options=options + " --freezeParameters r") # Results = CombineResults( cardFile=cardFileNameTxt, plotDirectory="./", year="combined", bkgOnly=args.bkgOnly, isSearch=False ) # postFit = Results.getPulls( postFit=True ) # if "EFT_nJet" in postFit.keys(): # freezeParams = "EFT_nJet=%f,r=1"%postFit["EFT_nJet"].val # print freezeParams # c.calcNuisances( cardFileName, bonly=False, options="--expectSignal=1 --freezeParameters EFT_nJet,r --setParameters %s --rMin 0.999 --rMax 1.001 --cminDefaultMinimizerTolerance=0.001"%freezeParams ) # else: c.calcNuisances( cardFileName, bonly=False, options= "--expectSignal=1 --freezeParameters r --setParameters r=1 --rMin 0.99 --rMax 1.01 --cminDefaultMinimizerTolerance=0.01" ) else: options = "" if args.freezeR: options = "--setParameters r=1 --freezeParameters r" res = c.calcLimit(cardFileName, options=options) # options for running the bkg only fit with r=1 options = " --customStartingPoint --expectSignal=1 --cminDefaultMinimizerStrategy=0" if args.freezeR: # options += " --setParameters r=1.14 --freezeParameters r --cminDefaultMinimizerTolerance=0.1" # options += " --rMin 1.13 --rMax 1.15 --cminDefaultMinimizerTolerance=0.1" # options += " --rMin 0.99 --rMax 1.01" # options += " --redefineSignalPOI Signal_mu_4p_Bin0_2018 --freezeParameters r --setParameters r=1" # --rMin 1.11 --rMax 1.13" options += " --rMin 0.99 --rMax 1.01 --freezeParameters r --setParameters r=1" # --rMin 1.11 --rMax 1.13" else: options += " --rMin 0.5 --rMax 1.5 --cminDefaultMinimizerTolerance=0.1" c.calcNuisances(cardFileName, bonly=args.bkgOnly, options=options) if args.freezeSigUnc: Results = CombineResults(cardFile=cardFileNameTxt, plotDirectory="./", year="combined", bkgOnly=args.bkgOnly, isSearch=False) postFit = Results.getPulls(postFit=True) freezeParams = [ p for p in postFit.keys() if p.startswith("Signal_") ] pulls = [p + "=" + str(postFit[p].val) for p in freezeParams] print pulls c.calcNuisances(cardFileName, bonly=args.bkgOnly, options=options + " --setParameters %s --freezeParameters %s" % (",".join(pulls), ",".join(freezeParams))) if args.plot and not args.parameters: path = os.environ["CMSSW_BASE"] path += "/src/TTGammaEFT/plots/plotsLukas/regions" if args.expected: cdir = "limits/cardFiles/defaultSetup/expected" else: cdir = "limits/cardFiles/defaultSetup/observed" cfile = cardFileNameTxt.split("/")[-1].split(".")[0] cmd = "python %s/fitResults.py --carddir %s --cardfile %s --linTest %s --year %s --plotCovMatrix --plotRegionPlot %s --cores %i %s %s %s %s %s %s" % ( path, cdir, cfile, str(args.linTest), "combined", "--bkgOnly" if args.bkgOnly else "", 1, "--expected" if args.expected else "", "--misIDPOI" if args.misIDPOI else "", "--wgPOI" if args.wgPOI else "", "--ttPOI" if args.ttPOI else "", "--dyPOI" if args.dyPOI else "", "--wJetsPOI" if args.wJetsPOI else "") # cmd = "python %s/fitResults.py --carddir %s --cardfile %s --linTest %s --year %s --plotRegionPlot %s --cores %i %s %s %s %s %s %s"%(path, cdir, cfile, str(args.linTest), "combined", "--bkgOnly" if args.bkgOnly else "", 1, "--expected" if args.expected else "", "--misIDPOI" if args.misIDPOI else "", "--wgPOI" if args.wgPOI else "", "--ttPOI" if args.ttPOI else "", "--dyPOI" if args.dyPOI else "", "--wJetsPOI" if args.wJetsPOI else "") logger.info("Executing plot command: %s" % cmd) os.system(cmd) cmd = "python %s/fitResults.py --carddir %s --cardfile %s --linTest %s --year %s --plotCorrelations --plotCovMatrix --plotRegionPlot --plotImpacts --postFit %s --cores %i %s %s %s %s %s %s" % ( path, cdir, cfile, str(args.linTest), "combined", "--bkgOnly" if args.bkgOnly else "", 1, "--expected" if args.expected else "", "--misIDPOI" if args.misIDPOI else "", "--wgPOI" if args.wgPOI else "", "--ttPOI" if args.ttPOI else "", "--dyPOI" if args.dyPOI else "", "--wJetsPOI" if args.wJetsPOI else "") # cmd = "python %s/fitResults.py --carddir %s --cardfile %s --linTest %s --year %s --plotRegionPlot --postFit %s --cores %i %s %s %s %s %s %s"%(path, cdir, cfile, str(args.linTest), "combined", "--bkgOnly" if args.bkgOnly else "", 1, "--expected" if args.expected else "", "--misIDPOI" if args.misIDPOI else "", "--wgPOI" if args.wgPOI else "", "--ttPOI" if args.ttPOI else "", "--dyPOI" if args.dyPOI else "", "--wJetsPOI" if args.wJetsPOI else "") # cmd = "python %s/fitResults.py --carddir %s --cardfile %s --year %s --plotImpacts --postFit %s --cores %i %s %s %s %s %s"%(path, cdir, cfile, "combined", "--bkgOnly" if args.bkgOnly else "", 1, "--expected" if args.expected else "", "--misIDPOI" if args.misIDPOI else "", "--ttPOI" if args.ttPOI else "", "--dyPOI" if args.dyPOI else "", "--wJetsPOI" if args.wJetsPOI else "") logger.info("Executing plot command: %s" % cmd) os.system(cmd) ################### # extract the SFs # ################### if not args.parameters: default_QCD_unc = 0.5 default_HadFakes_unc = 0.05 default_ZG_unc = 0.3 default_Other_unc = 0.30 default_misID4p_unc = 0.2 default_ZG4p_unc = 0.4 default_WG4p_unc = 0.2 default_DY_unc = 0.1 unc = { "QCD_0b_normalization": default_QCD_unc, "QCD_1b_normalization": default_QCD_unc, "ZGamma_normalization": default_ZG_unc, "Other_normalization": default_Other_unc, "fake_photon_DD_normalization": default_HadFakes_unc, "MisID_nJet_dependence_2016": default_misID4p_unc, "MisID_nJet_dependence_2017": default_misID4p_unc, "MisID_nJet_dependence_2018": default_misID4p_unc, "MisID_extrapolation_2016": 0.1, "MisID_extrapolation_2017": 0.1, "MisID_extrapolation_2018": 0.1, "ZGamma_nJet_dependence": default_ZG4p_unc, "WGamma_nJet_dependence": default_WG4p_unc, "ZJets_normalization": default_DY_unc, } rateParam = [ "MisID_normalization_2016", "MisID_normalization_2017", "MisID_normalization_2018", "WGamma_normalization", # "ZJets_normalization", ] Results = CombineResults(cardFile=cardFileNameTxt, plotDirectory="./", year="combined", bkgOnly=args.bkgOnly, isSearch=False) postFit = Results.getPulls(postFit=True) if not os.path.isdir("logs"): os.mkdir("logs") write_time = time.strftime("%Y %m %d %H:%M:%S", time.localtime()) with open("logs/cardFiles.dat", "a") as f: f.write(cardFileNameTxt + "\n") with open("logs/scaleFactors.dat", "a") as f: f.write("\n\n" + cardFileNameTxt + "\n") sf = "{:20} {:4.2f} +- {:4.2f}".format("POI", postFit["r"].val, postFit["r"].sigma) print sf f.write( str("combined") + ": " + write_time + ": " + "_".join(regionNames) + ": " + sf + "\n") for sf_name in unc.keys(): if sf_name not in postFit.keys(): continue sf = "{:20} {:4.2f} +- {:4.2f}".format( sf_name, 1 + (postFit[sf_name].val * unc[sf_name]), postFit[sf_name].sigma * unc[sf_name]) print sf f.write( str("combined") + ": " + write_time + ": " + "_".join(regionNames) + ": " + sf + "\n") for sf_name in rateParam: if sf_name not in postFit.keys(): continue sf = "{:20} {:4.2f} +- {:4.2f}".format(sf_name, postFit[sf_name].val, postFit[sf_name].sigma) print sf f.write( str("combined") + ": " + write_time + ": " + "_".join(regionNames) + ": " + sf + "\n")