Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf503_wspaceread.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Organisation and simultaneous fits: reading and using a workspace
5 ///
6 /// --> The input file for this macro is generated by rf502_wspaceread.C
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 #include "RooRealVar.h"
14 #include "RooDataSet.h"
15 #include "RooGaussian.h"
16 #include "RooConstVar.h"
17 #include "RooChebychev.h"
18 #include "RooAddPdf.h"
19 #include "RooWorkspace.h"
20 #include "RooPlot.h"
21 #include "TCanvas.h"
22 #include "TAxis.h"
23 #include "TFile.h"
24 #include "TH1.h"
25 using namespace RooFit;
26 
27 void rf503_wspaceread()
28 {
29  // R e a d w o r k s p a c e f r o m f i l e
30  // -----------------------------------------------
31 
32  // Open input file with workspace (generated by rf14_wspacewrite)
33  TFile *f = new TFile("rf502_workspace.root");
34 
35  // Retrieve workspace from file
36  RooWorkspace *w = (RooWorkspace *)f->Get("w");
37 
38  // 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
39  // -----------------------------------------------------------------
40 
41  // Retrieve x,model and data from workspace
42  RooRealVar *x = w->var("x");
43  RooAbsPdf *model = w->pdf("model");
44  RooAbsData *data = w->data("modelData");
45 
46  // Print structure of composite p.d.f.
47  model->Print("t");
48 
49  // F i t m o d e l t o d a t a , p l o t m o d e l
50  // ---------------------------------------------------------
51 
52  // Fit model to data
53  model->fitTo(*data);
54 
55  // Plot data and PDF overlaid
56  RooPlot *xframe = x->frame(Title("Model and data read from workspace"));
57  data->plotOn(xframe);
58  model->plotOn(xframe);
59 
60  // Overlay the background component of model with a dashed line
61  model->plotOn(xframe, Components("bkg"), LineStyle(kDashed));
62 
63  // Overlay the background+sig2 components of model with a dotted line
64  model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted));
65 
66  // Draw the frame on the canvas
67  new TCanvas("rf503_wspaceread", "rf503_wspaceread", 600, 600);
68  gPad->SetLeftMargin(0.15);
69  xframe->GetYaxis()->SetTitleOffset(1.4);
70  xframe->Draw();
71 }