{ "cells": [ { "cell_type": "markdown", "id": "40a6836f", "metadata": {}, "source": [ "# rs701_BayesianCalculator\n", "Bayesian calculator: basic example\n", "\n", "\n", "\n", "\n", "**Author:** Gregory Schott \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:36 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "0539ea4e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:03.759491Z", "iopub.status.busy": "2026-05-19T20:36:03.759314Z", "iopub.status.idle": "2026-05-19T20:36:03.776328Z", "shell.execute_reply": "2026-05-19T20:36:03.775753Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooWorkspace.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooPlot.h\"\n", "#include \"RooMsgService.h\"\n", "\n", "#include \"RooStats/BayesianCalculator.h\"\n", "#include \"RooStats/SimpleInterval.h\"\n", "#include \"TCanvas.h\"\n", "\n", "using namespace RooFit;\n", "using namespace RooStats;" ] }, { "cell_type": "markdown", "id": "0bc0d548", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "b91be5f9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:03.781598Z", "iopub.status.busy": "2026-05-19T20:36:03.781469Z", "iopub.status.idle": "2026-05-19T20:36:04.106967Z", "shell.execute_reply": "2026-05-19T20:36:04.106587Z" } }, "outputs": [], "source": [ "bool useBkg = true;\n", "double confLevel = 0.90;" ] }, { "cell_type": "code", "execution_count": 3, "id": "431caa89", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:04.109549Z", "iopub.status.busy": "2026-05-19T20:36:04.109430Z", "iopub.status.idle": "2026-05-19T20:36:04.315357Z", "shell.execute_reply": "2026-05-19T20:36:04.314967Z" } }, "outputs": [], "source": [ "RooWorkspace *w = new RooWorkspace(\"w\");\n", "w->factory(\"SUM::pdf(s[0.001,15]*Uniform(x[0,1]),b[1,0,2]*Uniform(x))\");\n", "w->factory(\"Gaussian::prior_b(b,1,1)\");\n", "w->factory(\"PROD::model(pdf,prior_b)\");\n", "RooAbsPdf *model = w->pdf(\"model\"); // pdf*priorNuisance\n", "RooArgSet nuisanceParameters(*(w->var(\"b\")));\n", "\n", "RooAbsRealLValue *POI = w->var(\"s\");\n", "RooAbsPdf *priorPOI = (RooAbsPdf *)w->factory(\"Uniform::priorPOI(s)\");\n", "RooAbsPdf *priorPOI2 = (RooAbsPdf *)w->factory(\"GenericPdf::priorPOI2('1/sqrt(@0)',s)\");\n", "\n", "w->factory(\"n[3]\"); // observed number of events" ] }, { "cell_type": "markdown", "id": "72060fa8", "metadata": {}, "source": [ "create a data set with n observed events" ] }, { "cell_type": "code", "execution_count": 4, "id": "db327eff", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:04.326262Z", "iopub.status.busy": "2026-05-19T20:36:04.326122Z", "iopub.status.idle": "2026-05-19T20:36:04.533394Z", "shell.execute_reply": "2026-05-19T20:36:04.532631Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_62:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", " RooDataSet data(\"data\", \"\", {*w->var(\"x\"), *w->var(\"n\")}, RooFit::WeightVar(\"n\"));\n", " ^\n" ] } ], "source": [ "RooDataSet data(\"data\", \"\", {*w->var(\"x\"), *w->var(\"n\")}, RooFit::WeightVar(\"n\"));\n", "data.add({*(w->var(\"x\"))}, w->var(\"n\")->getVal());" ] }, { "cell_type": "markdown", "id": "713f42f8", "metadata": {}, "source": [ "to suppress messages when pdf goes to zero" ] }, { "cell_type": "code", "execution_count": 5, "id": "210050ca", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:04.534759Z", "iopub.status.busy": "2026-05-19T20:36:04.534630Z", "iopub.status.idle": "2026-05-19T20:36:04.744310Z", "shell.execute_reply": "2026-05-19T20:36:04.743311Z" } }, "outputs": [], "source": [ "RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);\n", "\n", "RooArgSet *nuisPar = nullptr;\n", "if (useBkg)\n", " nuisPar = &nuisanceParameters;" ] }, { "cell_type": "markdown", "id": "4c99fe48", "metadata": {}, "source": [ "if (!useBkg) ((RooRealVar *)w->var(\"b\"))->setVal(0);" ] }, { "cell_type": "code", "execution_count": 6, "id": "d8d260c0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:04.745749Z", "iopub.status.busy": "2026-05-19T20:36:04.745622Z", "iopub.status.idle": "2026-05-19T20:36:04.962205Z", "shell.execute_reply": "2026-05-19T20:36:04.961272Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_64:4:26: error: reference to 'data' is ambiguous\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", " ^\n", "input_line_62:2:13: note: candidate found by name lookup is 'data'\n", " RooDataSet data(\"data\", \"\", {*w->var(\"x\"), *w->var(\"n\")}, RooFit::WeightVar(\"n\"));\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n", "input_line_64:4:26: error: unknown type name 'data'\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", " ^\n", "input_line_64:4:33: error: a type specifier is required for all declarations\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", " ^\n", "input_line_64:4:58: error: a type specifier is required for all declarations\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", " ^\n", "input_line_64:4:68: error: unknown type name 'nuisPar'\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", " ^\n", "input_line_64:18:27: error: reference to 'data' is ambiguous\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", " ^\n", "input_line_62:2:13: note: candidate found by name lookup is 'data'\n", " RooDataSet data(\"data\", \"\", {*w->var(\"x\"), *w->var(\"n\")}, RooFit::WeightVar(\"n\"));\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n", " data(initializer_list<_Tp> __il) noexcept\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n", " data(_Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n", " data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n", " data(_Tp (&__array)[_Nm]) noexcept\n", " ^\n", "input_line_64:18:27: error: unknown type name 'data'\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", " ^\n", "input_line_64:18:34: error: a type specifier is required for all declarations\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", " ^\n", "input_line_64:18:59: error: a type specifier is required for all declarations\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", " ^\n", "input_line_64:18:70: error: unknown type name 'nuisPar'\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", " ^\n", "input_line_64:2:2: warning: 'size' shadows a declaration with the same name in the 'std' namespace; use '::size' to reference this declaration\n", " double size = 1. - confLevel;\n", " ^\n" ] } ], "source": [ "double size = 1. - confLevel;\n", "std::cout << \"\\nBayesian Result using a Flat prior \" << std::endl;\n", "BayesianCalculator bcalc(data, *model, RooArgSet(*POI), *priorPOI, nuisPar);\n", "bcalc.SetTestSize(size);\n", "SimpleInterval *interval = bcalc.GetInterval();\n", "double cl = bcalc.ConfidenceLevel();\n", "std::cout << cl << \"% CL central interval: [ \" << interval->LowerLimit() << \" - \" << interval->UpperLimit()\n", " << \" ] or \" << cl + (1. - cl) / 2 << \"% CL limits\\n\";\n", "RooPlot *plot = bcalc.GetPosteriorPlot();\n", "TCanvas *c1 = new TCanvas(\"c1\", \"Bayesian Calculator Result\");\n", "c1->Divide(1, 2);\n", "c1->cd(1);\n", "plot->Draw();\n", "c1->Update();\n", "\n", "std::cout << \"\\nBayesian Result using a 1/sqrt(s) prior \" << std::endl;\n", "BayesianCalculator bcalc2(data, *model, RooArgSet(*POI), *priorPOI2, nuisPar);\n", "bcalc2.SetTestSize(size);\n", "SimpleInterval *interval2 = bcalc2.GetInterval();\n", "cl = bcalc2.ConfidenceLevel();\n", "std::cout << cl << \"% CL central interval: [ \" << interval2->LowerLimit() << \" - \" << interval2->UpperLimit()\n", " << \" ] or \" << cl + (1. - cl) / 2 << \"% CL limits\\n\";\n", "\n", "RooPlot *plot2 = bcalc2.GetPosteriorPlot();\n", "c1->cd(2);\n", "plot2->Draw();\n", "gPad->SetLogy();\n", "c1->Update();" ] }, { "cell_type": "markdown", "id": "8a241397", "metadata": {}, "source": [ "observe one event while expecting one background event -> the 95% CL upper limit on s is 4.10\n", "observe one event while expecting zero background event -> the 95% CL upper limit on s is 4.74" ] }, { "cell_type": "markdown", "id": "8a05118e", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "ce2a8d05", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:04.975030Z", "iopub.status.busy": "2026-05-19T20:36:04.974852Z", "iopub.status.idle": "2026-05-19T20:36:05.184280Z", "shell.execute_reply": "2026-05-19T20:36:05.183582Z" } }, "outputs": [], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }