Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "RooConstVar.h"
16#include "RooPolynomial.h"
17#include "RooAddPdf.h"
18#include "TCanvas.h"
19#include "TAxis.h"
20#include "RooPlot.h"
21using namespace RooFit;
22
23void rf505_asciicfg()
24{
25 // C r e a t e p d f
26 // ------------------
27
28 // Construct gauss(x,m,s)
29 RooRealVar x("x", "x", -10, 10);
30 RooRealVar m("m", "m", 0, -10, 10);
31 RooRealVar s("s", "s", 1, -10, 10);
32 RooGaussian gauss("g", "g", x, m, s);
33
34 // Construct poly(x,p0)
35 RooRealVar p0("p0", "p0", 0.01, 0., 1.);
36 RooPolynomial poly("p", "p", x, p0);
37
38 // Construct model = f*gauss(x) + (1-f)*poly(x)
39 RooRealVar f("f", "f", 0.5, 0., 1.);
40 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
41
42 // F i t m o d e l t o t o y d a t a
43 // -----------------------------------------
44
45 RooDataSet *d = model.generate(x, 1000);
46 model.fitTo(*d);
47
48 // 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
49 // -----------------------------------------------------------
50
51 // Obtain set of parameters
52 RooArgSet *params = model.getParameters(x);
53
54 // Write parameters to file
55 params->writeToFile("rf505_asciicfg_example.txt");
56
57 TString dir1 = gROOT->GetTutorialDir() ;
58 dir1.Append("/roofit/rf505_asciicfg.txt") ;
59 TString dir2 = "rf505_asciicfg_example.txt";
60
61 // 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
62 // ----------------------------------------------------------------
63
64 // Read parameters from file
65 params->readFromFile(dir2);
66 params->Print("v");
67
68 // Read parameters from section 'Section2' of file
69 params->readFromFile(dir1, 0, "Section2");
70 params->Print("v");
71
72 // Read parameters from section 'Section3' of file. Mark all
73 // variables that were processed with the "READ" attribute
74 params->readFromFile(dir1, "READ", "Section3");
75
76 // Print the list of parameters that were not read from Section3
77 cout << "The following parameters of the were _not_ read from Section3: "
78 << (*params->selectByAttrib("READ", kFALSE)) << endl;
79
80 // Read parameters from section 'Section4' of file, which contains
81 // 'include file' statement of rf505_asciicfg_example.txt
82 // so that we effective read the same
83
84 params->readFromFile(dir1, 0, "Section4");
85 params->Print("v");
86}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
const Bool_t kFALSE
Definition RtypesCore.h:92
#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:32
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
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.
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:33
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
RooPolynomial implements a polynomial p.d.f of the form.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
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...
auto * m
Definition textangle.C:8