{ "cells": [ { "cell_type": "markdown", "id": "c2dc482b", "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", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:28 PM." ] }, { "cell_type": "markdown", "id": "0113bc39", "metadata": {}, "source": [ "Because RVec implements an iterator, the class is fully compatible with\n", "the sorting algorithms in the standard library." ] }, { "cell_type": "code", "execution_count": 1, "id": "0fdcef9e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:04.782176Z", "iopub.status.busy": "2026-05-19T20:28:04.782067Z", "iopub.status.idle": "2026-05-19T20:28:05.101199Z", "shell.execute_reply": "2026-05-19T20:28:05.100539Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sort vector { 6, 4, 5 }: { 4, 5, 6 }\n" ] } ], "source": [ "ROOT::RVecD v1{6., 4., 5.};\n", "ROOT::RVecD v2(v1);\n", "std::sort(v2.begin(), v2.end());\n", "std::cout << \"Sort vector \" << v1 << \": \" << v2 << std::endl;" ] }, { "cell_type": "markdown", "id": "4843faff", "metadata": {}, "source": [ "For convenience, ROOT implements helpers, e.g., to get a sorted copy of\n", "an RVec ..." ] }, { "cell_type": "code", "execution_count": 2, "id": "fb4ead97", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:05.102907Z", "iopub.status.busy": "2026-05-19T20:28:05.102783Z", "iopub.status.idle": "2026-05-19T20:28:05.310272Z", "shell.execute_reply": "2026-05-19T20:28:05.309695Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:2:12: error: cannot deduce 'auto' from unknown expression\n", " auto v3 = Sort(v1);\n", " ^\n" ] } ], "source": [ "auto v3 = Sort(v1);\n", "std::cout << \"Sort vector \" << v1 << \": \" << v3 << std::endl;" ] }, { "cell_type": "markdown", "id": "2c4d517a", "metadata": {}, "source": [ "... or a reversed copy of an RVec." ] }, { "cell_type": "code", "execution_count": 3, "id": "279efcc7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:05.311655Z", "iopub.status.busy": "2026-05-19T20:28:05.311513Z", "iopub.status.idle": "2026-05-19T20:28:05.519086Z", "shell.execute_reply": "2026-05-19T20:28:05.518432Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:12: error: cannot deduce 'auto' from unknown expression\n", " auto v4 = Reverse(v1);\n", " ^\n" ] } ], "source": [ "auto v4 = Reverse(v1);\n", "std::cout << \"Reverse vector \" << v1 << \": \" << v4 << std::endl;" ] }, { "cell_type": "markdown", "id": "0d13662a", "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": 4, "id": "00f5d912", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:05.520386Z", "iopub.status.busy": "2026-05-19T20:28:05.520268Z", "iopub.status.idle": "2026-05-19T20:28:05.727692Z", "shell.execute_reply": "2026-05-19T20:28:05.727111Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:11: error: cannot deduce 'auto' from unknown expression\n", " auto i = Argsort(v1);\n", " ^\n" ] } ], "source": [ "auto i = Argsort(v1);\n", "std::cout << \"Indices that sort the vector \" << v1 << \": \" << i << std::endl;\n", "\n", "ROOT::RVecD v5{9., 7., 8.};\n", "auto v6 = Take(v5, i);\n", "std::cout << \"Sort vector \" << v5 << \" respective to the previously\"\n", " << \" determined indices: \" << v6 << std::endl;" ] }, { "cell_type": "markdown", "id": "4da8272e", "metadata": {}, "source": [ "Take can also be used to get the first or last elements of an RVec." ] }, { "cell_type": "code", "execution_count": 5, "id": "ed872544", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:05.729043Z", "iopub.status.busy": "2026-05-19T20:28:05.728925Z", "iopub.status.idle": "2026-05-19T20:28:05.936338Z", "shell.execute_reply": "2026-05-19T20:28:05.935782Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:2:12: error: cannot deduce 'auto' from unknown expression\n", " auto v7 = Take(v1, 2);\n", " ^\n" ] } ], "source": [ "auto v7 = Take(v1, 2);\n", "auto v8 = Take(v1, -2);\n", "std::cout << \"Take the two first and last elements of vector \" << v1\n", " << \": \" << v7 << \", \" << v8 << std::endl;" ] }, { "cell_type": "markdown", "id": "42114885", "metadata": {}, "source": [ "Because the helpers return a copy of the input, you can chain the operations\n", "conveniently." ] }, { "cell_type": "code", "execution_count": 6, "id": "a114f66a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:05.937992Z", "iopub.status.busy": "2026-05-19T20:28:05.937870Z", "iopub.status.idle": "2026-05-19T20:28:06.145365Z", "shell.execute_reply": "2026-05-19T20:28:06.144769Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_57:2:12: error: cannot deduce 'auto' from unknown expression\n", " auto v9 = Reverse(Take(Sort(v1), -2));\n", " ^\n" ] } ], "source": [ "auto v9 = Reverse(Take(Sort(v1), -2));\n", "std::cout << \"Sort the vector \" << v1 << \", take the two last elements and \"\n", " << \"reverse the selection: \" << v9 << std::endl;" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }