From $ROOTSYS/tutorials/roostats/rs603_HLFactoryElaborateExample.C

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



#include <fstream>
#include "TString.h"
#include "TFile.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 rs603_HLFactoryElaborateExample() {

  // --- Prepare the 2 needed datacards for this example ---

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

  TString card_name2("rs603_card.rs");
  ofstream ofile2(card_name2);
  ofile2 << "// The simplest card for combination\n\n";
  ofile2 << "gauss1 = Gaussian(x[0,100],mean1[50,0,100],4);\n";
  ofile2 << "flat1 = Polynomial(x,0);\n";
  ofile2 << "sb_model1 = SUM(nsig1[120,0,300]*gauss1 , nbkg1[100,0,1000]*flat1);\n\n";
  ofile2 << "echo In the middle!;\n\n";
  ofile2 << "gauss2 = Gaussian(x,mean2[80,0,100],5);\n";
  ofile2 << "flat2 = Polynomial(x,0);\n";
  ofile2 << "sb_model2 = SUM(nsig2[90,0,400]*gauss2 , nbkg2[80,0,1000]*flat2);\n\n";
  ofile2 << "#include rs603_included_card.rs;\n\n";
  ofile2 << "echo At the end!;\n";
  ofile2.close();

  TString card_name3("rs603_included_card.rs");
  ofstream ofile3(card_name3);
  ofile3 << "echo Now reading the included file!;\n\n";
  ofile3 << "echo Including datasets in a Workspace in a Root file...;\n";
  ofile3 << "data1 = import(rs603_infile.root,\n";
  ofile3 << "               rs603_ws,\n";
  ofile3 << "               data1);\n\n";
  ofile3 << "data2 = import(rs603_infile.root,\n";
  ofile3 << "               rs603_ws,\n";
  ofile3 << "               data2);\n";
  ofile3.close();

// --- Produce the two separate datasets into a WorkSpace ---

HLFactory hlf("HLFactoryComplexExample",
              "rs603_card_WsMaker.rs",
              false);

RooRealVar* x = static_cast<RooRealVar*>(hlf.GetWs()->arg("x"));
RooAbsPdf* pdf1 = hlf.GetWs()->pdf("sb_model1");
RooAbsPdf* pdf2 = hlf.GetWs()->pdf("sb_model2");

RooWorkspace w("rs603_ws");

RooDataSet* data1 = pdf1->generate(RooArgSet(*x),Extended());
data1->SetName("data1");
w.import(*data1);

RooDataSet* data2 = pdf2->generate(RooArgSet(*x),Extended());
data2->SetName("data2");
w.import(*data2);

// --- Write the WorkSpace into a rootfile ---

TFile outfile("rs603_infile.root","RECREATE");
w.Write();
outfile.Close();

cout << "-------------------------------------------------------------------\n"
     << " Rootfile and Workspace prepared \n"
     << "-------------------------------------------------------------------\n";


HLFactory hlf_2("HLFactoryElaborateExample",
                "rs603_card.rs",
                false);

x = hlf_2.GetWs()->var("x");
pdf1 = hlf_2.GetWs()->pdf("sb_model1");
pdf2 = hlf_2.GetWs()->pdf("sb_model2");

hlf_2.AddChannel("model1","sb_model1","flat1","data1");
hlf_2.AddChannel("model2","sb_model2","flat2","data2");

RooDataSet* data = hlf_2.GetTotDataSet();
RooAbsPdf* pdf = hlf_2.GetTotSigBkgPdf();
RooCategory* thecat = hlf_2.GetTotCategory();

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

// --- Plot toy data and composite PDF overlaid ---
RooPlot* 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();

}
 rs603_HLFactoryElaborateExample.C:1
 rs603_HLFactoryElaborateExample.C:2
 rs603_HLFactoryElaborateExample.C:3
 rs603_HLFactoryElaborateExample.C:4
 rs603_HLFactoryElaborateExample.C:5
 rs603_HLFactoryElaborateExample.C:6
 rs603_HLFactoryElaborateExample.C:7
 rs603_HLFactoryElaborateExample.C:8
 rs603_HLFactoryElaborateExample.C:9
 rs603_HLFactoryElaborateExample.C:10
 rs603_HLFactoryElaborateExample.C:11
 rs603_HLFactoryElaborateExample.C:12
 rs603_HLFactoryElaborateExample.C:13
 rs603_HLFactoryElaborateExample.C:14
 rs603_HLFactoryElaborateExample.C:15
 rs603_HLFactoryElaborateExample.C:16
 rs603_HLFactoryElaborateExample.C:17
 rs603_HLFactoryElaborateExample.C:18
 rs603_HLFactoryElaborateExample.C:19
 rs603_HLFactoryElaborateExample.C:20
 rs603_HLFactoryElaborateExample.C:21
 rs603_HLFactoryElaborateExample.C:22
 rs603_HLFactoryElaborateExample.C:23
 rs603_HLFactoryElaborateExample.C:24
 rs603_HLFactoryElaborateExample.C:25
 rs603_HLFactoryElaborateExample.C:26
 rs603_HLFactoryElaborateExample.C:27
 rs603_HLFactoryElaborateExample.C:28
 rs603_HLFactoryElaborateExample.C:29
 rs603_HLFactoryElaborateExample.C:30
 rs603_HLFactoryElaborateExample.C:31
 rs603_HLFactoryElaborateExample.C:32
 rs603_HLFactoryElaborateExample.C:33
 rs603_HLFactoryElaborateExample.C:34
 rs603_HLFactoryElaborateExample.C:35
 rs603_HLFactoryElaborateExample.C:36
 rs603_HLFactoryElaborateExample.C:37
 rs603_HLFactoryElaborateExample.C:38
 rs603_HLFactoryElaborateExample.C:39
 rs603_HLFactoryElaborateExample.C:40
 rs603_HLFactoryElaborateExample.C:41
 rs603_HLFactoryElaborateExample.C:42
 rs603_HLFactoryElaborateExample.C:43
 rs603_HLFactoryElaborateExample.C:44
 rs603_HLFactoryElaborateExample.C:45
 rs603_HLFactoryElaborateExample.C:46
 rs603_HLFactoryElaborateExample.C:47
 rs603_HLFactoryElaborateExample.C:48
 rs603_HLFactoryElaborateExample.C:49
 rs603_HLFactoryElaborateExample.C:50
 rs603_HLFactoryElaborateExample.C:51
 rs603_HLFactoryElaborateExample.C:52
 rs603_HLFactoryElaborateExample.C:53
 rs603_HLFactoryElaborateExample.C:54
 rs603_HLFactoryElaborateExample.C:55
 rs603_HLFactoryElaborateExample.C:56
 rs603_HLFactoryElaborateExample.C:57
 rs603_HLFactoryElaborateExample.C:58
 rs603_HLFactoryElaborateExample.C:59
 rs603_HLFactoryElaborateExample.C:60
 rs603_HLFactoryElaborateExample.C:61
 rs603_HLFactoryElaborateExample.C:62
 rs603_HLFactoryElaborateExample.C:63
 rs603_HLFactoryElaborateExample.C:64
 rs603_HLFactoryElaborateExample.C:65
 rs603_HLFactoryElaborateExample.C:66
 rs603_HLFactoryElaborateExample.C:67
 rs603_HLFactoryElaborateExample.C:68
 rs603_HLFactoryElaborateExample.C:69
 rs603_HLFactoryElaborateExample.C:70
 rs603_HLFactoryElaborateExample.C:71
 rs603_HLFactoryElaborateExample.C:72
 rs603_HLFactoryElaborateExample.C:73
 rs603_HLFactoryElaborateExample.C:74
 rs603_HLFactoryElaborateExample.C:75
 rs603_HLFactoryElaborateExample.C:76
 rs603_HLFactoryElaborateExample.C:77
 rs603_HLFactoryElaborateExample.C:78
 rs603_HLFactoryElaborateExample.C:79
 rs603_HLFactoryElaborateExample.C:80
 rs603_HLFactoryElaborateExample.C:81
 rs603_HLFactoryElaborateExample.C:82
 rs603_HLFactoryElaborateExample.C:83
 rs603_HLFactoryElaborateExample.C:84
 rs603_HLFactoryElaborateExample.C:85
 rs603_HLFactoryElaborateExample.C:86
 rs603_HLFactoryElaborateExample.C:87
 rs603_HLFactoryElaborateExample.C:88
 rs603_HLFactoryElaborateExample.C:89
 rs603_HLFactoryElaborateExample.C:90
 rs603_HLFactoryElaborateExample.C:91
 rs603_HLFactoryElaborateExample.C:92
 rs603_HLFactoryElaborateExample.C:93
 rs603_HLFactoryElaborateExample.C:94
 rs603_HLFactoryElaborateExample.C:95
 rs603_HLFactoryElaborateExample.C:96
 rs603_HLFactoryElaborateExample.C:97
 rs603_HLFactoryElaborateExample.C:98
 rs603_HLFactoryElaborateExample.C:99
 rs603_HLFactoryElaborateExample.C:100
 rs603_HLFactoryElaborateExample.C:101
 rs603_HLFactoryElaborateExample.C:102
 rs603_HLFactoryElaborateExample.C:103
 rs603_HLFactoryElaborateExample.C:104
 rs603_HLFactoryElaborateExample.C:105
 rs603_HLFactoryElaborateExample.C:106
 rs603_HLFactoryElaborateExample.C:107
 rs603_HLFactoryElaborateExample.C:108
 rs603_HLFactoryElaborateExample.C:109
 rs603_HLFactoryElaborateExample.C:110
 rs603_HLFactoryElaborateExample.C:111
 rs603_HLFactoryElaborateExample.C:112
 rs603_HLFactoryElaborateExample.C:113
 rs603_HLFactoryElaborateExample.C:114
 rs603_HLFactoryElaborateExample.C:115
 rs603_HLFactoryElaborateExample.C:116
 rs603_HLFactoryElaborateExample.C:117
 rs603_HLFactoryElaborateExample.C:118
 rs603_HLFactoryElaborateExample.C:119
 rs603_HLFactoryElaborateExample.C:120
 rs603_HLFactoryElaborateExample.C:121
 rs603_HLFactoryElaborateExample.C:122
 rs603_HLFactoryElaborateExample.C:123
 rs603_HLFactoryElaborateExample.C:124
 rs603_HLFactoryElaborateExample.C:125
 rs603_HLFactoryElaborateExample.C:126
 rs603_HLFactoryElaborateExample.C:127
 rs603_HLFactoryElaborateExample.C:128
 rs603_HLFactoryElaborateExample.C:129
 rs603_HLFactoryElaborateExample.C:130
 rs603_HLFactoryElaborateExample.C:131
 rs603_HLFactoryElaborateExample.C:132
 rs603_HLFactoryElaborateExample.C:133
 rs603_HLFactoryElaborateExample.C:134
 rs603_HLFactoryElaborateExample.C:135
 rs603_HLFactoryElaborateExample.C:136
 rs603_HLFactoryElaborateExample.C:137
 rs603_HLFactoryElaborateExample.C:138
 rs603_HLFactoryElaborateExample.C:139
 rs603_HLFactoryElaborateExample.C:140