{ "cells": [ { "cell_type": "markdown", "id": "c70192b6", "metadata": {}, "source": [ "# rf309_ndimplot\n", "Multidimensional models: making 2/3 dimensional plots of pdfs and datasets\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:30 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "2c61a9c3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:58.133827Z", "iopub.status.busy": "2026-05-19T20:30:58.133707Z", "iopub.status.idle": "2026-05-19T20:30:58.147424Z", "shell.execute_reply": "2026-05-19T20:30:58.146867Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooProdPdf.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"TH1.h\"\n", "#include \"RooPlot.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "96a455c0", "metadata": {}, "source": [ "Create 2D model and dataset\n", "-----------------------------------------------------" ] }, { "cell_type": "markdown", "id": "1dc32cfa", "metadata": {}, "source": [ "Create observables" ] }, { "cell_type": "code", "execution_count": 2, "id": "b350cd59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:58.148891Z", "iopub.status.busy": "2026-05-19T20:30:58.148776Z", "iopub.status.idle": "2026-05-19T20:30:58.462448Z", "shell.execute_reply": "2026-05-19T20:30:58.461779Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -5, 5);\n", "RooRealVar y(\"y\", \"y\", -5, 5);" ] }, { "cell_type": "markdown", "id": "e0bc14a9", "metadata": {}, "source": [ "Create parameters" ] }, { "cell_type": "code", "execution_count": 3, "id": "cb7e5736", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:58.464522Z", "iopub.status.busy": "2026-05-19T20:30:58.464405Z", "iopub.status.idle": "2026-05-19T20:30:58.672576Z", "shell.execute_reply": "2026-05-19T20:30:58.671881Z" } }, "outputs": [], "source": [ "RooRealVar a0(\"a0\", \"a0\", -3.5, -5, 5);\n", "RooRealVar a1(\"a1\", \"a1\", -1.5, -1, 1);\n", "RooRealVar sigma(\"sigma\", \"width of gaussian\", 1.5);" ] }, { "cell_type": "markdown", "id": "4aad22db", "metadata": {}, "source": [ "Create interpreted function f(y) = a0 - a1*sqrt(10*abs(y))" ] }, { "cell_type": "code", "execution_count": 4, "id": "96211616", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:58.674420Z", "iopub.status.busy": "2026-05-19T20:30:58.674300Z", "iopub.status.idle": "2026-05-19T20:30:58.882434Z", "shell.execute_reply": "2026-05-19T20:30:58.881688Z" } }, "outputs": [], "source": [ "RooFormulaVar fy(\"fy\", \"a0-a1*sqrt(10*abs(y))\", RooArgSet(y, a0, a1));" ] }, { "cell_type": "markdown", "id": "d03d1a00", "metadata": {}, "source": [ "Create gauss(x,f(y),s)" ] }, { "cell_type": "code", "execution_count": 5, "id": "a12a6270", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:58.884224Z", "iopub.status.busy": "2026-05-19T20:30:58.884102Z", "iopub.status.idle": "2026-05-19T20:30:59.092870Z", "shell.execute_reply": "2026-05-19T20:30:59.092153Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 'sigma' with range [-inf, inf] of the RooGaussian 'model' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooGaussian model(\"model\", \"Gaussian with shifting mean\", x, fy, sigma);" ] }, { "cell_type": "markdown", "id": "cf8e9b1a", "metadata": {}, "source": [ "Sample dataset from gauss(x,y)" ] }, { "cell_type": "code", "execution_count": 6, "id": "7331d8ab", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:59.094499Z", "iopub.status.busy": "2026-05-19T20:30:59.094374Z", "iopub.status.idle": "2026-05-19T20:30:59.303252Z", "shell.execute_reply": "2026-05-19T20:30:59.302558Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_59: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, y}, 10000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{model.generate({x, y}, 10000)};" ] }, { "cell_type": "markdown", "id": "d9c646b3", "metadata": {}, "source": [ "Make 2D plots of data and model\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "df7aa9d3", "metadata": {}, "source": [ "Create and fill ROOT 2D histogram (20x20 bins) with contents of dataset" ] }, { "cell_type": "code", "execution_count": 7, "id": "4a502b66", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:59.305178Z", "iopub.status.busy": "2026-05-19T20:30:59.305057Z", "iopub.status.idle": "2026-05-19T20:30:59.514741Z", "shell.execute_reply": "2026-05-19T20:30:59.514102Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_60:2:17: error: reference to 'data' is ambiguous\n", " TH1 *hh_data = data->createHistogram(\"x,y\", Binning(20), Binning(20));\n", " ^\n", "input_line_59:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{model.generate({x, y}, 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": [ "TH1 *hh_data = data->createHistogram(\"x,y\", Binning(20), Binning(20));" ] }, { "cell_type": "markdown", "id": "773aff3e", "metadata": {}, "source": [ "Create and fill ROOT 2D histogram (50x50 bins) with sampling of pdf\n", "TH2D* hh_pdf = model.createHistogram(\"hh_model\",x,Binning(50),YVar(y,Binning(50))) ;" ] }, { "cell_type": "code", "execution_count": 8, "id": "224af53e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:59.516553Z", "iopub.status.busy": "2026-05-19T20:30:59.516423Z", "iopub.status.idle": "2026-05-19T20:30:59.726057Z", "shell.execute_reply": "2026-05-19T20:30:59.725188Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n" ] } ], "source": [ "TH1 *hh_pdf = model.createHistogram(\"x,y\", 50, 50);\n", "hh_pdf->SetLineColor(kBlue);" ] }, { "cell_type": "markdown", "id": "66296022", "metadata": {}, "source": [ "Create 3D model and dataset\n", "-----------------------------------------------------" ] }, { "cell_type": "markdown", "id": "474b9d5f", "metadata": {}, "source": [ "Create observables" ] }, { "cell_type": "code", "execution_count": 9, "id": "a6cfb06c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:59.727706Z", "iopub.status.busy": "2026-05-19T20:30:59.727560Z", "iopub.status.idle": "2026-05-19T20:30:59.937333Z", "shell.execute_reply": "2026-05-19T20:30:59.936360Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n", "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n" ] } ], "source": [ "RooRealVar z(\"z\", \"z\", -5, 5);\n", "\n", "RooGaussian gz(\"gz\", \"gz\", z, 0.0, 2.0);\n", "RooProdPdf model3(\"model3\", \"model3\", RooArgSet(model, gz));\n", "\n", "std::unique_ptr data3{model3.generate({x, y, z}, 10000)};" ] }, { "cell_type": "markdown", "id": "6d28f911", "metadata": {}, "source": [ "Make 3D plots of data and model\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "c09d2e18", "metadata": {}, "source": [ "Create and fill ROOT 2D histogram (8x8x8 bins) with contents of dataset" ] }, { "cell_type": "code", "execution_count": 10, "id": "e35ecba2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:30:59.938935Z", "iopub.status.busy": "2026-05-19T20:30:59.938810Z", "iopub.status.idle": "2026-05-19T20:31:00.147304Z", "shell.execute_reply": "2026-05-19T20:31:00.146178Z" } }, "outputs": [], "source": [ "TH1 *hh_data3 = data3->createHistogram(\"hh_data3\", x, Binning(8), YVar(y, Binning(8)), ZVar(z, Binning(8)));" ] }, { "cell_type": "markdown", "id": "b799a7db", "metadata": {}, "source": [ "Create and fill ROOT 2D histogram (20x20x20 bins) with sampling of pdf" ] }, { "cell_type": "code", "execution_count": 11, "id": "0da3e358", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:00.148960Z", "iopub.status.busy": "2026-05-19T20:31:00.148833Z", "iopub.status.idle": "2026-05-19T20:31:00.354510Z", "shell.execute_reply": "2026-05-19T20:31:00.354154Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:NumericIntegration -- RooRealIntegral::init(model_Int[x,y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_65:2:3: error: use of undeclared identifier 'hh_data'\n", " (hh_data->GetZaxis()->SetTitleOffset(1.3999999999999999))\n", " ^\n", "Error in : Error evaluating expression (hh_data->GetZaxis()->SetTitleOffset(1.3999999999999999))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "TH1 *hh_pdf3 = model3.createHistogram(\"hh_model3\", x, Binning(20), YVar(y, Binning(20)), ZVar(z, Binning(20)));\n", "hh_pdf3->SetFillColor(kBlue);\n", "\n", "TCanvas *c1 = new TCanvas(\"rf309_2dimplot\", \"rf309_2dimplot\", 800, 800);\n", "c1->Divide(2, 2);\n", "c1->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "hh_data->GetZaxis()->SetTitleOffset(1.4);\n", "hh_data->Draw(\"lego\");\n", "c1->cd(2);\n", "gPad->SetLeftMargin(0.20);\n", "hh_pdf->GetZaxis()->SetTitleOffset(2.5);\n", "hh_pdf->Draw(\"surf\");\n", "c1->cd(3);\n", "gPad->SetLeftMargin(0.15);\n", "hh_data->GetZaxis()->SetTitleOffset(1.4);\n", "hh_data->Draw(\"box\");\n", "c1->cd(4);\n", "gPad->SetLeftMargin(0.15);\n", "hh_pdf->GetZaxis()->SetTitleOffset(2.5);\n", "hh_pdf->Draw(\"cont3\");\n", "\n", "TCanvas *c2 = new TCanvas(\"rf309_3dimplot\", \"rf309_3dimplot\", 800, 400);\n", "c2->Divide(2);\n", "c2->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "hh_data3->GetZaxis()->SetTitleOffset(1.4);\n", "hh_data3->Draw(\"lego\");\n", "c2->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "hh_pdf3->GetZaxis()->SetTitleOffset(1.4);\n", "hh_pdf3->Draw(\"iso\");" ] }, { "cell_type": "markdown", "id": "cf8916c2", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 12, "id": "89fccd18", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:00.364558Z", "iopub.status.busy": "2026-05-19T20:31:00.364421Z", "iopub.status.idle": "2026-05-19T20:31:00.572494Z", "shell.execute_reply": "2026-05-19T20:31:00.571831Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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 }