Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rs603_HLFactoryElaborateExample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook -js
4 /// High Level Factory: creating a complex combined model.
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 ///
10 /// \author Danilo Piparo
11 
12 #include <fstream>
13 #include "TString.h"
14 #include "TFile.h"
15 #include "TROOT.h"
16 #include "RooGlobalFunc.h"
17 #include "RooWorkspace.h"
18 #include "RooRealVar.h"
19 #include "RooAbsPdf.h"
20 #include "RooDataSet.h"
21 #include "RooPlot.h"
22 #include "RooStats/HLFactory.h"
23 
24 // use this order for safety on library loading
25 using namespace RooFit;
26 using namespace RooStats;
27 using namespace std;
28 
29 void rs603_HLFactoryElaborateExample()
30 {
31 
32  // --- Prepare the 2 needed datacards for this example ---
33 
34  TString card_name("rs603_card_WsMaker.rs");
35  ofstream ofile(card_name);
36  ofile << "// The simplest card for combination\n\n";
37  ofile << "gauss1 = Gaussian(x[0,100],mean1[50,0,100],4);\n";
38  ofile << "flat1 = Polynomial(x,0);\n";
39  ofile << "sb_model1 = SUM(nsig1[120,0,300]*gauss1 , nbkg1[100,0,1000]*flat1);\n\n";
40  ofile << "echo In the middle!;\n\n";
41  ofile << "gauss2 = Gaussian(x,mean2[80,0,100],5);\n";
42  ofile << "flat2 = Polynomial(x,0);\n";
43  ofile << "sb_model2 = SUM(nsig2[90,0,400]*gauss2 , nbkg2[80,0,1000]*flat2);\n\n";
44  ofile << "echo At the end!;\n";
45  ofile.close();
46 
47  TString card_name2("rs603_card.rs");
48  ofstream ofile2(card_name2);
49  ofile2 << "// The simplest card for combination\n\n";
50  ofile2 << "gauss1 = Gaussian(x[0,100],mean1[50,0,100],4);\n";
51  ofile2 << "flat1 = Polynomial(x,0);\n";
52  ofile2 << "sb_model1 = SUM(nsig1[120,0,300]*gauss1 , nbkg1[100,0,1000]*flat1);\n\n";
53  ofile2 << "echo In the middle!;\n\n";
54  ofile2 << "gauss2 = Gaussian(x,mean2[80,0,100],5);\n";
55  ofile2 << "flat2 = Polynomial(x,0);\n";
56  ofile2 << "sb_model2 = SUM(nsig2[90,0,400]*gauss2 , nbkg2[80,0,1000]*flat2);\n\n";
57  ofile2 << "#include rs603_included_card.rs;\n\n";
58  ofile2 << "echo At the end!;\n";
59  ofile2.close();
60 
61  TString card_name3("rs603_included_card.rs");
62  ofstream ofile3(card_name3);
63  ofile3 << "echo Now reading the included file!;\n\n";
64  ofile3 << "echo Including datasets in a Workspace in a Root file...;\n";
65  ofile3 << "data1 = import(rs603_infile.root,\n";
66  ofile3 << " rs603_ws,\n";
67  ofile3 << " data1);\n\n";
68  ofile3 << "data2 = import(rs603_infile.root,\n";
69  ofile3 << " rs603_ws,\n";
70  ofile3 << " data2);\n";
71  ofile3.close();
72 
73  // --- Produce the two separate datasets into a WorkSpace ---
74 
75  HLFactory hlf("HLFactoryComplexExample", "rs603_card_WsMaker.rs", false);
76 
77  auto x = static_cast<RooRealVar *>(hlf.GetWs()->arg("x"));
78  auto pdf1 = hlf.GetWs()->pdf("sb_model1");
79  auto pdf2 = hlf.GetWs()->pdf("sb_model2");
80 
81  RooWorkspace w("rs603_ws");
82 
83  auto data1 = pdf1->generate(RooArgSet(*x), Extended());
84  data1->SetName("data1");
85  w.import(*data1);
86 
87  auto data2 = pdf2->generate(RooArgSet(*x), Extended());
88  data2->SetName("data2");
89  w.import(*data2);
90 
91  // --- Write the WorkSpace into a rootfile ---
92 
93  TFile outfile("rs603_infile.root", "RECREATE");
94  w.Write();
95  outfile.Close();
96 
97  cout << "-------------------------------------------------------------------\n"
98  << " Rootfile and Workspace prepared \n"
99  << "-------------------------------------------------------------------\n";
100 
101  HLFactory hlf_2("HLFactoryElaborateExample", "rs603_card.rs", false);
102 
103  x = hlf_2.GetWs()->var("x");
104  pdf1 = hlf_2.GetWs()->pdf("sb_model1");
105  pdf2 = hlf_2.GetWs()->pdf("sb_model2");
106 
107  hlf_2.AddChannel("model1", "sb_model1", "flat1", "data1");
108  hlf_2.AddChannel("model2", "sb_model2", "flat2", "data2");
109 
110  auto data = hlf_2.GetTotDataSet();
111  auto pdf = hlf_2.GetTotSigBkgPdf();
112  auto thecat = hlf_2.GetTotCategory();
113 
114  // --- Perform extended ML fit of composite PDF to toy data ---
115  pdf->fitTo(*data);
116 
117  // --- Plot toy data and composite PDF overlaid ---
118  auto xframe = x->frame();
119 
120  data->plotOn(xframe);
121  thecat->setIndex(0);
122  pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));
123 
124  thecat->setIndex(1);
125  pdf->plotOn(xframe, Slice(*thecat), ProjWData(*thecat, *data));
126 
127  gROOT->SetStyle("Plain");
128 
129  xframe->Draw();
130 }