{ "cells": [ { "cell_type": "markdown", "id": "8c25b821", "metadata": {}, "source": [ "# rf605_profilell\n", "Likelihood and minimization: working with the profile likelihood estimator\n", "\n", "\n", "\n", "\n", "**Author:** Wouter Verkerke \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:33 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "1d364e45", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:07.820340Z", "iopub.status.busy": "2026-05-19T20:33:07.820228Z", "iopub.status.idle": "2026-05-19T20:33:07.834973Z", "shell.execute_reply": "2026-05-19T20:33:07.834494Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooAddPdf.h\"\n", "#include \"RooMinimizer.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "62578488", "metadata": {}, "source": [ "Create model and dataset\n", "-----------------------------------------------" ] }, { "cell_type": "markdown", "id": "a21e00c7", "metadata": {}, "source": [ "Observable" ] }, { "cell_type": "code", "execution_count": 2, "id": "1dce2bde", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:07.836278Z", "iopub.status.busy": "2026-05-19T20:33:07.836163Z", "iopub.status.idle": "2026-05-19T20:33:08.151887Z", "shell.execute_reply": "2026-05-19T20:33:08.151218Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -20, 20);" ] }, { "cell_type": "markdown", "id": "5ddc7f1a", "metadata": {}, "source": [ "Model (intentional strong correlations)" ] }, { "cell_type": "code", "execution_count": 3, "id": "8c2199f2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:08.153986Z", "iopub.status.busy": "2026-05-19T20:33:08.153867Z", "iopub.status.idle": "2026-05-19T20:33:08.362990Z", "shell.execute_reply": "2026-05-19T20:33:08.362349Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 'sigma_g1' with range [-inf, inf] of the RooGaussian 'g1' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooRealVar mean(\"mean\", \"mean of g1 and g2\", 0, -10, 10);\n", "RooRealVar sigma_g1(\"sigma_g1\", \"width of g1\", 3);\n", "RooGaussian g1(\"g1\", \"g1\", x, mean, sigma_g1);\n", "\n", "RooRealVar sigma_g2(\"sigma_g2\", \"width of g2\", 4, 3.0, 6.0);\n", "RooGaussian g2(\"g2\", \"g2\", x, mean, sigma_g2);\n", "\n", "RooRealVar frac(\"frac\", \"frac\", 0.5, 0.0, 1.0);\n", "RooAddPdf model(\"model\", \"model\", RooArgList(g1, g2), frac);" ] }, { "cell_type": "markdown", "id": "ed9efd1b", "metadata": {}, "source": [ "Generate 1000 events" ] }, { "cell_type": "code", "execution_count": 4, "id": "3b35bf65", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:08.364364Z", "iopub.status.busy": "2026-05-19T20:33:08.364251Z", "iopub.status.idle": "2026-05-19T20:33:08.573131Z", "shell.execute_reply": "2026-05-19T20:33:08.572449Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_51:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", " std::unique_ptr data{model.generate(x, 1000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{model.generate(x, 1000)};" ] }, { "cell_type": "markdown", "id": "057e271f", "metadata": {}, "source": [ "Construct plain likelihood\n", "---------------------------------------------------" ] }, { "cell_type": "markdown", "id": "237467be", "metadata": {}, "source": [ "Construct unbinned likelihood" ] }, { "cell_type": "code", "execution_count": 5, "id": "9dbf0bca", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:08.574552Z", "iopub.status.busy": "2026-05-19T20:33:08.574438Z", "iopub.status.idle": "2026-05-19T20:33:08.783867Z", "shell.execute_reply": "2026-05-19T20:33:08.783258Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_52:2:51: error: reference to 'data' is ambiguous\n", " std::unique_ptr nll{model.createNLL(*data, NumCPU(2))};\n", " ^\n", "input_line_51:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{model.generate(x, 1000)};\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" ] } ], "source": [ "std::unique_ptr nll{model.createNLL(*data, NumCPU(2))};" ] }, { "cell_type": "markdown", "id": "c4231a62", "metadata": {}, "source": [ "Minimize likelihood w.r.t all parameters before making plots" ] }, { "cell_type": "code", "execution_count": 6, "id": "7c270d00", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:08.785308Z", "iopub.status.busy": "2026-05-19T20:33:08.785196Z", "iopub.status.idle": "2026-05-19T20:33:08.994078Z", "shell.execute_reply": "2026-05-19T20:33:08.993448Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:18: error: use of undeclared identifier 'nll'\n", " (RooMinimizer(* nll).migrad())\n", " ^\n", "Error in : Error evaluating expression (RooMinimizer(* nll).migrad())\n", "Execution of your code was aborted.\n" ] } ], "source": [ "RooMinimizer(*nll).migrad();" ] }, { "cell_type": "markdown", "id": "0204a1e5", "metadata": {}, "source": [ "Plot likelihood scan frac" ] }, { "cell_type": "code", "execution_count": 7, "id": "3a3963e4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:08.995590Z", "iopub.status.busy": "2026-05-19T20:33:08.995477Z", "iopub.status.idle": "2026-05-19T20:33:09.204251Z", "shell.execute_reply": "2026-05-19T20:33:09.203784Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()'\n", "nll->plotOn(frame1, ShiftToZero());\n", " ^~~~~~~~~~~\n" ] } ], "source": [ "RooPlot *frame1 = frac.frame(Bins(10), Range(0.01, 0.95), Title(\"LL and profileLL in frac\"));\n", "nll->plotOn(frame1, ShiftToZero());" ] }, { "cell_type": "markdown", "id": "7c725479", "metadata": {}, "source": [ "Plot likelihood scan in sigma_g2" ] }, { "cell_type": "code", "execution_count": 8, "id": "e4d8f8d1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:09.205918Z", "iopub.status.busy": "2026-05-19T20:33:09.205798Z", "iopub.status.idle": "2026-05-19T20:33:09.414917Z", "shell.execute_reply": "2026-05-19T20:33:09.414132Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()'\n", "nll->plotOn(frame2, ShiftToZero());\n", " ^~~~~~~~~~~\n" ] } ], "source": [ "RooPlot *frame2 = sigma_g2.frame(Bins(10), Range(3.3, 5.0), Title(\"LL and profileLL in sigma_g2\"));\n", "nll->plotOn(frame2, ShiftToZero());" ] }, { "cell_type": "markdown", "id": "3fea9706", "metadata": {}, "source": [ "Construct profile likelihood in frac\n", "-----------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "49e467bc", "metadata": {}, "source": [ "The profile likelihood estimator on nll for frac will minimize nll w.r.t\n", "all floating parameters except frac for each evaluation" ] }, { "cell_type": "code", "execution_count": 9, "id": "2934e836", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:09.416480Z", "iopub.status.busy": "2026-05-19T20:33:09.416362Z", "iopub.status.idle": "2026-05-19T20:33:09.723679Z", "shell.execute_reply": "2026-05-19T20:33:09.723175Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _GLOBAL__sub_I_cling_module_277, _ZN12__cling_N52624__dynamic__cling_Un1Qu30E, __orc_init_func.cling-module-277, _ZN12__cling_N5268pll_fracE, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, cling_module_277_, cling_module_277_.6, _ZN12__cling_N52616__cling_Un1Qu326EPv, $.cling-module-277.__inits.0 }) }\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*)\n", "Maybe you need to load the corresponding shared library?\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler()\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "std::unique_ptr pll_frac{nll->createProfile(frac)};" ] }, { "cell_type": "markdown", "id": "8ed7d8ef", "metadata": {}, "source": [ "Plot the profile likelihood in frac" ] }, { "cell_type": "code", "execution_count": 10, "id": "367f0597", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:09.725360Z", "iopub.status.busy": "2026-05-19T20:33:09.725242Z", "iopub.status.idle": "2026-05-19T20:33:09.933988Z", "shell.execute_reply": "2026-05-19T20:33:09.933447Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_58:2:37: error: cannot take the address of an rvalue of type 'EColor'\n", " pll_frac->plotOn(frame1, LineColor(kRed));\n", " ^~~~\n", "Error while creating dynamic expression for:\n", " pll_frac->plotOn(frame1, LineColor(kRed))\n" ] } ], "source": [ "pll_frac->plotOn(frame1, LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "610fb6fe", "metadata": {}, "source": [ "Adjust frame maximum for visual clarity" ] }, { "cell_type": "code", "execution_count": 11, "id": "c9ecd460", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:09.935653Z", "iopub.status.busy": "2026-05-19T20:33:09.935525Z", "iopub.status.idle": "2026-05-19T20:33:10.144445Z", "shell.execute_reply": "2026-05-19T20:33:10.143904Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-277 }) }\n", "input_line_60:2:3: error: use of undeclared identifier 'frame1'\n", " (frame1->SetMinimum(0))\n", " ^\n", "Error in : Error evaluating expression (frame1->SetMinimum(0))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "frame1->SetMinimum(0);\n", "frame1->SetMaximum(3);" ] }, { "cell_type": "markdown", "id": "e5abaaaa", "metadata": {}, "source": [ "Construct profile likelihood in sigma_g2\n", "-------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "9780daf8", "metadata": {}, "source": [ "The profile likelihood estimator on nll for sigma_g2 will minimize nll\n", "w.r.t all floating parameters except sigma_g2 for each evaluation" ] }, { "cell_type": "code", "execution_count": 12, "id": "5a833ab8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:10.146223Z", "iopub.status.busy": "2026-05-19T20:33:10.146107Z", "iopub.status.idle": "2026-05-19T20:33:10.355795Z", "shell.execute_reply": "2026-05-19T20:33:10.355277Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv }) }\n", "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { cling_module_280_, cling_module_280_.6, _GLOBAL__sub_I_cling_module_280, _ZN12__cling_N52911pll_sigmag2E, $.cling-module-280.__inits.0, __orc_init_func.cling-module-280, _ZN12__cling_N52916__cling_Un1Qu330EPv, _ZN12__cling_N52924__dynamic__cling_Un1Qu31E }) }\n" ] } ], "source": [ "std::unique_ptr pll_sigmag2{nll->createProfile(sigma_g2)};" ] }, { "cell_type": "markdown", "id": "c51ce0f5", "metadata": {}, "source": [ "Plot the profile likelihood in sigma_g2" ] }, { "cell_type": "code", "execution_count": 13, "id": "0c9b2e0b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:10.357484Z", "iopub.status.busy": "2026-05-19T20:33:10.357368Z", "iopub.status.idle": "2026-05-19T20:33:10.566133Z", "shell.execute_reply": "2026-05-19T20:33:10.565617Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_62:2:40: error: cannot take the address of an rvalue of type 'EColor'\n", " pll_sigmag2->plotOn(frame2, LineColor(kRed));\n", " ^~~~\n", "Error while creating dynamic expression for:\n", " pll_sigmag2->plotOn(frame2, LineColor(kRed))\n" ] } ], "source": [ "pll_sigmag2->plotOn(frame2, LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "f5943305", "metadata": {}, "source": [ "Adjust frame maximum for visual clarity" ] }, { "cell_type": "code", "execution_count": 14, "id": "f934e4d9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:10.567895Z", "iopub.status.busy": "2026-05-19T20:33:10.567779Z", "iopub.status.idle": "2026-05-19T20:33:10.776665Z", "shell.execute_reply": "2026-05-19T20:33:10.776144Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-280 }) }\n", "input_line_64:2:3: error: use of undeclared identifier 'frame2'\n", " (frame2->SetMinimum(0))\n", " ^\n", "Error in : Error evaluating expression (frame2->SetMinimum(0))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "frame2->SetMinimum(0);\n", "frame2->SetMaximum(3);" ] }, { "cell_type": "markdown", "id": "87924f78", "metadata": {}, "source": [ "Make canvas and draw RooPlots" ] }, { "cell_type": "code", "execution_count": 15, "id": "de08a422", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:10.778308Z", "iopub.status.busy": "2026-05-19T20:33:10.778190Z", "iopub.status.idle": "2026-05-19T20:33:10.987041Z", "shell.execute_reply": "2026-05-19T20:33:10.986522Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_66:2:3: error: use of undeclared identifier 'frame1'\n", " (frame1->GetYaxis()->SetTitleOffset(1.3999999999999999))\n", " ^\n", "Error in : Error evaluating expression (frame1->GetYaxis()->SetTitleOffset(1.3999999999999999))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "TCanvas *c = new TCanvas(\"rf605_profilell\", \"rf605_profilell\", 800, 400);\n", "c->Divide(2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "frame1->GetYaxis()->SetTitleOffset(1.4);\n", "frame1->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "frame2->GetYaxis()->SetTitleOffset(1.4);\n", "frame2->Draw();" ] }, { "cell_type": "markdown", "id": "ee8d637c", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 16, "id": "39324988", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:33:10.988721Z", "iopub.status.busy": "2026-05-19T20:33:10.988586Z", "iopub.status.idle": "2026-05-19T20:33:11.217649Z", "shell.execute_reply": "2026-05-19T20:33:11.217128Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "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 }