Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rs601_HLFactoryexample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook -js
4 /// High Level Factory: creation of a simple 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 "TROOT.h"
15 #include "RooGlobalFunc.h"
16 #include "RooWorkspace.h"
17 #include "RooRealVar.h"
18 #include "RooAbsPdf.h"
19 #include "RooDataSet.h"
20 #include "RooPlot.h"
21 #include "RooStats/HLFactory.h"
22 
23 // use this order for safety on library loading
24 using namespace RooFit;
25 using namespace RooStats;
26 using namespace std;
27 
28 void rs601_HLFactoryexample()
29 {
30 
31  // --- Build the datacard and dump to file---
32 
33  TString card_name("HLFavtoryexample.rs");
34  ofstream ofile(card_name);
35  ofile << "// The simplest card\n\n"
36  << "gauss = Gaussian(mes[5.20,5.30],mean[5.28,5.2,5.3],width[0.0027,0.001,1]);\n"
37  << "argus = ArgusBG(mes,5.291,argpar[-20,-100,-1]);\n"
38  << "sum = SUM(nsig[200,0,10000]*gauss,nbkg[800,0,10000]*argus);\n\n";
39 
40  ofile.close();
41 
42  HLFactory hlf("HLFavtoryexample", card_name, false);
43 
44  // --- Take elements out of the internal workspace ---
45 
46  auto w = hlf.GetWs();
47 
48  auto mes = dynamic_cast<RooRealVar *>(w->arg("mes"));
49  auto sum = dynamic_cast<RooAbsPdf *>(w->pdf("sum"));
50  auto argus = dynamic_cast<RooAbsPdf *>(w->pdf("argus"));
51 
52  // --- Generate a toyMC sample from composite PDF ---
53  auto data = sum->generate(*mes, 2000);
54 
55  // --- Perform extended ML fit of composite PDF to toy data ---
56  sum->fitTo(*data);
57 
58  // --- Plot toy data and composite PDF overlaid ---
59  auto mesframe = mes->frame();
60  data->plotOn(mesframe);
61  sum->plotOn(mesframe);
62  sum->plotOn(mesframe, Components(*argus), LineStyle(kDashed));
63 
64  gROOT->SetStyle("Plain");
65  mesframe->Draw();
66 }