Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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 <string_view>
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 <unordered_set>
26#include <shared_mutex>
27#include <string>
28#include <type_traits> // std::decay, std::false_type
29#include <vector>
30
31class TTree;
32class TTreeReader;
33
35class RDatasetSpec;
36}
37namespace ROOT {
38namespace RDF {
39using ColumnNames_t = std::vector<std::string>;
40}
41
42class RLogChannel;
43
44namespace RDF {
45class RDataSource;
46}
47
48namespace Detail {
49namespace RDF {
50
52
54
55// fwd decl for ColumnName2ColumnTypeName
56class RDefineBase;
57
58// type used for tag dispatching
60};
61
62} // end ns Detail
63} // end ns RDF
64
65namespace Internal {
66namespace RDF {
67
68using namespace ROOT::TypeTraits;
69using namespace ROOT::Detail::RDF;
70using namespace ROOT::RDF;
71
72/// Check for container traits.
73///
74/// Note that for all uses in RDF we don't want to classify std::string as a container.
75/// Template specializations of IsDataContainer make it return `true` for std::span<T>, std::vector<bool> and
76/// RVec<bool>, which we do want to count as containers even though they do not satisfy all the traits tested by the
77/// generic IsDataContainer<T>.
78template <typename T>
80 using Test_t = std::decay_t<T>;
81
82 template <typename A>
83 static constexpr bool Test(A *pt, A const *cpt = nullptr, decltype(pt->begin()) * = nullptr,
84 decltype(pt->end()) * = nullptr, decltype(cpt->begin()) * = nullptr,
85 decltype(cpt->end()) * = nullptr, typename A::iterator *pi = nullptr,
86 typename A::const_iterator *pci = nullptr)
87 {
88 using It_t = typename A::iterator;
89 using CIt_t = typename A::const_iterator;
90 using V_t = typename A::value_type;
91 return std::is_same<decltype(pt->begin()), It_t>::value && std::is_same<decltype(pt->end()), It_t>::value &&
92 std::is_same<decltype(cpt->begin()), CIt_t>::value && std::is_same<decltype(cpt->end()), CIt_t>::value &&
93 std::is_same<decltype(**pi), V_t &>::value && std::is_same<decltype(**pci), V_t const &>::value &&
94 !std::is_same<T, std::string>::value;
95 }
96
97 template <typename A>
98 static constexpr bool Test(...)
99 {
100 return false;
101 }
102
103 static constexpr bool value = Test<Test_t>(nullptr);
104};
105
106template<>
107struct IsDataContainer<std::vector<bool>> {
108 static constexpr bool value = true;
109};
110
111template<>
113 static constexpr bool value = true;
114};
115
116template<typename T>
117struct IsDataContainer<std::span<T>> {
118 static constexpr bool value = true;
119};
120
121/// Detect whether a type is an instantiation of vector<T,A>
122template <typename>
123struct IsVector_t : public std::false_type {};
124
125template <typename T, typename A>
126struct IsVector_t<std::vector<T, A>> : public std::true_type {};
127
128std::string GetBranchOrLeafTypeName(TTree &t, const std::string &colName);
129
130const std::type_info &TypeName2TypeID(const std::string &name);
131
132std::string TypeID2TypeName(const std::type_info &id);
133
134std::string GetTypeNameWithOpts(const ROOT::RDF::RDataSource &df, std::string_view colName, bool vector2RVec);
135std::string
136ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2RVec = true);
137
138char TypeName2ROOTTypeName(const std::string &b);
139
140unsigned int GetNSlots();
141
142/// `type` is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
143template <bool MustRemove, typename TypeList>
147
148template <typename TypeList>
152
153template <bool MustRemove, typename TypeList>
155
156template <bool MustRemove, typename TypeList>
160
161template <typename TypeList>
166
167template <bool MustRemove, typename TypeList>
169
170// Check the value_type type of a type with a SFINAE to allow compilation in presence
171// fundamental types
172template <typename T,
173 bool IsDataContainer = IsDataContainer<std::decay_t<T>>::value || std::is_same<std::string, T>::value>
174struct ValueType {
175 using value_type = typename T::value_type;
176};
177
178template <typename T>
179struct ValueType<T, false> {
180 using value_type = T;
181};
182
183template <typename T>
185 using value_type = T;
186};
187
188std::vector<std::string> ReplaceDotWithUnderscore(const std::vector<std::string> &columnNames);
189
190/// Erase `that` element from vector `v`
191template <typename T>
192void Erase(const T &that, std::vector<T> &v)
193{
194 v.erase(std::remove(v.begin(), v.end(), that), v.end());
195}
196
197/// Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors
198void InterpreterDeclare(const std::string &code);
199
200/// Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
201/// The optional `context` parameter, if present, is mentioned in the error message.
202void InterpreterCalc(const std::string &code, const std::string &context = "");
203
204/// Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_
205bool IsInternalColumn(std::string_view colName);
206
207/// Get optimal column width for printing a table given the names and the desired minimal space between columns
208unsigned int GetColumnWidth(const std::vector<std::string>& names, const unsigned int minColumnSpace = 8u);
209
210// We could just check `#ifdef __cpp_lib_hardware_interference_size`, but at least on Mac 11
211// libc++ defines that macro but is missing the actual feature, so we use an ad-hoc ROOT macro instead.
212// See the relevant entry in cmake/modules/RootConfiguration.cmake for more info.
213#ifdef R__HAS_HARDWARE_INTERFERENCE_SIZE
214 // C++17 feature (so we can use inline variables)
215 inline constexpr std::size_t kCacheLineSize = std::hardware_destructive_interference_size;
216#else
217 // safe bet: assume the typical 64 bytes
218 static constexpr std::size_t kCacheLineSize = 64;
219#endif
220
221/// Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
222/// Useful to avoid false sharing.
223template <typename T>
224constexpr std::size_t CacheLineStep() {
225 return (kCacheLineSize + sizeof(T) - 1) / sizeof(T);
226}
227
228void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType,
229 const std::string &colName);
230
231// TODO in C++17 this could be a lambda within FillHelper::Exec
232template <typename T>
233constexpr std::size_t FindIdxTrue(const T &arr)
234{
235 for (size_t i = 0; i < arr.size(); ++i) {
236 if (arr[i])
237 return i;
238 }
239 return arr.size();
240}
241
242// return type has to be decltype(auto) to preserve perfect forwarding
243template <std::size_t N, typename... Ts>
244decltype(auto) GetNthElement(Ts &&...args)
245{
246 auto tuple = std::forward_as_tuple(args...);
247 return std::get<N>(tuple);
248}
249
250#if __cplusplus >= 201703L
251template <class... Ts>
252using Disjunction = std::disjunction<Ts...>;
253#else
254template <class...>
255struct Disjunction : std::false_type {
256};
257template <class B1>
259};
260template <class B1, class... Bn>
261struct Disjunction<B1, Bn...> : std::conditional_t<bool(B1::value), B1, Disjunction<Bn...>> {
262};
263#endif
264
265bool IsStrInVec(const std::string &str, const std::vector<std::string> &vec);
266
267/// Return a vector with all elements of v1 and v2 and duplicates removed.
268/// Precondition: each of v1 and v2 must not have duplicate elements.
269template <typename T>
270std::vector<T> Union(const std::vector<T> &v1, const std::vector<T> &v2)
271{
272 std::vector<T> res = v1;
273
274 // Add the variations coming from the input columns
275 for (const auto &e : v2)
276 if (std::find(v1.begin(), v1.end(), e) == v1.end())
277 res.emplace_back(e);
278
279 return res;
280}
281
282/**
283 * \brief A Thread-safe cache for strings.
284 *
285 * This is used to generically store strings that are created in the computation
286 * graph machinery, for example when adding a new node.
287 */
289 std::unordered_set<std::string> fStrings{};
290 std::shared_mutex fMutex{};
291
292public:
293 /**
294 * \brief Inserts the input string in the cache and returns an iterator to the cached string.
295 *
296 * The function implements the following strategy for thread-safety:
297 * 1. Take a shared lock and early return if the string is already in the cache.
298 * 2. Release the shared lock and take an exclusive lock.
299 * 3. Check again if another thread filled the cache meanwhile. If so, return the cached value.
300 * 4. Insert the new value in the cache and return.
301 */
302 auto Insert(const std::string &string) -> decltype(fStrings)::const_iterator;
303};
304
305/**
306 * \brief Struct to wrap the call to a function with a guaranteed order of
307 * execution of its arguments.
308 * \tparam F Type of the callable.
309 * \tparam Args Variadic types of the arguments to the callable.
310 *
311 * The execution order is guaranteed by calling the function in the constructor
312 * thus enabling the exploitation of the list-initialization sequenced-before
313 * feature (See rule 9 at https://en.cppreference.com/w/cpp/language/eval_order).
314 */
316 template <typename F, typename... Args>
317 CallGuaranteedOrder(F &&f, Args &&...args)
318 {
319 f(std::forward<Args>(args)...);
320 }
321};
322
323template <typename T>
325{
326 const static std::shared_ptr<T> fgRawPtrCtrlBlock;
327 return std::shared_ptr<T>(fgRawPtrCtrlBlock, rawPtr);
328}
329
330
331/**
332 * \brief Function to retrieve RDatasetSpec from JSON file provided
333 * \param[in] jsonFile Path to the dataset specification JSON file.
334 *
335 * This function allows us to have access to an RDatasetSpec which needs to
336 * be created when we use the FromSpec factory function.
337 */
339
340} // end NS RDF
341} // end NS Internal
342} // end NS ROOT
343
344#endif // RDFUTILS
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define N
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 Thread-safe cache for strings.
Definition Utils.hxx:288
auto Insert(const std::string &string) -> decltype(fStrings)::const_iterator
Inserts the input string in the cache and returns an iterator to the cached string.
Definition RDFUtils.cxx:449
std::unordered_set< std::string > fStrings
Definition Utils.hxx:289
The dataset specification for RDataFrame.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
A log configuration for a channel, e.g.
Definition RLogger.hxx:98
const_iterator begin() const
const_iterator end() const
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1529
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
A TTree represents a columnar dataset.
Definition TTree.h:79
TPaveText * pt
#define F(x, y, z)
ROOT::RLogChannel & RDFLogChannel()
Definition RDFUtils.cxx:41
auto MakeAliasedSharedPtr(T *rawPtr)
Definition Utils.hxx:324
std::vector< std::string > ReplaceDotWithUnderscore(const std::vector< std::string > &columnNames)
Replace occurrences of '.
Definition RDFUtils.cxx:318
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition RDFUtils.cxx:66
typename RemoveFirstTwoParametersIf< MustRemove, TypeList >::type RemoveFirstTwoParametersIf_t
Definition Utils.hxx:168
ROOT::RDF::Experimental::RDatasetSpec RetrieveSpecFromJson(const std::string &jsonFile)
Function to retrieve RDatasetSpec from JSON file provided.
Definition RDFUtils.cxx:466
unsigned int GetNSlots()
Definition RDFUtils.cxx:305
decltype(auto) GetNthElement(Ts &&...args)
Definition Utils.hxx:244
static constexpr std::size_t kCacheLineSize
Definition Utils.hxx:218
char TypeName2ROOTTypeName(const std::string &b)
Convert type name (e.g.
Definition RDFUtils.cxx:263
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:123
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
Definition RDFUtils.cxx:444
void Erase(const T &that, std::vector< T > &v)
Erase that element from vector v
Definition Utils.hxx:192
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:395
std::string GetBranchOrLeafTypeName(TTree &t, const std::string &colName)
Return the typename of object colName stored in t, if any.
Definition RDFUtils.cxx:176
constexpr std::size_t CacheLineStep()
Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
Definition Utils.hxx:224
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:233
void 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:349
void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType, const std::string &colName)
Definition RDFUtils.cxx:407
constexpr std::size_t FindIdxTrue(const T &arr)
Definition Utils.hxx:233
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:270
bool IsInternalColumn(std::string_view colName)
Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_.
Definition RDFUtils.cxx:386
std::string GetTypeNameWithOpts(const ROOT::RDF::RDataSource &ds, std::string_view colName, bool vector2RVec)
Definition RDFUtils.cxx:548
void InterpreterDeclare(const std::string &code)
Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors.
Definition RDFUtils.cxx:337
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition Utils.hxx:154
std::vector< std::string > ColumnNames_t
ROOT type_traits extensions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Struct to wrap the call to a function with a guaranteed order of execution of its arguments.
Definition Utils.hxx:315
CallGuaranteedOrder(F &&f, Args &&...args)
Definition Utils.hxx:317
Check for container traits.
Definition Utils.hxx:79
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:83
static constexpr bool Test(...)
Definition Utils.hxx:98
static constexpr bool value
Definition Utils.hxx:103
Detect whether a type is an instantiation of vector<T,A>
Definition Utils.hxx:123
type is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
Definition Utils.hxx:144
typename RemoveFirstParameterIf< true, typeTmp >::type type
Definition Utils.hxx:164
typename RemoveFirstParameterIf< true, TypeList >::type typeTmp
Definition Utils.hxx:163
typename T::value_type value_type
Definition Utils.hxx:175
Lightweight storage for a collection of types.