{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6760f3fa",
   "metadata": {},
   "source": [
    "# df012_DefinesAndFiltersAsStrings\n",
    "Use just-in-time-compiled Filters and Defines for quick prototyping.\n",
    "\n",
    "This tutorial illustrates how to use jit-compiling features of RDataFrame\n",
    "to define data using C++ code in a Python script.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Guilherme Amadio (CERN)  \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:09 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da1dac50",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:40.133716Z",
     "iopub.status.busy": "2026-05-19T20:09:40.133506Z",
     "iopub.status.idle": "2026-05-19T20:09:41.123454Z",
     "shell.execute_reply": "2026-05-19T20:09:41.123005Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59134fd4",
   "metadata": {},
   "source": [
    " We will inefficiently calculate an approximation of pi by generating\n",
    " some data and doing very simple filtering and analysis on it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "317597de",
   "metadata": {},
   "source": [
    " We start by creating an empty dataframe where we will insert 10 million\n",
    " random points in a square of side 2.0 (that is, with an inscribed unit\n",
    " circle)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ff8fad8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:41.135739Z",
     "iopub.status.busy": "2026-05-19T20:09:41.135566Z",
     "iopub.status.idle": "2026-05-19T20:09:41.660231Z",
     "shell.execute_reply": "2026-05-19T20:09:41.659872Z"
    }
   },
   "outputs": [],
   "source": [
    "npoints = 10000000\n",
    "df = ROOT.RDataFrame(npoints)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d5b1fc5",
   "metadata": {},
   "source": [
    " Define what data we want inside the dataframe. We do not need to define p\n",
    " as an array, but we do it here to demonstrate how to use jitting with RDataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7073b3e1",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:41.672398Z",
     "iopub.status.busy": "2026-05-19T20:09:41.672261Z",
     "iopub.status.idle": "2026-05-19T20:09:41.823303Z",
     "shell.execute_reply": "2026-05-19T20:09:41.822920Z"
    }
   },
   "outputs": [],
   "source": [
    "pidf = df.Define(\"x\", \"gRandom->Uniform(-1.0, 1.0)\") \\\n",
    "         .Define(\"y\", \"gRandom->Uniform(-1.0, 1.0)\") \\\n",
    "         .Define(\"p\", \"std::array<double, 2> v{x, y}; return v;\") \\\n",
    "         .Define(\"r\", \"double r2 = 0.0; for (auto&& w : p) r2 += w*w; return sqrt(r2);\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "034f78b2",
   "metadata": {},
   "source": [
    " Now we have a dataframe with columns x, y, p (which is a point based on x\n",
    " and y), and the radius r = sqrt(x*x + y*y). In order to approximate pi, we\n",
    " need to know how many of our data points fall inside the circle of radius\n",
    " one compared with the total number of points. The ratio of the areas is\n",
    "\n",
    "     A_circle / A_square = pi r*r / l * l, where r = 1.0, and l = 2.0\n",
    "\n",
    " Therefore, we can approximate pi with four times the number of points inside\n",
    " the unit circle over the total number of points:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3e13dee7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:41.825369Z",
     "iopub.status.busy": "2026-05-19T20:09:41.825245Z",
     "iopub.status.idle": "2026-05-19T20:09:44.412237Z",
     "shell.execute_reply": "2026-05-19T20:09:44.411522Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi is approximately equal to 3.14146\n"
     ]
    }
   ],
   "source": [
    "incircle = pidf.Filter(\"r <= 1.0\").Count().GetValue()\n",
    "\n",
    "pi_approx = 4.0 * incircle / npoints\n",
    "\n",
    "print(\"pi is approximately equal to %g\" % (pi_approx))"
   ]
  }
 ],
 "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
}
