Rs 6 0 2_ H L Factory Combinationexample

High Level Factory: creation of a combined model

Author: Danilo Piparo
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, June 30, 2022 at 09:57 AM.

In [1]:
%%cpp -d
#include <fstream>
#include "TString.h"
#include "TROOT.h"
#include "RooGlobalFunc.h"
#include "RooWorkspace.h"
#include "RooRealVar.h"
#include "RooAbsPdf.h"
#include "RooDataSet.h"
#include "RooPlot.h"
#include "RooStats/HLFactory.h"

Use this order for safety on library loading

In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;
using namespace RooStats;
In [3]:
using namespace std;
In [4]:
using namespace RooStats;
using namespace RooFit;

Create a card

In [5]:
TString card_name("HLFavtoryCombinationexample.rs");
ofstream ofile(card_name);
ofile << "// The simplest card for combination\n\n"
      << "gauss1 = Gaussian(x[0,100],mean1[50,0,100],4);\n"
      << "flat1 = Polynomial(x,0);\n"
      << "sb_model1 = SUM(nsig1[120,0,300]*gauss1 , nbkg1[100,0,1000]*flat1);\n"
      << "gauss2 = Gaussian(x,mean2[80,0,100],5);\n"
      << "flat2 = Polynomial(x,0);\n"
      << "sb_model2 = SUM(nsig2[90,0,400]*gauss2 , nbkg2[80,0,1000]*flat2);\n";

ofile.close();

HLFactory hlf("HLFavtoryCombinationexample", card_name, false);

hlf.AddChannel("model1", "sb_model1", "flat1");
hlf.AddChannel("model2", "sb_model2", "flat2");
auto pdf = hlf.GetTotSigBkgPdf();
auto thecat = hlf.GetTotCategory();
auto x = static_cast<RooRealVar *>(hlf.GetWs()->arg("x"));

auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
[#1] INFO:ObjectHandling -- RooWorkspace::exportToCint(HLFavtoryCombinationexample_ws) INFO: references to all objects in this workspace will be created in CINT in 'namespace HLFavtoryCombinationexample_ws'
input_line_51:22:1: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
^

--- perform extended ml fit of composite pdf to toy data ---

In [6]:
pdf->fitTo(*data);
input_line_69:2:14: error: reference to 'data' is ambiguous
 pdf->fitTo(*data);
             ^
input_line_51:22:6: note: candidate found by name lookup is '__cling_N522::data'
auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
     ^
/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
    ^

--- plot toy data and composite pdf overlaid ---

In [7]:
auto xframe = x->frame();

data->plotOn(xframe);
thecat->setIndex(0);
pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));

thecat->setIndex(1);
pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));

gROOT->SetStyle("Plain");
xframe->Draw();
input_line_70:4:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_51:22:6: note: candidate found by name lookup is '__cling_N522::data'
auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
     ^
/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_70:6:57: error: reference to 'data' is ambiguous
pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));
                                                        ^
input_line_51:22:6: note: candidate found by name lookup is '__cling_N522::data'
auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
     ^
/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_70:9:57: error: reference to 'data' is ambiguous
pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));
                                                        ^
input_line_51:22:6: note: candidate found by name lookup is '__cling_N522::data'
auto data = pdf->generate(RooArgSet(*x, *thecat), Extended());
     ^
/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
    ^

Draw all canvases

In [8]:
gROOT->GetListOfCanvases()->Draw()