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