示例#1
0
        #Util.SetInterpolationCode(w,4)

        print "Processing analysis " + sigSamples[0]

        #if 'onestepCC' in sigSamples[0] and int(sigSamples[0].split("_")[3])>900:
        #    w.var("mu_SIG").setMax(50000.)
        #    print "Gluino mass above 900 - extending mu_SIG range \n"

        ## first asumptotic limit, to get a quick but reliable estimate for the upper limit
        ## dynamic evaluation of ranges
        testStatType = 3
        calcType = 2  #asympt
        nToys = 1000
        nPoints = 20  #mu sampling
        hypo = RooStats.DoHypoTestInversion(w, 1, 2, testStatType, True, 20, 0,
                                            -1)

        # then reevaluate with proper settings

        if (hypo != 0):
            hypo.ExclusionCleanup()

            i = 0
            while (i < 5):
                if min_CLs(hypo) > 0.05:
                    print "Starting rescan iteration: " + str(i)
                    hypo = RedoScan(w, testStatType, hypo)
                    if (hypo != 0):
                        hypo.ExclusionCleanup()
                else:
                    break
def latexfitresults(filename,
                    poiname='mu_SIG',
                    lumiFB=1.0,
                    nTOYS=3000,
                    asimov=False,
                    wname='combined'):
    """
  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 wname RooWorkspace name in file (default='combined')
  """
    """
  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 = 20

    if nTOYS != 3000 and nTOYS > 0:
        ntoys = nTOYS
    if asimov:
        calctype = 2
    """
  set the range of POI to be scanned and perform HypoTest inversion
  """
    murangelow = 0.0
    murangehigh = 40.0
    hti_result = RooStats.DoHypoTestInversion(w, ntoys, calctype, 3, True,
                                              npoints, murangelow, murangehigh)
    """
  save and print the HypoTest result
  """
    outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str(
        ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str(
            npoints) + ".root"
    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 = 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)

    ulList = [
        uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM,
        CLB, pval
    ]

    return ulList
示例#3
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