Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
12 using namespace ROOT::VecOps;
13 
14 void vo006_IndexManipulation()
15 {
16  // We assume that we have multiple linked collections, the elements of which
17  // represent different objects.
18  RVec<float> muon_pt = {20.0, 30.0, 10.0, 25.0};
19  RVec<float> muon_eta = {1.0, -2.0, 0.5, 2.5};
20 
21  for (size_t i = 0; i < muon_pt.size(); i++) {
22  std::cout << "Muon " << i + 1 << " (pt, eta): " << muon_pt[i] << ", "
23  << muon_eta[i] << std::endl;
24  }
25 
26  // First, let's make a selection and write out all indices, which pass.
27  auto idx_select = Nonzero(muon_pt > 15 && abs(muon_eta) < 2.5);
28 
29  // Second, get indices that sort one of the collections in descending order.
30  auto idx_sort = Reverse(Argsort(muon_pt));
31 
32  // Finally, we find all indices present in both collections of indices retrieved
33  // from sorting and selecting.
34  // Note, that the order of the first list passed to the Intersect helper is
35  // contained.
36  auto idx = Intersect(idx_sort, idx_select);
37 
38  // Take from all lists the elements of the passing objects.
39  auto good_muon_pt = Take(muon_pt, idx);
40  auto good_muon_eta = Take(muon_eta, idx);
41 
42  for (size_t i = 0; i < idx.size(); i++) {
43  std::cout << "Selected muon " << i + 1 << " (pt, eta): " << good_muon_pt[i]
44  << ", " << good_muon_eta[i] << std::endl;
45  }
46 }