Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df039_RResultPtr_basics.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook -nodraw
4/// Usage of RResultPtr.
5///
6/// This tutorial illustrates what is the difference between lazy and immediate results and how to use either of them in RDataFrame.
7/// "Lazy" or deferred results are only produced once they are accessed. This allows for declaring multiple desired results, and producing
8/// them in a single run of the event loop.
9///
10/// \macro_code
11/// \macro_output
12///
13/// \date 2025
14/// \author Stephan Hageboeck (CERN)
15
16#include <ROOT/RDataFrame.hxx>
17#include <ROOT/RDFHelpers.hxx>
18
19#include <iostream>
20
21// A function that adds a "lazy" histogram to a computation graph.
22// The event loop will not run if only the RResultPtr is declared.
24 return rdf.Histo1D({"Histo2", "Histogram running later", 10, 0, 20}, {"x"});
25}
26
27// A function that immediately produces a result.
28std::shared_ptr<TH1D> histoNow(ROOT::RDF::RNode & rdf) {
29 auto histo = rdf.Histo1D({"Histo2", "Histogram running later", 10, 0, 20}, {"x"});
30 return histo.GetSharedPtr();
31}
32
33
35{
36 // Create a simple dataframe that fills event numbers into a histogram.
38 auto rdf = bare_rdf.Define("x", [&](unsigned long long entry) -> unsigned int {
39 if (entry == 0) std::cout << "Event loop is running\n";
40 return entry;
41 }, {"rdfentry_"});
42
43 // Book a histogram action. This will be stored as RResultPtr.
44 // The action won't run yet.
45 ROOT::RDF::RResultPtr<TH1D> histo1 = rdf.Histo1D({"Histo1", "Histogram", 10, 0, 10}, {"x"});
46 std::cout << "Declared histo1\n";
47
48 // When RDF results are declared in functions, one has to choose if one wants run it to immediately or lazily.
49 // To run the event loop in a lazy fashion, return RResultPtr. This is equivalent to histo1 above, but happens
50 // inside a function.
53 std::cout << "Declared histo2\n";
54
55 // If the function should produce the result immediately, a shared_ptr to the underlying result should be returned.
56 std::shared_ptr<TH1D> histo3 = histoNow(rNode);
57 std::cout << "Declared histo3\n";
58}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
The public interface to the RDataFrame federation of classes.
Smart pointer for the return type of actions.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
RNode AsRNode(NodeType node)
Cast a RDataFrame node to the common type ROOT::RDF::RNode.