Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
InterfaceUtils.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 02/2018
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_RDF_TINTERFACE_UTILS
12#define ROOT_RDF_TINTERFACE_UTILS
13
14#include <ROOT/RDF/RAction.hxx>
16#include <ROOT/RDF/ActionHelpers.hxx> // for BuildAction
19#include <ROOT/RDF/RDefine.hxx>
21#include <ROOT/RDF/RFilter.hxx>
22#include <ROOT/RDF/Utils.hxx>
28#include <string_view>
30#include <ROOT/TypeTraits.hxx>
31#include <RConfigure.h> // for R__HAS_ROOT7
32#include <TError.h> // gErrorIgnoreLevel
33#include <TH1.h>
34#include <TROOT.h> // IsImplicitMTEnabled
35
36#include <deque>
37#include <functional>
38#include <list>
39#include <memory>
40#include <string>
41#include <type_traits>
42#include <typeinfo>
43#include <vector>
44
45class TTree;
46namespace ROOT {
47namespace Detail {
48namespace RDF {
49class RNodeBase;
50}
51}
52namespace RDF {
53template <typename Proxied>
54class RInterface;
56} // namespace RDF
57
58} // namespace ROOT
59
60/// \cond HIDDEN_SYMBOLS
61
62namespace ROOT {
63namespace Internal {
64namespace RDF {
65using namespace ROOT::Detail::RDF;
66using namespace ROOT::RDF;
67namespace TTraits = ROOT::TypeTraits;
68
69std::string DemangleTypeIdName(const std::type_info &typeInfo);
70
71ColumnNames_t
72ConvertRegexToColumns(const ColumnNames_t &colNames, std::string_view columnNameRegexp, std::string_view callerName);
73
74/// An helper object that sets and resets gErrorIgnoreLevel via RAII.
76private:
78
79public:
82};
83
84/****** BuildAction overloads *******/
85
86// clang-format off
87/// This namespace defines types to be used for tag dispatching in RInterface.
88namespace ActionTags {
89struct Histo1D{};
90struct Histo2D{};
91struct Histo3D{};
92struct HistoND{};
93struct HistoNSparseD{};
94struct Hist{};
95struct HistWithWeight{};
96struct Graph{};
97struct GraphAsymmErrors{};
98struct Profile1D{};
99struct Profile2D{};
100struct Min{};
101struct Max{};
102struct Sum{};
103struct Mean{};
104struct Fill{};
105struct StdDev{};
106struct Median{};
107struct Display{};
108struct Snapshot{};
109struct Book{};
110}
111// clang-format on
112
113template <typename T, bool ISV6HISTO = std::is_base_of<TH1, std::decay_t<T>>::value>
114struct HistoUtils {
115 static bool HasAxisLimits(T &h)
116 {
117 auto xaxis = h.GetXaxis();
118 return !(xaxis->GetXmin() == 0. && xaxis->GetXmax() == 0.);
119 }
120};
121
122template <typename T>
123struct HistoUtils<T, false> {
124 static bool HasAxisLimits(T &) { return true; }
125};
126
127// Generic filling (covers Histo2D, HistoND, HistoNSparseD, Profile1D and Profile2D actions, with and without weights)
128template <typename... ColTypes, typename ActionTag, typename ActionResultType, typename PrevNodeType>
129std::unique_ptr<RActionBase>
130BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &h, const unsigned int nSlots,
131 std::shared_ptr<PrevNodeType> prevNode, ActionTag, const RColumnRegister &colRegister)
132{
134 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
135 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
136}
137
138// Histo1D filling (must handle the special case of distinguishing FillHelper and BufferedFillHelper
139template <typename... ColTypes, typename PrevNodeType>
140std::unique_ptr<RActionBase>
141BuildAction(const ColumnNames_t &bl, const std::shared_ptr<::TH1D> &h, const unsigned int nSlots,
142 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Histo1D, const RColumnRegister &colRegister)
143{
145
148 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
149 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
150 } else {
152 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
153 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
154 }
155}
156
157// Action for Histo3D, where thread safe filling might be supported to save memory
158template <typename... ColTypes, typename ActionResultType, typename PrevNodeType>
159std::unique_ptr<RActionBase>
160BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &h, const unsigned int nSlots,
161 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Histo3D, const RColumnRegister &colRegister)
162{
163 if (RDFInternal::NThreadPerTH3() <= 1 || nSlots == 1) {
165 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
166 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
167 } else {
169 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
170 if constexpr (sizeof...(ColTypes) > 3) {
171 h->Sumw2();
172 }
173 const auto histoSlots = std::max(nSlots / RDFInternal::NThreadPerTH3(), 1u);
174 return std::make_unique<Action_t>(Helper_t(h, histoSlots), bl, std::move(prevNode), colRegister);
175 }
176}
177
178#ifdef R__HAS_ROOT7
179// Action for RHist using RHistConcurrentFiller without weights
180template <typename... ColTypes, typename BinContentType, typename PrevNodeType>
181std::unique_ptr<RActionBase>
182BuildAction(const ColumnNames_t &columnList, const std::shared_ptr<ROOT::Experimental::RHist<BinContentType>> &h,
183 const unsigned int nSlots, std::shared_ptr<PrevNodeType> prevNode, ActionTags::Hist,
184 const RColumnRegister &colRegister)
185{
187 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
188 return std::make_unique<Action_t>(Helper_t(h, nSlots), columnList, std::move(prevNode), colRegister);
189}
190
191// Action for RHist using RHistConcurrentFiller with weights
192template <typename... ColTypes, typename BinContentType, typename PrevNodeType>
193std::unique_ptr<RActionBase>
194BuildAction(const ColumnNames_t &columnList, const std::shared_ptr<ROOT::Experimental::RHist<BinContentType>> &h,
195 const unsigned int nSlots, std::shared_ptr<PrevNodeType> prevNode, ActionTags::HistWithWeight,
196 const RColumnRegister &colRegister)
197{
199 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
200 return std::make_unique<Action_t>(Helper_t(h, nSlots), columnList, std::move(prevNode), colRegister);
201}
202
203// Action for RHistEngine using FillAtomic without weights
204template <typename... ColTypes, typename BinContentType, typename PrevNodeType>
205std::unique_ptr<RActionBase>
206BuildAction(const ColumnNames_t &columnList, const std::shared_ptr<ROOT::Experimental::RHistEngine<BinContentType>> &h,
207 const unsigned int, std::shared_ptr<PrevNodeType> prevNode, ActionTags::Hist,
208 const RColumnRegister &colRegister)
209{
211 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
212 return std::make_unique<Action_t>(Helper_t(h), columnList, std::move(prevNode), colRegister);
213}
214
215// Action for RHistEngine using FillAtomic with weights
216template <typename... ColTypes, typename BinContentType, typename PrevNodeType>
217std::unique_ptr<RActionBase>
218BuildAction(const ColumnNames_t &columnList, const std::shared_ptr<ROOT::Experimental::RHistEngine<BinContentType>> &h,
219 const unsigned int, std::shared_ptr<PrevNodeType> prevNode, ActionTags::HistWithWeight,
220 const RColumnRegister &colRegister)
221{
223 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
224 return std::make_unique<Action_t>(Helper_t(h), columnList, std::move(prevNode), colRegister);
225}
226#endif
227
228template <typename... ColTypes, typename PrevNodeType>
229std::unique_ptr<RActionBase>
230BuildAction(const ColumnNames_t &bl, const std::shared_ptr<TGraph> &g, const unsigned int nSlots,
231 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Graph, const RColumnRegister &colRegister)
232{
234 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
235 return std::make_unique<Action_t>(Helper_t(g, nSlots), bl, std::move(prevNode), colRegister);
236}
237
238template <typename... ColTypes, typename PrevNodeType>
239std::unique_ptr<RActionBase>
240BuildAction(const ColumnNames_t &bl, const std::shared_ptr<TGraphAsymmErrors> &g, const unsigned int nSlots,
241 std::shared_ptr<PrevNodeType> prevNode, ActionTags::GraphAsymmErrors, const RColumnRegister &colRegister)
242{
244 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
245 return std::make_unique<Action_t>(Helper_t(g, nSlots), bl, std::move(prevNode), colRegister);
246}
247
248// Min action
249template <typename ColType, typename PrevNodeType, typename ActionResultType>
250std::unique_ptr<RActionBase>
251BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &minV, const unsigned int nSlots,
252 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Min, const RColumnRegister &colRegister)
253{
256 return std::make_unique<Action_t>(Helper_t(minV, nSlots), bl, std::move(prevNode), colRegister);
257}
258
259// Max action
260template <typename ColType, typename PrevNodeType, typename ActionResultType>
261std::unique_ptr<RActionBase>
262BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &maxV, const unsigned int nSlots,
263 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Max, const RColumnRegister &colRegister)
264{
267 return std::make_unique<Action_t>(Helper_t(maxV, nSlots), bl, std::move(prevNode), colRegister);
268}
269
270// Sum action
271template <typename ColType, typename PrevNodeType, typename ActionResultType>
272std::unique_ptr<RActionBase>
273BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &sumV, const unsigned int nSlots,
274 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Sum, const RColumnRegister &colRegister)
275{
278 return std::make_unique<Action_t>(Helper_t(sumV, nSlots), bl, std::move(prevNode), colRegister);
279}
280
281// Mean action
282template <typename ColType, typename PrevNodeType>
283std::unique_ptr<RActionBase>
284BuildAction(const ColumnNames_t &bl, const std::shared_ptr<double> &meanV, const unsigned int nSlots,
285 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Mean, const RColumnRegister &colRegister)
286{
287 using Helper_t = MeanHelper;
289 return std::make_unique<Action_t>(Helper_t(meanV, nSlots), bl, std::move(prevNode), colRegister);
290}
291
292// Standard Deviation action
293template <typename ColType, typename PrevNodeType>
294std::unique_ptr<RActionBase>
295BuildAction(const ColumnNames_t &bl, const std::shared_ptr<double> &stdDeviationV, const unsigned int nSlots,
296 std::shared_ptr<PrevNodeType> prevNode, ActionTags::StdDev, const RColumnRegister &colRegister)
297{
298 using Helper_t = StdDevHelper;
300 return std::make_unique<Action_t>(Helper_t(stdDeviationV, nSlots), bl, prevNode, colRegister);
301}
302
303// Median action
304template <typename ColType, typename PrevNodeType>
305std::unique_ptr<RActionBase>
306BuildAction(const ColumnNames_t &bl, const std::shared_ptr<double> &meanV, const unsigned int nSlots,
307 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Median, const RColumnRegister &colRegister)
308{
309 using Helper_t = MedianHelper;
311 return std::make_unique<Action_t>(Helper_t(meanV, nSlots), bl, std::move(prevNode), colRegister);
312}
313
314using displayHelperArgs_t = std::pair<size_t, std::shared_ptr<ROOT::RDF::RDisplay>>;
315
316// Display action
317template <typename... ColTypes, typename PrevNodeType>
318std::unique_ptr<RActionBase>
319BuildAction(const ColumnNames_t &bl, const std::shared_ptr<displayHelperArgs_t> &helperArgs, const unsigned int,
320 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Display, const RColumnRegister &colRegister)
321{
323 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
324 return std::make_unique<Action_t>(Helper_t(helperArgs->first, helperArgs->second, prevNode), bl, prevNode,
326}
327
328struct SnapshotHelperArgs {
329 std::string fFileName;
330 std::string fDirName;
331 std::string fTreeName;
332 std::vector<std::string> fOutputColNames;
334 ROOT::Detail::RDF::RLoopManager *fOutputLoopManager;
335 ROOT::Detail::RDF::RLoopManager *fInputLoopManager;
336 bool fToNTuple;
337 bool fIncludeVariations;
338};
339
340template <typename PrevNodeType>
341std::unique_ptr<RActionBase>
342BuildAction(const ColumnNames_t &colNames, const std::shared_ptr<SnapshotHelperArgs> &snapHelperArgs,
343 const unsigned int nSlots, std::shared_ptr<PrevNodeType> prevNode, const RColumnRegister &colRegister,
344 const std::vector<const std::type_info *> &colTypeIDs)
345{
346 const auto &filename = snapHelperArgs->fFileName;
347 const auto &dirname = snapHelperArgs->fDirName;
348 const auto &treename = snapHelperArgs->fTreeName;
349 const auto &outputColNames = snapHelperArgs->fOutputColNames;
350 const auto &options = snapHelperArgs->fOptions;
351 const auto &outputLM = snapHelperArgs->fOutputLoopManager;
352 const auto &inputLM = snapHelperArgs->fInputLoopManager;
353
354 auto sz = colNames.size();
355 std::vector<bool> isDefine(sz);
356 for (auto i = 0u; i < sz; ++i)
357 isDefine[i] = colRegister.IsDefineOrAlias(colNames[i]);
358
359 std::unique_ptr<RActionBase> actionPtr;
360 if (snapHelperArgs->fToNTuple) {
361 // We use the same helper for single- and multi-thread snapshot.
362 using Helper_t = UntypedSnapshotRNTupleHelper;
364
367 colNames, colTypeIDs, prevNode, colRegister));
368 } else {
370 // single-thread snapshot
371 if (snapHelperArgs->fIncludeVariations) {
372 using Helper_t = SnapshotHelperWithVariations;
375 std::move(isDefine), outputLM, inputLM, colTypeIDs),
376 colNames, colTypeIDs, prevNode, colRegister));
377 } else {
378 using Helper_t = UntypedSnapshotTTreeHelper;
381 std::move(isDefine), outputLM, inputLM, colTypeIDs),
382 colNames, colTypeIDs, prevNode, colRegister));
383 }
384 } else {
385 if (snapHelperArgs->fIncludeVariations) {
386 throw std::invalid_argument("Multi-threaded snapshot with variations is not supported yet.");
387 }
388 // multi-thread snapshot
389 using Helper_t = UntypedSnapshotTTreeHelperMT;
392 std::move(isDefine), outputLM, inputLM, colTypeIDs),
393 colNames, colTypeIDs, prevNode, colRegister));
394 }
395 }
396
397 return actionPtr;
398}
399
400// Book with custom helper type
401template <typename... ColTypes, typename PrevNodeType, typename Helper_t>
402std::unique_ptr<RActionBase>
403BuildAction(const ColumnNames_t &bl, const std::shared_ptr<Helper_t> &h, const unsigned int /*nSlots*/,
404 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Book, const RColumnRegister &colRegister)
405{
406 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
407 return std::make_unique<Action_t>(Helper_t(std::move(*h)), bl, std::move(prevNode), colRegister);
408}
409
410/****** end BuildAndBook ******/
411
412template <typename Filter>
413void CheckFilter(Filter &)
414{
415 using FilterRet_t = typename RDF::CallableTraits<Filter>::ret_type;
416 static_assert(std::is_convertible<FilterRet_t, bool>::value,
417 "filter expression returns a type that is not convertible to bool");
418}
419
420ColumnNames_t FilterArraySizeColNames(const ColumnNames_t &columnNames, const std::string &action);
421
422void CheckValidCppVarName(std::string_view var, const std::string &where);
423
424void CheckForRedefinition(const std::string &where, std::string_view definedCol, const RColumnRegister &colRegister,
425 const ColumnNames_t &dataSourceColumns);
426
427void CheckForDefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &colRegister,
428 const ColumnNames_t &dataSourceColumns);
429
430void CheckForNoVariations(const std::string &where, std::string_view definedColView,
431 const RColumnRegister &colRegister);
432
433std::string PrettyPrintAddr(const void *const addr);
434
435std::shared_ptr<RJittedFilter> BookFilterJit(std::shared_ptr<RNodeBase> prevNode, std::string_view name,
436 std::string_view expression, const RColumnRegister &colRegister,
437 TTree *tree, RDataSource *ds);
438
439std::shared_ptr<RJittedDefine> BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm,
440 RDataSource *ds, const RColumnRegister &colRegister);
441
442std::shared_ptr<RJittedDefine> BookDefinePerSampleJit(std::string_view name, std::string_view expression,
443 RLoopManager &lm, const RColumnRegister &colRegister);
444
445std::shared_ptr<RJittedVariation>
446BookVariationJit(const std::vector<std::string> &colNames, std::string_view variationName,
447 const std::vector<std::string> &variationTags, std::string_view expression, RLoopManager &lm,
448 RDataSource *ds, const RColumnRegister &colRegister, bool isSingleColumn,
449 const std::string &varyColType);
450
451std::string JitBuildAction(const ColumnNames_t &bl, const std::type_info &art, const std::type_info &at, TTree *tree,
452 const unsigned int nSlots, const RColumnRegister &colRegister, RDataSource *ds,
453 const bool vector2RVec = true);
454
455bool AtLeastOneEmptyString(const std::vector<std::string_view> strings);
456
457/// Take a shared_ptr<AnyNodeType> and return a shared_ptr<RNodeBase>.
458/// This works for RLoopManager nodes as well as filters and ranges.
459std::shared_ptr<RNodeBase> UpcastNode(std::shared_ptr<RNodeBase> ptr);
460
461ColumnNames_t GetValidatedColumnNames(RLoopManager &lm, const unsigned int nColumns, const ColumnNames_t &columns,
462 const RColumnRegister &validDefines, RDataSource *ds);
463
464std::vector<std::string> GetValidatedArgTypes(const ColumnNames_t &colNames, const RColumnRegister &colRegister,
465 TTree *tree, RDataSource *ds, const std::string &context,
466 bool vector2RVec);
467
468template <typename T>
469void AddDSColumnsHelper(const std::string &colName, RLoopManager &lm, RDataSource &ds, RColumnRegister &colRegister)
470{
471
472 if (colRegister.IsDefineOrAlias(colName))
473 return;
474
475 if (lm.HasDataSourceColumnReaders(colName, typeid(T)))
476 return;
477
478 if (!ds.HasColumn(colName) &&
479 lm.GetSuppressErrorsForMissingBranches().find(colName) == lm.GetSuppressErrorsForMissingBranches().end())
480 return;
481
482 const auto nSlots = lm.GetNSlots();
483 std::vector<std::unique_ptr<RColumnReaderBase>> colReaders;
484 colReaders.reserve(nSlots);
485
486 const auto valuePtrs = ds.GetColumnReaders<T>(colName);
487 if (!valuePtrs.empty()) { // we are using the old GetColumnReaders mechanism in this RDataSource
488 for (auto *ptr : valuePtrs)
489 colReaders.emplace_back(new RDSColumnReader<T>(ptr));
490
491 } else { // using the new GetColumnReaders mechanism
492 // TODO consider changing the interface so we return all of these for all slots in one go
493 for (auto slot = 0u; slot < lm.GetNSlots(); ++slot)
494 colReaders.emplace_back(
495 ROOT::Internal::RDF::CreateColumnReader(ds, slot, colName, typeid(T), /*treeReader*/ nullptr));
496 }
497
498 lm.AddDataSourceColumnReaders(colName, std::move(colReaders), typeid(T));
499}
500
501/// Take list of column names that must be defined, current map of custom columns, current list of defined column names,
502/// and return a new map of custom columns (with the new datasource columns added to it)
503template <typename... ColumnTypes>
504void AddDSColumns(const std::vector<std::string> &requiredCols, RLoopManager &lm, RDataSource &ds,
506{
507 // hack to expand a template parameter pack without c++17 fold expressions.
508 using expander = int[];
509 int i = 0;
511}
512
513void AddDSColumns(const std::vector<std::string> &requiredCols, ROOT::Detail::RDF::RLoopManager &lm,
514 ROOT::RDF::RDataSource &ds, const std::vector<const std::type_info *> &colTypeIDs,
516
517// this function is meant to be called by the jitted code generated by BookFilterJit
518template <typename F>
519void JitFilterHelper(F &&f, const ColumnNames_t &cols, RColumnRegister &colRegister,
521{
522 if (!jittedFilter) {
523 // The branch of the computation graph that needed this jitted code went out of scope between the type
524 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
525 return;
526 }
527
528 // mock Filter logic -- validity checks and Define-ition of RDataSource columns
529 using Callable_t = std::decay_t<F>;
530 auto prevNode = jittedFilter->MoveOutPrevNode();
531 using PrevNode_t = typename decltype(prevNode)::element_type;
533 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
534 constexpr auto nColumns = ColTypes_t::list_size;
535 CheckFilter(f);
536
537 auto ds = lm.GetDataSource();
538
539 if (ds != nullptr && !cols.empty())
541
542 jittedFilter->SetFilter(
543 std::unique_ptr<RFilterBase>(new F_t(std::forward<F>(f), cols, prevNode, colRegister, jittedFilter->GetName())));
544}
545
546namespace DefineTypes {
547struct RDefineTag {};
548struct RDefinePerSampleTag {};
549}
550
551template <typename F>
552auto MakeDefineNode(DefineTypes::RDefineTag, std::string_view name, std::string_view dummyType, F &&f,
553 const ColumnNames_t &cols, RColumnRegister &colRegister, RLoopManager &lm)
554{
555 return std::unique_ptr<RDefineBase>(new RDefine<std::decay_t<F>, ExtraArgsForDefine::None>(
556 name, dummyType, std::forward<F>(f), cols, colRegister, lm));
557}
558
559template <typename F>
560auto MakeDefineNode(DefineTypes::RDefinePerSampleTag, std::string_view name, std::string_view dummyType, F &&f,
561 const ColumnNames_t &, RColumnRegister &, RLoopManager &lm)
562{
563 return std::unique_ptr<RDefineBase>(
564 new RDefinePerSample<std::decay_t<F>>(name, dummyType, std::forward<F>(f), lm));
565}
566
567// Build a RDefine or a RDefinePerSample object and attach it to an existing RJittedDefine
568// This function is meant to be called by jitted code right before starting the event loop.
569// If colsPtr is null, build a RDefinePerSample (it has no input columns), otherwise a RDefine.
570template <typename RDefineTypeTag, typename F>
571void JitDefineHelper(F &&f, const ColumnNames_t &cols, RColumnRegister &colRegister,
573{
574
575 if (!jittedDefine) {
576 // The branch of the computation graph that needed this jitted code went out of scope between the type
577 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
578 return;
579 }
580
581 using Callable_t = std::decay_t<F>;
582 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
583
584 auto ds = lm.GetDataSource();
585 if (ds != nullptr && !cols.empty())
587
588 // will never actually be used (trumped by jittedDefine->GetTypeName()), but we set it to something meaningful
589 // to help devs debugging
590 const auto dummyType = "jittedCol_t";
591 // use unique_ptr<RDefineBase> instead of make_unique<NewCol_t> to reduce jit/compile-times
592 std::unique_ptr<RDefineBase> newCol{
593 MakeDefineNode(RDefineTypeTag{}, jittedDefine->GetName(), dummyType, std::forward<F>(f), cols, colRegister, lm)};
594 jittedDefine->SetDefine(std::move(newCol));
595}
596
597template <bool IsSingleColumn, typename F>
598void JitVariationHelper(F &&f, const ColumnNames_t &inputColNames, RColumnRegister &colRegister,
600 const ColumnNames_t &variedColNames, const ColumnNames_t &variationTags) noexcept
601{
602
603 if (!jittedVariation) {
604 // The branch of the computation graph that needed this jitted variation went out of scope between the type
605 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
606 return;
607 }
608
609 using Callable_t = std::decay_t<F>;
610 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
611
612 auto ds = lm.GetDataSource();
613 if (ds != nullptr && !inputColNames.empty())
615
616 // use unique_ptr<RDefineBase> instead of make_unique<NewCol_t> to reduce jit/compile-times
617 std::unique_ptr<RVariationBase> newVariation{new RVariation<std::decay_t<F>, IsSingleColumn>(
618 variedColNames, jittedVariation->GetVariationName(), std::forward<F>(f), variationTags,
619 jittedVariation->GetTypeName(), colRegister, lm, inputColNames)};
620 jittedVariation->SetVariation(std::move(newVariation));
621}
622
623/// Convenience function invoked by jitted code to build action nodes at runtime
624template <typename ActionTag, typename... ColTypes, typename HelperArgType>
625void CallBuildAction(const ColumnNames_t &cols, RColumnRegister &colRegister, ROOT::Detail::RDF::RLoopManager &lm,
626 RJittedAction *jittedAction, unsigned int nSlots,
627 std::shared_ptr<HelperArgType> *helperArg) noexcept
628{
629 if (!jittedAction) {
630 // The branch of the computation graph that needed this jitted variation went out of scope between the type
631 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
632 return;
633 }
634
635 using ColTypes_t = TypeList<ColTypes...>;
636 constexpr auto nColumns = ColTypes_t::list_size;
637 auto ds = lm.GetDataSource();
638 if (ds != nullptr && !cols.empty())
640
641 auto actionPtr =
643 jittedAction->SetAction(std::move(actionPtr));
644}
645
646/// The contained `type` alias is `double` if `T == RInferredType`, `U` if `T == std::container<U>`, `T` otherwise.
647template <typename T, bool Container = IsDataContainer<T>::value && !std::is_same<T, std::string>::value>
648struct RMinReturnType {
649 using type = T;
650};
651
652template <>
654 using type = double;
655};
656
657template <typename T>
658struct RMinReturnType<T, true> {
659 using type = TTraits::TakeFirstParameter_t<T>;
660};
661
662// return wrapper around f that prepends an `unsigned int slot` parameter
663template <typename R, typename F, typename... Args>
664std::function<R(unsigned int, Args...)> AddSlotParameter(F &f, TypeList<Args...>)
665{
666 return [f](unsigned int, Args... a) mutable -> R { return f(a...); };
667}
668
669template <typename ColType, typename... Rest>
670struct RNeedJittingHelper {
671 static constexpr bool value = RNeedJittingHelper<Rest...>::value;
672};
673
674template <typename... Rest>
676 static constexpr bool value = true;
677};
678
679template <typename T>
680struct RNeedJittingHelper<T> {
681 static constexpr bool value = false;
682};
683
684template <>
686 static constexpr bool value = true;
687};
688
689template <typename ...ColTypes>
690struct RNeedJitting {
691 static constexpr bool value = RNeedJittingHelper<ColTypes...>::value;
692};
693
694template <>
695struct RNeedJitting<> {
696 static constexpr bool value = false;
697};
698
699///////////////////////////////////////////////////////////////////////////////
700/// Check preconditions for RInterface::Aggregate:
701/// - the aggregator callable must have signature `U(U,T)` or `void(U&,T)`.
702/// - the merge callable must have signature `U(U,U)` or `void(std::vector<U>&)`
703template <typename R, typename Merge, typename U, typename T, typename decayedU = std::decay_t<U>,
704 typename mergeArgsNoDecay_t = typename CallableTraits<Merge>::arg_types_nodecay,
705 typename mergeArgs_t = typename CallableTraits<Merge>::arg_types,
706 typename mergeRet_t = typename CallableTraits<Merge>::ret_type>
708{
709 constexpr bool isAggregatorOk =
710 (std::is_same<R, decayedU>::value) || (std::is_same<R, void>::value && std::is_lvalue_reference<U>::value);
711 static_assert(isAggregatorOk, "aggregator function must have signature `U(U,T)` or `void(U&,T)`");
712 constexpr bool isMergeOk =
713 (std::is_same<TypeList<decayedU, decayedU>, mergeArgs_t>::value && std::is_same<decayedU, mergeRet_t>::value) ||
714 (std::is_same<TypeList<std::vector<decayedU> &>, mergeArgsNoDecay_t>::value &&
715 std::is_same<void, mergeRet_t>::value);
716 static_assert(isMergeOk, "merge function must have signature `U(U,U)` or `void(std::vector<U>&)`");
717}
718
719///////////////////////////////////////////////////////////////////////////////
720/// This overload of CheckAggregate is called when the aggregator takes more than two arguments
721template <typename R, typename T>
722void CheckAggregate(T)
723{
724 static_assert(sizeof(T) == 0, "aggregator function must take exactly two arguments");
725}
726
727///////////////////////////////////////////////////////////////////////////////
728/// Check as many template parameters were passed as the number of column names, throw if this is not the case.
729void CheckTypesAndPars(unsigned int nTemplateParams, unsigned int nColumnNames);
730
731/// Return local BranchNames or default BranchNames according to which one should be used
732const ColumnNames_t SelectColumns(unsigned int nArgs, const ColumnNames_t &bl, const ColumnNames_t &defBl);
733
734/// Check whether column names refer to a valid branch of a TTree or have been `Define`d. Return invalid column names.
735ColumnNames_t FindUnknownColumns(const ColumnNames_t &requiredCols, const RColumnRegister &definedCols,
736 const ColumnNames_t &dataSourceColumns);
737
738/// Returns the list of Filters defined in the whole graph
739std::vector<std::string> GetFilterNames(const std::shared_ptr<RLoopManager> &loopManager);
740
741/// Returns the list of Filters defined in the branch
742template <typename NodeType>
743std::vector<std::string> GetFilterNames(const std::shared_ptr<NodeType> &node)
744{
745 std::vector<std::string> filterNames;
746 node->AddFilterName(filterNames);
747 return filterNames;
748}
749
750struct ParsedTreePath {
751 std::string fTreeName;
752 std::string fDirName;
753};
754
756
757// Check if a condition is true for all types
758template <bool...>
759struct TBoolPack;
760
761template <bool... bs>
762using IsTrueForAllImpl_t = typename std::is_same<TBoolPack<bs..., true>, TBoolPack<true, bs...>>;
763
764template <bool... Conditions>
765struct TEvalAnd {
766 static constexpr bool value = IsTrueForAllImpl_t<Conditions...>::value;
767};
768
769// Check if a class is a specialisation of stl containers templates
770// clang-format off
771
772template <typename>
773struct IsList_t : std::false_type {};
774
775template <typename T>
776struct IsList_t<std::list<T>> : std::true_type {};
777
778template <typename>
779struct IsDeque_t : std::false_type {};
780
781template <typename T>
782struct IsDeque_t<std::deque<T>> : std::true_type {};
783// clang-format on
784
785void CheckForDuplicateSnapshotColumns(const ColumnNames_t &cols);
786
788
789template <typename T>
790struct InnerValueType {
791 using type = T; // fallback for when T is not a nested RVec
792};
793
794template <typename Elem>
795struct InnerValueType<ROOT::VecOps::RVec<ROOT::VecOps::RVec<Elem>>> {
796 using type = Elem;
797};
798
799template <typename T>
801
802std::pair<std::vector<std::string>, std::vector<std::string>>
804 std::vector<std::string> &&colsWithAliases);
805
806void RemoveDuplicates(ColumnNames_t &columnNames);
807void RemoveRNTupleSubfields(ColumnNames_t &columnNames);
808
809} // namespace RDF
810} // namespace Internal
811
812namespace Detail {
813namespace RDF {
814
815/// The aliased type is `double` if `T == RInferredType`, `U` if `T == container<U>`, `T` otherwise.
816template <typename T>
817using MinReturnType_t = typename RDFInternal::RMinReturnType<T>::type;
818
819template <typename T>
821
822template <typename T>
824
825} // namespace RDF
826} // namespace Detail
827} // namespace ROOT
828
829/// \endcond
830
831#endif
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Int_t gErrorIgnoreLevel
errors with level below this value will be ignored. Default is kUnset.
Definition TError.cxx:33
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 Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
A wrapper around a concrete RDefine, which forwards all calls to it RJittedDefine is a placeholder th...
A wrapper around a concrete RFilter, which forwards all calls to it RJittedFilter is the type of the ...
The head node of a RDF computation graph.
A histogram data structure to bin data along multiple dimensions.
A histogram for aggregation of data along multiple dimensions.
Definition RHist.hxx:66
A binder for user-defined columns, variations and aliases.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
The public interface to the RDataFrame federation of classes.
const_iterator end() const
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition RVec.hxx:1509
A TTree represents a columnar dataset.
Definition TTree.h:89
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 CheckForNoVariations(const std::string &where, std::string_view definedColView, const RColumnRegister &colRegister)
Throw if the column has systematic variations attached.
ParsedTreePath ParseTreePath(std::string_view fullTreeName)
std::shared_ptr< RJittedDefine > BookDefinePerSampleJit(std::string_view name, std::string_view expression, RLoopManager &lm, const RColumnRegister &colRegister)
Book the jitting of a DefinePerSample call.
void CheckValidCppVarName(std::string_view var, const std::string &where)
void RemoveDuplicates(ColumnNames_t &columnNames)
ColumnNames_t GetValidatedColumnNames(RLoopManager &lm, const unsigned int nColumns, const ColumnNames_t &columns, const RColumnRegister &colRegister, RDataSource *ds)
Given the desired number of columns and the user-provided list of columns:
std::shared_ptr< RNodeBase > UpcastNode(std::shared_ptr< RNodeBase > ptr)
void CheckSnapshotOptionsFormatCompatibility(const ROOT::RDF::RSnapshotOptions &opts)
void CheckForDefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &colRegister, const ColumnNames_t &dataSourceColumns)
Throw if column definedColView is not already there.
std::vector< std::string > GetFilterNames(const std::shared_ptr< RLoopManager > &loopManager)
std::string PrettyPrintAddr(const void *const addr)
std::shared_ptr< RDFDetail::RJittedFilter > BookFilterJit(std::shared_ptr< RDFDetail::RNodeBase > prevNode, std::string_view name, std::string_view expression, const RColumnRegister &colRegister, TTree *tree, RDataSource *ds)
Book the jitting of a Filter call.
std::string JitBuildAction(const ColumnNames_t &cols, const std::type_info &helperArgType, const std::type_info &at, TTree *tree, const unsigned int nSlots, const RColumnRegister &colRegister, RDataSource *ds, const bool vector2RVec)
void CheckTypesAndPars(unsigned int nTemplateParams, unsigned int nColumnNames)
std::string DemangleTypeIdName(const std::type_info &typeInfo)
bool AtLeastOneEmptyString(const std::vector< std::string_view > strings)
std::unique_ptr< ROOT::Detail::RDF::RColumnReaderBase > CreateColumnReader(ROOT::RDF::RDataSource &ds, unsigned int slot, std::string_view col, const std::type_info &tid, TTreeReader *treeReader)
Definition RDFUtils.cxx:708
std::pair< std::vector< std::string >, std::vector< std::string > > AddSizeBranches(ROOT::RDF::RDataSource *ds, std::vector< std::string > &&colsWithoutAliases, std::vector< std::string > &&colsWithAliases)
Return copies of colsWithoutAliases and colsWithAliases with size branches for variable-sized array b...
void RemoveRNTupleSubfields(ColumnNames_t &columnNames)
ColumnNames_t FilterArraySizeColNames(const ColumnNames_t &columnNames, const std::string &action)
Take a list of column names, return that list with entries starting by '#' filtered out.
std::vector< std::string > GetValidatedArgTypes(const ColumnNames_t &colNames, const RColumnRegister &colRegister, TTree *tree, RDataSource *ds, const std::string &context, bool vector2RVec)
void CheckForDuplicateSnapshotColumns(const ColumnNames_t &cols)
ColumnNames_t ConvertRegexToColumns(const ColumnNames_t &colNames, std::string_view columnNameRegexp, std::string_view callerName)
ColumnNames_t FindUnknownColumns(const ColumnNames_t &requiredCols, const RColumnRegister &definedCols, const ColumnNames_t &dataSourceColumns)
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
void CheckForRedefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &colRegister, const ColumnNames_t &dataSourceColumns)
Throw if column definedColView is already there.
std::shared_ptr< RJittedDefine > BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm, RDataSource *ds, const RColumnRegister &colRegister)
Book the jitting of a Define call.
std::shared_ptr< RJittedVariation > BookVariationJit(const std::vector< std::string > &colNames, std::string_view variationName, const std::vector< std::string > &variationTags, std::string_view expression, RLoopManager &lm, RDataSource *ds, const RColumnRegister &colRegister, bool isSingleColumn, const std::string &varyColType)
Book the jitting of a Vary call.
ROOT type_traits extensions.
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:673
A collection of options to steer the creation of the dataset on disk through Snapshot().
Lightweight storage for a collection of types.