Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf404_categories.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Data and categories: working with RooCategory objects to describe discrete variables

#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooPolynomial.h"
#include "RooCategory.h"
#include "Roo1DTable.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include <iostream>
using namespace RooFit;
{
// C o n s t r u c t a c a t e g o r y w i t h l a b e l s
// ----------------------------------------------------------------
// Define a category with labels only
RooCategory tagCat("tagCat", "Tagging category");
tagCat.defineType("Lepton");
tagCat.defineType("Kaon");
tagCat.defineType("NetTagger-1");
tagCat.defineType("NetTagger-2");
tagCat.Print();
// C o n s t r u c t a c a t e g o r y w i t h l a b e l s a n d i n d i c e s
// ----------------------------------------------------------------------------------------
// Define a category with explicitly numbered states
RooCategory b0flav("b0flav", "B0 flavour eigenstate");
b0flav["B0"] = -1;
b0flav["B0bar"] = 1;
// Print it in "verbose" mode to see all states.
b0flav.Print("V");
// Alternatively, define many states at once. The function takes
// a map with std::string --> index mapping.
RooCategory largeCat("largeCat", "A category with many states");
largeCat.defineTypes({
{"A", 0}, {"b", 2}, {"c", 8}, {"dee", 4},
{"F", 133}, {"g", 15}, {"H", -20}
});
// I t e r a t e, q u e r y a n d s e t s t a t e s
// --------------------------------------------------------
// One can iterate through the {index,name} pair of category objects
std::cout << "\nThis is the for loop over states of 'largeCat':";
for (const auto& idxAndName : largeCat)
std::cout << "\n\t" << idxAndName.first << "\t" << idxAndName.second;
std::cout << '\n' << std::endl;
// To ask whether a state is valid use:
std::cout << "Has label 'A': " << largeCat.hasLabel("A");
std::cout << "\nHas index '-20': " << largeCat.hasIndex(-20);
// To retrieve names or state numbers:
std::cout << "\nLabel corresponding to '2' is " << largeCat.lookupName(2);
std::cout << "\nIndex corresponding to 'A' is " << largeCat.lookupIndex("A");
// To get the current state:
std::cout << "\nCurrent index is " << largeCat.getCurrentIndex();
std::cout << "\nCurrent label is " << largeCat.getCurrentLabel();
std::cout << std::endl;
// To set the state, use one of the two:
largeCat.setIndex(8);
largeCat.setLabel("c");
// G e n e r a t e d u m m y d a t a f o r t a b u l a t i o n d e m o
// ----------------------------------------------------------------------------
// Generate a dummy dataset
RooRealVar x("x", "x", 0, 10);
std::unique_ptr<RooDataSet> data{RooPolynomial("p", "p", x).generate({x, b0flav, tagCat}, 10000)};
// P r i n t t a b l e s o f c a t e g o r y c o n t e n t s o f d a t a s e t s
// ------------------------------------------------------------------------------------------
// Tables are equivalent of plots for categories
Roo1DTable *btable = data->table(b0flav);
btable->Print();
btable->Print("v");
// Create table for subset of events matching cut expression
Roo1DTable *ttable = data->table(tagCat, "x>8.23");
ttable->Print();
ttable->Print("v");
// Create table for all (tagCat x b0flav) state combinations
Roo1DTable *bttable = data->table(RooArgSet(tagCat, b0flav));
bttable->Print("v");
// Retrieve number of events from table
// Number can be non-integer if source dataset has weighed events
double nb0 = btable->get("B0");
std::cout << "Number of events with B0 flavor is " << nb0 << std::endl;
// Retrieve fraction of events with "Lepton" tag
double fracLep = ttable->getFrac("Lepton");
std::cout << "Fraction of events tagged with Lepton tag is " << fracLep << std::endl;
// D e f i n i n g r a n g e s f o r p l o t t i n g , f i t t i n g o n c a t e g o r i e s
// ------------------------------------------------------------------------------------------------------
// 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
std::unique_ptr<RooAbsData> goodData{data->reduce(CutRange("good"))};
static_cast<RooDataSet&>(*goodData).table(tagCat)->Print("v");
}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
One-dimensional table.
Definition Roo1DTable.h:23
double get(const char *label, bool silent=false) const
Return the table entry named 'label'.
double getFrac(const char *label, bool silent=false) const
Return the fraction of entries in the table contained in the slot named 'label'.
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition Roo1DTable.h:50
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
RooFit::OwningPtr< RooDataSet > generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={})
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Object to represent discrete states.
Definition RooCategory.h:28
Container class to hold unbinned data.
Definition RooDataSet.h:34
RooPolynomial implements a polynomial p.d.f of the form.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
RooCmdArg CutRange(const char *rangeName)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
RooCategory::tagCat = Lepton(idx = 0)
--- RooAbsArg ---
Value State: clean
Shape State: clean
Attributes:
Address: 0x7ffe1eb75ec8
Clients:
Servers:
Proxies:
--- RooAbsCategory ---
Value = -1 "B0)
Possible states:
B0 -1
B0bar 1
This is the for loop over states of 'largeCat':
A 0
F 133
H -20
b 2
c 8
dee 4
g 15
Has label 'A': 1
Has index '-20': 1
Label corresponding to '2' is b
Index corresponding to 'A' is 0
Current index is 0
Current label is A
Roo1DTable::b0flav = (B0=5058,B0bar=4942)
Table b0flav : pData
+-------+------+
| B0 | 5058 |
| B0bar | 4942 |
+-------+------+
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 |
+-------------+-----+
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 |
+---------------------+------+
Number of events with B0 flavor is 5058
Fraction of events tagged with Lepton tag is 0.257224
Table tagCat : pData
+-------------+------+
| Lepton | 2494 |
| Kaon | 2546 |
| NetTagger-1 | 0 |
| NetTagger-2 | 0 |
+-------------+------+
Date
July 2008
Author
Wouter Verkerke

Definition in file rf404_categories.C.