{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a30c860",
   "metadata": {},
   "source": [
    "# rf408_RDataFrameToRooFit\n",
    "Fill RooDataSet/RooDataHist in RDataFrame.\n",
    "\n",
    "This tutorial shows how to fill RooFit data classes directly from RDataFrame.\n",
    "Using two small helpers, we tell RDataFrame where the data has to go.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Harshal Shende, Stephan Hageboeck (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": "44b33340",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:56.891971Z",
     "iopub.status.busy": "2026-05-19T20:31:56.891853Z",
     "iopub.status.idle": "2026-05-19T20:31:57.866396Z",
     "shell.execute_reply": "2026-05-19T20:31:57.865793Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d26589cb",
   "metadata": {},
   "source": [
    "Set up\n",
    "------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1e6d077",
   "metadata": {},
   "source": [
    "We create an RDataFrame with two columns filled with 2 million random numbers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4f8f395c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:57.868719Z",
     "iopub.status.busy": "2026-05-19T20:31:57.868558Z",
     "iopub.status.idle": "2026-05-19T20:31:58.388839Z",
     "shell.execute_reply": "2026-05-19T20:31:58.388135Z"
    }
   },
   "outputs": [],
   "source": [
    "df = ROOT.RDataFrame(2000000).Define(\"x\", \"gRandom->Uniform(-5.,  5.)\").Define(\"y\", \"gRandom->Gaus(1., 3.)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded2e37c",
   "metadata": {},
   "source": [
    "We create RooFit variables that will represent the dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1e08dde5",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:58.391156Z",
     "iopub.status.busy": "2026-05-19T20:31:58.391028Z",
     "iopub.status.idle": "2026-05-19T20:31:58.543833Z",
     "shell.execute_reply": "2026-05-19T20:31:58.543144Z"
    }
   },
   "outputs": [],
   "source": [
    "x = ROOT.RooRealVar(\"x\", \"x\", -5.0, 5.0)\n",
    "y = ROOT.RooRealVar(\"y\", \"y\", -50.0, 50.0)\n",
    "x.setBins(10)\n",
    "y.setBins(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb7802f2",
   "metadata": {},
   "source": [
    "Booking the creation of RooDataSet / RooDataHist in RDataFrame\n",
    "----------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98fbeedb",
   "metadata": {},
   "source": [
    "Method 1:\n",
    "---------\n",
    "We directly book the RooDataSetHelper action.\n",
    "We need to pass\n",
    "- the RDataFrame column types as template parameters\n",
    "- the constructor arguments for RooDataSet (they follow the same syntax as the usual RooDataSet constructors)\n",
    "- the column names that RDataFrame should fill into the dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c128e9d3",
   "metadata": {},
   "source": [
    "NOTE: RDataFrame columns are matched to RooFit variables by position, *not by name*!\n",
    "\n",
    "The returned object is not yet a RooDataSet, but an RResultPtr that will be\n",
    "lazy-evaluated once you call GetValue() on it. We will only evaluate the\n",
    "RResultPtr once all other RDataFrame related actions are declared. This way\n",
    "we trigger the event loop computation only once, which will improve the\n",
    "runtime significantly.\n",
    "\n",
    "To learn more about lazy actions, see:\n",
    "    https://root.cern/doc/master/classROOT_1_1RDataFrame.html#actions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "55b0a13a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:58.552574Z",
     "iopub.status.busy": "2026-05-19T20:31:58.552435Z",
     "iopub.status.idle": "2026-05-19T20:31:59.066204Z",
     "shell.execute_reply": "2026-05-19T20:31:59.065577Z"
    }
   },
   "outputs": [],
   "source": [
    "roo_data_set_result = df.Book(\n",
    "    ROOT.std.move(ROOT.RooDataSetHelper(\"dataset\", \"Title of dataset\", ROOT.RooArgSet(x, y))), (\"x\", \"y\")\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26520f1a",
   "metadata": {},
   "source": [
    "Method 2:\n",
    "---------\n",
    "We first declare the RooDataHistHelper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9561199e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:59.068264Z",
     "iopub.status.busy": "2026-05-19T20:31:59.068124Z",
     "iopub.status.idle": "2026-05-19T20:31:59.208262Z",
     "shell.execute_reply": "2026-05-19T20:31:59.207628Z"
    }
   },
   "outputs": [],
   "source": [
    "rdhMaker = ROOT.RooDataHistHelper(\"dataset\", \"Title of dataset\", ROOT.RooArgSet(x, y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cae36686",
   "metadata": {},
   "source": [
    "Then, we move it into an RDataFrame action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b5a1fba3",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:59.210081Z",
     "iopub.status.busy": "2026-05-19T20:31:59.209948Z",
     "iopub.status.idle": "2026-05-19T20:31:59.448594Z",
     "shell.execute_reply": "2026-05-19T20:31:59.447881Z"
    }
   },
   "outputs": [],
   "source": [
    "roo_data_hist_result = df.Book(ROOT.std.move(rdhMaker), (\"x\", \"y\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c5f69d0",
   "metadata": {},
   "source": [
    "Run it and inspect the results\n",
    "-------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc0a690a",
   "metadata": {},
   "source": [
    "At this point, all RDF actions were defined (namely, the `Book` operations),\n",
    "so we can get values from the RResultPtr objects, triggering the event loop\n",
    "and getting the actual RooFit data objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec11264f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:31:59.450324Z",
     "iopub.status.busy": "2026-05-19T20:31:59.450200Z",
     "iopub.status.idle": "2026-05-19T20:32:00.959130Z",
     "shell.execute_reply": "2026-05-19T20:32:00.958411Z"
    }
   },
   "outputs": [],
   "source": [
    "roo_data_set = roo_data_set_result.GetValue()\n",
    "roo_data_hist = roo_data_hist_result.GetValue()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f6e8b91",
   "metadata": {},
   "source": [
    "Let's inspect the dataset / datahist."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8811737d",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:00.961674Z",
     "iopub.status.busy": "2026-05-19T20:32:00.961522Z",
     "iopub.status.idle": "2026-05-19T20:32:01.453782Z",
     "shell.execute_reply": "2026-05-19T20:32:01.453016Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "RooDataSet::dataset[x,y] = 2000000 entries\n",
      "(   4.997,   -0.304, )  weight=     1.000\n",
      "(   4.472,    0.910, )  weight=     1.000\n",
      "(   4.575,    0.830, )  weight=     1.000\n",
      "(   0.400,    0.776, )  weight=     1.000\n",
      "(   2.599,   -0.232, )  weight=     1.000\n",
      "(  -1.844,    1.575, )  weight=     1.000\n",
      "(   0.197,    0.853, )  weight=     1.000\n",
      "(  -1.077,   -0.721, )  weight=     1.000\n",
      "(  -4.697,   -3.165, )  weight=     1.000\n",
      "(   4.437,   -1.208, )  weight=     1.000\n",
      "(   3.983,   -0.146, )  weight=     1.000\n",
      "(  -0.014,   -1.447, )  weight=     1.000\n",
      "(  -3.177,   -2.704, )  weight=     1.000\n",
      "(  -4.371,   -0.363, )  weight=     1.000\n",
      "(   2.254,   -0.499, )  weight=     1.000\n",
      "(   2.139,    6.533, )  weight=     1.000\n",
      "(   1.993,    6.991, )  weight=     1.000\n",
      "(  -3.708,    7.781, )  weight=     1.000\n",
      "(  -4.168,    1.284, )  weight=     1.000\n",
      "(  -4.177,    4.650, )  weight=     1.000\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean(x) = 0.001\tsigma(x) = 2.886\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean(y) = 1.000\tsigma(y) = 3.000\n",
      "\n",
      "\n",
      "(  -4.500,  -47.500, )  weight=     0.000\n",
      "(  -4.500,  -42.500, )  weight=     0.000\n",
      "(  -4.500,  -37.500, )  weight=     0.000\n",
      "(  -4.500,  -32.500, )  weight=     0.000\n",
      "(  -4.500,  -27.500, )  weight=     0.000\n",
      "(  -4.500,  -22.500, )  weight=     0.000\n",
      "(  -4.500,  -17.500, )  weight=     0.000\n",
      "(  -4.500,  -12.500, )  weight=    24.000\n",
      "(  -4.500,   -7.500, )  weight=  4537.000\n",
      "(  -4.500,   -2.500, )  weight= 69653.000\n",
      "(  -4.500,    2.500, )  weight=107838.000\n",
      "(  -4.500,    7.500, )  weight= 17790.000\n",
      "(  -4.500,   12.500, )  weight=   292.000\n",
      "(  -4.500,   17.500, )  weight=     0.000\n",
      "(  -4.500,   22.500, )  weight=     0.000\n",
      "(  -4.500,   27.500, )  weight=     0.000\n",
      "(  -4.500,   32.500, )  weight=     0.000\n",
      "(  -4.500,   37.500, )  weight=     0.000\n",
      "(  -4.500,   42.500, )  weight=     0.000\n",
      "(  -4.500,   47.500, )  weight=     0.000\n",
      "mean(x) = 0.001\tsigma(x) = 2.872\n",
      "mean(y) = 0.999\tsigma(y) = 3.329\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RooDataHist::dataset[x,y] = 200 bins (2e+06 weights)\n"
     ]
    }
   ],
   "source": [
    "def print_data(data):\n",
    "    print(\"\")\n",
    "    data.Print()\n",
    "    for i in range(min(data.numEntries(), 20)):\n",
    "        print(\n",
    "            \"(\"\n",
    "            + \", \".join([\"{0:8.3f}\".format(var.getVal()) for var in data.get(i)])\n",
    "            + \", )  weight={0:10.3f}\".format(data.weight())\n",
    "        )\n",
    "\n",
    "    print(\"mean(x) = {0:.3f}\".format(data.mean(x)) + \"\\tsigma(x) = {0:.3f}\".format(math.sqrt(data.moment(x, 2.0))))\n",
    "    print(\"mean(y) = {0:.3f}\".format(data.mean(y)) + \"\\tsigma(y) = {0:.3f}\\n\".format(math.sqrt(data.moment(y, 2.0))))\n",
    "\n",
    "\n",
    "print_data(roo_data_set)\n",
    "print_data(roo_data_hist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df9bc57e",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "061b7c98",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:32:01.455315Z",
     "iopub.status.busy": "2026-05-19T20:32:01.455189Z",
     "iopub.status.idle": "2026-05-19T20:32:01.562677Z",
     "shell.execute_reply": "2026-05-19T20:32:01.562230Z"
    }
   },
   "outputs": [],
   "source": [
    "from ROOT import gROOT \n",
    "gROOT.GetListOfCanvases().Draw()"
   ]
  }
 ],
 "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
}
