Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf402_datahandling.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ##
5 ## Data and categories: tools for manipulation of (un)binned datasets
6 ##
7 ## \macro_code
8 ##
9 ## \date February 2018
10 ## \author Clemens Lange, Wouter Verkerke (C++ version)
11 
12 from __future__ import print_function
13 import ROOT
14 import 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 
23 x = ROOT.RooRealVar("x", "x", -10, 10)
24 y = ROOT.RooRealVar("y", "y", 0, 40)
25 c = ROOT.RooCategory("c", "c")
26 c.defineType("Plus", +1)
27 c.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)
34 d = ROOT.RooDataSet("d", "d", ROOT.RooArgSet(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
41 for 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  print(x, y, c)
52  print(type(x))
53  d.add(ROOT.RooArgSet(x, y, c))
54 
55 d.Print("v")
56 print("")
57 
58 # The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
59 # supplied in the constructor
60 row = d.get()
61 row.Print("v")
62 print("")
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
67 d.get(900).Print("v")
68 print("")
69 
70 # Reducing, appending and merging
71 # -------------------------------------------------------------
72 
73 # The reduce() function returns a dataset which is a subset of the
74 # original
75 print("\n >> d1 has only columns x,c")
76 d1 = d.reduce(ROOT.RooArgSet(x, c))
77 d1.Print("v")
78 
79 print("\n >> d2 has only column y")
80 d2 = d.reduce(ROOT.RooArgSet(y))
81 d2.Print("v")
82 
83 print("\n >> d3 has only the points with y>5.17")
84 d3 = d.reduce("y>5.17")
85 d3.Print("v")
86 
87 print("\n >> d4 has only columns x, for data points with y>5.17")
88 d4 = d.reduce(ROOT.RooArgSet(x, c), "y>5.17")
89 d4.Print("v")
90 
91 # The merge() function adds two data set column-wise
92 print("\n >> merge d2(y) with d1(x,c) to form d1(x,c,y)")
93 d1.merge(d2)
94 d1.Print("v")
95 
96 # The append() function addes two datasets row-wise
97 print("\n >> append data points of d3 to d1")
98 d1.append(d3)
99 d1.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 
107 print(">> construct dh (binned) from d(unbinned) but only take the x and y dimensions, ")
108 print(">> 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
114 x.setBins(10)
115 y.setBins(10)
116 dh = ROOT.RooDataHist("dh", "binned version of d", ROOT.RooArgSet(x, y), d)
117 dh.Print("v")
118 
119 yframe = y.frame(ROOT.RooFit.Bins(10), ROOT.RooFit.Title(
120  "Operations on binned datasets"))
121 dh.plotOn(yframe) # plot projection of 2D binned data on y
122 
123 # Examine the statistics of a binned dataset
124 print(">> number of bins in dh : ", dh.numEntries())
125 print(">> sum of weights in dh : ", dh.sum(ROOT.kFALSE))
126 # accounts for bin volume
127 print(">> integral over histogram: ", dh.sum(ROOT.kTRUE))
128 
129 # Locate a bin from a set of coordinates and retrieve its properties
130 x.setVal(0.3)
131 y.setVal(20.5)
132 print(">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) bin center:")
133 # load bin center coordinates in internal buffer
134 dh.get(ROOT.RooArgSet(x, y)).Print("v")
135 print(" 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.
142 print(">> Creating 1-dimensional projection on y of dh for bins with x>0")
143 dh2 = dh.reduce(ROOT.RooArgSet(y), "x>0")
144 dh2.Print("v")
145 
146 # Add dh2 to yframe and redraw
147 dh2.plotOn(yframe, ROOT.RooFit.LineColor(ROOT.kRed),
148  ROOT.RooFit.MarkerColor(ROOT.kRed))
149 
150 # Saving and loading from file
151 # -------------------------------------------------------
152 
153 # Datasets can be persisted with ROOT I/O
154 print("\n >> Persisting d via ROOT I/O")
155 f = ROOT.TFile("rf402_datahandling.root", "RECREATE")
156 d.Write()
157 f.ls()
158 
159 # To read back in future session:
160 # > ROOT.TFile f("rf402_datahandling.root")
161 # > d = (ROOT.RooDataSet*) f.FindObject("d")
162 
163 c = ROOT.TCanvas("rf402_datahandling", "rf402_datahandling", 600, 600)
164 ROOT.gPad.SetLeftMargin(0.15)
165 yframe.GetYaxis().SetTitleOffset(1.4)
166 yframe.Draw()
167 
168 c.SaveAs("rf402_datahandling.png")