Df 0 1 6_Vec Ops

This tutorial shows the potential of the VecOps approach for treating collections stored in datasets, a situation very common in HEP data analysis.

Author: Danilo Piparo
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 27, 2020 at 01:12 AM.

In [1]:
using ROOT::RDataFrame;
using namespace ROOT::VecOps;

We re-create a set of points in a square. This is a technical detail, just to create a dataset to play with!

In [2]:
auto unifGen = [](double) { return gRandom->Uniform(-1.0, 1.0); };
auto vGen = [&](int len) {
   RVec<double> v(len);
   std::transform(v.begin(), v.end(), v.begin(), unifGen);
   return v;
};
RDataFrame d(1024);
auto d0 = d.Define("len", []() { return (int)gRandom->Uniform(0, 16); })
   .Define("x", vGen, {"len"})
   .Define("y", vGen, {"len"});
input_line_132:4:4: error: use of undeclared identifier 'RVec'
   RVec<double> v(len);
   ^
input_line_132:4:15: error: expected '(' for function-style cast or type construction
   RVec<double> v(len);
        ~~~~~~^
input_line_132:4:17: error: use of undeclared identifier 'v'
   RVec<double> v(len);
                ^
input_line_132:5:19: error: use of undeclared identifier 'v'
   std::transform(v.begin(), v.end(), v.begin(), unifGen);
                  ^
input_line_132:5:30: error: use of undeclared identifier 'v'
   std::transform(v.begin(), v.end(), v.begin(), unifGen);
                             ^
input_line_132:5:39: error: use of undeclared identifier 'v'
   std::transform(v.begin(), v.end(), v.begin(), unifGen);
                                      ^
input_line_132:6:11: error: use of undeclared identifier 'v'
   return v;
          ^
In module 'ROOTDataFrame' imported from input_line_83:1:
/mnt/build/workspace/root-makedoc/rootspi/rdoc/src/master.build/include/ROOT/RDF/RInterface.hxx:2400:7: error: static_assert failed "Error in `Define`: type returned by expression is not default-constructible"
      static_assert(std::is_default_constructible<typename TTraits::CallableTraits<F>::ret_type>::value,
      ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mnt/build/workspace/root-makedoc/rootspi/rdoc/src/master.build/include/ROOT/RDF/RInterface.hxx:297:14: note: in instantiation of function template specialization 'ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager, void>::DefineImpl<(lambda at input_line_132:3:13), ROOT::Detail::RDF::CustomColExtraArgs::None, void, false, false>' requested here
      return DefineImpl<F, RDFDetail::CustomColExtraArgs::None>(name, std::move(expression), columns);
             ^
input_line_132:10:5: note: in instantiation of function template specialization 'ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager, void>::Define<(lambda at input_line_132:3:13), 0>' requested here
   .Define("x", vGen, {"len"})
    ^

Now we have in hands d, a rdataframe with two columns, x and y, which hold collections of coordinates. The size of these collections vary. Let's now define radii out of x and y. We'll do it treating the collections stored in the columns without looping on the individual elements.

In [3]:
auto d1 = d0.Define("r", "sqrt(x*x + y*y)");
input_line_133:2:2: error: Syntax error
 auto d1 = d0.Define("r", "sqrt(x*x + y*y)");
 ^
FunctionDecl 0x7fed7081bf18 <input_line_133:1:1, line:4:1> line:1:6 __cling_Un1Qu34 'void (void *)'
|-ParmVarDecl 0x7fed7081be68 <col:22, col:28> col:28 vpClingValue 'void *'
|-CompoundStmt 0x7fed7081c288 <col:42, line:4:1>
| |-DeclStmt 0x7fed7081c260 <line:2:2, col:45>
| | `-VarDecl 0x7fed7081bff0 <col:2, col:44> col:7 d1 'auto' cinit
| |   `-CallExpr 0x7fed7081c228 <col:12, col:44> '<dependent type>'
| |     |-CXXDependentScopeMemberExpr 0x7fed7081c168 <col:12, col:15> '<dependent type>' lvalue .Define
| |     | `-DeclRefExpr 0x7fed7081c120 <col:12> '<dependent type>' lvalue Var 0x7fed7081c058 'd0' '<dependent type>'
| |     |-StringLiteral 0x7fed7081c1c0 <col:22> 'const char [2]' lvalue "r"
| |     `-StringLiteral 0x7fed7081c1f0 <col:27> 'const char [16]' lvalue "sqrt(x*x + y*y)"
| `-NullStmt 0x7fed7081c278 <line:3:1>
`-AnnotateAttr 0x7fed7081c0b8 <<invalid sloc>> R"ATTRDUMP(__ResolveAtRuntime)ATTRDUMP"
<<<NULL>>>

Now we want to plot 2 quarters of a ring with radii .5 and 1 Note how the cuts are performed on RVecs, comparing them with integers and among themselves

In [4]:
auto ring_h = d1.Define("rInFig", "r > .4 && r < .8 && x*y < 0")
                 .Define("yFig", "y[rInFig]")
                 .Define("xFig", "x[rInFig]")
                 .Histo2D({"fig", "Two quarters of a ring", 64, -1, 1, 64, -1, 1}, "xFig", "yFig");

auto cring = new TCanvas();
ring_h->DrawCopy("Colz");

return 0;
input_line_134:2:2: error: Syntax error
 auto ring_h = d1.Define("rInFig", "r > .4 && r < .8 && x*y < 0")
 ^
FunctionDecl 0x7fed7194c680 <input_line_134:1:1, line:12:1> line:1:6 __cling_Un1Qu35 'void (void *)'
|-ParmVarDecl 0x7fed7194c5d0 <col:22, col:28> col:28 vpClingValue 'void *'
|-CompoundStmt 0x7fed719553f8 <col:42, line:12:1>
| |-DeclStmt 0x7fed7194ceb0 <line:2:2, line:5:99>
| | `-VarDecl 0x7fed7194c758 <line:2:2, line:5:98> line:2:7 used ring_h 'auto' cinit
| |   `-CallExpr 0x7fed7194ce70 <col:16, line:5:98> '<dependent type>'
| |     |-CXXDependentScopeMemberExpr 0x7fed7194cbc8 <line:2:16, line:5:19> '<dependent type>' lvalue .Histo2D
| |     | `-CallExpr 0x7fed7194cb90 <line:2:16, line:4:45> '<dependent type>'
| |     |   |-CXXDependentScopeMemberExpr 0x7fed7194cad0 <line:2:16, line:4:19> '<dependent type>' lvalue .Define
| |     |   | `-CallExpr 0x7fed7194ca98 <line:2:16, line:3:45> '<dependent type>'
| |     |   |   |-CXXDependentScopeMemberExpr 0x7fed7194c9d8 <line:2:16, line:3:19> '<dependent type>' lvalue .Define
| |     |   |   | `-CallExpr 0x7fed7194c9a0 <line:2:16, col:65> '<dependent type>'
| |     |   |   |   |-CXXDependentScopeMemberExpr 0x7fed7194c8d0 <col:16, col:19> '<dependent type>' lvalue .Define
| |     |   |   |   | `-DeclRefExpr 0x7fed7194c888 <col:16> '<dependent type>' lvalue Var 0x7fed7194c7c0 'd1' '<dependent type>'
| |     |   |   |   |-StringLiteral 0x7fed7194c928 <col:26> 'const char [7]' lvalue "rInFig"
| |     |   |   |   `-StringLiteral 0x7fed7194c958 <col:36> 'const char [28]' lvalue "r > .4 && r < .8 && x*y < 0"
| |     |   |   |-StringLiteral 0x7fed7194ca30 <line:3:26> 'const char [5]' lvalue "yFig"
| |     |   |   `-StringLiteral 0x7fed7194ca60 <col:34> 'const char [10]' lvalue "y[rInFig]"
| |     |   |-StringLiteral 0x7fed7194cb28 <line:4:26> 'const char [5]' lvalue "xFig"
| |     |   `-StringLiteral 0x7fed7194cb58 <col:34> 'const char [10]' lvalue "x[rInFig]"
| |     |-InitListExpr 0x7fed7194cd90 <line:5:27, col:81> 'void'
| |     | |-StringLiteral 0x7fed7194cc20 <col:28> 'const char [4]' lvalue "fig"
| |     | |-StringLiteral 0x7fed7194cc50 <col:35> 'const char [23]' lvalue "Two quarters of a ring"
| |     | |-IntegerLiteral 0x7fed7194cc90 <col:61> 'int' 64
| |     | |-UnaryOperator 0x7fed7194ccd0 <col:65, col:66> 'int' prefix '-'
| |     | | `-IntegerLiteral 0x7fed7194ccb0 <col:66> 'int' 1
| |     | |-IntegerLiteral 0x7fed7194ccf0 <col:69> 'int' 1
| |     | |-IntegerLiteral 0x7fed7194cd10 <col:72> 'int' 64
| |     | |-UnaryOperator 0x7fed7194cd50 <col:76, col:77> 'int' prefix '-'
| |     | | `-IntegerLiteral 0x7fed7194cd30 <col:77> 'int' 1
| |     | `-IntegerLiteral 0x7fed7194cd70 <col:80> 'int' 1
| |     |-StringLiteral 0x7fed7194ce10 <col:84> 'const char [5]' lvalue "xFig"
| |     `-StringLiteral 0x7fed7194ce40 <col:92> 'const char [5]' lvalue "yFig"
| |-DeclStmt 0x7fed71955280 <line:7:1, col:27>
| | `-VarDecl 0x7fed7194cee0 <col:1, col:26> col:6 cring 'class TCanvas *':'class TCanvas *' cinit
| |   `-CXXNewExpr 0x7fed71955188 <col:14, col:26> 'class TCanvas *' CXXMethod 0x55e8881f6598 'operator new' 'void *(size_t)'
| |     `-CXXConstructExpr 0x7fed71954e78 <col:18, col:26> 'class TCanvas' 'void (Bool_t)'
| |       `-CXXDefaultArgExpr 0x7fed71954e58 <<invalid sloc>> 'Bool_t':'_Bool'
| |-CallExpr 0x7fed71955368 <line:8:1, col:24> '<dependent type>'
| | |-CXXDependentScopeMemberExpr 0x7fed719552e0 <col:1, col:9> '<dependent type>' lvalue ->DrawCopy
| | | `-DeclRefExpr 0x7fed71955298 <col:1> 'auto' lvalue Var 0x7fed7194c758 'ring_h' 'auto'
| | `-StringLiteral 0x7fed71955338 <col:18> 'const char [5]' lvalue "Colz"
| |-ReturnStmt 0x7fed719553d0 <line:10:1, col:8>
| | `-ImplicitCastExpr 0x7fed719553b8 <col:8> 'void' <ToVoid>
| |   `-IntegerLiteral 0x7fed71955398 <col:8> 'int' 0
| `-NullStmt 0x7fed719553e8 <line:11:1>
`-AnnotateAttr 0x7fed7194c820 <<invalid sloc>> R"ATTRDUMP(__ResolveAtRuntime)ATTRDUMP"
<<<NULL>>>

Draw all canvases

In [5]:
gROOT->GetListOfCanvases()->Draw()