Logo ROOT  
Reference Guide
Utils.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 12/2016
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#ifndef ROOT_RDFUTILS
12#define ROOT_RDFUTILS
13
14#include "ROOT/RDataSource.hxx" // ColumnName2ColumnTypeName
15#include "ROOT/TypeTraits.hxx"
16#include "ROOT/RVec.hxx"
18#include "ROOT/RSpan.hxx" // for IsDataContainer
19#include "TH1.h"
20
21#include <array>
22#include <deque>
23#include <functional>
24#include <memory>
25#include <string>
26#include <type_traits> // std::decay
27
28class TTree;
29class TTreeReader;
30
31/// \cond HIDDEN_SYMBOLS
32
33namespace ROOT {
34
35namespace Detail {
36namespace RDF {
37using ColumnNames_t = std::vector<std::string>;
38
39// fwd decl for ColumnName2ColumnTypeName
40class RCustomColumnBase;
41
42// type used for tag dispatching
43struct RInferredType {
44};
45
46} // end ns Detail
47} // end ns RDF
48
49namespace Internal {
50namespace RDF {
51using namespace ROOT::TypeTraits;
52using namespace ROOT::Detail::RDF;
53using namespace ROOT::RDF;
54
55/// Check for container traits.
56///
57/// Note that for all uses in RDF we don't want to classify std::string as a container.
58/// Template specializations of IsDataContainer make it return `true` for std::span<T>, std::vector<bool> and
59/// RVec<bool>, which we do want to count as containers even though they do not satisfy all the traits tested by the
60/// generic IsDataContainer<T>.
61template <typename T>
62struct IsDataContainer {
63 using Test_t = typename std::decay<T>::type;
64
65 template <typename A>
66 static constexpr bool Test(A *pt, A const *cpt = nullptr, decltype(pt->begin()) * = nullptr,
67 decltype(pt->end()) * = nullptr, decltype(cpt->begin()) * = nullptr,
68 decltype(cpt->end()) * = nullptr, typename A::iterator *pi = nullptr,
69 typename A::const_iterator *pci = nullptr)
70 {
71 using It_t = typename A::iterator;
72 using CIt_t = typename A::const_iterator;
73 using V_t = typename A::value_type;
74 return std::is_same<decltype(pt->begin()), It_t>::value && std::is_same<decltype(pt->end()), It_t>::value &&
75 std::is_same<decltype(cpt->begin()), CIt_t>::value && std::is_same<decltype(cpt->end()), CIt_t>::value &&
76 std::is_same<decltype(**pi), V_t &>::value && std::is_same<decltype(**pci), V_t const &>::value &&
77 !std::is_same<T, std::string>::value;
78 }
79
80 template <typename A>
81 static constexpr bool Test(...)
82 {
83 return false;
84 }
85
86 static constexpr bool value = Test<Test_t>(nullptr);
87};
88
89template<>
90struct IsDataContainer<std::vector<bool>> {
91 static constexpr bool value = true;
92};
93
94template<>
95struct IsDataContainer<ROOT::VecOps::RVec<bool>> {
96 static constexpr bool value = true;
97};
98
99template<typename T>
100struct IsDataContainer<std::span<T>> {
101 static constexpr bool value = true;
102};
103
104/// Detect whether a type is an instantiation of vector<T,A>
105template <typename>
106struct IsVector_t : public std::false_type {};
107
108template <typename T, typename A>
109struct IsVector_t<std::vector<T, A>> : public std::true_type {};
110
111const std::type_info &TypeName2TypeID(const std::string &name);
112
113std::string TypeID2TypeName(const std::type_info &id);
114
115std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RCustomColumnBase *,
116 bool vector2rvec = true);
117
118char TypeName2ROOTTypeName(const std::string &b);
119
120unsigned int GetNSlots();
121
122/// `type` is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
123template <bool MustRemove, typename TypeList>
124struct RemoveFirstParameterIf {
125 using type = TypeList;
126};
127
128template <typename TypeList>
129struct RemoveFirstParameterIf<true, TypeList> {
130 using type = RemoveFirstParameter_t<TypeList>;
131};
132
133template <bool MustRemove, typename TypeList>
134using RemoveFirstParameterIf_t = typename RemoveFirstParameterIf<MustRemove, TypeList>::type;
135
136template <bool MustRemove, typename TypeList>
137struct RemoveFirstTwoParametersIf {
138 using type = TypeList;
139};
140
141template <typename TypeList>
142struct RemoveFirstTwoParametersIf<true, TypeList> {
143 using typeTmp = typename RemoveFirstParameterIf<true, TypeList>::type;
145};
146
147template <bool MustRemove, typename TypeList>
148using RemoveFirstTwoParametersIf_t = typename RemoveFirstTwoParametersIf<MustRemove, TypeList>::type;
149
150/// Detect whether a type is an instantiation of RVec<T>
151template <typename>
152struct IsRVec_t : public std::false_type {};
153
154template <typename T>
155struct IsRVec_t<ROOT::VecOps::RVec<T>> : public std::true_type {};
156
157// Check the value_type type of a type with a SFINAE to allow compilation in presence
158// fundamental types
160struct ValueType {
161 using value_type = typename T::value_type;
162};
163
164template <typename T>
165struct ValueType<T, false> {
166 using value_type = T;
167};
168
169template <typename T>
170struct ValueType<ROOT::VecOps::RVec<T>, false> {
171 using value_type = T;
172};
173
174std::vector<std::string> ReplaceDotWithUnderscore(const std::vector<std::string> &columnNames);
175
176/// Erase `that` element from vector `v`
177template <typename T>
178void Erase(const T &that, std::vector<T> &v)
179{
180 v.erase(std::remove(v.begin(), v.end(), that), v.end());
181}
182
183/// Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors
184void InterpreterDeclare(const std::string &code);
185
186/// Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
187/// The optional `context` parameter, if present, is mentioned in the error message.
188/// The pointer returned by the call to TInterpreter::Calc is returned in case of success.
189Long64_t InterpreterCalc(const std::string &code, const std::string &context = "");
190
191} // end NS RDF
192} // end NS Internal
193} // end NS ROOT
194
195/// \endcond
196
197#endif // RDFUTILS
#define b(i)
Definition: RSha256.hxx:100
long long Long64_t
Definition: RtypesCore.h:71
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:43
A TTree represents a columnar dataset.
Definition: TTree.h:78
TPaveText * pt
ROOT::VecOps::RVec< T > RVec
Definition: RVec.hxx:63
std::vector< std::string > ReplaceDotWithUnderscore(const std::vector< std::string > &columnNames)
Replace occurrences of '.
Definition: RDFUtils.cxx:283
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition: RDFUtils.cxx:42
unsigned int GetNSlots()
Definition: RDFUtils.cxx:270
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, RDataSource *ds, RCustomColumnBase *customColumn, bool vector2rvec)
Return a string containing the type of the given branch.
Definition: RDFUtils.cxx:211
char TypeName2ROOTTypeName(const std::string &b)
Convert type name (e.g.
Definition: RDFUtils.cxx:243
std::string TypeID2TypeName(const std::type_info &id)
Returns the name of a type starting from its type_info An empty string is returned in case of failure...
Definition: RDFUtils.cxx:84
Long64_t InterpreterCalc(const std::string &code, const std::string &context)
Definition: RDFUtils.cxx:312
void InterpreterDeclare(const std::string &code)
Definition: RDFUtils.cxx:302
static double A[]
double T(double x)
Definition: ChebyshevPol.h:34
ROOT type_traits extensions.
Definition: TypeTraits.hxx:21
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
static constexpr double pi
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25