Logo ROOT  
Reference Guide
RDFHelpers.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 02/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11// This header contains helper free functions that slim down RDataFrame's programming model
12
13#ifndef ROOT_RDF_HELPERS
14#define ROOT_RDF_HELPERS
15
16#include <ROOT/RDataFrame.hxx>
19#include <ROOT/TypeTraits.hxx>
20
21#include <algorithm> // std::transform
22#include <functional>
23#include <type_traits>
24#include <vector>
25#include <memory>
26#include <fstream>
27#include <iostream>
28
29namespace ROOT {
30namespace Internal {
31namespace RDF {
32template <typename... ArgTypes, typename F>
34{
35 return std::function<bool(ArgTypes...)>([=](ArgTypes... args) mutable { return !f(args...); });
36}
37
38template <typename... ArgTypes, typename Ret, typename... Args>
40{
41 return std::function<bool(ArgTypes...)>([=](ArgTypes... args) mutable { return !f(args...); });
42}
43
44template <typename I, typename T, typename F>
46
47template <std::size_t... N, typename T, typename F>
48class PassAsVecHelper<std::index_sequence<N...>, T, F> {
49 template <std::size_t Idx>
50 using AlwaysT = T;
51 F fFunc;
52
53public:
54 PassAsVecHelper(F &&f) : fFunc(std::forward<F>(f)) {}
55 auto operator()(AlwaysT<N>... args) -> decltype(fFunc({args...})) { return fFunc({args...}); }
56};
57
58template <std::size_t N, typename T, typename F>
60{
61 return PassAsVecHelper<std::make_index_sequence<N>, T, F>(std::forward<F>(f));
62}
63
64} // namespace RDF
65} // namespace Internal
66
67namespace RDF {
69
70
71// clag-format off
72/// Given a callable with signature bool(T1, T2, ...) return a callable with same signature that returns the negated result
73///
74/// The callable must have one single non-template definition of operator(). This is a limitation with respect to
75/// std::not_fn, required for interoperability with RDataFrame.
76// clang-format on
77template <typename F,
78 typename Args = typename ROOT::TypeTraits::CallableTraits<typename std::decay<F>::type>::arg_types_nodecay,
80auto Not(F &&f) -> decltype(RDFInternal::NotHelper(Args(), std::forward<F>(f)))
81{
82 static_assert(std::is_same<Ret, bool>::value, "RDF::Not requires a callable that returns a bool.");
83 return RDFInternal::NotHelper(Args(), std::forward<F>(f));
84}
85
86// clang-format off
87/// PassAsVec is a callable generator that allows passing N variables of type T to a function as a single collection.
88///
89/// PassAsVec<N, T>(func) returns a callable that takes N arguments of type T, passes them down to function `func` as
90/// an initializer list `{t1, t2, t3,..., tN}` and returns whatever f({t1, t2, t3, ..., tN}) returns.
91///
92/// Note that for this to work with RDataFrame the type of all columns that the callable is applied to must be exactly T.
93/// Example usage together with RDataFrame ("varX" columns must all be `float` variables):
94/// \code
95/// bool myVecFunc(std::vector<float> args);
96/// df.Filter(PassAsVec<3, float>(myVecFunc), {"var1", "var2", "var3"});
97/// \endcode
98// clang-format on
99template <std::size_t N, typename T, typename F>
101{
103}
104template <typename Proxied, typename DataSource>
105class RInterface;
106
107
108// clang-format off
109/// Create a graphviz representation of the dataframe computation graph, return it as a string.
110/// \param[in] node any node of the graph. Called on the head (first) node, it prints the entire graph. Otherwise, only the branch the node belongs to.
111// clang-format on
112template <typename NodeType>
113std::string SaveGraph(NodeType node)
114{
116 return helper(node);
117}
118
119// clang-format off
120/// Create a graphviz representation of the dataframe computation graph, write it to the specified file.
121/// \param[in] node any node of the graph. Called on the head (first) node, it prints the entire graph. Otherwise, only the branch the node belongs to.
122/// \param[in] outputFile file where to save the representation.
123// clang-format on
124template <typename NodeType>
125void SaveGraph(NodeType node, const std::string &outputFile)
126{
128 std::string dotGraph = helper(node);
129
130 std::ofstream out(outputFile);
131 if (!out.is_open()) {
132 throw std::runtime_error("Could not open output file \"" + outputFile + "\"for reading");
133 }
134
135 out << dotGraph;
136 out.close();
137}
138
139// clang-format off
140/// Cast a RDataFrame node to the common type ROOT::RDF::RNode
141/// \param[in] Any node of a RDataFrame graph
142// clang-format on
143template <typename NodeType>
144RNode AsRNode(NodeType node)
145{
146 return node;
147}
148
149} // namespace RDF
150} // namespace ROOT
151#endif
#define f(i)
Definition: RSha256.hxx:104
#define N
int type
Definition: TGX11.cxx:120
TRObject operator()(const T1 &t1) const
The public interface to the RDataFrame federation of classes.
Definition: RInterface.hxx:89
#define F(x, y, z)
std::function< bool(ArgTypes...)> NotHelper(ROOT::TypeTraits::TypeList< ArgTypes... >, Ret(*f)(Args...))
Definition: RDFHelpers.hxx:39
std::function< bool(ArgTypes...)> NotHelper(ROOT::TypeTraits::TypeList< ArgTypes... >, F &&f)
Definition: RDFHelpers.hxx:33
auto PassAsVec(F &&f) -> PassAsVecHelper< std::make_index_sequence< N >, T, F >
Definition: RDFHelpers.hxx:59
double T(double x)
Definition: ChebyshevPol.h:34
auto PassAsVec(F &&f) -> RDFInternal::PassAsVecHelper< std::make_index_sequence< N >, T, F >
PassAsVec is a callable generator that allows passing N variables of type T to a function as a single...
Definition: RDFHelpers.hxx:100
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 ...
Definition: RDFHelpers.hxx:80
std::string SaveGraph(NodeType node)
Create a graphviz representation of the dataframe computation graph, return it as a string.
Definition: RDFHelpers.hxx:113
RNode AsRNode(NodeType node)
Cast a RDataFrame node to the common type ROOT::RDF::RNode.
Definition: RDFHelpers.hxx:144
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25