{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0c3aa1f",
   "metadata": {},
   "source": [
    "# df006_ranges\n",
    "Use Range to limit the amount of data processed.\n",
    "\n",
    "This tutorial shows how to express the concept of ranges when working with the RDataFrame.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Danilo Piparo (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": "3c40ef53",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:28.089340Z",
     "iopub.status.busy": "2026-05-19T20:09:28.089210Z",
     "iopub.status.idle": "2026-05-19T20:09:29.111894Z",
     "shell.execute_reply": "2026-05-19T20:09:29.111455Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "\n",
    "def fill_tree(treeName, fileName):\n",
    "    df = ROOT.RDataFrame(100)\n",
    "    df.Define(\"b1\", \"(int) rdfentry_\")\\\n",
    "      .Define(\"b2\", \"(float) rdfentry_ * rdfentry_\").Snapshot(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1068ae2",
   "metadata": {},
   "source": [
    "We prepare an input tree to run on"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ddb4a643",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:29.136446Z",
     "iopub.status.busy": "2026-05-19T20:09:29.136290Z",
     "iopub.status.idle": "2026-05-19T20:09:30.897329Z",
     "shell.execute_reply": "2026-05-19T20:09:30.896961Z"
    }
   },
   "outputs": [],
   "source": [
    "fileName = \"df006_ranges_py.root\"\n",
    "treeName = \"myTree\"\n",
    "\n",
    "fill_tree(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfbd19f4",
   "metadata": {},
   "source": [
    "We read the tree from the file and create a RDataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3be6abc6",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:30.907254Z",
     "iopub.status.busy": "2026-05-19T20:09:30.907125Z",
     "iopub.status.idle": "2026-05-19T20:09:31.038112Z",
     "shell.execute_reply": "2026-05-19T20:09:31.033720Z"
    }
   },
   "outputs": [],
   "source": [
    "d = ROOT.RDataFrame(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27414dd5",
   "metadata": {},
   "source": [
    "## Usage of ranges\n",
    "Now we'll count some entries using ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "53bf7d79",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:31.050121Z",
     "iopub.status.busy": "2026-05-19T20:09:31.049972Z",
     "iopub.status.idle": "2026-05-19T20:09:31.673485Z",
     "shell.execute_reply": "2026-05-19T20:09:31.673054Z"
    }
   },
   "outputs": [],
   "source": [
    "c_all = d.Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5180e734",
   "metadata": {},
   "source": [
    "This is how you can express a range of the first 30 entries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3a64c612",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:31.688476Z",
     "iopub.status.busy": "2026-05-19T20:09:31.688316Z",
     "iopub.status.idle": "2026-05-19T20:09:32.385117Z",
     "shell.execute_reply": "2026-05-19T20:09:32.384621Z"
    }
   },
   "outputs": [],
   "source": [
    "d_0_30 = d.Range(30) \n",
    "c_0_30 = d_0_30.Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d098d866",
   "metadata": {},
   "source": [
    "This is how you pick all entries from 15 onwards"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "65c9451f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:32.397390Z",
     "iopub.status.busy": "2026-05-19T20:09:32.397239Z",
     "iopub.status.idle": "2026-05-19T20:09:32.542951Z",
     "shell.execute_reply": "2026-05-19T20:09:32.542377Z"
    }
   },
   "outputs": [],
   "source": [
    "d_15_end = d.Range(15, 0)\n",
    "c_15_end = d_15_end.Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05824114",
   "metadata": {},
   "source": [
    "We can use a stride too, in this case we pick an event every 3 entries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c69d37f2",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:32.570244Z",
     "iopub.status.busy": "2026-05-19T20:09:32.570100Z",
     "iopub.status.idle": "2026-05-19T20:09:32.674172Z",
     "shell.execute_reply": "2026-05-19T20:09:32.673059Z"
    }
   },
   "outputs": [],
   "source": [
    "d_15_end_3 = d.Range(15, 0, 3)\n",
    "c_15_end_3 = d_15_end_3.Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df0a2fb8",
   "metadata": {},
   "source": [
    "The Range here acts first on the (whole) RDataFrame graph:\n",
    "Not only actions (like Count) but also filters and new columns can be added to it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "67537db0",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:32.675700Z",
     "iopub.status.busy": "2026-05-19T20:09:32.675547Z",
     "iopub.status.idle": "2026-05-19T20:09:33.206369Z",
     "shell.execute_reply": "2026-05-19T20:09:33.191424Z"
    }
   },
   "outputs": [],
   "source": [
    "d_0_50 = d.Range(50)\n",
    "c_0_50_odd_b1 = d_0_50.Filter(\"1 == b1 % 2\").Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4141c11",
   "metadata": {},
   "source": [
    "An important thing to notice is that the counts of a filter are relative to the\n",
    "number of entries a filter \"sees\". Therefore, if a Range depends on a filter,\n",
    "the Range will act on the entries passing the filter only."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d34a1602",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:33.226648Z",
     "iopub.status.busy": "2026-05-19T20:09:33.226481Z",
     "iopub.status.idle": "2026-05-19T20:09:33.831807Z",
     "shell.execute_reply": "2026-05-19T20:09:33.831261Z"
    }
   },
   "outputs": [],
   "source": [
    "c_0_3_after_even_b1 = d.Filter(\"0 == b1 % 2\").Range(0, 3).Count()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81e27144",
   "metadata": {},
   "source": [
    "Ok, time to wrap up: let's print all counts!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "daa945ef",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:33.844396Z",
     "iopub.status.busy": "2026-05-19T20:09:33.844259Z",
     "iopub.status.idle": "2026-05-19T20:09:34.209204Z",
     "shell.execute_reply": "2026-05-19T20:09:34.208817Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Usage of ranges:\n",
      " - All entries: 100\n",
      " - Entries from 0 to 30: 30\n",
      " - Entries from 15 onwards: 85\n",
      " - Entries from 15 onwards in steps of 3: 29\n",
      " - Entries from 0 to 50, odd only: 25\n",
      " - First three entries of all even entries: 3\n"
     ]
    }
   ],
   "source": [
    "print(\"Usage of ranges:\")\n",
    "print(\" - All entries:\", c_all.GetValue())\n",
    "print(\" - Entries from 0 to 30:\", c_0_30.GetValue())\n",
    "print(\" - Entries from 15 onwards:\", c_15_end.GetValue())\n",
    "print(\" - Entries from 15 onwards in steps of 3:\", c_15_end_3.GetValue())\n",
    "print(\" - Entries from 0 to 50, odd only:\", c_0_50_odd_b1.GetValue())\n",
    "print(\" - First three entries of all even entries:\", c_0_3_after_even_b1.GetValue())"
   ]
  }
 ],
 "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
}
