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"
22 using namespace RooFit;
23 
24 void 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 }
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:648
RooAddPdf
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:122
RooGaussian.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAddPdf.h
TCanvas.h
TString
Definition: TString.h:136
RooDataSet.h
RooPolynomial.h
rf505_asciicfg
Definition: rf505_asciicfg.py:1
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:269
RooFit
Definition: RooCFunction1Binding.h:29
RooPolynomial
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:664
d
#define d(i)
Definition: RSha256.hxx:120
RooDataSet
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:199
RooRealVar
Definition: RooRealVar.h:35
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
Definition: RooArgSet.h:28
gROOT
#define gROOT
Definition: TROOT.h:406