Exemple #1
0
        if (hypo != 0):
            eul2 = 1.10 * hypo.GetExpectedUpperLimit(2)
            del hypo
            hypo = 0
            hypo = RooStats.DoHypoTestInversion(w, nToys, calcType,
                                                testStatType, True, nPoints, 0,
                                                eul2)
            nPointsRemoved = hypo.ExclusionCleanup()
            print "ExclusionCleanup() removed " + str(
                nPointsRemoved
            ) + " scan point(s) for hypo test inversion: " + hypo.GetName()

        ##store ul as nice plot ..
        if (hypo != 0):
            RooStats.AnalyzeHypoTestInverterResult(
                hypo, calcType, testStatType, True, nPoints, sigSamples[0],
                "_" + sigSamples[0] + ".eps")

        ##save complete hypotestinverterresult to file
        if (hypo != 0):
            outfile.cd()
            if hypo.ArraySize() > 0 and (min_CLs(hypo) > 0.05
                                         or max_CLs(hypo) < 0.05):
                print "ERROR Final CLs value not below threshold of 0.05 or initial CLs value not above threshold of 0.05 - upper limit scan most likely failed."
                print "ERROR Will store result only for debugging purposes - do not use it in contour plots!"
                hypName = "debug_" + sigSamples[0]
            elif hypo.ArraySize() == 0:
                print "ERROR All fits seem to have failed - cannot compute upper limit!"
                hypName = "debug_" + sigSamples[0]
            else:
                hypName = "hypo_" + sigSamples[0]
Exemple #2
0
def latexfitresults(filename,
                    poiname='mu_SIG',
                    lumiFB=1.0,
                    nTOYS=3000,
                    nPoints=20,
                    muRange=40,
                    asimov=False,
                    wname='combined',
                    outputPrefix=""):
    """
  Calculate before/after-fit yields in all channels given
  
  @param filename The filename containing afterFit workspace
  @param poiname Name of ParameterOfInterest = POI (default='mu_SIG')
  @param lumiFB Given lumi in fb-1 to translate upper limit on N_events into xsection limit (default='1.0')
  @param nTOYS Number of toys to be run
  @param asimov Boolean to run asimov or not (default=False)
  @param nPoints Number of points of mu_SIG ranges to scan
  @param muRange Maximim value of mu_SIG to probe
  @param wname RooWorkspace name in file (default='combined')
  @param outputPrefix Prefix of the output file name (default="")
  """
    """
  pick up workspace from file
  """
    workspacename = wname
    w = Util.GetWorkspaceFromFile(filename, workspacename)
    if w == None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)
    """
  Set the POI in ModelConfig
  """
    if len(poiname) == 0:
        print " "
    else:
        modelConfig = w.obj("ModelConfig")
        poi = w.var(poiname)
        if poi == None:
            print "ERROR : Cannot find POI with name: ", poiname, " in workspace from file ", filename
            sys.exit(1)
        modelConfig.SetParametersOfInterest(RooArgSet(poi))
        modelConfig.GetNuisanceParameters().remove(poi)
    """
  set some default values for nToys, calculator type and nPoints to be scanned
  """
    ntoys = 3000
    calctype = 0  # toys = 0, asymptotic (asimov) = 2
    nPoints = nPoints

    if nTOYS != 3000 and nTOYS > 0:
        ntoys = nTOYS
    if asimov:
        calctype = 2
    """
  set the range of POI to be scanned and perform HypoTest inversion
  """
    nCPUs = 8
    murangelow = 0.0
    murangehigh = muRange  #set here -1. if you want to have automatic determined scan range, if using values != -1, please check the log file if the scan range was large enough
    hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True,
                                              nPoints, murangelow, murangehigh,
                                              False, False, "ModelConfig", "",
                                              "obsData", "")
    #hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True, nPoints, murangelow, murangehigh, False, False, "ModelConfig", "", "obsData", "", nCPUs)

    nRemoved = hti_result.ExclusionCleanup()
    if nRemoved > 0:
        print "WARNING: removed %d points from hti_result" % nRemoved

    #store plot
    RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True,
                                           nPoints,
                                           "%s%s" % (outputPrefix, poiname),
                                           ".eps")
    RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True,
                                           nPoints,
                                           "%s%s" % (outputPrefix, poiname),
                                           ".pdf")
    RooStats.AnalyzeHypoTestInverterResult(hti_result, calctype, 3, True,
                                           nPoints,
                                           "%s%s" % (outputPrefix, poiname),
                                           ".png")

    outFileName = "./%shtiResult_poi_%s_ntoys_%d_calctype_%s_nPoints_%d.root" % (
        outputPrefix, poiname, ntoys, calctype, nPoints)

    hti_result.SaveAs(outFileName)
    hti_result.Print()
    """
  get the upper limit on N_obs out of hypotest result, and transform to limit on visible xsection
  """
    uL_nobsinSR = hti_result.UpperLimit()
    uL_visXsec = uL_nobsinSR / lumiFB
    """
  get the expected upper limit and one scan point up and down to calculate the error on upper limit
  """
    uL_nexpinSR = hti_result.GetExpectedUpperLimit(0)
    uL_nexpinSR_P = hti_result.GetExpectedUpperLimit(1)
    uL_nexpinSR_M = hti_result.GetExpectedUpperLimit(-1)
    if uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M:
        print " \n something very strange, either the uL_nexpinSR > uL_nexpinSR_P or uL_nexpinSR < uL_nexpinSR_M"
        print "  uL_nexpinSR = ", uL_nexpinSR, " uL_nexpinSR_P = ", uL_nexpinSR_P, " uL_nexpinSR_M = ", uL_nexpinSR_M
    uL_nexpinSRerrP = hti_result.GetExpectedUpperLimit(1) - uL_nexpinSR
    uL_nexpinSRerrM = uL_nexpinSR - hti_result.GetExpectedUpperLimit(-1)
    """
  find the CLB values at indexes above and below observed CLs p-value
  """
    CLB_P = 0.
    CLB_M = 0.
    mu_P = 0.
    mu_M = 0.
    index_P = 0
    indexFound = False
    for iresult in range(hti_result.ArraySize()):
        xval = hti_result.GetXValue(iresult)
        yval = hti_result.GetYValue(iresult)
        if xval > uL_nobsinSR and not indexFound:
            index_P = iresult
            CLB_P = hti_result.CLb(iresult)
            mu_P = xval
            if iresult > 0:
                CLB_M = hti_result.CLb(iresult - 1)
                mu_M = hti_result.GetXValue(iresult - 1)
                indexFound = True
    """
  interpolate (linear) the value of CLB to be exactly above upperlimit p-val
  """
    try:
        alpha_CLB = (CLB_P - CLB_M) / (mu_P - mu_M)
        beta_CLB = CLB_P - alpha_CLB * mu_P
        # CLB is taken as the point on the CLB curve for the same poi value, as the observed upperlimit
        CLB = alpha_CLB * uL_nobsinSR + beta_CLB
    except ZeroDivisionError:
        print "WARNING ZeroDivisionError while calculating CLb. Setting CLb=0."
        CLB = 0.0

    print "\n\n\n\n  ***---  now doing p-value (s=0) calculation ---*** \n\n\n\n"
    """
  reset parameter values and errors for p(s=0) calculation by reopening workspace
  """
    w2 = Util.GetWorkspaceFromFile(filename, workspacename)

    if w2 == None:
        print "ERROR : Cannot open workspace : ", workspacename
        sys.exit(1)
    """
  calculate p(s=0) from the workspace given
  """
    pval = RooStats.get_Presult(w2, False, ntoys, calctype)
    #print pval
    #sigma = StatTools.GetSigma(pval)
    #print sigma

    UL = {}
    UL["visXsec"] = uL_visXsec
    UL["nObsInSR"] = uL_nobsinSR
    UL["nExpInSR"] = uL_nexpinSR
    UL["nExpInSRPlus1Sigma"] = uL_nexpinSRerrP
    UL["nExpInSRMinus1Sigma"] = uL_nexpinSRerrM
    UL["CLb"] = CLB
    UL["p0"] = pval
    UL["Z"] = StatTools.GetSigma(pval)

    return UL