rf303_conditional.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #303
00004 // 
00005 // Use of tailored p.d.f as conditional p.d.fs.s
00006 // 
00007 // pdf = gauss(x,f(y),sx | y ) with f(y) = a0 + a1*y
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 "RooDataHist.h"
00020 #include "RooGaussian.h"
00021 #include "RooPolyVar.h"
00022 #include "RooProdPdf.h"
00023 #include "RooPlot.h"
00024 #include "TRandom.h"
00025 #include "TCanvas.h"
00026 #include "TAxis.h"
00027 #include "TH1.h"
00028 using namespace RooFit ;
00029 
00030 
00031 RooDataSet* makeFakeDataXY() ;
00032 
00033 void rf303_conditional()
00034 {
00035   // S e t u p   c o m p o s e d   m o d e l   g a u s s ( x , m ( y ) , s )
00036   // -----------------------------------------------------------------------
00037 
00038   // Create observables
00039   RooRealVar x("x","x",-10,10) ;
00040   RooRealVar y("y","y",-10,10) ;
00041 
00042   // Create function f(y) = a0 + a1*y
00043   RooRealVar a0("a0","a0",-0.5,-5,5) ;
00044   RooRealVar a1("a1","a1",-0.5,-1,1) ;
00045   RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
00046 
00047   // Creat gauss(x,f(y),s)
00048   RooRealVar sigma("sigma","width of gaussian",0.5,0.1,2.0) ;
00049   RooGaussian model("model","Gaussian with shifting mean",x,fy,sigma) ;  
00050 
00051 
00052   // Obtain fake external experimental dataset with values for x and y
00053   RooDataSet* expDataXY = makeFakeDataXY() ;
00054 
00055 
00056 
00057   // G e n e r a t e   d a t a   f r o m   c o n d i t i o n a l   p . d . f   m o d e l ( x | y )  
00058   // ---------------------------------------------------------------------------------------------
00059 
00060   // Make subset of experimental data with only y values
00061   RooDataSet* expDataY= (RooDataSet*) expDataXY->reduce(y) ;
00062 
00063   // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
00064   RooDataSet *data = model.generate(x,ProtoData(*expDataY)) ;
00065   data->Print() ;
00066 
00067 
00068 
00069   // F i t   c o n d i t i o n a l   p . d . f   m o d e l ( x | y )   t o   d a t a
00070   // ---------------------------------------------------------------------------------------------
00071 
00072   model.fitTo(*expDataXY,ConditionalObservables(y)) ;
00073   
00074 
00075 
00076   // P r o j e c t   c o n d i t i o n a l   p . d . f   o n   x   a n d   y   d i m e n s i o n s
00077   // ---------------------------------------------------------------------------------------------
00078 
00079   // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
00080   RooPlot* xframe = x.frame() ;
00081   expDataXY->plotOn(xframe) ;
00082   model.plotOn(xframe,ProjWData(*expDataY)) ; 
00083 
00084 
00085   // Speed up (and approximate) projection by using binned clone of data for projection
00086   RooAbsData* binnedDataY = expDataY->binnedClone() ;
00087   model.plotOn(xframe,ProjWData(*binnedDataY),LineColor(kCyan),LineStyle(kDotted)) ;
00088 
00089 
00090   // Show effect of projection with too coarse binning
00091   ((RooRealVar*)expDataY->get()->find("y"))->setBins(5) ;
00092   RooAbsData* binnedDataY2 = expDataY->binnedClone() ;
00093   model.plotOn(xframe,ProjWData(*binnedDataY2),LineColor(kRed)) ;
00094 
00095 
00096   // Make canvas and draw RooPlots
00097   new TCanvas("rf303_conditional","rf303_conditional",600, 460);
00098   gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.2) ; xframe->Draw() ;
00099 }
00100 
00101 
00102 
00103 
00104 RooDataSet* makeFakeDataXY() 
00105 {
00106   RooRealVar x("x","x",-10,10) ;
00107   RooRealVar y("y","y",-10,10) ;
00108   RooArgSet coord(x,y) ;
00109 
00110   RooDataSet* d = new RooDataSet("d","d",RooArgSet(x,y)) ;
00111 
00112   for (int i=0 ; i<10000 ; i++) {
00113     Double_t tmpy = gRandom->Gaus(0,10) ;
00114     Double_t tmpx = gRandom->Gaus(0.5*tmpy,1) ;
00115     if (fabs(tmpy)<10 && fabs(tmpx)<10) {
00116       x = tmpx ;
00117       y = tmpy ;
00118       d->add(coord) ;
00119     }
00120       
00121   }
00122 
00123   return d ;
00124 }
00125 

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