Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
vo006_IndexManipulation.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_vecops
3/// \notebook -nodraw
4/// In this tutorial we demonstrate RVec helpers for index manipulation.
5///
6/// \macro_code
7/// \macro_output
8///
9/// \date September 2018
10/// \author Stefan Wunsch
11
12void vo006_IndexManipulation()
13{
14 // We assume that we have multiple linked collections, the elements of which
15 // represent different objects.
16 ROOT::RVecF muon_pt = {20.0, 30.0, 10.0, 25.0};
17 ROOT::RVecF muon_eta = {1.0, -2.0, 0.5, 2.5};
18
19 for (size_t i = 0; i < muon_pt.size(); i++) {
20 std::cout << "Muon " << i + 1 << " (pt, eta): " << muon_pt[i] << ", "
21 << muon_eta[i] << std::endl;
22 }
23
24 // First, let's make a selection and write out all indices, which pass.
25 auto idx_select = Nonzero(muon_pt > 15 && abs(muon_eta) < 2.5);
26
27 // Second, get indices that sort one of the collections in descending order.
28 auto idx_sort = Reverse(Argsort(muon_pt));
29
30 // Finally, we find all indices present in both collections of indices retrieved
31 // from sorting and selecting.
32 // Note, that the order of the first list passed to the Intersect helper is
33 // contained.
34 auto idx = Intersect(idx_sort, idx_select);
35
36 // Take from all lists the elements of the passing objects.
37 auto good_muon_pt = Take(muon_pt, idx);
38 auto good_muon_eta = Take(muon_eta, idx);
39
40 for (size_t i = 0; i < idx.size(); i++) {
41 std::cout << "Selected muon " << i + 1 << " (pt, eta): " << good_muon_pt[i]
42 << ", " << good_muon_eta[i] << std::endl;
43 }
44}
RVec< T > Reverse(const RVec< T > &v)
Return copy of reversed vector.
Definition RVec.hxx:2481
RVec< T > Intersect(const RVec< T > &v1, const RVec< T > &v2, bool v2_is_sorted=false)
Return the intersection of elements of two RVecs.
Definition RVec.hxx:2758
RVec< typename RVec< T >::size_type > Nonzero(const RVec< T > &v)
Return the indices of the elements which are not zero.
Definition RVec.hxx:2727
RVec< PromoteType< T > > abs(const RVec< T > &v)
Definition RVec.hxx:1832
RVec< T > Take(const RVec< T > &v, const RVec< typename RVec< T >::size_type > &i)
Return elements of a vector at given indices.
Definition RVec.hxx:2339
RVec< typename RVec< T >::size_type > Argsort(const RVec< T > &v)
Return an RVec of indices that sort the input RVec.
Definition RVec.hxx:2250