{ "cells": [ { "cell_type": "markdown", "id": "2c08ae7d", "metadata": {}, "source": [ "# rf106_plotdecoration\n", "Basic functionality: adding boxes with parameters, statistics to RooPlots, decorating with arrows, text etc...\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": "c9ffed12", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:00.209176Z", "iopub.status.busy": "2026-05-19T20:29:00.209061Z", "iopub.status.idle": "2026-05-19T20:29:00.222230Z", "shell.execute_reply": "2026-05-19T20:29:00.221596Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "#include \"TText.h\"\n", "#include \"TArrow.h\"\n", "#include \"TFile.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "33523fed", "metadata": {}, "source": [ "Setup model\n", "---------------------" ] }, { "cell_type": "markdown", "id": "1bbb6424", "metadata": {}, "source": [ "Create observables" ] }, { "cell_type": "code", "execution_count": 2, "id": "2d1b6d9b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:00.223725Z", "iopub.status.busy": "2026-05-19T20:29:00.223572Z", "iopub.status.idle": "2026-05-19T20:29:00.538748Z", "shell.execute_reply": "2026-05-19T20:29:00.538300Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);" ] }, { "cell_type": "markdown", "id": "6c7e5835", "metadata": {}, "source": [ "Create Gaussian" ] }, { "cell_type": "code", "execution_count": 3, "id": "5fa3f495", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:00.547893Z", "iopub.status.busy": "2026-05-19T20:29:00.547763Z", "iopub.status.idle": "2026-05-19T20:29:00.755153Z", "shell.execute_reply": "2026-05-19T20:29:00.754735Z" } }, "outputs": [], "source": [ "RooRealVar sigma(\"sigma\", \"sigma\", 1, 0.1, 10);\n", "RooRealVar mean(\"mean\", \"mean\", -3, -10, 10);\n", "RooGaussian gauss(\"gauss\", \"gauss\", x, mean, sigma);" ] }, { "cell_type": "markdown", "id": "d4d73590", "metadata": {}, "source": [ "Generate a sample of 1000 events with sigma=3" ] }, { "cell_type": "code", "execution_count": 4, "id": "4351099b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:00.764249Z", "iopub.status.busy": "2026-05-19T20:29:00.764120Z", "iopub.status.idle": "2026-05-19T20:29:00.973493Z", "shell.execute_reply": "2026-05-19T20:29:00.972914Z" } }, "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{gauss.generate(x, 1000)};\n", " ^\n" ] } ], "source": [ "std::unique_ptr data{gauss.generate(x, 1000)};" ] }, { "cell_type": "markdown", "id": "1b3ab5ba", "metadata": {}, "source": [ "Fit pdf to data" ] }, { "cell_type": "code", "execution_count": 5, "id": "6e018e95", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:00.974793Z", "iopub.status.busy": "2026-05-19T20:29:00.974670Z", "iopub.status.idle": "2026-05-19T20:29:01.183117Z", "shell.execute_reply": "2026-05-19T20:29:01.182513Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_52:2:15: error: reference to 'data' is ambiguous\n", " gauss.fitTo(*data, PrintLevel(-1));\n", " ^\n", "input_line_51:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{gauss.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": [ "gauss.fitTo(*data, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "8dae4b04", "metadata": {}, "source": [ "Plot p.d.f and data\n", "-------------------------------------" ] }, { "cell_type": "markdown", "id": "2795da91", "metadata": {}, "source": [ "Overlay projection of gauss on data" ] }, { "cell_type": "code", "execution_count": 6, "id": "9e84cb2d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:01.184414Z", "iopub.status.busy": "2026-05-19T20:29:01.184299Z", "iopub.status.idle": "2026-05-19T20:29:01.392560Z", "shell.execute_reply": "2026-05-19T20:29:01.391985Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(frame);\n", "^\n", "input_line_51:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{gauss.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": [ "RooPlot *frame = x.frame(Name(\"xframe\"), Title(\"RooPlot with decorations\"), Bins(40));\n", "data->plotOn(frame);\n", "gauss.plotOn(frame);" ] }, { "cell_type": "markdown", "id": "d2d9799a", "metadata": {}, "source": [ "Add box with pdf parameters\n", "-----------------------------------------------------" ] }, { "cell_type": "markdown", "id": "8cbeac7d", "metadata": {}, "source": [ "Left edge of box starts at 55% of Xaxis)" ] }, { "cell_type": "code", "execution_count": 7, "id": "93c5120f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:01.393826Z", "iopub.status.busy": "2026-05-19T20:29:01.393712Z", "iopub.status.idle": "2026-05-19T20:29:01.601390Z", "shell.execute_reply": "2026-05-19T20:29:01.600826Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:23: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)(double, double, double)'\n", " gauss.paramOn(frame, Layout(0.55));\n", " ^~~~~~\n" ] } ], "source": [ "gauss.paramOn(frame, Layout(0.55));" ] }, { "cell_type": "markdown", "id": "e9afcf50", "metadata": {}, "source": [ "Add box with data statistics\n", "-------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "859e8035", "metadata": {}, "source": [ "X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)" ] }, { "cell_type": "code", "execution_count": 8, "id": "741ebf9d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:01.602674Z", "iopub.status.busy": "2026-05-19T20:29:01.602540Z", "iopub.status.idle": "2026-05-19T20:29:01.812956Z", "shell.execute_reply": "2026-05-19T20:29:01.812377Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:2: error: reference to 'data' is ambiguous\n", " data->statOn(frame, Layout(0.55, 0.99, 0.8));\n", " ^\n", "input_line_51:2:30: note: candidate found by name lookup is 'data'\n", " std::unique_ptr data{gauss.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": [ "data->statOn(frame, Layout(0.55, 0.99, 0.8));" ] }, { "cell_type": "markdown", "id": "d0eda60a", "metadata": {}, "source": [ "Add text and arrow\n", "-----------------------------------" ] }, { "cell_type": "markdown", "id": "aa0bffc6", "metadata": {}, "source": [ "Add text to frame" ] }, { "cell_type": "code", "execution_count": 9, "id": "91aeda06", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:01.814324Z", "iopub.status.busy": "2026-05-19T20:29:01.814203Z", "iopub.status.idle": "2026-05-19T20:29:02.146229Z", "shell.execute_reply": "2026-05-19T20:29:02.145710Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN7TObjectnwEm, $.cling-module-277.__inits.0, _GLOBAL__sub_I_cling_module_277, _ZN12__cling_N5263txtE, _ZN12__cling_N52616__cling_Un1Qu325EPv, _ZN5cling7runtime8internal15DynamicExprInfoC2EPKcPPvb, __orc_init_func.cling-module-277, cling_module_277_, _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }\n", "IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n", "You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n", "Maybe you need to load the corresponding shared library?\n" ] } ], "source": [ "TText *txt = new TText(2, 100, \"Signal\");\n", "txt->SetTextSize(0.04);\n", "txt->SetTextColor(kRed);\n", "frame->addObject(txt);" ] }, { "cell_type": "markdown", "id": "2e1278d1", "metadata": {}, "source": [ "Add arrow to frame" ] }, { "cell_type": "code", "execution_count": 10, "id": "13a2973c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:02.147413Z", "iopub.status.busy": "2026-05-19T20:29:02.147296Z", "iopub.status.idle": "2026-05-19T20:29:02.354956Z", "shell.execute_reply": "2026-05-19T20:29:02.354464Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cling JIT session error: Failed to materialize symbols: { (main, { _ZN7TObjectnwEm }) }\n", "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-278, cling_module_278_, _GLOBAL__sub_I_cling_module_278, $.cling-module-278.__inits.0, _ZN12__cling_N5275arrowE, _ZN12__cling_N52716__cling_Un1Qu326EPv }) }\n" ] } ], "source": [ "TArrow *arrow = new TArrow(2, 100, -1, 50, 0.01, \"|>\");\n", "arrow->SetLineColor(kRed);\n", "arrow->SetFillColor(kRed);\n", "arrow->SetLineWidth(3);\n", "frame->addObject(arrow);" ] }, { "cell_type": "markdown", "id": "65e570c9", "metadata": {}, "source": [ "Persist frame with all decorations in ROOT file\n", "---------------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 11, "id": "83c51529", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:02.356155Z", "iopub.status.busy": "2026-05-19T20:29:02.356039Z", "iopub.status.idle": "2026-05-19T20:29:02.563657Z", "shell.execute_reply": "2026-05-19T20:29:02.563192Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) }\n", "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _GLOBAL__sub_I_cling_module_279, $.cling-module-279.__inits.0, _ZN12__cling_N5281fE, _ZN12__cling_N52816__cling_Un1Qu327EPv, cling_module_279_, __orc_init_func.cling-module-279 }) }\n" ] } ], "source": [ "TFile f(\"rf106_plotdecoration.root\", \"RECREATE\");\n", "frame->Write();\n", "f.Close();" ] }, { "cell_type": "markdown", "id": "a2cfa99d", "metadata": {}, "source": [ "To read back and plot frame with all decorations in clean root session do\n", "root> TFile f(\"rf106_plotdecoration.root\") ;\n", "root> xframe->Draw() ;" ] }, { "cell_type": "code", "execution_count": 12, "id": "e535164a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:02.565054Z", "iopub.status.busy": "2026-05-19T20:29:02.564929Z", "iopub.status.idle": "2026-05-19T20:29:02.772197Z", "shell.execute_reply": "2026-05-19T20:29:02.771743Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-277 }) }\n", "cling JIT session error: Failed to materialize symbols: { (main, { _ZN7TObjectnwEm }) }\n" ] } ], "source": [ "new TCanvas(\"rf106_plotdecoration\", \"rf106_plotdecoration\", 600, 600);\n", "gPad->SetLeftMargin(0.15);\n", "frame->GetYaxis()->SetTitleOffset(1.6);\n", "frame->Draw();" ] }, { "cell_type": "markdown", "id": "e7bb38f0", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 13, "id": "1d7e9681", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:02.773394Z", "iopub.status.busy": "2026-05-19T20:29:02.773284Z", "iopub.status.idle": "2026-05-19T20:29:03.000682Z", "shell.execute_reply": "2026-05-19T20:29:03.000087Z" } }, "outputs": [], "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 }