{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ae0a15b4",
   "metadata": {},
   "source": [
    "# df004_cutFlowReport\n",
    "Display cut/Filter efficiencies with RDataFrame.\n",
    "\n",
    "This tutorial shows how to get information about the efficiency of the filters\n",
    "applied\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": "a140662f",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:26.223373Z",
     "iopub.status.busy": "2026-05-19T20:09:26.223251Z",
     "iopub.status.idle": "2026-05-19T20:09:27.183992Z",
     "shell.execute_reply": "2026-05-19T20:09:27.183524Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "\n",
    "def fill_tree(treeName, fileName):\n",
    "    df = ROOT.RDataFrame(50)\n",
    "    df.Define(\"b1\", \"(double) rdfentry_\")\\\n",
    "      .Define(\"b2\", \"(int) rdfentry_ * rdfentry_\").Snapshot(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1839021a",
   "metadata": {},
   "source": [
    "We prepare an input tree to run on"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "301fdf4e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:27.186677Z",
     "iopub.status.busy": "2026-05-19T20:09:27.186529Z",
     "iopub.status.idle": "2026-05-19T20:09:28.940293Z",
     "shell.execute_reply": "2026-05-19T20:09:28.939840Z"
    }
   },
   "outputs": [],
   "source": [
    "fileName = 'df004_cutFlowReport_py.root'\n",
    "treeName = 'myTree'\n",
    "fill_tree(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c971cfc7",
   "metadata": {},
   "source": [
    "We read the tree from the file and create a RDataFrame, a class that\n",
    "allows us to interact with the data contained in the tree."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c450b23d",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:28.945317Z",
     "iopub.status.busy": "2026-05-19T20:09:28.945192Z",
     "iopub.status.idle": "2026-05-19T20:09:29.060320Z",
     "shell.execute_reply": "2026-05-19T20:09:29.059646Z"
    }
   },
   "outputs": [],
   "source": [
    "d = ROOT.RDataFrame(treeName, fileName)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f608f2fa",
   "metadata": {},
   "source": [
    "## Define cuts and create the report\n",
    "An optional string parameter name can be passed to the Filter method to create a named filter.\n",
    "Named filters work as usual, but also keep track of how many entries they accept and reject."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "100b3d9d",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:29.062320Z",
     "iopub.status.busy": "2026-05-19T20:09:29.062201Z",
     "iopub.status.idle": "2026-05-19T20:09:29.275916Z",
     "shell.execute_reply": "2026-05-19T20:09:29.275400Z"
    }
   },
   "outputs": [],
   "source": [
    "filtered1 = d.Filter('b1 > 25', 'Cut1')\n",
    "filtered2 = d.Filter('0 == b2 % 2', 'Cut2')\n",
    "\n",
    "augmented1 = filtered2.Define('b3', 'b1 / b2')\n",
    "filtered3 = augmented1.Filter('b3 < .5','Cut3')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a716a4c",
   "metadata": {},
   "source": [
    "Statistics are retrieved through a call to the Report method:\n",
    "when Report is called on the main RDataFrame object, it retrieves stats for\n",
    "all named filters declared up to that point. When called on a stored chain\n",
    "state (i.e. a chain/graph node), it retrieves stats for all named filters in\n",
    "the section of the chain between the main RDataFrame and that node (included).\n",
    "Stats are printed in the same order as named filters that have been added to the\n",
    "graph, and refer to the latest event-loop that has been running using the relevant\n",
    "RDataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8f201b3e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:29.278284Z",
     "iopub.status.busy": "2026-05-19T20:09:29.278165Z",
     "iopub.status.idle": "2026-05-19T20:09:30.677007Z",
     "shell.execute_reply": "2026-05-19T20:09:30.676366Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cut3 stats:\n",
      "All stats:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cut1                : pass=24         all=50         -- eff=48.00 % cumulative eff=48.00 %\n",
      "Cut2                : pass=25         all=50         -- eff=50.00 % cumulative eff=50.00 %\n",
      "Cut3                : pass=23         all=25         -- eff=92.00 % cumulative eff=46.00 %\n"
     ]
    }
   ],
   "source": [
    "print('Cut3 stats:')\n",
    "filtered3.Report()\n",
    "print('All stats:')\n",
    "allCutsReport = d.Report()\n",
    "allCutsReport.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f938272",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2008bbe7",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:09:30.681653Z",
     "iopub.status.busy": "2026-05-19T20:09:30.681510Z",
     "iopub.status.idle": "2026-05-19T20:09:30.789466Z",
     "shell.execute_reply": "2026-05-19T20:09:30.788822Z"
    }
   },
   "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
}
