{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "64ed23e7",
   "metadata": {},
   "source": [
    "# vo008_numpyInterface\n",
    "This tutorial illustrates the conversion of RVec to numpy\n",
    "arrays without copying the data.\n",
    "The memory-adoption is achieved by the dictionary __array_interface__, which\n",
    "is added dynamically to the Python objects by PyROOT.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Stefan Wunsch  \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:28 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cc62e226",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:19.040345Z",
     "iopub.status.busy": "2026-05-19T20:28:19.040229Z",
     "iopub.status.idle": "2026-05-19T20:28:20.075184Z",
     "shell.execute_reply": "2026-05-19T20:28:20.074435Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "from sys import exit\n",
    "\n",
    "try:\n",
    "    import numpy as np\n",
    "except:\n",
    "    exit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42ca5908",
   "metadata": {},
   "source": [
    "Create a vector ROOT object and assign values\n",
    "Note that this works as well with a ROOT.std.vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca71b29d",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:20.077146Z",
     "iopub.status.busy": "2026-05-19T20:28:20.076942Z",
     "iopub.status.idle": "2026-05-19T20:28:20.221007Z",
     "shell.execute_reply": "2026-05-19T20:28:20.220371Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content of the ROOT vector object: [1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "vec = ROOT.RVecF(2) # or ROOT.std.vector(\"float\")(2)\n",
    "vec[0] = 1\n",
    "vec[1] = 2\n",
    "print(\"Content of the ROOT vector object: {}\".format([x for x in vec]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9957e8f",
   "metadata": {},
   "source": [
    "Interface ROOT vector with a numpy array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "35c09bab",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:20.222857Z",
     "iopub.status.busy": "2026-05-19T20:28:20.222727Z",
     "iopub.status.idle": "2026-05-19T20:28:20.334310Z",
     "shell.execute_reply": "2026-05-19T20:28:20.333514Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content of the associated numpy array: [np.float32(1.0), np.float32(2.0)]\n"
     ]
    }
   ],
   "source": [
    "array = np.asarray(vec)\n",
    "print(\"Content of the associated numpy array: {}\".format([x for x in array]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76b232d2",
   "metadata": {},
   "source": [
    "The numpy array adopts the memory of the vector without copying the content.\n",
    "Note that the first entry of the numpy array changes when assigning a new\n",
    "value to the first entry of the ROOT vector."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f0b0b264",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:20.335898Z",
     "iopub.status.busy": "2026-05-19T20:28:20.335767Z",
     "iopub.status.idle": "2026-05-19T20:28:20.439484Z",
     "shell.execute_reply": "2026-05-19T20:28:20.438705Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Content of the numpy array after changing the first entry of the ROOT vector: [np.float32(42.0), np.float32(2.0)]\n"
     ]
    }
   ],
   "source": [
    "vec[0] = 42\n",
    "print(\n",
    "    \"Content of the numpy array after changing the first entry of the ROOT vector: {}\".\n",
    "    format([x for x in array]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d0be739",
   "metadata": {},
   "source": [
    "Use numpy features on data of ROOT objects"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e90edc5a",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:20.440986Z",
     "iopub.status.busy": "2026-05-19T20:28:20.440859Z",
     "iopub.status.idle": "2026-05-19T20:28:20.544159Z",
     "shell.execute_reply": "2026-05-19T20:28:20.543536Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean of the numpy array entries: 22.0\n"
     ]
    }
   ],
   "source": [
    "print(\"Mean of the numpy array entries: {}\".format(np.mean(array)))"
   ]
  }
 ],
 "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
}
