Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rs701_BayesianCalculator.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roostats
3## \notebook
4## Bayesian calculator: basic example
5##
6## \macro_image
7## \macro_output
8## \macro_code
9##
10## \date July 2022
11## \authors Artem Busorgin, Gregory Schott (C++ version)
12
13import ROOT
14
15useBkg = True
16confLevel = 0.90
17
18w = ROOT.RooWorkspace("w")
19w.factory("SUM::pdf(s[0.001,15]*Uniform(x[0,1]),b[1,0,2]*Uniform(x))")
20w.factory("Gaussian::prior_b(b,1,1)")
21model = w.factory("PROD::model(pdf,prior_b)") # pdf*priorNuisance
22nuisanceParameters = ROOT.RooArgSet(w["b"])
23
24POI = w["s"]
25priorPOI = w.factory("Uniform::priorPOI(s)")
26priorPOI2 = w.factory("GenericPdf::priorPOI2('1/sqrt(@0)',s)")
27
28w.factory("n[3]") # observed number of events
29# create a data set with n observed events
30data = ROOT.RooDataSet("data", "", {w["x"], w["n"]}, WeightVar="n")
31data.add({w["x"]}, w["n"].getVal())
32
33# to suppress messages when pdf goes to zero
34ROOT.RooMsgService.instance().setGlobalKillBelow(ROOT.RooFit.FATAL)
35
36nuisPar = 0
37if useBkg:
38 nuisPar = nuisanceParameters
39else:
40 w["b"].setVal(0)
41
42size = 1.0 - confLevel
43print("\nBayesian Result using a Flat prior ")
44bcalc = ROOT.RooStats.BayesianCalculator(data, model, {POI}, priorPOI, nuisPar)
45bcalc.SetTestSize(size)
46interval = bcalc.GetInterval()
47cl = bcalc.ConfidenceLevel()
48print(
49 "{}% CL central interval: [ {} - {} ] or {}% CL limits\n".format(
50 cl, interval.LowerLimit(), interval.UpperLimit(), cl + (1.0 - cl) / 2
51 )
52)
53plot = bcalc.GetPosteriorPlot()
54c1 = ROOT.TCanvas("c1", "Bayesian Calculator Result")
55c1.Divide(1, 2)
56c1.cd(1)
57plot.Draw()
58c1.Update()
59
60print("\nBayesian Result using a 1/sqrt(s) prior ")
61bcalc2 = ROOT.RooStats.BayesianCalculator(data, model, {POI}, priorPOI2, nuisPar)
62bcalc2.SetTestSize(size)
63interval2 = bcalc2.GetInterval()
64cl = bcalc2.ConfidenceLevel()
65print(
66 "{}% CL central interval: [ {} - {} ] or {}% CL limits\n".format(
67 cl, interval2.LowerLimit(), interval2.UpperLimit(), cl + (1.0 - cl) / 2
68 )
69)
70plot2 = bcalc2.GetPosteriorPlot()
71c1.cd(2)
72plot2.Draw()
73ROOT.gPad.SetLogy()
74c1.Update()
75
76# observe one event while expecting one background event -> the 95% CL upper limit on s is 4.10
77# observe one event while expecting zero background event -> the 95% CL upper limit on s is 4.74
78
79c1.SaveAs("rs701_BayesianCalculator.png")
80
81# TODO: The BayesianCalculator has to be destructed first. Otherwise, we can get
82# segmentation faults depending on the destruction order, which is random in
83# Python. Probably the issue is that some object has a non-owning pointer to
84# another object, which it uses in its destructor. This should be fixed either
85# in the design of RooStats in C++, or with phythonizations.
86del bcalc, bcalc2
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format