ROOT logo
/////////////////////////////////////////////////////////////////////////
//
// 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #503
// 
// Reading and using a workspace
//
// --> The input file for this macro is generated by rf502_wspaceread.C
// 
//
// 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 rf503_wspaceread()
{
  // R e a d   w o r k s p a c e   f r o m   f i l e
  // -----------------------------------------------

  // Open input file with workspace (generated by rf14_wspacewrite)
  TFile *f = new TFile("rf502_workspace.root") ;

  // Retrieve workspace from file
  RooWorkspace* w = (RooWorkspace*) f->Get("w") ;



  // R e t r i e v e   p d f ,   d a t a   f r o m   w o r k s p a c e
  // -----------------------------------------------------------------

  // Retrieve x,model and data from workspace
  RooRealVar* x = w->var("x") ;
  RooAbsPdf* model = w->pdf("model") ;
  RooAbsData* data = w->data("modelData") ;

  // Print structure of composite p.d.f.
  model->Print("t") ;


  // F i t   m o d e l   t o   d a t a ,   p l o t   m o d e l 
  // ---------------------------------------------------------

  // Fit model to data
  model->fitTo(*data) ;
  
  // Plot data and PDF overlaid
  RooPlot* xframe = x->frame(Title("Model and data read from workspace")) ;
  data->plotOn(xframe) ;
  model->plotOn(xframe) ;

  // Overlay the background component of model with a dashed line
  model->plotOn(xframe,Components("bkg"),LineStyle(kDashed)) ;

  // Overlay the background+sig2 components of model with a dotted line
  model->plotOn(xframe,Components("bkg,sig2"),LineStyle(kDotted)) ;



  // Draw the frame on the canvas
  new TCanvas("rf503_wspaceread","rf503_wspaceread",600,600) ;
  gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;


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