00001 ///////////////////////////////////////////////////////////////////////// 00002 // 00003 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #503 00004 // 00005 // Reading and using a workspace 00006 // 00007 // --> The input file for this macro is generated by rf502_wspaceread.C 00008 // 00009 // 00010 // 07/2008 - Wouter Verkerke 00011 // 00012 ///////////////////////////////////////////////////////////////////////// 00013 00014 #ifndef __CINT__ 00015 #include "RooGlobalFunc.h" 00016 #endif 00017 #include "RooRealVar.h" 00018 #include "RooDataSet.h" 00019 #include "RooGaussian.h" 00020 #include "RooConstVar.h" 00021 #include "RooChebychev.h" 00022 #include "RooAddPdf.h" 00023 #include "RooWorkspace.h" 00024 #include "RooPlot.h" 00025 #include "TCanvas.h" 00026 #include "TAxis.h" 00027 #include "TFile.h" 00028 #include "TH1.h" 00029 using namespace RooFit ; 00030 00031 00032 void rf503_wspaceread() 00033 { 00034 // R e a d w o r k s p a c e f r o m f i l e 00035 // ----------------------------------------------- 00036 00037 // Open input file with workspace (generated by rf14_wspacewrite) 00038 TFile *f = new TFile("rf502_workspace.root") ; 00039 00040 // Retrieve workspace from file 00041 RooWorkspace* w = (RooWorkspace*) f->Get("w") ; 00042 00043 00044 00045 // 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 00046 // ----------------------------------------------------------------- 00047 00048 // Retrieve x,model and data from workspace 00049 RooRealVar* x = w->var("x") ; 00050 RooAbsPdf* model = w->pdf("model") ; 00051 RooAbsData* data = w->data("modelData") ; 00052 00053 // Print structure of composite p.d.f. 00054 model->Print("t") ; 00055 00056 00057 // F i t m o d e l t o d a t a , p l o t m o d e l 00058 // --------------------------------------------------------- 00059 00060 // Fit model to data 00061 model->fitTo(*data) ; 00062 00063 // Plot data and PDF overlaid 00064 RooPlot* xframe = x->frame(Title("Model and data read from workspace")) ; 00065 data->plotOn(xframe) ; 00066 model->plotOn(xframe) ; 00067 00068 // Overlay the background component of model with a dashed line 00069 model->plotOn(xframe,Components("bkg"),LineStyle(kDashed)) ; 00070 00071 // Overlay the background+sig2 components of model with a dotted line 00072 model->plotOn(xframe,Components("bkg,sig2"),LineStyle(kDotted)) ; 00073 00074 00075 00076 // Draw the frame on the canvas 00077 new TCanvas("rf503_wspaceread","rf503_wspaceread",600,600) ; 00078 gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ; 00079 00080 00081 }