{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c3346fe4",
   "metadata": {},
   "source": [
    "# rf511_wsfactory_basic\n",
    "Organization and simultaneous fits: basic use of the 'object factory' associated with a\n",
    "workspace to rapidly build pdfs functions and their parameter components\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:32 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a3df80f3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:35.116204Z",
     "iopub.status.busy": "2026-05-19T20:32:35.116090Z",
     "iopub.status.idle": "2026-05-19T20:32:36.114786Z",
     "shell.execute_reply": "2026-05-19T20:32:36.114238Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "\n",
    "\n",
    "compact = False\n",
    "w = ROOT.RooWorkspace(\"w\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79b1e8ab",
   "metadata": {},
   "source": [
    "Creating and adding basic pdfs\n",
    "----------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "152df195",
   "metadata": {},
   "source": [
    "Remake example pdf of tutorial rs502_wspacewrite.C:\n",
    "\n",
    "Basic pdf construction: ClassName.ObjectName(constructor arguments)\n",
    "Variable construction    : VarName[x,xlo,xhi], VarName[xlo,xhi], VarName[x]\n",
    "P.d.f. addition          : SUM.ObjectName(coef1*pdf1,...coefM*pdfM,pdfN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "31c6e851",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:36.116857Z",
     "iopub.status.busy": "2026-05-19T20:32:36.116726Z",
     "iopub.status.idle": "2026-05-19T20:32:36.311427Z",
     "shell.execute_reply": "2026-05-19T20:32:36.310833Z"
    }
   },
   "outputs": [],
   "source": [
    "if not compact:\n",
    "    # Use object factory to build pdf of tutorial rs502_wspacewrite\n",
    "    w.factory(\"Gaussian::sig1(x[-10,10],mean[5,0,10],0.5)\")\n",
    "    w.factory(\"Gaussian::sig2(x,mean,1)\")\n",
    "    w.factory(\"Chebychev::bkg(x,{a0[0.5,0.,1],a1[-0.2,0.,1.]})\")\n",
    "    w.factory(\"SUM::sig(sig1frac[0.8,0.,1.]*sig1,sig2)\")\n",
    "    w.factory(\"SUM::model(bkgfrac[0.5,0.,1.]*bkg,sig)\")\n",
    "\n",
    "else:\n",
    "\n",
    "    # Use object factory to build pdf of tutorial rs502_wspacewrite but\n",
    "    #  - Contracted to a single line recursive expression,\n",
    "    #  - Omitting explicit names for components that are not referred to explicitly later\n",
    "\n",
    "    w.factory(\n",
    "        \"SUM::model(bkgfrac[0.5,0.,1.]*Chebychev::bkg(x[-10,10],{a0[0.5,0.,1],a1[-0.2,0.,1.]}), \"\n",
    "        \"SUM(sig1frac[0.8,0.,1.]*Gaussian(x,mean[5,0,10],0.5), Gaussian(x,mean,1)))\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6688fbc5",
   "metadata": {},
   "source": [
    "Advanced pdf constructor arguments\n",
    "----------------------------------------------------------------\n",
    "\n",
    "P.d.f. constructor arguments may by any type of ROOT.RooAbsArg, also\n",
    "\n",
    "Double_t -. converted to ROOT.RooConst(...)\n",
    "{a,b,c} -. converted to ROOT.RooArgSet() or ROOT.RooArgList() depending on required ctor arg\n",
    "dataset name -. converted to ROOT.RooAbsData reference for any dataset residing in the workspace\n",
    "enum -. Any enum label that belongs to an enum defined in the (base)\n",
    "class"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ac0bad7",
   "metadata": {},
   "source": [
    "Make a dummy dataset pdf 'model' and import it in the workspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "06a5ed45",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:36.313124Z",
     "iopub.status.busy": "2026-05-19T20:32:36.313002Z",
     "iopub.status.idle": "2026-05-19T20:32:36.472295Z",
     "shell.execute_reply": "2026-05-19T20:32:36.471616Z"
    }
   },
   "outputs": [],
   "source": [
    "data = w[\"model\"].generate({w[\"x\"]}, 1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a118011b",
   "metadata": {},
   "source": [
    "Cannot call 'import' directly because this is a python keyword:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4ee1e6c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:36.473963Z",
     "iopub.status.busy": "2026-05-19T20:32:36.473837Z",
     "iopub.status.idle": "2026-05-19T20:32:36.615787Z",
     "shell.execute_reply": "2026-05-19T20:32:36.615124Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing dataset modelData\n",
      "[#1] INFO:ObjectHandling -- RooWorkSpace::import(w) changing name of dataset from  modelData to data\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.Import(data, Rename=\"data\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77d3d7fd",
   "metadata": {},
   "source": [
    "Construct a KEYS pdf passing a dataset name and an enum type defining the\n",
    "mirroring strategy\n",
    "w.factory(\"KeysPdf::k(x,data,NoMirror,0.2)\")\n",
    "Workaround for pyROOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b1fb61cf",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:36.617329Z",
     "iopub.status.busy": "2026-05-19T20:32:36.617211Z",
     "iopub.status.idle": "2026-05-19T20:32:36.761097Z",
     "shell.execute_reply": "2026-05-19T20:32:36.760622Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) Resolving name conflict in workspace by changing name of imported node  k to k_workspace\n",
      "[#1] INFO:ObjectHandling -- RooWorkspace::import(w) importing RooKeysPdf::k_workspace\n"
     ]
    }
   ],
   "source": [
    "x = w[\"x\"]\n",
    "k = ROOT.RooKeysPdf(\"k\", \"k\", x, data, ROOT.RooKeysPdf.NoMirror, 0.2)\n",
    "w.Import(k, RenameAllNodes=\"workspace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc18470",
   "metadata": {},
   "source": [
    "Print workspace contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "af84d835",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:36.771552Z",
     "iopub.status.busy": "2026-05-19T20:32:36.771389Z",
     "iopub.status.idle": "2026-05-19T20:32:36.883541Z",
     "shell.execute_reply": "2026-05-19T20:32:36.882919Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "RooWorkspace(w) w contents\n",
      "\n",
      "variables\n",
      "---------\n",
      "(a0,a1,bkgfrac,mean,sig1frac,x)\n",
      "\n",
      "p.d.f.s\n",
      "-------\n",
      "RooChebychev::bkg[ x=x coefList=(a0,a1) ] = 1\n",
      "RooKeysPdf::k_workspace[ x=x ] = 0.0139016\n",
      "RooAddPdf::model[ bkgfrac * bkg + [%] * sig ] = 0.5/1\n",
      "RooAddPdf::sig[ sig1frac * sig1 + [%] * sig2 ] = 7.45331e-07/1\n",
      "RooGaussian::sig1[ x=x mean=mean sigma=0.5 ] = 1.92875e-22\n",
      "RooGaussian::sig2[ x=x mean=mean sigma=1 ] = 3.72665e-06\n",
      "\n",
      "datasets\n",
      "--------\n",
      "RooDataSet::data(x)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "w.Print()"
   ]
  }
 ],
 "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
}
