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