71ROOT.gInterpreter.Declare(
73using namespace RooFit;
74using namespace RooStats;
76class BinCountTestStat : public TestStatistic {
78 BinCountTestStat(void) : fColumnName("tmp") {}
79 BinCountTestStat(string columnName) : fColumnName(columnName) {}
81 virtual Double_t Evaluate(RooAbsData &data, RooArgSet & /*nullPOI*/)
83 // This is the main method in the interface
85 for (int i = 0; i < data.numEntries(); i++) {
86 value += data.get(i)->getRealValue(fColumnName.c_str());
90 virtual const TString GetVarName() const { return fColumnName; }
96 ClassDef(BinCountTestStat, 1)
131w = ROOT.RooWorkspace(
"w")
132w.factory(
"Poisson::px(x[150,0,500],sum::splusb(s[0,0,100],b[100,0.1,300]))")
133w.factory(
"Poisson::py(y[100,0.1,500],prod::taub(tau[1.],b))")
134w.factory(
"PROD::model(px,py)")
135w.factory(
"Uniform::prior_b(b)")
140msglevel = ROOT.RooMsgService.instance().globalKillBelow()
153w.factory(
"PROJ::averagedModel(PROD::foo(px|b,py,prior_b),b)")
155ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.ERROR)
158frame = w.var(
"x").frame(Range=(50, 230))
159w.pdf(
"averagedModel").plotOn(frame, LineColor=
"r")
160w.pdf(
"px").plotOn(frame, LineColor=
"g")
161w.var(
"s").setVal(50.0)
162w.pdf(
"averagedModel").plotOn(frame, LineColor=
"b")
165w.var(
"s").setVal(0.0)
172w.var(
"y").setVal(100)
173w.var(
"x").setVal(150)
174cdf = w.pdf(
"averagedModel").createCdf(w.var(
"x"))
177print(
"-----------------------------------------")
179print(f
"Hybrid p-value from direct integration = {1 - cdf.getVal()}")
180print(f
"Z_Gamma Significance = {ROOT.RooStats.PValueToSignificance(1 - cdf.getVal())}")
181ROOT.RooMsgService.instance().setGlobalKillBelow(msglevel)
191p_Bi = ROOT.RooStats.NumberCountingUtils.BinomialWithTauObsP(150, 100, 1)
192Z_Bi = ROOT.RooStats.NumberCountingUtils.BinomialWithTauObsZ(150, 100, 1)
193print(
"-----------------------------------------")
195print(f
"Z_Bi p-value (analytic): {p_Bi}")
196print(f
"Z_Bi significance (analytic): {Z_Bi}")
220w.defineSet(
"obs",
"x")
221w.defineSet(
"poi",
"s")
224data = ROOT.RooDataSet(
"d",
"d", w.set(
"obs"))
225data.add(w.set(
"obs"))
230b_model = ROOT.RooStats.ModelConfig(
"B_model", w)
231b_model.SetPdf(w.pdf(
"px"))
232b_model.SetObservables(w.set(
"obs"))
233b_model.SetParametersOfInterest(w.set(
"poi"))
234w.var(
"s").setVal(0.0)
236b_model.SetSnapshot(w.set(
"poi"))
239sb_model = ROOT.RooStats.ModelConfig(
"S+B_model", w)
240sb_model.SetPdf(w.pdf(
"px"))
241sb_model.SetObservables(w.set(
"obs"))
242sb_model.SetParametersOfInterest(w.set(
"poi"))
243w.var(
"s").setVal(50.0)
245sb_model.SetSnapshot(w.set(
"poi"))
255binCount = ROOT.BinCountTestStat(
"x")
277w.factory(
"Gaussian::gauss_prior(b,y, expr::sqrty('sqrt(y)',y))")
281w.factory(
"Lognormal::lognorm_prior(b,y, expr::kappa('1+1./sqrt(y)',y))")
299hc1 = ROOT.RooStats.HybridCalculator(data, sb_model, b_model)
300toymcs1 = ROOT.RooStats.ToyMCSampler()
301toymcs1 = hc1.GetTestStatSampler()
302toymcs1.SetNEventsPerToy(1)
304toymcs1.SetTestStatistic(binCount)
306hc1.SetToys(ntoys, ntoys // nToysRatio)
307hc1.ForcePriorNuisanceAlt(w.pdf(
"py"))
308hc1.ForcePriorNuisanceNull(w.pdf(
"py"))
321ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.ERROR)
323r1 = hc1.GetHypoTest()
324ROOT.RooMsgService.instance().setGlobalKillBelow(msglevel)
326print(
"-----------------------------------------")
335p1 = ROOT.RooStats.HypoTestPlot(r1, 30)
347slrts = ROOT.RooStats.SimpleLikelihoodRatioTestStat(b_model.GetPdf(), sb_model.GetPdf())
348slrts.SetNullParameters(b_model.GetSnapshot())
349slrts.SetAltParameters(sb_model.GetSnapshot())
352hc2 = ROOT.RooStats.HybridCalculator(data, sb_model, b_model)
353toymcs2 = ROOT.RooStats.ToyMCSampler()
354toymcs2 = hc2.GetTestStatSampler()
355toymcs2.SetNEventsPerToy(1)
356toymcs2.SetTestStatistic(slrts)
357hc2.SetToys(ntoys, ntoys // nToysRatio)
358hc2.ForcePriorNuisanceAlt(w.pdf(
"py"))
359hc2.ForcePriorNuisanceNull(w.pdf(
"py"))
372ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.ERROR)
374r2 = hc2.GetHypoTest()
375print(
"-----------------------------------------")
382ROOT.RooMsgService.instance().setGlobalKillBelow(msglevel)
385p2 = ROOT.RooStats.HypoTestPlot(r2, 30)
402w.defineSet(
"obsXY",
"x,y")
405w.var(
"x").setVal(150.0)
406w.var(
"y").setVal(100.0)
407dataXY = ROOT.RooDataSet(
"dXY",
"dXY", w.set(
"obsXY"))
408dataXY.add(w.set(
"obsXY"))
411b_modelXY = ROOT.RooStats.ModelConfig(
"B_modelXY", w)
412b_modelXY.SetPdf(w.pdf(
"model"))
414b_modelXY.SetObservables(w.set(
"obsXY"))
415b_modelXY.SetParametersOfInterest(w.set(
"poi"))
416w.var(
"s").setVal(0.0)
418b_modelXY.SetSnapshot(w.set(
"poi"))
421sb_modelXY = ROOT.RooStats.ModelConfig(
"S+B_modelXY", w)
422sb_modelXY.SetPdf(w.pdf(
"model"))
424sb_modelXY.SetObservables(w.set(
"obsXY"))
425sb_modelXY.SetParametersOfInterest(w.set(
"poi"))
426w.var(
"s").setVal(50.0)
428sb_modelXY.SetSnapshot(w.set(
"poi"))
442ropl = ROOT.RooStats.RatioOfProfiledLikelihoodsTestStat(
443 b_modelXY.GetPdf(), sb_modelXY.GetPdf(), sb_modelXY.GetSnapshot()
445ropl.SetSubtractMLE(
False)
449profll = ROOT.RooStats.ProfileLikelihoodTestStat(b_modelXY.GetPdf())
453mlets = ROOT.RooStats.MaxLikelihoodEstimateTestStat(sb_modelXY.GetPdf(), w.var(
"s"))
461w.factory(
"Gamma::gamma_y0(b,sum::temp0(y0,1),1,0)")
462w.factory(
"Gaussian::gauss_prior_y0(b,y0, expr::sqrty0('sqrt(y0)',y0))")
465hc3 = ROOT.RooStats.HybridCalculator(dataXY, sb_modelXY, b_modelXY)
466toymcs3 = ROOT.RooStats.ToyMCSampler()
467toymcs3 = hc3.GetTestStatSampler()
468toymcs3.SetNEventsPerToy(1)
469toymcs3.SetTestStatistic(slrts)
470hc3.SetToys(ntoys, ntoys // nToysRatio)
471hc3.ForcePriorNuisanceAlt(w.pdf(
"gamma_y0"))
472hc3.ForcePriorNuisanceNull(w.pdf(
"gamma_y0"))
480toymcs3.SetTestStatistic(profll)
485ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.ERROR)
487r3 = hc3.GetHypoTest()
488print(
"-----------------------------------------")
495ROOT.RooMsgService.instance().setGlobalKillBelow(msglevel)
499p3 = ROOT.RooStats.HypoTestPlot(r3, 50)