ROOT logo

From $ROOTSYS/tutorials/roofit/rf502_wspacewrite.C

/////////////////////////////////////////////////////////////////////////
//
// 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #502
// 
// Creating and writing a workspace
//
//
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooWorkspace.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TFile.h"
#include "TH1.h"
using namespace RooFit ;



void rf502_wspacewrite()
{
  // C r e a t e   m o d e l   a n d   d a t a s e t
  // -----------------------------------------------

  // Declare observable x
  RooRealVar x("x","x",0,10) ;

  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
  RooRealVar mean("mean","mean of gaussians",5,0,10) ;
  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
  RooRealVar sigma2("sigma2","width of gaussians",1) ;

  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
  
  // Build Chebychev polynomial p.d.f.  
  RooRealVar a0("a0","a0",0.5,0.,1.) ;
  RooRealVar a1("a1","a1",-0.2,0.,1.) ;
  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;

  // Sum the signal components into a composite signal p.d.f.
  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;

  // Sum the composite signal and background 
  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
  RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;

  // Generate a data sample of 1000 events in x from model
  RooDataSet *data = model.generate(x,1000) ;



  // 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
  // -----------------------------------------------------------------------------

  // Create a new empty workspace
  RooWorkspace *w = new RooWorkspace("w","workspace") ;

  // Import model and all its components into the workspace
  w->import(model) ;

  // Import data into the workspace
  w->import(*data) ;

  // Print workspace contents
  w->Print() ;



  // S a v e   w o r k s p a c e   i n   f i l e
  // -------------------------------------------

  // Save the workspace into a ROOT file
  w->writeToFile("rf502_workspace.root") ;


  // Workspace will remain in memory after macro finishes
  gDirectory->Add(w) ;

}
 rf502_wspacewrite.C:1
 rf502_wspacewrite.C:2
 rf502_wspacewrite.C:3
 rf502_wspacewrite.C:4
 rf502_wspacewrite.C:5
 rf502_wspacewrite.C:6
 rf502_wspacewrite.C:7
 rf502_wspacewrite.C:8
 rf502_wspacewrite.C:9
 rf502_wspacewrite.C:10
 rf502_wspacewrite.C:11
 rf502_wspacewrite.C:12
 rf502_wspacewrite.C:13
 rf502_wspacewrite.C:14
 rf502_wspacewrite.C:15
 rf502_wspacewrite.C:16
 rf502_wspacewrite.C:17
 rf502_wspacewrite.C:18
 rf502_wspacewrite.C:19
 rf502_wspacewrite.C:20
 rf502_wspacewrite.C:21
 rf502_wspacewrite.C:22
 rf502_wspacewrite.C:23
 rf502_wspacewrite.C:24
 rf502_wspacewrite.C:25
 rf502_wspacewrite.C:26
 rf502_wspacewrite.C:27
 rf502_wspacewrite.C:28
 rf502_wspacewrite.C:29
 rf502_wspacewrite.C:30
 rf502_wspacewrite.C:31
 rf502_wspacewrite.C:32
 rf502_wspacewrite.C:33
 rf502_wspacewrite.C:34
 rf502_wspacewrite.C:35
 rf502_wspacewrite.C:36
 rf502_wspacewrite.C:37
 rf502_wspacewrite.C:38
 rf502_wspacewrite.C:39
 rf502_wspacewrite.C:40
 rf502_wspacewrite.C:41
 rf502_wspacewrite.C:42
 rf502_wspacewrite.C:43
 rf502_wspacewrite.C:44
 rf502_wspacewrite.C:45
 rf502_wspacewrite.C:46
 rf502_wspacewrite.C:47
 rf502_wspacewrite.C:48
 rf502_wspacewrite.C:49
 rf502_wspacewrite.C:50
 rf502_wspacewrite.C:51
 rf502_wspacewrite.C:52
 rf502_wspacewrite.C:53
 rf502_wspacewrite.C:54
 rf502_wspacewrite.C:55
 rf502_wspacewrite.C:56
 rf502_wspacewrite.C:57
 rf502_wspacewrite.C:58
 rf502_wspacewrite.C:59
 rf502_wspacewrite.C:60
 rf502_wspacewrite.C:61
 rf502_wspacewrite.C:62
 rf502_wspacewrite.C:63
 rf502_wspacewrite.C:64
 rf502_wspacewrite.C:65
 rf502_wspacewrite.C:66
 rf502_wspacewrite.C:67
 rf502_wspacewrite.C:68
 rf502_wspacewrite.C:69
 rf502_wspacewrite.C:70
 rf502_wspacewrite.C:71
 rf502_wspacewrite.C:72
 rf502_wspacewrite.C:73
 rf502_wspacewrite.C:74
 rf502_wspacewrite.C:75
 rf502_wspacewrite.C:76
 rf502_wspacewrite.C:77
 rf502_wspacewrite.C:78
 rf502_wspacewrite.C:79
 rf502_wspacewrite.C:80
 rf502_wspacewrite.C:81
 rf502_wspacewrite.C:82
 rf502_wspacewrite.C:83
 rf502_wspacewrite.C:84
 rf502_wspacewrite.C:85
 rf502_wspacewrite.C:86
 rf502_wspacewrite.C:87
 rf502_wspacewrite.C:88
 rf502_wspacewrite.C:89
 rf502_wspacewrite.C:90
 rf502_wspacewrite.C:91
 rf502_wspacewrite.C:92
 rf502_wspacewrite.C:93