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 ROOT::RDataFrame d(2000000);
47 auto dd = d.Define("x", [](){ return gRandom->Uniform(-5., 5.); })
48 .Define("y", [](){ return gRandom->Gaus(1., 3.); });
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 auto rooDataSet = dd.Book<double, double>(
72 RooDataSetHelper("dataset", // Name
73 "Title of dataset", // Title
74 RooArgSet(x, y) // Variables in this dataset
75 ),
76 {"x", "y"} // Column names in RDataFrame.
77 );
78
79
80 // Method 2:
81 // ---------
82 // We first declare the RooDataHistHelper
83 RooDataHistHelper rdhMaker{"datahist", // Name
84 "Title of data hist", // Title
85 RooArgSet(x, y) // Variables in this dataset
86 };
87
88 // Then, we move it into an RDataFrame action:
89 auto rooDataHist = dd.Book<double, double>(std::move(rdhMaker), {"x", "y"});
90
91
92
93 // Run it and inspect the results
94 // -------------------------------
95
96 // Let's inspect the dataset / datahist.
97 // Note that the first time we touch one of those objects, the RDataFrame event loop will run.
98 printData(*rooDataSet);
99 printData(*rooDataHist);
100}
101
102int main() {
104 return 0;
105}
int main()
Definition Prototype.cxx:12
#define d(i)
Definition RSha256.hxx:102
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
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.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
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:274
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition TRandom.cxx:672
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17