Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf502_wspacewrite.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -nodraw
4 /// Organisation and simultaneous fits: creating and writing a workspace
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 "RooChebychev.h"
15 #include "RooAddPdf.h"
16 #include "RooWorkspace.h"
17 #include "RooPlot.h"
18 #include "TCanvas.h"
19 #include "TAxis.h"
20 #include "TFile.h"
21 #include "TH1.h"
22 using namespace RooFit;
23 
24 void rf502_wspacewrite()
25 {
26  // C r e a t e m o d e l a n d d a t a s e t
27  // -----------------------------------------------
28 
29  // Declare observable x
30  RooRealVar x("x", "x", 0, 10);
31 
32  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
33  RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
34  RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
35  RooRealVar sigma2("sigma2", "width of gaussians", 1);
36 
37  RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
38  RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
39 
40  // Build Chebychev polynomial p.d.f.
41  RooRealVar a0("a0", "a0", 0.5, 0., 1.);
42  RooRealVar a1("a1", "a1", 0.2, 0, 1.);
43  RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
44 
45  // Sum the signal components into a composite signal p.d.f.
46  RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
47  RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
48 
49  // Sum the composite signal and background
50  RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
51  RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
52 
53  // Generate a data sample of 1000 events in x from model
54  RooDataSet *data = model.generate(x, 1000);
55 
56  // C r e a t e w o r k s p a c e , i m p o r t d a t a a n d m o d e l
57  // -----------------------------------------------------------------------------
58 
59  // Create a new empty workspace
60  RooWorkspace *w = new RooWorkspace("w", "workspace");
61 
62  // Import model and all its components into the workspace
63  w->import(model);
64 
65  // Import data into the workspace
66  w->import(*data);
67 
68  // Print workspace contents
69  w->Print();
70 
71  // S a v e w o r k s p a c e i n f i l e
72  // -------------------------------------------
73 
74  // Save the workspace into a ROOT file
75  w->writeToFile("rf502_workspace.root");
76 
77  // Workspace will remain in memory after macro finishes
78  gDirectory->Add(w);
79 }