Rs 6 0 1_ H L Factoryexample

High Level Factory: creation of a simple 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;

--- build the datacard and dump to file---

In [4]:
TString card_name("HLFavtoryexample.rs");
ofstream ofile(card_name);
ofile << "// The simplest card\n\n"
      << "gauss = Gaussian(mes[5.20,5.30],mean[5.28,5.2,5.3],width[0.0027,0.001,1]);\n"
      << "argus = ArgusBG(mes,5.291,argpar[-20,-100,-1]);\n"
      << "sum = SUM(nsig[200,0,10000]*gauss,nbkg[800,0,10000]*argus);\n\n";

ofile.close();

HLFactory hlf("HLFavtoryexample", card_name, false);
[#1] INFO:ObjectHandling -- RooWorkspace::exportToCint(HLFavtoryexample_ws) INFO: references to all objects in this workspace will be created in CINT in 'namespace HLFavtoryexample_ws'

--- take elements out of the internal workspace ---

In [5]:
auto w = hlf.GetWs();

auto mes = dynamic_cast<RooRealVar *>(w->arg("mes"));
auto sum = dynamic_cast<RooAbsPdf *>(w->pdf("sum"));
auto argus = dynamic_cast<RooAbsPdf *>(w->pdf("argus"));

--- generate a toymc sample from composite pdf ---

In [6]:
auto data = sum->generate(*mes, 2000);
input_line_63:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
 auto data = sum->generate(*mes, 2000);
 ^

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

In [7]:
sum->fitTo(*data);
input_line_64:2:14: error: reference to 'data' is ambiguous
 sum->fitTo(*data);
             ^
input_line_63:2:7: note: candidate found by name lookup is '__cling_N523::data'
 auto data = sum->generate(*mes, 2000);
      ^
/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 [8]:
auto mesframe = mes->frame();
data->plotOn(mesframe);
sum->plotOn(mesframe);
sum->plotOn(mesframe, Components(*argus), LineStyle(kDashed));

gROOT->SetStyle("Plain");
mesframe->Draw();
input_line_65:3:1: error: reference to 'data' is ambiguous
data->plotOn(mesframe);
^
input_line_63:2:7: note: candidate found by name lookup is '__cling_N523::data'
 auto data = sum->generate(*mes, 2000);
      ^
/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 [9]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()