Logo ROOT   6.14/05
Reference Guide
vo003_LogicalOperations.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_vecops
3 /// \notebook -nodraw
4 /// In this tutorial we learn how the RVec class can be used to
5 /// express logical operations.
6 ///
7 /// \macro_code
8 ///
9 /// \date May 2018
10 /// \author Danilo Piparo
11 
12 using namespace ROOT::VecOps;
13 
14 void vo003_LogicalOperations()
15 {
16 
17  // Logical operations on RVec instances are made to be very easy to use.
18  RVec<double> v1{1., 2., 3.};
19  RVec<double> v2{3., 2., 1.};
20 
21  // Let's start with operations which act element by element. In this case
22  // we expext a RVec which holds {1. > 3., 2. > 2., 3. > 1.}, i.e. {1, 0, 0}:
23  auto v1_gr_v2 = v1 > v2;
24  std::cout << v1 << " > " << v2 << " = " << v1_gr_v2 << std::endl;
25 
26  // Other logical operations are supported, of course:
27  auto v1_noteq_v2 = v1 != v2;
28  std::cout << v1 << " != " << v2 << " = " << v1_noteq_v2 << std::endl;
29 
30  // Selections on the RVec contents can be applied with the "square brackets" operator,
31  // which is not only a way to access the content of the RVec.
32  // This operation can change the size of the RVec.
33  RVec<double> v{1., 2., 3., 4., 5.};
34  auto v_filtered = v[v > 3.];
35  std::cout << "v = " << v << ". v[ v > 3. ] = " << v_filtered << std::endl;
36 
37  // This filtering operation can be particularely useful when cleaning collections of
38  // objects coming from HEP events. For example:
39  RVec<double> mu_pt{15., 12., 10.6, 2.3, 4., 3.};
40  RVec<double> mu_eta{1.2, -0.2, 4.2, -5.3, 0.4, -2.};
41 
42  // Suppose the pts of the muons with a pt greater than 10 and eta smaller than 2.1 are needed:
43  auto good_mu_pt = mu_pt[mu_pt > 10 && abs(mu_eta) < 2.1];
44  std::cout << "mu_pt = " << mu_pt << " mu_pt[ mu_pt > 10 && abs(mu_eta) < 2.1] = " << good_mu_pt << std::endl;
45 }
A "std::vector"-like collection of values implementing handy operation to analyse them...
Definition: RVec.hxx:146
SVector< double, 2 > v
Definition: Dict.h:5