Rf 4 0 5_Realtocatfuncs

Data and categories: demonstration of real-->discrete mapping functions

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, January 19, 2022 at 10:22 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooCategory.h"
#include "RooThresholdCategory.h"
#include "RooBinningCategory.h"
#include "Roo1DTable.h"
#include "RooArgusBG.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Define pdf in x, sample dataset in x

Define a dummy pdf in x

In [3]:
RooRealVar x("x", "x", 0, 10);
RooArgusBG a("a", "argus(x)", x, RooConst(10), RooConst(-1));
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

Generate a dummy dataset

In [4]:
RooDataSet *data = a.generate(x, 10000);

Create a threshold real->cat function

A roothresholdcategory is a category function that maps regions in a real-valued input observable observables to state names. At construction time a 'default' state name must be specified to which all values of x are mapped that are not otherwise assigned

In [5]:
RooThresholdCategory xRegion("xRegion", "region of x", x, "Background");

Specify thresholds and state assignments one-by-one. Each statement specifies that all values below the given value (and above any lower specified threshold) are mapped to the category state with the given name

Background | SideBand | Signal | SideBand | Background 4.23 5.23 8.23 9.23

In [6]:
xRegion.addThreshold(4.23, "Background");
xRegion.addThreshold(5.23, "SideBand");
xRegion.addThreshold(8.23, "Signal");
xRegion.addThreshold(9.23, "SideBand");

Use threshold function to plot data regions

Add values of threshold function to dataset so that it can be used as observable

In [7]:
data->addColumn(xRegion);
input_line_57:2:2: error: reference to 'data' is ambiguous
 data->addColumn(xRegion);
 ^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^

Make plot of data in x

In [8]:
RooPlot *xframe = x.frame(Title("Demo of threshold and binning mapping functions"));
data->plotOn(xframe);
input_line_58:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^

Use calculated category to select sideband data

In [9]:
data->plotOn(xframe, Cut("xRegion==xRegion::SideBand"), MarkerColor(kRed), LineColor(kRed));
input_line_59:2:2: error: reference to 'data' is ambiguous
 data->plotOn(xframe, Cut("xRegion==xRegion::SideBand"), MarkerColor(kRed), LineColor(kRed));
 ^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^

Create a binning real->cat function

A roobinningcategory is a category function that maps bins of a (named) binning definition in a real-valued input observable observables to state names. The state names are automatically constructed from the variable name, the binning name and the bin number. If no binning name is specified the default binning is mapped

In [10]:
x.setBins(10, "coarse");
RooBinningCategory xBins("xBins", "coarse bins in x", x, "coarse");

Use binning function for tabulation and plotting

Print table of xbins state multiplicity. note that xbins does not need to be an observable in data it can be a function of observables in data as well

In [11]:
Roo1DTable *xbtable = data->table(xBins);
xbtable->Print("v");
input_line_61:2:24: error: reference to 'data' is ambiguous
 Roo1DTable *xbtable = data->table(xBins);
                       ^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^

Add values of xbins function to dataset so that it can be used as observable

In [12]:
RooCategory *xb = (RooCategory *)data->addColumn(xBins);
input_line_62:2:35: error: reference to 'data' is ambiguous
 RooCategory *xb = (RooCategory *)data->addColumn(xBins);
                                  ^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^

Define range "alt" as including bins 1,3,5,7,9

In [13]:
xb->setRange("alt", "x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9");
input_line_64:2:3: error: use of undeclared identifier 'xb'
 (xb->setRange("alt", "x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9"))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (xb->setRange("alt", "x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9"))
Execution of your code was aborted.

Construct subset of data matching range "alt" but only for the first 5000 events and plot it on the frame

In [14]:
RooDataSet *dataSel = (RooDataSet *)data->reduce(CutRange("alt"), EventRange(0, 5000));
dataSel->plotOn(xframe, MarkerColor(kGreen), LineColor(kGreen));

new TCanvas("rf405_realtocatfuncs", "rf405_realtocatfuncs", 600, 600);
xframe->SetMinimum(0.01);
gPad->SetLeftMargin(0.15);
xframe->GetYaxis()->SetTitleOffset(1.4);
xframe->Draw();
input_line_65:2:38: error: reference to 'data' is ambiguous
 RooDataSet *dataSel = (RooDataSet *)data->reduce(CutRange("alt"), EventRange(0, 5000));
                                     ^
input_line_54:2:14: note: candidate found by name lookup is '__cling_N522::data'
 RooDataSet *data = a.generate(x, 10000);
             ^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
    data(initializer_list<_Tp> __il) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
    data(_Tp (&__array)[_Nm]) noexcept
    ^
input_line_65:3:17: error: use of undeclared identifier 'xframe'
dataSel->plotOn(xframe, MarkerColor(kGreen), LineColor(kGreen));
                ^
input_line_65:6:1: error: use of undeclared identifier 'xframe'
xframe->SetMinimum(0.01);
^
input_line_65:8:1: error: use of undeclared identifier 'xframe'
xframe->GetYaxis()->SetTitleOffset(1.4);
^
input_line_65:9:1: error: use of undeclared identifier 'xframe'
xframe->Draw();
^

Draw all canvases

In [15]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()