Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf408_RDataFrameToRooFit.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Fill RooDataSet/RooDataHist in RDataFrame.
5///
6/// This tutorial shows how to fill RooFit data classes directly from RDataFrame.
7/// Using two small helpers, we tell RDataFrame where the data has to go.
8///
9/// \macro_code
10/// \macro_output
11///
12/// \date Mar 2021
13/// \author Stephan Hageboeck (CERN)
14
15#include <RooAbsDataHelper.h>
16
17#include <TRandom.h>
18
19/// Print the first few entries and summary statistics.
20void printData(const RooAbsData& data) {
21 std::cout << "\n";
22 data.Print();
23
24 for (int i=0; i < data.numEntries() && i < 20; ++i) {
25 std::cout << "(";
26 for (const auto var : *data.get(i)) {
27 std::cout << std::setprecision(3) << std::right << std::fixed << std::setw(8) << static_cast<const RooAbsReal*>(var)->getVal() << ", ";
28 }
29 std::cout << ")\tweight=" << std::setw(10) << data.weight() << std::endl;
30 }
31
32 // Get the x and y variables from the dataset:
33 const auto & x = static_cast<const RooRealVar&>(*(*data.get())[0]);
34 const auto & y = static_cast<const RooRealVar&>(*(*data.get())[1]);
35
36 std::cout << "mean(x) = " << data.mean(x) << "\tsigma(x) = " << std::sqrt(data.moment(x, 2.))
37 << "\n" << "mean(y) = " << data.mean(y) << "\tsigma(y) = " << std::sqrt(data.moment(y, 2.)) << std::endl;
38}
39
41{
42 // Set up
43 // ------------------------
44
45 // We create an RDataFrame with two columns filled with 2 million random numbers.
46 auto df = ROOT::RDataFrame{2000000}.Define("x", []() { return gRandom->Uniform(-5., 5.); }).Define("y", []() {
47 return gRandom->Gaus(1., 3.);
48 });
49
50
51 // We create RooFit variables that will represent the dataset.
52 RooRealVar x("x", "x", -5., 5.);
53 RooRealVar y("y", "y", -50., 50.);
54 x.setBins(10);
55 y.setBins(20);
56
57
58
59 // Booking the creation of RooDataSet / RooDataHist in RDataFrame
60 // ----------------------------------------------------------------
61
62 // Method 1:
63 // ---------
64 // We directly book the RooDataSetHelper action.
65 // We need to pass
66 // - the RDataFrame column types as template parameters
67 // - the constructor arguments for RooDataSet (they follow the same syntax as the usual RooDataSet constructors)
68 // - the column names that RDataFrame should fill into the dataset
69 //
70 // NOTE: RDataFrame columns are matched to RooFit variables by position, *not by name*!
71 //
72 // The returned object is not yet a RooDataSet, but an RResultPtr that will
73 // be lazy-evaluated once you call GetValue() on it. We will only evaluate
74 // the RResultPtr once all other RDataFrame related actions are declared.
75 // This way we trigger the event loop computation only once, which will
76 // improve the runtime significantly.
77 //
78 // To learn more about lazy actions, see:
79 // https://root.cern/doc/master/classROOT_1_1RDataFrame.html#actions
80 ROOT::RDF::RResultPtr<RooDataSet> rooDataSetResult = df.Book<double, double>(
81 RooDataSetHelper("dataset", // Name
82 "Title of dataset", // Title
83 RooArgSet(x, y) // Variables in this dataset
84 ),
85 {"x", "y"} // Column names in RDataFrame.
86 );
87
88
89 // Method 2:
90 // ---------
91 // We first declare the RooDataHistHelper
92 RooDataHistHelper rdhMaker{"datahist", // Name
93 "Title of data hist", // Title
94 RooArgSet(x, y) // Variables in this dataset
95 };
96
97 // Then, we move it into an RDataFrame action:
98 ROOT::RDF::RResultPtr<RooDataHist> rooDataHistResult = df.Book<double, double>(std::move(rdhMaker), {"x", "y"});
99
100
101 // Run it and inspect the results
102 // -------------------------------
103
104 // At this point, all RDF actions were defined (namely, the `Book`
105 // operations), so we can get values from the RResultPtr objects, triggering
106 // the event loop and getting the actual RooFit data objects.
107 RooDataSet const& rooDataSet = rooDataSetResult.GetValue();
108 RooDataHist const& rooDataHist = rooDataHistResult.GetValue();
109
110 // Let's inspect the dataset / datahist.
111 printData(rooDataSet);
112 printData(rooDataHist);
113}
114
115int main() {
117 return 0;
118}
int main()
Definition Prototype.cxx:12
RooAbsDataHelper< RooDataSet > RooDataSetHelper
Helper for creating a RooDataSet inside RDataFrame.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
RInterface< Proxied, DS_t > Define(std::string_view name, F expression, const ColumnNames_t &columns={})
Define a new column.
Smart pointer for the return type of actions.
const T & GetValue()
Get a const reference to the encapsulated object.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
This is a helper for an RDataFrame action, which fills RooFit data classes.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:40
Container class to hold unbinned data.
Definition RooDataSet.h:34
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:682
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17