Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf405_realtocatfuncs.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Data and categories: demonstration of real-discrete mapping functions
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
14
15
16# Define pdf in x, sample dataset in x
17# ------------------------------------------------------------------------
18
19# Define a dummy PDF in x
20x = ROOT.RooRealVar("x", "x", 0, 10)
21a = ROOT.RooArgusBG("a", "argus(x)", x, 10.0, -1.0)
22
23# Generate a dummy dataset
24data = a.generate({x}, 10000)
25
26# Create a threshold real -> cat function
27# --------------------------------------------------------------------------
28
29# A RooThresholdCategory is a category function that maps regions in a real-valued
30# input observable observables to state names. At construction time a 'default'
31# state name must be specified to which all values of x are mapped that are not
32# otherwise assigned
33xRegion = ROOT.RooThresholdCategory("xRegion", "region of x", x, "Background")
34
35# Specify thresholds and state assignments one-by-one.
36# Each statement specifies that all values _below_ the given value
37# (and above any lower specified threshold) are mapped to the
38# category state with the given name
39#
40# Background | SideBand | Signal | SideBand | Background
41# 4.23 5.23 8.23 9.23
42xRegion.addThreshold(4.23, "Background")
43xRegion.addThreshold(5.23, "SideBand")
44xRegion.addThreshold(8.23, "Signal")
45xRegion.addThreshold(9.23, "SideBand")
46
47# Use threshold function to plot data regions
48# ----------------------------------------------
49
50# Add values of threshold function to dataset so that it can be used as
51# observable
52data.addColumn(xRegion)
53
54# Make plot of data in x
55xframe = x.frame(Title="Demo of threshold and binning mapping functions")
56data.plotOn(xframe)
57
58# Use calculated category to select sideband data
59data.plotOn(xframe, Cut="xRegion==xRegion::SideBand", MarkerColor="r", LineColor="r")
60
61# Create a binning real -> cat function
62# ----------------------------------------------------------------------
63
64# A RooBinningCategory is a category function that maps bins of a (named) binning definition
65# in a real-valued input observable observables to state names. The state names are automatically
66# constructed from the variable name, binning name and the bin number. If no binning name
67# is specified the default binning is mapped
68
69x.setBins(10, "coarse")
70xBins = ROOT.RooBinningCategory("xBins", "coarse bins in x", x, "coarse")
71
72# Use binning function for tabulation and plotting
73# -----------------------------------------------------------------------------------------------
74
75# Print table of xBins state multiplicity. Note that xBins does not need to be an observable in data
76# it can be a function of observables in data as well
77xbtable = data.table(xBins)
78xbtable.Print("v")
79
80# Add values of xBins function to dataset so that it can be used as
81# observable
82xb = data.addColumn(xBins)
83
84# Define range "alt" as including bins 1,3,5,7,9
85xb.setRange("alt", "x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9")
86
87# Construct subset of data matching range "alt" but only for the first
88# 5000 events and plot it on the frame
89dataSel = data.reduce(CutRange="alt", EventRange=(0, 5000))
90dataSel.plotOn(xframe, MarkerColor="g", LineColor="g")
91
92c = ROOT.TCanvas("rf405_realtocatfuncs", "rf405_realtocatfuncs", 600, 600)
93xframe.SetMinimum(0.01)
94ROOT.gPad.SetLeftMargin(0.15)
95xframe.GetYaxis().SetTitleOffset(1.4)
96xframe.Draw()
97
98c.SaveAs("rf405_realtocatfuncs.png")