Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf402_datahandling.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Data and categories: tools for manipulation of (un)binned datasets
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11from __future__ import print_function
12import ROOT
13import math
14
15# WVE Add reduction by range
16
17# Binned (RooDataHist) and unbinned datasets (RooDataSet) share
18# many properties and inherit from a common abstract base class
19# (RooAbsData), provides an interface for all operations
20# that can be performed regardless of the data format
21
22x = ROOT.RooRealVar("x", "x", -10, 10)
23y = ROOT.RooRealVar("y", "y", 0, 40)
24c = ROOT.RooCategory("c", "c")
25c.defineType("Plus", +1)
26c.defineType("Minus", -1)
27
28# Basic operations on unbinned datasetss
29# --------------------------------------------------------------
30
31# ROOT.RooDataSet is an unbinned dataset (a collection of points in
32# N-dimensional space)
33d = ROOT.RooDataSet("d", "d", {x, y, c})
34
35# Unlike ROOT.RooAbsArgs (ROOT.RooAbsPdf, ROOT.RooFormulaVar,....) datasets are not attached to
36# the variables they are constructed from. Instead they are attached to an internal
37# clone of the supplied set of arguments
38
39# Fill d with dummy values
40for i in range(1000):
41 x.setVal(i / 50 - 10)
42 y.setVal(math.sqrt(1.0 * i))
43 if i % 2:
44 c.setLabel("Plus")
45 else:
46 c.setLabel("Minus")
47
48 # We must explicitly refer to x,y, here to pass the values because
49 # d is not linked to them (as explained above)
50 if i < 3:
51 print(x, y, c)
52 print(type(x))
53 d.add({x, y, c})
54
55d.Print("v")
56print("")
57
58# The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
59# supplied in the constructor
60row = d.get()
61row.Print("v")
62print("")
63
64# Get with an argument loads a specific data point in row and returns
65# a pointer to row argset. get() always returns the same pointer, unless
66# an invalid row number is specified. In that case a null ptr is returned
67d.get(900).Print("v")
68print("")
69
70# Reducing, appending and merging
71# -------------------------------------------------------------
72
73# The reduce() function returns a dataset which is a subset of the
74# original
75print("\n >> d1 has only columns x,c")
76d1 = d.reduce({x, c})
77d1.Print("v")
78
79print("\n >> d2 has only column y")
80d2 = d.reduce({y})
81d2.Print("v")
82
83print("\n >> d3 has only the points with y>5.17")
84d3 = d.reduce("y>5.17")
85d3.Print("v")
86
87print("\n >> d4 has only columns x, for data points with y>5.17")
88d4 = d.reduce({x, c}, "y>5.17")
89d4.Print("v")
90
91# The merge() function adds two data set column-wise
92print("\n >> merge d2(y) with d1(x,c) to form d1(x,c,y)")
93d1.merge(d2)
94d1.Print("v")
95
96# The append() function addes two datasets row-wise
97print("\n >> append data points of d3 to d1")
98d1.append(d3)
99d1.Print("v")
100
101# Operations on binned datasets
102# ---------------------------------------------------------
103
104# A binned dataset can be constructed empty, an unbinned dataset, or
105# from a ROOT native histogram (TH1,2,3)
106
107print(">> construct dh (binned) from d(unbinned) but only take the x and y dimensions, ")
108print(">> the category 'c' will be projected in the filling process")
109
110# The binning of real variables (like x,y) is done using their fit range
111# 'get/setRange()' and number of specified fit bins 'get/setBins()'.
112# Category dimensions of binned datasets get one bin per defined category
113# state
114x.setBins(10)
115y.setBins(10)
116dh = ROOT.RooDataHist("dh", "binned version of d", {x, y}, d)
117dh.Print("v")
118
119yframe = y.frame(Bins=10, Title="Operations on binned datasets")
120dh.plotOn(yframe) # plot projection of 2D binned data on y
121
122# Examine the statistics of a binned dataset
123print(">> number of bins in dh : ", dh.numEntries())
124print(">> sum of weights in dh : ", dh.sum(False))
125# accounts for bin volume
126print(">> integral over histogram: ", dh.sum(True))
127
128# Locate a bin from a set of coordinates and retrieve its properties
129x.setVal(0.3)
130y.setVal(20.5)
131print(">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) bin center:")
132# load bin center coordinates in internal buffer
133dh.get({x, y}).Print("v")
134print(" weight = ", dh.weight()) # return weight of last loaded coordinates
135
136# Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
137#
138# All reduce() methods are interfaced in RooAbsData. All reduction techniques
139# demonstrated on unbinned datasets can be applied to binned datasets as
140# well.
141print(">> Creating 1-dimensional projection on y of dh for bins with x>0")
142dh2 = dh.reduce({y}, "x>0")
143dh2.Print("v")
144
145# Add dh2 to yframe and redraw
146dh2.plotOn(yframe, LineColor="r", MarkerColor="r")
147
148# Saving and loading from file
149# -------------------------------------------------------
150
151# Datasets can be persisted with ROOT I/O
152print("\n >> Persisting d via ROOT I/O")
153f = ROOT.TFile("rf402_datahandling.root", "RECREATE")
154d.Write()
155f.ls()
156
157# To read back in future session:
158# > ROOT.TFile f("rf402_datahandling.root")
159# > d = (ROOT.RooDataSet*) f.FindObject("d")
160
161c = ROOT.TCanvas("rf402_datahandling", "rf402_datahandling", 600, 600)
162ROOT.gPad.SetLeftMargin(0.15)
163yframe.GetYaxis().SetTitleOffset(1.4)
164yframe.Draw()
165
166c.SaveAs("rf402_datahandling.png")
int type
Definition TGX11.cxx:121