Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf404_categories.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Data and categories: working with RooCategory objects to describe discrete variables
5///
6/// \macro_output
7/// \macro_code
8///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooPolynomial.h"
15#include "RooCategory.h"
16#include "Roo1DTable.h"
17#include "RooGaussian.h"
18#include "RooConstVar.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22#include <iostream>
23using namespace RooFit;
24
26{
27
28 // 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
29 // ----------------------------------------------------------------
30
31 // Define a category with labels only
32 RooCategory tagCat("tagCat", "Tagging category");
33 tagCat.defineType("Lepton");
34 tagCat.defineType("Kaon");
35 tagCat.defineType("NetTagger-1");
36 tagCat.defineType("NetTagger-2");
37 tagCat.Print();
38
39 // 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
40 // ----------------------------------------------------------------------------------------
41
42 // Define a category with explicitly numbered states
43 RooCategory b0flav("b0flav", "B0 flavour eigenstate");
44 b0flav["B0"] = -1;
45 b0flav["B0bar"] = 1;
46 // Print it in "verbose" mode to see all states.
47 b0flav.Print("V");
48
49
50 // Alternatively, define many states at once. The function takes
51 // a map with std::string --> index mapping.
52 RooCategory largeCat("largeCat", "A category with many states");
53 largeCat.defineTypes({
54 {"A", 0}, {"b", 2}, {"c", 8}, {"dee", 4},
55 {"F", 133}, {"g", 15}, {"H", -20}
56 });
57
58
59 // I t e r a t e, q u e r y a n d s e t s t a t e s
60 // --------------------------------------------------------
61
62 // One can iterate through the {index,name} pair of category objects
63 std::cout << "\nThis is the for loop over states of 'largeCat':";
64 for (const auto& idxAndName : largeCat)
65 std::cout << "\n\t" << idxAndName.first << "\t" << idxAndName.second;
66 std::cout << '\n' << std::endl;
67
68 // To ask whether a state is valid use:
69 std::cout << "Has label 'A': " << largeCat.hasLabel("A");
70 std::cout << "\nHas index '-20': " << largeCat.hasIndex(-20);
71
72 // To retrieve names or state numbers:
73 std::cout << "\nLabel corresponding to '2' is " << largeCat.lookupName(2);
74 std::cout << "\nIndex corresponding to 'A' is " << largeCat.lookupIndex("A");
75
76 // To get the current state:
77 std::cout << "\nCurrent index is " << largeCat.getCurrentIndex();
78 std::cout << "\nCurrent label is " << largeCat.getCurrentLabel();
79 std::cout << std::endl;
80
81 // To set the state, use one of the two:
82 largeCat.setIndex(8);
83 largeCat.setLabel("c");
84
85
86
87 // 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
88 // ----------------------------------------------------------------------------
89
90 // Generate a dummy dataset
91 RooRealVar x("x", "x", 0, 10);
92 RooDataSet *data = RooPolynomial("p", "p", x).generate(RooArgSet(x, b0flav, tagCat), 10000);
93
94
95 // 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
96 // ------------------------------------------------------------------------------------------
97
98 // Tables are equivalent of plots for categories
99 Roo1DTable *btable = data->table(b0flav);
100 btable->Print();
101 btable->Print("v");
102
103 // Create table for subset of events matching cut expression
104 Roo1DTable *ttable = data->table(tagCat, "x>8.23");
105 ttable->Print();
106 ttable->Print("v");
107
108 // Create table for all (tagCat x b0flav) state combinations
109 Roo1DTable *bttable = data->table(RooArgSet(tagCat, b0flav));
110 bttable->Print("v");
111
112 // Retrieve number of events from table
113 // Number can be non-integer if source dataset has weighed events
114 Double_t nb0 = btable->get("B0");
115 std::cout << "Number of events with B0 flavor is " << nb0 << std::endl;
116
117 // Retrieve fraction of events with "Lepton" tag
118 Double_t fracLep = ttable->getFrac("Lepton");
119 std::cout << "Fraction of events tagged with Lepton tag is " << fracLep << std::endl;
120
121 // 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
122 // ------------------------------------------------------------------------------------------------------
123
124 // Define named range as comma separated list of labels
125 tagCat.setRange("good", "Lepton,Kaon");
126
127 // Or add state names one by one
128 tagCat.addToRange("soso", "NetTagger-1");
129 tagCat.addToRange("soso", "NetTagger-2");
130
131 // Use category range in dataset reduction specification
132 RooDataSet *goodData = (RooDataSet *)data->reduce(CutRange("good"));
133 goodData->table(tagCat)->Print("v");
134}
double Double_t
Definition RtypesCore.h:59
Roo1DTable implements a one-dimensional table.
Definition Roo1DTable.h:23
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Double_t getFrac(const char *label, Bool_t silent=kFALSE) const
Return the fraction of entries in the table contained in the slot named 'label'.
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
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.
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:58
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
RooCategory is an object to represent discrete states.
Definition RooCategory.h:27
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:33
RooPolynomial implements a polynomial p.d.f of the form.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
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 first.py:1