{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ff9be255",
   "metadata": {},
   "source": [
    "# vo004_SortAndSelect\n",
    "In this tutorial we learn how elements of an RVec can be easily sorted and\n",
    "selected.\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": "f4368576",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:07.559129Z",
     "iopub.status.busy": "2026-05-19T20:28:07.559015Z",
     "iopub.status.idle": "2026-05-19T20:28:08.519863Z",
     "shell.execute_reply": "2026-05-19T20:28:08.518402Z"
    }
   },
   "outputs": [],
   "source": [
    "import ROOT\n",
    "from ROOT.VecOps import Argsort, Take, Sort, Reverse"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa2251a4",
   "metadata": {},
   "source": [
    "RVec can be sorted in Python with the inbuilt sorting function because\n",
    "PyROOT implements a Python iterator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c1477ef6",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:08.521637Z",
     "iopub.status.busy": "2026-05-19T20:28:08.521481Z",
     "iopub.status.idle": "2026-05-19T20:28:08.680441Z",
     "shell.execute_reply": "2026-05-19T20:28:08.679595Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sort vector { 6.0000000, 4.0000000, 5.0000000 }: [4.0, 5.0, 6.0]\n"
     ]
    }
   ],
   "source": [
    "v1 = ROOT.RVecD(3)\n",
    "v1[0], v1[1], v1[2] = 6, 4, 5\n",
    "v2 = sorted(v1)\n",
    "print(\"Sort vector {}: {}\".format(v1, v2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "369348b9",
   "metadata": {},
   "source": [
    "For convenience, ROOT implements helpers, e.g., to get a sorted copy of\n",
    "an RVec ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19399897",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:08.682215Z",
     "iopub.status.busy": "2026-05-19T20:28:08.682084Z",
     "iopub.status.idle": "2026-05-19T20:28:08.825585Z",
     "shell.execute_reply": "2026-05-19T20:28:08.824890Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sort vector { 6.0000000, 4.0000000, 5.0000000 }: { 4.0000000, 5.0000000, 6.0000000 }\n"
     ]
    }
   ],
   "source": [
    "v2 = Sort(v1);\n",
    "print(\"Sort vector {}: {}\".format(v1, v2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bdad3ac",
   "metadata": {},
   "source": [
    "... or a reversed copy of an RVec."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aefbdceb",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:08.827631Z",
     "iopub.status.busy": "2026-05-19T20:28:08.827489Z",
     "iopub.status.idle": "2026-05-19T20:28:08.942615Z",
     "shell.execute_reply": "2026-05-19T20:28:08.941984Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reverse vector { 6.0000000, 4.0000000, 5.0000000 }: { 5.0000000, 4.0000000, 6.0000000 }\n"
     ]
    }
   ],
   "source": [
    "v2 = Reverse(v1);\n",
    "print(\"Reverse vector {}: {}\".format(v1, v2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a476d345",
   "metadata": {},
   "source": [
    "Helpers are provided to get the indices that sort the vector and to\n",
    "select these indices from an RVec."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de96b929",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:08.944559Z",
     "iopub.status.busy": "2026-05-19T20:28:08.944434Z",
     "iopub.status.idle": "2026-05-19T20:28:09.124424Z",
     "shell.execute_reply": "2026-05-19T20:28:09.123724Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Indices that sort the vector { 6.0000000, 4.0000000, 5.0000000 }: { 1, 2, 0 }\n",
      "Sort vector { 9.0000000, 7.0000000, 8.0000000 } respective to the previously determined indices: { 7.0000000, 8.0000000, 9.0000000 }\n"
     ]
    }
   ],
   "source": [
    "v2 = Argsort(v1)\n",
    "print(\"Indices that sort the vector {}: {}\".format(v1, v2))\n",
    "\n",
    "v3 = ROOT.RVecD(3)\n",
    "v3[0], v3[1], v3[2] = 9, 7, 8\n",
    "v4 = Take(v3, v2)\n",
    "print(\"Sort vector {} respective to the previously determined indices: {}\".format(v3, v4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc455b42",
   "metadata": {},
   "source": [
    "Take can also be used to get the first or last elements of an RVec."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3a5dc0ea",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:09.126591Z",
     "iopub.status.busy": "2026-05-19T20:28:09.126465Z",
     "iopub.status.idle": "2026-05-19T20:28:09.250771Z",
     "shell.execute_reply": "2026-05-19T20:28:09.250086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Take the two first and last elements of vector { 6.0000000, 4.0000000, 5.0000000 }: { 6.0000000, 4.0000000 }, { 4.0000000, 5.0000000 }\n"
     ]
    }
   ],
   "source": [
    "v2 = Take(v1, 2)\n",
    "v3 = Take(v1, -2)\n",
    "print(\"Take the two first and last elements of vector {}: {}, {}\".format(v1, v2, v3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2164a31",
   "metadata": {},
   "source": [
    "Because the VecOps helpers return a copy of the input, you can chain the operations\n",
    "conveniently."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aaae38e8",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:28:09.252787Z",
     "iopub.status.busy": "2026-05-19T20:28:09.252660Z",
     "iopub.status.idle": "2026-05-19T20:28:09.356527Z",
     "shell.execute_reply": "2026-05-19T20:28:09.355845Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sort the vector { 6.0000000, 4.0000000, 5.0000000 }, take the two last elements and reverse the selection: { 6.0000000, 5.0000000 }\n"
     ]
    }
   ],
   "source": [
    "v2 = Reverse(Take(Sort(v1), -2))\n",
    "print(\"Sort the vector {}, take the two last elements and reverse the selection: {}\".format(v1, v2))"
   ]
  }
 ],
 "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
}
