{ "cells": [ { "cell_type": "markdown", "id": "10e21b0e", "metadata": {}, "source": [ "# rf101_basics\n", "Basic functionality: fitting, plotting, toy data generation on one-dimensional PDFs.\n", "\n", " pdf = gauss(x,m,s)\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:28 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "1e41f7b0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:19.911966Z", "iopub.status.busy": "2026-05-19T20:28:19.911855Z", "iopub.status.idle": "2026-05-19T20:28:19.924021Z", "shell.execute_reply": "2026-05-19T20:28:19.923458Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"TCanvas.h\"\n", "#include \"RooPlot.h\"\n", "#include \"TAxis.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "6ea1fe0a", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "f5edced3", "metadata": {}, "source": [ "Declare variables x,mean,sigma with associated name, title, initial value and allowed range" ] }, { "cell_type": "code", "execution_count": 2, "id": "6b08ee6e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:19.925204Z", "iopub.status.busy": "2026-05-19T20:28:19.925092Z", "iopub.status.idle": "2026-05-19T20:28:20.131930Z", "shell.execute_reply": "2026-05-19T20:28:20.131236Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);\n", "RooRealVar mean(\"mean\", \"mean of gaussian\", 1, -10, 10);\n", "RooRealVar sigma(\"sigma\", \"width of gaussian\", 1, 0.1, 10);" ] }, { "cell_type": "markdown", "id": "27933c3c", "metadata": {}, "source": [ "Build gaussian pdf in terms of x,mean and sigma" ] }, { "cell_type": "code", "execution_count": 3, "id": "b330e9d2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:20.133529Z", "iopub.status.busy": "2026-05-19T20:28:20.133414Z", "iopub.status.idle": "2026-05-19T20:28:20.340613Z", "shell.execute_reply": "2026-05-19T20:28:20.339970Z" } }, "outputs": [], "source": [ "RooGaussian gauss(\"gauss\", \"gaussian PDF\", x, mean, sigma);" ] }, { "cell_type": "markdown", "id": "4cb0f6a0", "metadata": {}, "source": [ "Construct plot frame in 'x'" ] }, { "cell_type": "code", "execution_count": 4, "id": "ab66d86d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:20.342113Z", "iopub.status.busy": "2026-05-19T20:28:20.341999Z", "iopub.status.idle": "2026-05-19T20:28:20.546938Z", "shell.execute_reply": "2026-05-19T20:28:20.546247Z" } }, "outputs": [], "source": [ "RooPlot *xframe = x.frame(Title(\"Gaussian pdf.\"));" ] }, { "cell_type": "markdown", "id": "580d08b8", "metadata": {}, "source": [ "Plot model and change parameter values\n", "---------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "7e6b7043", "metadata": {}, "source": [ "Plot gauss in frame (i.e. in x)" ] }, { "cell_type": "code", "execution_count": 5, "id": "3e034135", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:20.548808Z", "iopub.status.busy": "2026-05-19T20:28:20.548690Z", "iopub.status.idle": "2026-05-19T20:28:20.755860Z", "shell.execute_reply": "2026-05-19T20:28:20.755218Z" } }, "outputs": [], "source": [ "gauss.plotOn(xframe);" ] }, { "cell_type": "markdown", "id": "9d92decc", "metadata": {}, "source": [ "Change the value of sigma to 3" ] }, { "cell_type": "code", "execution_count": 6, "id": "2f6069a1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:20.757715Z", "iopub.status.busy": "2026-05-19T20:28:20.757575Z", "iopub.status.idle": "2026-05-19T20:28:20.964844Z", "shell.execute_reply": "2026-05-19T20:28:20.964295Z" } }, "outputs": [], "source": [ "sigma.setVal(3);" ] }, { "cell_type": "markdown", "id": "f8a70c5a", "metadata": {}, "source": [ "Plot gauss in frame (i.e. in x) and draw frame on canvas" ] }, { "cell_type": "code", "execution_count": 7, "id": "bdf0d5e8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:20.974026Z", "iopub.status.busy": "2026-05-19T20:28:20.973892Z", "iopub.status.idle": "2026-05-19T20:28:21.181256Z", "shell.execute_reply": "2026-05-19T20:28:21.180669Z" } }, "outputs": [], "source": [ "gauss.plotOn(xframe, LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "8ebeb529", "metadata": {}, "source": [ "Generate events\n", "-----------------------------" ] }, { "cell_type": "markdown", "id": "f90c37b8", "metadata": {}, "source": [ "Generate a dataset of 1000 events in x from gauss" ] }, { "cell_type": "code", "execution_count": 8, "id": "27de3095", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:21.182957Z", "iopub.status.busy": "2026-05-19T20:28:21.182837Z", "iopub.status.idle": "2026-05-19T20:28:21.390155Z", "shell.execute_reply": "2026-05-19T20:28:21.389705Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55: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{gauss.generate(x, 10000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{gauss.generate(x, 10000)};" ] }, { "cell_type": "markdown", "id": "23fcd632", "metadata": {}, "source": [ "Make a second plot frame in x and draw both the\n", "data and the pdf in the frame" ] }, { "cell_type": "code", "execution_count": 9, "id": "2d70ff70", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:21.391567Z", "iopub.status.busy": "2026-05-19T20:28:21.391451Z", "iopub.status.idle": "2026-05-19T20:28:21.599221Z", "shell.execute_reply": "2026-05-19T20:28:21.598773Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(xframe2);\n", "^\n", "input_line_55:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{gauss.generate(x, 10000)};\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": [ "RooPlot *xframe2 = x.frame(Title(\"Gaussian pdf with data\"));\n", "data->plotOn(xframe2);\n", "gauss.plotOn(xframe2);" ] }, { "cell_type": "markdown", "id": "f3da7017", "metadata": {}, "source": [ "Fit model to data\n", "-----------------------------" ] }, { "cell_type": "markdown", "id": "b5537d0c", "metadata": {}, "source": [ "Fit pdf to data" ] }, { "cell_type": "code", "execution_count": 10, "id": "6004edda", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:21.600621Z", "iopub.status.busy": "2026-05-19T20:28:21.600487Z", "iopub.status.idle": "2026-05-19T20:28:21.808566Z", "shell.execute_reply": "2026-05-19T20:28:21.807962Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_57:2:15: error: reference to 'data' is ambiguous\n", " gauss.fitTo(*data, PrintLevel(-1));\n", " ^\n", "input_line_55:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{gauss.generate(x, 10000)};\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": [ "gauss.fitTo(*data, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "508376ca", "metadata": {}, "source": [ "Print values of mean and sigma (that now reflect fitted values and errors)" ] }, { "cell_type": "code", "execution_count": 11, "id": "09ebc080", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:21.809899Z", "iopub.status.busy": "2026-05-19T20:28:21.809782Z", "iopub.status.idle": "2026-05-19T20:28:22.016986Z", "shell.execute_reply": "2026-05-19T20:28:22.016570Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RooRealVar::mean = 1 L(-10 - 10) \n", "RooRealVar::sigma = 3 L(0.1 - 10) \n" ] } ], "source": [ "mean.Print();\n", "sigma.Print();" ] }, { "cell_type": "markdown", "id": "ef761cdb", "metadata": {}, "source": [ "Draw all frames on a canvas" ] }, { "cell_type": "code", "execution_count": 12, "id": "1c21c581", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:22.018689Z", "iopub.status.busy": "2026-05-19T20:28:22.018557Z", "iopub.status.idle": "2026-05-19T20:28:22.226119Z", "shell.execute_reply": "2026-05-19T20:28:22.225624Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_60:2:3: error: use of undeclared identifier 'xframe2'\n", " (xframe2->GetYaxis()->SetTitleOffset(1.6000000000000001))\n", " ^\n", "Error in : Error evaluating expression (xframe2->GetYaxis()->SetTitleOffset(1.6000000000000001))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "TCanvas *c = new TCanvas(\"rf101_basics\", \"rf101_basics\", 800, 400); c->Divide(2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "xframe->GetYaxis()->SetTitleOffset(1.6);\n", "xframe->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "xframe2->GetYaxis()->SetTitleOffset(1.6);\n", "xframe2->Draw();" ] }, { "cell_type": "markdown", "id": "0c26b422", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 13, "id": "d19759d6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:22.227461Z", "iopub.status.busy": "2026-05-19T20:28:22.227344Z", "iopub.status.idle": "2026-05-19T20:28:22.453895Z", "shell.execute_reply": "2026-05-19T20:28:22.453380Z" } }, "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 }