{ "cells": [ { "cell_type": "markdown", "id": "4976a1bc", "metadata": {}, "source": [ "# rf205_compplot\n", "Addition and convolution: options for plotting components of composite pdfs.\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:29 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "222e0cdd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:54.138040Z", "iopub.status.busy": "2026-05-19T20:29:54.137903Z", "iopub.status.idle": "2026-05-19T20:29:54.152375Z", "shell.execute_reply": "2026-05-19T20:29:54.151742Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooAddPdf.h\"\n", "#include \"RooChebychev.h\"\n", "#include \"RooExponential.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "using namespace RooFit;" ] }, { "cell_type": "markdown", "id": "824aae51", "metadata": {}, "source": [ "Setup composite pdf\n", "--------------------------------------" ] }, { "cell_type": "markdown", "id": "5f1f520d", "metadata": {}, "source": [ "Declare observable x" ] }, { "cell_type": "code", "execution_count": 2, "id": "6565836a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:54.153934Z", "iopub.status.busy": "2026-05-19T20:29:54.153817Z", "iopub.status.idle": "2026-05-19T20:29:54.469323Z", "shell.execute_reply": "2026-05-19T20:29:54.468591Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", 0, 10);" ] }, { "cell_type": "markdown", "id": "c4396567", "metadata": {}, "source": [ "Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters" ] }, { "cell_type": "code", "execution_count": 3, "id": "0b43b23c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:54.471056Z", "iopub.status.busy": "2026-05-19T20:29:54.470935Z", "iopub.status.idle": "2026-05-19T20:29:54.679228Z", "shell.execute_reply": "2026-05-19T20:29:54.678631Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-inf, inf] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.\n", "[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-inf, inf] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.\n" ] } ], "source": [ "RooRealVar mean(\"mean\", \"mean of gaussians\", 5);\n", "RooRealVar sigma1(\"sigma1\", \"width of gaussians\", 0.5);\n", "RooRealVar sigma2(\"sigma2\", \"width of gaussians\", 1);\n", "RooGaussian sig1(\"sig1\", \"Signal component 1\", x, mean, sigma1);\n", "RooGaussian sig2(\"sig2\", \"Signal component 2\", x, mean, sigma2);" ] }, { "cell_type": "markdown", "id": "f4301374", "metadata": {}, "source": [ "Sum the signal components into a composite signal pdf" ] }, { "cell_type": "code", "execution_count": 4, "id": "4fdcd2cc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:54.680907Z", "iopub.status.busy": "2026-05-19T20:29:54.680787Z", "iopub.status.idle": "2026-05-19T20:29:54.888618Z", "shell.execute_reply": "2026-05-19T20:29:54.887914Z" } }, "outputs": [], "source": [ "RooRealVar sig1frac(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0., 1.);\n", "RooAddPdf sig(\"sig\", \"Signal\", {sig1, sig2}, sig1frac);" ] }, { "cell_type": "markdown", "id": "56c5d557", "metadata": {}, "source": [ "Build Chebychev polynomial pdf" ] }, { "cell_type": "code", "execution_count": 5, "id": "10529aed", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:54.890634Z", "iopub.status.busy": "2026-05-19T20:29:54.890495Z", "iopub.status.idle": "2026-05-19T20:29:55.107228Z", "shell.execute_reply": "2026-05-19T20:29:55.106450Z" } }, "outputs": [], "source": [ "RooRealVar a0(\"a0\", \"a0\", 0.5, 0., 1.);\n", "RooRealVar a1(\"a1\", \"a1\", 0.2, 0., 1.);\n", "RooChebychev bkg1(\"bkg1\", \"Background 1\", x, {a0, a1});" ] }, { "cell_type": "markdown", "id": "736afe99", "metadata": {}, "source": [ "Build exponential pdf" ] }, { "cell_type": "code", "execution_count": 6, "id": "430b9e9d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:55.109020Z", "iopub.status.busy": "2026-05-19T20:29:55.108893Z", "iopub.status.idle": "2026-05-19T20:29:55.316522Z", "shell.execute_reply": "2026-05-19T20:29:55.315883Z" } }, "outputs": [], "source": [ "RooRealVar alpha(\"alpha\", \"alpha\", -1);\n", "RooExponential bkg2(\"bkg2\", \"Background 2\", x, alpha);" ] }, { "cell_type": "markdown", "id": "e6fcbb81", "metadata": {}, "source": [ "Sum the background components into a composite background pdf" ] }, { "cell_type": "code", "execution_count": 7, "id": "e5b36cdf", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:55.321362Z", "iopub.status.busy": "2026-05-19T20:29:55.321238Z", "iopub.status.idle": "2026-05-19T20:29:55.528757Z", "shell.execute_reply": "2026-05-19T20:29:55.528133Z" } }, "outputs": [], "source": [ "RooRealVar bkg1frac(\"bkg1frac\", \"fraction of component 1 in background\", 0.8, 0., 1.);\n", "RooAddPdf bkg(\"bkg\", \"Total background\", {bkg1, bkg2}, bkg1frac);" ] }, { "cell_type": "markdown", "id": "ca2ba7b6", "metadata": {}, "source": [ "Sum the composite signal and background" ] }, { "cell_type": "code", "execution_count": 8, "id": "e2b609ee", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:55.530761Z", "iopub.status.busy": "2026-05-19T20:29:55.530644Z", "iopub.status.idle": "2026-05-19T20:29:55.738273Z", "shell.execute_reply": "2026-05-19T20:29:55.737640Z" } }, "outputs": [], "source": [ "RooRealVar bkgfrac(\"bkgfrac\", \"fraction of background\", 0.5, 0., 1.);\n", "RooAddPdf model(\"model\", \"g1+g2+a\", {bkg, sig}, bkgfrac);" ] }, { "cell_type": "markdown", "id": "681f4a09", "metadata": {}, "source": [ "Setup basic plot with data and full pdf\n", "------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "e70b071b", "metadata": {}, "source": [ "Generate a data sample of 1000 events in x from model" ] }, { "cell_type": "code", "execution_count": 9, "id": "d1986846", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:55.740189Z", "iopub.status.busy": "2026-05-19T20:29:55.740074Z", "iopub.status.idle": "2026-05-19T20:29:55.947880Z", "shell.execute_reply": "2026-05-19T20:29:55.947339Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56: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": "6b75ac4c", "metadata": {}, "source": [ "Plot data and complete PDF overlaid" ] }, { "cell_type": "code", "execution_count": 10, "id": "5082a5cf", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:55.949601Z", "iopub.status.busy": "2026-05-19T20:29:55.949482Z", "iopub.status.idle": "2026-05-19T20:29:56.157717Z", "shell.execute_reply": "2026-05-19T20:29:56.157323Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_57:3:1: error: reference to 'data' is ambiguous\n", "data->plotOn(xframe);\n", "^\n", "input_line_56: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": [ "RooPlot *xframe = x.frame(Title(\"Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)\"));\n", "data->plotOn(xframe);\n", "model.plotOn(xframe);" ] }, { "cell_type": "markdown", "id": "6823c382", "metadata": {}, "source": [ "Clone xframe for use below" ] }, { "cell_type": "code", "execution_count": 11, "id": "ca64e1f7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:56.160193Z", "iopub.status.busy": "2026-05-19T20:29:56.160072Z", "iopub.status.idle": "2026-05-19T20:29:56.365284Z", "shell.execute_reply": "2026-05-19T20:29:56.364853Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_59:2:14: error: use of undeclared identifier 'xframe'\n", " ((RooPlot *)xframe->Clone(\"xframe2\"))\n", " ^\n", "Error in : Error evaluating expression ((RooPlot *)xframe->Clone(\"xframe2\"))\n", "Execution of your code was aborted.\n" ] } ], "source": [ "RooPlot *xframe2 = (RooPlot *)xframe->Clone(\"xframe2\");" ] }, { "cell_type": "markdown", "id": "ff26ea16", "metadata": {}, "source": [ "Make component by object reference\n", "--------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "1cdd8e69", "metadata": {}, "source": [ "Plot single background component specified by object reference" ] }, { "cell_type": "code", "execution_count": 12, "id": "aaebbf3d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:56.367169Z", "iopub.status.busy": "2026-05-19T20:29:56.367056Z", "iopub.status.idle": "2026-05-19T20:29:56.574892Z", "shell.execute_reply": "2026-05-19T20:29:56.574309Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_60:2:50: error: cannot take the address of an rvalue of type 'EColor'\n", " model.plotOn(xframe, Components(bkg), LineColor(kRed));\n", " ^~~~\n", "Error while creating dynamic expression for:\n", " model.plotOn(xframe, Components(bkg), LineColor(kRed))\n" ] } ], "source": [ "model.plotOn(xframe, Components(bkg), LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "1ad98f58", "metadata": {}, "source": [ "Plot single background component specified by object reference" ] }, { "cell_type": "code", "execution_count": 13, "id": "cf2b40ce", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:56.576635Z", "iopub.status.busy": "2026-05-19T20:29:56.576494Z", "iopub.status.idle": "2026-05-19T20:29:56.784198Z", "shell.execute_reply": "2026-05-19T20:29:56.783905Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_61:2:51: error: cannot take the address of an rvalue of type 'ELineStyle'\n", " model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed));\n", " ^~~~~~~\n", "Error while creating dynamic expression for:\n", " model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed))\n" ] } ], "source": [ "model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed));" ] }, { "cell_type": "markdown", "id": "59b84322", "metadata": {}, "source": [ "Plot multiple background components specified by object reference\n", "Note that specified components may occur at any level in object tree\n", "(e.g bkg is component of 'model' and 'sig2' is component 'sig')" ] }, { "cell_type": "code", "execution_count": 14, "id": "16136940", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:56.786347Z", "iopub.status.busy": "2026-05-19T20:29:56.786236Z", "iopub.status.idle": "2026-05-19T20:29:56.994047Z", "shell.execute_reply": "2026-05-19T20:29:56.993535Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_62:2:56: error: cannot take the address of an rvalue of type 'ELineStyle'\n", " model.plotOn(xframe, Components(bkg, sig2), LineStyle(kDotted));\n", " ^~~~~~~\n", "Error while creating dynamic expression for:\n", " model.plotOn(xframe, Components(bkg, sig2), LineStyle(kDotted))\n" ] } ], "source": [ "model.plotOn(xframe, Components(bkg, sig2), LineStyle(kDotted));" ] }, { "cell_type": "markdown", "id": "8fc30806", "metadata": {}, "source": [ "Make component by name / regexp\n", "------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "8d49db98", "metadata": {}, "source": [ "Plot single background component specified by name" ] }, { "cell_type": "code", "execution_count": 15, "id": "854f45ac", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:56.995928Z", "iopub.status.busy": "2026-05-19T20:29:56.995816Z", "iopub.status.idle": "2026-05-19T20:29:57.203388Z", "shell.execute_reply": "2026-05-19T20:29:57.202824Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:Plotting -- RooAddPdf::model:plotOn: frame is null\n" ] } ], "source": [ "model.plotOn(xframe2, Components(\"bkg\"), LineColor(kCyan));" ] }, { "cell_type": "markdown", "id": "f0f03f58", "metadata": {}, "source": [ "Plot multiple background components specified by name" ] }, { "cell_type": "code", "execution_count": 16, "id": "a02bcd35", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:57.205206Z", "iopub.status.busy": "2026-05-19T20:29:57.205049Z", "iopub.status.idle": "2026-05-19T20:29:57.412902Z", "shell.execute_reply": "2026-05-19T20:29:57.412346Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:Plotting -- RooAddPdf::model:plotOn: frame is null\n" ] } ], "source": [ "model.plotOn(xframe2, Components(\"bkg1,sig2\"), LineStyle(kDotted), LineColor(kCyan));" ] }, { "cell_type": "markdown", "id": "349689df", "metadata": {}, "source": [ "Plot multiple background components specified by regular expression on name" ] }, { "cell_type": "code", "execution_count": 17, "id": "b7f3f009", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:57.427324Z", "iopub.status.busy": "2026-05-19T20:29:57.427195Z", "iopub.status.idle": "2026-05-19T20:29:57.635003Z", "shell.execute_reply": "2026-05-19T20:29:57.634452Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:Plotting -- RooAddPdf::model:plotOn: frame is null\n" ] } ], "source": [ "model.plotOn(xframe2, Components(\"sig*\"), LineStyle(kDashed), LineColor(kCyan));" ] }, { "cell_type": "markdown", "id": "52df1912", "metadata": {}, "source": [ "Plot multiple background components specified by multiple regular expressions on name" ] }, { "cell_type": "code", "execution_count": 18, "id": "12126cb8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:57.636807Z", "iopub.status.busy": "2026-05-19T20:29:57.636674Z", "iopub.status.idle": "2026-05-19T20:29:57.844565Z", "shell.execute_reply": "2026-05-19T20:29:57.844037Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] ERROR:Plotting -- RooAddPdf::model:plotOn: frame is null\n" ] } ], "source": [ "model.plotOn(xframe2, Components(\"bkg1,sig*\"), LineStyle(kDashed), LineColor(kYellow), Invisible());" ] }, { "cell_type": "markdown", "id": "456869c0", "metadata": {}, "source": [ "Draw the frame on the canvas" ] }, { "cell_type": "code", "execution_count": 19, "id": "38ac78a0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:29:57.846347Z", "iopub.status.busy": "2026-05-19T20:29:57.846228Z", "iopub.status.idle": "2026-05-19T20:29:58.090420Z", "shell.execute_reply": "2026-05-19T20:29:58.089962Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N53616__cling_Un1Qu336EPv, cling_module_288_, __orc_init_func.cling-module-288, $.cling-module-288.__inits.0, _GLOBAL__sub_I_cling_module_288, _ZN12__cling_N5361cE, _ZN7TObjectnwEm }) }\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": [ "TCanvas *c = new TCanvas(\"rf205_compplot\", \"rf205_compplot\", 800, 400);\n", "c->Divide(2);\n", "c->cd(1);\n", "gPad->SetLeftMargin(0.15);\n", "xframe->GetYaxis()->SetTitleOffset(1.4);\n", "xframe->Draw();\n", "c->cd(2);\n", "gPad->SetLeftMargin(0.15);\n", "xframe2->GetYaxis()->SetTitleOffset(1.4);\n", "xframe2->Draw();\n", "c->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 }