{ "cells": [ { "cell_type": "markdown", "id": "85946a31", "metadata": {}, "source": [ "# StandardProfileLikelihoodDemo\n", "Standard demo of the Profile Likelihood calculator\n", "StandardProfileLikelihoodDemo\n", "\n", "This is a standard demo that can be used with any ROOT file\n", "prepared in the standard way. You specify:\n", " - name for input ROOT file\n", " - name of workspace inside ROOT file that holds model and data\n", " - name of ModelConfig that specifies details for calculator tools\n", " - name of dataset\n", "\n", "With default parameters the macro will attempt to run the\n", "standard hist2workspace example and read the ROOT file\n", "that it produces.\n", "\n", "The actual heart of the demo is only about 10 lines long.\n", "\n", "The ProfileLikelihoodCalculator is based on Wilks's theorem\n", "and the asymptotic properties of the profile likelihood ratio\n", "(eg. that it is chi-square distributed for the true value).\n", "\n", "\n", "\n", "\n", "**Author:** Kyle Cranmer \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": "c4615b0e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:31.398462Z", "iopub.status.busy": "2026-05-19T20:36:31.398300Z", "iopub.status.idle": "2026-05-19T20:36:31.415645Z", "shell.execute_reply": "2026-05-19T20:36:31.415274Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TFile.h\"\n", "#include \"TROOT.h\"\n", "#include \"TSystem.h\"\n", "#include \"RooWorkspace.h\"\n", "#include \"RooAbsData.h\"\n", "#include \"RooRealVar.h\"\n", "\n", "#include \"RooStats/ModelConfig.h\"\n", "#include \"RooStats/ProfileLikelihoodCalculator.h\"\n", "#include \"RooStats/LikelihoodInterval.h\"\n", "#include \"RooStats/LikelihoodIntervalPlot.h\"\n", "\n", "using namespace RooFit;\n", "using namespace RooStats;\n", "\n", "struct ProfileLikelihoodOptions {\n", "\n", " double confLevel = 0.95;\n", " int nScanPoints = 50;\n", " bool plotAsTF1 = false;\n", " double poiMinPlot = 1;\n", " double poiMaxPlot = 0;\n", " bool doHypoTest = false;\n", " double nullValue = 0;\n", "};\n", "\n", "ProfileLikelihoodOptions optPL;" ] }, { "cell_type": "markdown", "id": "1f0e49c4", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "5aa68e16", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:31.425984Z", "iopub.status.busy": "2026-05-19T20:36:31.425856Z", "iopub.status.idle": "2026-05-19T20:36:31.787039Z", "shell.execute_reply": "2026-05-19T20:36:31.779279Z" } }, "outputs": [], "source": [ "const char *infile = \"\";\n", "const char *workspaceName = \"combined\";\n", "const char *modelConfigName = \"ModelConfig\";\n", "const char *dataName = \"obsData\";" ] }, { "cell_type": "code", "execution_count": 3, "id": "15dac456", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:31.799382Z", "iopub.status.busy": "2026-05-19T20:36:31.799240Z", "iopub.status.idle": "2026-05-19T20:36:32.015110Z", "shell.execute_reply": "2026-05-19T20:36:32.014487Z" } }, "outputs": [], "source": [ "double confLevel = optPL.confLevel;\n", "double nScanPoints = optPL.nScanPoints;\n", "bool plotAsTF1 = optPL.plotAsTF1;\n", "double poiXMin = optPL.poiMinPlot;\n", "double poiXMax = optPL.poiMaxPlot;\n", "bool doHypoTest = optPL.doHypoTest;\n", "double nullParamValue = optPL.nullValue;" ] }, { "cell_type": "markdown", "id": "183ba9a0", "metadata": {}, "source": [ "-------------------------------------------------------\n", "First part is just to access a user-defined file\n", "or create the standard example file if it doesn't exist" ] }, { "cell_type": "code", "execution_count": 4, "id": "c6bb5240", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:32.019319Z", "iopub.status.busy": "2026-05-19T20:36:32.019196Z", "iopub.status.idle": "2026-05-19T20:36:32.228236Z", "shell.execute_reply": "2026-05-19T20:36:32.227655Z" } }, "outputs": [], "source": [ "const char *filename = \"\";\n", "if (!strcmp(infile, \"\")) {\n", " filename = \"results/example_combined_GaussExample_model.root\";\n", " bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code\n", " // if file does not exists generate with histfactory\n", " if (!fileExist) {\n", " // Normally this would be run on the command line\n", " cout << \"will run standard hist2workspace example\" << endl;\n", " gROOT->ProcessLine(\".! prepareHistFactory .\");\n", " gROOT->ProcessLine(\".! hist2workspace config/example.xml\");\n", " cout << \"\\n\\n---------------------\" << endl;\n", " cout << \"Done creating example input\" << endl;\n", " cout << \"---------------------\\n\\n\" << endl;\n", " }\n", "\n", "} else\n", " filename = infile;" ] }, { "cell_type": "markdown", "id": "0e3312e8", "metadata": {}, "source": [ "Try to open the file" ] }, { "cell_type": "code", "execution_count": 5, "id": "9f8c5f51", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:32.237331Z", "iopub.status.busy": "2026-05-19T20:36:32.237175Z", "iopub.status.idle": "2026-05-19T20:36:32.581990Z", "shell.execute_reply": "2026-05-19T20:36:32.572360Z" } }, "outputs": [], "source": [ "TFile *file = TFile::Open(filename);" ] }, { "cell_type": "markdown", "id": "d6b662f9", "metadata": {}, "source": [ "-------------------------------------------------------\n", "Tutorial starts here\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "6861333c", "metadata": {}, "source": [ "get the workspace out of the file" ] }, { "cell_type": "code", "execution_count": 6, "id": "6b6bf5dd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:32.583682Z", "iopub.status.busy": "2026-05-19T20:36:32.583546Z", "iopub.status.idle": "2026-05-19T20:36:32.970251Z", "shell.execute_reply": "2026-05-19T20:36:32.960695Z" } }, "outputs": [], "source": [ "RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);" ] }, { "cell_type": "markdown", "id": "f8518039", "metadata": {}, "source": [ "get the modelConfig out of the file" ] }, { "cell_type": "code", "execution_count": 7, "id": "ae8ee4bd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:32.979562Z", "iopub.status.busy": "2026-05-19T20:36:32.979428Z", "iopub.status.idle": "2026-05-19T20:36:33.190211Z", "shell.execute_reply": "2026-05-19T20:36:33.189210Z" } }, "outputs": [], "source": [ "ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);" ] }, { "cell_type": "markdown", "id": "e6fe0a19", "metadata": {}, "source": [ "get the modelConfig out of the file" ] }, { "cell_type": "code", "execution_count": 8, "id": "35540ab8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:33.191858Z", "iopub.status.busy": "2026-05-19T20:36:33.191728Z", "iopub.status.idle": "2026-05-19T20:36:33.416008Z", "shell.execute_reply": "2026-05-19T20:36:33.415488Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_76:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n", " RooAbsData *data = w->data(dataName);\n", " ^\n" ] } ], "source": [ "RooAbsData *data = w->data(dataName);" ] }, { "cell_type": "markdown", "id": "46ea5bd3", "metadata": {}, "source": [ "---------------------------------------------\n", "create and use the ProfileLikelihoodCalculator\n", "to find and plot the 95% confidence interval\n", "on the parameter of interest as specified\n", "in the model config" ] }, { "cell_type": "code", "execution_count": 9, "id": "34aedf99", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:33.434237Z", "iopub.status.busy": "2026-05-19T20:36:33.434066Z", "iopub.status.idle": "2026-05-19T20:36:33.651217Z", "shell.execute_reply": "2026-05-19T20:36:33.650738Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_77:2:34: error: reference to 'data' is ambiguous\n", " ProfileLikelihoodCalculator pl(*data, *mc);\n", " ^\n", "input_line_76:2:14: note: candidate found by name lookup is 'data'\n", " RooAbsData *data = w->data(dataName);\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": [ "ProfileLikelihoodCalculator pl(*data, *mc);\n", "pl.SetConfidenceLevel(confLevel); // 95% interval\n", "LikelihoodInterval *interval = pl.GetInterval();" ] }, { "cell_type": "markdown", "id": "ad32965f", "metadata": {}, "source": [ "print out the interval on the first Parameter of Interest" ] }, { "cell_type": "code", "execution_count": 10, "id": "96876f4b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:33.652708Z", "iopub.status.busy": "2026-05-19T20:36:33.652581Z", "iopub.status.idle": "2026-05-19T20:36:33.863134Z", "shell.execute_reply": "2026-05-19T20:36:33.862643Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_79:2:178: error: use of undeclared identifier 'interval'\n", " (((*(ostream*)0x7f8547ed3480)) << \"\\n>>>> RESULT : \" << ((*(double*)0x7f8549a1b000)) * 100 << \"% interval on \" << ((*(RooRealVar **)0x7f8524009028))->GetName() << \" is : [\" << interval->LowerLimit(*((*(RooRealVar **)0x7f8524009028))) << \", \" << interval->UpperLimit(*((*(RooRealVar **)0x7f8524009028))) << \"]\\n \" << endl)\n", " ^\n", "input_line_79:2:247: error: use of undeclared identifier 'interval'\n", " (((*(ostream*)0x7f8547ed3480)) << \"\\n>>>> RESULT : \" << ((*(double*)0x7f8549a1b000)) * 100 << \"% interval on \" << ((*(RooRealVar **)0x7f8524009028))->GetName() << \" is : [\" << interval->LowerLimit(*((*(RooRealVar **)0x7f8524009028))) << \", \" << interval->UpperLimit(*((*(RooRealVar **)0x7f8524009028))) << \"]\\n \" << endl)\n", " ^\n", "Error in : Error evaluating expression (((*(ostream*)0x7f8547ed3480)) << \"\\n>>>> RESULT : \" << ((*(double*)0x7f8549a1b000)) * 100 << \"% interval on \" << ((*(RooRealVar **)0x7f8524009028))->GetName() << \" is : [\" << interval->LowerLimit(*((*(RooRealVar **)0x7f8524009028))) << \", \" << interval->UpperLimit(*((*(RooRealVar **)0x7f8524009028))) << \"]\\n \" << endl)\n", "Execution of your code was aborted.\n" ] } ], "source": [ "RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();\n", "cout << \"\\n>>>> RESULT : \" << confLevel * 100 << \"% interval on \" << firstPOI->GetName() << \" is : [\"\n", " << interval->LowerLimit(*firstPOI) << \", \" << interval->UpperLimit(*firstPOI) << \"]\\n \" << endl;" ] }, { "cell_type": "markdown", "id": "54d9626d", "metadata": {}, "source": [ "make a plot" ] }, { "cell_type": "code", "execution_count": 11, "id": "9e6f41e6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:33.864535Z", "iopub.status.busy": "2026-05-19T20:36:33.864417Z", "iopub.status.idle": "2026-05-19T20:36:34.066862Z", "shell.execute_reply": "2026-05-19T20:36:34.066260Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_80:21:18: error: cannot deduce 'auto' from unknown expression\n", " auto result = pl.GetHypoTest();\n", " ^\n" ] } ], "source": [ "cout << \"making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the \"\n", " \"TF1 drawing option)\\n\";\n", "LikelihoodIntervalPlot plot(interval);\n", "plot.SetNPoints(nScanPoints); // do not use too many points, it could become very slow for some models\n", "if (poiXMin < poiXMax)\n", " plot.SetRange(poiXMin, poiXMax);\n", "TString opt;\n", "if (plotAsTF1)\n", " opt += TString(\"tf1\");\n", "plot.Draw(opt); // use option TF1 if too slow (plot.Draw(\"tf1\")\n", "\n", "// if requested perform also an hypothesis test for the significance\n", "if (doHypoTest) {\n", " RooArgSet nullparams(\"nullparams\");\n", " nullparams.addClone(*firstPOI);\n", " nullparams.setRealValue(firstPOI->GetName(), nullParamValue);\n", " pl.SetNullParameters(nullparams);\n", " std::cout << \"Perform Test of Hypothesis : null Hypothesis is \" << firstPOI->GetName() << nullParamValue\n", " << std::endl;\n", " auto result = pl.GetHypoTest();\n", " std::cout << \"\\n>>>> Hypotheis Test Result \\n\";\n", " result->Print();\n", "}" ] }, { "cell_type": "markdown", "id": "3786f6d1", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 12, "id": "654ba2ce", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:36:34.068295Z", "iopub.status.busy": "2026-05-19T20:36:34.068177Z", "iopub.status.idle": "2026-05-19T20:36:34.284873Z", "shell.execute_reply": "2026-05-19T20:36:34.284445Z" } }, "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 }