{ "cells": [ { "cell_type": "markdown", "id": "df5c688b", "metadata": {}, "source": [ "# rf407_ComputationalGraphVisualization\n", "Data and categories: Visualing computational graph model before fitting, and latex printing of lists and sets of RooArgSets after fitting\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:31 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "c74fde3c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:55.914881Z", "iopub.status.busy": "2026-05-19T20:31:55.914776Z", "iopub.status.idle": "2026-05-19T20:31:55.928744Z", "shell.execute_reply": "2026-05-19T20:31:55.928318Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"RooChebychev.h\"\n", "#include \"RooAddPdf.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": "bcff756e", "metadata": {}, "source": [ "Setup composite pdf\n", "--------------------------------------" ] }, { "cell_type": "markdown", "id": "94b28cc6", "metadata": {}, "source": [ "Declare observable x" ] }, { "cell_type": "code", "execution_count": 2, "id": "85b5e336", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:55.930180Z", "iopub.status.busy": "2026-05-19T20:31:55.930071Z", "iopub.status.idle": "2026-05-19T20:31:56.243334Z", "shell.execute_reply": "2026-05-19T20:31:56.242968Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", 0, 10);" ] }, { "cell_type": "markdown", "id": "23ca300c", "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": "a851b2d2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:56.246086Z", "iopub.status.busy": "2026-05-19T20:31:56.245972Z", "iopub.status.idle": "2026-05-19T20:31:56.454139Z", "shell.execute_reply": "2026-05-19T20:31:56.453738Z" } }, "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": "ce3e65aa", "metadata": {}, "source": [ "Sum the signal components into a composite signal pdf" ] }, { "cell_type": "code", "execution_count": 4, "id": "2d043406", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:56.455762Z", "iopub.status.busy": "2026-05-19T20:31:56.455650Z", "iopub.status.idle": "2026-05-19T20:31:56.663461Z", "shell.execute_reply": "2026-05-19T20:31:56.662963Z" } }, "outputs": [], "source": [ "RooRealVar sig1frac(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0., 1.);\n", "RooAddPdf sig(\"sig\", \"Signal\", RooArgList(sig1, sig2), sig1frac);" ] }, { "cell_type": "markdown", "id": "039f9bc0", "metadata": {}, "source": [ "Build Chebychev polynomial pdf" ] }, { "cell_type": "code", "execution_count": 5, "id": "4b90e17a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:56.665277Z", "iopub.status.busy": "2026-05-19T20:31:56.665166Z", "iopub.status.idle": "2026-05-19T20:31:56.873037Z", "shell.execute_reply": "2026-05-19T20:31:56.872543Z" } }, "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, RooArgSet(a0, a1));" ] }, { "cell_type": "markdown", "id": "1f909351", "metadata": {}, "source": [ "Build expontential pdf" ] }, { "cell_type": "code", "execution_count": 6, "id": "6161dff3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:56.875209Z", "iopub.status.busy": "2026-05-19T20:31:56.875074Z", "iopub.status.idle": "2026-05-19T20:31:57.083365Z", "shell.execute_reply": "2026-05-19T20:31:57.082798Z" } }, "outputs": [], "source": [ "RooRealVar alpha(\"alpha\", \"alpha\", -1);\n", "RooExponential bkg2(\"bkg2\", \"Background 2\", x, alpha);" ] }, { "cell_type": "markdown", "id": "af28466c", "metadata": {}, "source": [ "Sum the background components into a composite background pdf" ] }, { "cell_type": "code", "execution_count": 7, "id": "9f6ef103", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:57.085098Z", "iopub.status.busy": "2026-05-19T20:31:57.084977Z", "iopub.status.idle": "2026-05-19T20:31:57.293238Z", "shell.execute_reply": "2026-05-19T20:31:57.292489Z" } }, "outputs": [], "source": [ "RooRealVar bkg1frac(\"sig1frac\", \"fraction of component 1 in background\", 0.2, 0., 1.);\n", "RooAddPdf bkg(\"bkg\", \"Signal\", RooArgList(bkg1, bkg2), sig1frac);" ] }, { "cell_type": "markdown", "id": "95f57ae8", "metadata": {}, "source": [ "Sum the composite signal and background" ] }, { "cell_type": "code", "execution_count": 8, "id": "41e809cd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:57.295022Z", "iopub.status.busy": "2026-05-19T20:31:57.294905Z", "iopub.status.idle": "2026-05-19T20:31:57.503060Z", "shell.execute_reply": "2026-05-19T20:31:57.502593Z" } }, "outputs": [], "source": [ "RooRealVar bkgfrac(\"bkgfrac\", \"fraction of background\", 0.5, 0., 1.);\n", "RooAddPdf model(\"model\", \"g1+g2+a\", RooArgList(bkg, sig), bkgfrac);" ] }, { "cell_type": "markdown", "id": "f1f8c3ab", "metadata": {}, "source": [ "Print composite tree in ASCII\n", "-----------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "753d579e", "metadata": {}, "source": [ "Print tree to stdout" ] }, { "cell_type": "code", "execution_count": 9, "id": "018af34f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:57.507513Z", "iopub.status.busy": "2026-05-19T20:31:57.507387Z", "iopub.status.idle": "2026-05-19T20:31:57.713746Z", "shell.execute_reply": "2026-05-19T20:31:57.713360Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0x7f71522773e8 RooAddPdf::model = 0.820674/1 [Auto,Clean] \n", " 0x7f715227a3e8/V- RooAddPdf::bkg = 0.641348/1 [Auto,Clean] \n", " 0x7f71522807d0/V- RooChebychev::bkg1 = 0.8 [Auto,Dirty] \n", " 0x7f7152358000/V- RooRealVar::x = 5\n", " 0x7f7152280000/V- RooRealVar::a0 = 0.5\n", " 0x7f71522803e8/V- RooRealVar::a1 = 0.2\n", " 0x7f7152288000/V- RooRealVar::sig1frac = 0.8\n", " 0x7f715227d3e8/V- RooExponential::bkg2 = 0.00673795 [Auto,Dirty] \n", " 0x7f7152358000/V- RooRealVar::x = 5\n", " 0x7f715227d000/V- RooRealVar::alpha = -1\n", " 0x7f7152277000/V- RooRealVar::bkgfrac = 0.5\n", " 0x7f71522883e8/V- RooAddPdf::sig = 1/1 [Auto,Clean] \n", " 0x7f715228bbb8/V- RooGaussian::sig1 = 1 [Auto,Dirty] \n", " 0x7f7152358000/V- RooRealVar::x = 5\n", " 0x7f715228b000/V- RooRealVar::mean = 5\n", " 0x7f715228b3e8/V- RooRealVar::sigma1 = 0.5\n", " 0x7f7152288000/V- RooRealVar::sig1frac = 0.8\n", " 0x7f715228c110/V- RooGaussian::sig2 = 1 [Auto,Dirty] \n", " 0x7f7152358000/V- RooRealVar::x = 5\n", " 0x7f715228b000/V- RooRealVar::mean = 5\n", " 0x7f715228b7d0/V- RooRealVar::sigma2 = 1\n" ] } ], "source": [ "model.Print(\"t\");" ] }, { "cell_type": "markdown", "id": "80aeee1d", "metadata": {}, "source": [ "Print tree to file" ] }, { "cell_type": "code", "execution_count": 10, "id": "18bee0a3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:57.722042Z", "iopub.status.busy": "2026-05-19T20:31:57.721914Z", "iopub.status.idle": "2026-05-19T20:31:57.927796Z", "shell.execute_reply": "2026-05-19T20:31:57.926788Z" } }, "outputs": [], "source": [ "model.printCompactTree(\"\", \"rf206_asciitree.txt\");" ] }, { "cell_type": "markdown", "id": "98a957bb", "metadata": {}, "source": [ "Draw composite tree graphically\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "b2fa7eaa", "metadata": {}, "source": [ "Print GraphViz DOT file with representation of tree" ] }, { "cell_type": "code", "execution_count": 11, "id": "74152d04", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:57.930353Z", "iopub.status.busy": "2026-05-19T20:31:57.930230Z", "iopub.status.idle": "2026-05-19T20:31:58.136061Z", "shell.execute_reply": "2026-05-19T20:31:58.135057Z" } }, "outputs": [], "source": [ "model.graphVizTree(\"rf206_model.dot\");" ] }, { "cell_type": "markdown", "id": "249bafe8", "metadata": {}, "source": [ "Make graphic output file with one of the GraphViz tools\n", "(freely available from www.graphviz.org)\n", "\n", "'Top-to-bottom graph'\n", "unix> dot -Tgif -o rf207_model_dot.gif rf207_model.dot\n", "\n", "'Spring-model graph'\n", "unix> fdp -Tgif -o rf207_model_fdp.gif rf207_model.dot" ] }, { "cell_type": "markdown", "id": "fa5dc1cc", "metadata": {}, "source": [ "Make list of parameters before and after fit\n", "----------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "123c5364", "metadata": {}, "source": [ "Make list of model parameters" ] }, { "cell_type": "code", "execution_count": 12, "id": "7bf9a800", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:58.137686Z", "iopub.status.busy": "2026-05-19T20:31:58.137529Z", "iopub.status.idle": "2026-05-19T20:31:58.346257Z", "shell.execute_reply": "2026-05-19T20:31:58.345274Z" } }, "outputs": [], "source": [ "std::unique_ptr params{model.getParameters(x)};" ] }, { "cell_type": "markdown", "id": "ceb65f0b", "metadata": {}, "source": [ "Save snapshot of prefit parameters" ] }, { "cell_type": "code", "execution_count": 13, "id": "c2e8ff59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:58.347740Z", "iopub.status.busy": "2026-05-19T20:31:58.347595Z", "iopub.status.idle": "2026-05-19T20:31:58.556028Z", "shell.execute_reply": "2026-05-19T20:31:58.555039Z" } }, "outputs": [], "source": [ "std::unique_ptr initParams{static_cast(params->snapshot())};" ] }, { "cell_type": "markdown", "id": "63685394", "metadata": {}, "source": [ "Do fit to data, to obtain error estimates on parameters" ] }, { "cell_type": "code", "execution_count": 14, "id": "96ab15cf", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:58.557731Z", "iopub.status.busy": "2026-05-19T20:31:58.557573Z", "iopub.status.idle": "2026-05-19T20:31:58.768194Z", "shell.execute_reply": "2026-05-19T20:31:58.767460Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n", "[#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations\n", "[#1] INFO:Fitting -- Creation of NLL object took 774.354 μs\n", "[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n", "[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "input_line_61: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)};\n", "model.fitTo(*data, PrintLevel(-1));" ] }, { "cell_type": "markdown", "id": "30e19e7c", "metadata": {}, "source": [ "Print latex table of parameters of pdf\n", "--------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "17b0d16c", "metadata": {}, "source": [ "Print parameter list in LaTeX for (one column with names, one column with values)" ] }, { "cell_type": "code", "execution_count": 15, "id": "802dd25b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:58.769572Z", "iopub.status.busy": "2026-05-19T20:31:58.769435Z", "iopub.status.idle": "2026-05-19T20:31:58.979471Z", "shell.execute_reply": "2026-05-19T20:31:58.978715Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\begin{tabular}{lc}\n", "$\\verb+a0+ $ & $ 0.6\\pm 0.2$\\\\\n", "$\\verb+a1+ $ & $ 0.2\\pm 0.2$\\\\\n", "$\\verb+alpha+ $ & $ -1.00$\\\\\n", "$\\verb+bkgfrac+ $ & $ 0.45\\pm 0.03$\\\\\n", "$\\verb+mean+ $ & $ 5$\\\\\n", "$\\verb+sig1frac+ $ & $ 0.71\\pm 0.06$\\\\\n", "$\\verb+sigma1+ $ & $ 0.5$\\\\\n", "$\\verb+sigma2+ $ & $ 1$\\\\\n", "\\end{tabular}\n" ] } ], "source": [ "params->printLatex();" ] }, { "cell_type": "markdown", "id": "0cfda4cc", "metadata": {}, "source": [ "Print parameter list in LaTeX for (names values|names values)" ] }, { "cell_type": "code", "execution_count": 16, "id": "959ec3f0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:58.980836Z", "iopub.status.busy": "2026-05-19T20:31:58.980715Z", "iopub.status.idle": "2026-05-19T20:31:59.190096Z", "shell.execute_reply": "2026-05-19T20:31:59.189365Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\begin{tabular}{lc|lc}\n", "$\\verb+a0+ $ & $ 0.6\\pm 0.2$ & $\\verb+mean+ $ & $ 5$\\\\\n", "$\\verb+a1+ $ & $ 0.2\\pm 0.2$ & $\\verb+sig1frac+ $ & $ 0.71\\pm 0.06$\\\\\n", "$\\verb+alpha+ $ & $ -1.00$ & $\\verb+sigma1+ $ & $ 0.5$\\\\\n", "$\\verb+bkgfrac+ $ & $ 0.45\\pm 0.03$ & $\\verb+sigma2+ $ & $ 1$\\\\\n", "\\end{tabular}\n" ] } ], "source": [ "params->printLatex(Columns(2));" ] }, { "cell_type": "markdown", "id": "e9131226", "metadata": {}, "source": [ "Print two parameter lists side by side (name values initvalues)" ] }, { "cell_type": "code", "execution_count": 17, "id": "898d8240", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:59.191462Z", "iopub.status.busy": "2026-05-19T20:31:59.191341Z", "iopub.status.idle": "2026-05-19T20:31:59.400697Z", "shell.execute_reply": "2026-05-19T20:31:59.399952Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\begin{tabular}{lcc}\n", "$\\verb+a0+ $ & $ 0.6\\pm 0.2$ & $ 0.5$\\\\\n", "$\\verb+a1+ $ & $ 0.2\\pm 0.2$ & $ 0.2$\\\\\n", "$\\verb+alpha+ $ & $ -1.00$ & $-1.00$\\\\\n", "$\\verb+bkgfrac+ $ & $ 0.45\\pm 0.03$ & $ 0.5$\\\\\n", "$\\verb+mean+ $ & $ 5$ & $ 5$\\\\\n", "$\\verb+sig1frac+ $ & $ 0.71\\pm 0.06$ & $ 0.8$\\\\\n", "$\\verb+sigma1+ $ & $ 0.5$ & $ 0.5$\\\\\n", "$\\verb+sigma2+ $ & $ 1$ & $ 1$\\\\\n", "\\end{tabular}\n" ] } ], "source": [ "params->printLatex(Sibling(*initParams));" ] }, { "cell_type": "markdown", "id": "e15a01c9", "metadata": {}, "source": [ "Print two parameter lists side by side (name values initvalues|name values initvalues)" ] }, { "cell_type": "code", "execution_count": 18, "id": "5348733f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:59.402045Z", "iopub.status.busy": "2026-05-19T20:31:59.401926Z", "iopub.status.idle": "2026-05-19T20:31:59.610505Z", "shell.execute_reply": "2026-05-19T20:31:59.609818Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\begin{tabular}{lcc|lcc}\n", "$\\verb+a0+ $ & $ 0.6\\pm 0.2$ & $ 0.5$ & $\\verb+mean+ $ & $ 5$ & $ 5$\\\\\n", "$\\verb+a1+ $ & $ 0.2\\pm 0.2$ & $ 0.2$ & $\\verb+sig1frac+ $ & $ 0.71\\pm 0.06$ & $ 0.8$\\\\\n", "$\\verb+alpha+ $ & $ -1.00$ & $-1.00$ & $\\verb+sigma1+ $ & $ 0.5$ & $ 0.5$\\\\\n", "$\\verb+bkgfrac+ $ & $ 0.45\\pm 0.03$ & $ 0.5$ & $\\verb+sigma2+ $ & $ 1$ & $ 1$\\\\\n", "\\end{tabular}\n" ] } ], "source": [ "params->printLatex(Sibling(*initParams), Columns(2));" ] }, { "cell_type": "markdown", "id": "1bff5e99", "metadata": {}, "source": [ "Write LaTex table to file" ] }, { "cell_type": "code", "execution_count": 19, "id": "680698d0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:59.611902Z", "iopub.status.busy": "2026-05-19T20:31:59.611763Z", "iopub.status.idle": "2026-05-19T20:31:59.817473Z", "shell.execute_reply": "2026-05-19T20:31:59.816843Z" } }, "outputs": [], "source": [ "params->printLatex(Sibling(*initParams), OutputFile(\"rf407_latextables.tex\"));" ] } ], "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 }