Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 /// \author 07/2008 - Wouter Verkerke
9 
10 #include "RooRealVar.h"
11 #include "RooDataSet.h"
12 #include "RooPolynomial.h"
13 #include "RooCategory.h"
14 #include "Roo1DTable.h"
15 #include "RooGaussian.h"
16 #include "RooConstVar.h"
17 #include "TCanvas.h"
18 #include "TAxis.h"
19 #include "RooPlot.h"
20 using namespace RooFit;
21 
22 void rf404_categories()
23 {
24 
25  // 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
26  // ----------------------------------------------------------------
27 
28  // Define a category with labels only
29  RooCategory tagCat("tagCat", "Tagging category");
30  tagCat.defineType("Lepton");
31  tagCat.defineType("Kaon");
32  tagCat.defineType("NetTagger-1");
33  tagCat.defineType("NetTagger-2");
34  tagCat.Print();
35 
36  // 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 e c e s
37  // ----------------------------------------------------------------------------------------
38 
39  // Define a category with explicitly numbered states
40  RooCategory b0flav("b0flav", "B0 flavour eigenstate");
41  b0flav.defineType("B0", -1);
42  b0flav.defineType("B0bar", 1);
43  b0flav.Print();
44 
45  // 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
46  // ----------------------------------------------------------------------------
47 
48  // Generate a dummy dataset
49  RooRealVar x("x", "x", 0, 10);
50  RooDataSet *data = RooPolynomial("p", "p", x).generate(RooArgSet(x, b0flav, tagCat), 10000);
51 
52  // 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
53  // ------------------------------------------------------------------------------------------
54 
55  // Tables are equivalent of plots for categories
56  Roo1DTable *btable = data->table(b0flav);
57  btable->Print();
58  btable->Print("v");
59 
60  // Create table for subset of events matching cut expression
61  Roo1DTable *ttable = data->table(tagCat, "x>8.23");
62  ttable->Print();
63  ttable->Print("v");
64 
65  // Create table for all (tagCat x b0flav) state combinations
66  Roo1DTable *bttable = data->table(RooArgSet(tagCat, b0flav));
67  bttable->Print("v");
68 
69  // Retrieve number of events from table
70  // Number can be non-integer if source dataset has weighed events
71  Double_t nb0 = btable->get("B0");
72  cout << "Number of events with B0 flavor is " << nb0 << endl;
73 
74  // Retrieve fraction of events with "Lepton" tag
75  Double_t fracLep = ttable->getFrac("Lepton");
76  cout << "Fraction of events tagged with Lepton tag is " << fracLep << endl;
77 
78  // 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
79  // ------------------------------------------------------------------------------------------------------
80 
81  // Define named range as comma separated list of labels
82  tagCat.setRange("good", "Lepton,Kaon");
83 
84  // Or add state names one by one
85  tagCat.addToRange("soso", "NetTagger-1");
86  tagCat.addToRange("soso", "NetTagger-2");
87 
88  // Use category range in dataset reduction specification
89  RooDataSet *goodData = (RooDataSet *)data->reduce(CutRange("good"));
90  goodData->table(tagCat)->Print("v");
91 }