Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rs603_HLFactoryElaborateExample.C File Reference

Detailed Description

View in nbviewer Open in SWAN
High Level Factory: creating a complex combined model.

[HLFactoryComplexExample] echo: In the middle!
[HLFactoryComplexExample] echo: At the end!
[#1] INFO:ObjectHandling -- RooWorkspace::import(rs603_ws) importing dataset data1
[#1] INFO:ObjectHandling -- RooWorkspace::import(rs603_ws) importing RooRealVar::x
[#1] INFO:ObjectHandling -- RooWorkspace::import(rs603_ws) importing dataset data2
-------------------------------------------------------------------
Rootfile and Workspace prepared
-------------------------------------------------------------------
[HLFactoryElaborateExample] echo: In the middle!
[HLFactoryElaborateExample] echo: Now reading the included file!
[HLFactoryElaborateExample] echo: Including datasets in a Workspace in a Root file...
[#1] INFO:ObjectHandling -- RooWorkspace::import(HLFactoryElaborateExample_ws) importing dataset data1
[#1] INFO:ObjectHandling -- RooWorkspace::import(HLFactoryElaborateExample_ws) importing dataset data2
[HLFactoryElaborateExample] echo: At the end!
RooDataSet::data1[x] = 219 entries
RooCategory::HLFactoryElaborateExample_category = model2(idx = 1)
[#1] INFO:Minimization -- p.d.f. provides expected number of events, including extended term in likelihood.
RooAbsTestStatistic::initSimMode: creating slave calculator #0 for state model1 (219 dataset entries)
RooAbsTestStatistic::initSimMode: creating slave calculator #1 for state model2 (164 dataset entries)
[#1] INFO:Fitting -- RooAbsTestStatistic::initSimMode: created 2 slave calculators.
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization -- The following expressions have been identified as constant and will be precalculated and cached: (flat1)
[#1] INFO:Minimization -- The following expressions will be evaluated in cache-and-track mode: (gauss1)
[#1] INFO:Minimization -- The following expressions have been identified as constant and will be precalculated and cached: (flat2)
[#1] INFO:Minimization -- The following expressions will be evaluated in cache-and-track mode: (gauss2)
Minuit2Minimizer: Minimize with max-calls 3000 convergence for edm < 1 strategy 1
Minuit2Minimizer : Valid minimum - status = 0
FVAL = 165.576629828309819
Edm = 9.10124649092437687e-07
Nfcn = 106
mean1 = 50.6657 +/- 0.411801 (limited)
mean2 = 79.8756 +/- 0.629188 (limited)
nbkg1 = 87.4181 +/- 10.4372 (limited)
nbkg2 = 67.5739 +/- 9.49233 (limited)
nsig1 = 131.59 +/- 12.3613 (limited)
nsig2 = 96.4299 +/- 10.9011 (limited)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
[#1] INFO:Plotting -- RooSimultaneous::plotOn(HLFactoryElaborateExample_sigbkg) plot on x represents a slice in the index category (HLFactoryElaborateExample_category)
[#1] INFO:Plotting -- RooAbsReal::plotOn(sb_model1) slice variable HLFactoryElaborateExample_category was not projected anyway
[#1] INFO:Plotting -- RooSimultaneous::plotOn(HLFactoryElaborateExample_sigbkg) plot on x represents a slice in the index category (HLFactoryElaborateExample_category)
[#1] INFO:Plotting -- RooAbsReal::plotOn(sb_model2) slice variable HLFactoryElaborateExample_category was not projected anyway
#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"
// 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);
auto x = static_cast<RooRealVar *>(hlf.GetWs()->arg("x"));
auto pdf1 = hlf.GetWs()->pdf("sb_model1");
auto pdf2 = hlf.GetWs()->pdf("sb_model2");
RooWorkspace w("rs603_ws");
auto data1 = pdf1->generate(RooArgSet(*x), Extended());
data1->SetName("data1");
w.import(*data1);
auto 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");
auto data = hlf_2.GetTotDataSet();
auto pdf = hlf_2.GetTotSigBkgPdf();
auto thecat = hlf_2.GetTotCategory();
// --- Perform extended ML fit of composite PDF to toy data ---
pdf->fitTo(*data);
// --- Plot toy data and composite PDF overlaid ---
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();
}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gROOT
Definition TROOT.h:407
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:37
HLFactory is an High Level model Factory allows you to describe your models in a configuration file (...
Definition HLFactory.h:29
Persistable container for RooFit projects.
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
Basic string class.
Definition TString.h:139
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19
Author
Danilo Piparo

Definition in file rs603_HLFactoryElaborateExample.C.