Loading [MathJax]/extensions/tex2jax.js
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 "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
27#include <vector>
28
29class TTree;
30class TTreeReader;
31
32/// \cond HIDDEN_SYMBOLS
33namespace ROOT {
34namespace Experimental {
35class RLogChannel;
36}
37
38namespace RDF {
39class RDataSource;
40}
41
42namespace Detail {
43namespace RDF {
44using ColumnNames_t = std::vector<std::string>;
45
46ROOT::Experimental::RLogChannel &RDFLogChannel();
47
48// fwd decl for ColumnName2ColumnTypeName
49class RDefineBase;
50
51// type used for tag dispatching
52struct RInferredType {
53};
54
55} // end ns Detail
56} // end ns RDF
57
58namespace Internal {
59namespace RDF {
60
61using namespace ROOT::TypeTraits;
62using namespace ROOT::Detail::RDF;
63using namespace ROOT::RDF;
64
65/// Check for container traits.
66///
67/// Note that for all uses in RDF we don't want to classify std::string as a container.
68/// Template specializations of IsDataContainer make it return `true` for std::span<T>, std::vector<bool> and
69/// RVec<bool>, which we do want to count as containers even though they do not satisfy all the traits tested by the
70/// generic IsDataContainer<T>.
71template <typename T>
72struct IsDataContainer {
73 using Test_t = typename std::decay<T>::type;
74
75 template <typename A>
76 static constexpr bool Test(A *pt, A const *cpt = nullptr, decltype(pt->begin()) * = nullptr,
77 decltype(pt->end()) * = nullptr, decltype(cpt->begin()) * = nullptr,
78 decltype(cpt->end()) * = nullptr, typename A::iterator *pi = nullptr,
79 typename A::const_iterator *pci = nullptr)
80 {
81 using It_t = typename A::iterator;
82 using CIt_t = typename A::const_iterator;
83 using V_t = typename A::value_type;
84 return std::is_same<decltype(pt->begin()), It_t>::value && std::is_same<decltype(pt->end()), It_t>::value &&
85 std::is_same<decltype(cpt->begin()), CIt_t>::value && std::is_same<decltype(cpt->end()), CIt_t>::value &&
86 std::is_same<decltype(**pi), V_t &>::value && std::is_same<decltype(**pci), V_t const &>::value &&
87 !std::is_same<T, std::string>::value;
88 }
89
90 template <typename A>
91 static constexpr bool Test(...)
92 {
93 return false;
94 }
95
96 static constexpr bool value = Test<Test_t>(nullptr);
97};
98
99template<>
100struct IsDataContainer<std::vector<bool>> {
101 static constexpr bool value = true;
102};
103
104template<>
105struct IsDataContainer<ROOT::VecOps::RVec<bool>> {
106 static constexpr bool value = true;
107};
108
109template<typename T>
110struct IsDataContainer<std::span<T>> {
111 static constexpr bool value = true;
112};
113
114/// Detect whether a type is an instantiation of vector<T,A>
115template <typename>
116struct IsVector_t : public std::false_type {};
117
118template <typename T, typename A>
119struct IsVector_t<std::vector<T, A>> : public std::true_type {};
120
121const std::type_info &TypeName2TypeID(const std::string &name);
122
123std::string TypeID2TypeName(const std::type_info &id);
124
125std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *,
126 bool vector2rvec = true);
127
128char TypeName2ROOTTypeName(const std::string &b);
129
130unsigned int GetNSlots();
131
132/// `type` is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
133template <bool MustRemove, typename TypeList>
134struct RemoveFirstParameterIf {
135 using type = TypeList;
136};
137
138template <typename TypeList>
139struct RemoveFirstParameterIf<true, TypeList> {
141};
142
143template <bool MustRemove, typename TypeList>
144using RemoveFirstParameterIf_t = typename RemoveFirstParameterIf<MustRemove, TypeList>::type;
145
146template <bool MustRemove, typename TypeList>
147struct RemoveFirstTwoParametersIf {
148 using type = TypeList;
149};
150
151template <typename TypeList>
152struct RemoveFirstTwoParametersIf<true, TypeList> {
153 using typeTmp = typename RemoveFirstParameterIf<true, TypeList>::type;
154 using type = typename RemoveFirstParameterIf<true, typeTmp>::type;
155};
156
157template <bool MustRemove, typename TypeList>
158using RemoveFirstTwoParametersIf_t = typename RemoveFirstTwoParametersIf<MustRemove, TypeList>::type;
159
160/// Detect whether a type is an instantiation of RVec<T>
161template <typename>
162struct IsRVec_t : public std::false_type {};
163
164template <typename T>
165struct IsRVec_t<ROOT::VecOps::RVec<T>> : public std::true_type {};
166
167// Check the value_type type of a type with a SFINAE to allow compilation in presence
168// fundamental types
169template <typename T, bool IsDataContainer = IsDataContainer<typename std::decay<T>::type>::value || std::is_same<std::string, T>::value>
170struct ValueType {
171 using value_type = typename T::value_type;
172};
173
174template <typename T>
175struct ValueType<T, false> {
176 using value_type = T;
177};
178
179template <typename T>
180struct ValueType<ROOT::VecOps::RVec<T>, false> {
181 using value_type = T;
182};
183
184std::vector<std::string> ReplaceDotWithUnderscore(const std::vector<std::string> &columnNames);
185
186/// Erase `that` element from vector `v`
187template <typename T>
188void Erase(const T &that, std::vector<T> &v)
189{
190 v.erase(std::remove(v.begin(), v.end(), that), v.end());
191}
192
193/// Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors
194void InterpreterDeclare(const std::string &code);
195
196/// Jit code in the interpreter with TInterpreter::Calc, throw in case of errors.
197/// The optional `context` parameter, if present, is mentioned in the error message.
198/// The pointer returned by the call to TInterpreter::Calc is returned in case of success.
199Long64_t InterpreterCalc(const std::string &code, const std::string &context = "");
200
201/// Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_
202bool IsInternalColumn(std::string_view colName);
203
204// We could just check `#ifdef __cpp_lib_hardware_interference_size`, but at least on Mac 11
205// libc++ defines that macro but is missing the actual feature, so we use an ad-hoc ROOT macro instead.
206// See the relevant entry in cmake/modules/RootConfiguration.cmake for more info.
207#ifdef R__HAS_HARDWARE_INTERFERENCE_SIZE
208 // C++17 feature (so we can use inline variables)
209 inline constexpr std::size_t kCacheLineSize = std::hardware_destructive_interference_size;
210#else
211 // safe bet: assume the typical 64 bytes
212 static constexpr std::size_t kCacheLineSize = 64;
213#endif
214
215/// Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
216/// Useful to avoid false sharing.
217template <typename T>
218constexpr std::size_t CacheLineStep() {
219 return (kCacheLineSize + sizeof(T) - 1) / sizeof(T);
220}
221
222} // end NS RDF
223} // end NS Internal
224} // end NS ROOT
225
226/// \endcond
227
228#endif // RDFUTILS
#define b(i)
Definition RSha256.hxx:100
long long Long64_t
Definition RtypesCore.h:73
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
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
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:296
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
std::vector< std::string > ColumnNames_t
std::vector< std::string > ReplaceDotWithUnderscore(const std::vector< std::string > &columnNames)
Replace occurrences of '.
Definition RDFUtils.cxx:299
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:286
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, RDataSource *ds, RDefineBase *define, bool vector2rvec)
Return a string containing the type of the given branch.
Definition RDFUtils.cxx:223
char TypeName2ROOTTypeName(const std::string &b)
Convert type name (e.g.
Definition RDFUtils.cxx:255
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 IsInternalColumn(std::string_view colName)
Definition RDFUtils.cxx:347
Long64_t InterpreterCalc(const std::string &code, const std::string &context)
Definition RDFUtils.cxx:330
void InterpreterDeclare(const std::string &code)
Definition RDFUtils.cxx:318
double T(double x)
ROOT type_traits extensions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Lightweight storage for a collection of types.