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