Logo ROOT  
Reference Guide
rf402_datahandling.py File Reference


namespace  rf402_datahandling

Detailed Description

View in nbviewer Open in SWAN
Data and categories: tools for manipulation of (un)binned datasets

from __future__ import print_function
import ROOT
import math
# WVE Add reduction by range
# Binned (RooDataHist) and unbinned datasets (RooDataSet) share
# many properties and inherit from a common abstract base class
# (RooAbsData), provides an interface for all operations
# that can be performed regardless of the data format
x = ROOT.RooRealVar("x", "x", -10, 10)
y = ROOT.RooRealVar("y", "y", 0, 40)
c = ROOT.RooCategory("c", "c")
c.defineType("Plus", +1)
c.defineType("Minus", -1)
# Basic operations on unbinned datasetss
# --------------------------------------------------------------
# ROOT.RooDataSet is an unbinned dataset (a collection of points in
# N-dimensional space)
d = ROOT.RooDataSet("d", "d", {x, y, c})
# Unlike ROOT.RooAbsArgs (ROOT.RooAbsPdf, ROOT.RooFormulaVar,....) datasets are not attached to
# the variables they are constructed from. Instead they are attached to an internal
# clone of the supplied set of arguments
# Fill d with dummy values
for i in range(1000):
x.setVal(i / 50 - 10)
y.setVal(math.sqrt(1.0 * i))
if i % 2:
# We must explicitly refer to x,y, here to pass the values because
# d is not linked to them (as explained above)
if i < 3:
print(x, y, c)
d.add({x, y, c})
# The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
# supplied in the constructor
row = d.get()
# Get with an argument loads a specific data point in row and returns
# a pointer to row argset. get() always returns the same pointer, unless
# an invalid row number is specified. In that case a null ptr is returned
# Reducing, appending and merging
# -------------------------------------------------------------
# The reduce() function returns a dataset which is a subset of the
# original
print("\n >> d1 has only columns x,c")
d1 = d.reduce({x, c})
print("\n >> d2 has only column y")
d2 = d.reduce({y})
print("\n >> d3 has only the points with y>5.17")
d3 = d.reduce("y>5.17")
print("\n >> d4 has only columns x, for data points with y>5.17")
d4 = d.reduce({x, c}, "y>5.17")
# The merge() function adds two data set column-wise
print("\n >> merge d2(y) with d1(x,c) to form d1(x,c,y)")
# The append() function addes two datasets row-wise
print("\n >> append data points of d3 to d1")
# Operations on binned datasets
# ---------------------------------------------------------
# A binned dataset can be constructed empty, an unbinned dataset, or
# from a ROOT native histogram (TH1,2,3)
print(">> construct dh (binned) from d(unbinned) but only take the x and y dimensions, ")
print(">> the category 'c' will be projected in the filling process")
# The binning of real variables (like x,y) is done using their fit range
# 'get/setRange()' and number of specified fit bins 'get/setBins()'.
# Category dimensions of binned datasets get one bin per defined category
# state
dh = ROOT.RooDataHist("dh", "binned version of d", {x, y}, d)
yframe = y.frame(Bins=10, Title="Operations on binned datasets")
dh.plotOn(yframe) # plot projection of 2D binned data on y
# Examine the statistics of a binned dataset
print(">> number of bins in dh : ", dh.numEntries())
print(">> sum of weights in dh : ", dh.sum(False))
# accounts for bin volume
print(">> integral over histogram: ", dh.sum(True))
# Locate a bin from a set of coordinates and retrieve its properties
print(">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) bin center:")
# load bin center coordinates in internal buffer
dh.get({x, y}).Print("v")
print(" weight = ", dh.weight()) # return weight of last loaded coordinates
# Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
# All reduce() methods are interfaced in RooAbsData. All reduction techniques
# demonstrated on unbinned datasets can be applied to binned datasets as
# well.
print(">> Creating 1-dimensional projection on y of dh for bins with x>0")
dh2 = dh.reduce({y}, "x>0")
# Add dh2 to yframe and redraw
dh2.plotOn(yframe, LineColor="r", MarkerColor="r")
# Saving and loading from file
# -------------------------------------------------------
# Datasets can be persisted with ROOT I/O
print("\n >> Persisting d via ROOT I/O")
f = ROOT.TFile("rf402_datahandling.root", "RECREATE")
# To read back in future session:
# > ROOT.TFile f("rf402_datahandling.root")
# > d = (ROOT.RooDataSet*) f.FindObject("d")
c = ROOT.TCanvas("rf402_datahandling", "rf402_datahandling", 600, 600)
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(std::ostream &os, const OptionType &opt)
February 2018
Clemens Lange, Wouter Verkerke (C++ version)

Definition in file rf402_datahandling.py.