{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49570e5e",
   "metadata": {},
   "source": [
    "# rf502_wspacewrite\n",
    "Organization and simultaneous fits: creating and writing a workspace\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:05 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "303aea90",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:05:58.911813Z",
     "iopub.status.busy": "2026-05-19T20:05:58.911668Z",
     "iopub.status.idle": "2026-05-19T20:06:00.123493Z",
     "shell.execute_reply": "2026-05-19T20:06:00.107435Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93c89a95",
   "metadata": {},
   "source": [
    "Create model and dataset\n",
    "-----------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc7b1808",
   "metadata": {},
   "source": [
    "Declare observable x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75ab462b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:00.134874Z",
     "iopub.status.busy": "2026-05-19T20:06:00.134704Z",
     "iopub.status.idle": "2026-05-19T20:06:00.355452Z",
     "shell.execute_reply": "2026-05-19T20:06:00.343701Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", 0, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8ab2697",
   "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": "b0db7829",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:00.370980Z",
     "iopub.status.busy": "2026-05-19T20:06:00.370821Z",
     "iopub.status.idle": "2026-05-19T20:06:00.555777Z",
     "shell.execute_reply": "2026-05-19T20:06:00.538006Z"
    }
   },
   "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, 0, 10)\n",
    "sigma1 = ROOT.RooRealVar(\"sigma1\", \"width of gaussians\", 0.5)\n",
    "sigma2 = ROOT.RooRealVar(\"sigma2\", \"width of gaussians\", 1)\n",
    "\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": "4f5e8dba",
   "metadata": {},
   "source": [
    "Build Chebychev polynomial pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bdffc4c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:00.592214Z",
     "iopub.status.busy": "2026-05-19T20:06:00.592039Z",
     "iopub.status.idle": "2026-05-19T20:06:00.836248Z",
     "shell.execute_reply": "2026-05-19T20:06:00.830747Z"
    }
   },
   "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",
    "bkg = ROOT.RooChebychev(\"bkg\", \"Background\", x, [a0, a1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c19659a5",
   "metadata": {},
   "source": [
    "Sum the signal components into a composite signal pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "63a077e1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:00.860902Z",
     "iopub.status.busy": "2026-05-19T20:06:00.860744Z",
     "iopub.status.idle": "2026-05-19T20:06:01.026413Z",
     "shell.execute_reply": "2026-05-19T20:06:01.004659Z"
    }
   },
   "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": "522a00fb",
   "metadata": {},
   "source": [
    "Sum the composite signal and background"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f99b12c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.032845Z",
     "iopub.status.busy": "2026-05-19T20:06:01.032683Z",
     "iopub.status.idle": "2026-05-19T20:06:01.147994Z",
     "shell.execute_reply": "2026-05-19T20:06:01.139700Z"
    }
   },
   "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": "f029be7c",
   "metadata": {},
   "source": [
    "Generate a data sample of 1000 events in x from model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "50197840",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.161888Z",
     "iopub.status.busy": "2026-05-19T20:06:01.161721Z",
     "iopub.status.idle": "2026-05-19T20:06:01.333993Z",
     "shell.execute_reply": "2026-05-19T20:06:01.332674Z"
    }
   },
   "outputs": [],
   "source": [
    "data = model.generate({x}, 1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bc567c5",
   "metadata": {},
   "source": [
    "Create workspace, import data and model\n",
    "-----------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "964ed1ab",
   "metadata": {},
   "source": [
    "Create a empty workspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a72a321f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.351316Z",
     "iopub.status.busy": "2026-05-19T20:06:01.351165Z",
     "iopub.status.idle": "2026-05-19T20:06:01.503890Z",
     "shell.execute_reply": "2026-05-19T20:06:01.491689Z"
    }
   },
   "outputs": [],
   "source": [
    "w = ROOT.RooWorkspace(\"w\", \"workspace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7655ae29",
   "metadata": {},
   "source": [
    "Import model and all its components into the workspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5c6357e7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.528589Z",
     "iopub.status.busy": "2026-05-19T20:06:01.528423Z",
     "iopub.status.idle": "2026-05-19T20:06:01.718981Z",
     "shell.execute_reply": "2026-05-19T20:06:01.713208Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::model\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooChebychev::bkg\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::x\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a0\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::a1\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::bkgfrac\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooAddPdf::sig\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig1\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::mean\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma1\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sig1frac\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooGaussian::sig2\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooRealVar::sigma2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.Import(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f207d9",
   "metadata": {},
   "source": [
    "Import data into the workspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "29d7ba02",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.729348Z",
     "iopub.status.busy": "2026-05-19T20:06:01.729186Z",
     "iopub.status.idle": "2026-05-19T20:06:01.929285Z",
     "shell.execute_reply": "2026-05-19T20:06:01.909959Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset modelData\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.Import(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9331a14e",
   "metadata": {},
   "source": [
    "Print workspace contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fc34e1d8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:01.940115Z",
     "iopub.status.busy": "2026-05-19T20:06:01.939947Z",
     "iopub.status.idle": "2026-05-19T20:06:02.051851Z",
     "shell.execute_reply": "2026-05-19T20:06:02.051331Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "RooWorkspace(w) workspace contents\n",
      "\n",
      "variables\n",
      "---------\n",
      "(a0,a1,bkgfrac,mean,sig1frac,sigma1,sigma2,x)\n",
      "\n",
      "p.d.f.s\n",
      "-------\n",
      "RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 1\n",
      "RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 1/1\n",
      "RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 1/1\n",
      "RooGaussian::sig1[ x=x mean=mean sigma=sigma1 ] = 1\n",
      "RooGaussian::sig2[ x=x mean=mean sigma=sigma2 ] = 1\n",
      "\n",
      "datasets\n",
      "--------\n",
      "RooDataSet::modelData(x)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "w.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cfe46d7",
   "metadata": {},
   "source": [
    "Save workspace in file\n",
    "-------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "648c297e",
   "metadata": {},
   "source": [
    "Save the workspace into a ROOT file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f1c43dc9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:06:02.081918Z",
     "iopub.status.busy": "2026-05-19T20:06:02.081748Z",
     "iopub.status.idle": "2026-05-19T20:06:02.458385Z",
     "shell.execute_reply": "2026-05-19T20:06:02.436686Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.writeToFile(\"rf502_workspace_py.root\")"
   ]
  }
 ],
 "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
}
