Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RInterface.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 03/2017
2
3/*************************************************************************
4 * Copyright (C) 1995-2021, 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
12#define ROOT_RDF_TINTERFACE
13
14#include "ROOT/RDataSource.hxx"
20#include "ROOT/RDF/RDefine.hxx"
22#include "ROOT/RDF/RFilter.hxx"
27#include "ROOT/RDF/RRange.hxx"
29#include "ROOT/RDF/Utils.hxx"
32#include "ROOT/RResultPtr.hxx"
34#include <string_view>
35#include "ROOT/RVec.hxx"
36#include "ROOT/TypeTraits.hxx"
37#include "RtypesCore.h" // for ULong64_t
38#include "TDirectory.h"
39#include "TH1.h" // For Histo actions
40#include "TH2.h" // For Histo actions
41#include "TH3.h" // For Histo actions
42#include "THn.h"
43#include "THnSparse.h"
44#include "TProfile.h"
45#include "TProfile2D.h"
46#include "TStatistic.h"
47
48#include "RConfigure.h" // for R__HAS_ROOT7
49#ifdef R__HAS_ROOT7
51#include <ROOT/RHist.hxx>
52#include <ROOT/RHistEngine.hxx>
53#endif
54
55#include <algorithm>
56#include <cstddef>
57#include <initializer_list>
58#include <iterator> // std::back_insterter
59#include <limits>
60#include <memory>
61#include <set>
62#include <sstream>
63#include <stdexcept>
64#include <string>
65#include <type_traits> // is_same, enable_if
66#include <typeinfo>
67#include <unordered_set>
68#include <utility> // std::index_sequence
69#include <vector>
70#include <any>
71
72class TGraph;
73
74// Windows requires a forward decl of printValue to accept it as a valid friend function in RInterface
75namespace ROOT {
78void EnableImplicitMT(UInt_t numthreads);
79class RDataFrame;
80} // namespace ROOT
81namespace cling {
82std::string printValue(ROOT::RDataFrame *tdf);
83}
84
85namespace ROOT {
86namespace RDF {
89namespace TTraits = ROOT::TypeTraits;
90
91template <typename Proxied>
92class RInterface;
93
94using RNode = RInterface<::ROOT::Detail::RDF::RNodeBase>;
95} // namespace RDF
96
97namespace Internal {
98namespace RDF {
100void ChangeEmptyEntryRange(const ROOT::RDF::RNode &node, std::pair<ULong64_t, ULong64_t> &&newRange);
101void ChangeBeginAndEndEntries(const RNode &node, Long64_t begin, Long64_t end);
103std::vector<std::pair<std::uint64_t, std::uint64_t>> GetDatasetGlobalClusterBoundaries(const RNode &node);
105std::string GetDataSourceLabel(const ROOT::RDF::RNode &node);
106void SetTTreeLifeline(ROOT::RDF::RNode &node, std::any lifeline);
107void LoadCustomValues(ROOT::RDF::RNode &node, const std::vector<std::string> &cols,
108 const std::vector<std::size_t> &vecSizes, float vecPadding, ROOT::RVecF &dest);
109} // namespace RDF
110} // namespace Internal
111
112namespace RDF {
113
114// clang-format off
115/**
116 * \class ROOT::RDF::RInterface
117 * \ingroup dataframe
118 * \brief The public interface to the RDataFrame federation of classes.
119 * \tparam Proxied One of the "node" base types (e.g. RLoopManager, RFilterBase). The user never specifies this type manually.
120 *
121 * The documentation of each method features a one liner illustrating how to use the method, for example showing how
122 * the majority of the template parameters are automatically deduced requiring no or very little effort by the user.
123 */
124// clang-format on
125template <typename Proxied>
130 friend std::string cling::printValue(::ROOT::RDataFrame *tdf); // For a nice printing at the prompt
132
133 template <typename T>
134 friend class RInterface;
135
137 friend void RDFInternal::ChangeEmptyEntryRange(const RNode &node, std::pair<ULong64_t, ULong64_t> &&newRange);
138 friend void RDFInternal::ChangeBeginAndEndEntries(const RNode &node, Long64_t start, Long64_t end);
140 friend std::vector<std::pair<std::uint64_t, std::uint64_t>>
142 friend std::string ROOT::Internal::RDF::GetDataSourceLabel(const RNode &node);
143 friend void ROOT::Internal::RDF::SetTTreeLifeline(ROOT::RDF::RNode &node, std::any lifeline);
144 friend void ROOT::Internal::RDF::LoadCustomValues(ROOT::RDF::RNode &node, const std::vector<std::string> &cols,
145 const std::vector<std::size_t> &vecSizes, float vecPadding,
147
148 std::shared_ptr<Proxied> fProxiedPtr; ///< Smart pointer to the graph node encapsulated by this RInterface.
149
150 void LoadValuesCustom(const ColumnNames_t &columnList, const std::vector<std::size_t> &vecSizes, float vecPadding,
152 {
153 const auto validColumnNames = GetValidatedColumnNames(columnList.size(), columnList);
154
155 auto nColumns = columnList.size();
156 std::vector<const std::type_info *> colTypeIDs;
157 colTypeIDs.reserve(nColumns);
158 for (decltype(nColumns) i{}; i < nColumns; i++) {
159 const auto &colName = validColumnNames[i];
160 const auto colTypeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(
161 colName, /*tree*/ nullptr, GetDataSource(), fColRegister.GetDefine(colName), /*vector2RVec*/ true);
162 const std::type_info *colTypeID = &ROOT::Internal::RDF::TypeName2TypeID(colTypeName);
163 colTypeIDs.push_back(colTypeID);
164 }
165 // Crucial e.g. if the column names do not correspond to already-available column readers created by the data
166 // source
167 CheckAndFillDSColumns(validColumnNames, colTypeIDs);
168
169 // the values are appended to the caller's buffer, which outlives the event loop run below
170 auto location = std::shared_ptr<ROOT::RVecF>(&dest, [](ROOT::RVecF *) {});
171 auto action = RDFInternal::BuildAction(validColumnNames, colTypeIDs, location, vecSizes, vecPadding, fProxiedPtr,
173 auto resPtr = MakeResultPtr(location, *GetLoopManager(), std::move(action));
174
175 *resPtr;
176 }
177
178public:
179 ////////////////////////////////////////////////////////////////////////////
180 /// \brief Copy-assignment operator for RInterface.
181 RInterface &operator=(const RInterface &) = default;
182
183 ////////////////////////////////////////////////////////////////////////////
184 /// \brief Copy-ctor for RInterface.
185 RInterface(const RInterface &) = default;
186
187 ////////////////////////////////////////////////////////////////////////////
188 /// \brief Move-ctor for RInterface.
189 RInterface(RInterface &&) = default;
190
191 ////////////////////////////////////////////////////////////////////////////
192 /// \brief Move-assignment operator for RInterface.
194
195 ////////////////////////////////////////////////////////////////////////////
196 /// \brief Build a RInterface from a RLoopManager.
197 /// This constructor is only available for RInterface<RLoopManager>.
198 template <typename T = Proxied, typename = std::enable_if_t<std::is_same<T, RLoopManager>::value, int>>
199 RInterface(const std::shared_ptr<RLoopManager> &proxied) : RInterfaceBase(proxied), fProxiedPtr(proxied)
200 {
201 }
202
203 ////////////////////////////////////////////////////////////////////////////
204 /// \brief Cast any RDataFrame node to a common type ROOT::RDF::RNode.
205 /// Different RDataFrame methods return different C++ types. All nodes, however,
206 /// can be cast to this common type at the cost of a small performance penalty.
207 /// This allows, for example, storing RDataFrame nodes in a vector, or passing them
208 /// around via (non-template, C++11) helper functions.
209 /// Example usage:
210 /// ~~~{.cpp}
211 /// // a function that conditionally adds a Range to a RDataFrame node.
212 /// RNode MaybeAddRange(RNode df, bool mustAddRange)
213 /// {
214 /// return mustAddRange ? df.Range(1) : df;
215 /// }
216 /// // use as :
217 /// ROOT::RDataFrame df(10);
218 /// auto maybeRanged = MaybeAddRange(df, true);
219 /// ~~~
220 /// Note that it is not a problem to pass RNode's by value.
221 operator RNode() const
222 {
223 return RNode(std::static_pointer_cast<::ROOT::Detail::RDF::RNodeBase>(fProxiedPtr), *fLoopManager, fColRegister);
224 }
225
226 /// \name Transformations
227 /// These functions transform the columns of the dataframe, such as filtering events or defining columns.
228 /// Transformations can be chained, for example
229 /// ~~~{.cpp}
230 /// auto filtered = rdf.Filter(...).Define(...).Define(...);
231 /// ~~~
232 /// \{
233
234 ////////////////////////////////////////////////////////////////////////////
235 /// \brief Append a filter to the call graph.
236 /// \param[in] f Function, lambda expression, functor class or any other callable object. It must return a `bool`
237 /// signalling whether the event has passed the selection (true) or not (false).
238 /// \param[in] columns Names of the columns/branches in input to the filter function.
239 /// \param[in] name Optional name of this filter. See `Report`.
240 /// \return the filter node of the computation graph.
241 ///
242 /// Append a filter node at the point of the call graph corresponding to the
243 /// object this method is called on.
244 /// The callable `f` should not have side-effects (e.g. modification of an
245 /// external or static variable) to ensure correct results when implicit
246 /// multi-threading is active.
247 ///
248 /// RDataFrame only evaluates filters when necessary: if multiple filters
249 /// are chained one after another, they are executed in order and the first
250 /// one returning false causes the event to be discarded.
251 /// Even if multiple actions or transformations depend on the same filter,
252 /// it is executed once per entry. If its result is requested more than
253 /// once, the cached result is served.
254 ///
255 /// ### Example usage:
256 /// ~~~{.cpp}
257 /// // C++ callable (function, functor class, lambda...) that takes two parameters of the types of "x" and "y"
258 /// auto filtered = df.Filter(myCut, {"x", "y"});
259 ///
260 /// // String: it must contain valid C++ except that column names can be used instead of variable names
261 /// auto filtered = df.Filter("x*y > 0");
262 /// ~~~
263 ///
264 /// \note If the body of the string expression contains an explicit `return` statement (even if it is in a nested
265 /// scope), RDataFrame _will not_ add another one in front of the expression. So this will not work:
266 /// ~~~{.cpp}
267 /// df.Filter("Sum(Map(vec, [](float e) { return e*e > 0.5; }))")
268 /// ~~~
269 /// but instead this will:
270 /// ~~~{.cpp}
271 /// df.Filter("return Sum(Map(vec, [](float e) { return e*e > 0.5; }))")
272 /// ~~~
273 template <typename F, std::enable_if_t<!std::is_convertible<F, std::string>::value, int> = 0>
274 RInterface<RDFDetail::RFilter<F, Proxied>> Filter(F f, const ColumnNames_t &columns = {}, std::string_view name = "")
275 {
276 RDFInternal::CheckFilter(f);
277 using ColTypes_t = typename TTraits::CallableTraits<F>::arg_types;
278 constexpr auto nColumns = ColTypes_t::list_size;
279 const auto validColumnNames = GetValidatedColumnNames(nColumns, columns);
280 CheckAndFillDSColumns(validColumnNames, ColTypes_t());
281
283
284 auto filterPtr = std::make_shared<F_t>(std::move(f), validColumnNames, fProxiedPtr, fColRegister, name);
285 return RInterface<F_t>(std::move(filterPtr), *fLoopManager, fColRegister);
286 }
287
288 ////////////////////////////////////////////////////////////////////////////
289 /// \brief Append a filter to the call graph.
290 /// \param[in] f Function, lambda expression, functor class or any other callable object. It must return a `bool`
291 /// signalling whether the event has passed the selection (true) or not (false).
292 /// \param[in] name Optional name of this filter. See `Report`.
293 /// \return the filter node of the computation graph.
294 ///
295 /// Refer to the first overload of this method for the full documentation.
296 template <typename F, std::enable_if_t<!std::is_convertible<F, std::string>::value, int> = 0>
298 {
299 // The sfinae is there in order to pick up the overloaded method which accepts two strings
300 // rather than this template method.
301 return Filter(f, {}, name);
302 }
303
304 ////////////////////////////////////////////////////////////////////////////
305 /// \brief Append a filter to the call graph.
306 /// \param[in] f Function, lambda expression, functor class or any other callable object. It must return a `bool`
307 /// signalling whether the event has passed the selection (true) or not (false).
308 /// \param[in] columns Names of the columns/branches in input to the filter function.
309 /// \return the filter node of the computation graph.
310 ///
311 /// Refer to the first overload of this method for the full documentation.
312 template <typename F>
313 RInterface<RDFDetail::RFilter<F, Proxied>> Filter(F f, const std::initializer_list<std::string> &columns)
314 {
315 return Filter(f, ColumnNames_t{columns});
316 }
317
318 ////////////////////////////////////////////////////////////////////////////
319 /// \brief Append a filter to the call graph.
320 /// \param[in] expression The filter expression in C++
321 /// \param[in] name Optional name of this filter. See `Report`.
322 /// \return the filter node of the computation graph.
323 ///
324 /// The expression is just-in-time compiled and used to filter entries. It must
325 /// be valid C++ syntax in which variable names are substituted with the names
326 /// of branches/columns.
327 ///
328 /// ### Example usage:
329 /// ~~~{.cpp}
330 /// auto filtered_df = df.Filter("myCollection.size() > 3");
331 /// auto filtered_name_df = df.Filter("myCollection.size() > 3", "Minumum collection size");
332 /// ~~~
333 ///
334 /// \note If the body of the string expression contains an explicit `return` statement (even if it is in a nested
335 /// scope), RDataFrame _will not_ add another one in front of the expression. So this will not work:
336 /// ~~~{.cpp}
337 /// df.Filter("Sum(Map(vec, [](float e) { return e*e > 0.5; }))")
338 /// ~~~
339 /// but instead this will:
340 /// ~~~{.cpp}
341 /// df.Filter("return Sum(Map(vec, [](float e) { return e*e > 0.5; }))")
342 /// ~~~
343 RInterface<RDFDetail::RJittedFilter> Filter(std::string_view expression, std::string_view name = "")
344 {
345 const auto jittedFilter = RDFInternal::BookFilterJit(RDFInternal::UpcastNode(fProxiedPtr), name, expression,
346 fColRegister, nullptr, GetDataSource());
347
348 return RInterface<RDFDetail::RJittedFilter>(std::move(jittedFilter), *fLoopManager, fColRegister);
349 }
350
351 ////////////////////////////////////////////////////////////////////////////
352 /// \brief Discard entries with missing values
353 /// \param[in] column Column name whose entries with missing values should be discarded
354 /// \return The filter node of the computation graph
355 ///
356 /// This operation is useful in case an entry of the dataset is incomplete,
357 /// i.e. if one or more of the columns do not have valid values. If the value
358 /// of the input column is missing for an entry, the entire entry will be
359 /// discarded from the rest of this branch of the computation graph.
360 ///
361 /// Use cases include:
362 /// * When processing multiple files, one or more of them is missing a column
363 /// * In horizontal joining with entry matching, a certain dataset has no
364 /// match for the current entry.
365 ///
366 /// ### Example usage:
367 ///
368 /// \code{.py}
369 /// # Assume a dataset with columns [idx, x] matching another dataset with
370 /// # columns [idx, y]. For idx == 42, the right-hand dataset has no match
371 /// df = ROOT.RDataFrame(dataset)
372 /// df_nomissing = df.FilterAvailable("idx").Define("z", "x + y")
373 /// colz = df_nomissing.Take[int]("z")
374 /// \endcode
375 ///
376 /// \code{.cpp}
377 /// // Assume a dataset with columns [idx, x] matching another dataset with
378 /// // columns [idx, y]. For idx == 42, the right-hand dataset has no match
379 /// ROOT::RDataFrame df{dataset};
380 /// auto df_nomissing = df.FilterAvailable("idx")
381 /// .Define("z", [](int x, int y) { return x + y; }, {"x", "y"});
382 /// auto colz = df_nomissing.Take<int>("z");
383 /// \endcode
384 ///
385 /// \note See FilterMissing() if you want to keep only the entries with
386 /// missing values instead.
388 {
389 const auto columns = ColumnNames_t{column.data()};
390 // For now disable this functionality in case of an empty data source and
391 // the column name was not defined previously.
392 if (ROOT::Internal::RDF::GetDataSourceLabel(*this) == "EmptyDS")
393 throw std::runtime_error("Unknown column: \"" + std::string(column) + "\"");
395 auto filterPtr = std::make_shared<F_t>(/*discardEntry*/ true, fProxiedPtr, fColRegister, columns);
397 return RInterface<F_t>(std::move(filterPtr), *fLoopManager, fColRegister);
398 }
399
400 ////////////////////////////////////////////////////////////////////////////
401 /// \brief Keep only the entries that have missing values.
402 /// \param[in] column Column name whose entries with missing values should be kept
403 /// \return The filter node of the computation graph
404 ///
405 /// This operation is useful in case an entry of the dataset is incomplete,
406 /// i.e. if one or more of the columns do not have valid values. It only
407 /// keeps the entries for which the value of the input column is missing.
408 ///
409 /// Use cases include:
410 /// * When processing multiple files, one or more of them is missing a column
411 /// * In horizontal joining with entry matching, a certain dataset has no
412 /// match for the current entry.
413 ///
414 /// ### Example usage:
415 ///
416 /// \code{.py}
417 /// # Assume a dataset made of two files vertically chained together, one has
418 /// # column "x" and the other has column "y"
419 /// df = ROOT.RDataFrame(dataset)
420 /// df_valid_col_x = df.FilterMissing("y")
421 /// df_valid_col_y = df.FilterMissing("x")
422 /// display_x = df_valid_col_x.Display(("x",))
423 /// display_y = df_valid_col_y.Display(("y",))
424 /// \endcode
425 ///
426 /// \code{.cpp}
427 /// // Assume a dataset made of two files vertically chained together, one has
428 /// // column "x" and the other has column "y"
429 /// ROOT.RDataFrame df{dataset};
430 /// auto df_valid_col_x = df.FilterMissing("y");
431 /// auto df_valid_col_y = df.FilterMissing("x");
432 /// auto display_x = df_valid_col_x.Display<int>({"x"});
433 /// auto display_y = df_valid_col_y.Display<int>({"y"});
434 /// \endcode
435 ///
436 /// \note See FilterAvailable() if you want to discard the entries in case
437 /// there is a missing value instead.
439 {
440 const auto columns = ColumnNames_t{column.data()};
441 // For now disable this functionality in case of an empty data source and
442 // the column name was not defined previously.
443 if (ROOT::Internal::RDF::GetDataSourceLabel(*this) == "EmptyDS")
444 throw std::runtime_error("Unknown column: \"" + std::string(column) + "\"");
446 auto filterPtr = std::make_shared<F_t>(/*discardEntry*/ false, fProxiedPtr, fColRegister, columns);
448 return RInterface<F_t>(std::move(filterPtr), *fLoopManager, fColRegister);
449 }
450
451 // clang-format off
452 ////////////////////////////////////////////////////////////////////////////
453 /// \brief Define a new column.
454 /// \param[in] name The name of the defined column.
455 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column. This callable must be thread safe when used with multiple threads.
456 /// \param[in] columns Names of the columns/branches in input to the producer function.
457 /// \return the first node of the computation graph for which the new quantity is defined.
458 ///
459 /// Define a column that will be visible from all subsequent nodes
460 /// of the functional chain. The `expression` is only evaluated for entries that pass
461 /// all the preceding filters.
462 /// A new variable is created called `name`, accessible as if it was contained
463 /// in the dataset from subsequent transformations/actions.
464 ///
465 /// Use cases include:
466 /// * caching the results of complex calculations for easy and efficient multiple access
467 /// * extraction of quantities of interest from complex objects
468 ///
469 /// An exception is thrown if the name of the new column is already in use in this branch of the computation graph.
470 /// Note that the callable must be thread safe when called from multiple threads. Use DefineSlot() if needed.
471 ///
472 /// ### Example usage:
473 /// ~~~{.cpp}
474 /// // assuming a function with signature:
475 /// double myComplexCalculation(const RVec<float> &muon_pts);
476 /// // we can pass it directly to Define
477 /// auto df_with_define = df.Define("newColumn", myComplexCalculation, {"muon_pts"});
478 /// // alternatively, we can pass the body of the function as a string, as in Filter:
479 /// auto df_with_define = df.Define("newColumn", "x*x + y*y");
480 /// ~~~
481 ///
482 /// \note If the body of the string expression contains an explicit `return` statement (even if it is in a nested
483 /// scope), RDataFrame _will not_ add another one in front of the expression. So this will not work:
484 /// ~~~{.cpp}
485 /// df.Define("x2", "Map(v, [](float e) { return e*e; })")
486 /// ~~~
487 /// but instead this will:
488 /// ~~~{.cpp}
489 /// df.Define("x2", "return Map(v, [](float e) { return e*e; })")
490 /// ~~~
491 template <typename F, typename std::enable_if_t<!std::is_convertible<F, std::string>::value, int> = 0>
492 RInterface<Proxied> Define(std::string_view name, F expression, const ColumnNames_t &columns = {})
493 {
494 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::None>(name, std::move(expression), columns, "Define");
495 }
496 // clang-format on
497
498 // clang-format off
499 ////////////////////////////////////////////////////////////////////////////
500 /// \brief Define a new column with a value dependent on the processing slot.
501 /// \param[in] name The name of the defined column.
502 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column.
503 /// \param[in] columns Names of the columns/branches in input to the producer function (excluding the slot number).
504 /// \return the first node of the computation graph for which the new quantity is defined.
505 ///
506 /// This alternative implementation of `Define` is meant as a helper to evaluate new column values in a thread-safe manner.
507 /// The expression must be a callable of signature R(unsigned int, T1, T2, ...) where `T1, T2...` are the types
508 /// of the columns that the expression takes as input. The first parameter is reserved for an unsigned integer
509 /// representing a "slot number". RDataFrame guarantees that different threads will invoke the expression with
510 /// different slot numbers - slot numbers will range from zero to ROOT::GetThreadPoolSize()-1.
511 /// Note that there is no guarantee as to how often each slot will be reached during the event loop.
512 ///
513 /// The following two calls are equivalent, although `DefineSlot` is slightly more performant:
514 /// ~~~{.cpp}
515 /// int function(unsigned int, double, double);
516 /// df.Define("x", function, {"rdfslot_", "column1", "column2"})
517 /// df.DefineSlot("x", function, {"column1", "column2"})
518 /// ~~~
519 ///
520 /// See Define() for more information.
521 template <typename F>
522 RInterface<Proxied> DefineSlot(std::string_view name, F expression, const ColumnNames_t &columns = {})
523 {
524 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::Slot>(name, std::move(expression), columns, "DefineSlot");
525 }
526 // clang-format on
527
528 // clang-format off
529 ////////////////////////////////////////////////////////////////////////////
530 /// \brief Define a new column with a value dependent on the processing slot and the current entry.
531 /// \param[in] name The name of the defined column.
532 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column.
533 /// \param[in] columns Names of the columns/branches in input to the producer function (excluding slot and entry).
534 /// \return the first node of the computation graph for which the new quantity is defined.
535 ///
536 /// This alternative implementation of `Define` is meant as a helper in writing entry-specific, thread-safe custom
537 /// columns. The expression must be a callable of signature R(unsigned int, ULong64_t, T1, T2, ...) where `T1, T2...`
538 /// are the types of the columns that the expression takes as input. The first parameter is reserved for an unsigned
539 /// integer representing a "slot number". RDataFrame guarantees that different threads will invoke the expression with
540 /// different slot numbers - slot numbers will range from zero to ROOT::GetThreadPoolSize()-1.
541 /// Note that there is no guarantee as to how often each slot will be reached during the event loop.
542 /// The second parameter is reserved for a `ULong64_t` representing the current entry being processed by the current thread.
543 ///
544 /// The following two `Define`s are equivalent, although `DefineSlotEntry` is slightly more performant:
545 /// ~~~{.cpp}
546 /// int function(unsigned int, ULong64_t, double, double);
547 /// Define("x", function, {"rdfslot_", "rdfentry_", "column1", "column2"})
548 /// DefineSlotEntry("x", function, {"column1", "column2"})
549 /// ~~~
550 ///
551 /// See Define() for more information.
552 template <typename F>
553 RInterface<Proxied> DefineSlotEntry(std::string_view name, F expression, const ColumnNames_t &columns = {})
554 {
555 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::SlotAndEntry>(name, std::move(expression), columns,
556 "DefineSlotEntry");
557 }
558 // clang-format on
559
560 ////////////////////////////////////////////////////////////////////////////
561 /// \brief Define a new column.
562 /// \param[in] name The name of the defined column.
563 /// \param[in] expression An expression in C++ which represents the defined value
564 /// \return the first node of the computation graph for which the new quantity is defined.
565 ///
566 /// The expression is just-in-time compiled and used to produce the column entries.
567 /// It must be valid C++ syntax in which variable names are substituted with the names
568 /// of branches/columns.
569 ///
570 /// \note If the body of the string expression contains an explicit `return` statement (even if it is in a nested
571 /// scope), RDataFrame _will not_ add another one in front of the expression. So this will not work:
572 /// ~~~{.cpp}
573 /// df.Define("x2", "Map(v, [](float e) { return e*e; })")
574 /// ~~~
575 /// but instead this will:
576 /// ~~~{.cpp}
577 /// df.Define("x2", "return Map(v, [](float e) { return e*e; })")
578 /// ~~~
579 ///
580 /// Refer to the first overload of this method for the full documentation.
581 RInterface<Proxied> Define(std::string_view name, std::string_view expression)
582 {
583 constexpr auto where = "Define";
585 // these checks must be done before jitting lest we throw exceptions in jitted code
588
589 auto jittedDefine = RDFInternal::BookDefineJit(name, expression, *fLoopManager, GetDataSource(), fColRegister);
590
592 newCols.AddDefine(std::move(jittedDefine));
593
594 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
595
596 return newInterface;
597 }
598
599 ////////////////////////////////////////////////////////////////////////////
600 /// \brief Overwrite the value and/or type of an existing column.
601 /// \param[in] name The name of the column to redefine.
602 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column.
603 /// \param[in] columns Names of the columns/branches in input to the expression.
604 /// \return the first node of the computation graph for which the quantity is redefined.
605 ///
606 /// The old value of the column can be used as an input for the expression.
607 ///
608 /// An exception is thrown in case the column to redefine does not already exist.
609 /// See Define() for more information.
610 template <typename F, std::enable_if_t<!std::is_convertible<F, std::string>::value, int> = 0>
611 RInterface<Proxied> Redefine(std::string_view name, F expression, const ColumnNames_t &columns = {})
612 {
613 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::None>(name, std::move(expression), columns, "Redefine");
614 }
615
616 // clang-format off
617 ////////////////////////////////////////////////////////////////////////////
618 /// \brief Overwrite the value and/or type of an existing column.
619 /// \param[in] name The name of the column to redefine.
620 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column.
621 /// \param[in] columns Names of the columns/branches in input to the producer function (excluding slot).
622 /// \return the first node of the computation graph for which the new quantity is defined.
623 ///
624 /// The old value of the column can be used as an input for the expression.
625 /// An exception is thrown in case the column to redefine does not already exist.
626 ///
627 /// See DefineSlot() for more information.
628 // clang-format on
629 template <typename F>
630 RInterface<Proxied> RedefineSlot(std::string_view name, F expression, const ColumnNames_t &columns = {})
631 {
632 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::Slot>(name, std::move(expression), columns, "RedefineSlot");
633 }
634
635 // clang-format off
636 ////////////////////////////////////////////////////////////////////////////
637 /// \brief Overwrite the value and/or type of an existing column.
638 /// \param[in] name The name of the column to redefine.
639 /// \param[in] expression Function, lambda expression, functor class or any other callable object producing the defined value. Returns the value that will be assigned to the defined column.
640 /// \param[in] columns Names of the columns/branches in input to the producer function (excluding slot and entry).
641 /// \return the first node of the computation graph for which the new quantity is defined.
642 ///
643 /// The old value of the column can be used as an input for the expression.
644 /// An exception is thrown in case the column to re-define does not already exist.
645 ///
646 /// See DefineSlotEntry() for more information.
647 // clang-format on
648 template <typename F>
649 RInterface<Proxied> RedefineSlotEntry(std::string_view name, F expression, const ColumnNames_t &columns = {})
650 {
651 return DefineImpl<F, RDFDetail::ExtraArgsForDefine::SlotAndEntry>(name, std::move(expression), columns,
652 "RedefineSlotEntry");
653 }
654
655 ////////////////////////////////////////////////////////////////////////////
656 /// \brief Overwrite the value and/or type of an existing column.
657 /// \param[in] name The name of the column to redefine.
658 /// \param[in] expression An expression in C++ which represents the defined value
659 /// \return the first node of the computation graph for which the new quantity is defined.
660 ///
661 /// The expression is just-in-time compiled and used to produce the column entries.
662 /// It must be valid C++ syntax in which variable names are substituted with the names
663 /// of branches/columns.
664 ///
665 /// The old value of the column can be used as an input for the expression.
666 /// An exception is thrown in case the column to re-define does not already exist.
667 ///
668 /// Aliases cannot be overridden. See the corresponding Define() overload for more information.
669 RInterface<Proxied> Redefine(std::string_view name, std::string_view expression)
670 {
671 constexpr auto where = "Redefine";
676
677 auto jittedDefine = RDFInternal::BookDefineJit(name, expression, *fLoopManager, GetDataSource(), fColRegister);
678
680 newCols.AddDefine(std::move(jittedDefine));
681
682 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
683
684 return newInterface;
685 }
686
687 ////////////////////////////////////////////////////////////////////////////
688 /// \brief In case the value in the given column is missing, provide a default value
689 /// \tparam T The type of the column
690 /// \param[in] column Column name where missing values should be replaced by the given default value
691 /// \param[in] defaultValue Value to provide instead of a missing value
692 /// \return The node of the graph that will provide a default value
693 ///
694 /// This operation is useful in case an entry of the dataset is incomplete,
695 /// i.e. if one or more of the columns do not have valid values. It does not
696 /// modify the values of the column, but in case any entry is missing, it
697 /// will provide the default value to downstream nodes instead.
698 ///
699 /// Use cases include:
700 /// * When processing multiple files, one or more of them is missing a column
701 /// * In horizontal joining with entry matching, a certain dataset has no
702 /// match for the current entry.
703 ///
704 /// ### Example usage:
705 ///
706 /// \code{.cpp}
707 /// // Assume a dataset with columns [idx, x] matching another dataset with
708 /// // columns [idx, y]. For idx == 42, the right-hand dataset has no match
709 /// ROOT::RDataFrame df{dataset};
710 /// auto df_default = df.DefaultValueFor("y", 33)
711 /// .Define("z", [](int x, int y) { return x + y; }, {"x", "y"});
712 /// auto colz = df_default.Take<int>("z");
713 /// \endcode
714 ///
715 /// \code{.py}
716 /// df = ROOT.RDataFrame(dataset)
717 /// df_default = df.DefaultValueFor("y", 33).Define("z", "x + y")
718 /// colz = df_default.Take[int]("z")
719 /// \endcode
720 template <typename T>
721 RInterface<Proxied> DefaultValueFor(std::string_view column, const T &defaultValue)
722 {
723 constexpr auto where{"DefaultValueFor"};
725 // For now disable this functionality in case of an empty data source and
726 // the column name was not defined previously.
727 if (ROOT::Internal::RDF::GetDataSourceLabel(*this) == "EmptyDS")
730
731 // Declare return type to the interpreter, for future use by jitted actions
732 auto retTypeName = RDFInternal::TypeID2TypeName(typeid(T));
733 if (retTypeName.empty()) {
734 // The type is not known to the interpreter.
735 // We must not error out here, but if/when this column is used in jitted code
736 const auto demangledType = RDFInternal::DemangleTypeIdName(typeid(T));
737 retTypeName = "CLING_UNKNOWN_TYPE_" + demangledType;
738 }
739
740 const auto validColumnNames = ColumnNames_t{column.data()};
741 auto newColumn = std::make_shared<ROOT::Internal::RDF::RDefaultValueFor<T>>(
742 column, retTypeName, defaultValue, validColumnNames, fColRegister, *fLoopManager);
743 CheckAndFillDSColumns(validColumnNames, TTraits::TypeList<T>{});
744
746 newCols.AddDefine(std::move(newColumn));
747
748 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
749
750 return newInterface;
751 }
752
753 // clang-format off
754 ////////////////////////////////////////////////////////////////////////////
755 /// \brief Define a new column that is updated when the input sample changes.
756 /// \param[in] name The name of the defined column.
757 /// \param[in] expression A C++ callable that computes the new value of the defined column.
758 /// \return the first node of the computation graph for which the new quantity is defined.
759 ///
760 /// The signature of the callable passed as second argument should be `T(unsigned int slot, const ROOT::RDF::RSampleInfo &id)`
761 /// where:
762 /// - `T` is the type of the defined column
763 /// - `slot` is a number in the range [0, nThreads) that is different for each processing thread. This can simplify
764 /// the definition of thread-safe callables if you are interested in using parallel capabilities of RDataFrame.
765 /// - `id` is an instance of a ROOT::RDF::RSampleInfo object which contains information about the sample which is
766 /// being processed (see the class docs for more information).
767 ///
768 /// DefinePerSample() is useful to e.g. define a quantity that depends on which TTree in which TFile is being
769 /// processed or to inject a callback into the event loop that is only called when the processing of a new sample
770 /// starts rather than at every entry.
771 ///
772 /// The callable will be invoked once per input TTree or once per multi-thread task, whichever is more often.
773 ///
774 /// ### Example usage:
775 /// ~~~{.cpp}
776 /// ROOT::RDataFrame df{"mytree", {"sample1.root","sample2.root"}};
777 /// df.DefinePerSample("weightbysample",
778 /// [](unsigned int slot, const ROOT::RDF::RSampleInfo &id)
779 /// { return id.Contains("sample1") ? 1.0f : 2.0f; });
780 /// ~~~
781 // clang-format on
782 // TODO we could SFINAE on F's signature to provide friendlier compilation errors in case of signature mismatch
783 template <typename F, typename RetType_t = typename TTraits::CallableTraits<F>::ret_type>
784 RInterface<Proxied> DefinePerSample(std::string_view name, F expression)
785 {
786 return DefinePerSampleImpl<F, RetType_t>(name, std::move(expression), false);
787 }
788
789 ////////////////////////////////////////////////////////////////////////////
790 /// \brief Redefine an existing column that is updated when the input sample changes.
791 /// \sa DefinePerSample. Works similarly, but the column must already exist and will be overwritten.
792 template <typename F, typename RetType_t = typename TTraits::CallableTraits<F>::ret_type>
793 RInterface<Proxied> RedefinePerSample(std::string_view name, F expression)
794 {
795 return DefinePerSampleImpl<F, RetType_t>(name, std::move(expression), true);
796 }
797
798 // clang-format off
799 ////////////////////////////////////////////////////////////////////////////
800 /// \brief Define a new column that is updated when the input sample changes.
801 /// \param[in] name The name of the defined column.
802 /// \param[in] expression A valid C++ expression as a string, which will be used to compute the defined value.
803 /// \return the first node of the computation graph for which the new quantity is defined.
804 ///
805 /// The expression is just-in-time compiled and used to produce the column entries.
806 /// It must be valid C++ syntax and the usage of the special variable names `rdfslot_` and `rdfsampleinfo_` is
807 /// permitted, where these variables will take the same values as the `slot` and `id` parameters described at the
808 /// DefinePerSample(std::string_view name, F expression) overload. See the documentation of that overload for more information.
809 ///
810 /// ### Example usage:
811 /// ~~~{.py}
812 /// df = ROOT.RDataFrame('mytree', ['sample1.root','sample2.root'])
813 /// df.DefinePerSample('weightbysample', 'rdfsampleinfo_.Contains("sample1") ? 1.0f : 2.0f')
814 /// ~~~
815 ///
816 /// \note
817 /// If you have declared some C++ function to the interpreter, the correct syntax to call that function with this
818 /// overload of DefinePerSample is by calling it explicitly with the special names `rdfslot_` and `rdfsampleinfo_` as
819 /// input parameters. This is for example the correct way to call this overload when working in PyROOT:
820 /// ~~~{.py}
821 /// ROOT.gInterpreter.Declare(
822 /// """
823 /// float weights(unsigned int slot, const ROOT::RDF::RSampleInfo &id){
824 /// return id.Contains("sample1") ? 1.0f : 2.0f;
825 /// }
826 /// """)
827 /// df = ROOT.RDataFrame("mytree", ["sample1.root","sample2.root"])
828 /// df.DefinePerSample("weightsbysample", "weights(rdfslot_, rdfsampleinfo_)")
829 /// ~~~
830 ///
831 /// \note
832 /// Differently from what happens in Define(), the string expression passed to DefinePerSample cannot contain
833 /// column names other than those mentioned above: the expression is evaluated once before the processing of the
834 /// sample even starts, so column values are not accessible.
835 // clang-format on
836 RInterface<Proxied> DefinePerSample(std::string_view name, std::string_view expression)
837 {
838 return DefinePerSampleJitImpl(name, expression, false);
839 }
840
841 ////////////////////////////////////////////////////////////////////////////
842 /// \brief Redefine an existing column that is updated when the input sample changes.
843 /// \sa DefinePerSample. Works similarly, but the column must already exist and will be overwritten.
844 RInterface<Proxied> RedefinePerSample(std::string_view name, std::string_view expression)
845 {
846 return DefinePerSampleJitImpl(name, expression, true);
847 }
848
849 /// \brief Register systematic variations for a single existing column using custom variation tags.
850 /// \param[in] colName name of the column for which varied values are provided.
851 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
852 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
853 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
854 /// \param[in] inputColumns the names of the columns to be passed to the callable.
855 /// \param[in] variationTags names for each of the varied values, e.g. `"up"` and `"down"`.
856 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
857 ///
858 /// Vary provides a natural and flexible syntax to define systematic variations that automatically propagate to
859 /// Filters, Defines and results. RDataFrame usage of columns with attached variations does not change, but for
860 /// results that depend on any varied quantity, a map/dictionary of varied results can be produced with
861 /// ROOT::RDF::Experimental::VariationsFor (see the example below).
862 ///
863 /// The dictionary will contain a "nominal" value (accessed with the "nominal" key) for the unchanged result, and
864 /// values for each of the systematic variations that affected the result (via upstream Filters or via direct or
865 /// indirect dependencies of the column values on some registered variations). The keys will be a composition of
866 /// variation names and tags, e.g. "pt:up" and "pt:down" for the example below.
867 ///
868 /// In the following example we add up/down variations of pt and fill a histogram with a quantity that depends on pt.
869 /// We automatically obtain three histograms in output ("nominal", "pt:up" and "pt:down"):
870 /// ~~~{.cpp}
871 /// auto nominal_hx =
872 /// df.Vary("pt", [] (double pt) { return RVecD{pt*0.9, pt*1.1}; }, {"down", "up"})
873 /// .Filter("pt > k")
874 /// .Define("x", someFunc, {"pt"})
875 /// .Histo1D("x");
876 ///
877 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
878 /// hx["nominal"].Draw();
879 /// hx["pt:down"].Draw("SAME");
880 /// hx["pt:up"].Draw("SAME");
881 /// ~~~
882 /// RDataFrame computes all variations as part of a single loop over the data.
883 /// In particular, this means that I/O and computation of values shared
884 /// among variations only happen once for all variations. Thus, the event loop
885 /// run-time typically scales much better than linearly with the number of
886 /// variations.
887 ///
888 /// RDataFrame lazily computes the varied values required to produce the
889 /// outputs of \ref ROOT::RDF::Experimental::VariationsFor "VariationsFor()". If \ref
890 /// ROOT::RDF::Experimental::VariationsFor "VariationsFor()" was not called for a result, the computations are only
891 /// run for the nominal case.
892 ///
893 /// See other overloads for examples when variations are added for multiple existing columns,
894 /// or when the tags are auto-generated instead of being directly defined.
895 template <typename F>
896 RInterface<Proxied> Vary(std::string_view colName, F &&expression, const ColumnNames_t &inputColumns,
897 const std::vector<std::string> &variationTags, std::string_view variationName = "")
898 {
899 std::vector<std::string> colNames{{std::string(colName)}};
900 const std::string theVariationName{variationName.empty() ? colName : variationName};
901
902 return VaryImpl<true>(std::move(colNames), std::forward<F>(expression), inputColumns, variationTags,
903 theVariationName);
904 }
905
906 /// \brief Register systematic variations for a single existing column using auto-generated variation tags.
907 /// \param[in] colName name of the column for which varied values are provided.
908 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
909 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
910 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
911 /// \param[in] inputColumns the names of the columns to be passed to the callable.
912 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
913 /// `"1"`, etc.
914 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
915 /// colName is used if none is provided.
916 ///
917 /// This overload of Vary takes an nVariations parameter instead of a list of tag names.
918 /// The varied results will be accessible via the keys of the dictionary with the form `variationName:N` where `N`
919 /// is the corresponding sequential tag starting at 0 and going up to `nVariations - 1`.
920 ///
921 /// Example usage:
922 /// ~~~{.cpp}
923 /// auto nominal_hx =
924 /// df.Vary("pt", [] (double pt) { return RVecD{pt*0.9, pt*1.1}; }, 2)
925 /// .Histo1D("x");
926 ///
927 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
928 /// hx["nominal"].Draw();
929 /// hx["x:0"].Draw("SAME");
930 /// hx["x:1"].Draw("SAME");
931 /// ~~~
932 ///
933 /// \note See also This Vary() overload for more information.
934 template <typename F>
935 RInterface<Proxied> Vary(std::string_view colName, F &&expression, const ColumnNames_t &inputColumns,
936 std::size_t nVariations, std::string_view variationName = "")
937 {
938 R__ASSERT(nVariations > 0 && "Must have at least one variation.");
939
940 std::vector<std::string> variationTags;
941 variationTags.reserve(nVariations);
942 for (std::size_t i = 0u; i < nVariations; ++i)
943 variationTags.emplace_back(std::to_string(i));
944
945 const std::string theVariationName{variationName.empty() ? colName : variationName};
946
947 return Vary(colName, std::forward<F>(expression), inputColumns, std::move(variationTags), theVariationName);
948 }
949
950 /// \brief Register systematic variations for multiple existing columns using custom variation tags.
951 /// \param[in] colNames set of names of the columns for which varied values are provided.
952 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
953 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
954 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
955 /// \param[in] inputColumns the names of the columns to be passed to the callable.
956 /// \param[in] variationTags names for each of the varied values, e.g. `"up"` and `"down"`.
957 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`
958 ///
959 /// This overload of Vary takes a list of column names as first argument and
960 /// requires that the expression returns an RVec of RVecs of values: one inner RVec for the variations of each
961 /// affected column. The `variationTags` are defined as `{"down", "up"}`.
962 ///
963 /// Example usage:
964 /// ~~~{.cpp}
965 /// // produce variations "ptAndEta:down" and "ptAndEta:up"
966 /// auto nominal_hx =
967 /// df.Vary({"pt", "eta"}, // the columns that will vary simultaneously
968 /// [](double pt, double eta) { return RVec<RVecF>{{pt*0.9, pt*1.1}, {eta*0.9, eta*1.1}}; },
969 /// {"pt", "eta"}, // inputs to the Vary expression, independent of what columns are varied
970 /// {"down", "up"}, // variation tags
971 /// "ptAndEta") // variation name
972 /// .Histo1D("pt", "eta");
973 ///
974 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
975 /// hx["nominal"].Draw();
976 /// hx["ptAndEta:down"].Draw("SAME");
977 /// hx["ptAndEta:up"].Draw("SAME");
978 /// ~~~
979 ///
980 /// \note See also This Vary() overload for more information.
981
982 template <typename F>
983 RInterface<Proxied> Vary(const std::vector<std::string> &colNames, F &&expression, const ColumnNames_t &inputColumns,
984 const std::vector<std::string> &variationTags, std::string_view variationName)
985 {
986 return VaryImpl<false>(colNames, std::forward<F>(expression), inputColumns, variationTags, variationName);
987 }
988
989 /// \brief Register systematic variations for multiple existing columns using custom variation tags.
990 /// \param[in] colNames set of names of the columns for which varied values are provided.
991 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
992 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
993 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
994 /// \param[in] inputColumns the names of the columns to be passed to the callable.
995 /// \param[in] variationTags names for each of the varied values, e.g. `"up"` and `"down"`.
996 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
997 /// colName is used if none is provided.
998 ///
999 /// \note This overload ensures that the ambiguity between C++20 string, vector<string> construction from init list
1000 /// is avoided.
1001 ///
1002 /// \note See also This Vary() overload for more information.
1003 template <typename F>
1005 Vary(std::initializer_list<std::string> colNames, F &&expression, const ColumnNames_t &inputColumns,
1006 const std::vector<std::string> &variationTags, std::string_view variationName)
1007 {
1008 return Vary(std::vector<std::string>(colNames), std::forward<F>(expression), inputColumns, variationTags, variationName);
1009 }
1010
1011 /// \brief Register systematic variations for multiple existing columns using auto-generated tags.
1012 /// \param[in] colNames set of names of the columns for which varied values are provided.
1013 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
1014 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
1015 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
1016 /// \param[in] inputColumns the names of the columns to be passed to the callable.
1017 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
1018 /// `"1"`, etc.
1019 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1020 /// colName is used if none is provided.
1021 ///
1022 /// This overload of Vary takes a list of column names as first argument.
1023 /// It takes an `nVariations` parameter instead of a list of tag names (`variationTags`). Tag names
1024 /// will be auto-generated as the sequence 0...``nVariations-1``.
1025 ///
1026 /// Example usage:
1027 /// ~~~{.cpp}
1028 /// auto nominal_hx =
1029 /// df.Vary({"pt", "eta"}, // the columns that will vary simultaneously
1030 /// [](double pt, double eta) { return RVec<RVecF>{{pt*0.9, pt*1.1}, {eta*0.9, eta*1.1}}; },
1031 /// {"pt", "eta"}, // inputs to the Vary expression, independent of what columns are varied
1032 /// 2, // auto-generated variation tags
1033 /// "ptAndEta") // variation name
1034 /// .Histo1D("pt", "eta");
1035 ///
1036 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1037 /// hx["nominal"].Draw();
1038 /// hx["ptAndEta:0"].Draw("SAME");
1039 /// hx["ptAndEta:1"].Draw("SAME");
1040 /// ~~~
1041 ///
1042 /// \note See also This Vary() overload for more information.
1043 template <typename F>
1044 RInterface<Proxied> Vary(const std::vector<std::string> &colNames, F &&expression, const ColumnNames_t &inputColumns,
1045 std::size_t nVariations, std::string_view variationName)
1046 {
1047 R__ASSERT(nVariations > 0 && "Must have at least one variation.");
1048
1049 std::vector<std::string> variationTags;
1050 variationTags.reserve(nVariations);
1051 for (std::size_t i = 0u; i < nVariations; ++i)
1052 variationTags.emplace_back(std::to_string(i));
1053
1054 return Vary(colNames, std::forward<F>(expression), inputColumns, std::move(variationTags), variationName);
1055 }
1056
1057 /// \brief Register systematic variations for for multiple existing columns using custom variation tags.
1058 /// \param[in] colNames set of names of the columns for which varied values are provided.
1059 /// \param[in] expression a callable that evaluates the varied values for the specified columns. The callable can
1060 /// take any column values as input, similarly to what happens during Filter and Define calls. It must
1061 /// return an RVec of varied values, one for each variation tag, in the same order as the tags.
1062 /// \param[in] inputColumns the names of the columns to be passed to the callable.
1063 /// \param[in] inputColumns the names of the columns to be passed to the callable.
1064 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
1065 /// `"1"`, etc.
1066 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1067 /// colName is used if none is provided.
1068 ///
1069 /// \note This overload ensures that the ambiguity between C++20 string, vector<string> construction from init list
1070 /// is avoided.
1071 ///
1072 /// \note See also This Vary() overload for more information.
1073 template <typename F>
1074 RInterface<Proxied> Vary(std::initializer_list<std::string> colNames, F &&expression,
1075 const ColumnNames_t &inputColumns, std::size_t nVariations, std::string_view variationName)
1076 {
1077 return Vary(std::vector<std::string>(colNames), std::forward<F>(expression), inputColumns, nVariations, variationName);
1078 }
1079
1080 /// \brief Register systematic variations for a single existing column using custom variation tags.
1081 /// \param[in] colName name of the column for which varied values are provided.
1082 /// \param[in] expression a string containing valid C++ code that evaluates to an RVec containing the varied
1083 /// values for the specified column.
1084 /// \param[in] variationTags names for each of the varied values, e.g. `"up"` and `"down"`.
1085 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1086 /// colName is used if none is provided.
1087 ///
1088 /// This overload adds the possibility for the expression used to evaluate the varied values to be just-in-time
1089 /// compiled. The example below shows how Vary() is used while dealing with a single column. The variation tags are
1090 /// defined as `{"down", "up"}`.
1091 /// ~~~{.cpp}
1092 /// auto nominal_hx =
1093 /// df.Vary("pt", "ROOT::RVecD{pt*0.9, pt*1.1}", {"down", "up"})
1094 /// .Filter("pt > k")
1095 /// .Define("x", someFunc, {"pt"})
1096 /// .Histo1D("x");
1097 ///
1098 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1099 /// hx["nominal"].Draw();
1100 /// hx["pt:down"].Draw("SAME");
1101 /// hx["pt:up"].Draw("SAME");
1102 /// ~~~
1103 ///
1104 /// ## Short-hand expression syntax
1105 ///
1106 /// For convenience, when a C++ expression is passed to Vary, the return type can be omitted if the string begins
1107 /// with '{' and ends with '}' (whitespace, tab and newline characters are excluded from the search). This means that
1108 /// the following is equivalent to the example above:
1109 ///
1110 /// ~~~{.cpp}
1111 /// auto nominal_hx =
1112 /// df.Vary("pt", "{pt*0.9, pt*1.1}", {"down", "up"})
1113 /// // Same as above
1114 /// ~~~
1115 ///
1116 /// \note See also This Vary() overload for more information.
1117 RInterface<Proxied> Vary(std::string_view colName, std::string_view expression,
1118 const std::vector<std::string> &variationTags, std::string_view variationName = "")
1119 {
1120 std::vector<std::string> colNames{{std::string(colName)}};
1121 const std::string theVariationName{variationName.empty() ? colName : variationName};
1122
1123 return JittedVaryImpl(colNames, expression, variationTags, theVariationName, /*isSingleColumn=*/true);
1124 }
1125
1126 /// \brief Register systematic variations for a single existing column using auto-generated variation tags.
1127 /// \param[in] colName name of the column for which varied values are provided.
1128 /// \param[in] expression a string containing valid C++ code that evaluates to an RVec containing the varied
1129 /// values for the specified column.
1130 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
1131 /// `"1"`, etc.
1132 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1133 /// colName is used if none is provided.
1134 ///
1135 /// This overload adds the possibility for the expression used to evaluate the varied values to be a just-in-time
1136 /// compiled. The example below shows how Vary() is used while dealing with a single column. The variation tags are
1137 /// auto-generated.
1138 /// ~~~{.cpp}
1139 /// auto nominal_hx =
1140 /// df.Vary("pt", "ROOT::RVecD{pt*0.9, pt*1.1}", 2)
1141 /// .Histo1D("pt");
1142 ///
1143 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1144 /// hx["nominal"].Draw();
1145 /// hx["pt:0"].Draw("SAME");
1146 /// hx["pt:1"].Draw("SAME");
1147 /// ~~~
1148 ///
1149 /// ## Short-hand expression syntax
1150 ///
1151 /// For convenience, when a C++ expression is passed to Vary, the return type can be omitted if the string begins
1152 /// with '{' and ends with '}' (whitespace, tab and newline characters are excluded from the search). This means that
1153 /// the following is equivalent to the example above:
1154 ///
1155 /// ~~~{.cpp}
1156 /// auto nominal_hx =
1157 /// df.Vary("pt", "{pt*0.9, pt*1.1}", 2)
1158 /// // Same as above
1159 /// ~~~
1160 ///
1161 /// \note See also This Vary() overload for more information.
1162 RInterface<Proxied> Vary(std::string_view colName, std::string_view expression, std::size_t nVariations,
1163 std::string_view variationName = "")
1164 {
1165 std::vector<std::string> variationTags;
1166 variationTags.reserve(nVariations);
1167 for (std::size_t i = 0u; i < nVariations; ++i)
1168 variationTags.emplace_back(std::to_string(i));
1169
1170 return Vary(colName, expression, std::move(variationTags), variationName);
1171 }
1172
1173 /// \brief Register systematic variations for multiple existing columns using auto-generated variation tags.
1174 /// \param[in] colNames set of names of the columns for which varied values are provided.
1175 /// \param[in] expression a string containing valid C++ code that evaluates to an RVec or RVecs containing the varied
1176 /// values for the specified columns.
1177 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
1178 /// `"1"`, etc.
1179 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1180 ///
1181 /// This overload adds the possibility for the expression used to evaluate the varied values to be just-in-time
1182 /// compiled. It takes an nVariations parameter instead of a list of tag names.
1183 /// The varied results will be accessible via the keys of the dictionary with the form `variationName:N` where `N`
1184 /// is the corresponding sequential tag starting at 0 and going up to `nVariations - 1`.
1185 /// The example below shows how Vary() is used while dealing with multiple columns.
1186 ///
1187 /// ~~~{.cpp}
1188 /// auto nominal_hx =
1189 /// df.Vary({"x", "y"}, "ROOT::RVec<ROOT::RVecD>{{x*0.9, x*1.1}, {y*0.9, y*1.1}}", 2, "xy")
1190 /// .Histo1D("x", "y");
1191 ///
1192 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1193 /// hx["nominal"].Draw();
1194 /// hx["xy:0"].Draw("SAME");
1195 /// hx["xy:1"].Draw("SAME");
1196 /// ~~~
1197 ///
1198 /// ## Short-hand expression syntax
1199 ///
1200 /// For convenience, when a C++ expression is passed to Vary, the return type can be omitted if the string begins
1201 /// with '{' and ends with '}' (whitespace, tab and newline characters are excluded from the search). This means that
1202 /// the following is equivalent to the example above:
1203 ///
1204 /// ~~~{.cpp}
1205 /// auto nominal_hx =
1206 /// df.Vary("pt", "{{x*0.9, x*1.1}, {y*0.9, y*1.1}}", 2, "xy")
1207 /// // Same as above
1208 /// ~~~
1209 ///
1210 /// or also:
1211 ///
1212 /// ~~~{.cpp}
1213 /// auto nominal_hx =
1214 /// df.Vary("pt", R"(
1215 /// {
1216 /// {x*0.9, x*1.1}, // x variations
1217 /// {y*0.9, y*1.1} // y variations
1218 /// }
1219 /// )", 2, "xy")
1220 /// // Same as above
1221 /// ~~~
1222 ///
1223 /// \note See also This Vary() overload for more information.
1224 RInterface<Proxied> Vary(const std::vector<std::string> &colNames, std::string_view expression,
1225 std::size_t nVariations, std::string_view variationName)
1226 {
1227 std::vector<std::string> variationTags;
1228 variationTags.reserve(nVariations);
1229 for (std::size_t i = 0u; i < nVariations; ++i)
1230 variationTags.emplace_back(std::to_string(i));
1231
1232 return Vary(colNames, expression, std::move(variationTags), variationName);
1233 }
1234
1235 /// \brief Register systematic variations for multiple existing columns using auto-generated variation tags.
1236 /// \param[in] colNames set of names of the columns for which varied values are provided.
1237 /// \param[in] expression a string containing valid C++ code that evaluates to an RVec containing the varied
1238 /// values for the specified column.
1239 /// \param[in] nVariations number of variations returned by the expression. The corresponding tags will be `"0"`,
1240 /// `"1"`, etc.
1241 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1242 /// colName is used if none is provided.
1243 ///
1244 /// \note This overload ensures that the ambiguity between C++20 string, vector<string> construction from init list
1245 /// is avoided.
1246 ///
1247 /// \note See also This Vary() overload for more information.
1248 RInterface<Proxied> Vary(std::initializer_list<std::string> colNames, std::string_view expression,
1249 std::size_t nVariations, std::string_view variationName)
1250 {
1251 return Vary(std::vector<std::string>(colNames), expression, nVariations, variationName);
1252 }
1253
1254 /// \brief Register systematic variations for multiple existing columns using custom variation tags.
1255 /// \param[in] colNames set of names of the columns for which varied values are provided.
1256 /// \param[in] expression a string containing valid C++ code that evaluates to an RVec or RVecs containing the varied
1257 /// values for the specified columns.
1258 /// \param[in] variationTags names for each of the varied values, e.g. `"up"` and `"down"`.
1259 /// \param[in] variationName a generic name for this set of varied values, e.g. `"ptvariation"`.
1260 ///
1261 /// This overload adds the possibility for the expression used to evaluate the varied values to be just-in-time
1262 /// compiled. The example below shows how Vary() is used while dealing with multiple columns. The tags are defined as
1263 /// `{"down", "up"}`.
1264 /// ~~~{.cpp}
1265 /// auto nominal_hx =
1266 /// df.Vary({"x", "y"}, "ROOT::RVec<ROOT::RVecD>{{x*0.9, x*1.1}, {y*0.9, y*1.1}}", {"down", "up"}, "xy")
1267 /// .Histo1D("x", "y");
1268 ///
1269 /// auto hx = ROOT::RDF::Experimental::VariationsFor(nominal_hx);
1270 /// hx["nominal"].Draw();
1271 /// hx["xy:down"].Draw("SAME");
1272 /// hx["xy:up"].Draw("SAME");
1273 /// ~~~
1274 ///
1275 /// ## Short-hand expression syntax
1276 ///
1277 /// For convenience, when a C++ expression is passed to Vary, the return type can be omitted if the string begins
1278 /// with '{' and ends with '}' (whitespace, tab and newline characters are excluded from the search). This means that
1279 /// the following is equivalent to the example above:
1280 ///
1281 /// ~~~{.cpp}
1282 /// auto nominal_hx =
1283 /// df.Vary("pt", "{{x*0.9, x*1.1}, {y*0.9, y*1.1}}", {"down", "up"}, "xy")
1284 /// // Same as above
1285 /// ~~~
1286 ///
1287 /// or also:
1288 ///
1289 /// ~~~{.cpp}
1290 /// auto nominal_hx =
1291 /// df.Vary("pt", R"(
1292 /// {
1293 /// {x*0.9, x*1.1}, // x variations
1294 /// {y*0.9, y*1.1} // y variations
1295 /// }
1296 /// )", {"down", "up"}, "xy")
1297 /// // Same as above
1298 /// ~~~
1299 ///
1300 /// \note See also This Vary() overload for more information.
1301 RInterface<Proxied> Vary(const std::vector<std::string> &colNames, std::string_view expression,
1302 const std::vector<std::string> &variationTags, std::string_view variationName)
1303 {
1304 return JittedVaryImpl(colNames, expression, variationTags, variationName, /*isSingleColumn=*/false);
1305 }
1306
1307 ////////////////////////////////////////////////////////////////////////////
1308 /// \brief Allow to refer to a column with a different name.
1309 /// \param[in] alias name of the column alias
1310 /// \param[in] columnName of the column to be aliased
1311 /// \return the first node of the computation graph for which the alias is available.
1312 ///
1313 /// Aliasing an alias is supported.
1314 ///
1315 /// ### Example usage:
1316 /// ~~~{.cpp}
1317 /// auto df_with_alias = df.Alias("simple_name", "very_long&complex_name!!!");
1318 /// ~~~
1319 RInterface<Proxied> Alias(std::string_view alias, std::string_view columnName)
1320 {
1321 // The symmetry with Define is clear. We want to:
1322 // - Create globally the alias and return this very node, unchanged
1323 // - Make aliases accessible based on chains and not globally
1324
1325 // Helper to find out if a name is a column
1326 auto &dsColumnNames = GetDataSource() ? GetDataSource()->GetColumnNames() : ColumnNames_t{};
1327
1328 constexpr auto where = "Alias";
1330 // If the alias name is a column name, there is a problem
1331 RDFInternal::CheckForRedefinition(where, alias, fColRegister, dsColumnNames);
1332
1333 const auto validColumnName = GetValidatedColumnNames(1, {std::string(columnName)})[0];
1334
1336 newCols.AddAlias(alias, validColumnName);
1337
1338 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
1339
1340 return newInterface;
1341 }
1342
1343 // clang-format off
1344 ////////////////////////////////////////////////////////////////////////////
1345 /// \brief Creates a node that filters entries based on range: [begin, end).
1346 /// \param[in] begin Initial entry number considered for this range.
1347 /// \param[in] end Final entry number (excluded) considered for this range. 0 means that the range goes until the end of the dataset.
1348 /// \param[in] stride Process one entry of the [begin, end) range every `stride` entries. Must be strictly greater than 0.
1349 /// \return the first node of the computation graph for which the event loop is limited to a certain range of entries.
1350 ///
1351 /// Note that in case of previous Ranges and Filters the selected range refers to the transformed dataset.
1352 /// Ranges are only available if EnableImplicitMT has _not_ been called. Multi-thread ranges are not supported.
1353 ///
1354 /// ### Example usage:
1355 /// ~~~{.cpp}
1356 /// auto d_0_30 = d.Range(0, 30); // Pick the first 30 entries
1357 /// auto d_15_end = d.Range(15, 0); // Pick all entries from 15 onwards
1358 /// auto d_15_end_3 = d.Range(15, 0, 3); // Stride: from event 15, pick an event every 3
1359 /// ~~~
1360 // clang-format on
1361 RInterface<RDFDetail::RRange<Proxied>> Range(unsigned int begin, unsigned int end, unsigned int stride = 1)
1362 {
1363 // check invariants
1364 if (stride == 0 || (end != 0 && end < begin))
1365 throw std::runtime_error("Range: stride must be strictly greater than 0 and end must be greater than begin.");
1366 CheckIMTDisabled("Range");
1367
1368 using Range_t = RDFDetail::RRange<Proxied>;
1369 auto rangePtr = std::make_shared<Range_t>(begin, end, stride, fProxiedPtr);
1370 RInterface<RDFDetail::RRange<Proxied>> newInterface(std::move(rangePtr), *fLoopManager, fColRegister);
1371 return newInterface;
1372 }
1373
1374 // clang-format off
1375 ////////////////////////////////////////////////////////////////////////////
1376 /// \brief Creates a node that filters entries based on range.
1377 /// \param[in] end Final entry number (excluded) considered for this range. 0 means that the range goes until the end of the dataset.
1378 /// \return a node of the computation graph for which the range is defined.
1379 ///
1380 /// See the other Range overload for a detailed description.
1381 // clang-format on
1382 RInterface<RDFDetail::RRange<Proxied>> Range(unsigned int end) { return Range(0, end, 1); }
1383
1384 /// \}
1385 // ---------------------------------------------------------------------------------
1386 // End of the doxygen group for Transformations
1387
1388 /// \name Actions
1389 /// Actions declare a type of result to be produced, for example histograms or summary statistics.
1390 /// Actions are lazy, i.e. they are only executed once a result is requested.
1391 /// \{
1392
1393 ////////////////////////////////////////////////////////////////////////////
1394 /// \brief Return the number of entries processed (*lazy action*).
1395 /// \return the number of entries wrapped in a RResultPtr.
1396 ///
1397 /// Useful e.g. for counting the number of entries passing a certain filter (see also `Report`).
1398 /// This action is *lazy*: upon invocation of this method the calculation is
1399 /// booked but not executed. Also see RResultPtr.
1400 ///
1401 /// ### Example usage:
1402 /// ~~~{.cpp}
1403 /// auto nEntriesAfterCuts = myFilteredDf.Count();
1404 /// ~~~
1405 ///
1407 {
1408 const auto nSlots = fLoopManager->GetNSlots();
1409 auto cSPtr = std::make_shared<ULong64_t>(0);
1410 using Helper_t = RDFInternal::CountHelper;
1412 auto action = std::make_unique<Action_t>(Helper_t(cSPtr, nSlots), ColumnNames_t({}), fProxiedPtr,
1414 return MakeResultPtr(cSPtr, *fLoopManager, std::move(action));
1415 }
1416
1417 ////////////////////////////////////////////////////////////////////////////
1418 /// \brief Return a collection of values of a column (*lazy action*, returns a std::vector by default).
1419 /// \tparam T The type of the column.
1420 /// \tparam COLL The type of collection used to store the values.
1421 /// \param[in] column The name of the column to collect the values of.
1422 /// \return the content of the selected column wrapped in a RResultPtr.
1423 ///
1424 /// The collection type to be specified for C-style array columns is `RVec<T>`:
1425 /// in this case the returned collection is a `std::vector<RVec<T>>`.
1426 /// ### Example usage:
1427 /// ~~~{.cpp}
1428 /// // In this case intCol is a std::vector<int>
1429 /// auto intCol = rdf.Take<int>("integerColumn");
1430 /// // Same content as above but in this case taken as a RVec<int>
1431 /// auto intColAsRVec = rdf.Take<int, RVec<int>>("integerColumn");
1432 /// // In this case intCol is a std::vector<RVec<int>>, a collection of collections
1433 /// auto cArrayIntCol = rdf.Take<RVec<int>>("cArrayInt");
1434 /// ~~~
1435 /// This action is *lazy*: upon invocation of this method the calculation is
1436 /// booked but not executed. Also see RResultPtr.
1437 template <typename T, typename COLL = std::vector<T>>
1438 RResultPtr<COLL> Take(std::string_view column = "")
1439 {
1440 const auto columns = column.empty() ? ColumnNames_t() : ColumnNames_t({std::string(column)});
1441
1442 const auto validColumnNames = GetValidatedColumnNames(1, columns);
1443 CheckAndFillDSColumns(validColumnNames, TTraits::TypeList<T>());
1444
1445 using Helper_t = RDFInternal::TakeHelper<T, T, COLL>;
1447 auto valuesPtr = std::make_shared<COLL>();
1448 const auto nSlots = fLoopManager->GetNSlots();
1449
1450 auto action =
1451 std::make_unique<Action_t>(Helper_t(valuesPtr, nSlots), validColumnNames, fProxiedPtr, fColRegister);
1452 return MakeResultPtr(valuesPtr, *fLoopManager, std::move(action));
1453 }
1454
1455 ////////////////////////////////////////////////////////////////////////////
1456 /// \brief Fill and return a one-dimensional histogram with the values of a column (*lazy action*).
1457 /// \tparam V The type of the column used to fill the histogram.
1458 /// \param[in] model The returned histogram will be constructed using this as a model.
1459 /// \param[in] vName The name of the column that will fill the histogram.
1460 /// \return the monodimensional histogram wrapped in a RResultPtr.
1461 ///
1462 /// Columns can be of a container type (e.g. `std::vector<double>`), in which case the histogram
1463 /// is filled with each one of the elements of the container. In case multiple columns of container type
1464 /// are provided (e.g. values and weights) they must have the same length for each one of the events (but
1465 /// possibly different lengths between events).
1466 /// This action is *lazy*: upon invocation of this method the calculation is
1467 /// booked but not executed. Also see RResultPtr.
1468 ///
1469 /// ### Example usage:
1470 /// ~~~{.cpp}
1471 /// // Deduce column type (this invocation needs jitting internally)
1472 /// auto myHist1 = myDf.Histo1D({"histName", "histTitle", 64u, 0., 128.}, "myColumn");
1473 /// // Explicit column type
1474 /// auto myHist2 = myDf.Histo1D<float>({"histName", "histTitle", 64u, 0., 128.}, "myColumn");
1475 /// ~~~
1476 ///
1477 /// \note Differently from other ROOT interfaces, the returned histogram is not associated to gDirectory
1478 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
1479 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
1480 template <typename V = RDFDetail::RInferredType>
1481 RResultPtr<::TH1D> Histo1D(const TH1DModel &model = {"", "", 128u, 0., 0.}, std::string_view vName = "")
1482 {
1483 const auto userColumns = vName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(vName)});
1484
1485 const auto validatedColumns = GetValidatedColumnNames(1, userColumns);
1486
1487 std::shared_ptr<::TH1D> h(nullptr);
1488 {
1489 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1490 h = model.GetHistogram();
1491 }
1492
1493 if (h->GetXaxis()->GetXmax() == h->GetXaxis()->GetXmin())
1494 h->SetCanExtend(::TH1::kAllAxes);
1495 return CreateAction<RDFInternal::ActionTags::Histo1D, V>(validatedColumns, h, h, fProxiedPtr);
1496 }
1497
1498 ////////////////////////////////////////////////////////////////////////////
1499 /// \brief Fill and return a one-dimensional histogram with the values of a column (*lazy action*).
1500 /// \tparam V The type of the column used to fill the histogram.
1501 /// \param[in] vName The name of the column that will fill the histogram.
1502 /// \return the monodimensional histogram wrapped in a RResultPtr.
1503 ///
1504 /// This overload uses a default model histogram TH1D(name, title, 128u, 0., 0.).
1505 /// The "name" and "title" strings are built starting from the input column name.
1506 /// See the description of the first Histo1D() overload for more details.
1507 ///
1508 /// ### Example usage:
1509 /// ~~~{.cpp}
1510 /// // Deduce column type (this invocation needs jitting internally)
1511 /// auto myHist1 = myDf.Histo1D("myColumn");
1512 /// // Explicit column type
1513 /// auto myHist2 = myDf.Histo1D<float>("myColumn");
1514 /// ~~~
1515 template <typename V = RDFDetail::RInferredType>
1516 RResultPtr<::TH1D> Histo1D(std::string_view vName)
1517 {
1518 const auto h_name = std::string(vName);
1519 const auto h_title = h_name + ";" + h_name + ";count";
1520 return Histo1D<V>({h_name.c_str(), h_title.c_str(), 128u, 0., 0.}, vName);
1521 }
1522
1523 ////////////////////////////////////////////////////////////////////////////
1524 /// \brief Fill and return a one-dimensional histogram with the weighted values of a column (*lazy action*).
1525 /// \tparam V The type of the column used to fill the histogram.
1526 /// \tparam W The type of the column used as weights.
1527 /// \param[in] model The returned histogram will be constructed using this as a model.
1528 /// \param[in] vName The name of the column that will fill the histogram.
1529 /// \param[in] wName The name of the column that will provide the weights.
1530 /// \return the monodimensional histogram wrapped in a RResultPtr.
1531 ///
1532 /// See the description of the first Histo1D() overload for more details.
1533 ///
1534 /// ### Example usage:
1535 /// ~~~{.cpp}
1536 /// // Deduce column type (this invocation needs jitting internally)
1537 /// auto myHist1 = myDf.Histo1D({"histName", "histTitle", 64u, 0., 128.}, "myValue", "myweight");
1538 /// // Explicit column type
1539 /// auto myHist2 = myDf.Histo1D<float, int>({"histName", "histTitle", 64u, 0., 128.}, "myValue", "myweight");
1540 /// ~~~
1541 template <typename V = RDFDetail::RInferredType, typename W = RDFDetail::RInferredType>
1542 RResultPtr<::TH1D> Histo1D(const TH1DModel &model, std::string_view vName, std::string_view wName)
1543 {
1544 const std::vector<std::string_view> columnViews = {vName, wName};
1545 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
1546 ? ColumnNames_t()
1547 : ColumnNames_t(columnViews.begin(), columnViews.end());
1548 std::shared_ptr<::TH1D> h(nullptr);
1549 {
1550 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1551 h = model.GetHistogram();
1552 }
1553
1554 if (h->GetXaxis()->GetXmax() == h->GetXaxis()->GetXmin())
1555 h->SetCanExtend(::TH1::kAllAxes);
1556 return CreateAction<RDFInternal::ActionTags::Histo1D, V, W>(userColumns, h, h, fProxiedPtr);
1557 }
1558
1559 ////////////////////////////////////////////////////////////////////////////
1560 /// \brief Fill and return a one-dimensional histogram with the weighted values of a column (*lazy action*).
1561 /// \tparam V The type of the column used to fill the histogram.
1562 /// \tparam W The type of the column used as weights.
1563 /// \param[in] vName The name of the column that will fill the histogram.
1564 /// \param[in] wName The name of the column that will provide the weights.
1565 /// \return the monodimensional histogram wrapped in a RResultPtr.
1566 ///
1567 /// This overload uses a default model histogram TH1D(name, title, 128u, 0., 0.).
1568 /// The "name" and "title" strings are built starting from the input column names.
1569 /// See the description of the first Histo1D() overload for more details.
1570 ///
1571 /// ### Example usage:
1572 /// ~~~{.cpp}
1573 /// // Deduce column types (this invocation needs jitting internally)
1574 /// auto myHist1 = myDf.Histo1D("myValue", "myweight");
1575 /// // Explicit column types
1576 /// auto myHist2 = myDf.Histo1D<float, int>("myValue", "myweight");
1577 /// ~~~
1578 template <typename V = RDFDetail::RInferredType, typename W = RDFDetail::RInferredType>
1579 RResultPtr<::TH1D> Histo1D(std::string_view vName, std::string_view wName)
1580 {
1581 // We build name and title based on the value and weight column names
1582 std::string str_vName{vName};
1583 std::string str_wName{wName};
1584 const auto h_name = str_vName + "_weighted_" + str_wName;
1585 const auto h_title = str_vName + ", weights: " + str_wName + ";" + str_vName + ";count * " + str_wName;
1586 return Histo1D<V, W>({h_name.c_str(), h_title.c_str(), 128u, 0., 0.}, vName, wName);
1587 }
1588
1589 ////////////////////////////////////////////////////////////////////////////
1590 /// \brief Fill and return a one-dimensional histogram with the weighted values of a column (*lazy action*).
1591 /// \tparam V The type of the column used to fill the histogram.
1592 /// \tparam W The type of the column used as weights.
1593 /// \param[in] model The returned histogram will be constructed using this as a model.
1594 /// \return the monodimensional histogram wrapped in a RResultPtr.
1595 ///
1596 /// This overload will use the first two default columns as column names.
1597 /// See the description of the first Histo1D() overload for more details.
1598 template <typename V, typename W>
1599 RResultPtr<::TH1D> Histo1D(const TH1DModel &model = {"", "", 128u, 0., 0.})
1600 {
1601 return Histo1D<V, W>(model, "", "");
1602 }
1603
1604 ////////////////////////////////////////////////////////////////////////////
1605 /// \brief Fill and return a two-dimensional histogram (*lazy action*).
1606 /// \tparam V1 The type of the column used to fill the x axis of the histogram.
1607 /// \tparam V2 The type of the column used to fill the y axis of the histogram.
1608 /// \param[in] model The returned histogram will be constructed using this as a model.
1609 /// \param[in] v1Name The name of the column that will fill the x axis.
1610 /// \param[in] v2Name The name of the column that will fill the y axis.
1611 /// \return the bidimensional histogram wrapped in a RResultPtr.
1612 ///
1613 /// Columns can be of a container type (e.g. std::vector<double>), in which case the histogram
1614 /// is filled with each one of the elements of the container. In case multiple columns of container type
1615 /// are provided (e.g. values and weights) they must have the same length for each one of the events (but
1616 /// possibly different lengths between events).
1617 /// This action is *lazy*: upon invocation of this method the calculation is
1618 /// booked but not executed. Also see RResultPtr.
1619 ///
1620 /// ### Example usage:
1621 /// ~~~{.cpp}
1622 /// // Deduce column types (this invocation needs jitting internally)
1623 /// auto myHist1 = myDf.Histo2D({"histName", "histTitle", 64u, 0., 128., 32u, -4., 4.}, "myValueX", "myValueY");
1624 /// // Explicit column types
1625 /// auto myHist2 = myDf.Histo2D<float, float>({"histName", "histTitle", 64u, 0., 128., 32u, -4., 4.}, "myValueX", "myValueY");
1626 /// ~~~
1627 ///
1628 ///
1629 /// \note Differently from other ROOT interfaces, the returned histogram is not associated to gDirectory
1630 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
1631 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
1632 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType>
1633 RResultPtr<::TH2D> Histo2D(const TH2DModel &model, std::string_view v1Name = "", std::string_view v2Name = "")
1634 {
1635 std::shared_ptr<::TH2D> h(nullptr);
1636 {
1637 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1638 h = model.GetHistogram();
1639 }
1640 if (!RDFInternal::HistoUtils<::TH2D>::HasAxisLimits(*h)) {
1641 throw std::runtime_error("2D histograms with no axes limits are not supported yet.");
1642 }
1643 const std::vector<std::string_view> columnViews = {v1Name, v2Name};
1644 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
1645 ? ColumnNames_t()
1646 : ColumnNames_t(columnViews.begin(), columnViews.end());
1647 return CreateAction<RDFInternal::ActionTags::Histo2D, V1, V2>(userColumns, h, h, fProxiedPtr);
1648 }
1649
1650 ////////////////////////////////////////////////////////////////////////////
1651 /// \brief Fill and return a weighted two-dimensional histogram (*lazy action*).
1652 /// \tparam V1 The type of the column used to fill the x axis of the histogram.
1653 /// \tparam V2 The type of the column used to fill the y axis of the histogram.
1654 /// \tparam W The type of the column used for the weights of the histogram.
1655 /// \param[in] model The returned histogram will be constructed using this as a model.
1656 /// \param[in] v1Name The name of the column that will fill the x axis.
1657 /// \param[in] v2Name The name of the column that will fill the y axis.
1658 /// \param[in] wName The name of the column that will provide the weights.
1659 /// \return the bidimensional histogram wrapped in a RResultPtr.
1660 ///
1661 /// This action is *lazy*: upon invocation of this method the calculation is
1662 /// booked but not executed. Also see RResultPtr.
1663 ///
1664 /// ### Example usage:
1665 /// ~~~{.cpp}
1666 /// // Deduce column types (this invocation needs jitting internally)
1667 /// auto myHist1 = myDf.Histo2D({"histName", "histTitle", 64u, 0., 128., 32u, -4., 4.}, "myValueX", "myValueY", "myWeight");
1668 /// // Explicit column types
1669 /// auto myHist2 = myDf.Histo2D<float, float, double>({"histName", "histTitle", 64u, 0., 128., 32u, -4., 4.}, "myValueX", "myValueY", "myWeight");
1670 /// ~~~
1671 ///
1672 /// See the documentation of the first Histo2D() overload for more details.
1673 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
1674 typename W = RDFDetail::RInferredType>
1676 Histo2D(const TH2DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view wName)
1677 {
1678 std::shared_ptr<::TH2D> h(nullptr);
1679 {
1680 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1681 h = model.GetHistogram();
1682 }
1683 if (!RDFInternal::HistoUtils<::TH2D>::HasAxisLimits(*h)) {
1684 throw std::runtime_error("2D histograms with no axes limits are not supported yet.");
1685 }
1686 const std::vector<std::string_view> columnViews = {v1Name, v2Name, wName};
1687 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
1688 ? ColumnNames_t()
1689 : ColumnNames_t(columnViews.begin(), columnViews.end());
1690 return CreateAction<RDFInternal::ActionTags::Histo2D, V1, V2, W>(userColumns, h, h, fProxiedPtr);
1691 }
1692
1693 template <typename V1, typename V2, typename W>
1695 {
1696 return Histo2D<V1, V2, W>(model, "", "", "");
1697 }
1698
1699 ////////////////////////////////////////////////////////////////////////////
1700 /// \brief Fill and return a three-dimensional histogram (*lazy action*).
1701 /// \tparam V1 The type of the column used to fill the x axis of the histogram. Inferred if not present.
1702 /// \tparam V2 The type of the column used to fill the y axis of the histogram. Inferred if not present.
1703 /// \tparam V3 The type of the column used to fill the z axis of the histogram. Inferred if not present.
1704 /// \param[in] model The returned histogram will be constructed using this as a model.
1705 /// \param[in] v1Name The name of the column that will fill the x axis.
1706 /// \param[in] v2Name The name of the column that will fill the y axis.
1707 /// \param[in] v3Name The name of the column that will fill the z axis.
1708 /// \return the tridimensional histogram wrapped in a RResultPtr.
1709 ///
1710 /// This action is *lazy*: upon invocation of this method the calculation is
1711 /// booked but not executed. Also see RResultPtr.
1712 ///
1713 /// ### Example usage:
1714 /// ~~~{.cpp}
1715 /// // Deduce column types (this invocation needs jitting internally)
1716 /// auto myHist1 = myDf.Histo3D({"name", "title", 64u, 0., 128., 32u, -4., 4., 8u, -2., 2.},
1717 /// "myValueX", "myValueY", "myValueZ");
1718 /// // Explicit column types
1719 /// auto myHist2 = myDf.Histo3D<double, double, float>({"name", "title", 64u, 0., 128., 32u, -4., 4., 8u, -2., 2.},
1720 /// "myValueX", "myValueY", "myValueZ");
1721 /// ~~~
1722 /// \note If three-dimensional histograms consume too much memory in multithreaded runs, the cloning of TH3D
1723 /// per thread can be reduced using ROOT::RDF::Experimental::ThreadsPerTH3(). See the section "Memory Usage" in
1724 /// the RDataFrame description.
1725 /// \note Differently from other ROOT interfaces, the returned histogram is not associated to gDirectory
1726 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
1727 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
1728 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
1729 typename V3 = RDFDetail::RInferredType>
1730 RResultPtr<::TH3D> Histo3D(const TH3DModel &model, std::string_view v1Name = "", std::string_view v2Name = "",
1731 std::string_view v3Name = "")
1732 {
1733 std::shared_ptr<::TH3D> h(nullptr);
1734 {
1735 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1736 h = model.GetHistogram();
1737 }
1738 if (!RDFInternal::HistoUtils<::TH3D>::HasAxisLimits(*h)) {
1739 throw std::runtime_error("3D histograms with no axes limits are not supported yet.");
1740 }
1741 const std::vector<std::string_view> columnViews = {v1Name, v2Name, v3Name};
1742 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
1743 ? ColumnNames_t()
1744 : ColumnNames_t(columnViews.begin(), columnViews.end());
1745 return CreateAction<RDFInternal::ActionTags::Histo3D, V1, V2, V3>(userColumns, h, h, fProxiedPtr);
1746 }
1747
1748 ////////////////////////////////////////////////////////////////////////////
1749 /// \brief Fill and return a three-dimensional histogram (*lazy action*).
1750 /// \tparam V1 The type of the column used to fill the x axis of the histogram. Inferred if not present.
1751 /// \tparam V2 The type of the column used to fill the y axis of the histogram. Inferred if not present.
1752 /// \tparam V3 The type of the column used to fill the z axis of the histogram. Inferred if not present.
1753 /// \tparam W The type of the column used for the weights of the histogram. Inferred if not present.
1754 /// \param[in] model The returned histogram will be constructed using this as a model.
1755 /// \param[in] v1Name The name of the column that will fill the x axis.
1756 /// \param[in] v2Name The name of the column that will fill the y axis.
1757 /// \param[in] v3Name The name of the column that will fill the z axis.
1758 /// \param[in] wName The name of the column that will provide the weights.
1759 /// \return the tridimensional histogram wrapped in a RResultPtr.
1760 ///
1761 /// This action is *lazy*: upon invocation of this method the calculation is
1762 /// booked but not executed. Also see RResultPtr.
1763 ///
1764 /// ### Example usage:
1765 /// ~~~{.cpp}
1766 /// // Deduce column types (this invocation needs jitting internally)
1767 /// auto myHist1 = myDf.Histo3D({"name", "title", 64u, 0., 128., 32u, -4., 4., 8u, -2., 2.},
1768 /// "myValueX", "myValueY", "myValueZ", "myWeight");
1769 /// // Explicit column types
1770 /// using d_t = double;
1771 /// auto myHist2 = myDf.Histo3D<d_t, d_t, float, d_t>({"name", "title", 64u, 0., 128., 32u, -4., 4., 8u, -2., 2.},
1772 /// "myValueX", "myValueY", "myValueZ", "myWeight");
1773 /// ~~~
1774 ///
1775 ///
1776 /// See the documentation of the first Histo2D() overload for more details.
1777 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
1778 typename V3 = RDFDetail::RInferredType, typename W = RDFDetail::RInferredType>
1779 RResultPtr<::TH3D> Histo3D(const TH3DModel &model, std::string_view v1Name, std::string_view v2Name,
1780 std::string_view v3Name, std::string_view wName)
1781 {
1782 std::shared_ptr<::TH3D> h(nullptr);
1783 {
1784 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1785 h = model.GetHistogram();
1786 }
1787 if (!RDFInternal::HistoUtils<::TH3D>::HasAxisLimits(*h)) {
1788 throw std::runtime_error("3D histograms with no axes limits are not supported yet.");
1789 }
1790 const std::vector<std::string_view> columnViews = {v1Name, v2Name, v3Name, wName};
1791 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
1792 ? ColumnNames_t()
1793 : ColumnNames_t(columnViews.begin(), columnViews.end());
1794 return CreateAction<RDFInternal::ActionTags::Histo3D, V1, V2, V3, W>(userColumns, h, h, fProxiedPtr);
1795 }
1796
1797 template <typename V1, typename V2, typename V3, typename W>
1799 {
1800 return Histo3D<V1, V2, V3, W>(model, "", "", "", "");
1801 }
1802
1803 ////////////////////////////////////////////////////////////////////////////
1804 /// \brief Fill and return an N-dimensional histogram (*lazy action*).
1805 /// \tparam FirstColumn The first type of the column the values of which are used to fill the object. Inferred if not
1806 /// present.
1807 /// \tparam OtherColumns A list of the other types of the columns the values of which are used to fill the
1808 /// object.
1809 /// \param[in] model The returned histogram will be constructed using this as a model.
1810 /// \param[in] columnList
1811 /// A list containing the names of the columns that will be passed when calling `Fill`.
1812 /// \param[in] wName The name of the column that will provide the weights.
1813 /// \return the N-dimensional histogram wrapped in a RResultPtr.
1814 ///
1815 /// This action is *lazy*: upon invocation of this method the calculation is
1816 /// booked but not executed. See RResultPtr documentation.
1817 ///
1818 /// ### Example usage:
1819 /// ~~~{.cpp}
1820 /// auto myFilledObj = myDf.HistoND<float, float, float, float>({"name","title", 4,
1821 /// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
1822 /// {"col0", "col1", "col2", "col3"});
1823 /// ~~~
1824 ///
1825 /// \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
1826 /// argument `wName`: `HistoND(model, cols, weightCol)`.
1827 ///
1828 template <typename FirstColumn, typename... OtherColumns> // need FirstColumn to disambiguate overloads
1829 RResultPtr<::THnD> HistoND(const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName = "")
1830 {
1831 std::shared_ptr<::THnD> h(nullptr);
1832 {
1833 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1834 h = model.GetHistogram();
1835 const auto hDims = h->GetNdimensions();
1836 decltype(hDims) nCols = columnList.size();
1837
1838 if (!wName.empty() && nCols == hDims + 1)
1839 throw std::invalid_argument("The weight column was passed as an argument and at the same time the list of "
1840 "input columns contains one column more than the number of dimensions of the "
1841 "histogram. Call as 'HistoND(model, cols, weightCol)'.");
1842
1843 if (nCols == hDims + 1)
1844 Warning("HistoND", "Passing the column with the weights as the last column in the list is deprecated. "
1845 "Instead, pass it as a separate argument, e.g. 'HistoND(model, cols, weightCol)'.");
1846
1847 if (!wName.empty() || nCols == hDims + 1)
1848 h->Sumw2();
1849
1850 if (nCols != hDims + 1 && nCols != hDims)
1851 throw std::invalid_argument("Wrong number of columns for the specified number of histogram axes.");
1852 }
1853
1854 if (!wName.empty()) {
1855 // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
1856 // passed arguments is one more the number of dimensions of the histogram.
1857 ColumnNames_t userColumns = columnList;
1858 userColumns.push_back(std::string{wName});
1859 return CreateAction<RDFInternal::ActionTags::HistoND, FirstColumn, OtherColumns...>(userColumns, h, h,
1860 fProxiedPtr);
1861 }
1862 return CreateAction<RDFInternal::ActionTags::HistoND, FirstColumn, OtherColumns...>(columnList, h, h,
1863 fProxiedPtr);
1864 }
1865
1866 ////////////////////////////////////////////////////////////////////////////
1867 /// \brief Fill and return an N-dimensional histogram (*lazy action*).
1868 /// \param[in] model The returned histogram will be constructed using this as a model.
1869 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
1870 /// \param[in] wName The name of the column that will provide the weights.
1871 /// \return the N-dimensional histogram wrapped in a RResultPtr.
1872 ///
1873 /// This action is *lazy*: upon invocation of this method the calculation is
1874 /// booked but not executed. Also see RResultPtr.
1875 ///
1876 /// ### Example usage:
1877 /// ~~~{.cpp}
1878 /// auto myFilledObj = myDf.HistoND({"name","title", 4,
1879 /// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
1880 /// {"col0", "col1", "col2", "col3"});
1881 /// ~~~
1882 ///
1883 /// \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
1884 /// argument `wName`: `HistoND(model, cols, weightCol)`.
1885 ///
1886 RResultPtr<::THnD> HistoND(const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName = "")
1887 {
1888 std::shared_ptr<::THnD> h(nullptr);
1889 {
1890 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1891 h = model.GetHistogram();
1892 const auto hDims = h->GetNdimensions();
1893 decltype(hDims) nCols = columnList.size();
1894
1895 if (!wName.empty() && nCols == hDims + 1)
1896 throw std::invalid_argument("The weight column was passed as an argument and at the same time the list of "
1897 "input columns contains one column more than the number of dimensions of the "
1898 "histogram. Call as 'HistoND(model, cols, weightCol)'.");
1899
1900 if (nCols == hDims + 1)
1901 Warning("HistoND", "Passing the column with the weights as the last column in the list is deprecated. "
1902 "Instead, pass it as a separate argument, e.g. 'HistoND(model, cols, weightCol)'.");
1903
1904 if (!wName.empty() || nCols == hDims + 1)
1905 h->Sumw2();
1906
1907 if (nCols != hDims + 1 && nCols != hDims)
1908 throw std::invalid_argument("Wrong number of columns for the specified number of histogram axes.");
1909 }
1910
1911 if (!wName.empty()) {
1912 // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
1913 // passed arguments is one more the number of dimensions of the histogram.
1914 ColumnNames_t userColumns = columnList;
1915 userColumns.push_back(std::string{wName});
1916 return CreateAction<RDFInternal::ActionTags::HistoND, RDFDetail::RInferredType>(userColumns, h, h, fProxiedPtr,
1917 userColumns.size());
1918 }
1919 return CreateAction<RDFInternal::ActionTags::HistoND, RDFDetail::RInferredType>(columnList, h, h, fProxiedPtr,
1920 columnList.size());
1921 }
1922
1923 ////////////////////////////////////////////////////////////////////////////
1924 /// \brief Fill and return a sparse N-dimensional histogram (*lazy action*).
1925 /// \tparam FirstColumn The first type of the column the values of which are used to fill the object. Inferred if not
1926 /// present.
1927 /// \tparam OtherColumns A list of the other types of the columns the values of which are used to fill the
1928 /// object.
1929 /// \param[in] model The returned histogram will be constructed using this as a model.
1930 /// \param[in] columnList
1931 /// A list containing the names of the columns that will be passed when calling `Fill`.
1932 /// \param[in] wName The name of the column that will provide the weights.
1933 /// \return the N-dimensional histogram wrapped in a RResultPtr.
1934 ///
1935 /// This action is *lazy*: upon invocation of this method the calculation is
1936 /// booked but not executed. See RResultPtr documentation.
1937 ///
1938 /// ### Example usage:
1939 /// ~~~{.cpp}
1940 /// auto myFilledObj = myDf.HistoNSparseD<float, float, float, float>({"name","title", 4,
1941 /// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
1942 /// {"col0", "col1", "col2", "col3"});
1943 /// ~~~
1944 ///
1945 /// \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
1946 /// argument `wName`: `HistoND(model, cols, weightCol)`.
1947 ///
1948 template <typename FirstColumn, typename... OtherColumns> // need FirstColumn to disambiguate overloads
1950 HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName = "")
1951 {
1952 std::shared_ptr<::THnSparseD> h(nullptr);
1953 {
1954 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
1955 h = model.GetHistogram();
1956 const auto hDims = h->GetNdimensions();
1957 decltype(hDims) nCols = columnList.size();
1958
1959 if (!wName.empty() && nCols == hDims + 1)
1960 throw std::invalid_argument("The weight column was passed as an argument and at the same time the list of "
1961 "input columns contains one column more than the number of dimensions of the "
1962 "histogram. Call as 'HistoNSparseD(model, cols, weightCol)'.");
1963
1964 if (nCols == hDims + 1)
1965 Warning("HistoNSparseD",
1966 "Passing the column with the weights as the last column in the list is deprecated. "
1967 "Instead, pass it as a separate argument, e.g. 'HistoNSparseD(model, cols, weightCol)'.");
1968
1969 if (!wName.empty() || nCols == hDims + 1)
1970 h->Sumw2();
1971
1972 if (nCols != hDims + 1 && nCols != hDims)
1973 throw std::invalid_argument("Wrong number of columns for the specified number of histogram axes.");
1974 }
1975
1976 if (!wName.empty()) {
1977 // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
1978 // passed arguments is one more the number of dimensions of the histogram.
1979 ColumnNames_t userColumns = columnList;
1980 userColumns.push_back(std::string{wName});
1981 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, FirstColumn, OtherColumns...>(userColumns, h, h,
1982 fProxiedPtr);
1983 }
1984 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, FirstColumn, OtherColumns...>(columnList, h, h,
1985 fProxiedPtr);
1986 }
1987
1988 ////////////////////////////////////////////////////////////////////////////
1989 /// \brief Fill and return a sparse N-dimensional histogram (*lazy action*).
1990 /// \param[in] model The returned histogram will be constructed using this as a model.
1991 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
1992 /// \param[in] wName The name of the column that will provide the weights.
1993 /// \return the N-dimensional histogram wrapped in a RResultPtr.
1994 ///
1995 /// This action is *lazy*: upon invocation of this method the calculation is
1996 /// booked but not executed. Also see RResultPtr.
1997 ///
1998 /// ### Example usage:
1999 /// ~~~{.cpp}
2000 /// auto myFilledObj = myDf.HistoNSparseD({"name","title", 4,
2001 /// {40,40,40,40}, {20.,20.,20.,20.}, {60.,60.,60.,60.}},
2002 /// {"col0", "col1", "col2", "col3"});
2003 /// ~~~
2004 ///
2005 /// \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
2006 /// argument `wName`: `HistoND(model, cols, weightCol)`.
2007 ///
2009 HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName = "")
2010 {
2011 std::shared_ptr<::THnSparseD> h(nullptr);
2012 {
2013 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2014 h = model.GetHistogram();
2015 const auto hDims = h->GetNdimensions();
2016 decltype(hDims) nCols = columnList.size();
2017
2018 if (!wName.empty() && nCols == hDims + 1)
2019 throw std::invalid_argument("The weight column was passed as an argument and at the same time the list of "
2020 "input columns contains one column more than the number of dimensions of the "
2021 "histogram. Call as 'HistoNSparseD(model, cols, weightCol)'.");
2022
2023 if (nCols == hDims + 1)
2024 Warning("HistoNSparseD",
2025 "Passing the column with the weights as the last column in the list is deprecated. "
2026 "Instead, pass it as a separate argument, e.g. 'HistoNSparseD(model, cols, weightCol)'.");
2027
2028 if (!wName.empty() || nCols == hDims + 1)
2029 h->Sumw2();
2030
2031 if (nCols != hDims + 1 && nCols != hDims)
2032 throw std::invalid_argument("Wrong number of columns for the specified number of histogram axes.");
2033 }
2034
2035 if (!wName.empty()) {
2036 // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
2037 // passed arguments is one more the number of dimensions of the histogram.
2038 ColumnNames_t userColumns = columnList;
2039 userColumns.push_back(std::string{wName});
2040 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, RDFDetail::RInferredType>(
2041 userColumns, h, h, fProxiedPtr, userColumns.size());
2042 }
2043 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, RDFDetail::RInferredType>(
2044 columnList, h, h, fProxiedPtr, columnList.size());
2045 }
2046
2047#ifdef R__HAS_ROOT7
2048 ////////////////////////////////////////////////////////////////////////////
2049 /// \brief Fill and return a one-dimensional RHist (*lazy action*).
2050 /// \tparam BinContentType The bin content type of the returned RHist.
2051 /// \param[in] nNormalBins The returned histogram will be constructed using this number of normal bins.
2052 /// \param[in] interval The axis interval of the constructed histogram (lower end inclusive, upper end exclusive).
2053 /// \param[in] vName The name of the column that will fill the histogram.
2054 /// \return the histogram wrapped in a RResultPtr.
2055 ///
2056 /// The column can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2057 /// each one of the elements of the container, and the container can also be nested.
2058 ///
2059 /// This action is *lazy*: upon invocation of this method the calculation is
2060 /// booked but not executed. Also see RResultPtr.
2061 ///
2062 /// ### Example usage:
2063 /// ~~~{.cpp}
2064 /// auto myHist = myDf.Hist(10, {5, 15}, "col0");
2065 /// ~~~
2066 template <typename BinContentType = double, typename V = RDFDetail::RInferredType>
2068 Hist(std::uint64_t nNormalBins, std::pair<double, double> interval, std::string_view vName)
2069 {
2070 std::shared_ptr h = std::make_shared<ROOT::Experimental::RHist<BinContentType>>(nNormalBins, interval);
2071
2072 const ColumnNames_t columnList = {std::string(vName)};
2073
2074 return Hist<V>(h, columnList);
2075 }
2076
2077 ////////////////////////////////////////////////////////////////////////////
2078 /// \brief Fill and return an RHist (*lazy action*).
2079 /// \tparam BinContentType The bin content type of the returned RHist.
2080 /// \param[in] axes The returned histogram will be constructed using these axes.
2081 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2082 /// \return the histogram wrapped in a RResultPtr.
2083 ///
2084 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2085 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2086 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2087 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2088 ///
2089 /// This action is *lazy*: upon invocation of this method the calculation is
2090 /// booked but not executed. Also see RResultPtr.
2091 ///
2092 /// ### Example usage:
2093 /// ~~~{.cpp}
2094 /// ROOT::Experimental::RRegularAxis axis(10, {5.0, 15.0});
2095 /// auto myHist = myDf.Hist({axis}, {"col0"});
2096 /// ~~~
2097 template <typename BinContentType = double, typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes>
2099 Hist(std::vector<ROOT::Experimental::RAxisVariant> axes, const ColumnNames_t &columnList)
2100 {
2101 if (axes.size() != columnList.size()) {
2102 std::string msg = "Wrong number of columns for the specified number of histogram axes: ";
2103 msg += "expected " + std::to_string(axes.size()) + ", got " + std::to_string(columnList.size());
2104 throw std::invalid_argument(msg);
2105 }
2106
2107 std::shared_ptr h = std::make_shared<ROOT::Experimental::RHist<BinContentType>>(std::move(axes));
2108
2109 return Hist<ColumnType, ColumnTypes...>(h, columnList);
2110 }
2111
2112 ////////////////////////////////////////////////////////////////////////////
2113 /// \brief Fill the provided RHist (*lazy action*).
2114 /// \param[in] h The histogram that should be filled.
2115 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2116 /// \return the histogram wrapped in a RResultPtr.
2117 ///
2118 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2119 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2120 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2121 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2122 ///
2123 /// This action is *lazy*: upon invocation of this method the calculation is
2124 /// booked but not executed. Also see RResultPtr.
2125 ///
2126 /// During execution of the computation graph, the passed histogram must only be accessed with methods that are
2127 /// allowed during concurrent filling.
2128 ///
2129 /// ### Example usage:
2130 /// ~~~{.cpp}
2131 /// auto h = std::make_shared<ROOT::Experimental::RHist<double>>(10, {5.0, 15.0});
2132 /// auto myHist = myDf.Hist(h, {"col0"});
2133 /// ~~~
2134 template <typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes, typename BinContentType>
2137 {
2139
2140 if (h->GetNDimensions() != columnList.size()) {
2141 std::string msg = "Wrong number of columns for the passed histogram: ";
2142 msg += "expected " + std::to_string(h->GetNDimensions()) + ", got " + std::to_string(columnList.size());
2143 throw std::invalid_argument(msg);
2144 }
2145
2146 return CreateAction<RDFInternal::ActionTags::Hist, ColumnType, ColumnTypes...>(columnList, h, h, fProxiedPtr,
2147 columnList.size());
2148 }
2149
2150 ////////////////////////////////////////////////////////////////////////////
2151 /// \brief Fill and return a one-dimensional RHist with weights (*lazy action*).
2152 /// \tparam BinContentType The bin content type of the returned RHist.
2153 /// \param[in] nNormalBins The returned histogram will be constructed using this number of normal bins.
2154 /// \param[in] interval The axis interval of the constructed histogram (lower end inclusive, upper end exclusive).
2155 /// \param[in] vName The name of the column that will fill the histogram.
2156 /// \param[in] wName The name of the column that will provide the weights.
2157 /// \return the histogram wrapped in a RResultPtr.
2158 ///
2159 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2160 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2161 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2162 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2163 ///
2164 /// This action is *lazy*: upon invocation of this method the calculation is
2165 /// booked but not executed. Also see RResultPtr.
2166 ///
2167 /// ### Example usage:
2168 /// ~~~{.cpp}
2169 /// auto myHist = myDf.Hist(10, {5, 15}, "col0", "colW");
2170 /// ~~~
2171 template <typename BinContentType = ROOT::Experimental::RBinWithError, typename V = RDFDetail::RInferredType,
2172 typename W = RDFDetail::RInferredType>
2174 Hist(std::uint64_t nNormalBins, std::pair<double, double> interval, std::string_view vName, std::string_view wName)
2175 {
2176 std::shared_ptr h = std::make_shared<ROOT::Experimental::RHist<BinContentType>>(nNormalBins, interval);
2177
2178 const ColumnNames_t columnList = {std::string(vName)};
2179
2180 return Hist<V, W>(h, columnList, wName);
2181 }
2182
2183 ////////////////////////////////////////////////////////////////////////////
2184 /// \brief Fill and return an RHist with weights (*lazy action*).
2185 /// \tparam BinContentType The bin content type of the returned RHist.
2186 /// \param[in] axes The returned histogram will be constructed using these axes.
2187 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2188 /// \param[in] wName The name of the column that will provide the weights.
2189 /// \return the histogram wrapped in a RResultPtr.
2190 ///
2191 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2192 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2193 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2194 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2195 ///
2196 /// This action is *lazy*: upon invocation of this method the calculation is
2197 /// booked but not executed. Also see RResultPtr.
2198 ///
2199 /// This overload is not available for integral bin content types (see \ref RHistEngine::SupportsWeightedFilling).
2200 ///
2201 /// ### Example usage:
2202 /// ~~~{.cpp}
2203 /// ROOT::Experimental::RRegularAxis axis(10, {5.0, 15.0});
2204 /// auto myHist = myDf.Hist({axis}, {"col0"}, "colW");
2205 /// ~~~
2206 template <typename BinContentType = ROOT::Experimental::RBinWithError,
2207 typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes>
2209 Hist(std::vector<ROOT::Experimental::RAxisVariant> axes, const ColumnNames_t &columnList, std::string_view wName)
2210 {
2212 "weighted filling is not supported for integral bin content types");
2213
2214 if (axes.size() != columnList.size()) {
2215 std::string msg = "Wrong number of columns for the specified number of histogram axes: ";
2216 msg += "expected " + std::to_string(axes.size()) + ", got " + std::to_string(columnList.size());
2217 throw std::invalid_argument(msg);
2218 }
2219
2220 std::shared_ptr h = std::make_shared<ROOT::Experimental::RHist<BinContentType>>(std::move(axes));
2221
2222 return Hist<ColumnType, ColumnTypes...>(h, columnList, wName);
2223 }
2224
2225 ////////////////////////////////////////////////////////////////////////////
2226 /// \brief Fill the provided RHist with weights (*lazy action*).
2227 /// \param[in] h The histogram that should be filled.
2228 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2229 /// \param[in] wName The name of the column that will provide the weights.
2230 /// \return the histogram wrapped in a RResultPtr.
2231 ///
2232 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2233 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2234 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2235 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2236 ///
2237 /// This action is *lazy*: upon invocation of this method the calculation is
2238 /// booked but not executed. Also see RResultPtr.
2239 ///
2240 /// This overload is not available for integral bin content types (see \ref RHistEngine::SupportsWeightedFilling).
2241 ///
2242 /// During execution of the computation graph, the passed histogram must only be accessed with methods that are
2243 /// allowed during concurrent filling.
2244 ///
2245 /// ### Example usage:
2246 /// ~~~{.cpp}
2247 /// auto h = std::make_shared<ROOT::Experimental::RHist<double>>(10, {5.0, 15.0});
2248 /// auto myHist = myDf.Hist(h, {"col0"}, "colW");
2249 /// ~~~
2250 template <typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes, typename BinContentType>
2253 std::string_view wName)
2254 {
2256 "weighted filling is not supported for integral bin content types");
2257
2259
2260 if (h->GetNDimensions() != columnList.size()) {
2261 std::string msg = "Wrong number of columns for the passed histogram: ";
2262 msg += "expected " + std::to_string(h->GetNDimensions()) + ", got " + std::to_string(columnList.size());
2263 throw std::invalid_argument(msg);
2264 }
2265
2266 // Add the weight column to the list of argument columns to pass it through the infrastructure.
2267 ColumnNames_t columnListWithWeights(columnList);
2268 columnListWithWeights.push_back(std::string(wName));
2269
2270 return CreateAction<RDFInternal::ActionTags::HistWithWeight, ColumnType, ColumnTypes...>(
2271 columnListWithWeights, h, h, fProxiedPtr, columnListWithWeights.size());
2272 }
2273
2274 ////////////////////////////////////////////////////////////////////////////
2275 /// \brief Fill the provided RHistEngine (*lazy action*).
2276 /// \param[in] h The histogram that should be filled.
2277 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2278 /// \return the histogram wrapped in a RResultPtr.
2279 ///
2280 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2281 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2282 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2283 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2284 ///
2285 /// This action is *lazy*: upon invocation of this method the calculation is
2286 /// booked but not executed. Also see RResultPtr.
2287 ///
2288 /// During execution of the computation graph, the passed histogram must only be accessed with methods that are
2289 /// allowed during concurrent filling.
2290 ///
2291 /// ### Example usage:
2292 /// ~~~{.cpp}
2293 /// auto h = std::make_shared<ROOT::Experimental::RHistEngine<double>>(10, {5.0, 15.0});
2294 /// auto myHist = myDf.Hist(h, {"col0"});
2295 /// ~~~
2296 template <typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes, typename BinContentType>
2299 {
2301
2302 if (h->GetNDimensions() != columnList.size()) {
2303 std::string msg = "Wrong number of columns for the passed histogram: ";
2304 msg += "expected " + std::to_string(h->GetNDimensions()) + ", got " + std::to_string(columnList.size());
2305 throw std::invalid_argument(msg);
2306 }
2307
2308 return CreateAction<RDFInternal::ActionTags::Hist, ColumnType, ColumnTypes...>(columnList, h, h, fProxiedPtr,
2309 columnList.size());
2310 }
2311
2312 ////////////////////////////////////////////////////////////////////////////
2313 /// \brief Fill the provided RHistEngine with weights (*lazy action*).
2314 /// \param[in] h The histogram that should be filled.
2315 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2316 /// \param[in] wName The name of the column that will provide the weights.
2317 /// \return the histogram wrapped in a RResultPtr.
2318 ///
2319 /// Columns can be of a container type (e.g. `std::vector` or `RVec`), in which case the histogram is filled with
2320 /// each one of the elements of the container. In case multiple columns of container type are provided, they must
2321 /// have the same length for each event (but possibly different lengths between events). Containers can be nested,
2322 /// in which case their sizes must match recursively. Scalars are broadcasted to match container columns.
2323 ///
2324 /// This action is *lazy*: upon invocation of this method the calculation is
2325 /// booked but not executed. Also see RResultPtr.
2326 ///
2327 /// This overload is not available for integral bin content types (see \ref RHistEngine::SupportsWeightedFilling).
2328 ///
2329 /// During execution of the computation graph, the passed histogram must only be accessed with methods that are
2330 /// allowed during concurrent filling.
2331 ///
2332 /// ### Example usage:
2333 /// ~~~{.cpp}
2334 /// auto h = std::make_shared<ROOT::Experimental::RHistEngine<double>>(10, {5.0, 15.0});
2335 /// auto myHist = myDf.Hist(h, {"col0"}, "colW");
2336 /// ~~~
2337 template <typename ColumnType = RDFDetail::RInferredType, typename... ColumnTypes, typename BinContentType>
2340 std::string_view wName)
2341 {
2343 "weighted filling is not supported for integral bin content types");
2344
2346
2347 if (h->GetNDimensions() != columnList.size()) {
2348 std::string msg = "Wrong number of columns for the passed histogram: ";
2349 msg += "expected " + std::to_string(h->GetNDimensions()) + ", got " + std::to_string(columnList.size());
2350 throw std::invalid_argument(msg);
2351 }
2352
2353 // Add the weight column to the list of argument columns to pass it through the infrastructure.
2354 ColumnNames_t columnListWithWeights(columnList);
2355 columnListWithWeights.push_back(std::string(wName));
2356
2357 return CreateAction<RDFInternal::ActionTags::HistWithWeight, ColumnType, ColumnTypes...>(
2358 columnListWithWeights, h, h, fProxiedPtr, columnListWithWeights.size());
2359 }
2360#endif
2361
2362 ////////////////////////////////////////////////////////////////////////////
2363 /// \brief Fill and return a TGraph object (*lazy action*).
2364 /// \tparam X The type of the column used to fill the x axis.
2365 /// \tparam Y The type of the column used to fill the y axis.
2366 /// \param[in] x The name of the column that will fill the x axis.
2367 /// \param[in] y The name of the column that will fill the y axis.
2368 /// \return the TGraph wrapped in a RResultPtr.
2369 ///
2370 /// Columns can be of a container type (e.g. std::vector<double>), in which case the TGraph
2371 /// is filled with each one of the elements of the container.
2372 /// If Multithreading is enabled, the order in which points are inserted is undefined.
2373 /// If the Graph has to be drawn, it is suggested to the user to sort it on the x before printing.
2374 /// A name and a title to the TGraph is given based on the input column names.
2375 ///
2376 /// This action is *lazy*: upon invocation of this method the calculation is
2377 /// booked but not executed. Also see RResultPtr.
2378 ///
2379 /// ### Example usage:
2380 /// ~~~{.cpp}
2381 /// // Deduce column types (this invocation needs jitting internally)
2382 /// auto myGraph1 = myDf.Graph("xValues", "yValues");
2383 /// // Explicit column types
2384 /// auto myGraph2 = myDf.Graph<int, float>("xValues", "yValues");
2385 /// ~~~
2386 ///
2387 /// \note Differently from other ROOT interfaces, the returned TGraph is not associated to gDirectory
2388 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
2389 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
2390 template <typename X = RDFDetail::RInferredType, typename Y = RDFDetail::RInferredType>
2391 RResultPtr<::TGraph> Graph(std::string_view x = "", std::string_view y = "")
2392 {
2393 auto graph = std::make_shared<::TGraph>();
2394 const std::vector<std::string_view> columnViews = {x, y};
2395 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2396 ? ColumnNames_t()
2397 : ColumnNames_t(columnViews.begin(), columnViews.end());
2398
2399 const auto validatedColumns = GetValidatedColumnNames(2, userColumns);
2400
2401 // We build a default name and title based on the input columns
2402 const auto g_name = validatedColumns[1] + "_vs_" + validatedColumns[0];
2403 const auto g_title = validatedColumns[1] + " vs " + validatedColumns[0];
2404 graph->SetNameTitle(g_name.c_str(), g_title.c_str());
2405 graph->GetXaxis()->SetTitle(validatedColumns[0].c_str());
2406 graph->GetYaxis()->SetTitle(validatedColumns[1].c_str());
2407
2408 return CreateAction<RDFInternal::ActionTags::Graph, X, Y>(validatedColumns, graph, graph, fProxiedPtr);
2409 }
2410
2411 ////////////////////////////////////////////////////////////////////////////
2412 /// \brief Fill and return a TGraphAsymmErrors object (*lazy action*).
2413 /// \param[in] x The name of the column that will fill the x axis.
2414 /// \param[in] y The name of the column that will fill the y axis.
2415 /// \param[in] exl The name of the column of X low errors
2416 /// \param[in] exh The name of the column of X high errors
2417 /// \param[in] eyl The name of the column of Y low errors
2418 /// \param[in] eyh The name of the column of Y high errors
2419 /// \return the TGraphAsymmErrors wrapped in a RResultPtr.
2420 ///
2421 /// Columns can be of a container type (e.g. std::vector<double>), in which case the graph
2422 /// is filled with each one of the elements of the container.
2423 /// If Multithreading is enabled, the order in which points are inserted is undefined.
2424 ///
2425 /// This action is *lazy*: upon invocation of this method the calculation is
2426 /// booked but not executed. Also see RResultPtr.
2427 ///
2428 /// ### Example usage:
2429 /// ~~~{.cpp}
2430 /// // Deduce column types (this invocation needs jitting internally)
2431 /// auto myGAE1 = myDf.GraphAsymmErrors("xValues", "yValues", "exl", "exh", "eyl", "eyh");
2432 /// // Explicit column types
2433 /// using f = float
2434 /// auto myGAE2 = myDf.GraphAsymmErrors<f, f, f, f, f, f>("xValues", "yValues", "exl", "exh", "eyl", "eyh");
2435 /// ~~~
2436 ///
2437 /// `GraphAsymmErrors` should also be used for the cases in which values associated only with
2438 /// one of the axes have associated errors. For example, only `ey` exist and `ex` are equal to zero.
2439 /// In such cases, user should do the following:
2440 /// ~~~{.cpp}
2441 /// // Create a column of zeros in RDataFrame
2442 /// auto rdf_withzeros = rdf.Define("zero", "0");
2443 /// // or alternatively:
2444 /// auto rdf_withzeros = rdf.Define("zero", []() -> double { return 0.;});
2445 /// // Create the graph with y errors only
2446 /// auto rdf_errorsOnYOnly = rdf_withzeros.GraphAsymmErrors("xValues", "yValues", "zero", "zero", "eyl", "eyh");
2447 /// ~~~
2448 ///
2449 /// \note Differently from other ROOT interfaces, the returned TGraphAsymmErrors is not associated to gDirectory
2450 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
2451 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
2452 template <typename X = RDFDetail::RInferredType, typename Y = RDFDetail::RInferredType,
2453 typename EXL = RDFDetail::RInferredType, typename EXH = RDFDetail::RInferredType,
2454 typename EYL = RDFDetail::RInferredType, typename EYH = RDFDetail::RInferredType>
2456 GraphAsymmErrors(std::string_view x = "", std::string_view y = "", std::string_view exl = "",
2457 std::string_view exh = "", std::string_view eyl = "", std::string_view eyh = "")
2458 {
2459 auto graph = std::make_shared<::TGraphAsymmErrors>();
2460 const std::vector<std::string_view> columnViews = {x, y, exl, exh, eyl, eyh};
2461 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2462 ? ColumnNames_t()
2463 : ColumnNames_t(columnViews.begin(), columnViews.end());
2464
2465 const auto validatedColumns = GetValidatedColumnNames(6, userColumns);
2466
2467 // We build a default name and title based on the input columns
2468 const auto g_name = validatedColumns[1] + "_vs_" + validatedColumns[0];
2469 const auto g_title = validatedColumns[1] + " vs " + validatedColumns[0];
2470 graph->SetNameTitle(g_name.c_str(), g_title.c_str());
2471 graph->GetXaxis()->SetTitle(validatedColumns[0].c_str());
2472 graph->GetYaxis()->SetTitle(validatedColumns[1].c_str());
2473
2474 return CreateAction<RDFInternal::ActionTags::GraphAsymmErrors, X, Y, EXL, EXH, EYL, EYH>(validatedColumns, graph,
2475 graph, fProxiedPtr);
2476 }
2477
2478 ////////////////////////////////////////////////////////////////////////////
2479 /// \brief Fill and return a one-dimensional profile (*lazy action*).
2480 /// \tparam V1 The type of the column the values of which are used to fill the profile. Inferred if not present.
2481 /// \tparam V2 The type of the column the values of which are used to fill the profile. Inferred if not present.
2482 /// \param[in] model The model to be considered to build the new return value.
2483 /// \param[in] v1Name The name of the column that will fill the x axis.
2484 /// \param[in] v2Name The name of the column that will fill the y axis.
2485 /// \return the monodimensional profile wrapped in a RResultPtr.
2486 ///
2487 /// This action is *lazy*: upon invocation of this method the calculation is
2488 /// booked but not executed. Also see RResultPtr.
2489 ///
2490 /// ### Example usage:
2491 /// ~~~{.cpp}
2492 /// // Deduce column types (this invocation needs jitting internally)
2493 /// auto myProf1 = myDf.Profile1D({"profName", "profTitle", 64u, -4., 4.}, "xValues", "yValues");
2494 /// // Explicit column types
2495 /// auto myProf2 = myDf.Graph<int, float>({"profName", "profTitle", 64u, -4., 4.}, "xValues", "yValues");
2496 /// ~~~
2497 ///
2498 /// \note Differently from other ROOT interfaces, the returned profile is not associated to gDirectory
2499 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
2500 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
2501 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType>
2503 Profile1D(const TProfile1DModel &model, std::string_view v1Name = "", std::string_view v2Name = "")
2504 {
2505 std::shared_ptr<::TProfile> h(nullptr);
2506 {
2507 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2508 h = model.GetProfile();
2509 }
2510
2511 if (!RDFInternal::HistoUtils<::TProfile>::HasAxisLimits(*h)) {
2512 throw std::runtime_error("Profiles with no axes limits are not supported yet.");
2513 }
2514 const std::vector<std::string_view> columnViews = {v1Name, v2Name};
2515 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2516 ? ColumnNames_t()
2517 : ColumnNames_t(columnViews.begin(), columnViews.end());
2518 return CreateAction<RDFInternal::ActionTags::Profile1D, V1, V2>(userColumns, h, h, fProxiedPtr);
2519 }
2520
2521 ////////////////////////////////////////////////////////////////////////////
2522 /// \brief Fill and return a one-dimensional profile (*lazy action*).
2523 /// \tparam V1 The type of the column the values of which are used to fill the profile. Inferred if not present.
2524 /// \tparam V2 The type of the column the values of which are used to fill the profile. Inferred if not present.
2525 /// \tparam W The type of the column the weights of which are used to fill the profile. Inferred if not present.
2526 /// \param[in] model The model to be considered to build the new return value.
2527 /// \param[in] v1Name The name of the column that will fill the x axis.
2528 /// \param[in] v2Name The name of the column that will fill the y axis.
2529 /// \param[in] wName The name of the column that will provide the weights.
2530 /// \return the monodimensional profile wrapped in a RResultPtr.
2531 ///
2532 /// This action is *lazy*: upon invocation of this method the calculation is
2533 /// booked but not executed. Also see RResultPtr.
2534 ///
2535 /// ### Example usage:
2536 /// ~~~{.cpp}
2537 /// // Deduce column types (this invocation needs jitting internally)
2538 /// auto myProf1 = myDf.Profile1D({"profName", "profTitle", 64u, -4., 4.}, "xValues", "yValues", "weight");
2539 /// // Explicit column types
2540 /// auto myProf2 = myDf.Profile1D<int, float, double>({"profName", "profTitle", 64u, -4., 4.},
2541 /// "xValues", "yValues", "weight");
2542 /// ~~~
2543 ///
2544 /// See the first Profile1D() overload for more details.
2545 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
2546 typename W = RDFDetail::RInferredType>
2548 Profile1D(const TProfile1DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view wName)
2549 {
2550 std::shared_ptr<::TProfile> h(nullptr);
2551 {
2552 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2553 h = model.GetProfile();
2554 }
2555
2556 if (!RDFInternal::HistoUtils<::TProfile>::HasAxisLimits(*h)) {
2557 throw std::runtime_error("Profile histograms with no axes limits are not supported yet.");
2558 }
2559 const std::vector<std::string_view> columnViews = {v1Name, v2Name, wName};
2560 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2561 ? ColumnNames_t()
2562 : ColumnNames_t(columnViews.begin(), columnViews.end());
2563 return CreateAction<RDFInternal::ActionTags::Profile1D, V1, V2, W>(userColumns, h, h, fProxiedPtr);
2564 }
2565
2566 ////////////////////////////////////////////////////////////////////////////
2567 /// \brief Fill and return a one-dimensional profile (*lazy action*).
2568 /// See the first Profile1D() overload for more details.
2569 template <typename V1, typename V2, typename W>
2571 {
2572 return Profile1D<V1, V2, W>(model, "", "", "");
2573 }
2574
2575 ////////////////////////////////////////////////////////////////////////////
2576 /// \brief Fill and return a two-dimensional profile (*lazy action*).
2577 /// \tparam V1 The type of the column used to fill the x axis of the histogram. Inferred if not present.
2578 /// \tparam V2 The type of the column used to fill the y axis of the histogram. Inferred if not present.
2579 /// \tparam V3 The type of the column used to fill the z axis of the histogram. Inferred if not present.
2580 /// \param[in] model The returned profile will be constructed using this as a model.
2581 /// \param[in] v1Name The name of the column that will fill the x axis.
2582 /// \param[in] v2Name The name of the column that will fill the y axis.
2583 /// \param[in] v3Name The name of the column that will fill the z axis.
2584 /// \return the bidimensional profile wrapped in a RResultPtr.
2585 ///
2586 /// This action is *lazy*: upon invocation of this method the calculation is
2587 /// booked but not executed. Also see RResultPtr.
2588 ///
2589 /// ### Example usage:
2590 /// ~~~{.cpp}
2591 /// // Deduce column types (this invocation needs jitting internally)
2592 /// auto myProf1 = myDf.Profile2D({"profName", "profTitle", 40, -4, 4, 40, -4, 4, 0, 20},
2593 /// "xValues", "yValues", "zValues");
2594 /// // Explicit column types
2595 /// auto myProf2 = myDf.Profile2D<int, float, double>({"profName", "profTitle", 40, -4, 4, 40, -4, 4, 0, 20},
2596 /// "xValues", "yValues", "zValues");
2597 /// ~~~
2598 ///
2599 /// \note Differently from other ROOT interfaces, the returned profile is not associated to gDirectory
2600 /// and the caller is responsible for its lifetime (in particular, a typical source of confusion is that
2601 /// if result histograms go out of scope before the end of the program, ROOT might display a blank canvas).
2602 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
2603 typename V3 = RDFDetail::RInferredType>
2604 RResultPtr<::TProfile2D> Profile2D(const TProfile2DModel &model, std::string_view v1Name = "",
2605 std::string_view v2Name = "", std::string_view v3Name = "")
2606 {
2607 std::shared_ptr<::TProfile2D> h(nullptr);
2608 {
2609 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2610 h = model.GetProfile();
2611 }
2612
2613 if (!RDFInternal::HistoUtils<::TProfile2D>::HasAxisLimits(*h)) {
2614 throw std::runtime_error("2D profiles with no axes limits are not supported yet.");
2615 }
2616 const std::vector<std::string_view> columnViews = {v1Name, v2Name, v3Name};
2617 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2618 ? ColumnNames_t()
2619 : ColumnNames_t(columnViews.begin(), columnViews.end());
2620 return CreateAction<RDFInternal::ActionTags::Profile2D, V1, V2, V3>(userColumns, h, h, fProxiedPtr);
2621 }
2622
2623 ////////////////////////////////////////////////////////////////////////////
2624 /// \brief Fill and return a two-dimensional profile (*lazy action*).
2625 /// \tparam V1 The type of the column used to fill the x axis of the histogram. Inferred if not present.
2626 /// \tparam V2 The type of the column used to fill the y axis of the histogram. Inferred if not present.
2627 /// \tparam V3 The type of the column used to fill the z axis of the histogram. Inferred if not present.
2628 /// \tparam W The type of the column used for the weights of the histogram. Inferred if not present.
2629 /// \param[in] model The returned histogram will be constructed using this as a model.
2630 /// \param[in] v1Name The name of the column that will fill the x axis.
2631 /// \param[in] v2Name The name of the column that will fill the y axis.
2632 /// \param[in] v3Name The name of the column that will fill the z axis.
2633 /// \param[in] wName The name of the column that will provide the weights.
2634 /// \return the bidimensional profile wrapped in a RResultPtr.
2635 ///
2636 /// This action is *lazy*: upon invocation of this method the calculation is
2637 /// booked but not executed. Also see RResultPtr.
2638 ///
2639 /// ### Example usage:
2640 /// ~~~{.cpp}
2641 /// // Deduce column types (this invocation needs jitting internally)
2642 /// auto myProf1 = myDf.Profile2D({"profName", "profTitle", 40, -4, 4, 40, -4, 4, 0, 20},
2643 /// "xValues", "yValues", "zValues", "weight");
2644 /// // Explicit column types
2645 /// auto myProf2 = myDf.Profile2D<int, float, double, int>({"profName", "profTitle", 40, -4, 4, 40, -4, 4, 0, 20},
2646 /// "xValues", "yValues", "zValues", "weight");
2647 /// ~~~
2648 ///
2649 /// See the first Profile2D() overload for more details.
2650 template <typename V1 = RDFDetail::RInferredType, typename V2 = RDFDetail::RInferredType,
2651 typename V3 = RDFDetail::RInferredType, typename W = RDFDetail::RInferredType>
2652 RResultPtr<::TProfile2D> Profile2D(const TProfile2DModel &model, std::string_view v1Name, std::string_view v2Name,
2653 std::string_view v3Name, std::string_view wName)
2654 {
2655 std::shared_ptr<::TProfile2D> h(nullptr);
2656 {
2657 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel(kError);
2658 h = model.GetProfile();
2659 }
2660
2661 if (!RDFInternal::HistoUtils<::TProfile2D>::HasAxisLimits(*h)) {
2662 throw std::runtime_error("2D profiles with no axes limits are not supported yet.");
2663 }
2664 const std::vector<std::string_view> columnViews = {v1Name, v2Name, v3Name, wName};
2665 const auto userColumns = RDFInternal::AtLeastOneEmptyString(columnViews)
2666 ? ColumnNames_t()
2667 : ColumnNames_t(columnViews.begin(), columnViews.end());
2668 return CreateAction<RDFInternal::ActionTags::Profile2D, V1, V2, V3, W>(userColumns, h, h, fProxiedPtr);
2669 }
2670
2671 /// \brief Fill and return a two-dimensional profile (*lazy action*).
2672 /// See the first Profile2D() overload for more details.
2673 template <typename V1, typename V2, typename V3, typename W>
2675 {
2676 return Profile2D<V1, V2, V3, W>(model, "", "", "", "");
2677 }
2678
2679 ////////////////////////////////////////////////////////////////////////////
2680 /// \brief Return an object of type T on which `T::Fill` will be called once per event (*lazy action*).
2681 ///
2682 /// Type T must provide at least:
2683 /// - a copy-constructor
2684 /// - a `Fill` method that accepts as many arguments and with same types as the column names passed as columnList
2685 /// (these types can also be passed as template parameters to this method)
2686 /// - a `Merge` method with signature `Merge(TCollection *)` or `Merge(const std::vector<T *>&)` that merges the
2687 /// objects passed as argument into the object on which `Merge` was called (an analogous of TH1::Merge). Note that
2688 /// if the signature that takes a `TCollection*` is used, then T must inherit from TObject (to allow insertion in
2689 /// the TCollection*).
2690 ///
2691 /// \tparam FirstColumn The first type of the column the values of which are used to fill the object. Inferred together with OtherColumns if not present.
2692 /// \tparam OtherColumns A list of the other types of the columns the values of which are used to fill the object.
2693 /// \tparam T The type of the object to fill. Automatically deduced.
2694 /// \param[in] model The model to be considered to build the new return value.
2695 /// \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2696 /// \return the filled object wrapped in a RResultPtr.
2697 ///
2698 /// The user gives up ownership of the model object.
2699 /// The list of column names to be used for filling must always be specified.
2700 /// This action is *lazy*: upon invocation of this method the calculation is booked but not executed.
2701 /// Also see RResultPtr.
2702 ///
2703 /// ### Example usage:
2704 /// ~~~{.cpp}
2705 /// MyClass obj;
2706 /// // Deduce column types (this invocation needs jitting internally, and in this case
2707 /// // MyClass needs to be known to the interpreter)
2708 /// auto myFilledObj = myDf.Fill(obj, {"col0", "col1"});
2709 /// // explicit column types
2710 /// auto myFilledObj = myDf.Fill<float, float>(obj, {"col0", "col1"});
2711 /// ~~~
2712 ///
2713 template <typename FirstColumn = RDFDetail::RInferredType, typename... OtherColumns, typename T>
2714 RResultPtr<std::decay_t<T>> Fill(T &&model, const ColumnNames_t &columnList)
2715 {
2716 auto h = std::make_shared<std::decay_t<T>>(std::forward<T>(model));
2717 if (!RDFInternal::HistoUtils<T>::HasAxisLimits(*h)) {
2718 throw std::runtime_error("The absence of axes limits is not supported yet.");
2719 }
2720 return CreateAction<RDFInternal::ActionTags::Fill, FirstColumn, OtherColumns...>(columnList, h, h, fProxiedPtr,
2721 columnList.size());
2722 }
2723
2724 ////////////////////////////////////////////////////////////////////////////
2725 /// \brief Return a TStatistic object, filled once per event (*lazy action*).
2726 ///
2727 /// \tparam V The type of the value column
2728 /// \param[in] value The name of the column with the values to fill the statistics with.
2729 /// \return the filled TStatistic object wrapped in a RResultPtr.
2730 ///
2731 /// ### Example usage:
2732 /// ~~~{.cpp}
2733 /// // Deduce column type (this invocation needs jitting internally)
2734 /// auto stats0 = myDf.Stats("values");
2735 /// // Explicit column type
2736 /// auto stats1 = myDf.Stats<float>("values");
2737 /// ~~~
2738 ///
2739 template <typename V = RDFDetail::RInferredType>
2740 RResultPtr<TStatistic> Stats(std::string_view value = "")
2741 {
2742 ColumnNames_t columns;
2743 if (!value.empty()) {
2744 columns.emplace_back(std::string(value));
2745 }
2746 const auto validColumnNames = GetValidatedColumnNames(1, columns);
2747 if (std::is_same<V, RDFDetail::RInferredType>::value) {
2748 return Fill(TStatistic(), validColumnNames);
2749 } else {
2750 return Fill<V>(TStatistic(), validColumnNames);
2751 }
2752 }
2753
2754 ////////////////////////////////////////////////////////////////////////////
2755 /// \brief Return a TStatistic object, filled once per event (*lazy action*).
2756 ///
2757 /// \tparam V The type of the value column
2758 /// \tparam W The type of the weight column
2759 /// \param[in] value The name of the column with the values to fill the statistics with.
2760 /// \param[in] weight The name of the column with the weights to fill the statistics with.
2761 /// \return the filled TStatistic object wrapped in a RResultPtr.
2762 ///
2763 /// ### Example usage:
2764 /// ~~~{.cpp}
2765 /// // Deduce column types (this invocation needs jitting internally)
2766 /// auto stats0 = myDf.Stats("values", "weights");
2767 /// // Explicit column types
2768 /// auto stats1 = myDf.Stats<int, float>("values", "weights");
2769 /// ~~~
2770 ///
2771 template <typename V = RDFDetail::RInferredType, typename W = RDFDetail::RInferredType>
2772 RResultPtr<TStatistic> Stats(std::string_view value, std::string_view weight)
2773 {
2774 ColumnNames_t columns{std::string(value), std::string(weight)};
2775 constexpr auto vIsInferred = std::is_same<V, RDFDetail::RInferredType>::value;
2776 constexpr auto wIsInferred = std::is_same<W, RDFDetail::RInferredType>::value;
2777 const auto validColumnNames = GetValidatedColumnNames(2, columns);
2778 // We have 3 cases:
2779 // 1. Both types are inferred: we use Fill and let the jit kick in.
2780 // 2. One of the two types is explicit and the other one is inferred: the case is not supported.
2781 // 3. Both types are explicit: we invoke the fully compiled Fill method.
2782 if (vIsInferred && wIsInferred) {
2783 return Fill(TStatistic(), validColumnNames);
2784 } else if (vIsInferred != wIsInferred) {
2785 std::string error("The ");
2786 error += vIsInferred ? "value " : "weight ";
2787 error += "column type is explicit, while the ";
2788 error += vIsInferred ? "weight " : "value ";
2789 error += " is specified to be inferred. This case is not supported: please specify both types or none.";
2790 throw std::runtime_error(error);
2791 } else {
2792 return Fill<V, W>(TStatistic(), validColumnNames);
2793 }
2794 }
2795
2796 ////////////////////////////////////////////////////////////////////////////
2797 /// \brief Return the minimum of processed column values (*lazy action*).
2798 /// \tparam T The type of the branch/column.
2799 /// \param[in] columnName The name of the branch/column to be treated.
2800 /// \return the minimum value of the selected column wrapped in a RResultPtr.
2801 ///
2802 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2803 /// template specialization of this method.
2804 /// If the type of the column is inferred, the return type is `double`, the type of the column otherwise.
2805 ///
2806 /// This action is *lazy*: upon invocation of this method the calculation is
2807 /// booked but not executed. Also see RResultPtr.
2808 ///
2809 /// ### Example usage:
2810 /// ~~~{.cpp}
2811 /// // Deduce column type (this invocation needs jitting internally)
2812 /// auto minVal0 = myDf.Min("values");
2813 /// // Explicit column type
2814 /// auto minVal1 = myDf.Min<double>("values");
2815 /// ~~~
2816 ///
2817 template <typename T = RDFDetail::RInferredType>
2818 RResultPtr<RDFDetail::MinReturnType_t<T>> Min(std::string_view columnName = "")
2819 {
2820 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2821 using RetType_t = RDFDetail::MinReturnType_t<T>;
2822 auto minV = std::make_shared<RetType_t>(std::numeric_limits<RetType_t>::max());
2823 return CreateAction<RDFInternal::ActionTags::Min, T>(userColumns, minV, minV, fProxiedPtr);
2824 }
2825
2826 ////////////////////////////////////////////////////////////////////////////
2827 /// \brief Return the maximum of processed column values (*lazy action*).
2828 /// \tparam T The type of the branch/column.
2829 /// \param[in] columnName The name of the branch/column to be treated.
2830 /// \return the maximum value of the selected column wrapped in a RResultPtr.
2831 ///
2832 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2833 /// template specialization of this method.
2834 /// If the type of the column is inferred, the return type is `double`, the type of the column otherwise.
2835 ///
2836 /// This action is *lazy*: upon invocation of this method the calculation is
2837 /// booked but not executed. Also see RResultPtr.
2838 ///
2839 /// ### Example usage:
2840 /// ~~~{.cpp}
2841 /// // Deduce column type (this invocation needs jitting internally)
2842 /// auto maxVal0 = myDf.Max("values");
2843 /// // Explicit column type
2844 /// auto maxVal1 = myDf.Max<double>("values");
2845 /// ~~~
2846 ///
2847 template <typename T = RDFDetail::RInferredType>
2848 RResultPtr<RDFDetail::MaxReturnType_t<T>> Max(std::string_view columnName = "")
2849 {
2850 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2851 using RetType_t = RDFDetail::MaxReturnType_t<T>;
2852 auto maxV = std::make_shared<RetType_t>(std::numeric_limits<RetType_t>::lowest());
2853 return CreateAction<RDFInternal::ActionTags::Max, T>(userColumns, maxV, maxV, fProxiedPtr);
2854 }
2855
2856 ////////////////////////////////////////////////////////////////////////////
2857 /// \brief Return the mean of processed column values (*lazy action*).
2858 /// \tparam T The type of the branch/column.
2859 /// \param[in] columnName The name of the branch/column to be treated.
2860 /// \return the mean value of the selected column wrapped in a RResultPtr.
2861 ///
2862 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2863 /// template specialization of this method.
2864 /// Note that internally, the summations are executed with Kahan sums in double precision, irrespective
2865 /// of the type of column that is read.
2866 ///
2867 /// This action is *lazy*: upon invocation of this method the calculation is
2868 /// booked but not executed. Also see RResultPtr.
2869 ///
2870 /// ### Example usage:
2871 /// ~~~{.cpp}
2872 /// // Deduce column type (this invocation needs jitting internally)
2873 /// auto meanVal0 = myDf.Mean("values");
2874 /// // Explicit column type
2875 /// auto meanVal1 = myDf.Mean<double>("values");
2876 /// ~~~
2877 ///
2878 template <typename T = RDFDetail::RInferredType>
2879 RResultPtr<double> Mean(std::string_view columnName = "")
2880 {
2881 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2882 auto meanV = std::make_shared<double>(0);
2883 return CreateAction<RDFInternal::ActionTags::Mean, T>(userColumns, meanV, meanV, fProxiedPtr);
2884 }
2885
2886 ////////////////////////////////////////////////////////////////////////////
2887 /// \brief Return the unbiased standard deviation of processed column values (*lazy action*).
2888 /// \tparam T The type of the branch/column.
2889 /// \param[in] columnName The name of the branch/column to be treated.
2890 /// \return the standard deviation value of the selected column wrapped in a RResultPtr.
2891 ///
2892 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2893 /// template specialization of this method.
2894 ///
2895 /// This action is *lazy*: upon invocation of this method the calculation is
2896 /// booked but not executed. Also see RResultPtr.
2897 ///
2898 /// ### Example usage:
2899 /// ~~~{.cpp}
2900 /// // Deduce column type (this invocation needs jitting internally)
2901 /// auto stdDev0 = myDf.StdDev("values");
2902 /// // Explicit column type
2903 /// auto stdDev1 = myDf.StdDev<double>("values");
2904 /// ~~~
2905 ///
2906 template <typename T = RDFDetail::RInferredType>
2907 RResultPtr<double> StdDev(std::string_view columnName = "")
2908 {
2909 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2910 auto stdDeviationV = std::make_shared<double>(0);
2911 return CreateAction<RDFInternal::ActionTags::StdDev, T>(userColumns, stdDeviationV, stdDeviationV, fProxiedPtr);
2912 }
2913
2914 ////////////////////////////////////////////////////////////////////////////
2915 /// \brief Return the median value of the input column (*lazy action*).
2916 /// \tparam T The type of the column.
2917 /// \param[in] columnName The name of the column to be treated.
2918 /// \return The median value of the selected column wrapped in a RResultPtr.
2919 ///
2920 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2921 /// template specialization of this method.
2922 /// The result is always a double, irrespective of the type of column that is read. For an even
2923 /// number of entries, the median is the average of the two middle values.
2924 ///
2925 /// \note If the column is empty, a signaling NaN is returned.
2926 ///
2927 /// \note Computing the exact median requires all column values to be held in memory at once, so the
2928 /// memory used by this action grows with the number of processed entries. Consider this when running
2929 /// over large datasets.
2930 ///
2931 /// This action is *lazy*: upon invocation of this method the calculation is
2932 /// booked but not executed. Also see RResultPtr.
2933 ///
2934 /// ### Example usage:
2935 /// ~~~{.cpp}
2936 /// // Deduce column type (this invocation needs jitting internally)
2937 /// auto medianVal0 = myDf.Median("values");
2938 /// // Explicit column type
2939 /// auto medianVal1 = myDf.Median<double>("values");
2940 /// ~~~
2941 ///
2942 template <typename T = RDFDetail::RInferredType>
2943 RResultPtr<double> Median(std::string_view columnName = "")
2944 {
2945 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2946 auto medianV = std::make_shared<double>(0);
2947 return CreateAction<RDFInternal::ActionTags::Median, T>(userColumns, medianV, medianV, fProxiedPtr);
2948 }
2949
2950 // clang-format off
2951 ////////////////////////////////////////////////////////////////////////////
2952 /// \brief Return the sum of processed column values (*lazy action*).
2953 /// \tparam T The type of the branch/column.
2954 /// \param[in] columnName The name of the branch/column.
2955 /// \param[in] initValue Optional initial value for the sum. If not present, the column values must be default-constructible.
2956 /// \return the sum of the selected column wrapped in a RResultPtr.
2957 ///
2958 /// If T is not specified, RDataFrame will infer it from the data and just-in-time compile the correct
2959 /// template specialization of this method.
2960 /// If the type of the column is inferred, the return type is `double`, the type of the column otherwise.
2961 ///
2962 /// This action is *lazy*: upon invocation of this method the calculation is
2963 /// booked but not executed. Also see RResultPtr.
2964 ///
2965 /// ### Example usage:
2966 /// ~~~{.cpp}
2967 /// // Deduce column type (this invocation needs jitting internally)
2968 /// auto sum0 = myDf.Sum("values");
2969 /// // Explicit column type
2970 /// auto sum1 = myDf.Sum<double>("values");
2971 /// ~~~
2972 ///
2973 template <typename T = RDFDetail::RInferredType>
2975 Sum(std::string_view columnName = "",
2976 const RDFDetail::SumReturnType_t<T> &initValue = RDFDetail::SumReturnType_t<T>{})
2977 {
2978 const auto userColumns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
2979 auto sumV = std::make_shared<RDFDetail::SumReturnType_t<T>>(initValue);
2980 return CreateAction<RDFInternal::ActionTags::Sum, T>(userColumns, sumV, sumV, fProxiedPtr);
2981 }
2982 // clang-format on
2983
2984 ////////////////////////////////////////////////////////////////////////////
2985 /// \brief Gather filtering statistics.
2986 /// \return the resulting `RCutFlowReport` instance wrapped in a RResultPtr.
2987 ///
2988 /// Calling `Report` on the main `RDataFrame` object gathers stats for
2989 /// all named filters in the call graph. Calling this method on a
2990 /// stored chain state (i.e. a graph node different from the first) gathers
2991 /// the stats for all named filters in the chain section between the original
2992 /// `RDataFrame` and that node (included). Stats are gathered in the same
2993 /// order as the named filters have been added to the graph.
2994 /// A RResultPtr<RCutFlowReport> is returned to allow inspection of the
2995 /// effects cuts had.
2996 ///
2997 /// This action is *lazy*: upon invocation of
2998 /// this method the calculation is booked but not executed. See RResultPtr
2999 /// documentation.
3000 ///
3001 /// ### Example usage:
3002 /// ~~~{.cpp}
3003 /// auto filtered = d.Filter(cut1, {"b1"}, "Cut1").Filter(cut2, {"b2"}, "Cut2");
3004 /// auto cutReport = filtered3.Report();
3005 /// cutReport->Print();
3006 /// ~~~
3007 ///
3009 {
3010 bool returnEmptyReport = false;
3011 // if this is a RInterface<RLoopManager> on which `Define` has been called, users
3012 // are calling `Report` on a chain of the form LoopManager->Define->Define->..., which
3013 // certainly does not contain named filters.
3014 // The number 4 takes into account the implicit columns for entry and slot number
3015 // and their aliases (2 + 2, i.e. {r,t}dfentry_ and {r,t}dfslot_)
3016 if (std::is_same<Proxied, RLoopManager>::value && fColRegister.GenerateColumnNames().size() > 4)
3017 returnEmptyReport = true;
3018
3019 auto rep = std::make_shared<RCutFlowReport>();
3020 using Helper_t = RDFInternal::ReportHelper<Proxied>;
3022
3023 auto action = std::make_unique<Action_t>(Helper_t(rep, fProxiedPtr.get(), returnEmptyReport), ColumnNames_t({}),
3025
3026 return MakeResultPtr(rep, *fLoopManager, std::move(action));
3027 }
3028
3029
3030 ////////////////////////////////////////////////////////////////////////////
3031 /// \brief Provides a representation of the columns in the dataset.
3032 /// \tparam ColumnTypes variadic list of branch/column types.
3033 /// \param[in] columnList Names of the columns to be displayed.
3034 /// \param[in] nRows Number of events for each column to be displayed.
3035 /// \param[in] nMaxCollectionElements Maximum number of collection elements to display per row.
3036 /// \return the `RDisplay` instance wrapped in a RResultPtr.
3037 ///
3038 /// This function returns a `RResultPtr<RDisplay>` containing all the entries to be displayed, organized in a tabular
3039 /// form. RDisplay will either print on the standard output a summarized version through `RDisplay::Print()` or will
3040 /// return a complete version through `RDisplay::AsString()`.
3041 ///
3042 /// This action is *lazy*: upon invocation of this method the calculation is booked but not executed. Also see
3043 /// RResultPtr.
3044 ///
3045 /// Example usage:
3046 /// ~~~{.cpp}
3047 /// // Preparing the RResultPtr<RDisplay> object with all columns and default number of entries
3048 /// auto d1 = rdf.Display("");
3049 /// // Preparing the RResultPtr<RDisplay> object with two columns and 128 entries
3050 /// auto d2 = d.Display({"x", "y"}, 128);
3051 /// // Printing the short representations, the event loop will run
3052 /// d1->Print();
3053 /// d2->Print();
3054 /// ~~~
3055 template <typename... ColumnTypes>
3056 RResultPtr<RDisplay> Display(const ColumnNames_t &columnList, size_t nRows = 5, size_t nMaxCollectionElements = 10)
3057 {
3058 CheckIMTDisabled("Display");
3059 auto newCols = columnList;
3060 newCols.insert(newCols.begin(), "rdfentry_"); // Artificially insert first column
3061 auto displayer = std::make_shared<RDisplay>(newCols, GetColumnTypeNamesList(newCols), nMaxCollectionElements);
3062 using displayHelperArgs_t = std::pair<size_t, std::shared_ptr<RDisplay>>;
3063 // Need to add ULong64_t type corresponding to the first column rdfentry_
3064 return CreateAction<RDFInternal::ActionTags::Display, ULong64_t, ColumnTypes...>(
3065 std::move(newCols), displayer, std::make_shared<displayHelperArgs_t>(nRows, displayer), fProxiedPtr);
3066 }
3067
3068 ////////////////////////////////////////////////////////////////////////////
3069 /// \brief Provides a representation of the columns in the dataset.
3070 /// \param[in] columnList Names of the columns to be displayed.
3071 /// \param[in] nRows Number of events for each column to be displayed.
3072 /// \param[in] nMaxCollectionElements Maximum number of collection elements to display per row.
3073 /// \return the `RDisplay` instance wrapped in a RResultPtr.
3074 ///
3075 /// This overload automatically infers the column types.
3076 /// See the previous overloads for further details.
3077 ///
3078 /// Invoked when no types are specified to Display
3079 RResultPtr<RDisplay> Display(const ColumnNames_t &columnList, size_t nRows = 5, size_t nMaxCollectionElements = 10)
3080 {
3081 CheckIMTDisabled("Display");
3082 auto newCols = columnList;
3083 newCols.insert(newCols.begin(), "rdfentry_"); // Artificially insert first column
3084 auto displayer = std::make_shared<RDisplay>(newCols, GetColumnTypeNamesList(newCols), nMaxCollectionElements);
3085 using displayHelperArgs_t = std::pair<size_t, std::shared_ptr<RDisplay>>;
3086 return CreateAction<RDFInternal::ActionTags::Display, RDFDetail::RInferredType>(
3087 std::move(newCols), displayer, std::make_shared<displayHelperArgs_t>(nRows, displayer), fProxiedPtr,
3088 columnList.size() + 1);
3089 }
3090
3091 ////////////////////////////////////////////////////////////////////////////
3092 /// \brief Provides a representation of the columns in the dataset.
3093 /// \param[in] columnNameRegexp A regular expression to select the columns.
3094 /// \param[in] nRows Number of events for each column to be displayed.
3095 /// \param[in] nMaxCollectionElements Maximum number of collection elements to display per row.
3096 /// \return the `RDisplay` instance wrapped in a RResultPtr.
3097 ///
3098 /// The existing columns are matched against the regular expression. If the string provided
3099 /// is empty, all columns are selected.
3100 /// See the previous overloads for further details.
3102 Display(std::string_view columnNameRegexp = "", size_t nRows = 5, size_t nMaxCollectionElements = 10)
3103 {
3104 const auto columnNames = GetColumnNames();
3105 const auto selectedColumns = RDFInternal::ConvertRegexToColumns(columnNames, columnNameRegexp, "Display");
3106 return Display(selectedColumns, nRows, nMaxCollectionElements);
3107 }
3108
3109 ////////////////////////////////////////////////////////////////////////////
3110 /// \brief Provides a representation of the columns in the dataset.
3111 /// \param[in] columnList Names of the columns to be displayed.
3112 /// \param[in] nRows Number of events for each column to be displayed.
3113 /// \param[in] nMaxCollectionElements Number of maximum elements in collection.
3114 /// \return the `RDisplay` instance wrapped in a RResultPtr.
3115 ///
3116 /// See the previous overloads for further details.
3118 Display(std::initializer_list<std::string> columnList, size_t nRows = 5, size_t nMaxCollectionElements = 10)
3119 {
3120 ColumnNames_t selectedColumns(columnList);
3121 return Display(selectedColumns, nRows, nMaxCollectionElements);
3122 }
3123
3124 /// \}
3125 // End of the doxygen group for actions
3126 // ----------------------------------------------------------------------------------------
3127
3128 /// \name Immediate Actions
3129 /// Immediate Actions eagerly start the event loop and produce a result.
3130 /// \{
3131
3132 template <typename... ColumnTypes>
3133 [[deprecated("Snapshot is not any more a template. You can safely remove the template parameters.")]]
3135 Snapshot(std::string_view treename, std::string_view filename, const ColumnNames_t &columnList,
3136 const RSnapshotOptions &options = RSnapshotOptions())
3137 {
3138 return Snapshot(treename, filename, columnList, options);
3139 }
3140
3141 ////////////////////////////////////////////////////////////////////////////
3142 /// \brief Save selected columns to disk, in a new TTree or RNTuple `treename` in file `filename`.
3143 /// \param[in] treename The name of the output TTree or RNTuple.
3144 /// \param[in] filename The name of the output TFile.
3145 /// \param[in] columnList The list of names of the columns/branches/fields to be written.
3146 /// \param[in] options RSnapshotOptions struct with extra options to pass to TFile and TTree/RNTuple.
3147 /// \return a `RDataFrame` that wraps the snapshotted dataset.
3148 ///
3149 /// This function returns a `RDataFrame` built with the output TTree or RNTuple as a source.
3150 /// The types of the columns are automatically inferred and do not need to be specified.
3151 ///
3152 /// Support for writing of nested branches/fields is limited (although RDataFrame is able to read them) and dot ('.')
3153 /// characters in input column names will be replaced by underscores ('_') in the branches produced by Snapshot.
3154 /// When writing a variable size array through Snapshot, it is required that the column indicating its size is also
3155 /// written out and it appears before the array in the columnList.
3156 ///
3157 /// By default, in case of TTree, TChain or RNTuple inputs, Snapshot will try to write out all top-level branches.
3158 /// For other types of inputs, all columns returned by GetColumnNames() will be written out. Systematic variations of
3159 /// columns will be included if the corresponding flag is set in RSnapshotOptions. See \ref snapshot-with-variations
3160 /// "Snapshot with Variations" for more details. If friend trees or chains are present, by default all friend
3161 /// top-level branches that have names that do not collide with names of branches in the main TTree/TChain will be
3162 /// written out. Since v6.24, Snapshot will also write out friend branches with the same names of branches in the
3163 /// main TTree/TChain with names of the form
3164 /// `<friendname>_<branchname>` in order to differentiate them from the branches in the main tree/chain.
3165 ///
3166 /// ### Writing to a sub-directory
3167 ///
3168 /// Snapshot supports writing the TTree or RNTuple in a sub-directory inside the TFile. It is sufficient to specify
3169 /// the directory path as part of the TTree or RNTuple name, e.g. `df.Snapshot("subdir/t", "f.root")` writes TTree
3170 /// `t` in the sub-directory `subdir` of file `f.root` (creating file and sub-directory as needed).
3171 ///
3172 /// \attention In multi-thread runs (i.e. when EnableImplicitMT() has been called) threads will loop over clusters of
3173 /// entries in an undefined order, so Snapshot will produce outputs in which (clusters of) entries will be shuffled
3174 /// with respect to the input TTree. Using such "shuffled" TTrees as friends of the original trees would result in
3175 /// wrong associations between entries in the main TTree and entries in the "shuffled" friend. Since v6.22, ROOT will
3176 /// error out if such a "shuffled" TTree is used in a friendship.
3177 ///
3178 /// \note In case no events are written out (e.g. because no event passes all filters), Snapshot will still write the
3179 /// requested output TTree or RNTuple to the file, with all the branches requested to preserve the dataset schema.
3180 ///
3181 /// \note Snapshot will refuse to process columns with names of the form `#columnname`. These are special columns
3182 /// made available by some data sources (e.g. RNTupleDS) that represent the size of column `columnname`, and are
3183 /// not meant to be written out with that name (which is not a valid C++ variable name). Instead, go through an
3184 /// Alias(): `df.Alias("nbar", "#bar").Snapshot(..., {"nbar"})`.
3185 ///
3186 /// ### Example invocations:
3187 ///
3188 /// ~~~{.cpp}
3189 /// // No need to specify column types, they are automatically deduced thanks
3190 /// // to information coming from the data source
3191 /// df.Snapshot("outputTree", "outputFile.root", {"x", "y"});
3192 /// ~~~
3193 ///
3194 /// To book a Snapshot without triggering the event loop, one needs to set the appropriate flag in
3195 /// `RSnapshotOptions`:
3196 /// ~~~{.cpp}
3197 /// RSnapshotOptions opts;
3198 /// opts.fLazy = true;
3199 /// df.Snapshot("outputTree", "outputFile.root", {"x"}, opts);
3200 /// ~~~
3201 ///
3202 /// To snapshot to the RNTuple data format, the `fOutputFormat` option in `RSnapshotOptions` needs to be set
3203 /// accordingly:
3204 /// ~~~{.cpp}
3205 /// RSnapshotOptions opts;
3206 /// opts.fOutputFormat = ROOT::RDF::ESnapshotOutputFormat::kRNTuple;
3207 /// df.Snapshot("outputNTuple", "outputFile.root", {"x"}, opts);
3208 /// ~~~
3209 ///
3210 /// Snapshot systematic variations resulting from a Vary() call (see details \ref snapshot-with-variations "here"):
3211 /// ~~~{.cpp}
3212 /// RSnapshotOptions opts;
3213 /// opts.fIncludeVariations = true;
3214 /// df.Snapshot("outputTree", "outputFile.root", {"x"}, opts);
3215 /// ~~~
3216 RResultPtr<RInterface<RLoopManager>> Snapshot(std::string_view treename, std::string_view filename,
3217 const ColumnNames_t &columnList,
3218 const RSnapshotOptions &options = RSnapshotOptions())
3219 {
3220 // like columnList but with `#var` columns removed
3221 auto colListNoPoundSizes = RDFInternal::FilterArraySizeColNames(columnList, "Snapshot");
3222 // like columnListWithoutSizeColumns but with aliases resolved
3223 auto colListNoAliases = GetValidatedColumnNames(colListNoPoundSizes.size(), colListNoPoundSizes);
3225 // like validCols but with missing size branches required by array branches added in the right positions
3226 const auto pairOfColumnLists =
3227 RDFInternal::AddSizeBranches(GetDataSource(), std::move(colListNoAliases), std::move(colListNoPoundSizes));
3228 const auto &colListNoAliasesWithSizeBranches = pairOfColumnLists.first;
3229 const auto &colListWithAliasesAndSizeBranches = pairOfColumnLists.second;
3230
3231 const auto fullTreeName = treename;
3232 const auto parsedTreePath = RDFInternal::ParseTreePath(fullTreeName);
3233 treename = parsedTreePath.fTreeName;
3234 const auto &dirname = parsedTreePath.fDirName;
3235
3237
3239
3240 auto retrieveTypeID = [](const std::string &colName, const std::string &colTypeName,
3241 bool isRNTuple = false) -> const std::type_info * {
3242 try {
3243 return &ROOT::Internal::RDF::TypeName2TypeID(colTypeName);
3244 } catch (const std::runtime_error &err) {
3245 if (isRNTuple)
3247
3248 if (std::string(err.what()).find("Cannot extract type_info of type") != std::string::npos) {
3249 // We could not find RTTI for this column, thus we cannot write it out at the moment.
3250 std::string trueTypeName{colTypeName};
3251 if (colTypeName.rfind("CLING_UNKNOWN_TYPE", 0) == 0)
3252 trueTypeName = colTypeName.substr(19);
3253 std::string msg{"No runtime type information is available for column \"" + colName +
3254 "\" with type name \"" + trueTypeName +
3255 "\". Thus, it cannot be written to disk with Snapshot. Make sure to generate and load "
3256 "ROOT dictionaries for the type of this column."};
3257
3258 throw std::runtime_error(msg);
3259 } else {
3260 throw;
3261 }
3262 }
3263 };
3264
3266
3267 if (options.fOutputFormat == ESnapshotOutputFormat::kRNTuple) {
3268 // The data source of the RNTuple resulting from the Snapshot action does not exist yet here, so we create one
3269 // without a data source for now, and set it once the actual data source can be created (i.e., after
3270 // writing the RNTuple).
3271 auto newRDF = std::make_shared<RInterface<RLoopManager>>(std::make_shared<RLoopManager>(colListNoPoundSizes));
3272
3273 auto snapHelperArgs = std::make_shared<RDFInternal::SnapshotHelperArgs>(RDFInternal::SnapshotHelperArgs{
3274 std::string(filename), std::string(dirname), std::string(treename), colListWithAliasesAndSizeBranches,
3275 options, newRDF->GetLoopManager(), GetLoopManager(), true /* fToNTuple */, /*fIncludeVariations=*/false});
3276
3277 auto &&nColumns = colListNoAliasesWithSizeBranches.size();
3278 const auto validColumnNames = GetValidatedColumnNames(nColumns, colListNoAliasesWithSizeBranches);
3279
3280 const auto nSlots = fLoopManager->GetNSlots();
3281 std::vector<const std::type_info *> colTypeIDs;
3282 colTypeIDs.reserve(nColumns);
3283 for (decltype(nColumns) i{}; i < nColumns; i++) {
3284 const auto &colName = validColumnNames[i];
3285 const auto colTypeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(
3286 colName, /*tree*/ nullptr, GetDataSource(), fColRegister.GetDefine(colName), options.fVector2RVec);
3287 const std::type_info *colTypeID = retrieveTypeID(colName, colTypeName, /*isRNTuple*/ true);
3288 colTypeIDs.push_back(colTypeID);
3289 }
3290 // Crucial e.g. if the column names do not correspond to already-available column readers created by the data
3291 // source
3292 CheckAndFillDSColumns(validColumnNames, colTypeIDs);
3293
3294 auto action =
3295 RDFInternal::BuildAction(validColumnNames, snapHelperArgs, nSlots, fProxiedPtr, fColRegister, colTypeIDs);
3296 resPtr = MakeResultPtr(newRDF, *GetLoopManager(), std::move(action));
3297 } else {
3298 if (RDFInternal::GetDataSourceLabel(*this) == "RNTupleDS" &&
3299 options.fOutputFormat == ESnapshotOutputFormat::kDefault) {
3300 Warning("Snapshot",
3301 "The default Snapshot output data format is TTree, but the input data format is RNTuple. If you "
3302 "want to Snapshot to RNTuple or suppress this warning, set the appropriate fOutputFormat option in "
3303 "RSnapshotOptions. Note that this current default behaviour might change in the future.");
3304 }
3305
3306 // We create an RLoopManager without a data source. This needs to be initialised when the output TTree dataset
3307 // has actually been created and written to TFile, i.e. at the end of the Snapshot execution.
3308 auto newRDF = std::make_shared<RInterface<RLoopManager>>(
3309 std::make_shared<RLoopManager>(colListNoAliasesWithSizeBranches));
3310
3311 auto snapHelperArgs = std::make_shared<RDFInternal::SnapshotHelperArgs>(RDFInternal::SnapshotHelperArgs{
3312 std::string(filename), std::string(dirname), std::string(treename), colListWithAliasesAndSizeBranches,
3313 options, newRDF->GetLoopManager(), GetLoopManager(), false /* fToRNTuple */, options.fIncludeVariations});
3314
3315 auto &&nColumns = colListNoAliasesWithSizeBranches.size();
3316 const auto validColumnNames = GetValidatedColumnNames(nColumns, colListNoAliasesWithSizeBranches);
3317
3318 const auto nSlots = fLoopManager->GetNSlots();
3319 std::vector<const std::type_info *> colTypeIDs;
3320 colTypeIDs.reserve(nColumns);
3321 for (decltype(nColumns) i{}; i < nColumns; i++) {
3322 const auto &colName = validColumnNames[i];
3323 const auto colTypeName = ROOT::Internal::RDF::ColumnName2ColumnTypeName(
3324 colName, /*tree*/ nullptr, GetDataSource(), fColRegister.GetDefine(colName), options.fVector2RVec);
3325 const std::type_info *colTypeID = retrieveTypeID(colName, colTypeName);
3326 colTypeIDs.push_back(colTypeID);
3327 }
3328 // Crucial e.g. if the column names do not correspond to already-available column readers created by the data
3329 // source
3330 CheckAndFillDSColumns(validColumnNames, colTypeIDs);
3331
3332 auto action =
3333 RDFInternal::BuildAction(validColumnNames, snapHelperArgs, nSlots, fProxiedPtr, fColRegister, colTypeIDs);
3334 resPtr = MakeResultPtr(newRDF, *GetLoopManager(), std::move(action));
3335 }
3336
3337 if (!options.fLazy)
3338 *resPtr;
3339 return resPtr;
3340 }
3341
3342 // clang-format off
3343 ////////////////////////////////////////////////////////////////////////////
3344 /// \brief Save selected columns to disk, in a new TTree or RNTuple `treename` in file `filename`.
3345 /// \param[in] treename The name of the output TTree or RNTuple.
3346 /// \param[in] filename The name of the output TFile.
3347 /// \param[in] columnNameRegexp The regular expression to match the column names to be selected. The presence of a '^' and a '$' at the end of the string is implicitly assumed if they are not specified. The dialect supported is PCRE via the TPRegexp class. An empty string signals the selection of all columns.
3348 /// \param[in] options RSnapshotOptions struct with extra options to pass to TFile and TTree/RNTuple
3349 /// \return a `RDataFrame` that wraps the snapshotted dataset.
3350 ///
3351 /// This function returns a `RDataFrame` built with the output TTree or RNTuple as a source.
3352 /// The types of the columns are automatically inferred and do not need to be specified.
3353 ///
3354 /// See Snapshot(std::string_view, std::string_view, const ColumnNames_t&, const RSnapshotOptions &) for a more complete description and example usages.
3355 RResultPtr<RInterface<RLoopManager>> Snapshot(std::string_view treename, std::string_view filename,
3356 std::string_view columnNameRegexp = "",
3357 const RSnapshotOptions &options = RSnapshotOptions())
3358 {
3359 const auto definedColumns = fColRegister.GenerateColumnNames();
3360
3362 // Ignore R_rdf_sizeof_* columns coming from datasources: we don't want to Snapshot those
3363 ColumnNames_t dsColumnsWithoutSizeColumns;
3364 std::copy_if(dsColumns.begin(), dsColumns.end(), std::back_inserter(dsColumnsWithoutSizeColumns),
3365 [](const std::string &name) { return name.size() < 13 || name.substr(0, 13) != "R_rdf_sizeof_"; });
3366 ColumnNames_t columnNames;
3367 columnNames.reserve(definedColumns.size() + dsColumnsWithoutSizeColumns.size());
3368 columnNames.insert(columnNames.end(), definedColumns.begin(), definedColumns.end());
3369 columnNames.insert(columnNames.end(), dsColumnsWithoutSizeColumns.begin(), dsColumnsWithoutSizeColumns.end());
3370
3371 // The only way we can get duplicate entries is if a column coming from a tree or data-source is Redefine'd.
3372 // RemoveDuplicates should preserve ordering of the columns: it might be meaningful.
3373 RDFInternal::RemoveDuplicates(columnNames);
3374
3375 std::vector<std::string> selectedColumns;
3376 try {
3377 selectedColumns = RDFInternal::ConvertRegexToColumns(columnNames, columnNameRegexp, "Snapshot");
3378 }
3379 catch (const std::runtime_error &e){
3380 // No columns were found, try again but consider all input data source columns
3381 if (auto ds = GetDataSource())
3382 selectedColumns = RDFInternal::ConvertRegexToColumns(ds->GetColumnNames(), columnNameRegexp, "Snapshot");
3383 else
3384 throw e;
3385 }
3386
3387 if (RDFInternal::GetDataSourceLabel(*this) == "RNTupleDS") {
3388 RDFInternal::RemoveRNTupleSubfields(selectedColumns);
3389 }
3390
3391 return Snapshot(treename, filename, selectedColumns, options);
3392 }
3393 // clang-format on
3394
3395 // clang-format off
3396 ////////////////////////////////////////////////////////////////////////////
3397 /// \brief Save selected columns to disk, in a new TTree or RNTuple `treename` in file `filename`.
3398 /// \param[in] treename The name of the output TTree or RNTuple.
3399 /// \param[in] filename The name of the output TFile.
3400 /// \param[in] columnList The list of names of the columns/branches to be written.
3401 /// \param[in] options RSnapshotOptions struct with extra options to pass to TFile and TTree/RNTuple.
3402 /// \return a `RDataFrame` that wraps the snapshotted dataset.
3403 ///
3404 /// This function returns a `RDataFrame` built with the output TTree or RNTuple as a source.
3405 /// The types of the columns are automatically inferred and do not need to be specified.
3406 ///
3407 /// See Snapshot(std::string_view, std::string_view, const ColumnNames_t&, const RSnapshotOptions &) for a more complete description and example usages.
3408 RResultPtr<RInterface<RLoopManager>> Snapshot(std::string_view treename, std::string_view filename,
3409 std::initializer_list<std::string> columnList,
3410 const RSnapshotOptions &options = RSnapshotOptions())
3411 {
3412 ColumnNames_t selectedColumns(columnList);
3413 return Snapshot(treename, filename, selectedColumns, options);
3414 }
3415 // clang-format on
3416
3417 ////////////////////////////////////////////////////////////////////////////
3418 /// \brief Save selected columns in memory.
3419 /// \tparam ColumnTypes variadic list of branch/column types.
3420 /// \param[in] columnList columns to be cached in memory.
3421 /// \return a `RDataFrame` that wraps the cached dataset.
3422 ///
3423 /// This action returns a new `RDataFrame` object, completely detached from
3424 /// the originating `RDataFrame`. The new dataframe only contains the cached
3425 /// columns and stores their content in memory for fast, zero-copy subsequent access.
3426 ///
3427 /// Use `Cache` if you know you will only need a subset of the (`Filter`ed) data that
3428 /// fits in memory and that will be accessed many times.
3429 ///
3430 /// \note Cache will refuse to process columns with names of the form `#columnname`. These are special columns
3431 /// made available by some data sources (e.g. RNTupleDS) that represent the size of column `columnname`, and are
3432 /// not meant to be written out with that name (which is not a valid C++ variable name). Instead, go through an
3433 /// Alias(): `df.Alias("nbar", "#bar").Cache<std::size_t>(..., {"nbar"})`.
3434 ///
3435 /// ### Example usage:
3436 ///
3437 /// **Types and columns specified:**
3438 /// ~~~{.cpp}
3439 /// auto cache_some_cols_df = df.Cache<double, MyClass, int>({"col0", "col1", "col2"});
3440 /// ~~~
3441 ///
3442 /// **Types inferred and columns specified (this invocation relies on jitting):**
3443 /// ~~~{.cpp}
3444 /// auto cache_some_cols_df = df.Cache({"col0", "col1", "col2"});
3445 /// ~~~
3446 ///
3447 /// **Types inferred and columns selected with a regexp (this invocation relies on jitting):**
3448 /// ~~~{.cpp}
3449 /// auto cache_all_cols_df = df.Cache(myRegexp);
3450 /// ~~~
3451 template <typename... ColumnTypes>
3453 {
3454 auto staticSeq = std::make_index_sequence<sizeof...(ColumnTypes)>();
3455 return CacheImpl<ColumnTypes...>(columnList, staticSeq);
3456 }
3457
3458 ////////////////////////////////////////////////////////////////////////////
3459 /// \brief Save selected columns in memory.
3460 /// \param[in] columnList columns to be cached in memory
3461 /// \return a `RDataFrame` that wraps the cached dataset.
3462 ///
3463 /// See the previous overloads for more information.
3465 {
3466 // Early return: if the list of columns is empty, just return an empty RDF
3467 // If we proceed, the jitted call will not compile!
3468 if (columnList.empty()) {
3469 auto nEntries = *this->Count();
3470 RInterface<RLoopManager> emptyRDF(std::make_shared<RLoopManager>(nEntries));
3471 return emptyRDF;
3472 }
3473
3474 std::stringstream cacheCall;
3475 auto upcastNode = RDFInternal::UpcastNode(fProxiedPtr);
3476 RInterface<TTraits::TakeFirstParameter_t<decltype(upcastNode)>> upcastInterface(fProxiedPtr, *fLoopManager,
3477 fColRegister);
3478 // build a string equivalent to
3479 // "(RInterface<nodetype*>*)(this)->Cache<Ts...>(*(ColumnNames_t*)(&columnList))"
3480 RInterface<RLoopManager> resRDF(std::make_shared<ROOT::Detail::RDF::RLoopManager>(0));
3481 cacheCall << "*reinterpret_cast<ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager>*>("
3483 << ") = reinterpret_cast<ROOT::RDF::RInterface<ROOT::Detail::RDF::RNodeBase>*>("
3484 << RDFInternal::PrettyPrintAddr(&upcastInterface) << ")->Cache<";
3485
3486 const auto columnListWithoutSizeColumns = RDFInternal::FilterArraySizeColNames(columnList, "Cache");
3487
3488 const auto validColumnNames =
3489 GetValidatedColumnNames(columnListWithoutSizeColumns.size(), columnListWithoutSizeColumns);
3490 const auto colTypes =
3491 GetValidatedArgTypes(validColumnNames, fColRegister, nullptr, GetDataSource(), "Cache", /*vector2RVec=*/false);
3492 for (const auto &colType : colTypes)
3493 cacheCall << colType << ", ";
3494 if (!columnListWithoutSizeColumns.empty())
3495 cacheCall.seekp(-2, cacheCall.cur); // remove the last ",
3496 cacheCall << ">(*reinterpret_cast<std::vector<std::string>*>(" // vector<string> should be ColumnNames_t
3497 << RDFInternal::PrettyPrintAddr(&columnListWithoutSizeColumns) << "));";
3498
3499 // book the code to jit with the RLoopManager and trigger the event loop
3500 fLoopManager->ToJitExec(cacheCall.str());
3501 fLoopManager->Jit();
3502
3503 return resRDF;
3504 }
3505
3506 ////////////////////////////////////////////////////////////////////////////
3507 /// \brief Save selected columns in memory.
3508 /// \param[in] columnNameRegexp The regular expression to match the column names to be selected. The presence of a '^' and a '$' at the end of the string is implicitly assumed if they are not specified. The dialect supported is PCRE via the TPRegexp class. An empty string signals the selection of all columns.
3509 /// \return a `RDataFrame` that wraps the cached dataset.
3510 ///
3511 /// The existing columns are matched against the regular expression. If the string provided
3512 /// is empty, all columns are selected. See the previous overloads for more information.
3513 RInterface<RLoopManager> Cache(std::string_view columnNameRegexp = "")
3514 {
3515 const auto definedColumns = fColRegister.GenerateColumnNames();
3516 const auto dsColumns = GetDataSource() ? GetDataSource()->GetColumnNames() : ColumnNames_t{};
3517 // Ignore R_rdf_sizeof_* columns coming from datasources: we don't want to Snapshot those
3518 ColumnNames_t dsColumnsWithoutSizeColumns;
3519 std::copy_if(dsColumns.begin(), dsColumns.end(), std::back_inserter(dsColumnsWithoutSizeColumns),
3520 [](const std::string &name) { return name.size() < 13 || name.substr(0, 13) != "R_rdf_sizeof_"; });
3521 ColumnNames_t columnNames;
3522 columnNames.reserve(definedColumns.size() + dsColumns.size());
3523 columnNames.insert(columnNames.end(), definedColumns.begin(), definedColumns.end());
3524 columnNames.insert(columnNames.end(), dsColumns.begin(), dsColumns.end());
3525 const auto selectedColumns = RDFInternal::ConvertRegexToColumns(columnNames, columnNameRegexp, "Cache");
3526 return Cache(selectedColumns);
3527 }
3528
3529 ////////////////////////////////////////////////////////////////////////////
3530 /// \brief Save selected columns in memory.
3531 /// \param[in] columnList columns to be cached in memory.
3532 /// \return a `RDataFrame` that wraps the cached dataset.
3533 ///
3534 /// See the previous overloads for more information.
3535 RInterface<RLoopManager> Cache(std::initializer_list<std::string> columnList)
3536 {
3537 ColumnNames_t selectedColumns(columnList);
3538 return Cache(selectedColumns);
3539 }
3540
3541
3542 // clang-format off
3543 ////////////////////////////////////////////////////////////////////////////
3544 /// \brief Execute a user-defined function on each entry (*instant action*).
3545 /// \param[in] f Function, lambda expression, functor class or any other callable object performing user defined calculations.
3546 /// \param[in] columns Names of the columns/branches in input to the user function.
3547 ///
3548 /// The callable `f` is invoked once per entry. This is an *instant action*:
3549 /// upon invocation, an event loop as well as execution of all scheduled actions
3550 /// is triggered.
3551 /// Users are responsible for the thread-safety of this callable when executing
3552 /// with implicit multi-threading enabled (i.e. ROOT::EnableImplicitMT).
3553 ///
3554 /// ### Example usage:
3555 /// ~~~{.cpp}
3556 /// myDf.Foreach([](int i){ std::cout << i << std::endl;}, {"myIntColumn"});
3557 /// ~~~
3558 // clang-format on
3559 template <typename F>
3560 void Foreach(F f, const ColumnNames_t &columns = {})
3561 {
3562 using arg_types = typename TTraits::CallableTraits<decltype(f)>::arg_types_nodecay;
3563 using ret_type = typename TTraits::CallableTraits<decltype(f)>::ret_type;
3564 ForeachSlot(RDFInternal::AddSlotParameter<ret_type>(f, arg_types()), columns);
3565 }
3566
3567 // clang-format off
3568 ////////////////////////////////////////////////////////////////////////////
3569 /// \brief Execute a user-defined function requiring a processing slot index on each entry (*instant action*).
3570 /// \param[in] f Function, lambda expression, functor class or any other callable object performing user defined calculations.
3571 /// \param[in] columns Names of the columns/branches in input to the user function.
3572 ///
3573 /// Same as `Foreach`, but the user-defined function takes an extra
3574 /// `unsigned int` as its first parameter, the *processing slot index*.
3575 /// This *slot index* will be assigned a different value, `0` to `poolSize - 1`,
3576 /// for each thread of execution.
3577 /// This is meant as a helper in writing thread-safe `Foreach`
3578 /// actions when using `RDataFrame` after `ROOT::EnableImplicitMT()`.
3579 /// The user-defined processing callable is able to follow different
3580 /// *streams of processing* indexed by the first parameter.
3581 /// `ForeachSlot` works just as well with single-thread execution: in that
3582 /// case `slot` will always be `0`.
3583 ///
3584 /// ### Example usage:
3585 /// ~~~{.cpp}
3586 /// myDf.ForeachSlot([](unsigned int s, int i){ std::cout << "Slot " << s << ": "<< i << std::endl;}, {"myIntColumn"});
3587 /// ~~~
3588 // clang-format on
3589 template <typename F>
3590 void ForeachSlot(F f, const ColumnNames_t &columns = {})
3591 {
3593 constexpr auto nColumns = ColTypes_t::list_size;
3594
3595 const auto validColumnNames = GetValidatedColumnNames(nColumns, columns);
3596 CheckAndFillDSColumns(validColumnNames, ColTypes_t());
3597
3598 using Helper_t = RDFInternal::ForeachSlotHelper<F>;
3600
3601 auto action = std::make_unique<Action_t>(Helper_t(std::move(f)), validColumnNames, fProxiedPtr, fColRegister);
3602
3603 fLoopManager->Run();
3604 }
3605
3606 /// \}
3607 // End of doxygen group for immediate actions
3608 // ----------------------------------------------------------------------------------------
3609
3610 /// \brief Returns the names of the filters created.
3611 /// \return the container of filters names.
3612 ///
3613 /// If called on a root node, all the filters in the computation graph will
3614 /// be printed. For any other node, only the filters upstream of that node.
3615 /// Filters without a name are printed as "Unnamed Filter"
3616 /// This is not an action nor a transformation, just a query to the RDataFrame object.
3617 ///
3618 /// ### Example usage:
3619 /// ~~~{.cpp}
3620 /// auto filtNames = d.GetFilterNames();
3621 /// for (auto &&filtName : filtNames) std::cout << filtName << std::endl;
3622 /// ~~~
3623 ///
3624 std::vector<std::string> GetFilterNames() { return RDFInternal::GetFilterNames(fProxiedPtr); }
3625
3626 /// \name User-defined Actions (lazy)
3627 /// Pass user-defined functions to be applied to the data and create results.
3628 /// These actions are lazy, i.e., they only run once a result is actually requested.
3629 /// \{
3630
3631 // clang-format off
3632 ////////////////////////////////////////////////////////////////////////////
3633 /// \brief Execute a user-defined accumulation operation on the processed column values in each processing slot.
3634 /// \tparam F The type of the aggregator callable. Automatically deduced.
3635 /// \tparam U The type of the aggregator variable. Must be default-constructible, copy-constructible and copy-assignable. Automatically deduced.
3636 /// \tparam T The type of the column to apply the reduction to. Automatically deduced.
3637 /// \param[in] aggregator A callable with signature `U(U,T)` or `void(U&,T)`, where T is the type of the column, U is the type of the aggregator variable
3638 /// \param[in] merger A callable with signature `U(U,U)` or `void(std::vector<U>&)` used to merge the results of the accumulations of each thread
3639 /// \param[in] columnName The column to be aggregated. If omitted, the first default column is used instead.
3640 /// \param[in] aggIdentity The aggregator variable of each thread is initialized to this value (or is default-constructed if the parameter is omitted)
3641 /// \return the result of the aggregation wrapped in a RResultPtr.
3642 ///
3643 /// An aggregator callable takes two values, an aggregator variable and a column value. The aggregator variable is
3644 /// initialized to aggIdentity or default-constructed if aggIdentity is omitted.
3645 /// This action calls the aggregator callable for each processed entry, passing in the aggregator variable and
3646 /// the value of the column columnName.
3647 /// If the signature is `U(U,T)` the aggregator variable is then copy-assigned the result of the execution of the callable.
3648 /// Otherwise the signature of aggregator must be `void(U&,T)`.
3649 ///
3650 /// The merger callable is used to merge the partial accumulation results of each processing thread. It is only called in multi-thread executions.
3651 /// If its signature is `U(U,U)` the aggregator variables of each thread are merged two by two.
3652 /// If its signature is `void(std::vector<U>& a)` it is assumed that it merges all aggregators in a[0].
3653 ///
3654 /// This action is *lazy*: upon invocation of this method the calculation is booked but not executed. Also see RResultPtr.
3655 ///
3656 /// Example usage:
3657 /// ~~~{.cpp}
3658 /// auto aggregator = [](double acc, double x) { return acc * x; };
3659 /// ROOT::EnableImplicitMT();
3660 /// // If multithread is enabled, the aggregator function will be called by more threads
3661 /// // and will produce a vector of partial accumulators.
3662 /// // The merger function performs the final aggregation of these partial results.
3663 /// auto merger = [](std::vector<double> &accumulators) {
3664 /// for (auto i : ROOT::TSeqU(1u, accumulators.size())) {
3665 /// accumulators[0] *= accumulators[i];
3666 /// }
3667 /// };
3668 ///
3669 /// // The accumulator is initialized at this value by every thread.
3670 /// double initValue = 1.;
3671 ///
3672 /// // Multiplies all elements of the column "x"
3673 /// auto result = d.Aggregate(aggregator, merger, "x", initValue);
3674 /// ~~~
3675 // clang-format on
3676 template <typename AccFun, typename MergeFun, typename R = typename TTraits::CallableTraits<AccFun>::ret_type,
3677 typename ArgTypes = typename TTraits::CallableTraits<AccFun>::arg_types,
3678 typename ArgTypesNoDecay = typename TTraits::CallableTraits<AccFun>::arg_types_nodecay,
3679 typename U = TTraits::TakeFirstParameter_t<ArgTypes>,
3680 typename T = TTraits::TakeFirstParameter_t<TTraits::RemoveFirstParameter_t<ArgTypes>>>
3681 RResultPtr<U> Aggregate(AccFun aggregator, MergeFun merger, std::string_view columnName, const U &aggIdentity)
3682 {
3683 RDFInternal::CheckAggregate<R, MergeFun>(ArgTypesNoDecay());
3684 const auto columns = columnName.empty() ? ColumnNames_t() : ColumnNames_t({std::string(columnName)});
3685
3686 const auto validColumnNames = GetValidatedColumnNames(1, columns);
3687 CheckAndFillDSColumns(validColumnNames, TTraits::TypeList<T>());
3688
3689 auto accObjPtr = std::make_shared<U>(aggIdentity);
3690 using Helper_t = RDFInternal::AggregateHelper<AccFun, MergeFun, R, T, U>;
3692 auto action = std::make_unique<Action_t>(
3693 Helper_t(std::move(aggregator), std::move(merger), accObjPtr, fLoopManager->GetNSlots()), validColumnNames,
3695 return MakeResultPtr(accObjPtr, *fLoopManager, std::move(action));
3696 }
3697
3698 // clang-format off
3699 ////////////////////////////////////////////////////////////////////////////
3700 /// \brief Execute a user-defined accumulation operation on the processed column values in each processing slot.
3701 /// \tparam F The type of the aggregator callable. Automatically deduced.
3702 /// \tparam U The type of the aggregator variable. Must be default-constructible, copy-constructible and copy-assignable. Automatically deduced.
3703 /// \tparam T The type of the column to apply the reduction to. Automatically deduced.
3704 /// \param[in] aggregator A callable with signature `U(U,T)` or `void(U,T)`, where T is the type of the column, U is the type of the aggregator variable
3705 /// \param[in] merger A callable with signature `U(U,U)` or `void(std::vector<U>&)` used to merge the results of the accumulations of each thread
3706 /// \param[in] columnName The column to be aggregated. If omitted, the first default column is used instead.
3707 /// \return the result of the aggregation wrapped in a RResultPtr.
3708 ///
3709 /// See previous Aggregate overload for more information.
3710 // clang-format on
3711 template <typename AccFun, typename MergeFun, typename R = typename TTraits::CallableTraits<AccFun>::ret_type,
3712 typename ArgTypes = typename TTraits::CallableTraits<AccFun>::arg_types,
3713 typename U = TTraits::TakeFirstParameter_t<ArgTypes>,
3714 typename T = TTraits::TakeFirstParameter_t<TTraits::RemoveFirstParameter_t<ArgTypes>>>
3715 RResultPtr<U> Aggregate(AccFun aggregator, MergeFun merger, std::string_view columnName = "")
3716 {
3717 static_assert(
3718 std::is_default_constructible<U>::value,
3719 "aggregated object cannot be default-constructed. Please provide an initialisation value (aggIdentity)");
3720 return Aggregate(std::move(aggregator), std::move(merger), columnName, U());
3721 }
3722
3723 // clang-format off
3724 ////////////////////////////////////////////////////////////////////////////
3725 /// \brief Book execution of a custom action using a user-defined helper object.
3726 /// \tparam FirstColumn The type of the first column used by this action. Inferred together with OtherColumns if not present.
3727 /// \tparam OtherColumns A list of the types of the other columns used by this action
3728 /// \tparam Helper The type of the user-defined helper. See below for the required interface it should expose.
3729 /// \param[in] helper The Action Helper to be scheduled.
3730 /// \param[in] columns The names of the columns on which the helper acts.
3731 /// \return the result of the helper wrapped in a RResultPtr.
3732 ///
3733 /// This method books a custom action for execution. The behavior of the action is completely dependent on the
3734 /// Helper object provided by the caller. The required interface for the helper is described below (more
3735 /// methods that the ones required can be present, e.g. a constructor that takes the number of worker threads is usually useful):
3736 ///
3737 /// ### Mandatory interface
3738 ///
3739 /// * `Helper` must publicly inherit from `ROOT::Detail::RDF::RActionImpl<Helper>`
3740 /// * `Helper::Result_t`: public alias for the type of the result of this action helper. `Result_t` must be default-constructible.
3741 /// * `Helper(Helper &&)`: a move-constructor is required. Copy-constructors are discouraged.
3742 /// * `std::shared_ptr<Result_t> GetResultPtr() const`: return a shared_ptr to the result of this action (of type
3743 /// Result_t). The RResultPtr returned by Book will point to this object. Note that this method can be called
3744 /// _before_ Initialize(), because the RResultPtr is constructed before the event loop is started.
3745 /// * `void Initialize()`: this method is called once before starting the event-loop. Useful for setup operations.
3746 /// It must reset the state of the helper to the expected state at the beginning of the event loop: the same helper,
3747 /// or copies of it, might be used for multiple event loops (e.g. in the presence of systematic variations).
3748 /// * `void InitTask(TTreeReader *, unsigned int slot)`: each working thread shall call this method during the event
3749 /// loop, before processing a batch of entries. The pointer passed as argument, if not null, will point to the TTreeReader
3750 /// that RDataFrame has set up to read the task's batch of entries. It is passed to the helper to allow certain advanced optimizations
3751 /// it should not usually serve any purpose for the Helper. This method is often no-op for simple helpers.
3752 /// * `void Exec(unsigned int slot, ColumnTypes...columnValues)`: each working thread shall call this method
3753 /// during the event-loop, possibly concurrently. No two threads will ever call Exec with the same 'slot' value:
3754 /// this parameter is there to facilitate writing thread-safe helpers. The other arguments will be the values of
3755 /// the requested columns for the particular entry being processed.
3756 /// * `void Finalize()`: this method is called at the end of the event loop. Commonly used to finalize the contents of the result.
3757 /// * `std::string GetActionName()`: it returns a string identifier for this type of action that RDataFrame will use in
3758 /// diagnostics, SaveGraph(), etc.
3759 ///
3760 /// ### Optional methods
3761 ///
3762 /// If these methods are implemented they enable extra functionality as per the description below.
3763 ///
3764 /// * `Result_t &PartialUpdate(unsigned int slot)`: if present, it must return the value of the partial result of this action for the given 'slot'.
3765 /// Different threads might call this method concurrently, but will do so with different 'slot' numbers.
3766 /// RDataFrame leverages this method to implement RResultPtr::OnPartialResult().
3767 /// * `ROOT::RDF::SampleCallback_t GetSampleCallback()`: if present, it must return a callable with the
3768 /// appropriate signature (see ROOT::RDF::SampleCallback_t) that will be invoked at the beginning of the processing
3769 /// of every sample, as in DefinePerSample().
3770 /// * `Helper MakeNew(void *newResult, std::string_view variation = "nominal")`: if implemented, it enables varying
3771 /// the action's result with VariationsFor(). It takes a type-erased new result that can be safely cast to a
3772 /// `std::shared_ptr<Result_t> *` (a pointer to shared pointer) and should be used as the action's output result.
3773 /// The function optionally takes the name of the current variation which could be useful in customizing its behaviour.
3774 ///
3775 /// In case Book is called without specifying column types as template arguments, corresponding typed code will be just-in-time compiled
3776 /// by RDataFrame. In that case the Helper class needs to be known to the ROOT interpreter.
3777 ///
3778 /// This action is *lazy*: upon invocation of this method the calculation is booked but not executed. Also see RResultPtr.
3779 ///
3780 /// ### Examples
3781 /// See [this tutorial](https://root.cern/doc/master/df018__customActions_8C.html) for an example implementation of an action helper.
3782 ///
3783 /// It is also possible to inspect the code used by built-in RDataFrame actions at ActionHelpers.hxx.
3784 ///
3785 // clang-format on
3786 template <typename FirstColumn = RDFDetail::RInferredType, typename... OtherColumns, typename Helper>
3788 {
3789 using HelperT = std::decay_t<Helper>;
3790 // TODO add more static sanity checks on Helper
3792 static_assert(std::is_base_of<AH, HelperT>::value && std::is_convertible<HelperT *, AH *>::value,
3793 "Action helper of type T must publicly inherit from ROOT::Detail::RDF::RActionImpl<T>");
3794
3795 auto hPtr = std::make_shared<HelperT>(std::forward<Helper>(helper));
3796 auto resPtr = hPtr->GetResultPtr();
3797
3798 if (std::is_same<FirstColumn, RDFDetail::RInferredType>::value && columns.empty()) {
3799 return CallCreateActionWithoutColsIfPossible<HelperT>(resPtr, hPtr, TTraits::TypeList<FirstColumn>{});
3800 } else {
3801 return CreateAction<RDFInternal::ActionTags::Book, FirstColumn, OtherColumns...>(columns, resPtr, hPtr,
3802 fProxiedPtr, columns.size());
3803 }
3804 }
3805
3806
3807 // clang-format off
3808 ////////////////////////////////////////////////////////////////////////////
3809 /// \brief Execute a user-defined reduce operation on the values of a column.
3810 /// \tparam F The type of the reduce callable. Automatically deduced.
3811 /// \tparam T The type of the column to apply the reduction to. Automatically deduced.
3812 /// \param[in] f A callable with signature `T(T,T)`
3813 /// \param[in] columnName The column to be reduced. If omitted, the first default column is used instead.
3814 /// \return the reduced quantity wrapped in a ROOT::RDF:RResultPtr.
3815 ///
3816 /// A reduction takes two values of a column and merges them into one (e.g.
3817 /// by summing them, taking the maximum, etc). This action performs the
3818 /// specified reduction operation on all processed column values, returning
3819 /// a single value of the same type. The callable f must satisfy the general
3820 /// requirements of a *processing function* besides having signature `T(T,T)`
3821 /// where `T` is the type of column columnName.
3822 ///
3823 /// The returned reduced value of each thread (e.g. the initial value of a sum) is initialized to a
3824 /// default-constructed T object. This is commonly expected to be the neutral/identity element for the specific
3825 /// reduction operation `f` (e.g. 0 for a sum, 1 for a product). If a default-constructed T does not satisfy this
3826 /// requirement, users should explicitly specify an initialization value for T by calling the appropriate `Reduce`
3827 /// overload.
3828 ///
3829 /// ### Example usage:
3830 /// ~~~{.cpp}
3831 /// auto sumOfIntCol = d.Reduce([](int x, int y) { return x + y; }, "intCol");
3832 /// ~~~
3833 ///
3834 /// This action is *lazy*: upon invocation of this method the calculation is
3835 /// booked but not executed. Also see RResultPtr.
3836 // clang-format on
3837 template <typename F, typename T = typename TTraits::CallableTraits<F>::ret_type>
3838 RResultPtr<T> Reduce(F f, std::string_view columnName = "")
3839 {
3840 static_assert(
3841 std::is_default_constructible<T>::value,
3842 "reduce object cannot be default-constructed. Please provide an initialisation value (redIdentity)");
3843 return Reduce(std::move(f), columnName, T());
3844 }
3845
3846 ////////////////////////////////////////////////////////////////////////////
3847 /// \brief Execute a user-defined reduce operation on the values of a column.
3848 /// \tparam F The type of the reduce callable. Automatically deduced.
3849 /// \tparam T The type of the column to apply the reduction to. Automatically deduced.
3850 /// \param[in] f A callable with signature `T(T,T)`
3851 /// \param[in] columnName The column to be reduced. If omitted, the first default column is used instead.
3852 /// \param[in] redIdentity The reduced object of each thread is initialized to this value.
3853 /// \return the reduced quantity wrapped in a RResultPtr.
3854 ///
3855 /// ### Example usage:
3856 /// ~~~{.cpp}
3857 /// auto sumOfIntColWithOffset = d.Reduce([](int x, int y) { return x + y; }, "intCol", 42);
3858 /// ~~~
3859 /// See the description of the first Reduce overload for more information.
3860 template <typename F, typename T = typename TTraits::CallableTraits<F>::ret_type>
3861 RResultPtr<T> Reduce(F f, std::string_view columnName, const T &redIdentity)
3862 {
3863 return Aggregate(f, f, columnName, redIdentity);
3864 }
3865
3866 /// \}
3867 // End of the doxygen group for user-defined actions
3868
3869private:
3870 template <typename F, typename DefineType, typename RetType = typename TTraits::CallableTraits<F>::ret_type>
3871 std::enable_if_t<std::is_default_constructible<RetType>::value, RInterface<Proxied>>
3872 DefineImpl(std::string_view name, F &&expression, const ColumnNames_t &columns, const std::string &where)
3873 {
3874 if (where.compare(0, 8, "Redefine") != 0) { // not a Redefine
3878 } else {
3882 }
3883
3884 using ArgTypes_t = typename TTraits::CallableTraits<F>::arg_types;
3885 using ColTypesTmp_t = typename RDFInternal::RemoveFirstParameterIf<
3886 std::is_same<DefineType, RDFDetail::ExtraArgsForDefine::Slot>::value, ArgTypes_t>::type;
3887 using ColTypes_t = typename RDFInternal::RemoveFirstTwoParametersIf<
3888 std::is_same<DefineType, RDFDetail::ExtraArgsForDefine::SlotAndEntry>::value, ColTypesTmp_t>::type;
3889
3890 constexpr auto nColumns = ColTypes_t::list_size;
3891
3892 const auto validColumnNames = GetValidatedColumnNames(nColumns, columns);
3893 CheckAndFillDSColumns(validColumnNames, ColTypes_t());
3894
3895 // Declare return type to the interpreter, for future use by jitted actions
3896 auto retTypeName = RDFInternal::TypeID2TypeName(typeid(RetType));
3897 if (retTypeName.empty()) {
3898 // The type is not known to the interpreter.
3899 // We must not error out here, but if/when this column is used in jitted code
3900 const auto demangledType = RDFInternal::DemangleTypeIdName(typeid(RetType));
3901 retTypeName = "CLING_UNKNOWN_TYPE_" + demangledType;
3902 }
3903
3904 using NewCol_t = RDFDetail::RDefine<F, DefineType>;
3905 auto newColumn = std::make_shared<NewCol_t>(name, retTypeName, std::forward<F>(expression), validColumnNames,
3907
3909 newCols.AddDefine(std::move(newColumn));
3910
3911 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
3912
3913 return newInterface;
3914 }
3915
3916 // This overload is chosen when the callable passed to Define or DefineSlot returns void.
3917 // It simply fires a compile-time error. This is preferable to a static_assert in the main `Define` overload because
3918 // this way compilation of `Define` has no way to continue after throwing the error.
3919 template <typename F, typename DefineType, typename RetType = typename TTraits::CallableTraits<F>::ret_type,
3920 bool IsFStringConv = std::is_convertible<F, std::string>::value,
3921 bool IsRetTypeDefConstr = std::is_default_constructible<RetType>::value>
3922 std::enable_if_t<!IsFStringConv && !IsRetTypeDefConstr, RInterface<Proxied>>
3923 DefineImpl(std::string_view, F, const ColumnNames_t &, const std::string &)
3924 {
3925 static_assert(std::is_default_constructible<typename TTraits::CallableTraits<F>::ret_type>::value,
3926 "Error in `Define`: type returned by expression is not default-constructible");
3927 return *this; // never reached
3928 }
3929
3930 ////////////////////////////////////////////////////////////////////////////
3931 /// \brief Implementation of DefinePerSample and RedefinePerSample (non-jitted).
3932 template <typename F, typename RetType_t = typename TTraits::CallableTraits<F>::ret_type>
3933 RInterface<Proxied> DefinePerSampleImpl(std::string_view name, F expression, bool redefine)
3934 {
3935 if (!redefine) {
3936 RDFInternal::CheckValidCppVarName(name, "DefinePerSample");
3939 } else {
3943 }
3944
3945 auto retTypeName = RDFInternal::TypeID2TypeName(typeid(RetType_t));
3946 if (retTypeName.empty()) {
3947 // The type is not known to the interpreter.
3948 // We must not error out here, but if/when this column is used in jitted code
3949 const auto demangledType = RDFInternal::DemangleTypeIdName(typeid(RetType_t));
3950 retTypeName = "CLING_UNKNOWN_TYPE_" + demangledType;
3951 }
3952
3953 auto newColumn =
3954 std::make_shared<RDFDetail::RDefinePerSample<F>>(name, retTypeName, std::move(expression), *fLoopManager);
3955
3957 newCols.AddDefine(std::move(newColumn));
3958 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
3959 return newInterface;
3960 }
3961
3962 ////////////////////////////////////////////////////////////////////////////
3963 /// \brief Implementation of DefinePerSample and RedefinePerSample (jitted).
3964 RInterface<Proxied> DefinePerSampleJitImpl(std::string_view name, std::string_view expression, bool redefine)
3965 {
3966 // these checks must be done before jitting lest we throw exceptions in jitted code
3967 if (!redefine) {
3968 RDFInternal::CheckValidCppVarName(name, redefine ? "RedefinePerSample" : "DefinePerSample");
3971 } else {
3975 }
3976
3977 auto jittedDefine = RDFInternal::BookDefinePerSampleJit(name, expression, *fLoopManager, fColRegister);
3978
3980 newCols.AddDefine(std::move(jittedDefine));
3981
3982 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
3983
3984 return newInterface;
3985 }
3986
3987 ////////////////////////////////////////////////////////////////////////////
3988 /// \brief Implementation of cache.
3989 template <typename... ColTypes, std::size_t... S>
3990 RInterface<RLoopManager> CacheImpl(const ColumnNames_t &columnList, std::index_sequence<S...>)
3991 {
3992 const auto columnListWithoutSizeColumns = RDFInternal::FilterArraySizeColNames(columnList, "Snapshot");
3993
3994 // Check at compile time that the columns types are copy constructible
3995 constexpr bool areCopyConstructible =
3996 RDFInternal::TEvalAnd<std::is_copy_constructible<ColTypes>::value...>::value;
3997 static_assert(areCopyConstructible, "Columns of a type which is not copy constructible cannot be cached yet.");
3998
3999 RDFInternal::CheckTypesAndPars(sizeof...(ColTypes), columnListWithoutSizeColumns.size());
4000
4001 auto colHolders = std::make_tuple(Take<ColTypes>(columnListWithoutSizeColumns[S])...);
4002 auto ds = std::make_unique<RLazyDS<ColTypes...>>(
4003 std::make_pair(columnListWithoutSizeColumns[S], std::get<S>(colHolders))...);
4004
4005 RInterface<RLoopManager> cachedRDF(std::make_shared<RLoopManager>(std::move(ds), columnListWithoutSizeColumns));
4006
4007 return cachedRDF;
4008 }
4009
4010 template <bool IsSingleColumn, typename F>
4012 VaryImpl(const std::vector<std::string> &colNames, F &&expression, const ColumnNames_t &inputColumns,
4013 const std::vector<std::string> &variationTags, std::string_view variationName)
4014 {
4015 using F_t = std::decay_t<F>;
4016 using ColTypes_t = typename TTraits::CallableTraits<F_t>::arg_types;
4017 using RetType = typename TTraits::CallableTraits<F_t>::ret_type;
4018 constexpr auto nColumns = ColTypes_t::list_size;
4019
4020 SanityChecksForVary<RetType>(colNames, variationTags, variationName);
4021
4022 const auto validColumnNames = GetValidatedColumnNames(nColumns, inputColumns);
4023 CheckAndFillDSColumns(validColumnNames, ColTypes_t{});
4024
4025 auto retTypeName = RDFInternal::TypeID2TypeName(typeid(RetType));
4026 if (retTypeName.empty()) {
4027 // The type is not known to the interpreter, but we don't want to error out
4028 // here, rather if/when this column is used in jitted code, so we inject a broken but telling type name.
4029 const auto demangledType = RDFInternal::DemangleTypeIdName(typeid(RetType));
4030 retTypeName = "CLING_UNKNOWN_TYPE_" + demangledType;
4031 }
4032
4033 auto variation = std::make_shared<RDFInternal::RVariation<F_t, IsSingleColumn>>(
4034 colNames, variationName, std::forward<F>(expression), variationTags, retTypeName, fColRegister, *fLoopManager,
4035 validColumnNames);
4036
4038 newCols.AddVariation(std::move(variation));
4039
4040 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newCols));
4041
4042 return newInterface;
4043 }
4044
4045 RInterface<Proxied> JittedVaryImpl(const std::vector<std::string> &colNames, std::string_view expression,
4046 const std::vector<std::string> &variationTags, std::string_view variationName,
4047 bool isSingleColumn)
4048 {
4049 R__ASSERT(!variationTags.empty() && "Must have at least one variation.");
4050 R__ASSERT(!colNames.empty() && "Must have at least one varied column.");
4051 R__ASSERT(!variationName.empty() && "Must provide a variation name.");
4052
4053 for (auto &colName : colNames) {
4054 RDFInternal::CheckValidCppVarName(colName, "Vary");
4057 }
4058 RDFInternal::CheckValidCppVarName(variationName, "Vary");
4059
4060 // when varying multiple columns, they must be different columns
4061 if (colNames.size() > 1) {
4062 std::set<std::string> uniqueCols(colNames.begin(), colNames.end());
4063 if (uniqueCols.size() != colNames.size())
4064 throw std::logic_error("A column name was passed to the same Vary invocation multiple times.");
4065 }
4066
4067 // Cannot vary different input column types, assume the first
4068 auto varyColType = GetColumnType(colNames[0]);
4069 auto jittedVariation =
4070 RDFInternal::BookVariationJit(colNames, variationName, variationTags, expression, *fLoopManager,
4071 GetDataSource(), fColRegister, isSingleColumn, varyColType);
4072
4074 newColRegister.AddVariation(std::move(jittedVariation));
4075
4076 RInterface<Proxied> newInterface(fProxiedPtr, *fLoopManager, std::move(newColRegister));
4077
4078 return newInterface;
4079 }
4080
4081 template <typename Helper, typename ActionResultType>
4082 auto CallCreateActionWithoutColsIfPossible(const std::shared_ptr<ActionResultType> &resPtr,
4083 const std::shared_ptr<Helper> &hPtr,
4085 -> decltype(hPtr->Exec(0u), RResultPtr<ActionResultType>{})
4086 {
4087 return CreateAction<RDFInternal::ActionTags::Book>(/*columns=*/{}, resPtr, hPtr, fProxiedPtr, 0u);
4088 }
4089
4090 template <typename Helper, typename ActionResultType, typename... Others>
4091 RResultPtr<ActionResultType>
4092 CallCreateActionWithoutColsIfPossible(const std::shared_ptr<ActionResultType> &,
4093 const std::shared_ptr<Helper>& /*hPtr*/,
4094 Others...)
4095 {
4096 throw std::logic_error(std::string("An action was booked with no input columns, but the action requires "
4097 "columns! The action helper type was ") +
4098 typeid(Helper).name());
4099 return {};
4100 }
4101
4102protected:
4103 RInterface(const std::shared_ptr<Proxied> &proxied, RLoopManager &lm,
4104 const RDFInternal::RColumnRegister &colRegister)
4105 : RInterfaceBase(lm, colRegister), fProxiedPtr(proxied)
4106 {
4107 }
4108
4109 const std::shared_ptr<Proxied> &GetProxiedPtr() const { return fProxiedPtr; }
4110};
4111
4112} // namespace RDF
4113
4114} // namespace ROOT
4115
4116#endif // ROOT_RDF_INTERFACE
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
Basic types used by ROOT and required by TInterpreter.
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:61
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:85
#define X(type, name)
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
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
Base class for action helpers, see RInterface::Book() for more information.
implementation of FilterAvailable and FilterMissing operations
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
Helper class that provides the operation graph nodes.
A RDataFrame node that produces a result.
Definition RAction.hxx:53
A binder for user-defined columns, variations and aliases.
std::vector< std::string_view > GenerateColumnNames() const
Return the list of the names of the defined columns (Defines + Aliases).
void AddVariation(std::shared_ptr< RVariationBase > variation)
Register a new systematic variation.
void AddDefine(std::shared_ptr< RDFDetail::RDefineBase > column)
Add a new defined column.
RDFDetail::RDefineBase * GetDefine(std::string_view colName) const
Return the RDefine for the requested column name, or nullptr.
The dataset specification for RDataFrame.
virtual const std::vector< std::string > & GetColumnNames() const =0
Returns a reference to the collection of the dataset's column names.
The base public interface to the RDataFrame federation of classes.
std::string GetColumnType(std::string_view column)
Return the type of a given column as a string.
ColumnNames_t GetValidatedColumnNames(const unsigned int nColumns, const ColumnNames_t &columns)
ColumnNames_t GetColumnTypeNamesList(const ColumnNames_t &columnList)
std::shared_ptr< ROOT::Detail::RDF::RLoopManager > fLoopManager
< The RLoopManager at the root of this computation graph. Never null.
RResultPtr< ActionResultType > CreateAction(const ColumnNames_t &columns, const std::shared_ptr< ActionResultType > &r, const std::shared_ptr< HelperArgType > &helperArg, const std::shared_ptr< RDFNode > &proxiedPtr, const int=-1)
Create RAction object, return RResultPtr for the action Overload for the case in which all column typ...
RDataSource * GetDataSource() const
void CheckAndFillDSColumns(ColumnNames_t validCols, TTraits::TypeList< ColumnTypes... > typeList)
void CheckIMTDisabled(std::string_view callerName)
ColumnNames_t GetColumnNames()
Returns the names of the available columns.
RDFDetail::RLoopManager * GetLoopManager() const
RDFInternal::RColumnRegister fColRegister
Contains the columns defined up to this node.
The public interface to the RDataFrame federation of classes.
RResultPtr< RDisplay > Display(const ColumnNames_t &columnList, size_t nRows=5, size_t nMaxCollectionElements=10)
Provides a representation of the columns in the dataset.
RResultPtr<::TProfile > Profile1D(const TProfile1DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view wName)
Fill and return a one-dimensional profile (lazy action).
RResultPtr<::THnD > HistoND(const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName="")
Fill and return an N-dimensional histogram (lazy action).
RResultPtr<::TGraph > Graph(std::string_view x="", std::string_view y="")
Fill and return a TGraph object (lazy action).
RInterface< Proxied > Vary(std::string_view colName, F &&expression, const ColumnNames_t &inputColumns, const std::vector< std::string > &variationTags, std::string_view variationName="")
Register systematic variations for a single existing column using custom variation tags.
RInterface< Proxied > Vary(const std::vector< std::string > &colNames, std::string_view expression, std::size_t nVariations, std::string_view variationName)
Register systematic variations for multiple existing columns using auto-generated variation tags.
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::uint64_t nNormalBins, std::pair< double, double > interval, std::string_view vName, std::string_view wName)
Fill and return a one-dimensional RHist with weights (lazy action).
RInterface(const RInterface &)=default
Copy-ctor for RInterface.
RResultPtr< RDFDetail::MaxReturnType_t< T > > Max(std::string_view columnName="")
Return the maximum of processed column values (lazy action).
auto CallCreateActionWithoutColsIfPossible(const std::shared_ptr< ActionResultType > &resPtr, const std::shared_ptr< Helper > &hPtr, TTraits::TypeList< RDFDetail::RInferredType >) -> decltype(hPtr->Exec(0u), RResultPtr< ActionResultType >{})
RInterface(RInterface &&)=default
Move-ctor for RInterface.
RInterface< Proxied > Vary(std::string_view colName, std::string_view expression, const std::vector< std::string > &variationTags, std::string_view variationName="")
Register systematic variations for a single existing column using custom variation tags.
RInterface< RDFDetail::RFilter< F, Proxied > > Filter(F f, const std::initializer_list< std::string > &columns)
Append a filter to the call graph.
RInterface< RLoopManager > Cache(std::initializer_list< std::string > columnList)
Save selected columns in memory.
RInterface< Proxied > Vary(std::string_view colName, F &&expression, const ColumnNames_t &inputColumns, std::size_t nVariations, std::string_view variationName="")
Register systematic variations for a single existing column using auto-generated variation tags.
RInterface< Proxied > Vary(std::initializer_list< std::string > colNames, std::string_view expression, std::size_t nVariations, std::string_view variationName)
Register systematic variations for multiple existing columns using auto-generated variation tags.
RResultPtr< RInterface< RLoopManager > > Snapshot(std::string_view treename, std::string_view filename, const ColumnNames_t &columnList, const RSnapshotOptions &options=RSnapshotOptions())
RResultPtr<::TProfile2D > Profile2D(const TProfile2DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view v3Name, std::string_view wName)
Fill and return a two-dimensional profile (lazy action).
RResultPtr< RInterface< RLoopManager > > Snapshot(std::string_view treename, std::string_view filename, std::string_view columnNameRegexp="", const RSnapshotOptions &options=RSnapshotOptions())
Save selected columns to disk, in a new TTree or RNTuple treename in file filename.
RResultPtr< RDisplay > Display(const ColumnNames_t &columnList, size_t nRows=5, size_t nMaxCollectionElements=10)
Provides a representation of the columns in the dataset.
RResultPtr< RDisplay > Display(std::initializer_list< std::string > columnList, size_t nRows=5, size_t nMaxCollectionElements=10)
Provides a representation of the columns in the dataset.
RInterface(const std::shared_ptr< RLoopManager > &proxied)
Build a RInterface from a RLoopManager.
RResultPtr<::THnSparseD > HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName="")
Fill and return a sparse N-dimensional histogram (lazy action).
RInterface< Proxied > Redefine(std::string_view name, F expression, const ColumnNames_t &columns={})
Overwrite the value and/or type of an existing column.
std::shared_ptr< Proxied > fProxiedPtr
Smart pointer to the graph node encapsulated by this RInterface.
RInterface< Proxied > Vary(const std::vector< std::string > &colNames, std::string_view expression, const std::vector< std::string > &variationTags, std::string_view variationName)
Register systematic variations for multiple existing columns using custom variation tags.
RInterface< Proxied > Vary(std::string_view colName, std::string_view expression, std::size_t nVariations, std::string_view variationName="")
Register systematic variations for a single existing column using auto-generated variation tags.
RResultPtr<::TH1D > Histo1D(std::string_view vName)
Fill and return a one-dimensional histogram with the values of a column (lazy action).
RInterface< RDFDetail::RRange< Proxied > > Range(unsigned int begin, unsigned int end, unsigned int stride=1)
Creates a node that filters entries based on range: [begin, end).
RInterface< Proxied > DefinePerSampleImpl(std::string_view name, F expression, bool redefine)
Implementation of DefinePerSample and RedefinePerSample (non-jitted).
RResultPtr< typename std::decay_t< Helper >::Result_t > Book(Helper &&helper, const ColumnNames_t &columns={})
Book execution of a custom action using a user-defined helper object.
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::vector< ROOT::Experimental::RAxisVariant > axes, const ColumnNames_t &columnList)
Fill and return an RHist (lazy action).
RResultPtr<::TProfile > Profile1D(const TProfile1DModel &model, std::string_view v1Name="", std::string_view v2Name="")
Fill and return a one-dimensional profile (lazy action).
const std::shared_ptr< Proxied > & GetProxiedPtr() const
RResultPtr<::TH1D > Histo1D(const TH1DModel &model={"", "", 128u, 0., 0.})
Fill and return a one-dimensional histogram with the weighted values of a column (lazy action).
RResultPtr< T > Reduce(F f, std::string_view columnName="")
Execute a user-defined reduce operation on the values of a column.
RResultPtr< double > Median(std::string_view columnName="")
Return the median value of the input column (lazy action).
RResultPtr< T > Reduce(F f, std::string_view columnName, const T &redIdentity)
Execute a user-defined reduce operation on the values of a column.
RInterface< Proxied > Vary(const std::vector< std::string > &colNames, F &&expression, const ColumnNames_t &inputColumns, const std::vector< std::string > &variationTags, std::string_view variationName)
Register systematic variations for multiple existing columns using custom variation tags.
RInterface< RLoopManager > Cache(const ColumnNames_t &columnList)
Save selected columns in memory.
RResultPtr<::TH1D > Histo1D(const TH1DModel &model, std::string_view vName, std::string_view wName)
Fill and return a one-dimensional histogram with the weighted values of a column (lazy action).
RResultPtr< RDisplay > Display(std::string_view columnNameRegexp="", size_t nRows=5, size_t nMaxCollectionElements=10)
Provides a representation of the columns in the dataset.
RInterface & operator=(const RInterface &)=default
Copy-assignment operator for RInterface.
RInterface< Proxied > VaryImpl(const std::vector< std::string > &colNames, F &&expression, const ColumnNames_t &inputColumns, const std::vector< std::string > &variationTags, std::string_view variationName)
RResultPtr<::THnSparseD > HistoNSparseD(const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName="")
Fill and return a sparse N-dimensional histogram (lazy action).
RInterface< Proxied > Define(std::string_view name, std::string_view expression)
Define a new column.
RInterface< RDFDetail::RFilterWithMissingValues< Proxied > > FilterAvailable(std::string_view column)
Discard entries with missing values.
std::enable_if_t<!IsFStringConv &&!IsRetTypeDefConstr, RInterface< Proxied > > DefineImpl(std::string_view, F, const ColumnNames_t &, const std::string &)
RInterface< Proxied > Redefine(std::string_view name, std::string_view expression)
Overwrite the value and/or type of an existing column.
std::vector< std::string > GetFilterNames()
Returns the names of the filters created.
RInterface< RLoopManager > Cache(std::string_view columnNameRegexp="")
Save selected columns in memory.
RResultPtr<::TH1D > Histo1D(const TH1DModel &model={"", "", 128u, 0., 0.}, std::string_view vName="")
Fill and return a one-dimensional histogram with the values of a column (lazy action).
RInterface< Proxied > Vary(std::initializer_list< std::string > colNames, F &&expression, const ColumnNames_t &inputColumns, const std::vector< std::string > &variationTags, std::string_view variationName)
Register systematic variations for multiple existing columns using custom variation tags.
RResultPtr<::TH3D > Histo3D(const TH3DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view v3Name, std::string_view wName)
Fill and return a three-dimensional histogram (lazy action).
RInterface< Proxied > DefinePerSampleJitImpl(std::string_view name, std::string_view expression, bool redefine)
Implementation of DefinePerSample and RedefinePerSample (jitted).
void LoadValuesCustom(const ColumnNames_t &columnList, const std::vector< std::size_t > &vecSizes, float vecPadding, ROOT::RVecF &dest)
friend class RDFInternal::GraphDrawing::GraphCreatorHelper
RResultPtr< ROOT::Experimental::RHistEngine< BinContentType > > Hist(std::shared_ptr< ROOT::Experimental::RHistEngine< BinContentType > > h, const ColumnNames_t &columnList)
Fill the provided RHistEngine (lazy action).
RInterface< RLoopManager > CacheImpl(const ColumnNames_t &columnList, std::index_sequence< S... >)
Implementation of cache.
RResultPtr<::TProfile2D > Profile2D(const TProfile2DModel &model, std::string_view v1Name="", std::string_view v2Name="", std::string_view v3Name="")
Fill and return a two-dimensional profile (lazy action).
RInterface< RDFDetail::RFilter< F, Proxied > > Filter(F f, std::string_view name)
Append a filter to the call graph.
RResultPtr< U > Aggregate(AccFun aggregator, MergeFun merger, std::string_view columnName="")
Execute a user-defined accumulation operation on the processed column values in each processing slot.
RInterface< Proxied > RedefinePerSample(std::string_view name, std::string_view expression)
Redefine an existing column that is updated when the input sample changes.
std::enable_if_t< std::is_default_constructible< RetType >::value, RInterface< Proxied > > DefineImpl(std::string_view name, F &&expression, const ColumnNames_t &columns, const std::string &where)
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::shared_ptr< ROOT::Experimental::RHist< BinContentType > > h, const ColumnNames_t &columnList)
Fill the provided RHist (lazy action).
RInterface(const std::shared_ptr< Proxied > &proxied, RLoopManager &lm, const RDFInternal::RColumnRegister &colRegister)
RResultPtr< COLL > Take(std::string_view column="")
Return a collection of values of a column (lazy action, returns a std::vector by default).
RInterface< Proxied > Alias(std::string_view alias, std::string_view columnName)
Allow to refer to a column with a different name.
RResultPtr< RDFDetail::MinReturnType_t< T > > Min(std::string_view columnName="")
Return the minimum of processed column values (lazy action).
RResultPtr< RInterface< RLoopManager > > Snapshot(std::string_view treename, std::string_view filename, const ColumnNames_t &columnList, const RSnapshotOptions &options=RSnapshotOptions())
Save selected columns to disk, in a new TTree or RNTuple treename in file filename.
RResultPtr< ROOT::Experimental::RHistEngine< BinContentType > > Hist(std::shared_ptr< ROOT::Experimental::RHistEngine< BinContentType > > h, const ColumnNames_t &columnList, std::string_view wName)
Fill the provided RHistEngine with weights (lazy action).
RResultPtr< RCutFlowReport > Report()
Gather filtering statistics.
RResultPtr<::TH3D > Histo3D(const TH3DModel &model)
RResultPtr<::TH3D > Histo3D(const TH3DModel &model, std::string_view v1Name="", std::string_view v2Name="", std::string_view v3Name="")
Fill and return a three-dimensional histogram (lazy action).
RResultPtr<::TH1D > Histo1D(std::string_view vName, std::string_view wName)
Fill and return a one-dimensional histogram with the weighted values of a column (lazy action).
RInterface< Proxied > DefinePerSample(std::string_view name, std::string_view expression)
Define a new column that is updated when the input sample changes.
RInterface< Proxied > DefineSlotEntry(std::string_view name, F expression, const ColumnNames_t &columns={})
Define a new column with a value dependent on the processing slot and the current entry.
RResultPtr< std::decay_t< T > > Fill(T &&model, const ColumnNames_t &columnList)
Return an object of type T on which T::Fill will be called once per event (lazy action).
RInterface< Proxied > DefineSlot(std::string_view name, F expression, const ColumnNames_t &columns={})
Define a new column with a value dependent on the processing slot.
RInterface< RDFDetail::RFilterWithMissingValues< Proxied > > FilterMissing(std::string_view column)
Keep only the entries that have missing values.
RResultPtr< TStatistic > Stats(std::string_view value="")
Return a TStatistic object, filled once per event (lazy action).
RInterface< Proxied > JittedVaryImpl(const std::vector< std::string > &colNames, std::string_view expression, const std::vector< std::string > &variationTags, std::string_view variationName, bool isSingleColumn)
RInterface< Proxied > DefaultValueFor(std::string_view column, const T &defaultValue)
In case the value in the given column is missing, provide a default value.
RResultPtr< TStatistic > Stats(std::string_view value, std::string_view weight)
Return a TStatistic object, filled once per event (lazy action).
RResultPtr<::TProfile2D > Profile2D(const TProfile2DModel &model)
Fill and return a two-dimensional profile (lazy action).
RInterface< Proxied > RedefineSlot(std::string_view name, F expression, const ColumnNames_t &columns={})
Overwrite the value and/or type of an existing column.
void Foreach(F f, const ColumnNames_t &columns={})
Execute a user-defined function on each entry (instant action).
RResultPtr<::TH2D > Histo2D(const TH2DModel &model, std::string_view v1Name="", std::string_view v2Name="")
Fill and return a two-dimensional histogram (lazy action).
RResultPtr< ActionResultType > CallCreateActionWithoutColsIfPossible(const std::shared_ptr< ActionResultType > &, const std::shared_ptr< Helper > &, Others...)
RInterface< Proxied > Define(std::string_view name, F expression, const ColumnNames_t &columns={})
Define a new column.
void ForeachSlot(F f, const ColumnNames_t &columns={})
Execute a user-defined function requiring a processing slot index on each entry (instant action).
RResultPtr<::TGraphAsymmErrors > GraphAsymmErrors(std::string_view x="", std::string_view y="", std::string_view exl="", std::string_view exh="", std::string_view eyl="", std::string_view eyh="")
Fill and return a TGraphAsymmErrors object (lazy action).
RResultPtr< U > Aggregate(AccFun aggregator, MergeFun merger, std::string_view columnName, const U &aggIdentity)
Execute a user-defined accumulation operation on the processed column values in each processing slot.
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::shared_ptr< ROOT::Experimental::RHist< BinContentType > > h, const ColumnNames_t &columnList, std::string_view wName)
Fill the provided RHist with weights (lazy action).
RResultPtr<::TProfile > Profile1D(const TProfile1DModel &model)
Fill and return a one-dimensional profile (lazy action).
RResultPtr< RInterface< RLoopManager > > Snapshot(std::string_view treename, std::string_view filename, std::initializer_list< std::string > columnList, const RSnapshotOptions &options=RSnapshotOptions())
Save selected columns to disk, in a new TTree or RNTuple treename in file filename.
RInterface & operator=(RInterface &&)=default
Move-assignment operator for RInterface.
RResultPtr<::TH2D > Histo2D(const TH2DModel &model)
RResultPtr< double > Mean(std::string_view columnName="")
Return the mean of processed column values (lazy action).
RInterface< RDFDetail::RFilter< F, Proxied > > Filter(F f, const ColumnNames_t &columns={}, std::string_view name="")
Append a filter to the call graph.
RInterface< RLoopManager > Cache(const ColumnNames_t &columnList)
Save selected columns in memory.
RInterface< Proxied > DefinePerSample(std::string_view name, F expression)
Define a new column that is updated when the input sample changes.
RInterface< Proxied > Vary(std::initializer_list< std::string > colNames, F &&expression, const ColumnNames_t &inputColumns, std::size_t nVariations, std::string_view variationName)
Register systematic variations for for multiple existing columns using custom variation tags.
RInterface< RDFDetail::RRange< Proxied > > Range(unsigned int end)
Creates a node that filters entries based on range.
RInterface< Proxied > RedefineSlotEntry(std::string_view name, F expression, const ColumnNames_t &columns={})
Overwrite the value and/or type of an existing column.
RInterface< RDFDetail::RJittedFilter > Filter(std::string_view expression, std::string_view name="")
Append a filter to the call graph.
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::uint64_t nNormalBins, std::pair< double, double > interval, std::string_view vName)
Fill and return a one-dimensional RHist (lazy action).
RResultPtr< ROOT::Experimental::RHist< BinContentType > > Hist(std::vector< ROOT::Experimental::RAxisVariant > axes, const ColumnNames_t &columnList, std::string_view wName)
Fill and return an RHist with weights (lazy action).
RResultPtr< ULong64_t > Count()
Return the number of entries processed (lazy action).
RInterface< Proxied > RedefinePerSample(std::string_view name, F expression)
Redefine an existing column that is updated when the input sample changes.
RResultPtr<::TH2D > Histo2D(const TH2DModel &model, std::string_view v1Name, std::string_view v2Name, std::string_view wName)
Fill and return a weighted two-dimensional histogram (lazy action).
RInterface< Proxied > Vary(const std::vector< std::string > &colNames, F &&expression, const ColumnNames_t &inputColumns, std::size_t nVariations, std::string_view variationName)
Register systematic variations for multiple existing columns using auto-generated tags.
RResultPtr<::THnD > HistoND(const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName="")
Fill and return an N-dimensional histogram (lazy action).
RResultPtr< double > StdDev(std::string_view columnName="")
Return the unbiased standard deviation of processed column values (lazy action).
RResultPtr< RDFDetail::SumReturnType_t< T > > Sum(std::string_view columnName="", const RDFDetail::SumReturnType_t< T > &initValue=RDFDetail::SumReturnType_t< T >{})
Return the sum of processed column values (lazy action).
A RDataSource implementation which is built on top of result proxies.
Smart pointer for the return type of actions.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
typename RemoveFirstParameter< T >::type RemoveFirstParameter_t
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
@ kAllAxes
Definition TH1.h:126
Statistical variable, defined by its mean and variance (RMS).
Definition TStatistic.h:33
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
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)
const std::type_info & TypeName2TypeID(const std::string &name)
Return the type_info associated to a name.
Definition RDFUtils.cxx:86
void ChangeEmptyEntryRange(const ROOT::RDF::RNode &node, std::pair< ULong64_t, ULong64_t > &&newRange)
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 ChangeSpec(const ROOT::RDF::RNode &node, ROOT::RDF::Experimental::RDatasetSpec &&spec)
Changes the input dataset specification of an RDataFrame.
const std::vector< std::string > & GetTopLevelFieldNames(const ROOT::RDF::RDataSource &ds)
Definition RDFUtils.cxx:669
void RemoveDuplicates(ColumnNames_t &columnNames)
std::shared_ptr< RNodeBase > UpcastNode(std::shared_ptr< RNodeBase > ptr)
std::string TypeID2TypeName(const std::type_info &id)
Returns the name of a type starting from its type_info An empty string is returned in case of failure...
Definition RDFUtils.cxx:200
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 GetDataSourceLabel(const ROOT::RDF::RNode &node)
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.
void TriggerRun(ROOT::RDF::RNode node)
Trigger the execution of an RDataFrame computation graph.
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::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 LoadCustomValues(ROOT::RDF::RNode &node, const std::vector< std::string > &cols, const std::vector< std::size_t > &vecSizes, float vecPadding, ROOT::RVecF &dest)
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2RVec=true)
Return a string containing the type of the given branch.
Definition RDFUtils.cxx:339
void SetTTreeLifeline(ROOT::RDF::RNode &node, std::any lifeline)
void RemoveRNTupleSubfields(ColumnNames_t &columnNames)
std::vector< std::pair< std::uint64_t, std::uint64_t > > GetDatasetGlobalClusterBoundaries(const RNode &node)
Retrieve the cluster boundaries for each cluster in the dataset, across files, with a global offset.
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.
void WarnHist()
Warn once about experimental filling of RHist.
Definition RDFUtils.cxx:55
void CheckForDuplicateSnapshotColumns(const ColumnNames_t &cols)
ColumnNames_t ConvertRegexToColumns(const ColumnNames_t &colNames, std::string_view columnNameRegexp, std::string_view callerName)
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.
void ChangeBeginAndEndEntries(const RNode &node, Long64_t begin, Long64_t end)
RInterface<::ROOT::Detail::RDF::RNodeBase > RNode
std::vector< std::string > ColumnNames_t
ROOT type_traits extensions.
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition TROOT.cxx:617
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:673
@ kError
An error.
void DisableImplicitMT()
Disables the implicit multi-threading in ROOT (see EnableImplicitMT).
Definition TROOT.cxx:659
A special bin content type to compute the bin error in weighted filling.
type is TypeList if MustRemove is false, otherwise it is a TypeList with the first type removed
Definition Utils.hxx:156
Tag to let data sources use the native data type when creating a column reader.
Definition Utils.hxx:332
A collection of options to steer the creation of the dataset on disk through Snapshot().
A struct which stores some basic parameters of a TH1D.
A struct which stores some basic parameters of a TH2D.
A struct which stores some basic parameters of a TH3D.
A struct which stores some basic parameters of a THnD.
A struct which stores some basic parameters of a THnSparseD.
A struct which stores some basic parameters of a TProfile.
A struct which stores some basic parameters of a TProfile2D.
Lightweight storage for a collection of types.