Logo ROOT  
Reference Guide
rf405_realtocatfuncs.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 ///
5 ///
6 /// \brief Data and categories: demonstration of real-->discrete mapping functions
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 ///
12 /// \date 07/2008
13 /// \author Wouter Verkerke
14 
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooCategory.h"
20 #include "RooThresholdCategory.h"
21 #include "RooBinningCategory.h"
22 #include "Roo1DTable.h"
23 #include "RooArgusBG.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "RooPlot.h"
27 using namespace RooFit;
28 
30 {
31 
32  // D e f i n e p d f i n x , s a m p l e d a t a s e t i n x
33  // ------------------------------------------------------------------------
34 
35  // Define a dummy PDF in x
36  RooRealVar x("x", "x", 0, 10);
37  RooArgusBG a("a", "argus(x)", x, RooConst(10), RooConst(-1));
38 
39  // Generate a dummy dataset
40  RooDataSet *data = a.generate(x, 10000);
41 
42  // C r e a t e a t h r e s h o l d r e a l - > c a t f u n c t i o n
43  // --------------------------------------------------------------------------
44 
45  // A RooThresholdCategory is a category function that maps regions in a real-valued
46  // input observable observables to state names. At construction time a 'default'
47  // state name must be specified to which all values of x are mapped that are not
48  // otherwise assigned
49  RooThresholdCategory xRegion("xRegion", "region of x", x, "Background");
50 
51  // Specify thresholds and state assignments one-by-one.
52  // Each statement specifies that all values _below_ the given value
53  // (and above any lower specified threshold) are mapped to the
54  // category state with the given name
55  //
56  // Background | SideBand | Signal | SideBand | Background
57  // 4.23 5.23 8.23 9.23
58  xRegion.addThreshold(4.23, "Background");
59  xRegion.addThreshold(5.23, "SideBand");
60  xRegion.addThreshold(8.23, "Signal");
61  xRegion.addThreshold(9.23, "SideBand");
62 
63  // U s e t h r e s h o l d f u n c t i o n t o p l o t d a t a r e g i o n s
64  // -------------------------------------------------------------------------------------
65 
66  // Add values of threshold function to dataset so that it can be used as observable
67  data->addColumn(xRegion);
68 
69  // Make plot of data in x
70  RooPlot *xframe = x.frame(Title("Demo of threshold and binning mapping functions"));
71  data->plotOn(xframe);
72 
73  // Use calculated category to select sideband data
74  data->plotOn(xframe, Cut("xRegion==xRegion::SideBand"), MarkerColor(kRed), LineColor(kRed));
75 
76  // C r e a t e a b i n n i n g r e a l - > c a t f u n c t i o n
77  // ----------------------------------------------------------------------
78 
79  // A RooBinningCategory is a category function that maps bins of a (named) binning definition
80  // in a real-valued input observable observables to state names. The state names are automatically
81  // constructed from the variable name, the binning name and the bin number. If no binning name
82  // is specified the default binning is mapped
83 
84  x.setBins(10, "coarse");
85  RooBinningCategory xBins("xBins", "coarse bins in x", x, "coarse");
86 
87  // U s e b i n n i n g f u n c t i o n f o r t a b u l a t i o n a n d p l o t t i n g
88  // -----------------------------------------------------------------------------------------------
89 
90  // Print table of xBins state multiplicity. Note that xBins does not need to be an observable in data
91  // it can be a function of observables in data as well
92  Roo1DTable *xbtable = data->table(xBins);
93  xbtable->Print("v");
94 
95  // Add values of xBins function to dataset so that it can be used as observable
96  RooCategory *xb = (RooCategory *)data->addColumn(xBins);
97 
98  // Define range "alt" as including bins 1,3,5,7,9
99  xb->setRange("alt", "x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9");
100 
101  // Construct subset of data matching range "alt" but only for the first 5000 events and plot it on the frame
102  RooDataSet *dataSel = (RooDataSet *)data->reduce(CutRange("alt"), EventRange(0, 5000));
103  dataSel->plotOn(xframe, MarkerColor(kGreen), LineColor(kGreen));
104 
105  new TCanvas("rf405_realtocatfuncs", "rf405_realtocatfuncs", 600, 600);
106  xframe->SetMinimum(0.01);
107  gPad->SetLeftMargin(0.15);
108  xframe->GetYaxis()->SetTitleOffset(1.4);
109  xframe->Draw();
110 }
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooArgusBG.h
RooCategory::setRange
void setRange(const char *rangeName, const char *stateNameList)
Definition: RooCategory.cxx:330
RooBinningCategory.h
Roo1DTable
Definition: Roo1DTable.h:23
kGreen
@ kGreen
Definition: Rtypes.h:66
RooFit::EventRange
RooCmdArg EventRange(Int_t nStart, Int_t nStop)
Definition: RooGlobalFunc.cxx:180
RooGaussian.h
Roo1DTable::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: Roo1DTable.h:64
RooThresholdCategory.h
x
Double_t x[n]
Definition: legend1.C:17
TCanvas.h
RooDataSet.h
RooFit::CutRange
RooCmdArg CutRange(const char *rangeName)
Definition: RooGlobalFunc.cxx:88
rf405_realtocatfuncs
Definition: rf405_realtocatfuncs.py:1
RooAbsData::table
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:749
RooFit::Cut
RooCmdArg Cut(const char *cutSpec)
Definition: RooGlobalFunc.cxx:80
RooFit
Definition: RooCFunction1Binding.h:29
RooAbsData::plotOn
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Definition: RooAbsData.cxx:547
a
auto * a
Definition: textangle.C:12
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1258
RooPlot
Definition: RooPlot.h:44
RooCategory.h
RooBinningCategory
Definition: RooBinningCategory.h:23
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooConstVar.h
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
TCanvas
Definition: TCanvas.h:23
Roo1DTable.h
RooCategory
Definition: RooCategory.h:27
RooAbsData::reduce
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:382
TAxis.h
RooArgusBG
Definition: RooArgusBG.h:25
RooPlot::SetMinimum
virtual void SetMinimum(Double_t minimum=-1111)
Set minimum value of Y axis.
Definition: RooPlot.cxx:1091
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
RooFit::MarkerColor
RooCmdArg MarkerColor(Color_t color)
Definition: RooGlobalFunc.cxx:87
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:36
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173
RooThresholdCategory
Definition: RooThresholdCategory.h:26
RooDataSet::addColumn
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
Add a column with the values of the given (function) argument to this dataset.
Definition: RooDataSet.cxx:1383
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341