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 <TError.h> // gErrorIgnoreLevel
32#include <TH1.h>
33#include <TROOT.h> // IsImplicitMTEnabled
34
35#include <deque>
36#include <functional>
37#include <list>
38#include <memory>
39#include <string>
40#include <type_traits>
41#include <typeinfo>
42#include <vector>
43
44class TTree;
45namespace ROOT {
46namespace Detail {
47namespace RDF {
48class RNodeBase;
49}
50}
51namespace RDF {
52template<typename T, typename V>
53class RInterface;
55} // namespace RDF
56
57} // namespace ROOT
58
59/// \cond HIDDEN_SYMBOLS
60
61namespace ROOT {
62namespace Internal {
63namespace RDF {
64using namespace ROOT::Detail::RDF;
65using namespace ROOT::RDF;
66namespace TTraits = ROOT::TypeTraits;
67
68std::string DemangleTypeIdName(const std::type_info &typeInfo);
69
70ColumnNames_t
71ConvertRegexToColumns(const ColumnNames_t &colNames, std::string_view columnNameRegexp, std::string_view callerName);
72
73/// An helper object that sets and resets gErrorIgnoreLevel via RAII.
75private:
77
78public:
81};
82
83/****** BuildAction overloads *******/
84
85// clang-format off
86/// This namespace defines types to be used for tag dispatching in RInterface.
87namespace ActionTags {
88struct Histo1D{};
89struct Histo2D{};
90struct Histo3D{};
91struct HistoND{};
92struct Graph{};
93struct GraphAsymmErrors{};
94struct Profile1D{};
95struct Profile2D{};
96struct Min{};
97struct Max{};
98struct Sum{};
99struct Mean{};
100struct Fill{};
101struct StdDev{};
102struct Display{};
103struct Snapshot{};
104struct Book{};
105}
106// clang-format on
107
108template <typename T, bool ISV6HISTO = std::is_base_of<TH1, std::decay_t<T>>::value>
109struct HistoUtils {
110 static void SetCanExtendAllAxes(T &h) { h.SetCanExtend(::TH1::kAllAxes); }
111 static bool HasAxisLimits(T &h)
112 {
113 auto xaxis = h.GetXaxis();
114 return !(xaxis->GetXmin() == 0. && xaxis->GetXmax() == 0.);
115 }
116};
117
118template <typename T>
119struct HistoUtils<T, false> {
120 static void SetCanExtendAllAxes(T &) {}
121 static bool HasAxisLimits(T &) { return true; }
122};
123
124// Generic filling (covers Histo2D, HistoND, Profile1D and Profile2D actions, with and without weights)
125template <typename... ColTypes, typename ActionTag, typename ActionResultType, typename PrevNodeType>
126std::unique_ptr<RActionBase>
127BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &h, const unsigned int nSlots,
128 std::shared_ptr<PrevNodeType> prevNode, ActionTag, const RColumnRegister &colRegister)
129{
131 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
132 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
133}
134
135// Histo1D filling (must handle the special case of distinguishing FillHelper and BufferedFillHelper
136template <typename... ColTypes, typename PrevNodeType>
137std::unique_ptr<RActionBase>
138BuildAction(const ColumnNames_t &bl, const std::shared_ptr<::TH1D> &h, const unsigned int nSlots,
139 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Histo1D, const RColumnRegister &colRegister)
140{
142
145 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
146 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
147 } else {
149 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
150 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
151 }
152}
153
154// Action for Histo3D, where thread safe filling might be supported to save memory
155template <typename... ColTypes, typename ActionResultType, typename PrevNodeType>
156std::unique_ptr<RActionBase>
157BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &h, const unsigned int nSlots,
158 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Histo3D, const RColumnRegister &colRegister)
159{
160 if (RDFInternal::NThreadPerTH3() <= 1 || nSlots == 1) {
162 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
163 return std::make_unique<Action_t>(Helper_t(h, nSlots), bl, std::move(prevNode), colRegister);
164 } else {
166 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
167 if constexpr (sizeof...(ColTypes) > 3) {
168 h->Sumw2();
169 }
170 const auto histoSlots = std::max(nSlots / RDFInternal::NThreadPerTH3(), 1u);
171 return std::make_unique<Action_t>(Helper_t(h, histoSlots), bl, std::move(prevNode), colRegister);
172 }
173}
174
175template <typename... ColTypes, typename PrevNodeType>
176std::unique_ptr<RActionBase>
177BuildAction(const ColumnNames_t &bl, const std::shared_ptr<TGraph> &g, const unsigned int nSlots,
178 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Graph, const RColumnRegister &colRegister)
179{
181 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
182 return std::make_unique<Action_t>(Helper_t(g, nSlots), bl, std::move(prevNode), colRegister);
183}
184
185template <typename... ColTypes, typename PrevNodeType>
186std::unique_ptr<RActionBase>
187BuildAction(const ColumnNames_t &bl, const std::shared_ptr<TGraphAsymmErrors> &g, const unsigned int nSlots,
188 std::shared_ptr<PrevNodeType> prevNode, ActionTags::GraphAsymmErrors, const RColumnRegister &colRegister)
189{
191 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
192 return std::make_unique<Action_t>(Helper_t(g, nSlots), bl, std::move(prevNode), colRegister);
193}
194
195// Min action
196template <typename ColType, typename PrevNodeType, typename ActionResultType>
197std::unique_ptr<RActionBase>
198BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &minV, const unsigned int nSlots,
199 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Min, const RColumnRegister &colRegister)
200{
203 return std::make_unique<Action_t>(Helper_t(minV, nSlots), bl, std::move(prevNode), colRegister);
204}
205
206// Max action
207template <typename ColType, typename PrevNodeType, typename ActionResultType>
208std::unique_ptr<RActionBase>
209BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &maxV, const unsigned int nSlots,
210 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Max, const RColumnRegister &colRegister)
211{
214 return std::make_unique<Action_t>(Helper_t(maxV, nSlots), bl, std::move(prevNode), colRegister);
215}
216
217// Sum action
218template <typename ColType, typename PrevNodeType, typename ActionResultType>
219std::unique_ptr<RActionBase>
220BuildAction(const ColumnNames_t &bl, const std::shared_ptr<ActionResultType> &sumV, const unsigned int nSlots,
221 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Sum, const RColumnRegister &colRegister)
222{
225 return std::make_unique<Action_t>(Helper_t(sumV, nSlots), bl, std::move(prevNode), colRegister);
226}
227
228// Mean action
229template <typename ColType, typename PrevNodeType>
230std::unique_ptr<RActionBase>
231BuildAction(const ColumnNames_t &bl, const std::shared_ptr<double> &meanV, const unsigned int nSlots,
232 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Mean, const RColumnRegister &colRegister)
233{
234 using Helper_t = MeanHelper;
236 return std::make_unique<Action_t>(Helper_t(meanV, nSlots), bl, std::move(prevNode), colRegister);
237}
238
239// Standard Deviation action
240template <typename ColType, typename PrevNodeType>
241std::unique_ptr<RActionBase>
242BuildAction(const ColumnNames_t &bl, const std::shared_ptr<double> &stdDeviationV, const unsigned int nSlots,
243 std::shared_ptr<PrevNodeType> prevNode, ActionTags::StdDev, const RColumnRegister &colRegister)
244{
245 using Helper_t = StdDevHelper;
247 return std::make_unique<Action_t>(Helper_t(stdDeviationV, nSlots), bl, prevNode, colRegister);
248}
249
250using displayHelperArgs_t = std::pair<size_t, std::shared_ptr<ROOT::RDF::RDisplay>>;
251
252// Display action
253template <typename... ColTypes, typename PrevNodeType>
254std::unique_ptr<RActionBase>
255BuildAction(const ColumnNames_t &bl, const std::shared_ptr<displayHelperArgs_t> &helperArgs, const unsigned int,
256 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Display, const RColumnRegister &colRegister)
257{
259 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
260 return std::make_unique<Action_t>(Helper_t(helperArgs->first, helperArgs->second, prevNode), bl, prevNode,
262}
263
264struct SnapshotHelperArgs {
265 std::string fFileName;
266 std::string fDirName;
267 std::string fTreeName;
268 std::vector<std::string> fOutputColNames;
270 ROOT::Detail::RDF::RLoopManager *fOutputLoopManager;
271 ROOT::Detail::RDF::RLoopManager *fInputLoopManager;
272 bool fToNTuple;
273};
274
275template <typename PrevNodeType>
276std::unique_ptr<RActionBase>
277BuildAction(const ColumnNames_t &colNames, const std::shared_ptr<SnapshotHelperArgs> &snapHelperArgs,
278 const unsigned int nSlots, std::shared_ptr<PrevNodeType> prevNode, const RColumnRegister &colRegister,
279 const std::vector<const std::type_info *> &colTypeIDs)
280{
281 const auto &filename = snapHelperArgs->fFileName;
282 const auto &dirname = snapHelperArgs->fDirName;
283 const auto &treename = snapHelperArgs->fTreeName;
284 const auto &outputColNames = snapHelperArgs->fOutputColNames;
285 const auto &options = snapHelperArgs->fOptions;
286 const auto &outputLM = snapHelperArgs->fOutputLoopManager;
287 const auto &inputLM = snapHelperArgs->fInputLoopManager;
288
289 auto sz = colNames.size();
290 std::vector<bool> isDefine(sz);
291 for (auto i = 0u; i < sz; ++i)
292 isDefine[i] = colRegister.IsDefineOrAlias(colNames[i]);
293
294 std::unique_ptr<RActionBase> actionPtr;
295 if (snapHelperArgs->fToNTuple) {
296 // We use the same helper for single- and multi-thread snapshot.
297 using Helper_t = UntypedSnapshotRNTupleHelper;
299
302 colNames, colTypeIDs, prevNode, colRegister));
303 } else {
305 // single-thread snapshot
306 using Helper_t = UntypedSnapshotTTreeHelper;
309 std::move(isDefine), outputLM, inputLM, colTypeIDs),
310 colNames, colTypeIDs, prevNode, colRegister));
311 } else {
312 // multi-thread snapshot
313 using Helper_t = UntypedSnapshotTTreeHelperMT;
316 std::move(isDefine), outputLM, inputLM, colTypeIDs),
317 colNames, colTypeIDs, prevNode, colRegister));
318 }
319 }
320
321 return actionPtr;
322}
323
324// Book with custom helper type
325template <typename... ColTypes, typename PrevNodeType, typename Helper_t>
326std::unique_ptr<RActionBase>
327BuildAction(const ColumnNames_t &bl, const std::shared_ptr<Helper_t> &h, const unsigned int /*nSlots*/,
328 std::shared_ptr<PrevNodeType> prevNode, ActionTags::Book, const RColumnRegister &colRegister)
329{
330 using Action_t = RAction<Helper_t, PrevNodeType, TTraits::TypeList<ColTypes...>>;
331 return std::make_unique<Action_t>(Helper_t(std::move(*h)), bl, std::move(prevNode), colRegister);
332}
333
334/****** end BuildAndBook ******/
335
336template <typename Filter>
337void CheckFilter(Filter &)
338{
339 using FilterRet_t = typename RDF::CallableTraits<Filter>::ret_type;
340 static_assert(std::is_convertible<FilterRet_t, bool>::value,
341 "filter expression returns a type that is not convertible to bool");
342}
343
344ColumnNames_t FilterArraySizeColNames(const ColumnNames_t &columnNames, const std::string &action);
345
346void CheckValidCppVarName(std::string_view var, const std::string &where);
347
348void CheckForRedefinition(const std::string &where, std::string_view definedCol, const RColumnRegister &colRegister,
349 const ColumnNames_t &dataSourceColumns);
350
351void CheckForDefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &colRegister,
352 const ColumnNames_t &dataSourceColumns);
353
354void CheckForNoVariations(const std::string &where, std::string_view definedColView,
355 const RColumnRegister &colRegister);
356
357std::string PrettyPrintAddr(const void *const addr);
358
359std::shared_ptr<RJittedFilter> BookFilterJit(std::shared_ptr<RNodeBase> *prevNodeOnHeap, std::string_view name,
360 std::string_view expression, const RColumnRegister &colRegister,
361 TTree *tree, RDataSource *ds);
362
363std::shared_ptr<RJittedDefine> BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm,
364 RDataSource *ds, const RColumnRegister &colRegister,
365 std::shared_ptr<RNodeBase> *prevNodeOnHeap);
366
367std::shared_ptr<RJittedDefine> BookDefinePerSampleJit(std::string_view name, std::string_view expression,
368 RLoopManager &lm, const RColumnRegister &colRegister,
369 std::shared_ptr<RNodeBase> *upcastNodeOnHeap);
370
371std::shared_ptr<RJittedVariation>
372BookVariationJit(const std::vector<std::string> &colNames, std::string_view variationName,
373 const std::vector<std::string> &variationTags, std::string_view expression, RLoopManager &lm,
374 RDataSource *ds, const RColumnRegister &colRegister, std::shared_ptr<RNodeBase> *upcastNodeOnHeap,
375 bool isSingleColumn);
376
377std::string JitBuildAction(const ColumnNames_t &bl, std::shared_ptr<RDFDetail::RNodeBase> *prevNode,
378 const std::type_info &art, const std::type_info &at, void *rOnHeap, TTree *tree,
379 const unsigned int nSlots, const RColumnRegister &colRegister, RDataSource *ds,
380 std::weak_ptr<RJittedAction> *jittedActionOnHeap, const bool vector2RVec = true);
381
382// Allocate a weak_ptr on the heap, return a pointer to it. The user is responsible for deleting this weak_ptr.
383// This function is meant to be used by RInterface's methods that book code for jitting.
384// The problem it solves is that we generate code to be lazily jitted with the addresses of certain objects in them,
385// and we need to check those objects are still alive when the generated code is finally jitted and executed.
386// So we pass addresses to weak_ptrs allocated on the heap to the jitted code, which is then responsible for
387// the deletion of the weak_ptr object.
388template <typename T>
389std::weak_ptr<T> *MakeWeakOnHeap(const std::shared_ptr<T> &shPtr)
390{
391 return new std::weak_ptr<T>(shPtr);
392}
393
394// Same as MakeWeakOnHeap, but create a shared_ptr that makes sure the object is definitely kept alive.
395template <typename T>
396std::shared_ptr<T> *MakeSharedOnHeap(const std::shared_ptr<T> &shPtr)
397{
398 return new std::shared_ptr<T>(shPtr);
399}
400
401bool AtLeastOneEmptyString(const std::vector<std::string_view> strings);
402
403/// Take a shared_ptr<AnyNodeType> and return a shared_ptr<RNodeBase>.
404/// This works for RLoopManager nodes as well as filters and ranges.
405std::shared_ptr<RNodeBase> UpcastNode(std::shared_ptr<RNodeBase> ptr);
406
407ColumnNames_t GetValidatedColumnNames(RLoopManager &lm, const unsigned int nColumns, const ColumnNames_t &columns,
408 const RColumnRegister &validDefines, RDataSource *ds);
409
410std::vector<std::string> GetValidatedArgTypes(const ColumnNames_t &colNames, const RColumnRegister &colRegister,
411 TTree *tree, RDataSource *ds, const std::string &context,
412 bool vector2RVec);
413
414template <typename T>
415void AddDSColumnsHelper(const std::string &colName, RLoopManager &lm, RDataSource &ds, RColumnRegister &colRegister)
416{
417
418 if (colRegister.IsDefineOrAlias(colName))
419 return;
420
421 if (lm.HasDataSourceColumnReaders(colName, typeid(T)))
422 return;
423
424 if (!ds.HasColumn(colName) &&
425 lm.GetSuppressErrorsForMissingBranches().find(colName) == lm.GetSuppressErrorsForMissingBranches().end())
426 return;
427
428 const auto nSlots = lm.GetNSlots();
429 std::vector<std::unique_ptr<RColumnReaderBase>> colReaders;
430 colReaders.reserve(nSlots);
431
432 const auto valuePtrs = ds.GetColumnReaders<T>(colName);
433 if (!valuePtrs.empty()) { // we are using the old GetColumnReaders mechanism in this RDataSource
434 for (auto *ptr : valuePtrs)
435 colReaders.emplace_back(new RDSColumnReader<T>(ptr));
436
437 } else { // using the new GetColumnReaders mechanism
438 // TODO consider changing the interface so we return all of these for all slots in one go
439 for (auto slot = 0u; slot < lm.GetNSlots(); ++slot)
440 colReaders.emplace_back(
441 ROOT::Internal::RDF::CreateColumnReader(ds, slot, colName, typeid(T), /*treeReader*/ nullptr));
442 }
443
444 lm.AddDataSourceColumnReaders(colName, std::move(colReaders), typeid(T));
445}
446
447/// Take list of column names that must be defined, current map of custom columns, current list of defined column names,
448/// and return a new map of custom columns (with the new datasource columns added to it)
449template <typename... ColumnTypes>
450void AddDSColumns(const std::vector<std::string> &requiredCols, RLoopManager &lm, RDataSource &ds,
452{
453 // hack to expand a template parameter pack without c++17 fold expressions.
454 using expander = int[];
455 int i = 0;
457}
458
459void AddDSColumns(const std::vector<std::string> &requiredCols, ROOT::Detail::RDF::RLoopManager &lm,
460 ROOT::RDF::RDataSource &ds, const std::vector<const std::type_info *> &colTypeIDs,
462
463// this function is meant to be called by the jitted code generated by BookFilterJit
464template <typename F, typename PrevNode>
465void JitFilterHelper(F &&f, const char **colsPtr, std::size_t colsSize, std::string_view name,
466 std::weak_ptr<RJittedFilter> *wkJittedFilter, std::shared_ptr<PrevNode> *prevNodeOnHeap,
467 RColumnRegister *colRegister) noexcept
468{
469 if (wkJittedFilter->expired()) {
470 // The branch of the computation graph that needed this jitted code went out of scope between the type
471 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
472 delete wkJittedFilter;
473 delete colRegister;
474 delete prevNodeOnHeap;
475 return;
476 }
477
478 const ColumnNames_t cols(colsPtr, colsPtr + colsSize);
479 delete[] colsPtr;
480
481 const auto jittedFilter = wkJittedFilter->lock();
482
483 // mock Filter logic -- validity checks and Define-ition of RDataSource columns
484 using Callable_t = std::decay_t<F>;
486 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
487 constexpr auto nColumns = ColTypes_t::list_size;
488 CheckFilter(f);
489
490 auto &lm = *jittedFilter->GetLoopManagerUnchecked(); // RLoopManager must exist at this time
491 auto ds = lm.GetDataSource();
492
493 if (ds != nullptr)
495
496 jittedFilter->SetFilter(
497 std::unique_ptr<RFilterBase>(new F_t(std::forward<F>(f), cols, *prevNodeOnHeap, *colRegister, name)));
498 // colRegister points to the columns structure in the heap, created before the jitted call so that the jitter can
499 // share data after it has lazily compiled the code. Here the data has been used and the memory can be freed.
500 delete colRegister;
501 delete prevNodeOnHeap;
502 delete wkJittedFilter;
503}
504
505namespace DefineTypes {
506struct RDefineTag {};
507struct RDefinePerSampleTag {};
508}
509
510template <typename F>
511auto MakeDefineNode(DefineTypes::RDefineTag, std::string_view name, std::string_view dummyType, F &&f,
512 const ColumnNames_t &cols, RColumnRegister &colRegister, RLoopManager &lm)
513{
514 return std::unique_ptr<RDefineBase>(new RDefine<std::decay_t<F>, ExtraArgsForDefine::None>(
515 name, dummyType, std::forward<F>(f), cols, colRegister, lm));
516}
517
518template <typename F>
519auto MakeDefineNode(DefineTypes::RDefinePerSampleTag, std::string_view name, std::string_view dummyType, F &&f,
520 const ColumnNames_t &, RColumnRegister &, RLoopManager &lm)
521{
522 return std::unique_ptr<RDefineBase>(
523 new RDefinePerSample<std::decay_t<F>>(name, dummyType, std::forward<F>(f), lm));
524}
525
526// Build a RDefine or a RDefinePerSample object and attach it to an existing RJittedDefine
527// This function is meant to be called by jitted code right before starting the event loop.
528// If colsPtr is null, build a RDefinePerSample (it has no input columns), otherwise a RDefine.
529template <typename RDefineTypeTag, typename F>
530void JitDefineHelper(F &&f, const char **colsPtr, std::size_t colsSize, std::string_view name, RLoopManager *lm,
531 std::weak_ptr<RJittedDefine> *wkJittedDefine, RColumnRegister *colRegister,
532 std::shared_ptr<RNodeBase> *prevNodeOnHeap) noexcept
533{
534 // a helper to delete objects allocated before jitting, so that the jitter can share data with lazily jitted code
535 auto doDeletes = [&] {
536 delete wkJittedDefine;
537 delete colRegister;
538 delete prevNodeOnHeap;
539 delete[] colsPtr;
540 };
541
542 if (wkJittedDefine->expired()) {
543 // The branch of the computation graph that needed this jitted code went out of scope between the type
544 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
545 doDeletes();
546 return;
547 }
548
549 const ColumnNames_t cols(colsPtr, colsPtr + colsSize);
550
551 auto jittedDefine = wkJittedDefine->lock();
552
553 using Callable_t = std::decay_t<F>;
554 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
555
556 auto ds = lm->GetDataSource();
557 if (ds != nullptr && colsPtr)
559
560 // will never actually be used (trumped by jittedDefine->GetTypeName()), but we set it to something meaningful
561 // to help devs debugging
562 const auto dummyType = "jittedCol_t";
563 // use unique_ptr<RDefineBase> instead of make_unique<NewCol_t> to reduce jit/compile-times
564 std::unique_ptr<RDefineBase> newCol{
565 MakeDefineNode(RDefineTypeTag{}, name, dummyType, std::forward<F>(f), cols, *colRegister, *lm)};
566 jittedDefine->SetDefine(std::move(newCol));
567
568 doDeletes();
569}
570
571template <bool IsSingleColumn, typename F>
572void JitVariationHelper(F &&f, const char **colsPtr, std::size_t colsSize, const char **variedCols,
573 std::size_t variedColsSize, const char **variationTags, std::size_t variationTagsSize,
574 std::string_view variationName, RLoopManager *lm,
575 std::weak_ptr<RJittedVariation> *wkJittedVariation, RColumnRegister *colRegister,
576 std::shared_ptr<RNodeBase> *prevNodeOnHeap) noexcept
577{
578 // a helper to delete objects allocated before jitting, so that the jitter can share data with lazily jitted code
579 auto doDeletes = [&] {
580 delete[] colsPtr;
581 delete[] variedCols;
582 delete[] variationTags;
583
584 delete wkJittedVariation;
585 delete colRegister;
586 delete prevNodeOnHeap;
587 };
588
589 if (wkJittedVariation->expired()) {
590 // The branch of the computation graph that needed this jitted variation went out of scope between the type
591 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
592 doDeletes();
593 return;
594 }
595
596 const ColumnNames_t inputColNames(colsPtr, colsPtr + colsSize);
597 std::vector<std::string> variedColNames(variedCols, variedCols + variedColsSize);
598 std::vector<std::string> tags(variationTags, variationTags + variationTagsSize);
599
600 auto jittedVariation = wkJittedVariation->lock();
601
602 using Callable_t = std::decay_t<F>;
603 using ColTypes_t = typename TTraits::CallableTraits<Callable_t>::arg_types;
604
605 auto ds = lm->GetDataSource();
606 if (ds != nullptr)
608
609 // use unique_ptr<RDefineBase> instead of make_unique<NewCol_t> to reduce jit/compile-times
610 std::unique_ptr<RVariationBase> newVariation{new RVariation<std::decay_t<F>, IsSingleColumn>(
611 std::move(variedColNames), variationName, std::forward<F>(f), std::move(tags), jittedVariation->GetTypeName(),
613 jittedVariation->SetVariation(std::move(newVariation));
614
615 doDeletes();
616}
617
618/// Convenience function invoked by jitted code to build action nodes at runtime
619template <typename ActionTag, typename... ColTypes, typename PrevNodeType, typename HelperArgType>
620void CallBuildAction(std::shared_ptr<PrevNodeType> *prevNodeOnHeap, const char **colsPtr, std::size_t colsSize,
621 const unsigned int nSlots, std::shared_ptr<HelperArgType> *helperArgOnHeap,
622 std::weak_ptr<RJittedAction> *wkJittedActionOnHeap, RColumnRegister *colRegister) noexcept
623{
624 // a helper to delete objects allocated before jitting, so that the jitter can share data with lazily jitted code
625 auto doDeletes = [&] {
626 delete[] colsPtr;
627 delete helperArgOnHeap;
629 // colRegister must be deleted before prevNodeOnHeap because their dtor needs the RLoopManager to be alive
630 // and prevNodeOnHeap is what keeps it alive if the rest of the computation graph is already out of scope
631 delete colRegister;
632 delete prevNodeOnHeap;
633 };
634
635 if (wkJittedActionOnHeap->expired()) {
636 // The branch of the computation graph that needed this jitted variation went out of scope between the type
637 // jitting was booked and the time jitting actually happened. Nothing to do other than cleaning up.
638 doDeletes();
639 return;
640 }
641
642 const ColumnNames_t cols(colsPtr, colsPtr + colsSize);
643
645
646 // if we are here it means we are jitting, if we are jitting the loop manager must be alive
648 auto &loopManager = *prevNodePtr->GetLoopManagerUnchecked();
649 using ColTypes_t = TypeList<ColTypes...>;
650 constexpr auto nColumns = ColTypes_t::list_size;
651 auto ds = loopManager.GetDataSource();
652 if (ds != nullptr)
654
655 auto actionPtr = BuildAction<ColTypes...>(cols, std::move(*helperArgOnHeap), nSlots, std::move(prevNodePtr),
657 jittedActionOnHeap->SetAction(std::move(actionPtr));
658
659 doDeletes();
660}
661
662/// The contained `type` alias is `double` if `T == RInferredType`, `U` if `T == std::container<U>`, `T` otherwise.
663template <typename T, bool Container = IsDataContainer<T>::value && !std::is_same<T, std::string>::value>
664struct RMinReturnType {
665 using type = T;
666};
667
668template <>
670 using type = double;
671};
672
673template <typename T>
674struct RMinReturnType<T, true> {
675 using type = TTraits::TakeFirstParameter_t<T>;
676};
677
678// return wrapper around f that prepends an `unsigned int slot` parameter
679template <typename R, typename F, typename... Args>
680std::function<R(unsigned int, Args...)> AddSlotParameter(F &f, TypeList<Args...>)
681{
682 return [f](unsigned int, Args... a) mutable -> R { return f(a...); };
683}
684
685template <typename ColType, typename... Rest>
686struct RNeedJittingHelper {
687 static constexpr bool value = RNeedJittingHelper<Rest...>::value;
688};
689
690template <typename... Rest>
692 static constexpr bool value = true;
693};
694
695template <typename T>
696struct RNeedJittingHelper<T> {
697 static constexpr bool value = false;
698};
699
700template <>
702 static constexpr bool value = true;
703};
704
705template <typename ...ColTypes>
706struct RNeedJitting {
707 static constexpr bool value = RNeedJittingHelper<ColTypes...>::value;
708};
709
710template <>
711struct RNeedJitting<> {
712 static constexpr bool value = false;
713};
714
715///////////////////////////////////////////////////////////////////////////////
716/// Check preconditions for RInterface::Aggregate:
717/// - the aggregator callable must have signature `U(U,T)` or `void(U&,T)`.
718/// - the merge callable must have signature `U(U,U)` or `void(std::vector<U>&)`
719template <typename R, typename Merge, typename U, typename T, typename decayedU = std::decay_t<U>,
720 typename mergeArgsNoDecay_t = typename CallableTraits<Merge>::arg_types_nodecay,
721 typename mergeArgs_t = typename CallableTraits<Merge>::arg_types,
722 typename mergeRet_t = typename CallableTraits<Merge>::ret_type>
724{
725 constexpr bool isAggregatorOk =
726 (std::is_same<R, decayedU>::value) || (std::is_same<R, void>::value && std::is_lvalue_reference<U>::value);
727 static_assert(isAggregatorOk, "aggregator function must have signature `U(U,T)` or `void(U&,T)`");
728 constexpr bool isMergeOk =
729 (std::is_same<TypeList<decayedU, decayedU>, mergeArgs_t>::value && std::is_same<decayedU, mergeRet_t>::value) ||
730 (std::is_same<TypeList<std::vector<decayedU> &>, mergeArgsNoDecay_t>::value &&
731 std::is_same<void, mergeRet_t>::value);
732 static_assert(isMergeOk, "merge function must have signature `U(U,U)` or `void(std::vector<U>&)`");
733}
734
735///////////////////////////////////////////////////////////////////////////////
736/// This overload of CheckAggregate is called when the aggregator takes more than two arguments
737template <typename R, typename T>
738void CheckAggregate(T)
739{
740 static_assert(sizeof(T) == 0, "aggregator function must take exactly two arguments");
741}
742
743///////////////////////////////////////////////////////////////////////////////
744/// Check as many template parameters were passed as the number of column names, throw if this is not the case.
745void CheckTypesAndPars(unsigned int nTemplateParams, unsigned int nColumnNames);
746
747/// Return local BranchNames or default BranchNames according to which one should be used
748const ColumnNames_t SelectColumns(unsigned int nArgs, const ColumnNames_t &bl, const ColumnNames_t &defBl);
749
750/// Check whether column names refer to a valid branch of a TTree or have been `Define`d. Return invalid column names.
751ColumnNames_t FindUnknownColumns(const ColumnNames_t &requiredCols, const RColumnRegister &definedCols,
752 const ColumnNames_t &dataSourceColumns);
753
754/// Returns the list of Filters defined in the whole graph
755std::vector<std::string> GetFilterNames(const std::shared_ptr<RLoopManager> &loopManager);
756
757/// Returns the list of Filters defined in the branch
758template <typename NodeType>
759std::vector<std::string> GetFilterNames(const std::shared_ptr<NodeType> &node)
760{
761 std::vector<std::string> filterNames;
762 node->AddFilterName(filterNames);
763 return filterNames;
764}
765
766struct ParsedTreePath {
767 std::string fTreeName;
768 std::string fDirName;
769};
770
772
773// Check if a condition is true for all types
774template <bool...>
775struct TBoolPack;
776
777template <bool... bs>
778using IsTrueForAllImpl_t = typename std::is_same<TBoolPack<bs..., true>, TBoolPack<true, bs...>>;
779
780template <bool... Conditions>
781struct TEvalAnd {
782 static constexpr bool value = IsTrueForAllImpl_t<Conditions...>::value;
783};
784
785// Check if a class is a specialisation of stl containers templates
786// clang-format off
787
788template <typename>
789struct IsList_t : std::false_type {};
790
791template <typename T>
792struct IsList_t<std::list<T>> : std::true_type {};
793
794template <typename>
795struct IsDeque_t : std::false_type {};
796
797template <typename T>
798struct IsDeque_t<std::deque<T>> : std::true_type {};
799// clang-format on
800
801void CheckForDuplicateSnapshotColumns(const ColumnNames_t &cols);
802
803template <typename T>
804struct InnerValueType {
805 using type = T; // fallback for when T is not a nested RVec
806};
807
808template <typename Elem>
809struct InnerValueType<ROOT::VecOps::RVec<ROOT::VecOps::RVec<Elem>>> {
810 using type = Elem;
811};
812
813template <typename T>
815
816std::pair<std::vector<std::string>, std::vector<std::string>>
818 std::vector<std::string> &&colsWithAliases);
819
820void RemoveDuplicates(ColumnNames_t &columnNames);
821void RemoveRNTupleSubFields(ColumnNames_t &columnNames);
822
823} // namespace RDF
824} // namespace Internal
825
826namespace Detail {
827namespace RDF {
828
829/// The aliased type is `double` if `T == RInferredType`, `U` if `T == container<U>`, `T` otherwise.
830template <typename T>
831using MinReturnType_t = typename RDFInternal::RMinReturnType<T>::type;
832
833template <typename T>
835
836template <typename T>
838
839} // namespace RDF
840} // namespace Detail
841} // namespace ROOT
842
843/// \endcond
844
845#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
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:110
Int_t Fill(Double_t x) override
The head node of a RDF computation graph.
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:1526
@ kAllAxes
Definition TH1.h:126
A TTree represents a columnar dataset.
Definition TTree.h:89
#define F(x, y, z)
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 > BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm, RDataSource *ds, const RColumnRegister &colRegister, std::shared_ptr< RNodeBase > *upcastNodeOnHeap)
Book the jitting of a Define 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)
std::shared_ptr< RDFDetail::RJittedFilter > BookFilterJit(std::shared_ptr< RDFDetail::RNodeBase > *prevNodeOnHeap, std::string_view name, std::string_view expression, const RColumnRegister &colRegister, TTree *tree, RDataSource *ds)
Book the jitting of a Filter call.
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)
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:671
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)
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, std::shared_ptr< RNodeBase > *upcastNodeOnHeap, bool isSingleColumn)
Book the jitting of a Vary call.
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:63
std::shared_ptr< RJittedDefine > BookDefinePerSampleJit(std::string_view name, std::string_view expression, RLoopManager &lm, const RColumnRegister &colRegister, std::shared_ptr< RNodeBase > *upcastNodeOnHeap)
Book the jitting of a DefinePerSample call.
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::string JitBuildAction(const ColumnNames_t &cols, std::shared_ptr< RDFDetail::RNodeBase > *prevNode, const std::type_info &helperArgType, const std::type_info &at, void *helperArgOnHeap, TTree *tree, const unsigned int nSlots, const RColumnRegister &colRegister, RDataSource *ds, std::weak_ptr< RJittedAction > *jittedActionOnHeap, const bool vector2RVec)
ROOT type_traits extensions.
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:600
A collection of options to steer the creation of the dataset on file.
Lightweight storage for a collection of types.