diff --git a/tutorials/roostats/rs601_HLFactoryexample.C b/tutorials/roostats/rs601_HLFactoryexample.C new file mode 100644 index 0000000000000000000000000000000000000000..b85a56c4c74520bdcee9bf8123995ec808f54f5b --- /dev/null +++ b/tutorials/roostats/rs601_HLFactoryexample.C @@ -0,0 +1,71 @@ +///////////////////////////////////////////////////////////////////////// +// +// '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. +// +// +///////////////////////////////////////////////////////////////////////// + +#ifndef __CINT__ +#include "RooGlobalFunc.h" +#include <fstream> +#include "TString.h" +#include "RooWorkspace.h" +#include "RooRealVar.h" +#include "RooAbsPdf.h" +#include "RooDataSet.h" +#include "RooPlot.h" +#endif + + +// 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 = w->arg("mes"); + RooAbsPdf* sum = w->pdf("sum"); + RooAbsPdf* argus = w->pdf("argus"); + RooRealVar* mean = w->arg("mean"); + RooRealVar* argpar = 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() ; +} \ No newline at end of file diff --git a/tutorials/roostats/rs602_HLFactoryCombinationexample.C b/tutorials/roostats/rs602_HLFactoryCombinationexample.C new file mode 100644 index 0000000000000000000000000000000000000000..d05f6eeb53c013a303b2fc451a6a5d8bb8fdb95d --- /dev/null +++ b/tutorials/roostats/rs602_HLFactoryCombinationexample.C @@ -0,0 +1,75 @@ +///////////////////////////////////////////////////////////////////////// +// +// '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. +// +// +///////////////////////////////////////////////////////////////////////// + +#ifndef __CINT__ +#include "RooGlobalFunc.h" +#include <fstream> +#include "TString.h" +#include "RooWorkspace.h" +#include "RooRealVar.h" +#include "RooAbsPdf.h" +#include "RooDataSet.h" +#include "RooPlot.h" +#endif + + +// use this order for safety on library loading +using namespace RooFit ; +using namespace RooStats ; +using namespace std; + +void rs602_HLFactoryCombinationexample() { + +using namespace RooStats; +using namespace RooFit; + +// create a card +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); + +hlf.AddChannel("model1","sb_model1","flat1"); +hlf.AddChannel("model2","sb_model2","flat2"); +RooAbsPdf* pdf=hlf.GetTotSigBkgPdf(); +RooCategory* thecat = hlf.GetTotCategory(); +RooRealVar* x= hlf.GetWs()->arg("x"); + +RooDataSet* data = pdf->generate(RooArgSet(*x,*thecat),Extended()); + +// --- 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(); +} diff --git a/tutorials/roostats/rs603_HLFactoryElaborateExample.C b/tutorials/roostats/rs603_HLFactoryElaborateExample.C new file mode 100644 index 0000000000000000000000000000000000000000..eceeed9d22091d7c34949159df9c32f7253a9879 --- /dev/null +++ b/tutorials/roostats/rs603_HLFactoryElaborateExample.C @@ -0,0 +1,136 @@ +///////////////////////////////////////////////////////////////////////// +// +// '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. +// +// +///////////////////////////////////////////////////////////////////////// + +#ifndef __CINT__ +#include "RooGlobalFunc.h" +#include <fstream> +#include "TString.h" +#include "RooWorkspace.h" +#include "RooRealVar.h" +#include "RooAbsPdf.h" +#include "RooDataSet.h" +#include "RooPlot.h" +#endif + + +// 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 = 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("HLFactoryElaborateExample", + "rs603_card.rs", + false); + +RooRealVar* x = hlf.GetWs()->var("x"); +RooAbsPdf* pdf1 = hlf.GetWs()->pdf("sb_model1"); +RooAbsPdf* pdf2 = hlf.GetWs()->pdf("sb_model2"); + +hlf.AddChannel("model1","sb_model1","flat1","data1"); +hlf.AddChannel("model2","sb_model2","flat2","data2"); + +RooDataSet* data = hlf.GetTotDataSet(); +RooAbsPdf* pdf = hlf.GetTotSigBkgPdf(); +RooCategory* thecat = hlf.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(); + +}