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