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 Tuesday, March 19, 2024 at 07:18 PM.
%%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"
using namespace RooFit;
using namespace RooStats;
using std::ofstream;
--- Build the datacard and dump to file---
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);
--- Take elements out of the internal workspace ---
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 ---
auto data = sum->generate(*mes, 2000);
input_line_52: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 ---
sum->fitTo(*data);
input_line_53:2:14: error: reference to 'data' is ambiguous sum->fitTo(*data); ^ input_line_52:2:7: note: candidate found by name lookup is '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 ---
auto mesframe = mes->frame();
data->plotOn(mesframe);
sum->plotOn(mesframe);
sum->plotOn(mesframe, Components(*argus), LineStyle(kDashed));
gROOT->SetStyle("Plain");
mesframe->Draw();
input_line_54:3:1: error: reference to 'data' is ambiguous data->plotOn(mesframe); ^ input_line_52:2:7: note: candidate found by name lookup is '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
%jsroot on
gROOT->GetListOfCanvases()->Draw()