ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf502_wspacewrite.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #502
4 ///
5 /// Creating and writing a workspace
6 ///
7 ///
8 /// \macro_code
9 /// \author 07/2008 - Wouter Verkerke
10 
11 
12 #ifndef __CINT__
13 #include "RooGlobalFunc.h"
14 #endif
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooChebychev.h"
20 #include "RooAddPdf.h"
21 #include "RooWorkspace.h"
22 #include "RooPlot.h"
23 #include "TCanvas.h"
24 #include "TAxis.h"
25 #include "TFile.h"
26 #include "TH1.h"
27 using namespace RooFit ;
28 
29 
30 
31 void rf502_wspacewrite()
32 {
33  // C r e a t e m o d e l a n d d a t a s e t
34  // -----------------------------------------------
35 
36  // Declare observable x
37  RooRealVar x("x","x",0,10) ;
38 
39  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
40  RooRealVar mean("mean","mean of gaussians",5,0,10) ;
41  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
42  RooRealVar sigma2("sigma2","width of gaussians",1) ;
43 
44  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
45  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
46 
47  // Build Chebychev polynomial p.d.f.
48  RooRealVar a0("a0","a0",0.5,0.,1.) ;
49  RooRealVar a1("a1","a1",-0.2,0.,1.) ;
50  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
51 
52  // Sum the signal components into a composite signal p.d.f.
53  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
54  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
55 
56  // Sum the composite signal and background
57  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
58  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
59 
60  // Generate a data sample of 1000 events in x from model
61  RooDataSet *data = model.generate(x,1000) ;
62 
63 
64 
65  // 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
66  // -----------------------------------------------------------------------------
67 
68  // Create a new empty workspace
69  RooWorkspace *w = new RooWorkspace("w","workspace") ;
70 
71  // Import model and all its components into the workspace
72  w->import(model) ;
73 
74  // Import data into the workspace
75  w->import(*data) ;
76 
77  // Print workspace contents
78  w->Print() ;
79 
80 
81 
82  // S a v e w o r k s p a c e i n f i l e
83  // -------------------------------------------
84 
85  // Save the workspace into a ROOT file
86  w->writeToFile("rf502_workspace.root") ;
87 
88 
89  // Workspace will remain in memory after macro finishes
90  gDirectory->Add(w) ;
91 
92 }
friend class RooWorkspace
Definition: RooAbsArg.h:472
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
friend class RooArgSet
Definition: RooAbsArg.h:469
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
Bool_t writeToFile(const char *fileName, Bool_t recreate=kTRUE)
Save this current workspace into given file.
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
void Print(Option_t *opts=0) const
Print contents of the workspace.
#define gDirectory
Definition: TDirectory.h:221
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooAbsMoment * mean(RooRealVar &obs)
Definition: RooAbsReal.h:297
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42