rf502_wspacewrite.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #502
00004 // 
00005 // Creating and writing a workspace
00006 //
00007 //
00008 // 07/2008 - Wouter Verkerke 
00009 //
00010 /////////////////////////////////////////////////////////////////////////
00011 
00012 #ifndef __CINT__
00013 #include "RooGlobalFunc.h"
00014 #endif
00015 #include "RooRealVar.h"
00016 #include "RooDataSet.h"
00017 #include "RooGaussian.h"
00018 #include "RooConstVar.h"
00019 #include "RooChebychev.h"
00020 #include "RooAddPdf.h"
00021 #include "RooWorkspace.h"
00022 #include "RooPlot.h"
00023 #include "TCanvas.h"
00024 #include "TAxis.h"
00025 #include "TFile.h"
00026 #include "TH1.h"
00027 using namespace RooFit ;
00028 
00029 
00030 
00031 void rf502_wspacewrite()
00032 {
00033   // C r e a t e   m o d e l   a n d   d a t a s e t
00034   // -----------------------------------------------
00035 
00036   // Declare observable x
00037   RooRealVar x("x","x",0,10) ;
00038 
00039   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00040   RooRealVar mean("mean","mean of gaussians",5,0,10) ;
00041   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00042   RooRealVar sigma2("sigma2","width of gaussians",1) ;
00043 
00044   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00045   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00046   
00047   // Build Chebychev polynomial p.d.f.  
00048   RooRealVar a0("a0","a0",0.5,0.,1.) ;
00049   RooRealVar a1("a1","a1",-0.2,0.,1.) ;
00050   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00051 
00052   // Sum the signal components into a composite signal p.d.f.
00053   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00054   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00055 
00056   // Sum the composite signal and background 
00057   RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
00058   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
00059 
00060   // Generate a data sample of 1000 events in x from model
00061   RooDataSet *data = model.generate(x,1000) ;
00062 
00063 
00064 
00065   // 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
00066   // -----------------------------------------------------------------------------
00067 
00068   // Create a new empty workspace
00069   RooWorkspace *w = new RooWorkspace("w","workspace") ;
00070 
00071   // Import model and all its components into the workspace
00072   w->import(model) ;
00073 
00074   // Import data into the workspace
00075   w->import(*data) ;
00076 
00077   // Print workspace contents
00078   w->Print() ;
00079 
00080 
00081 
00082   // S a v e   w o r k s p a c e   i n   f i l e
00083   // -------------------------------------------
00084 
00085   // Save the workspace into a ROOT file
00086   w->writeToFile("rf502_workspace.root") ;
00087 
00088 
00089   // Workspace will remain in memory after macro finishes
00090   gDirectory->Add(w) ;
00091 
00092 }

Generated on Tue Jul 5 15:45:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1