ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf404_categories.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'DATA AND CATEGORIES' RooFit tutorial macro #404
4 ///
5 /// Working with RooCategory objects to describe discrete variables
6 ///
7 ///
8 ///
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooPolynomial.h"
19 #include "RooCategory.h"
20 #include "Roo1DTable.h"
21 #include "RooGaussian.h"
22 #include "RooConstVar.h"
23 #include "TCanvas.h"
24 #include "TAxis.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 void rf404_categories()
30 {
31 
32  // 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
33  // ----------------------------------------------------------------
34 
35  // Define a category with labels only
36  RooCategory tagCat("tagCat","Tagging category") ;
37  tagCat.defineType("Lepton") ;
38  tagCat.defineType("Kaon") ;
39  tagCat.defineType("NetTagger-1") ;
40  tagCat.defineType("NetTagger-2") ;
41  tagCat.Print() ;
42 
43 
44 
45  // 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
46  // ----------------------------------------------------------------------------------------
47 
48  // Define a category with explicitly numbered states
49  RooCategory b0flav("b0flav","B0 flavour eigenstate") ;
50  b0flav.defineType("B0",-1) ;
51  b0flav.defineType("B0bar",1) ;
52  b0flav.Print() ;
53 
54 
55 
56  // 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
57  // ----------------------------------------------------------------------------
58 
59  // Generate a dummy dataset
60  RooRealVar x("x","x",0,10) ;
61  RooDataSet *data = RooPolynomial("p","p",x).generate(RooArgSet(x,b0flav,tagCat),10000) ;
62 
63 
64 
65  // 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
66  // ------------------------------------------------------------------------------------------
67 
68  // Tables are equivalent of plots for categories
69  Roo1DTable* btable = data->table(b0flav) ;
70  btable->Print() ;
71  btable->Print("v") ;
72 
73  // Create table for subset of events matching cut expression
74  Roo1DTable* ttable = data->table(tagCat,"x>8.23") ;
75  ttable->Print() ;
76  ttable->Print("v") ;
77 
78  // Create table for all (tagCat x b0flav) state combinations
79  Roo1DTable* bttable = data->table(RooArgSet(tagCat,b0flav)) ;
80  bttable->Print("v") ;
81 
82  // Retrieve number of events from table
83  // Number can be non-integer if source dataset has weighed events
84  Double_t nb0 = btable->get("B0") ;
85  cout << "Number of events with B0 flavor is " << nb0 << endl ;
86 
87  // Retrieve fraction of events with "Lepton" tag
88  Double_t fracLep = ttable->getFrac("Lepton") ;
89  cout << "Fraction of events tagged with Lepton tag is " << fracLep << endl ;
90 
91 
92 
93  // 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
94  // ------------------------------------------------------------------------------------------------------
95 
96  // Define named range as comma separated list of labels
97  tagCat.setRange("good","Lepton,Kaon") ;
98 
99  // Or add state names one by one
100  tagCat.addToRange("soso","NetTagger-1") ;
101  tagCat.addToRange("soso","NetTagger-2") ;
102 
103  // Use category range in dataset reduction specification
104  RooDataSet* goodData = (RooDataSet*) data->reduce(CutRange("good")) ;
105  goodData->table(tagCat)->Print("v") ;
106 
107 
108 }
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:835
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.
Definition: RooAbsData.cxx:399
Double_t x[n]
Definition: legend1.C:17
friend class RooArgSet
Definition: RooAbsArg.h:469
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
RooCmdArg CutRange(const char *rangeName)
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: Roo1DTable.h:52
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:25
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:244
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'.
Definition: Roo1DTable.cxx:301
double Double_t
Definition: RtypesCore.h:55
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())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:25