# 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 Sunday, February 16, 2020 at 03:53 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"


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);

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());

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

[#1] INFO:ObjectHandling -- RooWorkspace::exportToCint(HLFavtoryCombinationexample_ws) INFO: references to all objects in this workspace will be created in CINT in 'namespace HLFavtoryCombinationexample_ws'


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

In [6]:
pdf->fitTo(*data);

[#1] INFO:Minization -- p.d.f. provides expected number of events, including extended term in likelihood.
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
RooAbsTestStatistic::initSimMode: creating slave calculator #0 for state model1 (211 dataset entries)
RooAbsTestStatistic::initSimMode: creating slave calculator #1 for state model2 (175 dataset entries)
[#1] INFO:Fitting -- RooAbsTestStatistic::initSimMode: created 2 slave calculators.
[#1] INFO:Minization --  The following expressions have been identified as constant and will be precalculated and cached: (flat1)
[#1] INFO:Minization --  The following expressions will be evaluated in cache-and-track mode: (gauss1)
[#1] INFO:Minization --  The following expressions have been identified as constant and will be precalculated and cached: (flat2)
[#1] INFO:Minization --  The following expressions will be evaluated in cache-and-track mode: (gauss2)
**********
**    1 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 mean1        5.00000e+01  1.00000e+01    0.00000e+00  1.00000e+02
2 mean2        8.00000e+01  1.00000e+01    0.00000e+00  1.00000e+02
3 nbkg1        1.00000e+02  5.00000e+01    0.00000e+00  1.00000e+03
4 nbkg2        8.00000e+01  4.00000e+01    0.00000e+00  1.00000e+03
5 nsig1        1.20000e+02  3.00000e+01    0.00000e+00  3.00000e+02
6 nsig2        9.00000e+01  4.00000e+01    0.00000e+00  4.00000e+02
**********
**    3 **SET ERR         0.5
**********
**********
**    4 **SET PRINT           1
**********
**********
**    5 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=199.77 FROM MIGRAD    STATUS=INITIATE       24 CALLS          25 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean1        5.00000e+01   1.00000e+01   2.01358e-01   3.07908e+01
2  mean2        8.00000e+01   1.00000e+01   2.57889e-01   9.66677e+00
3  nbkg1        1.00000e+02   5.00000e+01   1.72186e-01   2.07463e+01
4  nbkg2        8.00000e+01   4.00000e+01   1.52384e-01  -8.44368e+00
5  nsig1        1.20000e+02   3.00000e+01   2.05758e-01   2.55303e+00
6  nsig2        9.00000e+01   4.00000e+01   2.45245e-01  -4.65857e+00
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=199.349 FROM MIGRAD    STATUS=CONVERGED      92 CALLS          93 TOTAL
EDM=6.01075e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean1        4.98783e+01   4.26590e-01   8.34391e-05  -6.57258e-01
2  mean2        7.98952e+01   6.49972e-01   1.58369e-04  -2.57944e-01
3  nbkg1        9.18179e+01   1.06581e+01   3.55527e-04   5.08825e-02
4  nbkg2        8.25577e+01   1.03903e+01   3.59387e-04  -2.86339e-02
5  nsig1        1.19174e+02   1.18598e+01   7.72527e-04  -2.99954e-02
6  nsig2        9.24320e+01   1.08502e+01   6.13208e-04  -3.16233e-03
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  6    ERR DEF=0.5
1.820e-01  0.000e+00  9.170e-02  0.000e+00 -9.167e-02  0.000e+00
0.000e+00  4.225e-01  0.000e+00 -1.575e-02  0.000e+00  1.575e-02
9.170e-02  0.000e+00  1.136e+02  0.000e+00 -2.180e+01  0.000e+00
0.000e+00 -1.575e-02  0.000e+00  1.080e+02  0.000e+00 -2.545e+01
-9.167e-02  0.000e+00 -2.180e+01  0.000e+00  1.410e+02  0.000e+00
0.000e+00  1.575e-02  0.000e+00 -2.545e+01  0.000e+00  1.179e+02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3      4      5      6
1  0.02504   1.000  0.000  0.020  0.000 -0.018  0.000
2  0.00292   0.000  1.000  0.000 -0.002  0.000  0.002
3  0.17311   0.020  0.000  1.000  0.000 -0.172  0.000
4  0.22552   0.000 -0.002  0.000  1.000  0.000 -0.226
5  0.17289  -0.018  0.000 -0.172  0.000  1.000  0.000
6  0.22552   0.000  0.002  0.000 -0.226  0.000  1.000
**********
**    7 **SET ERR         0.5
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        3000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=199.349 FROM HESSE     STATUS=OK             40 CALLS         133 TOTAL
EDM=6.01118e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  mean1        4.98783e+01   4.26588e-01   1.66878e-05  -2.43458e-03
2  mean2        7.98952e+01   6.49972e-01   3.16737e-05   6.40885e-01
3  nbkg1        9.18179e+01   1.06583e+01   7.11055e-05  -9.55088e-01
4  nbkg2        8.25577e+01   1.03906e+01   7.18775e-05  -9.87923e-01
5  nsig1        1.19174e+02   1.18599e+01   1.54505e-04  -2.06982e-01
6  nsig2        9.24320e+01   1.08505e+01   1.22642e-04  -5.67873e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  6    ERR DEF=0.5
1.820e-01  0.000e+00  9.119e-02  0.000e+00 -9.117e-02  0.000e+00
0.000e+00  4.225e-01  0.000e+00 -1.684e-02  0.000e+00  1.684e-02
9.119e-02  0.000e+00  1.137e+02  0.000e+00 -2.182e+01  0.000e+00
0.000e+00 -1.684e-02  0.000e+00  1.080e+02  0.000e+00 -2.546e+01
-9.117e-02  0.000e+00 -2.182e+01  0.000e+00  1.410e+02  0.000e+00
0.000e+00  1.684e-02  0.000e+00 -2.546e+01  0.000e+00  1.179e+02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3      4      5      6
1  0.02490   1.000  0.000  0.020  0.000 -0.018  0.000
2  0.00312   0.000  1.000  0.000 -0.002  0.000  0.002
3  0.17319   0.020  0.000  1.000  0.000 -0.172  0.000
4  0.22564   0.000 -0.002  0.000  1.000  0.000 -0.226
5  0.17297  -0.018  0.000 -0.172  0.000  1.000  0.000
6  0.22564   0.000  0.002  0.000 -0.226  0.000  1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization


--- 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();

[#1] INFO:Plotting -- RooSimultaneous::plotOn(HLFavtoryCombinationexample_sigbkg) plot on x represents a slice in the index category (HLFavtoryCombinationexample_category)
[#1] INFO:InputArguments -- The formula HLFavtoryCombinationexample_category==0 claims to use the variables (x,HLFavtoryCombinationexample_category) but only (HLFavtoryCombinationexample_category) seem to be in use.
inputs:         x[1]==0
interpretation: [HLFavtoryCombinationexample_category]==0
[#1] INFO:InputArguments -- The formula HLFavtoryCombinationexample_category==0 claims to use the variables (x,HLFavtoryCombinationexample_category) but only (HLFavtoryCombinationexample_category) seem to be in use.
inputs:         x[1]==0
interpretation: [HLFavtoryCombinationexample_category]==0
[#1] INFO:Plotting -- RooAbsReal::plotOn(sb_model1) slice variable HLFavtoryCombinationexample_category was not projected anyway
[#1] INFO:Plotting -- RooSimultaneous::plotOn(HLFavtoryCombinationexample_sigbkg) plot on x represents a slice in the index category (HLFavtoryCombinationexample_category)
[#1] INFO:InputArguments -- The formula HLFavtoryCombinationexample_category==1 claims to use the variables (x,HLFavtoryCombinationexample_category) but only (HLFavtoryCombinationexample_category) seem to be in use.
inputs:         x[1]==1
interpretation: [HLFavtoryCombinationexample_category]==1
[#1] INFO:InputArguments -- The formula HLFavtoryCombinationexample_category==1 claims to use the variables (x,HLFavtoryCombinationexample_category) but only (HLFavtoryCombinationexample_category) seem to be in use.
inputs:         x[1]==1
interpretation: [HLFavtoryCombinationexample_category]==1
[#1] INFO:Plotting -- RooAbsReal::plotOn(sb_model2) slice variable HLFavtoryCombinationexample_category was not projected anyway

Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1


Draw all canvases

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