{ "cells": [ { "cell_type": "markdown", "id": "6a7294c3", "metadata": {}, "source": [ "# vo006_IndexManipulation\n", "In this tutorial we demonstrate RVec helpers for index manipulation.\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": "6640d3ec", "metadata": {}, "source": [ "We assume that we have multiple linked collections, the elements of which\n", "represent different objects." ] }, { "cell_type": "code", "execution_count": 1, "id": "9b665a15", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:14.025004Z", "iopub.status.busy": "2026-05-19T20:28:14.024892Z", "iopub.status.idle": "2026-05-19T20:28:14.333892Z", "shell.execute_reply": "2026-05-19T20:28:14.333236Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Muon 1 (pt, eta): 20, 1\n", "Muon 2 (pt, eta): 30, -2\n", "Muon 3 (pt, eta): 10, 0.5\n", "Muon 4 (pt, eta): 25, 2.5\n" ] } ], "source": [ "ROOT::RVecF muon_pt = {20.0, 30.0, 10.0, 25.0};\n", "ROOT::RVecF muon_eta = {1.0, -2.0, 0.5, 2.5};\n", "\n", "for (size_t i = 0; i < muon_pt.size(); i++) {\n", " std::cout << \"Muon \" << i + 1 << \" (pt, eta): \" << muon_pt[i] << \", \"\n", " << muon_eta[i] << std::endl;\n", "}" ] }, { "cell_type": "markdown", "id": "4be77c8b", "metadata": {}, "source": [ "First, let's make a selection and write out all indices, which pass." ] }, { "cell_type": "code", "execution_count": 2, "id": "fb8b746d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:14.335479Z", "iopub.status.busy": "2026-05-19T20:28:14.335358Z", "iopub.status.idle": "2026-05-19T20:28:14.542826Z", "shell.execute_reply": "2026-05-19T20:28:14.542212Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:2:20: error: cannot deduce 'auto' from unknown expression\n", " auto idx_select = Nonzero(muon_pt > 15 && abs(muon_eta) < 2.5);\n", " ^\n" ] } ], "source": [ "auto idx_select = Nonzero(muon_pt > 15 && abs(muon_eta) < 2.5);" ] }, { "cell_type": "markdown", "id": "052c0743", "metadata": {}, "source": [ "Second, get indices that sort one of the collections in descending order." ] }, { "cell_type": "code", "execution_count": 3, "id": "a2938e36", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:14.544595Z", "iopub.status.busy": "2026-05-19T20:28:14.544475Z", "iopub.status.idle": "2026-05-19T20:28:14.751952Z", "shell.execute_reply": "2026-05-19T20:28:14.751360Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_54:2:18: error: cannot deduce 'auto' from unknown expression\n", " auto idx_sort = Reverse(Argsort(muon_pt));\n", " ^\n" ] } ], "source": [ "auto idx_sort = Reverse(Argsort(muon_pt));" ] }, { "cell_type": "markdown", "id": "269eb127", "metadata": {}, "source": [ "Finally, we find all indices present in both collections of indices retrieved\n", "from sorting and selecting.\n", "Note, that the order of the first list passed to the Intersect helper is\n", "contained." ] }, { "cell_type": "code", "execution_count": 4, "id": "fc993457", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:14.753762Z", "iopub.status.busy": "2026-05-19T20:28:14.753639Z", "iopub.status.idle": "2026-05-19T20:28:14.961091Z", "shell.execute_reply": "2026-05-19T20:28:14.960467Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:13: error: cannot deduce 'auto' from unknown expression\n", " auto idx = Intersect(idx_sort, idx_select);\n", " ^\n" ] } ], "source": [ "auto idx = Intersect(idx_sort, idx_select);" ] }, { "cell_type": "markdown", "id": "739873af", "metadata": {}, "source": [ "Take from all lists the elements of the passing objects." ] }, { "cell_type": "code", "execution_count": 5, "id": "4dbf3290", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:28:14.962657Z", "iopub.status.busy": "2026-05-19T20:28:14.962519Z", "iopub.status.idle": "2026-05-19T20:28:15.165298Z", "shell.execute_reply": "2026-05-19T20:28:15.164637Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:5:24: error: use of undeclared identifier 'idx'\n", "for (size_t i = 0; i < idx.size(); i++) {\n", " ^\n", "input_line_56:2:22: error: cannot deduce 'auto' from unknown expression\n", " auto good_muon_pt = Take(muon_pt, idx);\n", " ^\n" ] } ], "source": [ "auto good_muon_pt = Take(muon_pt, idx);\n", "auto good_muon_eta = Take(muon_eta, idx);\n", "\n", "for (size_t i = 0; i < idx.size(); i++) {\n", " std::cout << \"Selected muon \" << i + 1 << \" (pt, eta): \" << good_muon_pt[i]\n", " << \", \" << good_muon_eta[i] << std::endl;\n", "}" ] } ], "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 }