{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e42232f8",
   "metadata": {},
   "source": [
    "# rf206_treevistools\n",
    "Addition and convolution: tools for visualization of ROOT.RooAbsArg expression trees\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:**  Clemens Lange, Wouter Verkerke (C++ version)  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:29 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf6bcec0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:57.465272Z",
     "iopub.status.busy": "2026-05-19T20:29:57.465119Z",
     "iopub.status.idle": "2026-05-19T20:29:58.436059Z",
     "shell.execute_reply": "2026-05-19T20:29:58.435439Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f73fc0e",
   "metadata": {},
   "source": [
    "Set up composite pdf\n",
    "--------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "547554cf",
   "metadata": {},
   "source": [
    "Declare observable x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "26a479ef",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:58.438372Z",
     "iopub.status.busy": "2026-05-19T20:29:58.438221Z",
     "iopub.status.idle": "2026-05-19T20:29:58.601894Z",
     "shell.execute_reply": "2026-05-19T20:29:58.601222Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", 0, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df7ed8d3",
   "metadata": {},
   "source": [
    "Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and\n",
    "their parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3100bdc8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:58.603951Z",
     "iopub.status.busy": "2026-05-19T20:29:58.603825Z",
     "iopub.status.idle": "2026-05-19T20:29:58.731784Z",
     "shell.execute_reply": "2026-05-19T20:29:58.731169Z"
    }
   },
   "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": [
    "mean = ROOT.RooRealVar(\"mean\", \"mean of gaussians\", 5)\n",
    "sigma1 = ROOT.RooRealVar(\"sigma1\", \"width of gaussians\", 0.5)\n",
    "sigma2 = ROOT.RooRealVar(\"sigma2\", \"width of gaussians\", 1)\n",
    "sig1 = ROOT.RooGaussian(\"sig1\", \"Signal component 1\", x, mean, sigma1)\n",
    "sig2 = ROOT.RooGaussian(\"sig2\", \"Signal component 2\", x, mean, sigma2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6d1083b",
   "metadata": {},
   "source": [
    "Sum the signal components into a composite signal pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "49e2dac4",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:58.735486Z",
     "iopub.status.busy": "2026-05-19T20:29:58.735357Z",
     "iopub.status.idle": "2026-05-19T20:29:58.915835Z",
     "shell.execute_reply": "2026-05-19T20:29:58.915208Z"
    }
   },
   "outputs": [],
   "source": [
    "sig1frac = ROOT.RooRealVar(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0.0, 1.0)\n",
    "sig = ROOT.RooAddPdf(\"sig\", \"Signal\", [sig1, sig2], [sig1frac])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64c2607c",
   "metadata": {},
   "source": [
    "Build Chebychev polynomial pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "92dfc8dd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:58.917500Z",
     "iopub.status.busy": "2026-05-19T20:29:58.917376Z",
     "iopub.status.idle": "2026-05-19T20:29:59.027724Z",
     "shell.execute_reply": "2026-05-19T20:29:59.027109Z"
    }
   },
   "outputs": [],
   "source": [
    "a0 = ROOT.RooRealVar(\"a0\", \"a0\", 0.5, 0.0, 1.0)\n",
    "a1 = ROOT.RooRealVar(\"a1\", \"a1\", -0.2, 0.0, 1.0)\n",
    "bkg1 = ROOT.RooChebychev(\"bkg1\", \"Background 1\", x, [a0, a1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0f38467",
   "metadata": {},
   "source": [
    "Build expontential pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "43a47e12",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.029620Z",
     "iopub.status.busy": "2026-05-19T20:29:59.029405Z",
     "iopub.status.idle": "2026-05-19T20:29:59.140920Z",
     "shell.execute_reply": "2026-05-19T20:29:59.140292Z"
    }
   },
   "outputs": [],
   "source": [
    "alpha = ROOT.RooRealVar(\"alpha\", \"alpha\", -1)\n",
    "bkg2 = ROOT.RooExponential(\"bkg2\", \"Background 2\", x, alpha)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4154fd4a",
   "metadata": {},
   "source": [
    "Sum the background components into a composite background pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1bac5f1c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.142788Z",
     "iopub.status.busy": "2026-05-19T20:29:59.142666Z",
     "iopub.status.idle": "2026-05-19T20:29:59.246546Z",
     "shell.execute_reply": "2026-05-19T20:29:59.245949Z"
    }
   },
   "outputs": [],
   "source": [
    "bkg1frac = ROOT.RooRealVar(\"bkg1frac\", \"fraction of component 1 in background\", 0.2, 0.0, 1.0)\n",
    "bkg = ROOT.RooAddPdf(\"bkg\", \"Signal\", [bkg1, bkg2], [bkg1frac])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8657ea63",
   "metadata": {},
   "source": [
    "Sum the composite signal and background"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7a7aecbb",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.248210Z",
     "iopub.status.busy": "2026-05-19T20:29:59.248074Z",
     "iopub.status.idle": "2026-05-19T20:29:59.351818Z",
     "shell.execute_reply": "2026-05-19T20:29:59.351173Z"
    }
   },
   "outputs": [],
   "source": [
    "bkgfrac = ROOT.RooRealVar(\"bkgfrac\", \"fraction of background\", 0.5, 0.0, 1.0)\n",
    "model = ROOT.RooAddPdf(\"model\", \"g1+g2+a\", [bkg, sig], [bkgfrac])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9de04dcc",
   "metadata": {},
   "source": [
    "Print composite tree in ASCII\n",
    "-----------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cfe00bc",
   "metadata": {},
   "source": [
    "Print tree to stdout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "95212b3e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.353496Z",
     "iopub.status.busy": "2026-05-19T20:29:59.353372Z",
     "iopub.status.idle": "2026-05-19T20:29:59.462918Z",
     "shell.execute_reply": "2026-05-19T20:29:59.462292Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0x55b4aa1b3090 RooAddPdf::model = 0.602695/1 [Auto,Clean] \n",
      "  0x55b4aa176700/V- RooAddPdf::bkg = 0.20539/1 [Auto,Clean] \n",
      "    0x55b4aa156560/V- RooChebychev::bkg1 = 1 [Auto,Dirty] \n",
      "      0x55b4a98c7000/V- RooRealVar::x = 5\n",
      "      0x55b4a9c12e20/V- RooRealVar::a0 = 0.5\n",
      "      0x55b4aa147c40/V- RooRealVar::a1 = 0\n",
      "    0x55b4a9f82b90/V- RooRealVar::bkg1frac = 0.2\n",
      "    0x55b4aa131810/V- RooExponential::bkg2 = 0.00673795 [Auto,Dirty] \n",
      "      0x55b4a98c7000/V- RooRealVar::x = 5\n",
      "      0x55b4aa03ec30/V- RooRealVar::alpha = -1\n",
      "  0x55b4aa169590/V- RooRealVar::bkgfrac = 0.5\n",
      "  0x55b4aa082770/V- RooAddPdf::sig = 1/1 [Auto,Clean] \n",
      "    0x55b4a9c29670/V- RooGaussian::sig1 = 1 [Auto,Dirty] \n",
      "      0x55b4a98c7000/V- RooRealVar::x = 5\n",
      "      0x55b4a9b46a00/V- RooRealVar::mean = 5\n",
      "      0x55b4a9b20130/V- RooRealVar::sigma1 = 0.5\n",
      "    0x55b4a9b34b70/V- RooRealVar::sig1frac = 0.8\n",
      "    0x55b4a9c2bd40/V- RooGaussian::sig2 = 1 [Auto,Dirty] \n",
      "      0x55b4a98c7000/V- RooRealVar::x = 5\n",
      "      0x55b4a9b46a00/V- RooRealVar::mean = 5\n",
      "      0x55b4a9ba72b0/V- RooRealVar::sigma2 = 1\n"
     ]
    }
   ],
   "source": [
    "model.Print(\"t\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d999ea9b",
   "metadata": {},
   "source": [
    "Print tree to file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cd54cdb",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.465549Z",
     "iopub.status.busy": "2026-05-19T20:29:59.465426Z",
     "iopub.status.idle": "2026-05-19T20:29:59.575891Z",
     "shell.execute_reply": "2026-05-19T20:29:59.575234Z"
    }
   },
   "outputs": [],
   "source": [
    "model.printCompactTree(\"\", \"rf206_asciitree.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18dc7ad7",
   "metadata": {},
   "source": [
    "Draw composite tree graphically\n",
    "-------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "889468a6",
   "metadata": {},
   "source": [
    "Print GraphViz DOT file with representation of tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "62e93763",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:29:59.577497Z",
     "iopub.status.busy": "2026-05-19T20:29:59.577374Z",
     "iopub.status.idle": "2026-05-19T20:29:59.685945Z",
     "shell.execute_reply": "2026-05-19T20:29:59.685313Z"
    }
   },
   "outputs": [],
   "source": [
    "model.graphVizTree(\"rf206_model.dot\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
