Logo ROOT  
Reference Guide
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///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooChebychev.h"
16#include "RooAddPdf.h"
17#include "RooWorkspace.h"
18#include "RooPlot.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "TFile.h"
22#include "TH1.h"
23using namespace RooFit;
24
26{
27 // C r e a t e m o d e l a n d d a t a s e t
28 // -----------------------------------------------
29
30 // Declare observable x
31 RooRealVar x("x", "x", 0, 10);
32
33 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
34 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
35 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
36 RooRealVar sigma2("sigma2", "width of gaussians", 1);
37
38 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
39 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
40
41 // Build Chebychev polynomial pdf
42 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
43 RooRealVar a1("a1", "a1", 0.2, 0, 1.);
44 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
45
46 // Sum the signal components into a composite signal pdf
47 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
48 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
49
50 // Sum the composite signal and background
51 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
52 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
53
54 // Generate a data sample of 1000 events in x from model
55 RooDataSet *data = model.generate(x, 1000);
56
57 // 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
58 // -----------------------------------------------------------------------------
59
60 // Create a new empty workspace
61 RooWorkspace *w = new RooWorkspace("w", "workspace");
62
63 // Import model and all its components into the workspace
64 w->import(model);
65
66 // Import data into the workspace
67 w->import(*data);
68
69 // Print workspace contents
70 w->Print();
71
72 // S a v e w o r k s p a c e i n f i l e
73 // -------------------------------------------
74
75 // Save the workspace into a ROOT file
76 w->writeToFile("rf502_workspace.root");
77
78 // Workspace will remain in memory after macro finishes
79 gDirectory->Add(w);
80}
#define gDirectory
Definition: TDirectory.h:348
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:34
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18