Logo ROOT   6.18/05
Reference Guide
rf505_asciicfg.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Organisation and simultaneous fits: reading and writing ASCII configuration files
5///
6/// \macro_output
7/// \macro_code
8/// \author 07/2008 - Wouter Verkerke
9
10#include "RooRealVar.h"
11#include "RooDataSet.h"
12#include "RooGaussian.h"
13#include "RooConstVar.h"
14#include "RooPolynomial.h"
15#include "RooAddPdf.h"
16#include "TCanvas.h"
17#include "TAxis.h"
18#include "RooPlot.h"
19using namespace RooFit;
20
21void rf505_asciicfg()
22{
23 // C r e a t e p d f
24 // ------------------
25
26 // Construct gauss(x,m,s)
27 RooRealVar x("x", "x", -10, 10);
28 RooRealVar m("m", "m", 0, -10, 10);
29 RooRealVar s("s", "s", 1, -10, 10);
30 RooGaussian gauss("g", "g", x, m, s);
31
32 // Construct poly(x,p0)
33 RooRealVar p0("p0", "p0", 0.01, 0., 1.);
34 RooPolynomial poly("p", "p", x, p0);
35
36 // Construct model = f*gauss(x) + (1-f)*poly(x)
37 RooRealVar f("f", "f", 0.5, 0., 1.);
38 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
39
40 // F i t m o d e l t o t o y d a t a
41 // -----------------------------------------
42
43 RooDataSet *d = model.generate(x, 1000);
44 model.fitTo(*d);
45
46 // W r i t e p a r a m e t e r s t o a s c i i f i l e
47 // -----------------------------------------------------------
48
49 // Obtain set of parameters
50 RooArgSet *params = model.getParameters(x);
51
52 // Write parameters to file
53 params->writeToFile("rf505_asciicfg_example.txt");
54
55 TString dir1 = gROOT->GetTutorialDir() ;
56 dir1.Append("/roofit/rf505_asciicfg.txt") ;
57 TString dir2 = "rf505_asciicfg_example.txt";
58
59 // R e a d p a r a m e t e r s f r o m a s c i i f i l e
60 // ----------------------------------------------------------------
61
62 // Read parameters from file
63 params->readFromFile(dir2);
64 params->Print("v");
65
66 // Read parameters from section 'Section2' of file
67 params->readFromFile(dir1, 0, "Section2");
68 params->Print("v");
69
70 // Read parameters from section 'Section3' of file. Mark all
71 // variables that were processed with the "READ" attribute
72 params->readFromFile(dir1, "READ", "Section3");
73
74 // Print the list of parameters that were not read from Section3
75 cout << "The following parameters of the were _not_ read from Section3: "
76 << (*params->selectByAttrib("READ", kFALSE)) << endl;
77
78 // Read parameters from section 'Section4' of file, which contains
79 // 'include file' statement of rf505_asciicfg_example.txt
80 // so that we effective read the same
81
82 params->readFromFile(dir1, 0, "Section4");
83 params->Print("v");
84}
#define d(i)
Definition: RSha256.hxx:102
#define f(i)
Definition: RSha256.hxx:104
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define gROOT
Definition: TROOT.h:414
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
RooAbsCollection * selectByAttrib(const char *name, Bool_t value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
Bool_t readFromFile(const char *fileName, const char *flagReadAtt=0, const char *section=0, Bool_t verbose=kFALSE)
Read contents of the argset from specified file.
Definition: RooArgSet.cxx:660
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:644
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
Basic string class.
Definition: TString.h:131
TString & Append(const char *cs)
Definition: TString.h:559
Double_t x[n]
Definition: legend1.C:17
Template specialisation used in RooAbsArg:
static constexpr double s
static constexpr double gauss
auto * m
Definition: textangle.C:8