Logo ROOT  
Reference Guide
rf505_asciicfg.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4///
5/// Organisation and simultaneous fits: reading and writing ASCII configuration files
6///
7/// \macro_output
8/// \macro_code
9///
10/// \date 07/2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooConstVar.h"
17#include "RooPolynomial.h"
18#include "RooAddPdf.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22using namespace RooFit;
23
24void rf505_asciicfg()
25{
26 // C r e a t e p d f
27 // ------------------
28
29 // Construct gauss(x,m,s)
30 RooRealVar x("x", "x", -10, 10);
31 RooRealVar m("m", "m", 0, -10, 10);
32 RooRealVar s("s", "s", 1, -10, 10);
33 RooGaussian gauss("g", "g", x, m, s);
34
35 // Construct poly(x,p0)
36 RooRealVar p0("p0", "p0", 0.01, 0., 1.);
37 RooPolynomial poly("p", "p", x, p0);
38
39 // Construct model = f*gauss(x) + (1-f)*poly(x)
40 RooRealVar f("f", "f", 0.5, 0., 1.);
41 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
42
43 // F i t m o d e l t o t o y d a t a
44 // -----------------------------------------
45
46 RooDataSet *d = model.generate(x, 1000);
47 model.fitTo(*d);
48
49 // 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
50 // -----------------------------------------------------------
51
52 // Obtain set of parameters
53 RooArgSet *params = model.getParameters(x);
54
55 // Write parameters to file
56 params->writeToFile("rf505_asciicfg_example.txt");
57
58 TString dir1 = gROOT->GetTutorialDir() ;
59 dir1.Append("/roofit/rf505_asciicfg.txt") ;
60 TString dir2 = "rf505_asciicfg_example.txt";
61
62 // 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
63 // ----------------------------------------------------------------
64
65 // Read parameters from file
66 params->readFromFile(dir2);
67 params->Print("v");
68
69 // Read parameters from section 'Section2' of file
70 params->readFromFile(dir1, 0, "Section2");
71 params->Print("v");
72
73 // Read parameters from section 'Section3' of file. Mark all
74 // variables that were processed with the "READ" attribute
75 params->readFromFile(dir1, "READ", "Section3");
76
77 // Print the list of parameters that were not read from Section3
78 cout << "The following parameters of the were _not_ read from Section3: "
79 << (*params->selectByAttrib("READ", kFALSE)) << endl;
80
81 // Read parameters from section 'Section4' of file, which contains
82 // 'include file' statement of rf505_asciicfg_example.txt
83 // so that we effective read the same
84
85 params->readFromFile(dir1, 0, "Section4");
86 params->Print("v");
87}
#define d(i)
Definition: RSha256.hxx:102
#define f(i)
Definition: RSha256.hxx:104
const Bool_t kFALSE
Definition: RtypesCore.h:90
#define gROOT
Definition: TROOT.h:406
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:492
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:476
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
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 variable that can be changed from the outside.
Definition: RooRealVar.h:35
Basic string class.
Definition: TString.h:131
TString & Append(const char *cs)
Definition: TString.h:559
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
static constexpr double s
static constexpr double gauss
auto * m
Definition: textangle.C:8