{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7fe67220",
   "metadata": {},
   "source": [
    "# rf406_cattocatfuncs\n",
    "Data and categories: demonstration of discrete-discrete (invertable) functions\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:31 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "796bbdc9",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:50.072554Z",
     "iopub.status.busy": "2026-05-19T20:31:50.072430Z",
     "iopub.status.idle": "2026-05-19T20:31:51.033364Z",
     "shell.execute_reply": "2026-05-19T20:31:51.032756Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfa2c33b",
   "metadata": {},
   "source": [
    "Construct two categories\n",
    "----------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "980105db",
   "metadata": {},
   "source": [
    "Define a category with labels only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e0d000d7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.035485Z",
     "iopub.status.busy": "2026-05-19T20:31:51.035350Z",
     "iopub.status.idle": "2026-05-19T20:31:51.182989Z",
     "shell.execute_reply": "2026-05-19T20:31:51.182253Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RooCategory::tagCat = Lepton(idx = 0)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "tagCat = ROOT.RooCategory(\"tagCat\", \"Tagging category\")\n",
    "tagCat.defineType(\"Lepton\")\n",
    "tagCat.defineType(\"Kaon\")\n",
    "tagCat.defineType(\"NetTagger-1\")\n",
    "tagCat.defineType(\"NetTagger-2\")\n",
    "tagCat.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74c01d37",
   "metadata": {},
   "source": [
    "Define a category with explicitly numbered states"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4bb00cef",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.185572Z",
     "iopub.status.busy": "2026-05-19T20:31:51.185447Z",
     "iopub.status.idle": "2026-05-19T20:31:51.297394Z",
     "shell.execute_reply": "2026-05-19T20:31:51.296370Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RooCategory::b0flav = B0(idx = -1)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "b0flav = ROOT.RooCategory(\"b0flav\", \"B0 flavour eigenstate\", {\"B0\": -1, \"B0bar\": 1})\n",
    "b0flav.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "898532b9",
   "metadata": {},
   "source": [
    "Construct a dummy dataset with random values of tagCat and b0flav"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f7c007ff",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.298998Z",
     "iopub.status.busy": "2026-05-19T20:31:51.298864Z",
     "iopub.status.idle": "2026-05-19T20:31:51.490802Z",
     "shell.execute_reply": "2026-05-19T20:31:51.490104Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", 0, 10)\n",
    "p = ROOT.RooPolynomial(\"p\", \"p\", x)\n",
    "data = p.generate({x, b0flav, tagCat}, 10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b62e62c1",
   "metadata": {},
   "source": [
    "Create a cat -> cat mapping category\n",
    "---------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "041b1bb5",
   "metadata": {},
   "source": [
    "A RooMappedCategory is category.category mapping function based on string expression\n",
    "The constructor takes an input category an a default state name to which unassigned\n",
    "states are mapped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66b0d83f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.493179Z",
     "iopub.status.busy": "2026-05-19T20:31:51.493032Z",
     "iopub.status.idle": "2026-05-19T20:31:51.606046Z",
     "shell.execute_reply": "2026-05-19T20:31:51.605483Z"
    }
   },
   "outputs": [],
   "source": [
    "tcatType = ROOT.RooMappedCategory(\"tcatType\", \"tagCat type\", tagCat, \"Cut based\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cd4de29",
   "metadata": {},
   "source": [
    "Enter fully specified state mappings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8aca7377",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.608117Z",
     "iopub.status.busy": "2026-05-19T20:31:51.607957Z",
     "iopub.status.idle": "2026-05-19T20:31:51.719267Z",
     "shell.execute_reply": "2026-05-19T20:31:51.718741Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tcatType.map(\"Lepton\", \"Cut based\")\n",
    "tcatType.map(\"Kaon\", \"Cut based\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad3740b6",
   "metadata": {},
   "source": [
    "Enter a wildcard expression mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c1487bcd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.721511Z",
     "iopub.status.busy": "2026-05-19T20:31:51.721377Z",
     "iopub.status.idle": "2026-05-19T20:31:51.826431Z",
     "shell.execute_reply": "2026-05-19T20:31:51.825395Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tcatType.map(\"NetTagger*\", \"Neural Network\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b6767ca",
   "metadata": {},
   "source": [
    "Make a table of the mapped category state multiplicit in data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6a37094b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.828048Z",
     "iopub.status.busy": "2026-05-19T20:31:51.827916Z",
     "iopub.status.idle": "2026-05-19T20:31:51.942989Z",
     "shell.execute_reply": "2026-05-19T20:31:51.942168Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  Table tcatType : pData\n",
      "  +----------------+------+\n",
      "  |      Cut based | 5058 |\n",
      "  | Neural Network | 4942 |\n",
      "  +----------------+------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "mtable = data.table(tcatType)\n",
    "mtable.Print(\"v\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "885baa30",
   "metadata": {},
   "source": [
    "Create a cat X cat product category\n",
    "----------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f2f539d",
   "metadata": {},
   "source": [
    "A SUPER-category is 'product' of _lvalue_ categories. The state names of a super\n",
    "category is a composite of the state labels of the input categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fee6a28a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:51.944550Z",
     "iopub.status.busy": "2026-05-19T20:31:51.944417Z",
     "iopub.status.idle": "2026-05-19T20:31:52.084051Z",
     "shell.execute_reply": "2026-05-19T20:31:52.079096Z"
    }
   },
   "outputs": [],
   "source": [
    "b0Xtcat = ROOT.RooSuperCategory(\"b0Xtcat\", \"b0flav X tagCat\", {b0flav, tagCat})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbf3a0e3",
   "metadata": {},
   "source": [
    "Make a table of the product category state multiplicity in data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a42ace1b",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:52.085760Z",
     "iopub.status.busy": "2026-05-19T20:31:52.085618Z",
     "iopub.status.idle": "2026-05-19T20:31:52.191849Z",
     "shell.execute_reply": "2026-05-19T20:31:52.191267Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  Table b0Xtcat : pData\n",
      "  +---------------------+------+\n",
      "  |         {B0;Lepton} | 1281 |\n",
      "  |      {B0bar;Lepton} | 1269 |\n",
      "  |           {B0;Kaon} | 1253 |\n",
      "  |        {B0bar;Kaon} | 1255 |\n",
      "  |    {B0;NetTagger-1} | 1234 |\n",
      "  | {B0bar;NetTagger-1} | 1219 |\n",
      "  |    {B0;NetTagger-2} | 1272 |\n",
      "  | {B0bar;NetTagger-2} | 1217 |\n",
      "  +---------------------+------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "stable = data.table(b0Xtcat)\n",
    "stable.Print(\"v\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c8ae307",
   "metadata": {},
   "source": [
    "Since the super category is an lvalue, is explicitly possible"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9e5a1e3a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:52.193645Z",
     "iopub.status.busy": "2026-05-19T20:31:52.193493Z",
     "iopub.status.idle": "2026-05-19T20:31:52.304806Z",
     "shell.execute_reply": "2026-05-19T20:31:52.304113Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b0Xtcat.setLabel(\"{B0bar;Lepton}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a547bce",
   "metadata": {},
   "source": [
    "A MULTI-category is a 'product' of any category (function). The state names of a super\n",
    "category is a composite of the state labels of the input categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1fa918ed",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:52.314455Z",
     "iopub.status.busy": "2026-05-19T20:31:52.314316Z",
     "iopub.status.idle": "2026-05-19T20:31:52.431391Z",
     "shell.execute_reply": "2026-05-19T20:31:52.430740Z"
    }
   },
   "outputs": [],
   "source": [
    "b0Xttype = ROOT.RooMultiCategory(\"b0Xttype\", \"b0flav X tagType\", {b0flav, tcatType})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd59279d",
   "metadata": {},
   "source": [
    "Make a table of the product category state multiplicity in data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dc60be39",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:52.433531Z",
     "iopub.status.busy": "2026-05-19T20:31:52.433397Z",
     "iopub.status.idle": "2026-05-19T20:31:52.539678Z",
     "shell.execute_reply": "2026-05-19T20:31:52.538835Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  Table b0Xttype : pData\n",
      "  +------------------------+------+\n",
      "  |         {B0;Cut based} | 2534 |\n",
      "  |      {B0bar;Cut based} | 2524 |\n",
      "  |    {B0;Neural Network} | 2506 |\n",
      "  | {B0bar;Neural Network} | 2436 |\n",
      "  +------------------------+------+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "xtable = data.table(b0Xttype)\n",
    "xtable.Print(\"v\")"
   ]
  }
 ],
 "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
}
