Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 /// \author 07/2008 - Wouter Verkerke
9 
10 #include "RooRealVar.h"
11 #include "RooDataSet.h"
12 #include "RooGaussian.h"
13 #include "RooConstVar.h"
14 #include "RooPolynomial.h"
15 #include "RooAddPdf.h"
16 #include "TCanvas.h"
17 #include "TAxis.h"
18 #include "RooPlot.h"
19 using namespace RooFit;
20 
21 void rf505_asciicfg()
22 {
23  // C r e a t e p d f
24  // ------------------
25 
26  // Construct gauss(x,m,s)
27  RooRealVar x("x", "x", -10, 10);
28  RooRealVar m("m", "m", 0, -10, 10);
29  RooRealVar s("s", "s", 1, -10, 10);
30  RooGaussian gauss("g", "g", x, m, s);
31 
32  // Construct poly(x,p0)
33  RooRealVar p0("p0", "p0", 0.01, 0., 1.);
34  RooPolynomial poly("p", "p", x, p0);
35 
36  // Construct model = f*gauss(x) + (1-f)*poly(x)
37  RooRealVar f("f", "f", 0.5, 0., 1.);
38  RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
39 
40  // F i t m o d e l t o t o y d a t a
41  // -----------------------------------------
42 
43  RooDataSet *d = model.generate(x, 1000);
44  model.fitTo(*d);
45 
46  // 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
47  // -----------------------------------------------------------
48 
49  // Obtain set of parameters
50  RooArgSet *params = model.getParameters(x);
51 
52  // Write parameters to file
53  params->writeToFile("rf505_asciicfg_example.txt");
54 
55  TString dir1 = gROOT->GetTutorialDir() ;
56  dir1.Append("/roofit/rf505_asciicfg.txt") ;
57  TString dir2 = "rf505_asciicfg_example.txt";
58 
59  // 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
60  // ----------------------------------------------------------------
61 
62  // Read parameters from file
63  params->readFromFile(dir2);
64  params->Print("v");
65 
66  // Read parameters from section 'Section2' of file
67  params->readFromFile(dir1, 0, "Section2");
68  params->Print("v");
69 
70  // Read parameters from section 'Section3' of file. Mark all
71  // variables that were processed with the "READ" attribute
72  params->readFromFile(dir1, "READ", "Section3");
73 
74  // Print the list of parameters that were not read from Section3
75  cout << "The following parameters of the were _not_ read from Section3: "
76  << (*params->selectByAttrib("READ", kFALSE)) << endl;
77 
78  // Read parameters from section 'Section4' of file, which contains
79  // 'include file' statement of rf505_asciicfg_example.txt
80  // so that we effective read the same
81 
82  params->readFromFile(dir1, 0, "Section4");
83  params->Print("v");
84 }