Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/// \macro_output
9///
10/// \date May 2018
11/// \author Danilo Piparo
12
13void vo003_LogicalOperations()
14{
15
16 // Logical operations on RVec instances are made to be very easy to use.
17 ROOT::RVecD v1{1., 2., 3.};
18 ROOT::RVecD v2{3., 2., 1.};
19
20 // Let's start with operations which act element by element. In this case
21 // we expext a RVec which holds {1. > 3., 2. > 2., 3. > 1.}, i.e. {1, 0, 0}:
22 auto v1_gr_v2 = v1 > v2;
23 std::cout << v1 << " > " << v2 << " = " << v1_gr_v2 << std::endl;
24
25 // Other logical operations are supported, of course:
26 auto v1_noteq_v2 = v1 != v2;
27 std::cout << v1 << " != " << v2 << " = " << v1_noteq_v2 << std::endl;
28
29 // All returns true if all of the elements equate to true, return false otherwise.
30 // Any returns true if any of the elements equates to true, return false otherwise.
31 auto all_true = v1 > .5 * v2;
32 std::cout << std::boolalpha;
33 std::cout << "All( " << v1 << " > .5 * " << v2 << " ) = " << All(all_true) << std::endl;
34 std::cout << "Any( " << v1 << " > " << v2 << " ) = " << Any(v1_noteq_v2) << std::endl;
35
36 // Selections on the RVec contents can be applied with the "square brackets" operator,
37 // which is not only a way to access the content of the RVec.
38 // This operation can change the size of the RVec.
39 ROOT::RVecD v{1., 2., 3., 4., 5.};
40 auto v_filtered = v[v > 3.];
41 std::cout << "v = " << v << ". v[ v > 3. ] = " << v_filtered << std::endl;
42
43 // This filtering operation can be particularely useful when cleaning collections of
44 // objects coming from HEP events. For example:
45 ROOT::RVecD mu_pt{15., 12., 10.6, 2.3, 4., 3.};
46 ROOT::RVecD mu_eta{1.2, -0.2, 4.2, -5.3, 0.4, -2.};
47
48 // Suppose the pts of the muons with a pt greater than 10 and eta smaller than 2.1 are needed:
49 auto good_mu_pt = mu_pt[mu_pt > 10 && abs(mu_eta) < 2.1];
50 std::cout << "mu_pt = " << mu_pt << " mu_pt[ mu_pt > 10 && abs(mu_eta) < 2.1] = " << good_mu_pt << std::endl;
51
52 // Advanced logical operations with masking can be performed with the Where helper.
53 auto masked_mu_pt = Where(abs(mu_eta) < 2., mu_pt, -999.);
54 std::cout << "mu_pt if abs(mu_eta) < 2 else -999 = " << masked_mu_pt << std::endl;
55}
auto All(const RVec< T > &v) -> decltype(v[0]==false)
Return true if all of the elements equate to true, return false otherwise.
Definition RVec.hxx:2128
RVec< T > Where(const RVec< int > &c, const RVec< T > &v1, const RVec< T > &v2)
Return the elements of v1 if the condition c is true and v2 if the condition c is false.
Definition RVec.hxx:2637
auto Any(const RVec< T > &v) -> decltype(v[0]==true)
Return true if any of the elements equates to true, return false otherwise.
Definition RVec.hxx:2109