示例#1
0
    def save_yields(hists, ofn, scale_factors):
        hmc, hdata = hists
        if not os.path.exists(os.path.dirname(ofn)):
            os.makedirs(os.path.dirname(ofn))
        of = open(ofn, "w+")
        items = hmc.items() + [
            ("MC", reduce(lambda x,y: x+y, hmc.values())),
            ("data", hdata)
        ]

        #Get the uncertainties that remain after the BDT fit
        errs_fit = dict()
        for k, v in hmc.items():
            unc_fit = 0

            def ratio(x):
                """
                Calculates the relative uncertainty of the scale factor of a
                process

                Args:
                    x - process name185GB
                """
                return scale_factors[x][1] / scale_factors[x][0]
            if k.lower() in ["dyjets", "diboson", "wjets"]:
                unc_fit = ratio("wzjets")
            elif k.lower() in ["ttjets", "twchan", "schan"]:
                unc_fit = ratio("top")
            elif k.lower() in ["qcd"]:
                unc_fit = ratio("qcd")
            elif k.lower() in ["tchan"]:
                unc_fit = ratio("tchan")
            errs_fit[k.lower()] = unc_fit * v.Integral()
            logger.debug("Fit uncertainty forforfor {0}={1:.2f}".format(k, unc_fit))

        err_vec = np.array([
            errs_fit["tchan"], errs_fit["ttjets"], errs_fit["twchan"],
            errs_fit["schan"], errs_fit["qcd"], errs_fit["wjets"],
            errs_fit["dyjets"], errs_fit["diboson"]
        ])

        #Naive error
        #errs_fit["mc"] = math.sqrt(sum([x**2 for x in errs_fit.values()]))

        #Calculate the error using the covariance matrix
        errs_fit["mc"] = math.sqrt(np.dot(err_vec, np.dot(corr_mat, err_vec.T)))

        for k, v in sorted(items, key=lambda x: x[0]):
            k = k.lower()
            i, e = calc_int_err(v)

            fit_unc = errs_fit.get(k, 0)

            #Total error is statistical (Poisson) + fit error (indep.)
            tot_err = math.sqrt(e**2 + fit_unc**2)

            of.write("%s | %.2f | %.2f \n" % (k, i, tot_err))
        of.close()
示例#2
0
 def save_yields(hists, ofn):
     hmc, hdata = hists
     if not os.path.exists(os.path.dirname(ofn)):
         os.makedirs(os.path.dirname(ofn))
     of = open(ofn, "w+")
     for k, v in sorted(
         hmc.items() + [("MC", reduce(lambda x, y: x + y, hmc.values())), ("data", hdata)], key=lambda x: x[0]
     ):
         i, e = calc_int_err(v)
         of.write("%s | %.2f | %.2f\n" % (k, i, e))
     of.close()
示例#3
0
def yield_string(h, hn=None):
    """
    Returns the yield of a Hist in a human-readable format.

    Args:
        h: a Hist of the yield to process

    Keywords:
        hn: the name of the process to be printed.

    Returns: the final yield string
    """
    if not hn:
        hn = hn.GetTitle()
    _int, _err = calc_int_err(h)
    return "%s | %.2f | %.2f\n" % (hn, _int, _err)