def plcLimit(obs_, poi_, model, ws, data, CL=0.95, verbose=False): # obs : observable variable or RooArgSet of observables # poi : parameter of interest or RooArgSet of parameters # model : RooAbsPdf of model to consider including any constraints # data : RooAbsData of the data # CL : confidence level for interval # returns a dictionary with the upper and lower limits for the first/only # parameter in poi_ as well as the interval object and status flag obs = RooArgSet(obs_) obs.setName('observables') poi = RooArgSet(poi_) poi.setName('poi') poi.setAttribAll('Constant', False) nuis = model.getParameters(obs) nuis.remove(poi) nuis.remove(nuis.selectByAttrib('Constant', True)) nuis.setName('nuisance') if verbose: print 'observables' obs.Print('v') print 'parameters of interest' poi.Print('v') print 'nuisance parameters' nuis.Print('v') mc = RooStats.ModelConfig('mc') mc.SetWorkspace(ws) mc.SetPdf(model) mc.SetObservables(obs) mc.SetParametersOfInterest(poi) mc.SetNuisanceParameters(nuis) plc = RooStats.ProfileLikelihoodCalculator(data, mc) plc.SetConfidenceLevel(CL) interval = plc.GetInterval() upperLimit = Double(999.) lowerLimit = Double(0.) Limits = {} paramIter = poi.createIterator() param = paramIter.Next() while param: ok = interval.FindLimits(param, lowerLimit, upperLimit) Limits[param.GetName()] = { 'ok': ok, 'upper': float(upperLimit), 'lower': float(lowerLimit) } param = paramIter.Next() if verbose: print '%.0f%% CL limits' % (interval.ConfidenceLevel() * 100) print Limits Limits['interval'] = interval return Limits
def GetPLRInterval(filename="workspace.root", wsname='myWS', interactive=False): # this function loads a workspace and computes # a Bayesian upper limit pInFile = ROOT.TFile(filename, "read") # load workspace pWs = pInFile.Get(wsname) if not pWs: print "workspace ", wsname, " not found" return -1 # printout workspace content pWs.Print() # load and print data from workspace data = pWs.data("data") data.Print() # load and print S+B Model Config pSbHypo = pWs.obj("SbHypo") pSbHypo.Print() # create RooStats Profiled Likelihood ratio calculator and set parameters bCalc = RooStats.ProfileLikelihoodCalculator(data, pSbHypo) bCalc.SetConfidenceLevel(0.95) # estimate credible interval # NOTE: unfortunate notation: the UpperLimit() name refers # to the upper boundary of an interval, # NOT to the upper limit on the parameter of interest # (it just happens to be the same for the one-sided # interval starting at 0) pSInt = bCalc.GetInterval() upper_bound = pSInt.UpperLimit(pWs.var('xsec')) lower_bound = pSInt.LowerLimit(pWs.var('xsec')) print "one-sided 95%.C.L. interval for xsec: ", "[", lower_bound, ", ", upper_bound, "]" # make posterior PDF plot for POI c1 = ROOT.TCanvas("plr", "likelihood furnction") pPlot = RooStats.LikelihoodIntervalPlot(pSInt) pPlot.SetRange(0., 0.006) pPlot.SetMaximum(5.) pPlot.SetNPoints(50) pPlot.Draw() ROOT.gPad.Update() c1.SaveAs("plr_plot.pdf") if interactive: raw_input("\npress <enter> to continue")
def calculate_cls(self, workspace_name): """ returns a dictionary of CLs values """ from scharmfit import utils utils.load_susyfit() from ROOT import Util from ROOT import RooStats if not isfile(workspace_name): raise OSError("can't find workspace {}".format(workspace_name)) workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined') Util.SetInterpolationCode(workspace,4) # NOTE: We're completely silencing the fitter. Add an empty string # to the accept_strings to get all output. with OutputFilter(accept_strings={}): limit = RooStats.get_Pvalue( workspace, True, # doUL 1, # n_toys 2, # asymtotic calculator 3, # test type (3 is atlas standard) ) return dict( cls=limit.GetCLs(), cls_exp=limit.GetCLsexp(), cls_up_1_sigma=limit.GetCLsu1S(), cls_down_1_sigma=limit.GetCLsd1S(), cls_up_2_sigma=limit.GetCLsu2S(), cls_down_2_sigma=limit.GetCLsd2S())
def GetBayesianInterval(filename="workspace.root", wsname='myWS', interactive=False): # this function loads a workspace and computes # a Bayesian upper limit pInFile = ROOT.TFile(filename, "read") # load workspace pWs = pInFile.Get(wsname) if not pWs: print "workspace ", wsname, " not found" return -1 # printout workspace content pWs.Print() # load and print data from workspace data = pWs.data("data") data.Print() # load and print S+B Model Config pSbHypo = pWs.obj("SbHypo") pSbHypo.Print() # create RooStats Bayesian calculator and set parameters bCalc = RooStats.BayesianCalculator(data, pSbHypo) bCalc.SetName("myBC") bCalc.SetConfidenceLevel(0.95) bCalc.SetLeftSideTailFraction(0.0) #bcalc->SetIntegrationType("ROOFIT") # estimate credible interval # NOTE: unfortunate notation: the UpperLimit() name refers # to the upper boundary of an interval, # NOT to the upper limit on the parameter of interest # (it just happens to be the same for the one-sided # interval starting at 0) pSInt = bCalc.GetInterval() upper_bound = pSInt.UpperLimit() lower_bound = pSInt.LowerLimit() print "one-sided 95%.C.L. bayesian credible interval for xsec: ", "[", lower_bound, ", ", upper_bound, "]" # make posterior PDF plot for POI c1 = ROOT.TCanvas("posterior", "posterior") bCalc.SetScanOfPosterior(100) pPlot = bCalc.GetPosteriorPlot() pPlot.Draw() ROOT.gPad.Update() c1.SaveAs("bayesian_num_posterior.pdf") if interactive: raw_input("\npress <enter> to continue")
def get_significance(s, b1, b2, err_b1, err_b2, threshold): ''' asimov significance https://root.cern.ch/doc/v614/RooStatsUtils_8cxx_source.html https://www.pp.rhul.ac.uk/~cowan/stat/medsig/medsigNote.pdf eq[10], eq[20] ''' err_b1_b2 = eff_error(b1, b2, err_b1, err_b2, threshold) nB = max(b1 + b2, threshold) significance = 0. if s > 1.1e-10: # value smaller than this cause NaN value when use vfunc # please think and figure out a better way to treat negative weight significance = RooStats.AsimovSignificance(s, nB, err_b1_b2) return err_b1_b2, significance
def execute(self, ws, debug = 0): print self.legend, 'Profile Likelihood calculation started...' # time the action t = TStopwatch() t.Start() # model config is guaranteed to be here # the action won't configure without it # and this code will never run unless valid model config is found mconf = ws.obj(self._model_config_name) _poi = mconf.GetParametersOfInterest() plcInt = self._plc.GetInterval() # stop watch and print how long it took to get the interval t.Print() # iterate over all parameters of interest and print out the intervals # (could there be more than one?) _iter = _poi.createIterator() while True: _poi_name = _iter().GetTitle() lower_limit = plcInt.LowerLimit( _poi[_poi_name] ) upper_limit = plcInt.UpperLimit( _poi[_poi_name] ) print self.legend, 'Profile Likelihood interval for', _poi_name, 'is ['+ \ str(lower_limit) + ', ' + \ str(upper_limit) + ']' if _iter.Next() == None: break if self._scan_plot: # draw scan plot print self.legend, 'making the likelihood scan plot' _plot_name = _poi_name+'_plc_scan_exost.'+self._plot_format c1 = TCanvas("c1", "c1", 600, 600) plcPlot = RooStats.LikelihoodIntervalPlot(plcInt) gROOT.SetStyle("Plain") plcPlot.Draw() c1.SaveAs(_plot_name) return (lower_limit, upper_limit)
def model(self, ws, debug=0): if self.sModel not in self.items: if debug > 0: print self.legend, 'no Model section defined in the config file' print self.legend, 'no model will be created' return legend = '[' + self.sModel + ']:' # FIXME: find a better way to load custom PDFs #gROOT.ProcessLine(".L dijetQstarPdf.cxx+"); #gROOT.ProcessLine(".L Qstar_qg.cxx+"); #gROOT.ProcessLine(".L Qstar_qg_2.cxx+"); #gROOT.ProcessLine(".L Jacobian_mt.cxx+"); model_items = self.items[self.sModel] if (debug > 0): print 'Loading model to the workspace', ws.GetName() # using RooStats::HLFactory to parse the model # make a temp file with a card file card_file_name = 'hlfCardFile.rs' with open(card_file_name, 'w') as cardfile: for item in model_items: print >> cardfile, item[0], '=', item[1] hlfVerbosity = debug > 0 #hlf = ROOT.RooStats.HLFactory('HLFactoryExost', card_file_name, True) hlf = RooStats.HLFactory('HLFactoryExost', ws, hlfVerbosity) self.modelAutoImportWarningExplanation(legend, debug) current_messaging_level = RooMsgService.instance().globalKillBelow() if debug <= 0: RooMsgService.instance().setGlobalKillBelow(RooFit.ERROR) hlf.ProcessCard(card_file_name) print legend, 'importing nonstandard class code into the workspace...' ws.importClassCode() #ws.importClassCode('Qstar_qg', true); print legend, '...code import done' # return messaging to the original level RooMsgService.instance().setGlobalKillBelow(current_messaging_level)
def limitBayesian(w=initializeWorkspace(), B=1, dB=0.35, S=1, dS=0.1): w.var("B").setVal(B) w.var("dB").setVal(dB) w.var("S").setVal(S) w.var("dS").setVal(dS) w.var("N").setVal(B) obs = ROOT.RooArgSet(w.var("N")) data = ROOT.RooDataSet("data", "data", obs) data.add(obs) # insert entry rbackup = w.var("r").getMax() nuisances = ROOT.RooArgSet() if dB != 0: nuisances.add(w.var("thetaB")) w.var("thetaB").setConstant(False) else: w.var("thetaB").setConstant(True) if dS != 0: nuisances.add(w.var("thetaS")) w.var("thetaS").setConstant(False) else: w.var("thetaS").setConstant(True) poi = ROOT.RooArgSet(w.var("r")) model = w.pdf({True: "model_s", False: "modelStat_s"}[dS > 0 or dB > 0]) prior = w.pdf("prior") nuisSet = {True: nuisances, False: None}[dS > 0 or dB > 0] bcalc = RooStats.BayesianCalculator(data, model, poi, prior, nuisSet) bcalc.SetLeftSideTailFraction(0) bcalc.SetConfidenceLevel(0.95) interval = bcalc.GetInterval() ret = interval.UpperLimit() while ret > 0.5 * w.var("r").getMax(): #print "Got ",ret,"too close to the upper bound",w.var("r").getMax() if (w.var("r").getMax() > 200): break w.var("r").setMax(w.var("r").getMax() * 2) interval = bcalc.GetInterval() ret = interval.UpperLimit() w.var("r").setMax(rbackup) return ret
def calculate_cls(self, workspace_name): """ returns a dictionary of CLs values """ from scharmfit import utils utils.load_susyfit() from ROOT import Util from ROOT import RooStats if not isfile(workspace_name): raise OSError("can't find workspace {}".format(workspace_name)) workspace = Util.GetWorkspaceFromFile(workspace_name, 'combined') Util.SetInterpolationCode(workspace,4) # NOTE: We're completely silencing the fitter. Add an empty string # to the accept_strings to get all output. with OutputFilter(accept_strings={}): limit = RooStats.get_Pvalue( workspace, True, # doUL 1, # n_toys 2, # asymtotic calculator 3, # test type (3 is atlas standard) ) ws_type = workspace_name.rsplit('_',1)[1].split('.')[0] if ws_type == self.nominal: return { 'obs':limit.GetCLs(), 'exp':limit.GetCLsexp(), 'exp_u1s':limit.GetCLsu1S(), 'exp_d1s':limit.GetCLsd1S(), } elif ws_type == self.up1s: return {'obs_u1s':limit.GetCLs()} elif ws_type == self.down1s: return {'obs_d1s':limit.GetCLs()} # should never get here raise ValueError('can\'t classify {} as type of limit'.format( workspace_name))
def GetLimits(tl,f): from ROOT import RooStats,Util #w=gDirectory.Get("w") print "analysis name: ",tl.name print "workspace name: ",tl.wsFileName if not ("SU" in tl.name): print "Do no hypothesis test for bkg only or discovery fit!\n" return print "Need to load workspace" Util.ReadWorkspace(tl.wsFileName,"combined") w=gDirectory.Get("w") result = RooStats.MakeUpperLimitPlot(tl.name,w,2,3,1000,True,20,True) if not result==0: result.Print() print result.UpperLimit() return
b0SideSub.SetMarkerStyle(kFullCircle) b0SideSub.SetMarkerColor(kBlack) b0SideSub.SetMarkerSize(0.8) b0SideSub.SetLineColor(kBlack) b0SideSub.Draw("E0") linezero = TLine(b0SideSub.GetBinCenter(1), 0.0, b0SideSub.GetBinCenter(b0SideSub.GetNbinsX()), 0.0) linezero.SetLineColor(kRed) linezero.SetLineWidth(2) linezero.SetLineStyle(kDotted) linezero.Draw() kcanvas.SaveAs(region + "/b0s_subtracted_" + region + ".png") kcanvas.SaveAs(region + "/b0s_subtracted_" + region + ".root") splot = RooStats.SPlot("sPlot", "sPlot", theData, kkTot, RooArgList(nSigKK, nBkgKK)) dstree = theData.store().tree() dstree.GetEntryNumber(88) sPlot_B0_hist = TH1F("sPlot_B0_hist", "#mu#muKK sPlot - " + region + "; m(#mu#muKK) [GeV]", 2000, 4.00, 6.0) sPlot_B0_hist.Sumw2() sPlot_B0_hist.SetLineColor(2) sPlot_B0_hist.SetMarkerColor(2) sPlot_B0_hist.SetMinimum(0.) dstree.Project("sPlot_B0_hist", "xM", "nSig_sw") sPlot_B0_hist.Draw("e0")
def getProfile(idx=None, update=False): from ROOT import TFile, TCanvas from ROOT import RooStats as RS f = TFile("%s/data/fitWorkspace.root" % dsi.latSWDir) fitWorkspace = f.Get("fitWorkspace") fData = fitWorkspace.allData().front() hitE = fitWorkspace.var("trapENFCal") model = fitWorkspace.pdf("model") fitResult = fitWorkspace.allGenericObjects().front() fPars = fitResult.floatParsFinal() nPars = fPars.getSize() # === get fit results: {name : [nCts, err]} === fitVals = {} for i in range(nPars): fp = fitResult.floatParsFinal() name = fp.at(i).GetName() fitVal, fitErr = fp.at(i).getValV(), fp.at(i).getError() if "amp" in name: # fitVals[name.split('-')[1]] = [fitVal, fitErr] fitVals[name] = [fitVal, fitErr, name.split('-')[1]] # for f in fitVals: # print(f, fitVals[f]) # === get "true" counts (reverse efficiency correction based on fit value) === hAx = getHistList("axion") x, y, xpb = wl.npTH1D(hAx) x, y = normPDF(x, y, eLo, eHi) nCts, nErr, _ = fitVals["amp-axion"] # fit result yc = nCts * getEffCorr(x, y, inv=True) nCorr = np.sum(yc) effCorr = nCorr / nCts print("nCts %d nCorr %.2f effCorr %.2f" % (nCts, nCorr, effCorr)) # thesis plot, don't delete # plt.step(x, y * nCts * (epb/xpb), c='r', lw=2, label="Eff-weighted: %.1f" % (nCts)) # plt.step(x, yc * (epb/xpb), c='b', lw=2, label="True counts: %.1f" % (nCorr)) # plt.xlabel("Energy (keV)", ha='right', x=1) # plt.ylabel("Counts / keV", ha='right', y=1) # plt.legend(loc=1) # plt.tight_layout() # plt.show() tMode = "UPDATE" if update else "RECREATE" tOut = TFile("%s/data/rs-plc.root" % dsi.latSWDir, "UPDATE") start = time.clock() name = "amp-axion" fitVal = fitVals[name][0] thisVar = fitWorkspace.var(name) pCL = 0.9 plc = RS.ProfileLikelihoodCalculator(fData, model, ROOT.RooArgSet(thisVar)) plc.SetConfidenceLevel(0.90) interval = plc.GetInterval() lower = interval.LowerLimit(thisVar) upper = interval.UpperLimit(thisVar) plot = RS.LikelihoodIntervalPlot(interval) plot.SetNPoints(50) plot.Draw("tf1") pName = "hP_%d" if idx is not None else "hP" hProfile = plot.GetPlottedObject() hProfile.SetName(pName) hProfile.SetTitle("PL %.2f %s lo %.3f mid %.3f hi %.3f eC %.3f" % (pCL, name, lower, fitVal, upper, effCorr)) hProfile.Write() print(hProfile.GetTitle()) tOut.Close() print("elapsed:", time.clock() - start)
tenIntegralSig = tenIntegralSig * nSig.getValV() / totIntegralSig fiveIntegralBkg = fiveIntegralBkg * nBkg.getValV() / totIntegralBkg tenIntegralBkg = tenIntegralBkg * nBkg.getValV() / totIntegralBkg fiveIntegralTot = fiveIntegralTot * (nBkg.getValV()+nSig.getValV()) / totIntegralTot tenIntegralTot = tenIntegralTot * (nBkg.getValV()+nSig.getValV()) / totIntegralTot c.SaveAs('phimassSPlot_'+ outname + '.png') c.SaveAs('phimassSPlot_' + outname + '.root') c.Clear() cD=TCanvas("cD","cD",750,600) cD.cd() splot = RooStats.SPlot ("sPlot","sPlot",splotData, tot, RooArgList(nSig,nBkg)) dstree = splotData.store().tree() #S plot hist for signal shistSig = TH1F('shistSig','shistSig', 50, 4.0, 6.0) shistSig.Sumw2() shistSig.SetLineColor(2) shistSig.SetMarkerColor(2); shistSig.SetMinimum(0.) dstree.Project('shistSig','dimuonditrk_m_rf_c','nSig_sw'); shistSig.Draw('e0'); cD.SaveAs('SigSPlotPhi_' + outname + '_' + outname + '.gif') cD.SaveAs('SigSPlotPhi_' + outname + '.root')
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
if opts.nullFit: fr_null.Write() #fitter.ws.Print() output.Close() if pars.btagSelection: c1.SaveAs("DibosonBoostedBtaglnuJ_%s_%ijets_Stacked.png" % (mode, opts.Nj)) c2.SaveAs("DibosonBoostedBtaglnuJ_%s_%ijets_Subtracted.png" % (mode, opts.Nj)) cp1.SaveAs("DibosonBoostedBtaglnuJ_%s_%ijets_Pull.png" % (mode, opts.Nj)) else: c1.SaveAs("DibosonBoostedlnuJ_%s_%ijets_Stacked.png" % (mode, opts.Nj)) c2.SaveAs("DibosonBoostedlnuJ_%s_%ijets_Subtracted.png" % (mode, opts.Nj)) cp1.SaveAs("DibosonBoostedlnuJ_%s_%ijets_Pull.png" % (mode, opts.Nj)) print 'Time elapsed: %.1f sec' % timer.RealTime() print 'CPU time used: %.1f sec' % timer.CpuTime() print '%i degrees of freedom' % ndf print 'chi2: %.2f / %i = %.2f' % (chi2_1, ndf, (chi2 / ndf)) print 'chi2 probability: %.4g' % (TMath.Prob(chi2, ndf)) if opts.nullFit: likelihoodRatio = 2. * fr_null.minNll() - 2. * fr.minNll() print '2*nll_null - 2*nll: %.4f - %.4f = %.4f' % ( 2. * fr_null.minNll(), 2. * fr.minNll(), likelihoodRatio) pval = TMath.Prob(likelihoodRatio, 1) print 'p-value: %.4g' % pval print 'Gaussian significance: %.3g' % RooStats.PValueToSignificance(pval)
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
observedZValue = [] tmpIntegratedLumi = 0.0 for ibin in xrange( h_den.GetNbinsX() ): if tmpIntegratedLumi/1000.>22.1: break tmpIntegratedLumi += h_den.GetBinContent(ibin) nobs = h_num["SRG3b"].Integral(0,ibin) nexp = 2.8*tmpIntegratedLumi/(22.1 * 1000.) nexpsig = 10*tmpIntegratedLumi/(22.1 * 1000.) integratedLumi.append(tmpIntegratedLumi/1000.) tmpPvalue = RooStats.NumberCountingUtils.BinomialObsP(nexpsig,nexp,0.27)/2. tmpZvalue = RooStats.PValueToSignificance( tmpPvalue ) if tmpPvalue else 0 expectedZValue.append( tmpZvalue ) tmpPvalue = RooStats.NumberCountingUtils.BinomialObsP(nexpsig,nexp*(1+0.27),0.27)/2. tmpZvalue = RooStats.PValueToSignificance( tmpPvalue ) if tmpPvalue else 0 expectedUpZValue.append( tmpZvalue ) tmpPvalue = RooStats.NumberCountingUtils.BinomialObsP(nexpsig,nexp*(1-0.27),0.27)/2. tmpZvalue = RooStats.PValueToSignificance( tmpPvalue ) if tmpPvalue else 0 expectedDownZValue.append( tmpZvalue ) tmpPvalue = RooStats.NumberCountingUtils.BinomialObsP(nobs,nexp,0.27)/2. tmpZvalue = RooStats.PValueToSignificance( tmpPvalue ) if tmpPvalue else 0 observedZValue.append( tmpZvalue )
c = TCanvas("canvas", "canvas", 1200, 800) phiFrame = masskk.frame(Range(massmin, massmax)) xdataPrompt.plotOn(phiFrame) tot.plotOn(phiFrame) phiFrame.Draw() c.SaveAs("phiMassSPlotPhi.png") c.SaveAs("phiMassSPlotPhi.root") c.Clear() # In[ ]: cD = TCanvas("cD", "cD", 750, 600) cD.cd() splot = RooStats.SPlot("sPlot", "sPlot", xdataPrompt, tot, alist2) dstree = xdataPrompt.store().tree() # In[19]: shist = TH1F('shist', 'shist', 200, 0.97, 1.07) # In[20]: shist.Sumw2() shist.SetLineColor(2) shist.SetMarkerColor(2) shist.SetMinimum(0.) dstree.Project('shist', 'kkM', 'S1_sw + S2_sw') # In[21]:
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
def MakeWorkspace(outfilename): # use this area to implement your model for a counting experiment print "building counting model..." #create workspace pWs = ROOT.RooWorkspace("myWS") #observable pWs.factory("n[0]") #this create a literal, real-valued container called n #signal yield # pWs.factory("nsig[0,0,100]") # name[value,min bound,max bound] # pWs.factory("lumi[0]") #luminosity pWs.factory("lumi_nom[20000, 10000, 30000]") #nominal lumi of 20 fb-1 pWs.factory("lumi_kappa[1.026]") pWs.factory("lumi_beta[0,-5,5]") pWs.factory("RooPowerFunction::lumi_alpha(lumi_kappa, lumi_beta)") pWs.factory("prod::lumi(lumi_nom,lumi_alpha)") #the new lumi pWs.factory("Gaussian::lumi_constr(lumi_beta,lumi_glob[0,-5,5],1)") pWs.factory("xsec[0,0,0.1]") #cross-section (our new POI) # pWs.factory("eff[0]") #efficiency pWs.factory("eff_nom[0.1, 0.01, 0.20]") #nominal eff pWs.factory("eff_kappa[1.1]") pWs.factory("eff_beta[0,-5,5]") pWs.factory("RooPowerFunction::eff_alpha(eff_kappa, eff_beta)") pWs.factory("prod::eff(eff_nom,eff_alpha)") #the new eff pWs.factory("Gaussian::eff_constr(eff_beta,eff_glob[0,-5,5],1)") pWs.factory("prod::nsig(lumi,xsec,eff)") #new definition of nsig #background yield # pWs.factory("nbkg[10,0,100]") pWs.factory("nbkg_nom[10.,5.,15.]") pWs.factory("nbkg_kappa[1.1]") pWs.factory("nbkg_beta[0,-5,5]") pWs.factory("RooPowerFunction::nbkg_alpha(nbkg_kappa, nbkg_beta)") pWs.factory("prod::nbkg(nbkg_nom,lumi_alpha,nbkg_alpha)") pWs.factory("Gaussian::nbkg_constr(nbkg_beta, nbkg_glob[0,-5,5],1)") #full yield pWs.factory("sum::yield(nsig,nbkg)") # creates a function nsig+nbkg # Bayesian prior pWs.factory("Uniform::prior(xsec)") # define the physics model a Poisson distribution pWs.factory("Poisson::model_core(n,yield)") # model and systematics # pWs.factory("PROD::model(model_core,lumi_constr)") # all-cap PROD means PDF instead of only function pWs.factory("PROD::model(model_core,lumi_constr,eff_constr,nbkg_constr)") # create set of observables (needed for datasets and ModelConfig later) pObs = pWs.var("n") # get the pointer to the observable obs = ROOT.RooArgSet("observables") obs.add(pObs) # create the dataset pObs.setVal(11) # this is your observed data: you counted eleven events data = ROOT.RooDataSet("data", "data", obs) data.add(obs) # import dataset into workspace getattr(pWs, 'import')( data ) # we call it this way because "import" is a reserved word in python # create set of global observables (need to be defined as constants!) pWs.var("lumi_glob").setConstant(True) pWs.var("eff_glob").setConstant(True) pWs.var("nbkg_glob").setConstant(True) globalObs = ROOT.RooArgSet("global_obs") globalObs.add(pWs.var("lumi_glob")) globalObs.add(pWs.var("eff_glob")) globalObs.add(pWs.var("nbkg_glob")) # create set of parameters of interest (POI) poi = ROOT.RooArgSet("poi") poi.add(pWs.var("xsec")) # create set of nuisance parameters nuis = ROOT.RooArgSet("nuis") nuis.add(pWs.var("lumi_beta")) nuis.add(pWs.var("eff_beta")) nuis.add(pWs.var("nbkg_beta")) # fix all other variables in model: # everything except observables, POI, and nuisance parameters # must be constant pWs.var("lumi_nom").setConstant(True) pWs.var("eff_nom").setConstant(True) pWs.var("nbkg_nom").setConstant(True) pWs.var("lumi_kappa").setConstant(True) pWs.var("eff_kappa").setConstant(True) pWs.var("nbkg_kappa").setConstant(True) fixed = ROOT.RooArgSet("fixed") fixed.add(pWs.var("lumi_nom")) fixed.add(pWs.var("eff_nom")) fixed.add(pWs.var("nbkg_nom")) fixed.add(pWs.var("lumi_kappa")) fixed.add(pWs.var("eff_kappa")) fixed.add(pWs.var("nbkg_kappa")) # create signal+background Model Config sbHypo = RooStats.ModelConfig("SbHypo") sbHypo.SetWorkspace(pWs) sbHypo.SetPdf(pWs.pdf("model")) sbHypo.SetObservables(obs) sbHypo.SetGlobalObservables(globalObs) sbHypo.SetParametersOfInterest(poi) sbHypo.SetNuisanceParameters(nuis) sbHypo.SetPriorPdf(pWs.pdf("prior")) # this is optional # set parameter snapshot that corresponds to the best fit to data pNll = sbHypo.GetPdf().createNLL(data) pProfile = pNll.createProfile( globalObs) # do not profile global observables pProfile.getVal( ) # this will do fit and set POI and nuisance parameters to fitted values pPoiAndNuisance = ROOT.RooArgSet("poiAndNuisance") pPoiAndNuisance.add(sbHypo.GetNuisanceParameters()) pPoiAndNuisance.add(sbHypo.GetParametersOfInterest()) sbHypo.SetSnapshot(pPoiAndNuisance) # import S+B ModelConfig into workspace getattr(pWs, 'import')(sbHypo) # create background-only Model Config from the S+B one bHypo = RooStats.ModelConfig(sbHypo) bHypo.SetName("BHypo") bHypo.SetWorkspace(pWs) # set parameter snapshot for bHypo, setting xsec=0 # it is useful to understand how this block of code works # but you can also use it as a recipe to make a parameter snapshot pNll = bHypo.GetPdf().createNLL(data) poiAndGlobalObs = ROOT.RooArgSet("poiAndGlobalObs") poiAndGlobalObs.add(poi) poiAndGlobalObs.add(globalObs) pProfile = pNll.createProfile( poiAndGlobalObs) # do not profile POI and global observables poi.first().setVal(0) # set xsec=0 here pProfile.getVal( ) # this will do fit and set nuisance parameters to profiled values pPoiAndNuisance = ROOT.RooArgSet("poiAndNuisance") pPoiAndNuisance.add(nuis) pPoiAndNuisance.add(poi) bHypo.SetSnapshot(pPoiAndNuisance) # import model config into the workspace getattr(pWs, 'import')(bHypo) #print the contents of the workspace pWs.Print() #save the workspace to a file pWs.SaveAs(outfilename) return 0
RooFit.LineStyle(kDashed)) bfr5.SetTitle("Jet slice of sim pdf") bfr5.Draw() #c.cd(6) #bfr6 = w.var("bdt").frame() #simPdf.plotOn(bfr6, RooFit.ProjWData(ROOT.RooArgSet(sample),combData)) ; #simPdf.plotOn(bfr6, RooFit.ProjWData(ROOT.RooArgSet(sample),combData), RooFit.Slice(sample, "JKS"), RooFit.LineColor(2), RooFit.LineStyle(kDashed)) #simPdf.plotOn(bfr6, RooFit.ProjWData(ROOT.RooArgSet(sample),combData), RooFit.Slice(sample, "Jet"), RooFit.LineColor(4), RooFit.LineStyle(kDashed)) #bfr6.SetTitle("sim pdf") #bfr6.Draw() """ sct = RooSimWSTool(w) model_sim = sct.build("model_sim","model",SplitParam("m","c")) """ mc = RooStats.ModelConfig("mc", w) mc.SetPdf(simPdf) mc.SetParametersOfInterest("mu") mc.SetObservables("bdt") mc.SetNuisanceParameters("nbkg") mc.SetNuisanceParameters("nbkg2") c.cd(7) bfr7 = w.var("bdt").frame() cdata = ROOT.RooStats.AsymptoticCalculator.MakeAsimovData( combData, mc, w.argSet("bdt,sample"), ROOT.RooArgSet()) dh = ROOT.RooDataHist("", "", w.argSet("bdt,sample"), cdata) err_correction = [ dh.set(dh.get(i), dh.weight(dh.get(i)), ROOT.TMath.Sqrt(dh.weight(dh.get(i)))) for i in range(0, dh.numEntries())
mean.setConstant(ROOT.kFALSE) rfit = tot.fitTo(b0dataNonPrompt, Range(massmin, massmax), RooFit.NumCPU(8)) gamma.setConstant(ROOT.kFALSE) rfit = tot.fitTo(b0dataNonPrompt, Range(phimean - 0.025, phimean + 0.025), RooFit.NumCPU(8)) masskkFrame = masskk.frame(Range(phimean - 0.025, phimean + 0.025)) b0dataNonPrompt.plotOn(massFrame, RooLinkedList()) tot.plotOn(masskkFrame) massFrame.Draw() c.SaveAs("testmassFit.png") cD = TCanvas("cD", "cD", 750, 600) cD.cd() splot = RooStats.SPlot("sPlot", "sPlot", b0dataNonPrompt, tot, RooArgList(nSig, nBkg)) # In[18]: dstree = b0dataNonPrompt.store().tree() # In[19]: shist = TH1F('shist', 'shist', 500, 1.00, 1.05) # In[20]: shist.Sumw2() shist.SetLineColor(2) shist.SetMarkerColor(2) shist.SetMinimum(0.)
def variables(self, ws, debug): var_items = self.items[self.sVariables] legend = '[' + self.sVariables + ']:' print legend, 'begin processing...' print legend, 'define variables, constants, categories, sets and lists' # dictionary of value lists for each item # this is more detailed than the original items object # because the multiple settings for a given item # are already split # NOTE: sets are special case and are not added here item_values = {} # using RooStats::HLFactory to parse the model # make a temp file with a card file card_file_name = 'hlfCardFile.rs' with open(card_file_name, 'w') as cardfile: vSets = {} for item in var_items: # # special case for sets # standard RooFactoryWSTool interface exists in newer versions # need to switch at some point # for now parsing and adding named sets by hand # if item[1].strip()[0:3] == 'set': set_arg_list = item[1].strip() set_arg_list = set_arg_list[3:len(set_arg_list)] set_arg_list = set_arg_list.strip(' ;()').split(',') set_args = [] for arg in set_arg_list: set_args.append(arg.strip()) vSets[item[0].strip()] = set_args if (debug > 2): print legend, 'Set arg value is ->' + item[1] + '<-' print legend, 'set args are', set_args else: # this is a way to handle multiple settings # for each item: they are separated by semicolon # in the value field of the config item item_values[item[0]] = self.split_values(item) #print >> cardfile, item[0], '=', item[1] print >> cardfile, item[0], '=', item_values[item[0]][0] # add variables using HLFactory cards hlfVerbosity = debug > 2 hlf = RooStats.HLFactory('HLFactoryExost', ws, hlfVerbosity) hlf.ProcessCard(card_file_name) # process the secondary options (fix/float etc.) for _key in item_values: n_values = len(item_values[_key]) for _index in range(0, n_values): self.process_value(ws, _key, item_values[_key][_index], debug) # now add sets by hand print legend, 'creating sets now...' # fixme experimental _sets = {} for set_args_key in vSets.keys(): arg_list = '' # fixme experimental _sets[set_args_key] = RooArgSet(set_args_key) for arg in vSets[set_args_key]: if len(arg_list) > 0: arg_list += ',' arg_list += arg # fixme experimental _sets[set_args_key].add(ws.var(arg)) #ws.defineSet(set_args_key, arg_list) print legend, 'set', set_args_key, 'created' print legend, 'done creating sets' return {'sets': _sets}
def bayesian(self, ws, debug=0): if self.sBayes not in self.items: if debug > 0: print self.legend, 'no Bayesian Calculator section in the config file' print self.legend, 'cannot configure Bayesian calculator' return {'status': 'fail'} legend = '[' + self.sBayes + ']:' _name = 'exostBayes' # getting items as a dictionary bayes_items = {} bayes_items.update(self.items[self.sBayes]) print legend, 'Configuring Bayesian calculator... ' # check if a CL is specified m_conf_name = self.check_value(self.sBayes, 'model_config') if m_conf_name == -1: print legend, 'Error: model config is not specified' print legend, 'Error:', self.sBayes, 'cannot be configured' return {'status': 'fail'} # check if data is specified data_name = self.check_value(self.sBayes, 'data') data_valid = False if data_name != -1: data_valid = True if ws.data(data_name) != None: data = ws.data(data_name) else: data_valid = False print legend, 'Error: dataset', data_name, 'is not defined' if data_valid == False: print legend, 'Error:', self.sBayes, 'cannot be configured' return {'status': 'fail'} # check if a CL is specified conf_level = self.check_value(self.sBayes, 'confidence_level') if conf_level == -1: print legend, 'Warning: desired confidence level is not specified, setting to 0.90' conf_level = 0.90 # check if a posterior plot is requested post_plot = self.check_value(self.sBayes, 'posterior_plot') b_post_plot = False if post_plot == -1: print legend, 'will generate a posterior plot' b_post_plot = True elif post_plot == 'True': b_post_plot = True else: b_post_plot = False # plot format if post_plot: plot_format = self.check_value(self.sBayes, 'plot_format') if plot_format == -1: plot_format = 'png' # to suppress messgaes when pdf goes to zero RooMsgService.instance().setGlobalKillBelow(RooFit.FATAL) #mconf = ws.obj('exostModelConfig') mconf = ws.obj(m_conf_name) if mconf == None: if debug > 0: print self.legend, 'fail to get Model Config' print self.legend, 'unable to configure Bayesian calculator' print self.legend, 'calculator will not be imported into the workspace' return {'status': 'fail'} bCalc = RooStats.BayesianCalculator(data, mconf) bCalc.SetConfidenceLevel(float(conf_level)) ############# FIXME: debug test #bInt = bCalc.GetInterval() #print legend, 'DEBUG3*******************************' # #cl = bCalc.ConfidenceLevel() #print legend, str(cl)+'% CL central interval: [', bInt.LowerLimit(), ' - ', bInt.UpperLimit(), ']' #print legend, 'or', str(cl+(1.0-cl)/2), '% CL limits' ############################## #size = 1.0 - float(conf_level) #bCalc.SetTestSize(size) # import the calculator into the Workspace getattr(ws, 'import')(bCalc, _name) print legend, 'Bayesian calculator', _name, 'is configured and added to workspace', ws.GetName( ) print legend, 'done' return { 'do_posterior_plot': b_post_plot, 'status': 'success', 'model_config_name': m_conf_name, 'plot_format': plot_format }
def configure(self, ws, items, section, debug = 0): print self.legend, 'configuring Profile Likelihood calculator...' _items = items[section] # check mandatory parameters # dataset name in the workspace _dataset_name = self.check_value(items, section, 'data') if _dataset_name == None: print self.legend, 'ERROR: dataset is not specified, cannot configure' return else: if ws.data(_dataset_name) != None: _data = ws.data(_dataset_name) else: print legend, 'Error: dataset', _dataset_name, 'is not defined, cannot configure' return # model config name in the workspace self._model_config_name = self.check_value(items, section, 'model_config') if self._model_config_name == None: print self.legend, 'ERROR: Model Config name is not specified, cannot configure' return else: if self.check_generic_object(ws, self._model_config_name, debug): _mconf = ws.obj(self._model_config_name.strip()) else: print self.legend, 'Error: model config object is not found, cannot configure' return # check optional parameters, set defaults if not specified # desired confidence level _conf_level = self.check_value(items, section, 'confidence_level') if _conf_level == None: print self.legend, 'no confidence level specified, setting to 0.95' _conf_level = 0.95 # whether to make the posterior plot or not self._scan_plot = self.check_value(items, section, 'make_scan_plot') if self._scan_plot == None: print self.legend, 'likelihood intervalscan plot is not requested' self._scan_plot = False elif self._scan_plot == 'True': self._scan_plot = True else: print self.legend, 'invalid assignment make_scan_plot =', self._scan_plot print self.legend, 'likelihood interval scan plot will not be made' self._scan_plot = False # plot format if self._scan_plot: self._plot_format = self.check_value(items, section, 'plot_format') if self._plot_format == None: print self.legend, 'no plot format specified, setting to PNG' self._plot_format = png # configuring now... self._plc = RooStats.ProfileLikelihoodCalculator(_data,_mconf) self._plc.SetConfidenceLevel( float(_conf_level) ) print legend, 'Profile Likelihood calculator is configured' # import the calculator into the Workspace #getattr(ws, 'import')(self._plc, 'exostMcmcCalc') #print legend, 'Markov chain MC calculator', 'exostMcmcCalc', 'is added to workspace', ws.GetName() #print legend, 'done' ws.Print() # flag that the action is configured successfully self.configured = True return {'do_scan_plot':self._scan_plot}
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
def plotProfiles(): f = TFile("./data/fitWorkspace.root") fitWorkspace = f.Get("fitWorkspace") fData = fitWorkspace.allData().front() fEnergy = fitWorkspace.var("energy_keV") model = fitWorkspace.pdf("model") fitResult = fitWorkspace.allGenericObjects().front() fitValsFinal = getRooArgDict(fitResult.floatParsFinal()) print "Fit Cov Qual:", fitResult.covQual() nameList = ["amp-axion"] # nameList = [] # for key in sorted(fitValsFinal): # if "amp-" in key: # nameList.append(key) print "Generating profiles ..." c = TCanvas("c", "c", 800, 600) for name in nameList: fitVal = fitValsFinal[name] thisVar = fitWorkspace.var(name) # Brian & Clint method plc = RS.ProfileLikelihoodCalculator(fData, model, ROOT.RooArgSet(thisVar)) plc.SetConfidenceLevel(0.90) interval = plc.GetInterval() lower = interval.LowerLimit(thisVar) upper = interval.UpperLimit(thisVar) print "%-10s = lo %-7.3f best %-7.3f hi %.3f" % (name, lower, fitVal, upper) plot = RS.LikelihoodIntervalPlot(interval) # p1, pltHi = fitVal - 1.5*(fitVal - lower), fitVal + 1.5*(upper - fitVal) # plot.SetRange(pltLo,pltHi) plot.SetTitle("%s: lo %.3f fit %.3f hi %.3f" % (name, lower, fitVal, upper)) plot.SetNPoints(50) plot.Draw("tf1") """ # Lukas method # note: lukas uses ModelConfig, to explicitly set observables, constraints, and nuisance parameters # https://root-forum.cern.ch/t/access-toy-datasets-generated-by-roostat-frequentistcalculator/24465/8 mc = RS.ModelConfig('mc', fitWorkspace) mc.SetPdf( model ) mc.SetParametersOfInterest( ROOT.RooArgSet(thisVar) ) mc.SetObservables( ROOT.RooArgSet(fEnergy) ) # lukas example # mc.SetConstraintParameters( ROOT.RooArgSet(mean, sigma) ) # mc.SetNuisanceParameters( ROOT.RooArgSet(mean, sigma, n_bkg) ) # mc.SetGlobalObservables( ROOT.RooArgSet(mean_obs, sigma_obs) ) # need to make some RooArgSets from RooRealVars # constraints, nuisances, globalobs = ROOT.RooArgSet(), ROOT.RooArgSet(), ROOT.RooArgSet() # for parName in sorted(fitValsFinal): # rrv = fitWorkspace.var(parName) # if parName != name: # constraints.add(rrv) # # .. etc. # pl = RS.ProfileLikelihoodCalculator(fData, mc) # pl.SetConfidenceLevel(0.683) # lukas used 0.90 # interval = pl.GetInterval() # plot = RS.LikelihoodIntervalPlot(interval) # plot.SetNPoints(50) # plot.Draw("") """ c.Print("./plots/profile_%s.pdf" % name)
################################################################################ poi = ROOT.RooArgSet("poi") #poi.add( pWs.var("glob_nttbar") ); poi.add(pWs.var("nttbar_nom")) #poi.add( pWs.var("beta_nttbar") ); ################################################################################ # create set of nuisance parameters ################################################################################ nuis = ROOT.RooArgSet("nuis") nuis.add(pWs.var("beta_nqcd")) nuis.add(pWs.var("beta_nt")) nuis.add(pWs.var("beta_ntbar")) nuis.add(pWs.var("beta_nwjets")) sbHypo = RooStats.ModelConfig("SbHypo") sbHypo.SetWorkspace(pWs) sbHypo.SetPdf(pWs.pdf("model")) sbHypo.SetObservables(obs) sbHypo.SetGlobalObservables(globalObs) sbHypo.SetParametersOfInterest(poi) sbHypo.SetNuisanceParameters(nuis) pWs.var("beta_nttbar").setConstant(False) pWs.var("beta_nqcd").setConstant(False) pWs.var("beta_nwjets").setConstant(False) pWs.var("beta_nt").setConstant(False) pWs.var("beta_ntbar").setConstant(False) pWs.var("nwjets_nom").setConstant(True) pWs.var("nt_nom").setConstant(True)
xpoints1=array.array('d') ypoints1=array.array('d') xerrors1=array.array('d') yerrors1=array.array('d') for i in range(2,20): x=i*0.25 ys=[] for toy in range(jobs*toysPerJob): ymax=0 for mass,significances in fWW: #if mass<1200: continue ymax=max(ymax,significances[toy]) ys+=[ymax] y=RooStats.PValueToSignificance(float(len([s for s in ys if s>=x]))/len(ys)) if y>10 or y<-10: continue xpoints1.append(x) ypoints1.append(y) xerrors1.append(0) if len([s for s in ys if s>=x])>0: yerror=2*(y-RooStats.PValueToSignificance(float(sqrt(len([s for s in ys if s>=x]))+len([s for s in ys if s>=x]))/len(ys))) else: yerror=y yerrors1.append(yerror) print x,y,yerror canvas = TCanvas("","",0,0,200,200) legend=TLegend(0.3,0.7,0.95,0.90,"")
def model_config(self, ws, debug=0, sets=None): if self.sModelConfig not in self.items: if debug > 0: print self.legend, 'no Model Config section in the config file' print self.legend, 'cannot create Model Config' return legend = '[' + self.sModelConfig + ']:' # getting items as a dictionary model_config_items = {} model_config_items.update(self.items[self.sModelConfig]) print legend, 'Configuring the model... ' # get the desired name for the model config object _model_config_name = self.check_value(self.sModelConfig, 'name') if _model_config_name == -1: print self.legend, 'using default name for Model Config: exostModelConfig' _model_config_name = 'exostModelConfig' else: print self.legend, 'creating Model Config with name', _model_config_name # create the ModelConfig object and associate it with the Workspace model_config = RooStats.ModelConfig(_model_config_name, _model_config_name) model_config.SetWorkspace(ws) # check if the model is defined if 'model' in model_config_items.keys(): _model = model_config_items['model'] print legend, 'The full likelihood model:', _model #model_config.SetPdf(ws.pdf(_model)) model_config.SetPdf(_model) else: print legend, 'Error: the model PDF is not specified' print legend, 'Error: the model config is invalid' print legend, 'Error: the model config is not added to the workspace' return -1 # check if the observables are specified observables_valid = False if 'observables' in model_config_items.keys(): observables_valid = True _observables = model_config_items['observables'] print legend, 'Observables:', _observables # fixme experimental #if ws.set(_observables) != None: # model_config.SetObservables(ws.set(_observables)) if sets[_observables] != None: model_config.SetObservables(sets[_observables]) else: observables_valid = False print legend, 'Warning: observables set', _observables, 'is not defined' else: observables_valid = False print legend, 'Warning: no observables specified' # observables is an optional field in ModelConfig, no need to invalidate #if observables_valid == False: # print legend, 'Error: the model config is invalid' # print legend, 'Error: the model config is not added to the workspace' # return -1 # check if the global global_observables are specified global_observables_valid = False if 'global_observables' in model_config_items.keys(): global_observables_valid = True _global_observables = model_config_items['global_observables'] print legend, 'Global observables:', _global_observables # fixme experimental #if ws.set(_global_observables) != None: # model_config.SetGlobalObservables(ws.set(_global_observables)) if sets[_global_observables] != None: model_config.SetGlobalObservables(sets[_global_observables]) else: global_observables_valid = False print legend, 'Warning: global observables set', _global_observables, 'is not defined' else: global_observables_valid = False print legend, 'Warning: no global observables specified' #if global_observables_valid == False: # print legend, 'Error: the model config is invalid' # print legend, 'Error: the model config is not added to the workspace' # return -1 # check if the parameters of interest are specified poi_valid = False if 'poi' in model_config_items.keys(): poi_valid = True _poi = model_config_items['poi'] print legend, 'Parameters of interest:', _poi # fixme experimental #if ws.set(_poi) != None: # model_config.SetParametersOfInterest(ws.set(_poi)) if sets[_poi] != None: model_config.SetParametersOfInterest(sets[_poi]) else: poi_valid = False print legend, 'Error: POI set', _poi, 'is not defined' else: poi_valid = False print legend, 'Error: no parameters of interest specified' if poi_valid == False: print legend, 'Error: the model config is invalid' print legend, 'Error: the model config is not added to the workspace' return -1 # check if the nuisance parameters are specified nuis_valid = False if 'nuisance_parameters' in model_config_items.keys(): nuis_valid = True _nuis = model_config_items['nuisance_parameters'] print legend, 'Nuisance parameters:', _nuis #fixme experimental #if ws.set(_nuis) != None: # model_config.SetNuisanceParameters(ws.set(_nuis)) if sets[_nuis] != None: model_config.SetNuisanceParameters(sets[_nuis]) else: nuis_valid = False print legend, 'Error: Nuisance parameter set', _nuis, 'is not defined' else: nuis_valid = False print legend, 'Error: no nuisance parameters specified' if nuis_valid == False: print legend, 'Error: the model config is invalid' print legend, 'Error: the model config is not added to the workspace' return -1 # check if a prior PDF is specified prior_valid = False if 'prior' in model_config_items.keys(): prior_valid = True _prior = model_config_items['prior'] print legend, 'Prior:', _prior if ws.pdf(_prior) != None: model_config.SetPriorPdf(ws.pdf(_prior)) else: prior_valid = False print legend, 'Error: prior', _prior, 'is not defined' else: prior_valid = False print legend, 'Error: no prior PDF specified' if prior_valid == False: print legend, 'Error: the model config is invalid' print legend, 'Error: the model config is not added to the workspace' return -1 # import the model config getattr(ws, 'import')(model_config, _model_config_name) print legend, 'Model config is added to workspace ', ws.GetName() print legend, 'done'
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, 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 frequentistCalculator( ): """Compute the significance (hypothesis test) using a frequentist calculator""" warnings.filterwarnings( action='ignore', category=RuntimeWarning, message='.*class stack<RooAbsArg\*,deque<RooAbsArg\*> >' ) inFile = TFile('Rootfiles/workspace_QCD_RPVSt100tojj_FitP4Gaus_rooFit_1fb.root') myWS = inFile.Get("myWS") data = myWS.data("data_obs") sbModel = myWS.obj("modelConfig") sbModel.SetName("S+B Model") #sbModel.GetParametersOfInterest().first().setVal(5.) #sbModel.GetParametersOfInterest().first().setConstant() sbModel.SetSnapshot( sbModel.GetParametersOfInterest() ) bModel = sbModel.Clone() bModel.SetName("B Model") bModel.GetParametersOfInterest().first().setVal(0.) bModel.GetParametersOfInterest().first().setConstant() bModel.SetSnapshot( bModel.GetParametersOfInterest() ) #c1 = TCanvas('c1', 'c1', 10, 10, 750, 500 ) #test = data.reduce( RooFit.Name('test')) #testHisto = TH1D('test','test', 10, 80, 180) #testHisto = test.fillHistogram( testHisto, RooArgList(x) ) #testHisto.Draw() #x = RooRealVar( "x", "x", 50., 180. ) #testHisto = sbModel.GetPdf() #xframe = x.frame() #testHisto.plotOn( xframe ) #c1.SaveAs("test.pdf") # create the AsymptoticCalculator from data,alt model, null model ################################### Here, roostats is fitting again and the parameters are wrong. asym_calc = RooStats.AsymptoticCalculator(data, bModel, sbModel) asym_calc.SetOneSidedDiscovery( True ) result = asym_calc.GetHypoTest() result.Print() tw = TStopwatch() tw.Start() # Frequentist calculator fc = RooStats.FrequentistCalculator(data, bModel, sbModel) fc.SetToys(10,10) # create the test statistics profll = RooStats.ProfileLikelihoodTestStat( sbModel.GetPdf() ) profll.SetOneSidedDiscovery( True ) # configure ToyMCSampler and set the test statistics toymcs = RooStats.ToyMCSampler( fc.GetTestStatSampler() ) toymcs.SetTestStatistic( profll ) pc = RooStats.ProofConfig( myWS, 10, "", 0 ) #ROOT.kFALSE) toymcs.SetProofConfig(pc) # enable proof ########################################## change to 0 if not sbModel.GetPdf().canBeExtended(): toymcs.SetNEventsPerToy(0) # run the test fqResult = RooStats.HypoTestResult( fc.GetHypoTest() ) fqResult.Print() tw.Stop() print tw.CpuTime(), tw.RealTime() c1 = TCanvas('c1', 'c1', 10, 10, 750, 500 ) plot = RooStats.HypoTestPlot( fqResult ) plot.SetLogYaxis(true) plot.Draw() c1.SaveAs("Plots/Significance.pdf")
#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
[(4800, 5400)], [(4200, 4800)], [(3600, 4200)], [(3000, 3600)], [(2400, 3000)], ] for signal, signalMass, massbinsset in [ ("CIplusLL", "12000", massbinssets2), ("cs_ct14nlo_", "13000", massbinssets1), ("AntiCIplusLL", "12000", massbinssets2), ("DMAxial_Dijet_LO_Mphi_1_1p0_1p0_Mar5_gdmv_0_gdma_1p0_gv_0_ga_1", "6000", ""), ]: for massbins in massbinsset: limits = {} name = "pvalue_LHCa" + signal + "_" + ("_".join([ s[0:4] for s in str(massbins).strip("[]").split("(") ])).strip("_") + "_exp_" + signalMass + "_2016" print name f1 = open(name + ".txt") observed = 0 for l in f1.readlines(): if "CLb" in l: pval = float(l.split("=")[1].split("+")[0].strip(" ")) print "pvalue", pval, "significance", RooStats.PValueToSignificance( (1. - pval) / 2.) if "Significance:" in l: significance = float(l.strip().split(" ")[-1].strip(")")) print "significance", significance
def GetBayesianInterval(filename="workspace.root", wsname='myWS', interactive=False): pInFile = ROOT.TFile(filename, "read") # load workspace pWs = pInFile.Get("myWS") if not pWs: print "workspace ", wsname, " not found" return -1 # printout workspace content pWs.Print() # load and print data from workspace data = pWs.data("data") data.Print() # load and print S+B Model Config pSbHypo = pWs.obj("SbHypo") pSbHypo.Print() # create RooStats Bayesian calculator and set parameters # Metropolis-Hastings algorithm needs a proposal function sp = RooStats.SequentialProposal(10.0) mcmc = RooStats.MCMCCalculator(data, pSbHypo) mcmc.SetConfidenceLevel(0.95) mcmc.SetNumIters(100000) # Metropolis-Hastings algorithm iterations mcmc.SetProposalFunction(sp) mcmc.SetNumBurnInSteps(500) # first N steps to be ignored as burn-in mcmc.SetLeftSideTailFraction(0.0) mcmc.SetNumBins( 40) # for plotting only - does not affect limit calculation # estimate credible interval # NOTE: unfortunate notation: the UpperLimit() name refers # to the upper boundary of an interval, # NOT to the upper limit on the parameter of interest # (it just happens to be the same for the one-sided # interval starting at 0) pMcmcInt = mcmc.GetInterval() upper_bound = pMcmcInt.UpperLimit(pWs.var("xsec")) lower_bound = pMcmcInt.LowerLimit(pWs.var("xsec")) print "one-sided 95%.C.L. bayesian credible interval for xsec: ", "[", lower_bound, ", ", upper_bound, "]" # make posterior PDF plot for POI c1 = ROOT.TCanvas("posterior", "posterior") plot = RooStats.MCMCIntervalPlot(pMcmcInt) plot.Draw() c1.SaveAs("bayesian_mcmc_posterior.pdf") # make scatter plots to visualise the Markov chain c2 = ROOT.TCanvas("xsec_vs_beta_lumi", "xsec vs lumi_beta") plot.DrawChainScatter(pWs.var("xsec"), pWs.var("lumi_beta")) c2.SaveAs("scatter_mcmc_xsec_vs_beta_lumi.pdf") c3 = ROOT.TCanvas("xsec_vs_beta_efficiency", "xsec vs eff_beta") plot.DrawChainScatter(pWs.var("xsec"), pWs.var("eff_beta")) c3.SaveAs("scatter_mcmc_xsec_vs_beta_efficiency.pdf") c4 = ROOT.TCanvas("xsec_vs_beta_nbkg", "xsec vs nbkg_beta") plot.DrawChainScatter(pWs.var("xsec"), pWs.var("nbkg_beta")) c4.SaveAs("scatter_mcmc_xsec_vs_beta_nbkg.pdf") ROOT.gPad.Update() if interactive: raw_input("\npress <enter> to continue")