Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf408_RDataFrameToRooFit.py
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 July 2021
13## \author Harshal Shende, Stephan Hageboeck (C++ version)
14
15import ROOT
16import math
17
18
19# Set up
20# ------------------------
21
22# We enable implicit parallelism, so RDataFrame runs in parallel.
23ROOT.ROOT.EnableImplicitMT()
24
25# We create an RDataFrame with two columns filled with 2 million random numbers.
26d = ROOT.RDataFrame(2000000)
27dd = d.Define("x", "gRandom->Uniform(-5., 5.)").Define("y", "gRandom->Gaus(1., 3.)")
28
29
30# We create RooFit variables that will represent the dataset.
31x = ROOT.RooRealVar("x", "x", -5.0, 5.0)
32y = ROOT.RooRealVar("y", "y", -50.0, 50.0)
33x.setBins(10)
34y.setBins(20)
35
36
37# Booking the creation of RooDataSet / RooDataHist in RDataFrame
38# ----------------------------------------------------------------
39
40# Method 1:
41# We directly book the RooDataSetMaker action.
42# We need to pass
43# - the RDataFrame column types as template parameters
44# - the constructor arguments for RooDataSet (they follow the same syntax as the usual RooDataSet constructors)
45# - the column names that RDataFrame should fill into the dataset
46#
47# NOTE: RDataFrame columns are matched to RooFit variables by position, *not by name*!
48rooDataSet = dd.Book(
49 ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Title of dataset", ROOT.RooArgSet(x, y))), ("x", "y")
50)
51
52
53# Method 2:
54# We first declare the RooDataHistMaker
55rdhMaker = ROOT.RooDataSetHelper("dataset", "Title of dataset", ROOT.RooArgSet(x, y))
56
57# Then, we move it into the RDataFrame action:
58rooDataHist = dd.Book(ROOT.std.move(rdhMaker), ("x", "y"))
59
60
61# Run it and inspect the results
62# -------------------------------
63
64# Let's inspect the dataset / datahist.
65# Note that the first time we touch one of those objects, the RDataFrame event loop will run.
66for data in [rooDataSet, rooDataHist]:
67 data.Print()
68 for i in range(data.numEntries(), 20):
69 print("(")
70 for var in data.get(i):
71 print("{0:.3f}".format(var.getVal()))
72 print(")\tweight= {0:<10}".format(data.weight()))
73
74 print("mean(x) = {0:.3f}".format(data.mean(x)) + "\tsigma(x) = {0:.3f}".format(math.sqrt(data.moment(x, 2.0))))
75 print("mean(y) = {0:.3f}".format(data.mean(y)) + "\tsigma(y) = {0:.3f}\n".format(math.sqrt(data.moment(y, 2.0))))
ROOT's RDataFrame offers a high level interface for analyses of data stored in TTree,...