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
Пример #2
0
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, asimov=False, wname='combined'):

  workspacename=wname
 
  w = Util.GetWorkspaceFromFile(filename,workspacename)
  
  if w==None:
    print "ERROR : Cannot open workspace : ", workspacename
    sys.exit(1) 

  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)

  ntoys = 3000
  calctype = 0   # toys = 0, asymptotic (asimov) = 2
  npoints = 20

  if nTOYS != 3000 and nTOYS>0:
    ntoys = nTOYS
  if asimov:
    calctype = 2
 
  # hti_result = RooStats.MakeUpperLimitPlot(poiname,w,calctype,3,ntoys,True,npoints)
##   #   RooStats::MakeUpperLimitPlot(const char* fileprefix,
##   # 			     RooWorkspace* w,
##   # 			     int calculatorType ,                         # toys = 0, asymptotic (asimov) = 2
##   # 			     int testStatType , 
##   # 			     int ntoys,
##   # 			     bool useCLs ,  
##   # 			     int npoints )

  murangelow = 0.0
  murangehigh = 40.0
  hti_result = RooStats.DoHypoTestInversion(w,ntoys,calctype,3,True,npoints,murangelow,murangehigh)

  outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str(ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str(npoints) + ".root"
  hti_result.SaveAs(outFileName)
  hti_result.Print()
  
  uL_nobsinSR = hti_result.UpperLimit()
  uL_visXsec = uL_nobsinSR / lumiFB
  # uL_visXsecErrorUp = uL_visXsec - uL_nobsinSR/(lumiFB * (1. + lumiRelUncert))
  # uL_visXsecErrorDown = uL_nobsinSR/(lumiFB * (1. - lumiRelUncert)) - uL_visXsec

  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
 #       print " \n   found the CLB values to interpolate"
 #       print " CLB_M =", CLB_M, " CLB_P =", CLB_P, "  mu_P = ", mu_P, " mu_M = ", mu_M

  # interpolate 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 " CLB = " , CLB

  print "\n\n\n\n  ***---  now doing p-value calculation ---*** \n\n\n\n"
  Util.resetAllValues(w)
  Util.resetAllErrors(w)
  Util.resetAllNominalValues(w)

  pval = RooStats.get_Presult(w,False,1000,2)
  # get_Presult(  RooWorkspace* w,
  #           		bool doUL, // = true, // true = exclusion, false = discovery
  #             		int ntoys, //=1000,
  #             		int calculatorType, // = 0,
  #             		int testStatType, // = 3,  
  #             		const char * modelSBName, // = "ModelConfig",
  #             		const char * modelBName, // = "",
  #             		const char * dataName, // = "obsData",
  #             		bool useCLs, // = true ,   
  #             		bool useNumberCounting, // = false,
  #             		const char * nuisPriorName) // = 0 
  
  ulList = [uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM, CLB, pval ]

  return ulList
Пример #4
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
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

  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 = 240
  murangehigh = 350 #40.0 #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

  npoints = int((murangehigh-murangelow)*2.)


  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
Пример #6
0
def latexfitresults(filename, poiname='mu_Sig', lumiFB=1.0,
                    nTOYS=3000, asimov=False, wname='combined'):

  workspacename=wname

  w = Util.GetWorkspaceFromFile(filename,workspacename)

  if w==None:
    print "ERROR : Cannot open workspace : ", workspacename
    sys.exit(1)

  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)

  ntoys = 3000
  calctype = 0   # toys = 0, asymptotic (asimov) = 2
  npoints = 20

  if nTOYS != 3000 and nTOYS>0:
    ntoys = nTOYS
  if asimov:
    calctype = 2

  hti_result = RooStats.MakeUpperLimitPlot(
    poiname,w,calctype,3,ntoys,True,npoints)
  outFileName = "./htiResult_poi_" + poiname + "_ntoys_" + str(ntoys) + "_calctype_" + str(calctype) + "_npoints_" + str(npoints) + ".root"
  hti_result.SaveAs(outFileName)
  hti_result.Print()

  uL_nobsinSR = hti_result.UpperLimit()
  uL_visXsec = uL_nobsinSR / lumiFB
  # uL_visXsecErrorUp = uL_visXsec - uL_nobsinSR/(lumiFB * (1. + lumiRelUncert))
  # uL_visXsecErrorDown = uL_nobsinSR/(lumiFB * (1. - lumiRelUncert)) - uL_visXsec

  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
 #       print " \n   found the CLB values to interpolate"
 #       print " CLB_M =", CLB_M, " CLB_P =", CLB_P, "  mu_P = ", mu_P, " mu_M = ", mu_M

  # interpolate the value of CLB to be exactly above upperlimit p-val
  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
  #print " CLB = " , CLB

  print "\n\n\n\n  ***---  now doing p-value calculation ---*** \n\n\n\n"
  pval = RooStats.get_Presult(w,False,1000,2)

  print "p-value is: ", pval
  
  ulList = [uL_visXsec, uL_nobsinSR, uL_nexpinSR, uL_nexpinSRerrP, uL_nexpinSRerrM, CLB, pval ]

  return ulList