{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "74ede07b",
   "metadata": {},
   "source": [
    "# df031_Stats\n",
    "Use the Stats action to extract the statistics of a column.\n",
    "\n",
    "Extract the statistics relative to RDataFrame columns and store them\n",
    "in TStatistic instances.\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:10 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c31b6c03",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:08.752937Z",
     "iopub.status.busy": "2026-05-19T20:10:08.752807Z",
     "iopub.status.idle": "2026-05-19T20:10:09.744286Z",
     "shell.execute_reply": "2026-05-19T20:10:09.743202Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9a929ca",
   "metadata": {},
   "source": [
    "Create a data frame and add two columns: one for the values and one for the weight."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1045773c",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:09.751591Z",
     "iopub.status.busy": "2026-05-19T20:10:09.751443Z",
     "iopub.status.idle": "2026-05-19T20:10:10.293094Z",
     "shell.execute_reply": "2026-05-19T20:10:10.292616Z"
    }
   },
   "outputs": [],
   "source": [
    "r = ROOT.RDataFrame(256);\n",
    "rr = r.Define(\"v\", \"rdfentry_\")\\\n",
    "      .Define(\"w\", \"return 1./(v+1)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a4573e1",
   "metadata": {},
   "source": [
    "Now extract the statistics, weighted, unweighted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "adb666a5",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:10.310400Z",
     "iopub.status.busy": "2026-05-19T20:10:10.310257Z",
     "iopub.status.idle": "2026-05-19T20:10:10.537045Z",
     "shell.execute_reply": "2026-05-19T20:10:10.536367Z"
    }
   },
   "outputs": [],
   "source": [
    "stats_iu = rr.Stats(\"v\")\n",
    "stats_iw = rr.Stats(\"v\", \"w\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f29d86a1",
   "metadata": {},
   "source": [
    "Now print them: they are all identical of course!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "22e41528",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:10.538853Z",
     "iopub.status.busy": "2026-05-19T20:10:10.538697Z",
     "iopub.status.idle": "2026-05-19T20:10:11.844012Z",
     "shell.execute_reply": "2026-05-19T20:10:11.843633Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " OBJ: TStatistic\t  \t Mean = 127.5 +- 4.628 \t RMS = 74.045 \t Count = 256 \t Min = 0 \t Max = 255\n",
      " OBJ: TStatistic\t  \t Mean = 40.8 +- 12.86 \t RMS = 60.318 \t Count = 256 \t Min = 0 \t Max = 255\n"
     ]
    }
   ],
   "source": [
    "stats_iu.Print()\n",
    "stats_iw.Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bfa1a3d",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21682026",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:11.870289Z",
     "iopub.status.busy": "2026-05-19T20:10:11.870141Z",
     "iopub.status.idle": "2026-05-19T20:10:11.977891Z",
     "shell.execute_reply": "2026-05-19T20:10:11.977419Z"
    }
   },
   "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
}
