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