Logo ROOT  
Reference Guide
 
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
68/// Warn once about experimental filling of RHist.
69void WarnHist();
70
71using namespace ROOT::TypeTraits;
72using namespace ROOT::Detail::RDF;
73using namespace ROOT::RDF;
74
75/// Obtain or set the number of threads that will share a clone of a thread-safe 3D histogram.
76/// Setting it to N will make N threads share a clone, setting it to 0 or 1 will use one clone
77/// per thread.
78/// Setting it to higher numbers reduces the RDF memory consumption, but might create contention
79/// on TH3Ds. When the RDF computation graph consists mostly of filling TH3Ds, lower values are better.
80/// \return A reference to the current divider.
81unsigned int &NThreadPerTH3();
82
83/// Check for container traits.
84///
85/// Note that for all uses in RDF we don't want to classify std::string as a container.
86/// Template specializations of IsDataContainer make it return `true` for std::span<T>, std::vector<bool> and
87/// RVec<bool>, which we do want to count as containers even though they do not satisfy all the traits tested by the
88/// generic IsDataContainer<T>.
89template <typename T>
91 using Test_t = std::decay_t<T>;
92
93 template <typename A>
94 static constexpr bool Test(A *pt, A const *cpt = nullptr, decltype(pt->begin()) * = nullptr,
95 decltype(pt->end()) * = nullptr, decltype(cpt->begin()) * = nullptr,
96 decltype(cpt->end()) * = nullptr, typename A::iterator *pi = nullptr,
97 typename A::const_iterator *pci = nullptr)
98 {
99 using It_t = typename A::iterator;
100 using CIt_t = typename A::const_iterator;
101 using V_t = typename A::value_type;
102 return std::is_same<decltype(pt->begin()), It_t>::value && std::is_same<decltype(pt->end()), It_t>::value &&
103 std::is_same<decltype(cpt->begin()), CIt_t>::value && std::is_same<decltype(cpt->end()), CIt_t>::value &&
104 std::is_same<decltype(**pi), V_t &>::value && std::is_same<decltype(**pci), V_t const &>::value &&
105 !std::is_same<T, std::string>::value;
106 }
107
108 template <typename A>
109 static constexpr bool Test(...)
110 {
111 return false;
112 }
113
114 static constexpr bool value = Test<Test_t>(nullptr);
115};
116
117template<>
118struct IsDataContainer<std::vector<bool>> {
119 static constexpr bool value = true;
120};
121
122template<>
124 static constexpr bool value = true;
125};
126
127template<typename T>
128struct IsDataContainer<std::span<T>> {
129 static constexpr bool value = true;
130};
131
132/// Detect whether a type is an instantiation of vector<T,A>
133template <typename>
134struct IsVector_t : public std::false_type {};
135
136template <typename T, typename A>
137struct IsVector_t<std::vector<T, A>> : public std::true_type {};
138
139std::string GetBranchOrLeafTypeName(TTree &t, const std::string &colName);
140
141const std::type_info &TypeName2TypeID(const std::string &name);
142
143std::string TypeID2TypeName(const std::type_info &id);
144
145std::string GetTypeNameWithOpts(const ROOT::RDF::RDataSource &df, std::string_view colName, bool vector2RVec);
146std::string
147ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2RVec = true);
148
149char TypeName2ROOTTypeName(const std::string &b);
150char TypeID2ROOTTypeName(const std::type_info &tid);
151
152unsigned int GetNSlots();
153
154/// `type` is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
155template <bool MustRemove, typename TypeList>
159
160template <typename TypeList>
164
165template <bool MustRemove, typename TypeList>
167
168template <bool MustRemove, typename TypeList>
172
173template <typename TypeList>
178
179template <bool MustRemove, typename TypeList>
181
182// Check the value_type type of a type with a SFINAE to allow compilation in presence
183// fundamental types
184template <typename T,
185 bool IsDataContainer = IsDataContainer<std::decay_t<T>>::value || std::is_same<std::string, T>::value>
186struct ValueType {
187 using value_type = typename T::value_type;
188};
189
190template <typename T>
191struct ValueType<T, false> {
192 using value_type = T;
193};
194
195template <typename T>
197 using value_type = T;
198};
199
200std::vector<std::string> ReplaceDotWithUnderscore(const std::vector<std::string> &columnNames);
201
202/// Erase `that` element from vector `v`
203template <typename T>
204void Erase(const T &that, std::vector<T> &v)
205{
206 v.erase(std::remove(v.begin(), v.end(), that), v.end());
207}
208
209/// Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors
210void InterpreterDeclare(const std::string &code);
211
212/// Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
213/// The optional `context` parameter, if present, is mentioned in the error message.
214void InterpreterCalc(const std::string &code, const std::string &context = "");
215
216/// Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_
217bool IsInternalColumn(std::string_view colName);
218
219/// Get optimal column width for printing a table given the names and the desired minimal space between columns
220unsigned int GetColumnWidth(const std::vector<std::string>& names, const unsigned int minColumnSpace = 8u);
221
222/// Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
223/// Useful to avoid false sharing.
224template <typename T>
225constexpr std::size_t CacheLineStep() {
226 constexpr std::size_t cacheLineSize = R__HARDWARE_INTERFERENCE_SIZE;
227 return (cacheLineSize + sizeof(T) - 1) / sizeof(T);
228}
229
230void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType,
231 const std::string &colName);
232
233// TODO in C++17 this could be a lambda within FillHelper::Exec
234template <typename T>
235constexpr std::size_t FindIdxTrue(const T &arr)
236{
237 for (size_t i = 0; i < arr.size(); ++i) {
238 if (arr[i])
239 return i;
240 }
241 return arr.size();
242}
243
244// return type has to be decltype(auto) to preserve perfect forwarding
245template <std::size_t N, typename... Ts>
246decltype(auto) GetNthElement(Ts &&...args)
247{
248 auto tuple = std::forward_as_tuple(args...);
249 return std::get<N>(tuple);
250}
251
252#if __cplusplus >= 201703L
253template <class... Ts>
254using Disjunction = std::disjunction<Ts...>;
255#else
256template <class...>
257struct Disjunction : std::false_type {
258};
259template <class B1>
261};
262template <class B1, class... Bn>
263struct Disjunction<B1, Bn...> : std::conditional_t<bool(B1::value), B1, Disjunction<Bn...>> {
264};
265#endif
266
267bool IsStrInVec(const std::string &str, const std::vector<std::string> &vec);
268
269/// Return a vector with all elements of v1 and v2 and duplicates removed.
270/// Precondition: each of v1 and v2 must not have duplicate elements.
271template <typename T>
272std::vector<T> Union(const std::vector<T> &v1, const std::vector<T> &v2)
273{
274 std::vector<T> res = v1;
275
276 // Add the variations coming from the input columns
277 for (const auto &e : v2)
278 if (std::find(v1.begin(), v1.end(), e) == v1.end())
279 res.emplace_back(e);
280
281 return res;
282}
283
284/**
285 * \brief A Thread-safe cache for strings.
286 *
287 * This is used to generically store strings that are created in the computation
288 * graph machinery, for example when adding a new node.
289 */
291 std::unordered_set<std::string> fStrings{};
292 std::shared_mutex fMutex{};
293
294public:
295 /**
296 * \brief Inserts the input string in the cache and returns an iterator to the cached string.
297 *
298 * The function implements the following strategy for thread-safety:
299 * 1. Take a shared lock and early return if the string is already in the cache.
300 * 2. Release the shared lock and take an exclusive lock.
301 * 3. Check again if another thread filled the cache meanwhile. If so, return the cached value.
302 * 4. Insert the new value in the cache and return.
303 */
304 auto Insert(const std::string &string) -> decltype(fStrings)::const_iterator;
305};
306
307/**
308 * \brief Struct to wrap the call to a function with a guaranteed order of
309 * execution of its arguments.
310 * \tparam F Type of the callable.
311 * \tparam Args Variadic types of the arguments to the callable.
312 *
313 * The execution order is guaranteed by calling the function in the constructor
314 * thus enabling the exploitation of the list-initialization sequenced-before
315 * feature (See rule 9 at https://en.cppreference.com/w/cpp/language/eval_order).
316 */
318 template <typename F, typename... Args>
319 CallGuaranteedOrder(F &&f, Args &&...args)
320 {
321 f(std::forward<Args>(args)...);
322 }
323};
324
325template <typename T>
327{
328 const static std::shared_ptr<T> fgRawPtrCtrlBlock;
329 return std::shared_ptr<T>(fgRawPtrCtrlBlock, rawPtr);
330}
331
332
333/**
334 * \brief Function to retrieve RDatasetSpec from JSON file provided
335 * \param[in] jsonFile Path to the dataset specification JSON file.
336 *
337 * This function allows us to have access to an RDatasetSpec which needs to
338 * be created when we use the FromSpec factory function.
339 */
341
342/**
343 * Tag to let data sources use the native data type when creating a column reader.
344 *
345 * See usage of this in RNTupleDS
346 */
348
349} // end NS RDF
350} // end NS Internal
351} // end NS ROOT
352
353#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:290
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:541
std::unordered_set< std::string > fStrings
Definition Utils.hxx:291
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:1524
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:89
TPaveText * pt
#define F(x, y, z)
ROOT::RLogChannel & RDFLogChannel()
Definition RDFUtils.cxx:43
auto MakeAliasedSharedPtr(T *rawPtr)
Definition Utils.hxx:326
std::vector< std::string > ReplaceDotWithUnderscore(const std::vector< std::string > &columnNames)
Replace occurrences of '.
Definition RDFUtils.cxx:410
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition RDFUtils.cxx:86
typename RemoveFirstTwoParametersIf< MustRemove, TypeList >::type RemoveFirstTwoParametersIf_t
Definition Utils.hxx:180
ROOT::RDF::Experimental::RDatasetSpec RetrieveSpecFromJson(const std::string &jsonFile)
Function to retrieve RDatasetSpec from JSON file provided.
Definition RDFUtils.cxx:558
unsigned int GetNSlots()
Definition RDFUtils.cxx:397
decltype(auto) GetNthElement(Ts &&...args)
Definition Utils.hxx:246
char TypeName2ROOTTypeName(const std::string &b)
Convert type name (e.g.
Definition RDFUtils.cxx:355
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:191
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
Definition RDFUtils.cxx:536
void Erase(const T &that, std::vector< T > &v)
Erase that element from vector v
Definition Utils.hxx:204
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:487
std::string GetBranchOrLeafTypeName(TTree &t, const std::string &colName)
Return the typename of object colName stored in t, if any.
Definition RDFUtils.cxx:268
constexpr std::size_t CacheLineStep()
Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
Definition Utils.hxx:225
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:325
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:441
void CheckReaderTypeMatches(const std::type_info &colType, const std::type_info &requestedType, const std::string &colName)
Definition RDFUtils.cxx:499
constexpr std::size_t FindIdxTrue(const T &arr)
Definition Utils.hxx:235
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:272
bool IsInternalColumn(std::string_view colName)
Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_.
Definition RDFUtils.cxx:478
void WarnHist()
Warn once about experimental filling of RHist.
Definition RDFUtils.cxx:55
std::string GetTypeNameWithOpts(const ROOT::RDF::RDataSource &ds, std::string_view colName, bool vector2RVec)
Definition RDFUtils.cxx:640
void InterpreterDeclare(const std::string &code)
Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors.
Definition RDFUtils.cxx:429
unsigned int & NThreadPerTH3()
Obtain or set the number of threads that will share a clone of a thread-safe 3D histogram.
Definition RDFUtils.cxx:76
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition Utils.hxx:166
char TypeID2ROOTTypeName(const std::type_info &tid)
Definition RDFUtils.cxx:219
std::vector< std::string > ColumnNames_t
ROOT type_traits extensions.
Struct to wrap the call to a function with a guaranteed order of execution of its arguments.
Definition Utils.hxx:317
CallGuaranteedOrder(F &&f, Args &&...args)
Definition Utils.hxx:319
Check for container traits.
Definition Utils.hxx:90
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:94
static constexpr bool Test(...)
Definition Utils.hxx:109
static constexpr bool value
Definition Utils.hxx:114
Detect whether a type is an instantiation of vector<T,A>
Definition Utils.hxx:134
type is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
Definition Utils.hxx:156
typename RemoveFirstParameterIf< true, typeTmp >::type type
Definition Utils.hxx:176
typename RemoveFirstParameterIf< true, TypeList >::type typeTmp
Definition Utils.hxx:175
Tag to let data sources use the native data type when creating a column reader.
Definition Utils.hxx:347
typename T::value_type value_type
Definition Utils.hxx:187
Lightweight storage for a collection of types.