{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d69aa7b3",
   "metadata": {},
   "source": [
    "# df032_RDFFromNumpy\n",
    "Read data from Numpy arrays into RDataFrame.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Stefan Wunsch (KIT, 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": "62bd8f58",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:08.838318Z",
     "iopub.status.busy": "2026-05-19T20:10:08.838176Z",
     "iopub.status.idle": "2026-05-19T20:10:09.864018Z",
     "shell.execute_reply": "2026-05-19T20:10:09.863567Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0a6bd73",
   "metadata": {},
   "source": [
    "Let's create some data in numpy arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e4bb4d0a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:09.866363Z",
     "iopub.status.busy": "2026-05-19T20:10:09.866159Z",
     "iopub.status.idle": "2026-05-19T20:10:09.984284Z",
     "shell.execute_reply": "2026-05-19T20:10:09.983644Z"
    }
   },
   "outputs": [],
   "source": [
    "x = np.array([1, 2, 3], dtype=np.int32)\n",
    "y = np.array([4, 5, 6], dtype=np.float64)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7fd7b43",
   "metadata": {},
   "source": [
    "Read the data with RDataFrame\n",
    "The column names in the RDataFrame are defined by the keys of the dictionary.\n",
    "Please note that only fundamental types (int, float, ...) are supported and\n",
    "the arrays must have the same length."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4fa19ba2",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:10.000444Z",
     "iopub.status.busy": "2026-05-19T20:10:10.000291Z",
     "iopub.status.idle": "2026-05-19T20:10:10.936913Z",
     "shell.execute_reply": "2026-05-19T20:10:10.926365Z"
    }
   },
   "outputs": [],
   "source": [
    "df = ROOT.RDF.FromNumpy({'x': x, 'y': y})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3f92e5b",
   "metadata": {},
   "source": [
    "You can now use the RDataFrame as usually, e.g. add a column ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "410d966a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:10.940397Z",
     "iopub.status.busy": "2026-05-19T20:10:10.940255Z",
     "iopub.status.idle": "2026-05-19T20:10:11.101132Z",
     "shell.execute_reply": "2026-05-19T20:10:11.100648Z"
    }
   },
   "outputs": [],
   "source": [
    "df = df.Define('z', 'x + y')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c0d7ea6",
   "metadata": {},
   "source": [
    "... or print the content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fe574c34",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:11.108350Z",
     "iopub.status.busy": "2026-05-19T20:10:11.108209Z",
     "iopub.status.idle": "2026-05-19T20:10:12.345123Z",
     "shell.execute_reply": "2026-05-19T20:10:12.336649Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+-----+---+----------+----------+\n",
      "| Row | x | y        | z        | \n",
      "+-----+---+----------+----------+\n",
      "| 0   | 1 | 4.000000 | 5.000000 | \n",
      "+-----+---+----------+----------+\n",
      "| 1   | 2 | 5.000000 | 7.000000 | \n",
      "+-----+---+----------+----------+\n",
      "| 2   | 3 | 6.000000 | 9.000000 | \n",
      "+-----+---+----------+----------+\n"
     ]
    }
   ],
   "source": [
    "df.Display().Print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e716db46",
   "metadata": {},
   "source": [
    "... or save the data as a ROOT file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4ee8312a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:12.346727Z",
     "iopub.status.busy": "2026-05-19T20:10:12.346552Z",
     "iopub.status.idle": "2026-05-19T20:10:13.284046Z",
     "shell.execute_reply": "2026-05-19T20:10:13.283348Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cppyy.gbl.ROOT.RDF.RResultPtr<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager> > object at 0x55fe404bdae0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.Snapshot('tree', 'df032_RDFFromNumpy.root')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdfd5b4e",
   "metadata": {},
   "source": [
    "Draw all canvases "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d4a199aa",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:10:13.295212Z",
     "iopub.status.busy": "2026-05-19T20:10:13.295068Z",
     "iopub.status.idle": "2026-05-19T20:10:13.409512Z",
     "shell.execute_reply": "2026-05-19T20:10:13.409137Z"
    }
   },
   "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
}
