Rf 4 0 4_Categories

Data and categories: working with ROOT.RooCategory objects to describe discrete variables

Author: Clemens Lange, Wouter Verkerke (C++ version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:02 AM.

In [ ]:
from __future__ import print_function
import ROOT

Construct a category with labels

Define a category with labels only

In [ ]:
tagCat = ROOT.RooCategory("tagCat", "Tagging category")
tagCat.defineType("Lepton")
tagCat.defineType("Kaon")
tagCat.defineType("NetTagger-1")
tagCat.defineType("NetTagger-2")
tagCat.Print()

Construct a category with labels and indices

Define a category with explicitly numbered states

In [ ]:
b0flav = ROOT.RooCategory("b0flav", "B0 flavour eigenstate", {"B0": -1, "B0bar": 1})
b0flav.Print()

Generate dummy data for tabulation demo

Generate a dummy dataset

In [ ]:
x = ROOT.RooRealVar("x", "x", 0, 10)
data = ROOT.RooPolynomial("p", "p", x).generate({x, b0flav, tagCat}, 10000)

Tables are equivalent of plots for categories

In [ ]:
btable = data.table(b0flav)
btable.Print()
btable.Print("v")

Create table for subset of events matching cut expression

In [ ]:
ttable = data.table(tagCat, "x>8.23")
ttable.Print()
ttable.Print("v")

Create table for all (tagCat x b0flav) state combinations

In [ ]:
bttable = data.table({tagCat, b0flav})
bttable.Print("v")

Retrieve number of events from table Number can be non-integer if source dataset has weighed events

In [ ]:
nb0 = btable.get("B0")
print("Number of events with B0 flavor is ", nb0)

Retrieve fraction of events with "Lepton" tag

In [ ]:
fracLep = ttable.getFrac("Lepton")
print("Fraction of events tagged with Lepton tag is ", fracLep)

Defining ranges for plotting, fitting on categories

Define named range as comma separated list of labels

In [ ]:
tagCat.setRange("good", "Lepton,Kaon")

Or add state names one by one

In [ ]:
tagCat.addToRange("soso", "NetTagger-1")
tagCat.addToRange("soso", "NetTagger-2")

Use category range in dataset reduction specification

In [ ]:
goodData = data.reduce(CutRange="good")
goodData.table(tagCat).Print("v")