11 #ifndef ROOT_RDF_TINTERFACE_UTILS 12 #define ROOT_RDF_TINTERFACE_UTILS 25 #include <initializer_list> 30 #include <type_traits> 59 class RIgnoreErrorLevelRAII {
64 RIgnoreErrorLevelRAII(
int errorIgnoreLevel) {
gErrorIgnoreLevel = errorIgnoreLevel; }
73 namespace ActionTypes {
88 template <
int D,
typename P,
template <
int,
typename,
template <
typename>
class>
class...
S>
93 struct IsV7Hist :
public std::false_type {
94 static_assert(std::is_base_of<TH1, T>::value,
"not implemented for this type");
97 template <
int D,
typename P,
template <
int,
typename,
template <
typename>
class>
class...
S>
98 struct IsV7Hist<THist<D,
P,
S...>> :
public std::true_type {
101 template <typename T, bool ISV7HISTO = IsV7Hist<T>::value>
103 static void SetCanExtendAllAxes(
T &
h) { h.SetCanExtend(::
TH1::kAllAxes); }
104 static bool HasAxisLimits(
T &h)
106 auto xaxis = h.GetXaxis();
107 return !(xaxis->GetXmin() == 0. && xaxis->GetXmax() == 0.);
111 template <
typename T>
112 struct HistoUtils<
T, true> {
113 static void SetCanExtendAllAxes(
T &) {}
114 static bool HasAxisLimits(
T &) {
return true; }
118 template <
typename... BranchTypes,
typename ActionType,
typename ActionResultType,
typename PrevNodeType>
119 RActionBase *BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<ActionResultType> &h,
120 const unsigned int nSlots,
RLoopManager &loopManager, PrevNodeType &prevNode, ActionType *)
122 using Helper_t = FillTOHelper<ActionResultType>;
123 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchTypes...>>;
124 auto action = std::make_shared<Action_t>(Helper_t(h, nSlots), bl, prevNode);
125 loopManager.
Book(action);
130 template <
typename... BranchTypes,
typename PrevNodeType>
131 RActionBase *BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<::TH1D> &h,
const unsigned int nSlots,
132 RLoopManager &loopManager, PrevNodeType &prevNode, ActionTypes::Histo1D *)
134 auto hasAxisLimits = HistoUtils<::TH1D>::HasAxisLimits(*h);
136 RActionBase *actionBase;
138 using Helper_t = FillTOHelper<::TH1D>;
139 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchTypes...>>;
140 auto action = std::make_shared<Action_t>(Helper_t(h, nSlots), bl, prevNode);
141 loopManager.
Book(action);
142 actionBase = action.get();
144 using Helper_t = FillHelper;
145 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchTypes...>>;
146 auto action = std::make_shared<Action_t>(Helper_t(h, nSlots), bl, prevNode);
147 loopManager.
Book(action);
148 actionBase = action.get();
155 template <
typename BranchType,
typename PrevNodeType,
typename ActionResultType>
157 BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<ActionResultType> &minV,
const unsigned int nSlots,
160 using Helper_t = MinHelper<ActionResultType>;
161 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchType>>;
162 auto action = std::make_shared<Action_t>(Helper_t(minV, nSlots), bl, prevNode);
163 loopManager.
Book(action);
168 template <
typename BranchType,
typename PrevNodeType,
typename ActionResultType>
170 BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<ActionResultType> &maxV,
const unsigned int nSlots,
173 using Helper_t = MaxHelper<ActionResultType>;
174 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchType>>;
175 auto action = std::make_shared<Action_t>(Helper_t(maxV, nSlots), bl, prevNode);
176 loopManager.
Book(action);
181 template <
typename BranchType,
typename PrevNodeType,
typename ActionResultType>
183 BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<ActionResultType> &sumV,
const unsigned int nSlots,
186 using Helper_t = SumHelper<ActionResultType>;
187 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchType>>;
188 auto action = std::make_shared<Action_t>(Helper_t(sumV, nSlots), bl, prevNode);
189 loopManager.
Book(action);
194 template <
typename BranchType,
typename PrevNodeType>
195 RActionBase *BuildAndBook(
const ColumnNames_t &bl,
const std::shared_ptr<double> &meanV,
const unsigned int nSlots,
198 using Helper_t = MeanHelper;
199 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<BranchType>>;
200 auto action = std::make_shared<Action_t>(Helper_t(meanV, nSlots), bl, prevNode);
201 loopManager.
Book(action);
206 template <
typename Filter>
207 void CheckFilter(
Filter &)
209 using FilterRet_t =
typename RDF::CallableTraits<Filter>::ret_type;
210 static_assert(std::is_same<FilterRet_t, bool>::value,
"filter functions must return a bool");
214 const ColumnNames_t &dataSourceColumns);
216 using TmpBranchBasePtr_t = std::shared_ptr<RCustomColumnBase>;
220 const std::map<std::string, std::string> &aliasMap,
const ColumnNames_t &
branches,
221 const ColumnNames_t &customColumns, TTree *
tree,
RDataSource *ds,
unsigned int namespaceID);
225 std::string
JitBuildAndBook(
const ColumnNames_t &bl,
const std::string &prevNodeTypename,
void *prevNode,
226 const std::type_info &art,
const std::type_info &at,
const void *
r, TTree *
tree,
227 const unsigned int nSlots,
const ColumnNames_t &customColumns,
RDataSource *ds,
228 const std::shared_ptr<RActionBase *> *
const actionPtrPtr,
unsigned int namespaceID);
236 template <
typename T>
237 std::shared_ptr<T> *MakeSharedOnHeap(
const std::shared_ptr<T> &shPtr)
239 return new std::shared_ptr<T>(shPtr);
246 std::shared_ptr<RFilterBase>
UpcastNode(
const std::shared_ptr<RFilterBase> ptr);
247 std::shared_ptr<RCustomColumnBase>
UpcastNode(
const std::shared_ptr<RCustomColumnBase> ptr);
248 std::shared_ptr<RRangeBase>
UpcastNode(
const std::shared_ptr<RRangeBase> ptr);
249 std::shared_ptr<RLoopManager>
UpcastNode(
const std::shared_ptr<RLoopManager> ptr);
250 std::shared_ptr<RJittedFilter>
UpcastNode(
const std::shared_ptr<RJittedFilter> ptr);
253 const ColumnNames_t &datasetColumns,
const ColumnNames_t &validCustomColumns,
256 std::vector<bool>
FindUndefinedDSColumns(
const ColumnNames_t &requestedCols,
const ColumnNames_t &definedDSCols);
259 template <
typename T>
263 auto getValue = [readers](
unsigned int slot) {
return *readers[slot]; };
265 lm.
Book(std::make_shared<NewCol_t>(name, std::move(getValue), ColumnNames_t{}, &lm,
true));
271 template <
typename... ColumnTypes, std::size_t... S>
276 if (std::none_of(mustBeDefined.begin(), mustBeDefined.end(), [](
bool b) {
return b; })) {
281 std::initializer_list<int> expander{
282 (mustBeDefined[
S] ? DefineDSColumnHelper<ColumnTypes>(columns[
S], lm, ds) : ((
void)0), 0)...};
288 template <
typename F,
typename PrevNode>
295 constexpr
auto nColumns = ColTypes_t::list_size;
296 RDFInternal::CheckFilter(
f);
302 jittedFilter->
SetFilter(std::make_unique<F_t>(std::move(
f), cols, *prevNode, name));
305 template <
typename F>
310 constexpr
auto nColumns = ColTypes_t::list_size;
316 lm->
Book(std::make_shared<NewCol_t>(name, std::move(
f), cols, lm));
320 template <
typename ActionType,
typename... BranchTypes,
typename PrevNodeType,
typename ActionResultType>
321 void CallBuildAndBook(PrevNodeType &prevNode,
const ColumnNames_t &bl,
const unsigned int nSlots,
322 const std::shared_ptr<ActionResultType> *rOnHeap,
323 const std::shared_ptr<RActionBase *> *actionPtrPtrOnHeap)
327 using ColTypes_t =
TypeList<BranchTypes...>;
328 constexpr
auto nColumns = ColTypes_t::list_size;
332 RActionBase *actionPtr =
333 BuildAndBook<BranchTypes...>(bl, *rOnHeap, nSlots, loopManager, prevNode, (ActionType *)
nullptr);
334 **actionPtrPtrOnHeap = actionPtr;
336 delete actionPtrPtrOnHeap;
340 template <typename T, bool Container = TTraits::IsContainer<T>::value && !std::is_same<T, std::string>::value>
341 struct TMinReturnType {
346 struct TMinReturnType<TInferType, false> {
350 template <
typename T>
351 struct TMinReturnType<
T, true> {
356 template <
typename R,
typename F,
typename... Args>
359 return [
f](
unsigned int, Args...
a) ->
R {
return f(
a...); };
362 template <
typename BranchType,
typename... Rest>
363 struct TNeedJitting {
364 static constexpr
bool value = TNeedJitting<Rest...>::value;
367 template <
typename... Rest>
368 struct TNeedJitting<TInferType, Rest...> {
369 static constexpr
bool value =
true;
372 template <
typename T>
373 struct TNeedJitting<
T> {
374 static constexpr
bool value =
false;
378 struct TNeedJitting<TInferType> {
379 static constexpr
bool value =
true;
391 typename mergeArgsNoDecay_t =
typename CallableTraits<Merge>::arg_types_nodecay,
392 typename mergeArgs_t =
typename CallableTraits<Merge>::arg_types,
393 typename mergeRet_t =
typename CallableTraits<Merge>::ret_type>
396 constexpr
bool isAggregatorOk =
397 (std::is_same<R, decayedU>::value) || (std::is_same<R, void>::value && std::is_lvalue_reference<U>::value);
398 static_assert(isAggregatorOk,
"aggregator function must have signature `U(U,T)` or `void(U&,T)`");
399 constexpr
bool isMergeOk =
400 (std::is_same<TypeList<decayedU, decayedU>, mergeArgs_t>::value && std::is_same<decayedU, mergeRet_t>::value) ||
401 (std::is_same<
TypeList<std::vector<decayedU> &>, mergeArgsNoDecay_t>::value &&
402 std::is_same<void, mergeRet_t>::value);
403 static_assert(isMergeOk,
"merge function must have signature `U(U,U)` or `void(std::vector<U>&)`");
408 template <
typename R,
typename T>
409 void CheckAggregate(
T)
411 static_assert(
sizeof(
T) == 0,
"aggregator function must take exactly two arguments");
419 const ColumnNames_t
SelectColumns(
unsigned int nArgs,
const ColumnNames_t &bl,
const ColumnNames_t &defBl);
422 ColumnNames_t
FindUnknownColumns(
const ColumnNames_t &requiredCols,
const ColumnNames_t &datasetColumns,
423 const ColumnNames_t &definedCols,
const ColumnNames_t &dataSourceColumns);
431 template <
bool... bs>
432 using IsTrueForAllImpl_t =
typename std::is_same<TBoolPack<bs...,
true>, TBoolPack<
true, bs...>>;
434 template <
bool... Conditions>
436 static constexpr
bool value = IsTrueForAllImpl_t<Conditions...>::value;
443 struct IsList_t : std::false_type {};
445 template <
typename T>
446 struct IsList_t<
std::list<T>> : std::true_type {};
449 struct IsDeque_t : std::false_type {};
451 template <
typename T>
452 struct IsDeque_t<std::deque<T>> : std::true_type {};
462 template <
typename T>
465 template <
typename T>
466 using MaxReturnType_t = MinReturnType_t<T>;
468 template <
typename T>
469 using SumReturnType_t = MinReturnType_t<T>;
std::string JitBuildAndBook(const ColumnNames_t &bl, const std::string &prevNodeTypename, void *prevNode, const std::type_info &art, const std::type_info &at, const void *rOnHeap, TTree *tree, const unsigned int nSlots, const ColumnNames_t &customColumns, RDataSource *ds, const std::shared_ptr< RActionBase *> *const actionPtrPtr, unsigned int namespaceID)
void BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm, RDataSource *ds)
void AddCustomColumnName(std::string_view name)
ColumnNames_t GetTopLevelBranchNames(TTree &t)
Get all the top-level branches names, including the ones of the friend trees.
bool AtLeastOneEmptyString(const std::vector< std::string_view > strings)
R__EXTERN Int_t gErrorIgnoreLevel
Namespace for new ROOT classes and functions.
bool IsInternalColumn(std::string_view colName)
typename TakeFirstParameter< T >::type TakeFirstParameter_t
Short_t Min(Short_t a, Short_t b)
#define R(a, b, c, d, e, f, g, h, i)
const ColumnNames_t SelectColumns(unsigned int nRequiredNames, const ColumnNames_t &names, const ColumnNames_t &defaultNames)
Choose between local column names or default column names, throw in case of errors.
void AddDataSourceColumn(std::string_view name)
RLoopManager * GetLoopManagerUnchecked()
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
A wrapper around a concrete RFilter, which forwards all calls to it RJittedFilter is the type of the ...
RVec< T > Filter(const RVec< T > &v, F &&f)
Create a new collection with the elements passing the filter expressed by the predicate.
ColumnNames_t FindUnknownColumns(const ColumnNames_t &requiredCols, const ColumnNames_t &datasetColumns, const ColumnNames_t &definedCols, const ColumnNames_t &dataSourceColumns)
RLoopManager * GetLoopManagerUnchecked() const
std::vector< bool > FindUndefinedDSColumns(const ColumnNames_t &requestedCols, const ColumnNames_t &definedCols)
Return a bitset each element of which indicates whether the corresponding element in selectedColumns ...
RooArgSet S(const RooAbsArg &v1)
const ColumnNames_t & GetCustomColumnNames() const
std::shared_ptr< RFilterBase > UpcastNode(const std::shared_ptr< RFilterBase > ptr)
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Return the weighted mean of an array a with length n.
void BookFilterJit(RJittedFilter *jittedFilter, void *prevNode, std::string_view prevNodeTypeName, std::string_view name, std::string_view expression, const std::map< std::string, std::string > &aliasMap, const ColumnNames_t &branches, const ColumnNames_t &customCols, TTree *tree, RDataSource *ds, unsigned int namespaceID)
ColumnNames_t GetValidatedColumnNames(RLoopManager &lm, const unsigned int nColumns, const ColumnNames_t &columns, const ColumnNames_t &datasetColumns, const ColumnNames_t &validCustomColumns, RDataSource *ds)
Given the desired number of columns and the user-provided list of columns:
Lightweight storage for a collection of types.
basic_string_view< char > string_view
ROOT type_traits extensions.
void CheckCustomColumn(std::string_view definedCol, TTree *treePtr, const ColumnNames_t &customCols, const ColumnNames_t &dataSourceColumns)
Extract types from the signature of a callable object. See CallableTraits.
T Sum(const RVec< T > &v)
Sum elements.
typedef void((*Func_t)())
RDataSource * GetDataSource() const
void CheckTypesAndPars(unsigned int nTemplateParams, unsigned int nColumnNames)
void SetFilter(std::unique_ptr< RFilterBase > f)
Short_t Max(Short_t a, Short_t b)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
std::vector< T ** > GetColumnReaders(std::string_view columnName)
Called at most once per column by RDF.
A TTree object has a header with a name and a title.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
make_integer_sequence< size_t, _Np > make_index_sequence
ColumnNames_t GetBranchNames(TTree &t)
Get all the branches names, including the ones of the friend trees.
void Book(const ActionBasePtr_t &actionPtr)