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/RSpan.hxx"
15#include "ROOT/RStringView.hxx"
16#include "ROOT/RVec.hxx"
17#include "ROOT/TypeTraits.hxx"
18#include "Rtypes.h"
19
20#include <array>
21#include <deque>
22#include <functional>
23#include <memory>
24#include <new> // std::hardware_destructive_interference_size
25#include <string>
26#include <type_traits> // std::decay, std::false_type
27#include <vector>
28
29class TTree;
30class TTreeReader;
31
32
33namespace ROOT {
34namespace RDF {
35using ColumnNames_t = std::vector<std::string>;
36}
37
38namespace Experimental {
39class RLogChannel;
40}
41
42namespace RDF {
43class RDataSource;
44}
45
46namespace Detail {
47namespace RDF {
48
50
52
53// fwd decl for ColumnName2ColumnTypeName
54class RDefineBase;
55
56// type used for tag dispatching
58};
59
60} // end ns Detail
61} // end ns RDF
62
63namespace Internal {
64namespace RDF {
65
66using namespace ROOT::TypeTraits;
67using namespace ROOT::Detail::RDF;
68using namespace ROOT::RDF;
69
70/// Check for container traits.
71///
72/// Note that for all uses in RDF we don't want to classify std::string as a container.
73/// Template specializations of IsDataContainer make it return `true` for std::span<T>, std::vector<bool> and
74/// RVec<bool>, which we do want to count as containers even though they do not satisfy all the traits tested by the
75/// generic IsDataContainer<T>.
76template <typename T>
78 using Test_t = std::decay_t<T>;
79
80 template <typename A>
81 static constexpr bool Test(A *pt, A const *cpt = nullptr, decltype(pt->begin()) * = nullptr,
82 decltype(pt->end()) * = nullptr, decltype(cpt->begin()) * = nullptr,
83 decltype(cpt->end()) * = nullptr, typename A::iterator *pi = nullptr,
84 typename A::const_iterator *pci = nullptr)
85 {
86 using It_t = typename A::iterator;
87 using CIt_t = typename A::const_iterator;
88 using V_t = typename A::value_type;
89 return std::is_same<decltype(pt->begin()), It_t>::value && std::is_same<decltype(pt->end()), It_t>::value &&
90 std::is_same<decltype(cpt->begin()), CIt_t>::value && std::is_same<decltype(cpt->end()), CIt_t>::value &&
91 std::is_same<decltype(**pi), V_t &>::value && std::is_same<decltype(**pci), V_t const &>::value &&
93 }
94
95 template <typename A>
96 static constexpr bool Test(...)
97 {
98 return false;
99 }
100
101 static constexpr bool value = Test<Test_t>(nullptr);
102};
103
104template<>
105struct IsDataContainer<std::vector<bool>> {
106 static constexpr bool value = true;
107};
108
109template<>
111 static constexpr bool value = true;
112};
113
114template<typename T>
115struct IsDataContainer<std::span<T>> {
116 static constexpr bool value = true;
117};
118
119/// Detect whether a type is an instantiation of vector<T,A>
120template <typename>
121struct IsVector_t : public std::false_type {};
122
123template <typename T, typename A>
124struct IsVector_t<std::vector<T, A>> : public std::true_type {};
125
126const std::type_info &TypeName2TypeID(const std::string &name);
127
128std::string TypeID2TypeName(const std::type_info &id);
129
130std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *,
131 bool vector2rvec = true);
132
133char TypeName2ROOTTypeName(const std::string &b);
134
135unsigned int GetNSlots();
136
137/// `type` is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
138template <bool MustRemove, typename TypeList>
140 using type = TypeList;
141};
142
143template <typename TypeList>
146};
147
148template <bool MustRemove, typename TypeList>
150
151template <bool MustRemove, typename TypeList>
153 using type = TypeList;
154};
155
156template <typename TypeList>
160};
161
162template <bool MustRemove, typename TypeList>
164
165// Check the value_type type of a type with a SFINAE to allow compilation in presence
166// fundamental types
167template <typename T,
169struct ValueType {
170 using value_type = typename T::value_type;
171};
172
173template <typename T>
174struct ValueType<T, false> {
175 using value_type = T;
176};
177
178template <typename T>
179struct ValueType<ROOT::VecOps::RVec<T>, false> {
180 using value_type = T;
181};
182
183std::vector<std::string> ReplaceDotWithUnderscore(const std::vector<std::string> &columnNames);
184
185/// Erase `that` element from vector `v`
186template <typename T>
187void Erase(const T &that, std::vector<T> &v)
188{
189 v.erase(std::remove(v.begin(), v.end(), that), v.end());
190}
191
192/// Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors
193void InterpreterDeclare(const std::string &code);
194
195/// Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
196/// The optional `context` parameter, if present, is mentioned in the error message.
197/// The pointer returned by the call to TInterpreter::Calc is returned in case of success.
198Long64_t InterpreterCalc(const std::string &code, const std::string &context = "");
199
200/// Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_
202
203/// Get optimal column width for printing a table given the names and the desired minimal space between columns
204unsigned int GetColumnWidth(const std::vector<std::string>& names, const unsigned int minColumnSpace = 8u);
205
206// We could just check `#ifdef __cpp_lib_hardware_interference_size`, but at least on Mac 11
207// libc++ defines that macro but is missing the actual feature, so we use an ad-hoc ROOT macro instead.
208// See the relevant entry in cmake/modules/RootConfiguration.cmake for more info.
209#ifdef R__HAS_HARDWARE_INTERFERENCE_SIZE
210 // C++17 feature (so we can use inline variables)
211 inline constexpr std::size_t kCacheLineSize = std::hardware_destructive_interference_size;
212#else
213 // safe bet: assume the typical 64 bytes
214 static constexpr std::size_t kCacheLineSize = 64;
215#endif
216
217/// Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
218/// Useful to avoid false sharing.
219template <typename T>
220constexpr std::size_t CacheLineStep() {
221 return (kCacheLineSize + sizeof(T) - 1) / sizeof(T);
222}
223
224void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType,
225 const std::string &colName, const std::string &where);
226
227// TODO in C++17 this could be a lambda within FillHelper::Exec
228template <typename T>
229constexpr std::size_t FindIdxTrue(const T &arr)
230{
231 for (size_t i = 0; i < arr.size(); ++i) {
232 if (arr[i])
233 return i;
234 }
235 return arr.size();
236}
237
238// return type has to be decltype(auto) to preserve perfect forwarding
239template <std::size_t N, typename... Ts>
240decltype(auto) GetNthElement(Ts &&...args)
241{
242 auto tuple = std::forward_as_tuple(args...);
243 return std::get<N>(tuple);
244}
245
246#if __cplusplus >= 201703L
247template <class... Ts>
248using Disjunction = std::disjunction<Ts...>;
249#else
250template <class...>
251struct Disjunction : std::false_type {
252};
253template <class B1>
255};
256template <class B1, class... Bn>
257struct Disjunction<B1, Bn...> : std::conditional_t<bool(B1::value), B1, Disjunction<Bn...>> {
258};
259#endif
260
261bool IsStrInVec(const std::string &str, const std::vector<std::string> &vec);
262
263// clang-format off
264template <typename>
265struct IsRVec : std::false_type {};
266
267template <typename T>
268struct IsRVec<ROOT::VecOps::RVec<T>> : std::true_type {};
269// clang-format on
270
271/// Return a vector with all elements of v1 and v2 and duplicates removed.
272/// Precondition: each of v1 and v2 must not have duplicate elements.
273template <typename T>
274std::vector<T> Union(const std::vector<T> &v1, const std::vector<T> &v2)
275{
276 std::vector<T> res = v1;
277
278 // Add the variations coming from the input columns
279 for (const auto &e : v2)
280 if (std::find(v1.begin(), v1.end(), e) == v1.end())
281 res.emplace_back(e);
282
283 return res;
284}
285
286} // end NS RDF
287} // end NS Internal
288} // end NS ROOT
289
290#endif // RDFUTILS
#define e(i)
Definition: RSha256.hxx:103
long long Long64_t
Definition: RtypesCore.h:80
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition: TGX11.cxx:110
A log configuration for a channel, e.g.
Definition: RLogger.hxx:101
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
typename RemoveFirstParameter< T >::type RemoveFirstParameter_t
Definition: TypeTraits.hxx:169
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition: RVec.hxx:1439
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:44
A TTree represents a columnar dataset.
Definition: TTree.h:79
TPaveText * pt
basic_string_view< char > string_view
ROOT::Experimental::RLogChannel & RDFLogChannel()
Definition: RDFUtils.cxx:37
std::vector< std::string > ReplaceDotWithUnderscore(const std::vector< std::string > &columnNames)
Replace occurrences of '.
Definition: RDFUtils.cxx:298
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition: RDFUtils.cxx:51
unsigned int GetNSlots()
Definition: RDFUtils.cxx:285
decltype(auto) GetNthElement(Ts &&...args)
Definition: Utils.hxx:240
static constexpr std::size_t kCacheLineSize
Definition: Utils.hxx:214
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2rvec=true)
Return a string containing the type of the given branch.
Definition: RDFUtils.cxx:224
char TypeName2ROOTTypeName(const std::string &b)
Convert type name (e.g.
Definition: RDFUtils.cxx:254
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:99
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
Definition: RDFUtils.cxx:419
void Erase(const T &that, std::vector< T > &v)
Erase that element from vector v
Definition: Utils.hxx:187
unsigned int GetColumnWidth(const std::vector< std::string > &names, const unsigned int minColumnSpace=8u)
Get optimal column width for printing a table given the names and the desired minimal space between c...
Definition: RDFUtils.cxx:374
constexpr std::size_t CacheLineStep()
Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
Definition: Utils.hxx:220
Long64_t InterpreterCalc(const std::string &code, const std::string &context="")
Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
Definition: RDFUtils.cxx:329
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition: Utils.hxx:149
constexpr std::size_t FindIdxTrue(const T &arr)
Definition: Utils.hxx:229
std::vector< T > Union(const std::vector< T > &v1, const std::vector< T > &v2)
Return a vector with all elements of v1 and v2 and duplicates removed.
Definition: Utils.hxx:274
bool IsInternalColumn(std::string_view colName)
Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_.
Definition: RDFUtils.cxx:365
void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType, const std::string &colName, const std::string &where)
Definition: RDFUtils.cxx:386
void InterpreterDeclare(const std::string &code)
Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors.
Definition: RDFUtils.cxx:317
typename RemoveFirstTwoParametersIf< MustRemove, TypeList >::type RemoveFirstTwoParametersIf_t
Definition: Utils.hxx:163
static double A[]
double T(double x)
Definition: ChebyshevPol.h:34
std::vector< std::string > ColumnNames_t
Definition: Utils.hxx:35
ROOT type_traits extensions.
Definition: TypeTraits.hxx:21
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
static constexpr double pi
Check for container traits.
Definition: Utils.hxx:77
static constexpr bool Test(A *pt, A const *cpt=nullptr, decltype(pt->begin()) *=nullptr, decltype(pt->end()) *=nullptr, decltype(cpt->begin()) *=nullptr, decltype(cpt->end()) *=nullptr, typename A::iterator *pi=nullptr, typename A::const_iterator *pci=nullptr)
Definition: Utils.hxx:81
static constexpr bool Test(...)
Definition: Utils.hxx:96
static constexpr bool value
Definition: Utils.hxx:101
Detect whether a type is an instantiation of vector<T,A>
Definition: Utils.hxx:121
type is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
Definition: Utils.hxx:139
typename RemoveFirstParameterIf< true, typeTmp >::type type
Definition: Utils.hxx:159
typename RemoveFirstParameterIf< true, TypeList >::type typeTmp
Definition: Utils.hxx:158
typename T::value_type value_type
Definition: Utils.hxx:170
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25
Definition: civetweb.c:1856