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