rf404_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 Sunday, November 27, 2022 at 11:07 AM.

In [1]:
from __future__ import print_function
import ROOT
Welcome to JupyROOT 6.27/01

Construct a category with labels

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)

Construct a category with labels and indices

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)

Generate dummy data for tabulation demo

Generate a dummy dataset

In [4]:
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 [5]:
btable = data.table(b0flav)
btable.Print()
btable.Print("v")
Roo1DTable::b0flav = (B0=5040,B0bar=4960)

  Table b0flav : pData
  +-------+------+
  |    B0 | 5040 |
  | B0bar | 4960 |
  +-------+------+

Create table for subset of events matching cut expression

In [6]:
ttable = data.table(tagCat, "x>8.23")
ttable.Print()
ttable.Print("v")
[#1] INFO:InputArguments -- The formula cutVar claims to use the variables (x,tagCat,b0flav) but only (x) seem to be in use.
  inputs:         x>8.23
Roo1DTable::tagCat = (Lepton=487,Kaon=433,NetTagger-1=439,NetTagger-2=406)

  Table tagCat : pData(x>8.23)
  +-------------+-----+
  |      Lepton | 487 |
  |        Kaon | 433 |
  | NetTagger-1 | 439 |
  | NetTagger-2 | 406 |
  +-------------+-----+

Create table for all (tagCat x b0flav) state combinations

In [7]:
bttable = data.table({tagCat, b0flav})
bttable.Print("v")
  Table (b0flav x tagCat) : pData
  +---------------------+------+
  |         {B0;Lepton} | 1281 |
  |      {B0bar;Lepton} | 1269 |
  |           {B0;Kaon} | 1253 |
  |        {B0bar;Kaon} | 1255 |
  |    {B0;NetTagger-1} | 1234 |
  | {B0bar;NetTagger-1} | 1219 |
  |    {B0;NetTagger-2} | 1272 |
  | {B0bar;NetTagger-2} | 1217 |
  +---------------------+------+

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

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

Retrieve fraction of events with "Lepton" tag

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

Defining ranges for plotting, fitting on categories

Define named range as comma separated list of labels

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

Or add state names one by one

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

Use category range in dataset reduction specification

In [12]:
goodData = data.reduce(CutRange="good")
goodData.table(tagCat).Print("v")
  Table tagCat : pData
  +-------------+------+
  |      Lepton | 2550 |
  |        Kaon | 2508 |
  | NetTagger-1 |    0 |
  | NetTagger-2 |    0 |
  +-------------+------+