ROOT   Reference Guide
rf404_categories.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Data and categories: working with ROOT.RooCategory objects to describe discrete variables
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11from __future__ import print_function
12import ROOT
13
14
15# Construct a category with labels
16# --------------------------------------------
17
18# Define a category with labels only
19tagCat = ROOT.RooCategory("tagCat", "Tagging category")
20tagCat.defineType("Lepton")
21tagCat.defineType("Kaon")
22tagCat.defineType("NetTagger-1")
23tagCat.defineType("NetTagger-2")
24tagCat.Print()
25
26# Construct a category with labels and indices
27# ------------------------------------------------
28
29# Define a category with explicitly numbered states
30b0flav = ROOT.RooCategory("b0flav", "B0 flavour eigenstate")
31b0flav.defineType("B0", -1)
32b0flav.defineType("B0bar", 1)
33b0flav.Print()
34
35# Generate dummy data for tabulation demo
36# ------------------------------------------------
37
38# Generate a dummy dataset
39x = ROOT.RooRealVar("x", "x", 0, 10)
40data = ROOT.RooPolynomial("p", "p", x).generate(
41 ROOT.RooArgSet(x, b0flav, tagCat), 10000)
42
43# Print tables of category contents of datasets
44# --------------------------------------------------
45
46# Tables are equivalent of plots for categories
47btable = data.table(b0flav)
48btable.Print()
49btable.Print("v")
50
51# Create table for subset of events matching cut expression
52ttable = data.table(tagCat, "x>8.23")
53ttable.Print()
54ttable.Print("v")
55
56# Create table for all (tagCat x b0flav) state combinations
57bttable = data.table(ROOT.RooArgSet(tagCat, b0flav))
58bttable.Print("v")
59
60# Retrieve number of events from table
61# Number can be non-integer if source dataset has weighed events
62nb0 = btable.get("B0")
63print("Number of events with B0 flavor is ", nb0)
64
65# Retrieve fraction of events with "Lepton" tag
66fracLep = ttable.getFrac("Lepton")
67print("Fraction of events tagged with Lepton tag is ", fracLep)
68
69# Defining ranges for plotting, fitting on categories
70# ------------------------------------------------------------------------------------------------------
71
72# Define named range as comma separated list of labels
73tagCat.setRange("good", "Lepton,Kaon")
74
75# Or add state names one by one