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 "RooConstVar.h"
16 #include "RooPolynomial.h"
17 #include "RooAddPdf.h"
18 #include "TCanvas.h"
19 #include "TAxis.h"
20 #include "RooPlot.h"
21 using namespace RooFit;
22 
23 void 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 }
m
auto * m
Definition: textangle.C:8
RooArgSet::writeToFile
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:517
RooAddPdf
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:104
RooGaussian.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooAddPdf.h
TCanvas.h
TString
Basic string class.
Definition: TString.h:136
RooDataSet.h
RooPolynomial.h
rf505_asciicfg
Definition: rf505_asciicfg.py:1
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:263
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooPolynomial
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
RooPlot.h
RooRealVar.h
RooConstVar.h
TAxis.h
RooArgSet::readFromFile
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:533
d
#define d(i)
Definition: RSha256.hxx:102
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooAbsCollection::Print
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
Definition: RooAbsCollection.h:200
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
RooAbsCollection::selectByAttrib
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...
Definition: RooAbsCollection.cxx:677
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
gROOT
#define gROOT
Definition: TROOT.h:406