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 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())
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'
################################################################################ 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)
wspace . defineSet("observables","x") # define parameters of interest wspace . defineSet("poi","s") # define nuisance parameters wspace . defineSet("nuisance_parameters","b") # load data observables = RooArgList( wspace.set("observables") ) data = RooDataSet.read("counting_data_3.ascii", observables) data . SetName("data") getattr(wspace, 'import')(data) # model config modelConfig = RooStats.ModelConfig("counting_model_config") modelConfig . SetWorkspace(wspace) modelConfig . SetPdf(wspace.pdf("model_likelihood")) modelConfig . SetPriorPdf(wspace.pdf("prior_pdf")) modelConfig . SetParametersOfInterest(wspace.set("poi")) modelConfig . SetNuisanceParameters(wspace.set("nuisance_parameters")) getattr(wspace, 'import')(modelConfig, "counting_model_config") # Bayesian Calculator bc = RooStats.BayesianCalculator(data, modelConfig) bc.SetName("exostBayes") getattr(wspace, 'import')(bc) # inspect workspace wspace . Print()
def main(): # ROOT settings ROOT.Math.MinimizerOptions.SetDefaultMinimizer('Minuit') # Workspace w = ROOT.RooWorkspace('w') # Observable E = w.factory('E[0.,100.]') # Constrained parameters and constraint PDF mean = w.factory('mean[50.,49.,51.]') mean_obs = w.factory('mean_obs[50.,49.,51.]') mean_obs.setConstant(True) mean_err = w.factory('mean_err[0.2]') cpdf_mean = w.factory('Gaussian::cpdf_mean(mean,mean_obs,mean_err)') print type(mean), type(mean_obs), type(mean_err), type(cpdf_mean) return sigma = w.factory('sigma[1.,0.5,1.5]') sigma_obs = w.factory('sigma_obs[1.,0.5,1.5]') sigma_obs.setConstant(True) sigma_err = w.factory('sigma_err[0.1]') cpdf_sigma = w.factory('Gaussian::cpdf_sigma(sigma,sigma_obs,sigma_err)') # Signal n_sig = w.factory('n_sig[0.,0.,10.]') pdf_sig = w.factory('Gaussian::pdf_sig(E,mean,sigma)') # Background n_bkg = w.factory('n_bkg[10.,0.,50.]') pdf_bkg = w.factory('Polynomial::pdf_bkg(E,{})') # PDF pdf_sum = w.factory('SUM::pdf_sum(n_sig*pdf_sig,n_bkg*pdf_bkg)') pdf_const = w.factory('PROD::pdf_const({pdf_sum,cpdf_mean,cpdf_sigma})') # ModelConfig mc = RS.ModelConfig('mc', w) mc.SetPdf(pdf_const) mc.SetParametersOfInterest(ROOT.RooArgSet(n_sig)) mc.SetObservables(ROOT.RooArgSet(E)) mc.SetConstraintParameters(ROOT.RooArgSet(mean, sigma)) mc.SetNuisanceParameters(ROOT.RooArgSet(mean, sigma, n_bkg)) mc.SetGlobalObservables(ROOT.RooArgSet(mean_obs, sigma_obs)) # Create empty dataset data = ROOT.RooDataSet('data', 'data', ROOT.RooArgSet(E)) # Profile Likelihood pl = RS.ProfileLikelihoodCalculator(data, mc) pl.SetConfidenceLevel(0.90) interval = pl.GetInterval() print(interval.LowerLimit(n_sig), interval.UpperLimit(n_sig)) plot = RS.LikelihoodIntervalPlot(interval) plot.SetNPoints(50) c = ROOT.TCanvas("c", "c", 800, 1100) plot.Draw("") c.Print("plotLukasPLC.pdf")