From $ROOTSYS/tutorials/roostats/rs601_HLFactoryexample.C

/////////////////////////////////////////////////////////////////////////
//
// 'High Level Factory Example' RooStats tutorial macro #601
// author: Danilo Piparo
// date August. 2009
//
// This tutorial shows an example of creating a simple
// model using the High Level model Factory.
//
//
/////////////////////////////////////////////////////////////////////////

#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
using namespace RooFit ;
using namespace RooStats ;
using namespace std;

void rs601_HLFactoryexample() {

    // --- 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 ---

    RooWorkspace* w = hlf.GetWs();

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

    // --- Generate a toyMC sample from composite PDF ---
    RooDataSet *data = sum->generate(*mes,2000) ;

    // --- Perform extended ML fit of composite PDF to toy data ---
    sum->fitTo(*data) ;

    // --- Plot toy data and composite PDF overlaid ---
    RooPlot* mesframe = mes->frame() ;
    data->plotOn(mesframe) ;
    sum->plotOn(mesframe) ;
    sum->plotOn(mesframe,Components(*argus),LineStyle(kDashed)) ;

    gROOT->SetStyle("Plain");
    mesframe->Draw()  ;
 rs601_HLFactoryexample.C:1
 rs601_HLFactoryexample.C:2
 rs601_HLFactoryexample.C:3
 rs601_HLFactoryexample.C:4
 rs601_HLFactoryexample.C:5
 rs601_HLFactoryexample.C:6
 rs601_HLFactoryexample.C:7
 rs601_HLFactoryexample.C:8
 rs601_HLFactoryexample.C:9
 rs601_HLFactoryexample.C:10
 rs601_HLFactoryexample.C:11
 rs601_HLFactoryexample.C:12
 rs601_HLFactoryexample.C:13
 rs601_HLFactoryexample.C:14
 rs601_HLFactoryexample.C:15
 rs601_HLFactoryexample.C:16
 rs601_HLFactoryexample.C:17
 rs601_HLFactoryexample.C:18
 rs601_HLFactoryexample.C:19
 rs601_HLFactoryexample.C:20
 rs601_HLFactoryexample.C:21
 rs601_HLFactoryexample.C:22
 rs601_HLFactoryexample.C:23
 rs601_HLFactoryexample.C:24
 rs601_HLFactoryexample.C:25
 rs601_HLFactoryexample.C:26
 rs601_HLFactoryexample.C:27
 rs601_HLFactoryexample.C:28
 rs601_HLFactoryexample.C:29
 rs601_HLFactoryexample.C:30
 rs601_HLFactoryexample.C:31
 rs601_HLFactoryexample.C:32
 rs601_HLFactoryexample.C:33
 rs601_HLFactoryexample.C:34
 rs601_HLFactoryexample.C:35
 rs601_HLFactoryexample.C:36
 rs601_HLFactoryexample.C:37
 rs601_HLFactoryexample.C:38
 rs601_HLFactoryexample.C:39
 rs601_HLFactoryexample.C:40
 rs601_HLFactoryexample.C:41
 rs601_HLFactoryexample.C:42
 rs601_HLFactoryexample.C:43
 rs601_HLFactoryexample.C:44
 rs601_HLFactoryexample.C:45
 rs601_HLFactoryexample.C:46
 rs601_HLFactoryexample.C:47
 rs601_HLFactoryexample.C:48
 rs601_HLFactoryexample.C:49
 rs601_HLFactoryexample.C:50
 rs601_HLFactoryexample.C:51
 rs601_HLFactoryexample.C:52
 rs601_HLFactoryexample.C:53
 rs601_HLFactoryexample.C:54
 rs601_HLFactoryexample.C:55
 rs601_HLFactoryexample.C:56
 rs601_HLFactoryexample.C:57
 rs601_HLFactoryexample.C:58
 rs601_HLFactoryexample.C:59
 rs601_HLFactoryexample.C:60
 rs601_HLFactoryexample.C:61
 rs601_HLFactoryexample.C:62
 rs601_HLFactoryexample.C:63
 rs601_HLFactoryexample.C:64
 rs601_HLFactoryexample.C:65
 rs601_HLFactoryexample.C:66
 rs601_HLFactoryexample.C:67
 rs601_HLFactoryexample.C:68
 rs601_HLFactoryexample.C:69
 rs601_HLFactoryexample.C:70
 rs601_HLFactoryexample.C:71