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 Tuesday, March 19, 2024 at 07:16 PM.
from __future__ import print_function
import ROOT
Define a category with labels only
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
b0flav = ROOT.RooCategory("b0flav", "B0 flavour eigenstate", {"B0": -1, "B0bar": 1})
b0flav.Print()
RooCategory::b0flav = B0(idx = -1)
Generate a dummy dataset
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
btable = data.table(b0flav)
btable.Print()
btable.Print("v")
Roo1DTable::b0flav = (B0=5058,B0bar=4942) Table b0flav : pData +-------+------+ | B0 | 5058 | | B0bar | 4942 | +-------+------+
Create table for subset of events matching cut expression
ttable = data.table(tagCat, "x>8.23")
ttable.Print()
ttable.Print("v")
[#1] INFO:InputArguments -- The formula cutVar claims to use the variables (x,b0flav,tagCat) but only (x) seem to be in use. inputs: x>8.23 Roo1DTable::tagCat = (Lepton=454,Kaon=450,NetTagger-1=432,NetTagger-2=429) Table tagCat : pData(x>8.23) +-------------+-----+ | Lepton | 454 | | Kaon | 450 | | NetTagger-1 | 432 | | NetTagger-2 | 429 | +-------------+-----+
Create table for all (tagCat x b0flav) state combinations
bttable = data.table({tagCat, b0flav})
bttable.Print("v")
Table (tagCat x b0flav) : pData +---------------------+------+ | {Lepton;B0} | 1302 | | {Kaon;B0} | 1232 | | {NetTagger-1;B0} | 1242 | | {NetTagger-2;B0} | 1282 | | {Lepton;B0bar} | 1192 | | {Kaon;B0bar} | 1314 | | {NetTagger-1;B0bar} | 1208 | | {NetTagger-2;B0bar} | 1228 | +---------------------+------+
Retrieve number of events from table Number can be non-integer if source dataset has weighed events
nb0 = btable.get("B0")
print("Number of events with B0 flavor is ", nb0)
Number of events with B0 flavor is 5058.0
Retrieve fraction of events with "Lepton" tag
fracLep = ttable.getFrac("Lepton")
print("Fraction of events tagged with Lepton tag is ", fracLep)
Fraction of events tagged with Lepton tag is 0.2572237960339943
Define named range as comma separated list of labels
tagCat.setRange("good", "Lepton,Kaon")
Or add state names one by one
tagCat.addToRange("soso", "NetTagger-1")
tagCat.addToRange("soso", "NetTagger-2")
Use category range in dataset reduction specification
goodData = data.reduce(CutRange="good")
goodData.table(tagCat).Print("v")
Table tagCat : pData +-------------+------+ | Lepton | 2494 | | Kaon | 2546 | | NetTagger-1 | 0 | | NetTagger-2 | 0 | +-------------+------+