"TRACE", "NOTSET" ]) args = argParser.parse_args() # 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()
if (item.startswith("add") or item == "incl") ] add.sort() fit = "_".join(["postFit" if args.postFit else "preFit"] + add) 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):
# 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 ) args.year = "combined" lumi_scale = int(35.92 + 41.53 + 59.74) #bf = "ctZ_-0.25_ctZI_-0.083" plotDirectory = "/mnt/hephy/cms/lukas.lechner/www/TTGammaEFT/fitEFT/combined/v0/%s/%s"%(args.bestfit, "postfit" if args.postFit else "prefit") options = "--rMin 0.99 --rMax 1.01 --cminDefaultMinimizerTolerance=0.1 --cminDefaultMinimizerStrategy=0 --freezeParameters r" # replace the combineResults object by the substituted card object cardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF_addMisIDSF/ctZ_0_ctZI_0.txt" Results = CombineResults( cardFile=cardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False ) #rebinCard = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFTAll_SR4pPtUnfoldEFTAll_addDYSF_addMisIDSF/ctZ_0_ctZI_0.txt" ##subCardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFTAll_SR4pPtUnfoldEFTAll_addDYSF_addMisIDSF/ctZ_0_ctZI_0.txt" #subCardFile = Results.createRebinnedResults( rebinCard, skipStatOnly=True, setParameters="r=1", options=options ) #Results = CombineResults( cardFile=subCardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False, rebinnedCardFile=rebinCard ) bfcardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF_addMisIDSF/%s.txt"%args.bestfit Resultsbf = CombineResults( cardFile=bfcardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False ) #bfrebinCard = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFTAll_SR4pPtUnfoldEFTAll_addDYSF_addMisIDSF/ctZ_-0.25_ctZI_-0.083.txt" ##bfsubCardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFTAll_SR4pPtUnfoldEFTAll_addDYSF_addMisIDSF/ctZ_-0.25_ctZI_-0.083.txt" #bfsubCardFile = Resultsbf.createRebinnedResults( bfrebinCard, skipStatOnly=True, setParameters="r=1", options=options ) #Resultsbf = CombineResults( cardFile=bfsubCardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False, rebinnedCardFile=bfrebinCard ) def formatLabel( label ): reg = label.split(" ")[3]
logger_rt = logger_rt.get_logger(args.logLevel, logFile=None) args.year = "combined" lumi_scale = int(35.92 + 41.53 + 59.74) #bf = "ctZ_-0.25_ctZI_-0.083" plotDirectory = "/mnt/hephy/cms/lukas.lechner/www/TTGammaEFT/fitEFTJHEP/combined/v1/%s/%s" % ( args.bestfit, "postfit" if args.postFit else "prefit") #options = "--rMin 0.99 --rMax 1.01 --cminDefaultMinimizerTolerance=0.1 --cminDefaultMinimizerStrategy=0 --freezeParameters r" # replace the combineResults object by the substituted card object cardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF_splitScale/ctZ_0_ctZI_0.txt" #cardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF/ctZ_0_ctZI_0.txt" Results = CombineResults(cardFile=cardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False) #rebinCard = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldAll_SR4pPtUnfoldAll_addDYSF_splitScale/ctZ_0_ctZI_0.txt" ##subCardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldAll_SR4pPtUnfoldAll_addDYSF_splitScale/ctZ_0_ctZI_0.txt" #subCardFile = Results.createRebinnedResults( rebinCard, skipStatOnly=True, setParameters="r=1", options=options ) #Results = CombineResults( cardFile=subCardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False, rebinnedCardFile=rebinCard ) bfcardFile = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF_splitScale/%s.txt" % args.bestfit Resultsbf = CombineResults(cardFile=bfcardFile, plotDirectory=plotDirectory, year=args.year, bkgOnly=False, isSearch=False) cardFilem045 = "/scratch-cbe/users/lukas.lechner/TTGammaEFT/cache_read/analysis/COMBINED/limits/withbkg/cardFiles/defaultSetup/observed/SR3PtUnfoldEFT_SR4pPtUnfoldEFT_VG3_VG4p_misDY3_misDY4p_addDYSF_splitScale/ctZ_-0.45_ctZI_0.txt"
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")
logger.info("Plotting from cardfile %s" % cardFile) sr = [r for r in args.cardfile.split("_") if "SR" in r][0] sr = sr.replace("M3", "") lintests = [0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4] hist = ROOT.TH1F("hist", "hist", len(lintests), min(lintests) - 0.05, max(lintests) + 0.05) hist.style = styles.errorStyle(ROOT.kAzure - 3, width=2) hist.legendText = "SSM (MC Signal / Multiplier)" #hist.legendText = "SSM (Exp.Observation #times Multiplier)" Results = CombineResults(cardFile=cardFile, plotDirectory=plot_directory_, year=args.year, bkgOnly=args.bkgOnly, isSearch=False) for val in lintests: postFit = Results.runLinearityTest(val) bin = hist.FindBin(val) print str(val), str(postFit) hist.SetBinContent(bin, postFit.val) hist.SetBinError(bin, postFit.sigma / val) line = ROOT.TLine( min(lintests) - 0.05, min(lintests) - 0.05, max(lintests) + 0.05,
if args.expected: add += ["expected"] if args.linTest != 1: add += ["linTest" + str(args.linTest)] fit = "_".join(["postFit" if args.postFit else "preFit"] + add) 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, bkgOnly=args.bkgOnly, isSearch=False) #print Results.fitResult #cov = Results.getCovarianceHisto(postFit=True) #for i in range( cov.GetNbinsX() ): # for j in range( cov.GetNbinsY() ): # print i+1, j+1, cov.GetBinContent(i+1,j+1) #sys.exit() #Results.runFitDiagnostics() #print Results.getUncertaintiesFromTxtCard( postFit=False )["Bin0"]["Bin39"]["DY_misID"] #sys.exit() # add all systematics histograms allNuisances = Results.getNuisancesList(addRateParameter=args.postFit)
argParser.add_argument("--cardfile", action='store', default='', help="which cardfile?") argParser.add_argument("--substituteCard", action='store', default=None, help="which cardfile to substitute the plot with?") argParser.add_argument("--plotNuisances", action='store', nargs="*", default=None, help="plot specific nuisances?") args = argParser.parse_args() # 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 ) cardFile = os.path.join( cache_directory, "analysis", str(args.year) if args.year != "combined" else "COMBINED", args.carddir, args.cardfile+".txt" ) if args.substituteCard: cardFile = cardFile.replace(".txt","/%s.txt"%args.substituteCard) Results = CombineResults( cardFile=cardFile, plotDirectory=".", year=args.year, bkgOnly=False, isSearch=False ) args.plotNuisances = [ # "QCD_0b_nJet_dependence", # "QCD_0b_normalization", "QCD_1b_nJet_dependence", "QCD_1b_normalization", "QCD_normalization", "TT_normalization", "WGamma_nJet_dependence", "WGamma_normalization", "WGamma_pT_Bin1", "WGamma_pT_Bin2", "ZGamma_nJet_dependence",