# rf406_cattocatfuncs¶

Data and categories: demonstration of discrete-discrete (invertable) functions

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 Wednesday, November 30, 2022 at 11:23 AM.

In [1]:
import ROOT

Welcome to JupyROOT 6.27/01


## Construct two categories¶

Define a category with labels only

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

RooCategory::tagCat = Lepton(idx = 0)



Define a category with explicitly numbered states

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

RooCategory::b0flav = B0(idx = -1)



Construct a dummy dataset with random values of tagCat and b0flav

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


## Create a cat -> cat mapping category¶

A RooMappedCategory is category.category mapping function based on string expression The constructor takes an input category an a default state name to which unassigned states are mapped

In [5]:
tcatType = ROOT.RooMappedCategory("tcatType", "tagCat type", tagCat, "Cut based")


Enter fully specified state mappings

In [6]:
tcatType.map("Lepton", "Cut based")
tcatType.map("Kaon", "Cut based")

Out[6]:
False

Enter a wilcard expression mapping

In [7]:
tcatType.map("NetTagger*", "Neural Network")

Out[7]:
False

Make a table of the mapped category state multiplicit in data

In [8]:
mtable = data.table(tcatType)
mtable.Print("v")

  Table tcatType : pData
+----------------+------+
|      Cut based | 5040 |
| Neural Network | 4960 |
+----------------+------+



## Create a cat X cat product category¶

A SUPER-category is 'product' of lvalue categories. The state names of a super category is a composite of the state labels of the input categories

In [9]:
b0Xtcat = ROOT.RooSuperCategory("b0Xtcat", "b0flav X tagCat", {b0flav, tagCat})


Make a table of the product category state multiplicity in data

In [10]:
stable = data.table(b0Xtcat)
stable.Print("v")

  Table b0Xtcat : pData
+---------------------+------+
|         {B0;Lepton} | 1302 |
|      {B0bar;Lepton} | 1192 |
|           {B0;Kaon} | 1232 |
|        {B0bar;Kaon} | 1314 |
|    {B0;NetTagger-1} | 1242 |
| {B0bar;NetTagger-1} | 1208 |
|    {B0;NetTagger-2} | 1282 |
| {B0bar;NetTagger-2} | 1228 |
+---------------------+------+



Since the super category is an lvalue, is explicitly possible

In [11]:
b0Xtcat.setLabel("{B0bar;Lepton}")

Out[11]:
False

A MULTI-category is a 'product' of any category (function). The state names of a super category is a composite of the state labels of the input categories

In [12]:
b0Xttype = ROOT.RooMultiCategory("b0Xttype", "b0flav X tagType", {b0flav, tcatType})


Make a table of the product category state multiplicity in data

In [13]:
xtable = data.table(b0Xttype)
xtable.Print("v")

  Table b0Xttype : pData
+------------------------+------+
|         {B0;Cut based} | 2534 |
|      {B0bar;Cut based} | 2506 |
|    {B0;Neural Network} | 2524 |
| {B0bar;Neural Network} | 2436 |
+------------------------+------+