Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df020_helpers.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook
4/// Show usage of RDataFrame's helper tools, contained in ROOT/RDFHelpers.hxx.
5///
6/// \macro_code
7///
8/// \date July 2018
9/// \author Enrico Guiraud (CERN)
10
11void df020_helpers()
12{
13 // First of all, we create a dataframe with 3 entries and define two simple columns
14 const auto nEntries = 3;
15 ROOT::RDataFrame _df(nEntries);
16 auto df = _df.Define("one", [] { return 1; }).Define("two", [] { return 2; });
17
18 // *** Not ***
19 // This helper takes a callable `f` (which must return a `bool`) and produces a new callable which takes the same
20 // arguments as `f` but returns its negated result. `Not` is useful to invert the check performed by a given Filter.
21 // Here we define a simple lambda that checks whether a value is equal to 1, and invert it with Not:
22 auto isOne = [] (int a) { return a == 1; };
23 auto isNotOne = ROOT::RDF::Not(isOne);
24
25 // Both `isOne` and `isNotOne` are callables that we can use in `Filters`:
26 auto c1 = df.Filter(isOne, {"one"}).Count();
27 auto c2 = df.Filter(isNotOne, {"two"}).Count();
28 // Both counts are equal to the total number of entries, as both Filters always pass.
29 R__ASSERT(*c1 == nEntries);
30 R__ASSERT(*c2 == nEntries);
31
32 // *** PassAsVec ***
33 // Consider the following function, which checks if a vector consists of two elements equal to 1 and 2:
34 auto checkOneTwo = [] (const std::vector<int> &v) { return v.size() == 2 && v[0] == 1 && v[1] == 2; };
35 // The following line, although it looks reasonable, would _not_ run correctly:
36 // df.Filter(checkOneTwo, {"one", "two"});
37 // The reason is that `Filter(..., {"one", "two"})` expects a callable that takes exactly two integers, while
38 // `checkOneTwo` actually takes a vector of integers (i.e. it does not have the right signature).
39 // PassAsVec helps passing down the single values "one", "two" to `checkOneTwo` as a collection: it takes a callable
40 // `f` that expects a collection as argument and returns a new callable that takes single arguments instead, passes
41 // them down to `f` and returns what `f` returns.
42 // PassAsVec requires that number of arguments and their type is specified as template argument.
43 // Here's an example usage (remember, PassAsVec(f) returns a new callable!):
44 auto c3 = df.Filter(ROOT::RDF::PassAsVec<2, int>(checkOneTwo), {"one", "two"}).Count();
45 R__ASSERT(*c3 == nEntries);
46}
#define a(i)
Definition RSha256.hxx:99
#define R__ASSERT(e)
Definition TError.h:117
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
return c1
Definition legend1.C:41
return c2
Definition legend2.C:14
return c3
Definition legend3.C:15
auto Not(F &&f) -> decltype(RDFInternal::NotHelper(Args(), std::forward< F >(f)))
Given a callable with signature bool(T1, T2, ...) return a callable with same signature that returns ...