rf509_wsinteractive.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #509
00004 // 
00005 //  Easy CINT interactive access to workspace contents through a 
00006 //  'C++' namespace in CINT that maps the workspace contents in a typesafe way
00007 //
00008 //  *********************************************************************************
00009 //  *** NB: This macro exploits a feature native to CINT and _cannot_ be compiled ***
00010 //  *********************************************************************************
00011 //
00012 // 04/2009 - Wouter Verkerke 
00013 //
00014 /////////////////////////////////////////////////////////////////////////
00015 
00016 #ifndef __CINT__
00017 #include "RooGlobalFunc.h"
00018 #endif
00019 #include "RooRealVar.h"
00020 #include "RooDataSet.h"
00021 #include "RooGaussian.h"
00022 #include "RooConstVar.h"
00023 #include "RooChebychev.h"
00024 #include "RooAddPdf.h"
00025 #include "RooWorkspace.h"
00026 #include "RooPlot.h"
00027 #include "TCanvas.h"
00028 #include "TAxis.h"
00029 #include "TFile.h"
00030 #include "TH1.h"
00031 using namespace RooFit ;
00032 
00033 
00034 void fillWorkspace(RooWorkspace& w) ;
00035 
00036 void rf509_wsinteractive()
00037 {
00038   // C r e a t e  a n d   f i l l   w o r k s p a c e
00039   // ------------------------------------------------
00040 
00041   // Create a workspace named 'w' that exports its contents to 
00042   // a same-name C++ namespace in CINT 'namespace w'.
00043   RooWorkspace* w = new RooWorkspace("w",kTRUE) ;
00044 
00045   // Fill workspace with p.d.f. and data in a separate function
00046   fillWorkspace(*w) ;
00047 
00048   // Print workspace contents
00049   w->Print() ;
00050 
00051   // U s e   w o r k s p a c e   c o n t e n t s   t h r o u g h   C I N T   C + +   n a m e s p a c e
00052   // -------------------------------------------------------------------------------------------------
00053 
00054   // Use the name space prefix operator to access the workspace contents
00055   RooDataSet* d = w::model.generate(w::x,1000) ;
00056   RooFitResult* r = w::model.fitTo(*d) ;
00057 
00058   RooPlot* frame = w::x.frame() ;
00059   d->plotOn(frame) ;
00060 
00061 
00062   // NB: The 'w::' prefix can be omitted if namespace w is imported in local namespace
00063   // in the usual C++ way
00064   using namespace w;
00065   model.plotOn(frame) ;
00066   model.plotOn(frame,Components(bkg),LineStyle(kDashed)) ;
00067 
00068 
00069   // Draw the frame on the canvas
00070   new TCanvas("rf509_wsinteractive","rf509_wsinteractive",600,600) ;
00071   gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
00072 
00073 
00074 }
00075 
00076 
00077 
00078 
00079 void fillWorkspace(RooWorkspace& w)
00080 {
00081   // C r e a t e  p d f   a n d   f i l l   w o r k s p a c e
00082   // --------------------------------------------------------
00083 
00084   // Declare observable x
00085   RooRealVar x("x","x",0,10) ;
00086 
00087   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00088   RooRealVar mean("mean","mean of gaussians",5,0,10) ;
00089   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00090   RooRealVar sigma2("sigma2","width of gaussians",1) ;
00091 
00092   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00093   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00094   
00095   // Build Chebychev polynomial p.d.f.  
00096   RooRealVar a0("a0","a0",0.5,0.,1.) ;
00097   RooRealVar a1("a1","a1",-0.2,0.,1.) ;
00098   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00099 
00100   // Sum the signal components into a composite signal p.d.f.
00101   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00102   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00103 
00104   // Sum the composite signal and background 
00105   RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
00106   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
00107 
00108   w.import(model) ;
00109 
00110 }

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