Logo ROOT  
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///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooPolynomial.h"
16#include "RooAddPdf.h"
17#include "TCanvas.h"
18#include "TAxis.h"
19#include "RooPlot.h"
20using namespace RooFit;
21
22void rf505_asciicfg()
23{
24 // C r e a t e p d f
25 // ------------------
26
27 // Construct gauss(x,m,s)
28 RooRealVar x("x", "x", -10, 10);
29 RooRealVar m("m", "m", 0, -10, 10);
30 RooRealVar s("s", "s", 1, -10, 10);
31 RooGaussian gauss("g", "g", x, m, s);
32
33 // Construct poly(x,p0)
34 RooRealVar p0("p0", "p0", 0.01, 0., 1.);
35 RooPolynomial poly("p", "p", x, p0);
36
37 // Construct model = f*gauss(x) + (1-f)*poly(x)
38 RooRealVar f("f", "f", 0.5, 0., 1.);
39 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
40
41 // F i t m o d e l t o t o y d a t a
42 // -----------------------------------------
43
44 RooDataSet *d = model.generate(x, 1000);
45 model.fitTo(*d);
46
47 // 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
48 // -----------------------------------------------------------
49
50 // Obtain set of parameters
51 RooArgSet *params = model.getParameters(x);
52
53 // Write parameters to file
54 params->writeToFile("rf505_asciicfg_example.txt");
55
56 TString dir1 = gROOT->GetTutorialDir() ;
57 dir1.Append("/roofit/rf505_asciicfg.txt") ;
58 TString dir2 = "rf505_asciicfg_example.txt";
59
60 // 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
61 // ----------------------------------------------------------------
62
63 // Read parameters from file
64 params->readFromFile(dir2);
65 params->Print("v");
66
67 // Read parameters from section 'Section2' of file
68 params->readFromFile(dir1, 0, "Section2");
69 params->Print("v");
70
71 // Read parameters from section 'Section3' of file. Mark all
72 // variables that were processed with the "READ" attribute
73 params->readFromFile(dir1, "READ", "Section3");
74
75 // Print the list of parameters that were not read from Section3
76 cout << "The following parameters of the were _not_ read from Section3: "
77 << (*params->selectByAttrib("READ", false)) << endl;
78
79 // Read parameters from section 'Section4' of file, which contains
80 // 'include file' statement of rf505_asciicfg_example.txt
81 // so that we effective read the same
82
83 params->readFromFile(dir1, 0, "Section4");
84 params->Print("v");
85}
#define d(i)
Definition: RSha256.hxx:102
#define f(i)
Definition: RSha256.hxx:104
#define gROOT
Definition: TROOT.h:404
RooAbsCollection * selectByAttrib(const char *name, bool value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:34
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:305
bool readFromFile(const char *fileName, const char *flagReadAtt=nullptr, const char *section=nullptr, bool verbose=false)
Read contents of the argset from specified file.
Definition: RooArgSet.cxx:321
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
Basic string class.
Definition: TString.h:136
TString & Append(const char *cs)
Definition: TString.h:564
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: Common.h:18
static constexpr double s
static constexpr double gauss
TMarker m
Definition: textangle.C:8