rf104_classfactory.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #104
00004 // 
00005 // The class factory for functions and p.d.f.s
00006 //
00007 // 
00008 // NOTE: This demo uses code that is generated by the macro, 
00009 //       therefore it cannot be compiled in one step by ACliC.
00010 //       To run this macro compiled with ACliC do
00011 //
00012 //         root>.x rf104_classfactory.C // run interpreted to generate code
00013 //         root>.L MyPdfV3.cxx+         // Compile and load created classs
00014 //         root>.x rf104_classfactory.C+ // run compiled code
00015 //       
00016 //
00017 // 07/2008 - Wouter Verkerke 
00018 //
00019 /////////////////////////////////////////////////////////////////////////
00020 
00021 #ifndef __CINT__
00022 #include "RooGlobalFunc.h"
00023 #endif
00024 #include "RooRealVar.h"
00025 #include "RooDataSet.h"
00026 #include "RooGaussian.h"
00027 #include "TCanvas.h"
00028 #include "TAxis.h"
00029 #include "RooPlot.h"
00030 #include "RooClassFactory.h"
00031 #include "TROOT.h"
00032 
00033 #ifndef __CINT__
00034 #include "MyPdfV3.h"
00035 #endif
00036 
00037 
00038 using namespace RooFit ;
00039 
00040 
00041 void rf104_classfactory()
00042 {  
00043   // W r i t e   c l a s s   s k e l e t o n   c o d e
00044   // --------------------------------------------------
00045 
00046   // Write skeleton p.d.f class with variable x,a,b
00047   // To use this class, 
00048   //    - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
00049   //    - Compile and link class with '.x MyPdfV1.cxx+'
00050   //
00051   RooClassFactory::makePdf("MyPdfV1","x,A,B") ;
00052 
00053 
00054   // W i t h   a d d e d   i n i t i a l   v a l u e   e x p r e s s i o n
00055   // ---------------------------------------------------------------------
00056 
00057   // Write skeleton p.d.f class with variable x,a,b and given formula expression 
00058   // To use this class, 
00059   //    - Compile and link class with '.x MyPdfV2.cxx+'
00060   //
00061   RooClassFactory::makePdf("MyPdfV2","x,A,B","","A*fabs(x)+pow(x-B,2)") ;
00062   
00063 
00064   // W i t h   a d d e d   a n a l y t i c a l   i n t e g r a l   e x p r e s s i o n
00065   // ---------------------------------------------------------------------------------
00066 
00067   // Write skeleton p.d.f class with variable x,a,b, given formula expression _and_
00068   // given expression for analytical integral over x
00069   // To use this class, 
00070   //    - Compile and link class with '.x MyPdfV3.cxx+'
00071   //
00072   RooClassFactory::makePdf("MyPdfV3","x,A,B","","A*fabs(x)+pow(x-B,2)",kTRUE,kFALSE,
00073                            "x:(A/2)*(pow(x.max(rangeName),2)+pow(x.min(rangeName),2))+(1./3)*(pow(x.max(rangeName)-B,3)-pow(x.min(rangeName)-B,3))") ;
00074 
00075 
00076 
00077   // U s e   i n s t a n c e   o f   c r e a t e d   c l a s s 
00078   // ---------------------------------------------------------
00079  
00080   // Compile MyPdfV3 class (only when running in CINT)
00081 #ifdef __CINT__
00082   gROOT->ProcessLineSync(".x MyPdfV3.cxx+") ;
00083 #endif
00084 
00085   // Creat instance of MyPdfV3 class
00086   RooRealVar a("a","a",1) ;
00087   RooRealVar b("b","b",2,-10,10) ;
00088   RooRealVar y("y","y",-10,10);
00089   MyPdfV3 pdf("pdf","pdf",y,a,b) ;
00090 
00091   // Generate toy data from pdf and plot data and p.d.f on frame
00092   RooPlot* frame1 = y.frame(Title("Compiled class MyPdfV3")) ;
00093   RooDataSet* data = pdf.generate(y,1000) ;
00094   pdf.fitTo(*data) ;
00095   data->plotOn(frame1) ;
00096   pdf.plotOn(frame1) ;
00097 
00098 
00099   ///////////////////////////////////////////////////////////////////////
00100   // C o m p i l e d   v e r s i o n   o f   e x a m p l e   r f 1 0 3 //
00101   ///////////////////////////////////////////////////////////////////////
00102 
00103   // Declare observable x
00104   RooRealVar x("x","x",-20,20) ;
00105 
00106   // The RooClassFactory::makePdfInstance() function performs code writing, compiling, linking
00107   // and object instantiation in one go and can serve as a straight replacement of RooGenericPdf
00108 
00109   RooRealVar alpha("alpha","alpha",5,0.1,10) ;
00110   RooAbsPdf* genpdf = RooClassFactory::makePdfInstance("GenPdf","(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))",RooArgSet(x,alpha)) ;
00111 
00112   // Generate a toy dataset from the interpreted p.d.f
00113   RooDataSet* data2 = genpdf->generate(x,50000) ;
00114 
00115   // Fit the interpreted p.d.f to the generated data
00116   genpdf->fitTo(*data2) ;
00117 
00118   // Make a plot of the data and the p.d.f overlaid
00119   RooPlot* frame2 = x.frame(Title("Compiled version of pdf of rf103")) ;
00120   data2->plotOn(frame2) ;
00121   genpdf->plotOn(frame2) ;  
00122 
00123   // Draw all frames on a canvas
00124   TCanvas* c = new TCanvas("rf104_classfactory","rf104_classfactory",800,400) ;
00125   c->Divide(2) ;
00126   c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
00127   c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
00128 
00129 }

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