stressRooFit_tests.cxx

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #101
00004 // 
00005 // Fitting, plotting, toy data generation on one-dimensional p.d.f
00006 //
00007 // pdf = gauss(x,m,s) 
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 "TCanvas.h"
00021 #include "RooPlot.h"
00022 using namespace RooFit ;
00023 
00024 
00025 // Elementary operations on a gaussian PDF
00026 class TestBasic101 : public RooFitTestUnit
00027 {
00028 public: 
00029   TestBasic101(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Fitting,plotting & event generation of basic p.d.f",refFile,writeRef,verbose) {} ;
00030   Bool_t testCode() {
00031 
00032     // S e t u p   m o d e l 
00033     // ---------------------
00034     
00035     // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
00036     RooRealVar x("x","x",-10,10) ;
00037     RooRealVar mean("mean","mean of gaussian",1,-10,10) ;
00038     RooRealVar sigma("sigma","width of gaussian",1,0.1,10) ;
00039     
00040     // Build gaussian p.d.f in terms of x,mean and sigma
00041     RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;  
00042     
00043     // Construct plot frame in 'x'
00044     RooPlot* xframe = x.frame(Title("Gaussian p.d.f.")) ;
00045     
00046     
00047     // P l o t   m o d e l   a n d   c h a n g e   p a r a m e t e r   v a l u e s
00048     // ---------------------------------------------------------------------------
00049     
00050     // Plot gauss in frame (i.e. in x) 
00051     gauss.plotOn(xframe) ;
00052     
00053     // Change the value of sigma to 3
00054     sigma.setVal(3) ;
00055     
00056     // Plot gauss in frame (i.e. in x) and draw frame on canvas
00057     gauss.plotOn(xframe,LineColor(kRed),Name("another")) ;
00058     
00059     
00060     // G e n e r a t e   e v e n t s 
00061     // -----------------------------
00062     
00063     // Generate a dataset of 1000 events in x from gauss
00064     RooDataSet* data = gauss.generate(x,10000) ;  
00065     
00066     // Make a second plot frame in x and draw both the 
00067     // data and the p.d.f in the frame
00068     RooPlot* xframe2 = x.frame(Title("Gaussian p.d.f. with data")) ;
00069     data->plotOn(xframe2) ;
00070     gauss.plotOn(xframe2) ;
00071     
00072     
00073     // F i t   m o d e l   t o   d a t a
00074     // -----------------------------
00075     
00076     // Fit pdf to data
00077     gauss.fitTo(*data) ;
00078         
00079     
00080     // --- Post processing for stressRooFit ---
00081     regPlot(xframe ,"rf101_plot1") ;
00082     regPlot(xframe2,"rf101_plot2") ;
00083     
00084     delete data ;
00085     
00086     return kTRUE ;
00087   }
00088 } ;
00089 
00090 
00091 //////////////////////////////////////////////////////////////////////////
00092 //
00093 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
00094 // 
00095 // Importing data from ROOT TTrees and THx histograms
00096 //
00097 //
00098 //
00099 // 07/2008 - Wouter Verkerke 
00100 // 
00101 /////////////////////////////////////////////////////////////////////////
00102 
00103 
00104 #ifndef __CINT__
00105 #include "RooGlobalFunc.h"
00106 #endif
00107 #include "RooRealVar.h"
00108 #include "RooDataSet.h"
00109 #include "RooDataHist.h"
00110 #include "RooGaussian.h"
00111 #include "TCanvas.h"
00112 #include "RooPlot.h"
00113 #include "TTree.h"
00114 #include "TH1D.h"
00115 #include "TRandom.h"
00116 using namespace RooFit ;
00117 
00118 
00119 class TestBasic102 : public RooFitTestUnit
00120 {
00121 public: 
00122   TestBasic102(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Data import methods",refFile,writeRef,verbose) {} ;
00123 
00124   TH1* makeTH1() 
00125   {
00126     // Create ROOT TH1 filled with a Gaussian distribution
00127     
00128     TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
00129     for (int i=0 ; i<100 ; i++) {
00130       hh->Fill(gRandom->Gaus(0,3)) ;
00131     }
00132     return hh ;
00133   }
00134   
00135   
00136   TTree* makeTTree() 
00137   {
00138     // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
00139     
00140     TTree* tree = new TTree("tree","tree") ;
00141     Double_t* px = new Double_t ;
00142     Double_t* py = new Double_t ;
00143     tree->Branch("x",px,"x/D") ;
00144     tree->Branch("y",py,"y/D") ;
00145     for (int i=0 ; i<100 ; i++) {
00146       *px = gRandom->Gaus(0,3) ;
00147       *py = gRandom->Uniform()*30 - 15 ;
00148       tree->Fill() ;
00149     }
00150 
00151     //delete px ;
00152     //delete py ;
00153 
00154     return tree ;
00155   }
00156   
00157   Bool_t testCode() {
00158     
00159     ////////////////////////////////////////////////////////
00160     // I m p o r t i n g   R O O T   h i s t o g r a m s  //
00161     ////////////////////////////////////////////////////////
00162     
00163     // I m p o r t   T H 1   i n t o   a   R o o D a t a H i s t
00164     // ---------------------------------------------------------
00165     
00166     // Create a ROOT TH1 histogram
00167     TH1* hh = makeTH1() ;
00168     
00169     // Declare observable x
00170     RooRealVar x("x","x",-10,10) ;
00171     
00172     // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
00173     RooDataHist dh("dh","dh",x,Import(*hh)) ;
00174     
00175     
00176     // P l o t   a n d   f i t   a   R o o D a t a H i s t
00177     // ---------------------------------------------------
00178     
00179     // Make plot of binned dataset showing Poisson error bars (RooFit default)
00180     RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
00181     dh.plotOn(frame) ; 
00182     
00183     // Fit a Gaussian p.d.f to the data
00184     RooRealVar mean("mean","mean",0,-10,10) ;
00185     RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00186     RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
00187     gauss.fitTo(dh) ;
00188     gauss.plotOn(frame) ;
00189     
00190     // P l o t   a n d   f i t   a   R o o D a t a H i s t   w i t h   i n t e r n a l   e r r o r s
00191     // ---------------------------------------------------------------------------------------------
00192     
00193     // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
00194     // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
00195     // (same error bars as shown by ROOT)
00196     RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
00197     dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ; 
00198     gauss.plotOn(frame2) ;
00199     
00200     // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
00201     // in a maximum likelihood fit
00202     //
00203     // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition 
00204     // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
00205     // fitted with a chi^2 fit (see rf602_chi2fit.C) 
00206     
00207     
00208     ////////////////////////////////////////////////
00209     // I m p o r t i n g   R O O T  T T r e e s   //
00210     ////////////////////////////////////////////////
00211     
00212     
00213     // I m p o r t   T T r e e   i n t o   a   R o o D a t a S e t
00214     // -----------------------------------------------------------
00215     
00216     TTree* tree = makeTTree() ;
00217     
00218     // Define 2nd observable y
00219     RooRealVar y("y","y",-10,10) ;
00220     
00221     // Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars 
00222     // is done by name of the branch/RRV 
00223     // 
00224     // Note that ONLY entries for which x,y have values within their allowed ranges as defined in 
00225     // RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
00226     // and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'
00227     
00228     RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
00229     
00230     
00231     // P l o t   d a t a s e t   w i t h   m u l t i p l e   b i n n i n g   c h o i c e s
00232     // ------------------------------------------------------------------------------------
00233     
00234     // Print unbinned dataset with default frame binning (100 bins)
00235     RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
00236     ds.plotOn(frame3) ;
00237     
00238     // Print unbinned dataset with custom binning choice (20 bins)
00239     RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
00240     ds.plotOn(frame4,Binning(20)) ;
00241     
00242     // Draw all frames on a canvas
00243     regPlot(frame ,"rf102_plot1") ;
00244     regPlot(frame2,"rf102_plot2") ;
00245     regPlot(frame3,"rf102_plot3") ;
00246     regPlot(frame4,"rf102_plot4") ;
00247     
00248     delete hh ;
00249     delete tree ;
00250 
00251     return kTRUE ;
00252   }
00253 } ;
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 //////////////////////////////////////////////////////////////////////////
00262 //
00263 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #103
00264 // 
00265 // Interpreted functions and p.d.f.s
00266 //
00267 // 
00268 //
00269 // 07/2008 - Wouter Verkerke 
00270 //
00271 /////////////////////////////////////////////////////////////////////////
00272 
00273 #ifndef __CINT__
00274 #include "RooGlobalFunc.h"
00275 #endif
00276 #include "RooRealVar.h"
00277 #include "RooDataSet.h"
00278 #include "RooGaussian.h"
00279 #include "TCanvas.h"
00280 #include "RooConstVar.h"
00281 #include "RooPlot.h"
00282 #include "RooFitResult.h"
00283 #include "RooGenericPdf.h"
00284 
00285 using namespace RooFit ;
00286 
00287 
00288 class TestBasic103 : public RooFitTestUnit
00289 {
00290 public: 
00291   TestBasic103(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Interpreted expression p.d.f.",refFile,writeRef,verbose) {} ;
00292   Bool_t testCode() {
00293     
00294     /////////////////////////////////////////////////////////
00295     // G e n e r i c   i n t e r p r e t e d   p . d . f . //
00296     /////////////////////////////////////////////////////////
00297     
00298     // Declare observable x
00299     RooRealVar x("x","x",-20,20) ;
00300     
00301     // C o n s t r u c t   g e n e r i c   p d f   f r o m   i n t e r p r e t e d   e x p r e s s i o n
00302     // -------------------------------------------------------------------------------------------------
00303     
00304     // To construct a proper p.d.f, the formula expression is explicitly normalized internally by dividing 
00305     // it by a numeric integral of the expresssion over x in the range [-20,20] 
00306     //
00307     RooRealVar alpha("alpha","alpha",5,0.1,10) ;
00308     RooGenericPdf genpdf("genpdf","genpdf","(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))",RooArgSet(x,alpha)) ;
00309     
00310     
00311     // S a m p l e ,   f i t   a n d   p l o t   g e n e r i c   p d f
00312     // ---------------------------------------------------------------
00313     
00314     // Generate a toy dataset from the interpreted p.d.f
00315     RooDataSet* data = genpdf.generate(x,10000) ;
00316     
00317     // Fit the interpreted p.d.f to the generated data
00318     genpdf.fitTo(*data) ;
00319     
00320     // Make a plot of the data and the p.d.f overlaid
00321     RooPlot* xframe = x.frame(Title("Interpreted expression pdf")) ;
00322     data->plotOn(xframe) ;
00323     genpdf.plotOn(xframe) ;  
00324     
00325     
00326     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00327     // S t a n d a r d   p . d . f   a d j u s t   w i t h   i n t e r p r e t e d   h e l p e r   f u n c t i o n //
00328     //                                                                                                             //
00329     // Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian                                               //
00330     //                                                                                                             //
00331     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00332     
00333     
00334     // C o n s t r u c t   s t a n d a r d   p d f  w i t h   f o r m u l a   r e p l a c i n g   p a r a m e t e r
00335     // ------------------------------------------------------------------------------------------------------------
00336     
00337     // Construct parameter mean2 and sigma
00338     RooRealVar mean2("mean2","mean^2",10,0,200) ;
00339     RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00340     
00341     // Construct interpreted function mean = sqrt(mean^2)
00342     RooFormulaVar mean("mean","mean","sqrt(mean2)",mean2) ;
00343     
00344     // Construct a gaussian g2(x,sqrt(mean2),sigma) ;
00345     RooGaussian g2("g2","h2",x,mean,sigma) ;
00346     
00347     
00348     // G e n e r a t e   t o y   d a t a 
00349     // ---------------------------------
00350     
00351     // Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3
00352     RooGaussian g1("g1","g1",x,RooConst(10),RooConst(3)) ;
00353     RooDataSet* data2 = g1.generate(x,1000) ;
00354     
00355     
00356     // F i t   a n d   p l o t   t a i l o r e d   s t a n d a r d   p d f 
00357     // -------------------------------------------------------------------
00358     
00359     // Fit g2 to data from g1
00360     RooFitResult* r = g2.fitTo(*data2,Save()) ;
00361     
00362     // Plot data on frame and overlay projection of g2
00363     RooPlot* xframe2 = x.frame(Title("Tailored Gaussian pdf")) ;
00364     data2->plotOn(xframe2) ;
00365     g2.plotOn(xframe2) ;
00366     
00367     regPlot(xframe,"rf103_plot1") ;
00368     regPlot(xframe2,"rf103_plot2") ;
00369     regResult(r,"rf103_fit1") ;
00370 
00371     delete data ;
00372     delete data2 ;
00373 
00374     return kTRUE ;
00375   }
00376 } ;
00377 //////////////////////////////////////////////////////////////////////////
00378 //
00379 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #105
00380 // 
00381 //  Demonstration of binding ROOT Math functions as RooFit functions
00382 //  and pdfs
00383 //
00384 // 07/2008 - Wouter Verkerke 
00385 // 
00386 /////////////////////////////////////////////////////////////////////////
00387 
00388 #ifndef __CINT__
00389 #include "RooGlobalFunc.h"
00390 #endif
00391 #include "RooRealVar.h"
00392 #include "RooDataSet.h"
00393 #include "RooGaussian.h"
00394 #include "TCanvas.h"
00395 #include "RooPlot.h"
00396 #include "TMath.h"
00397 #include "TF1.h"
00398 #include "Math/DistFunc.h"
00399 #include "RooCFunction1Binding.h" 
00400 #include "RooCFunction3Binding.h" 
00401 #include "RooTFnBinding.h" 
00402 
00403 using namespace RooFit ;
00404 
00405 class TestBasic105 : public RooFitTestUnit
00406 {
00407 public: 
00408   TestBasic105(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("C++ function binding operator p.d.f",refFile,writeRef,verbose) {} ;
00409   Bool_t testCode() {
00410 
00411     // B i n d   T M a t h : : E r f   C   f u n c t i o n
00412     // ---------------------------------------------------
00413     
00414     // Bind one-dimensional TMath::Erf function as RooAbsReal function
00415     RooRealVar x("x","x",-3,3) ;
00416     RooAbsReal* erf = bindFunction("erf",TMath::Erf,x) ;
00417     
00418     // Plot erf on frame 
00419     RooPlot* frame1 = x.frame(Title("TMath::Erf bound as RooFit function")) ;
00420     erf->plotOn(frame1) ;
00421     
00422     
00423     // B i n d   R O O T : : M a t h : : b e t a _ p d f   C   f u n c t i o n
00424     // -----------------------------------------------------------------------
00425     
00426     // Bind pdf ROOT::Math::Beta with three variables as RooAbsPdf function
00427     RooRealVar x2("x2","x2",0,0.999) ;
00428     RooRealVar a("a","a",5,0,10) ;
00429     RooRealVar b("b","b",2,0,10) ;
00430     RooAbsPdf* beta = bindPdf("beta",ROOT::Math::beta_pdf,x2,a,b) ;
00431     
00432     // Generate some events and fit
00433     RooDataSet* data = beta->generate(x2,10000) ;
00434     beta->fitTo(*data) ;
00435     
00436     // Plot data and pdf on frame
00437     RooPlot* frame2 = x2.frame(Title("ROOT::Math::Beta bound as RooFit pdf")) ;
00438     data->plotOn(frame2) ;
00439     beta->plotOn(frame2) ;
00440     
00441     
00442     
00443     // B i n d   R O O T   T F 1   a s   R o o F i t   f u n c t i o n
00444     // ---------------------------------------------------------------
00445     
00446     // Create a ROOT TF1 function
00447     TF1 *fa1 = new TF1("fa1","sin(x)/x",0,10);
00448     
00449     // Create an observable 
00450     RooRealVar x3("x3","x3",0.01,20) ;
00451     
00452     // Create binding of TF1 object to above observable
00453     RooAbsReal* rfa1 = bindFunction(fa1,x3) ;
00454     
00455     // Make plot frame in observable, plot TF1 binding function
00456     RooPlot* frame3 = x3.frame(Title("TF1 bound as RooFit function")) ;
00457     rfa1->plotOn(frame3) ;
00458       
00459       
00460     regPlot(frame1,"rf105_plot1") ;
00461     regPlot(frame2,"rf105_plot2") ;
00462     regPlot(frame3,"rf105_plot3") ;
00463     
00464     delete erf ;
00465     delete beta ;
00466     delete fa1 ;
00467     delete rfa1 ;
00468     delete data ;
00469     
00470     return kTRUE ;
00471   }  
00472 } ;
00473 /////////////////////////////////////////////////////////////////////////
00474 //
00475 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #108
00476 // 
00477 // Plotting unbinned data with alternate and variable binnings
00478 //
00479 // 
00480 // 07/2008 - Wouter Verkerke 
00481 // 
00482 /////////////////////////////////////////////////////////////////////////
00483 
00484 #ifndef __CINT__
00485 #include "RooGlobalFunc.h"
00486 #endif
00487 #include "RooRealVar.h"
00488 #include "RooDataSet.h"
00489 #include "RooGaussModel.h"
00490 #include "RooDecay.h"
00491 #include "RooBMixDecay.h"
00492 #include "RooCategory.h"
00493 #include "RooBinning.h"
00494 #include "RooPlot.h"
00495 #include "TCanvas.h"
00496 #include "TH1.h"
00497 using namespace RooFit ;
00498 
00499 
00500 class TestBasic108 : public RooFitTestUnit
00501 {
00502 public: 
00503   TestBasic108(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Non-standard binning in counting and asymmetry plots",refFile,writeRef,verbose) {} ;
00504   Bool_t testCode() {
00505 
00506     // S e t u p   m o d e l 
00507     // ---------------------
00508     
00509     // Build a B decay p.d.f with mixing
00510     RooRealVar dt("dt","dt",-20,20) ;
00511     RooRealVar dm("dm","dm",0.472) ;
00512     RooRealVar tau("tau","tau",1.547) ;
00513     RooRealVar w("w","mistag rate",0.1) ;
00514     RooRealVar dw("dw","delta mistag rate",0.) ;
00515     
00516     RooCategory mixState("mixState","B0/B0bar mixing state") ;
00517     mixState.defineType("mixed",-1) ;
00518     mixState.defineType("unmixed",1) ;
00519     RooCategory tagFlav("tagFlav","Flavour of the tagged B0") ;
00520     tagFlav.defineType("B0",1) ;
00521     tagFlav.defineType("B0bar",-1) ;
00522     
00523     // Build a gaussian resolution model
00524     RooRealVar dterr("dterr","dterr",0.1,1.0) ;
00525     RooRealVar bias1("bias1","bias1",0) ;
00526     RooRealVar sigma1("sigma1","sigma1",0.1) ;  
00527     RooGaussModel gm1("gm1","gauss model 1",dt,bias1,sigma1) ;
00528     
00529     // Construct Bdecay (x) gauss
00530     RooBMixDecay bmix("bmix","decay",dt,mixState,tagFlav,tau,dm,w,dw,gm1,RooBMixDecay::DoubleSided) ;
00531     
00532     
00533     // S a m p l e   d a t a   f r o m   m o d e l
00534     // --------------------------------------------
00535     
00536     // Sample 2000 events in (dt,mixState,tagFlav) from bmix
00537     RooDataSet *data = bmix.generate(RooArgSet(dt,mixState,tagFlav),2000) ;
00538     
00539     
00540     
00541     // S h o w   d t   d i s t r i b u t i o n   w i t h   c u s t o m   b i n n i n g
00542     // -------------------------------------------------------------------------------
00543     
00544     // Make plot of dt distribution of data in range (-15,15) with fine binning for dt>0 and coarse binning for dt<0
00545     
00546     // Create binning object with range (-15,15)
00547     RooBinning tbins(-15,15) ;
00548     
00549     // Add 60 bins with uniform spacing in range (-15,0)
00550     tbins.addUniform(60,-15,0) ;
00551     
00552     // Add 15 bins with uniform spacing in range (0,15)
00553     tbins.addUniform(15,0,15) ;
00554     
00555     // Make plot with specified binning
00556     RooPlot* dtframe = dt.frame(Range(-15,15),Title("dt distribution with custom binning")) ;
00557     data->plotOn(dtframe,Binning(tbins)) ;
00558     bmix.plotOn(dtframe) ;
00559     
00560     // NB: Note that bin density for each bin is adjusted to that of default frame binning as shown
00561     // in Y axis label (100 bins --> Events/0.4*Xaxis-dim) so that all bins represent a consistent density distribution
00562     
00563     
00564     // S h o w   m i x s t a t e   a s y m m e t r y  w i t h   c u s t o m   b i n n i n g
00565     // ------------------------------------------------------------------------------------
00566     
00567     // Make plot of dt distribution of data asymmetry in 'mixState' with variable binning 
00568     
00569     // Create binning object with range (-10,10)
00570     RooBinning abins(-10,10) ;
00571     
00572     // Add boundaries at 0, (-1,1), (-2,2), (-3,3), (-4,4) and (-6,6)
00573     abins.addBoundary(0) ;
00574     abins.addBoundaryPair(1) ;
00575     abins.addBoundaryPair(2) ;
00576     abins.addBoundaryPair(3) ;
00577     abins.addBoundaryPair(4) ;
00578     abins.addBoundaryPair(6) ;
00579     
00580     // Create plot frame in dt
00581     RooPlot* aframe = dt.frame(Range(-10,10),Title("mixState asymmetry distribution with custom binning")) ;
00582     
00583     // Plot mixState asymmetry of data with specified customg binning
00584     data->plotOn(aframe,Asymmetry(mixState),Binning(abins)) ;
00585     
00586     // Plot corresponding property of p.d.f
00587     bmix.plotOn(aframe,Asymmetry(mixState)) ;
00588     
00589     // Adjust vertical range of plot to sensible values for an asymmetry
00590     aframe->SetMinimum(-1.1) ;
00591     aframe->SetMaximum(1.1) ;
00592     
00593     // NB: For asymmetry distributions no density corrects are needed (and are thus not applied)
00594     
00595 
00596     regPlot(dtframe,"rf108_plot1") ;
00597     regPlot(aframe,"rf108_plot2") ;
00598 
00599     delete data ;
00600 
00601     return kTRUE ;
00602   }
00603 } ;
00604 //////////////////////////////////////////////////////////////////////////
00605 //
00606 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #109
00607 // 
00608 // Calculating chi^2 from histograms and curves in RooPlots, 
00609 // making histogram of residual and pull distributions
00610 //
00611 //
00612 //
00613 // 07/2008 - Wouter Verkerke 
00614 // 
00615 /////////////////////////////////////////////////////////////////////////
00616 
00617 #ifndef __CINT__
00618 #include "RooGlobalFunc.h"
00619 #endif
00620 #include "RooRealVar.h"
00621 #include "RooDataSet.h"
00622 #include "RooGaussian.h"
00623 #include "TCanvas.h"
00624 #include "RooPlot.h"
00625 #include "RooHist.h"
00626 using namespace RooFit ;
00627 
00628 class TestBasic109 : public RooFitTestUnit
00629 {
00630 public: 
00631   TestBasic109(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Calculation of chi^2 and residuals in plots",refFile,writeRef,verbose) {} ;
00632   Bool_t testCode() {
00633     
00634     // S e t u p   m o d e l 
00635     // ---------------------
00636     
00637     // Create observables
00638     RooRealVar x("x","x",-10,10) ;
00639     
00640     // Create Gaussian
00641     RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00642     RooRealVar mean("mean","mean",0,-10,10) ;
00643     RooGaussian gauss("gauss","gauss",x,RooConst(0),sigma) ;
00644     
00645     // Generate a sample of 1000 events with sigma=3
00646     RooDataSet* data = gauss.generate(x,10000) ;
00647     
00648     // Change sigma to 3.15
00649     sigma=3.15 ;
00650     
00651     
00652     // P l o t   d a t a   a n d   s l i g h t l y   d i s t o r t e d   m o d e l
00653     // ---------------------------------------------------------------------------
00654     
00655     // Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
00656     RooPlot* frame1 = x.frame(Title("Data with distorted Gaussian pdf"),Bins(40)) ;
00657     data->plotOn(frame1,DataError(RooAbsData::SumW2)) ;
00658     gauss.plotOn(frame1) ;
00659     
00660     
00661     // C a l c u l a t e   c h i ^ 2 
00662     // ------------------------------
00663     
00664     // Show the chi^2 of the curve w.r.t. the histogram
00665     // If multiple curves or datasets live in the frame you can specify
00666     // the name of the relevant curve and/or dataset in chiSquare()
00667     regValue(frame1->chiSquare(),"rf109_chi2") ;
00668     
00669     
00670     // S h o w   r e s i d u a l   a n d   p u l l   d i s t s
00671     // -------------------------------------------------------
00672     
00673     // Construct a histogram with the residuals of the data w.r.t. the curve
00674     RooHist* hresid = frame1->residHist() ;
00675     
00676     // Construct a histogram with the pulls of the data w.r.t the curve
00677     RooHist* hpull = frame1->pullHist() ;
00678     
00679     // Create a new frame to draw the residual distribution and add the distribution to the frame
00680     RooPlot* frame2 = x.frame(Title("Residual Distribution")) ;
00681     frame2->addPlotable(hresid,"P") ;
00682     
00683     // Create a new frame to draw the pull distribution and add the distribution to the frame
00684     RooPlot* frame3 = x.frame(Title("Pull Distribution")) ;
00685     frame3->addPlotable(hpull,"P") ;
00686     
00687     regPlot(frame1,"rf109_plot1") ;
00688     regPlot(frame2,"rf109_plot2") ;
00689     regPlot(frame3,"rf109_plot3") ;
00690 
00691     delete data ;
00692     //delete hresid ;
00693     //delete hpull ;
00694     
00695     return kTRUE ;    
00696   }
00697 } ;
00698 /////////////////////////////////////////////////////////////////////////
00699 //
00700 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #110
00701 // 
00702 // Examples on normalization of p.d.f.s,
00703 // integration of p.d.fs, construction
00704 // of cumulative distribution functions from p.d.f.s
00705 // in one dimension
00706 //
00707 // 07/2008 - Wouter Verkerke 
00708 //
00709 /////////////////////////////////////////////////////////////////////////
00710 
00711 #ifndef __CINT__
00712 #include "RooGlobalFunc.h"
00713 #endif
00714 #include "RooRealVar.h"
00715 #include "RooGaussian.h"
00716 #include "RooAbsReal.h"
00717 #include "RooPlot.h"
00718 #include "TCanvas.h"
00719 using namespace RooFit ;
00720 
00721 class TestBasic110 : public RooFitTestUnit
00722 {
00723 public: 
00724   TestBasic110(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Normalization of p.d.f.s in 1D",refFile,writeRef,verbose) {} ;
00725   Bool_t testCode() {
00726 
00727     // S e t u p   m o d e l 
00728     // ---------------------
00729     
00730     // Create observables x,y
00731     RooRealVar x("x","x",-10,10) ;
00732     
00733     // Create p.d.f. gaussx(x,-2,3) 
00734     RooGaussian gx("gx","gx",x,RooConst(-2),RooConst(3)) ;
00735     
00736     
00737     // R e t r i e v e   r a w  &   n o r m a l i z e d   v a l u e s   o f   R o o F i t   p . d . f . s
00738     // --------------------------------------------------------------------------------------------------
00739     
00740     // Return 'raw' unnormalized value of gx
00741     regValue(gx.getVal(),"rf110_gx") ;
00742     
00743     // Return value of gx normalized over x in range [-10,10]
00744     RooArgSet nset(x) ;
00745 
00746     regValue(gx.getVal(&nset),"rf110_gx_Norm[x]") ;
00747     
00748     // Create object representing integral over gx
00749     // which is used to calculate  gx_Norm[x] == gx / gx_Int[x]
00750     RooAbsReal* igx = gx.createIntegral(x) ;
00751     regValue(igx->getVal(),"rf110_gx_Int[x]") ;
00752     
00753     
00754     // I n t e g r a t e   n o r m a l i z e d   p d f   o v e r   s u b r a n g e
00755     // ----------------------------------------------------------------------------
00756     
00757     // Define a range named "signal" in x from -5,5
00758     x.setRange("signal",-5,5) ;
00759     
00760     // Create an integral of gx_Norm[x] over x in range "signal"
00761     // This is the fraction of of p.d.f. gx_Norm[x] which is in the
00762     // range named "signal"
00763     RooAbsReal* igx_sig = gx.createIntegral(x,NormSet(x),Range("signal")) ;
00764     regValue(igx_sig->getVal(),"rf110_gx_Int[x|signal]_Norm[x]") ;
00765     
00766     
00767     
00768     // C o n s t r u c t   c u m u l a t i v e   d i s t r i b u t i o n   f u n c t i o n   f r o m   p d f
00769     // -----------------------------------------------------------------------------------------------------
00770     
00771     // Create the cumulative distribution function of gx
00772     // i.e. calculate Int[-10,x] gx(x') dx'
00773     RooAbsReal* gx_cdf = gx.createCdf(x) ;
00774     
00775     // Plot cdf of gx versus x
00776     RooPlot* frame = x.frame(Title("c.d.f of Gaussian p.d.f")) ;
00777     gx_cdf->plotOn(frame) ;
00778     
00779     
00780     regPlot(frame,"rf110_plot1") ;
00781 
00782     delete igx ;
00783     delete igx_sig ;
00784     delete gx_cdf ;
00785 
00786     return kTRUE ;
00787   }
00788 } ;
00789 //////////////////////////////////////////////////////////////////////////
00790 //
00791 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #111 
00792 // 
00793 // Configuration and customization of how numeric (partial) integrals
00794 // are executed
00795 //
00796 //
00797 //
00798 // 07/2008 - Wouter Verkerke 
00799 // 
00800 /////////////////////////////////////////////////////////////////////////
00801 
00802 #ifndef __CINT__
00803 #include "RooGlobalFunc.h"
00804 #endif
00805 #include "RooRealVar.h"
00806 #include "RooDataSet.h"
00807 #include "RooGaussian.h"
00808 #include "TCanvas.h"
00809 #include "RooPlot.h"
00810 #include "RooNumIntConfig.h"
00811 #include "RooLandau.h"
00812 #include "RooArgSet.h"
00813 #include <iomanip>
00814 using namespace RooFit ;
00815 
00816 class TestBasic111 : public RooFitTestUnit
00817 {
00818 public: 
00819   TestBasic111(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Numeric integration configuration",refFile,writeRef,verbose) {} ;
00820   Bool_t testCode() {
00821 
00822     // A d j u s t   g l o b a l   1 D   i n t e g r a t i o n   p r e c i s i o n 
00823     // ----------------------------------------------------------------------------
00824     
00825     // Example: Change global precision for 1D integrals from 1e-7 to 1e-6
00826     //
00827     // The relative epsilon (change as fraction of current best integral estimate) and
00828     // absolute epsilon (absolute change w.r.t last best integral estimate) can be specified
00829     // separately. For most p.d.f integrals the relative change criterium is the most important,
00830     // however for certain non-p.d.f functions that integrate out to zero a separate absolute
00831     // change criterium is necessary to declare convergence of the integral
00832     //
00833     // NB: This change is for illustration only. In general the precision should be at least 1e-7 
00834     // for normalization integrals for MINUIT to succeed.
00835     //
00836     RooAbsReal::defaultIntegratorConfig()->setEpsAbs(1e-6) ;
00837     RooAbsReal::defaultIntegratorConfig()->setEpsRel(1e-6) ;
00838     
00839     
00840     // N u m e r i c   i n t e g r a t i o n   o f   l a n d a u   p d f 
00841     // ------------------------------------------------------------------
00842     
00843     // Construct p.d.f without support for analytical integrator for demonstration purposes
00844     RooRealVar x("x","x",-10,10) ;
00845     RooLandau landau("landau","landau",x,RooConst(0),RooConst(0.1)) ;
00846     
00847     
00848     // Calculate integral over landau with default choice of numeric integrator
00849     RooAbsReal* intLandau = landau.createIntegral(x) ;
00850     Double_t val = intLandau->getVal() ;
00851     regValue(val,"rf111_val1") ;
00852     
00853 
00854     // S a m e   w i t h   c u s t o m   c o n f i g u r a t i o n
00855     // -----------------------------------------------------------
00856     
00857     
00858     // Construct a custom configuration which uses the adaptive Gauss-Kronrod technique
00859     // for closed 1D integrals
00860     RooNumIntConfig customConfig(*RooAbsReal::defaultIntegratorConfig()) ;
00861     customConfig.method1D().setLabel("RooAdaptiveGaussKronrodIntegrator1D") ;
00862     
00863     
00864     // Calculate integral over landau with custom integral specification
00865     RooAbsReal* intLandau2 = landau.createIntegral(x,NumIntConfig(customConfig)) ;
00866     Double_t val2 = intLandau2->getVal() ;
00867     regValue(val2,"rf111_val2") ;
00868 
00869 
00870     // A d j u s t i n g   d e f a u l t   c o n f i g   f o r   a   s p e c i f i c   p d f 
00871     // -------------------------------------------------------------------------------------
00872     
00873     
00874     // Another possibility: associate custom numeric integration configuration as default for object 'landau'
00875     landau.setIntegratorConfig(customConfig) ;
00876     
00877     
00878     // Calculate integral over landau custom numeric integrator specified as object default
00879     RooAbsReal* intLandau3 = landau.createIntegral(x) ;
00880     Double_t val3 = intLandau3->getVal() ;
00881     regValue(val3,"rf111_val3") ;    
00882 
00883     
00884     delete intLandau ;
00885     delete intLandau2 ;
00886     delete intLandau3 ;
00887     
00888     return kTRUE ;
00889   }
00890 } ;
00891 /////////////////////////////////////////////////////////////////////////
00892 //
00893 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #201
00894 // 
00895 // Composite p.d.f with signal and background component
00896 //
00897 // pdf = f_bkg * bkg(x,a0,a1) + (1-fbkg) * (f_sig1 * sig1(x,m,s1 + (1-f_sig1) * sig2(x,m,s2)))
00898 //
00899 //
00900 // 07/2008 - Wouter Verkerke 
00901 //
00902 /////////////////////////////////////////////////////////////////////////
00903 
00904 #ifndef __CINT__
00905 #include "RooGlobalFunc.h"
00906 #endif
00907 #include "RooRealVar.h"
00908 #include "RooDataSet.h"
00909 #include "RooGaussian.h"
00910 #include "RooChebychev.h"
00911 #include "RooAddPdf.h"
00912 #include "TCanvas.h"
00913 #include "RooPlot.h"
00914 using namespace RooFit ;
00915 
00916 
00917 class TestBasic201 : public RooFitTestUnit
00918 {
00919 public: 
00920   TestBasic201(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Addition operator p.d.f.",refFile,writeRef,verbose) {} ;
00921   Bool_t testCode() {
00922 
00923     // S e t u p   c o m p o n e n t   p d f s 
00924     // ---------------------------------------
00925     
00926     // Declare observable x
00927     RooRealVar x("x","x",0,10) ;
00928     
00929     // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00930     RooRealVar mean("mean","mean of gaussians",5) ;
00931     RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00932     RooRealVar sigma2("sigma2","width of gaussians",1) ;
00933     
00934     RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00935     RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00936     
00937     // Build Chebychev polynomial p.d.f.  
00938     RooRealVar a0("a0","a0",0.5,0.,1.) ;
00939     RooRealVar a1("a1","a1",-0.2,0.,1.) ;
00940     RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00941     
00942     
00943     ////////////////////////////////////////////////////
00944     // M E T H O D   1 - T w o   R o o A d d P d f s  //
00945     ////////////////////////////////////////////////////
00946     
00947     
00948     // A d d   s i g n a l   c o m p o n e n t s 
00949     // ------------------------------------------
00950     
00951     // Sum the signal components into a composite signal p.d.f.
00952     RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00953     RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00954     
00955     
00956     // A d d  s i g n a l   a n d   b a c k g r o u n d
00957     // ------------------------------------------------
00958     
00959     // Sum the composite signal and background 
00960     RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
00961     RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
00962     
00963     
00964     // S a m p l e ,   f i t   a n d   p l o t   m o d e l
00965     // ---------------------------------------------------
00966     
00967     // Generate a data sample of 1000 events in x from model
00968     RooDataSet *data = model.generate(x,1000) ;
00969     
00970     // Fit model to data
00971     model.fitTo(*data) ;
00972     
00973     // Plot data and PDF overlaid
00974     RooPlot* xframe = x.frame(Title("Example of composite pdf=(sig1+sig2)+bkg")) ;
00975     data->plotOn(xframe) ;
00976     model.plotOn(xframe) ;
00977     
00978     // Overlay the background component of model with a dashed line
00979     model.plotOn(xframe,Components(bkg),LineStyle(kDashed)) ;
00980     
00981     // Overlay the background+sig2 components of model with a dotted line
00982     model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted)) ;
00983     
00984 
00985     ////////////////////////////////////////////////////////////////////////////////////////////////////
00986     // M E T H O D   2 - O n e   R o o A d d P d f   w i t h   r e c u r s i v e   f r a c t i o n s  //
00987     ////////////////////////////////////////////////////////////////////////////////////////////////////
00988     
00989     // Construct sum of models on one go using recursive fraction interpretations
00990     //
00991     //   model2 = bkg + (sig1 + sig2)
00992     //
00993     RooAddPdf  model2("model","g1+g2+a",RooArgList(bkg,sig1,sig2),RooArgList(bkgfrac,sig1frac),kTRUE) ;    
00994     
00995     // NB: Each coefficient is interpreted as the fraction of the 
00996     // left-hand component of the i-th recursive sum, i.e.
00997     //
00998     //   sum4 = A + ( B + ( C + D)  with fraction fA, fB and fC expands to
00999     //
01000     //   sum4 = fA*A + (1-fA)*(fB*B + (1-fB)*(fC*C + (1-fC)*D))
01001     
01002     
01003     // P l o t   r e c u r s i v e   a d d i t i o n   m o d e l
01004     // ---------------------------------------------------------
01005     model2.plotOn(xframe,LineColor(kRed),LineStyle(kDashed)) ;
01006     model2.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineColor(kRed),LineStyle(kDashed)) ;
01007 
01008   
01009     regPlot(xframe,"rf201_plot1") ;
01010     
01011     delete data ;
01012     return kTRUE ;
01013 
01014   }
01015 
01016 } ;
01017 
01018 //////////////////////////////////////////////////////////////////////////
01019 //
01020 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #202
01021 // 
01022 // Setting up an extended maximum likelihood fit
01023 //
01024 //
01025 //
01026 // 07/2008 - Wouter Verkerke 
01027 // 
01028 /////////////////////////////////////////////////////////////////////////
01029 
01030 #ifndef __CINT__
01031 #include "RooGlobalFunc.h"
01032 #endif
01033 #include "RooRealVar.h"
01034 #include "RooDataSet.h"
01035 #include "RooGaussian.h"
01036 #include "RooChebychev.h"
01037 #include "RooAddPdf.h"
01038 #include "RooExtendPdf.h"
01039 #include "TCanvas.h"
01040 #include "RooPlot.h"
01041 using namespace RooFit ;
01042 
01043 
01044 class TestBasic202 : public RooFitTestUnit
01045 {
01046 public: 
01047   TestBasic202(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Extended ML fits to addition operator p.d.f.s",refFile,writeRef,verbose) {} ;
01048   Bool_t testCode() {
01049 
01050     // S e t u p   c o m p o n e n t   p d f s 
01051     // ---------------------------------------
01052     
01053     // Declare observable x
01054     RooRealVar x("x","x",0,10) ;
01055     
01056     // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
01057     RooRealVar mean("mean","mean of gaussians",5) ;
01058     RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
01059     RooRealVar sigma2("sigma2","width of gaussians",1) ;
01060     
01061     RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
01062     RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
01063     
01064     // Build Chebychev polynomial p.d.f.  
01065     RooRealVar a0("a0","a0",0.5,0.,1.) ;
01066     RooRealVar a1("a1","a1",-0.2,0.,1.) ;
01067     RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
01068     
01069     // Sum the signal components into a composite signal p.d.f.
01070     RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
01071     RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
01072     
01073     /////////////////////
01074     // M E T H O D   1 //
01075     /////////////////////
01076     
01077     
01078     // C o n s t r u c t   e x t e n d e d   c o m p o s i t e   m o d e l 
01079     // -------------------------------------------------------------------
01080     
01081     // Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg
01082     RooRealVar nsig("nsig","number of signal events",500,0.,10000) ;
01083     RooRealVar nbkg("nbkg","number of background events",500,0,10000) ;
01084     RooAddPdf  model("model","(g1+g2)+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
01085     
01086     
01087     
01088     // S a m p l e ,   f i t   a n d   p l o t   e x t e n d e d   m o d e l 
01089     // ---------------------------------------------------------------------
01090     
01091     // Generate a data sample of expected number events in x from model
01092     // = model.expectedEvents() = nsig+nbkg
01093     RooDataSet *data = model.generate(x) ;
01094     
01095     // Fit model to data, extended ML term automatically included
01096     model.fitTo(*data) ; 
01097     
01098     // Plot data and PDF overlaid, use expected number of events for p.d.f projection normalization
01099     // rather than observed number of events (==data->numEntries())
01100     RooPlot* xframe = x.frame(Title("extended ML fit example")) ;
01101     data->plotOn(xframe) ;
01102     model.plotOn(xframe,Normalization(1.0,RooAbsReal::RelativeExpected)) ;
01103     
01104     // Overlay the background component of model with a dashed line
01105     model.plotOn(xframe,Components(bkg),LineStyle(kDashed),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
01106     
01107     // Overlay the background+sig2 components of model with a dotted line
01108     model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted),Normalization(1.0,RooAbsReal::RelativeExpected)) ;
01109     
01110     
01111     /////////////////////
01112     // M E T H O D   2 //
01113     /////////////////////
01114     
01115     // C o n s t r u c t   e x t e n d e d   c o m p o n e n t s   f i r s t
01116     // ---------------------------------------------------------------------
01117     
01118     // Associated nsig/nbkg as expected number of events with sig/bkg
01119     RooExtendPdf esig("esig","extended signal p.d.f",sig,nsig) ;
01120     RooExtendPdf ebkg("ebkg","extended background p.d.f",bkg,nbkg) ;
01121     
01122     
01123     // S u m   e x t e n d e d   c o m p o n e n t s   w i t h o u t   c o e f s 
01124     // -------------------------------------------------------------------------
01125     
01126     // Construct sum of two extended p.d.f. (no coefficients required)
01127     RooAddPdf  model2("model2","(g1+g2)+a",RooArgList(ebkg,esig)) ;
01128     
01129     
01130     regPlot(xframe,"rf202_plot1") ;
01131 
01132     delete data ;
01133     return kTRUE ;
01134 
01135   }
01136   
01137 } ;
01138 /////////////////////////////////////////////////////////////////////////
01139 //
01140 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #203
01141 // 
01142 // Fitting and plotting in sub ranges
01143 //
01144 //
01145 // 07/2008 - Wouter Verkerke 
01146 //
01147 /////////////////////////////////////////////////////////////////////////
01148 
01149 #ifndef __CINT__
01150 #include "RooGlobalFunc.h"
01151 #endif
01152 #include "RooRealVar.h"
01153 #include "RooDataSet.h"
01154 #include "RooGaussian.h"
01155 #include "RooPolynomial.h"
01156 #include "RooAddPdf.h"
01157 #include "RooFitResult.h"
01158 #include "RooPlot.h"
01159 #include "TCanvas.h"
01160 #include "TH1.h"
01161 using namespace RooFit ;
01162 
01163 
01164 class TestBasic203 : public RooFitTestUnit
01165 {
01166 public: 
01167   TestBasic203(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Basic fitting and plotting in ranges",refFile,writeRef,verbose) {} ;
01168   Bool_t testCode() {
01169 
01170     // S e t u p   m o d e l 
01171     // ---------------------
01172     
01173     // Construct observables x
01174     RooRealVar x("x","x",-10,10) ;
01175     
01176     // Construct gaussx(x,mx,1)
01177     RooRealVar mx("mx","mx",0,-10,10) ;
01178     RooGaussian gx("gx","gx",x,mx,RooConst(1)) ;
01179     
01180     // Construct px = 1 (flat in x)
01181     RooPolynomial px("px","px",x) ;
01182     
01183     // Construct model = f*gx + (1-f)px
01184     RooRealVar f("f","f",0.,1.) ;
01185     RooAddPdf model("model","model",RooArgList(gx,px),f) ;
01186     
01187     // Generated 10000 events in (x,y) from p.d.f. model
01188     RooDataSet* modelData = model.generate(x,10000) ;
01189     
01190     // F i t   f u l l   r a n g e 
01191     // ---------------------------
01192     
01193     // Fit p.d.f to all data
01194     RooFitResult* r_full = model.fitTo(*modelData,Save(kTRUE)) ;
01195     
01196     
01197     // F i t   p a r t i a l   r a n g e 
01198     // ----------------------------------
01199     
01200     // Define "signal" range in x as [-3,3]
01201     x.setRange("signal",-3,3) ;  
01202     
01203     // Fit p.d.f only to data in "signal" range
01204     RooFitResult* r_sig = model.fitTo(*modelData,Save(kTRUE),Range("signal")) ;
01205     
01206     
01207     // P l o t   /   p r i n t   r e s u l t s 
01208     // ---------------------------------------
01209     
01210     // Make plot frame in x and add data and fitted model
01211     RooPlot* frame = x.frame(Title("Fitting a sub range")) ;
01212     modelData->plotOn(frame) ;
01213     model.plotOn(frame,Range("Full"),LineStyle(kDashed),LineColor(kRed)) ; // Add shape in full ranged dashed
01214     model.plotOn(frame) ; // By default only fitted range is shown
01215     
01216     regPlot(frame,"rf203_plot") ;
01217     regResult(r_full,"rf203_r_full") ;
01218     regResult(r_sig,"rf203_r_sig") ;
01219 
01220     delete modelData ;    
01221     return kTRUE;
01222   }  
01223 } ;
01224 //////////////////////////////////////////////////////////////////////////
01225 //
01226 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #204
01227 // 
01228 // Extended maximum likelihood fit with alternate range definition
01229 // for observed number of events.
01230 //
01231 //
01232 //
01233 // 07/2008 - Wouter Verkerke 
01234 // 
01235 /////////////////////////////////////////////////////////////////////////
01236 
01237 #ifndef __CINT__
01238 #include "RooGlobalFunc.h"
01239 #endif
01240 #include "RooRealVar.h"
01241 #include "RooDataSet.h"
01242 #include "RooGaussian.h"
01243 #include "RooChebychev.h"
01244 #include "RooAddPdf.h"
01245 #include "RooExtendPdf.h"
01246 #include "RooFitResult.h"
01247 #include "TCanvas.h"
01248 #include "RooPlot.h"
01249 using namespace RooFit ;
01250 
01251 
01252 class TestBasic204 : public RooFitTestUnit
01253 {
01254 public: 
01255   TestBasic204(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Extended ML fit in sub range",refFile,writeRef,verbose) {} ;
01256   Bool_t testCode() {
01257     
01258     // S e t u p   c o m p o n e n t   p d f s 
01259     // ---------------------------------------
01260     
01261     // Declare observable x
01262     RooRealVar x("x","x",0,10) ;
01263     
01264     // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
01265     RooRealVar mean("mean","mean of gaussians",5) ;
01266     RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
01267     RooRealVar sigma2("sigma2","width of gaussians",1) ;
01268     
01269     RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
01270     RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
01271     
01272     // Build Chebychev polynomial p.d.f.  
01273     RooRealVar a0("a0","a0",0.5,0.,1.) ;
01274     RooRealVar a1("a1","a1",-0.2,0.,1.) ;
01275     RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
01276     
01277     // Sum the signal components into a composite signal p.d.f.
01278     RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
01279     RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
01280     
01281     
01282     // C o n s t r u c t   e x t e n d e d   c o m p s   wi t h   r a n g e   s p e c
01283     // ------------------------------------------------------------------------------
01284     
01285     // Define signal range in which events counts are to be defined
01286     x.setRange("signalRange",4,6) ;
01287     
01288     // Associated nsig/nbkg as expected number of events with sig/bkg _in_the_range_ "signalRange"
01289     RooRealVar nsig("nsig","number of signal events in signalRange",500,0.,10000) ;
01290     RooRealVar nbkg("nbkg","number of background events in signalRange",500,0,10000) ;
01291     RooExtendPdf esig("esig","extended signal p.d.f",sig,nsig,"signalRange") ;
01292     RooExtendPdf ebkg("ebkg","extended background p.d.f",bkg,nbkg,"signalRange") ;
01293     
01294     
01295     // S u m   e x t e n d e d   c o m p o n e n t s
01296     // ---------------------------------------------
01297     
01298     // Construct sum of two extended p.d.f. (no coefficients required)
01299     RooAddPdf  model("model","(g1+g2)+a",RooArgList(ebkg,esig)) ;
01300     
01301     
01302     // S a m p l e   d a t a ,   f i t   m o d e l
01303     // -------------------------------------------
01304     
01305     // Generate 1000 events from model so that nsig,nbkg come out to numbers <<500 in fit
01306     RooDataSet *data = model.generate(x,1000) ;
01307     
01308     
01309     // Perform unbinned extended ML fit to data
01310     RooFitResult* r = model.fitTo(*data,Extended(kTRUE),Save()) ;
01311 
01312 
01313     regResult(r,"rf204_result") ;
01314 
01315     delete data ;
01316     return kTRUE ;
01317   } 
01318 } ;
01319 //////////////////////////////////////////////////////////////////////////
01320 //
01321 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #205
01322 // 
01323 // Options for plotting components of composite p.d.f.s.
01324 //
01325 //
01326 //
01327 // 07/2008 - Wouter Verkerke 
01328 // 
01329 /////////////////////////////////////////////////////////////////////////
01330 
01331 #ifndef __CINT__
01332 #include "RooGlobalFunc.h"
01333 #endif
01334 #include "RooRealVar.h"
01335 #include "RooDataSet.h"
01336 #include "RooGaussian.h"
01337 #include "RooAddPdf.h"
01338 #include "RooChebychev.h"
01339 #include "RooExponential.h"
01340 #include "TCanvas.h"
01341 #include "RooPlot.h"
01342 using namespace RooFit ;
01343 
01344 
01345 class TestBasic205 : public RooFitTestUnit
01346 {
01347 public: 
01348   TestBasic205(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Component plotting variations",refFile,writeRef,verbose) {} ;
01349   Bool_t testCode() {
01350 
01351     // S e t u p   c o m p o s i t e    p d f
01352     // --------------------------------------
01353     
01354     // Declare observable x
01355     RooRealVar x("x","x",0,10) ;
01356     
01357     // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
01358     RooRealVar mean("mean","mean of gaussians",5) ;
01359     RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
01360     RooRealVar sigma2("sigma2","width of gaussians",1) ;
01361     RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
01362     RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
01363     
01364     // Sum the signal components into a composite signal p.d.f.
01365     RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
01366     RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
01367     
01368     // Build Chebychev polynomial p.d.f.  
01369     RooRealVar a0("a0","a0",0.5,0.,1.) ;
01370     RooRealVar a1("a1","a1",-0.2,0.,1.) ;
01371     RooChebychev bkg1("bkg1","Background 1",x,RooArgSet(a0,a1)) ;
01372     
01373     // Build expontential pdf
01374     RooRealVar alpha("alpha","alpha",-1) ;
01375     RooExponential bkg2("bkg2","Background 2",x,alpha) ;
01376     
01377     // Sum the background components into a composite background p.d.f.
01378     RooRealVar bkg1frac("sig1frac","fraction of component 1 in background",0.2,0.,1.) ;
01379     RooAddPdf bkg("bkg","Signal",RooArgList(bkg1,bkg2),sig1frac) ;
01380     
01381     // Sum the composite signal and background 
01382     RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
01383     RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
01384     
01385     
01386     
01387     // S e t u p   b a s i c   p l o t   w i t h   d a t a   a n d   f u l l   p d f 
01388     // ------------------------------------------------------------------------------
01389     
01390     // Generate a data sample of 1000 events in x from model
01391     RooDataSet *data = model.generate(x,1000) ;
01392     
01393     // Plot data and complete PDF overlaid
01394     RooPlot* xframe  = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)")) ;
01395     data->plotOn(xframe) ;
01396     model.plotOn(xframe) ;
01397     
01398     // Clone xframe for use below
01399     RooPlot* xframe2 = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"),Name("xframe2")) ;
01400     
01401     
01402     // M a k e   c o m p o n e n t   b y   o b j e c t   r e f e r e n c e 
01403     // --------------------------------------------------------------------
01404     
01405     // Plot single background component specified by object reference
01406     model.plotOn(xframe,Components(bkg),LineColor(kRed)) ;
01407     
01408     // Plot single background component specified by object reference
01409     model.plotOn(xframe,Components(bkg2),LineStyle(kDashed),LineColor(kRed)) ;
01410     
01411     // Plot multiple background components specified by object reference
01412     // Note that specified components may occur at any level in object tree
01413     // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
01414     model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted)) ;
01415     
01416     
01417     
01418     // M a k e   c o m p o n e n t   b y   n a m e  /   r e g e x p  
01419     // ------------------------------------------------------------
01420     
01421     // Plot single background component specified by name
01422     model.plotOn(xframe2,Components("bkg"),LineColor(kCyan)) ;
01423     
01424     // Plot multiple background components specified by name
01425     model.plotOn(xframe2,Components("bkg1,sig2"),LineStyle(kDotted),LineColor(kCyan)) ;
01426     
01427     // Plot multiple background components specified by regular expression on name
01428     model.plotOn(xframe2,Components("sig*"),LineStyle(kDashed),LineColor(kCyan)) ;
01429     
01430     // Plot multiple background components specified by multiple regular expressions on name
01431     model.plotOn(xframe2,Components("bkg1,sig*"),LineStyle(kDashed),LineColor(kYellow),Invisible()) ;
01432     
01433     
01434     regPlot(xframe,"rf205_plot1") ;
01435     regPlot(xframe2,"rf205_plot2") ;
01436 
01437     delete data ;
01438     return kTRUE ;
01439 
01440   }
01441 
01442 } ;
01443 /////////////////////////////////////////////////////////////////////////
01444 //
01445 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #208
01446 // 
01447 // One-dimensional numeric convolution
01448 // (require ROOT to be compiled with --enable-fftw3)
01449 // 
01450 // pdf = landau(t) (x) gauss(t)
01451 // 
01452 //
01453 // 07/2008 - Wouter Verkerke 
01454 //
01455 /////////////////////////////////////////////////////////////////////////
01456 
01457 #ifndef __CINT__
01458 #include "RooGlobalFunc.h"
01459 #endif
01460 #include "RooRealVar.h"
01461 #include "RooDataSet.h"
01462 #include "RooGaussian.h"
01463 #include "RooLandau.h"
01464 #include "RooFFTConvPdf.h"
01465 #include "RooPlot.h"
01466 #include "TCanvas.h"
01467 #include "TH1.h"
01468 #include "TPluginManager.h"
01469 #include "TROOT.h"
01470 
01471 using namespace RooFit ;
01472 
01473 
01474 
01475 class TestBasic208 : public RooFitTestUnit
01476 {
01477 public: 
01478   TestBasic208(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("FFT Convolution operator p.d.f.",refFile,writeRef,verbose) {} ;
01479 
01480   Bool_t isTestAvailable() { 
01481 
01482     TPluginHandler *h;
01483     if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualFFT"))) {
01484       if (h->LoadPlugin() == -1) {
01485         gROOT->ProcessLine("new TNamed ;") ;
01486         return kFALSE;
01487       } else {
01488         return kTRUE ;
01489       }
01490     }
01491     return kFALSE ;
01492   }
01493 
01494   Double_t ctol() { return 5e-3 ; } // Account for difficult shape of Landau distribution
01495 
01496   Bool_t testCode() {
01497 
01498     // S e t u p   c o m p o n e n t   p d f s 
01499     // ---------------------------------------
01500     
01501     // Construct observable
01502     RooRealVar t("t","t",-10,30) ;
01503     
01504     // Construct landau(t,ml,sl) ;
01505     RooRealVar ml("ml","mean landau",5.,-20,20) ;
01506     RooRealVar sl("sl","sigma landau",1,0.1,10) ;
01507     RooLandau landau("lx","lx",t,ml,sl) ;
01508     
01509     // Construct gauss(t,mg,sg)
01510     RooRealVar mg("mg","mg",0) ;
01511     RooRealVar sg("sg","sg",2,0.1,10) ;
01512     RooGaussian gauss("gauss","gauss",t,mg,sg) ;
01513     
01514     
01515     // C o n s t r u c t   c o n v o l u t i o n   p d f 
01516     // ---------------------------------------
01517     
01518     // Set #bins to be used for FFT sampling to 10000
01519     t.setBins(10000,"cache") ; 
01520     
01521     // Construct landau (x) gauss
01522     RooFFTConvPdf lxg("lxg","landau (X) gauss",t,landau,gauss) ;
01523     
01524         
01525     // S a m p l e ,   f i t   a n d   p l o t   c o n v o l u t e d   p d f 
01526     // ----------------------------------------------------------------------
01527     
01528     // Sample 1000 events in x from gxlx
01529     RooDataSet* data = lxg.generate(t,10000) ;
01530     
01531     // Fit gxlx to data
01532     lxg.fitTo(*data) ;
01533     
01534     // Plot data, landau pdf, landau (X) gauss pdf
01535     RooPlot* frame = t.frame(Title("landau (x) gauss convolution")) ;
01536     data->plotOn(frame) ;
01537     lxg.plotOn(frame) ;
01538     landau.plotOn(frame,LineStyle(kDashed)) ;
01539     
01540     regPlot(frame,"rf208_plot1") ;
01541 
01542     delete data ;
01543     return kTRUE ;
01544 
01545   }
01546 } ;
01547 
01548 
01549 
01550 /////////////////////////////////////////////////////////////////////////
01551 //
01552 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #209
01553 // 
01554 // Decay function p.d.fs with optional B physics 
01555 // effects (mixing and CP violation) that can be
01556 // analytically convolved with e.g. Gaussian resolution 
01557 // functions
01558 // 
01559 // pdf1 = decay(t,tau) (x) delta(t)
01560 // pdf2 = decay(t,tau) (x) gauss(t,m,s)
01561 // pdf3 = decay(t,tau) (x) (f*gauss1(t,m1,s1) + (1-f)*gauss2(t,m1,s1))
01562 // 
01563 // 07/2008 - Wouter Verkerke 
01564 //
01565 /////////////////////////////////////////////////////////////////////////
01566 
01567 #ifndef __CINT__
01568 #include "RooGlobalFunc.h"
01569 #endif
01570 #include "RooRealVar.h"
01571 #include "RooDataSet.h"
01572 #include "RooGaussModel.h"
01573 #include "RooAddModel.h"
01574 #include "RooTruthModel.h"
01575 #include "RooDecay.h"
01576 #include "RooPlot.h"
01577 #include "TCanvas.h"
01578 #include "TH1.h"
01579 using namespace RooFit ;
01580 
01581 
01582 
01583 class TestBasic209 : public RooFitTestUnit
01584 {
01585 public: 
01586   TestBasic209(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Analytical convolution operator",refFile,writeRef,verbose) {} ;
01587   Bool_t testCode() {
01588     
01589     // B - p h y s i c s   p d f   w i t h   t r u t h   r e s o l u t i o n
01590     // ---------------------------------------------------------------------
01591     
01592     // Variables of decay p.d.f.
01593     RooRealVar dt("dt","dt",-10,10) ;
01594     RooRealVar tau("tau","tau",1.548) ;
01595     
01596     // Build a truth resolution model (delta function)
01597     RooTruthModel tm("tm","truth model",dt) ;
01598     
01599     // Construct decay(t) (x) delta(t)
01600     RooDecay decay_tm("decay_tm","decay",dt,tau,tm,RooDecay::DoubleSided) ;
01601     
01602     // Plot p.d.f. (dashed)
01603     RooPlot* frame = dt.frame(Title("Bdecay (x) resolution")) ;
01604     decay_tm.plotOn(frame,LineStyle(kDashed)) ;
01605     
01606     
01607     // B - p h y s i c s   p d f   w i t h   G a u s s i a n   r e s o l u t i o n
01608     // ----------------------------------------------------------------------------
01609     
01610     // Build a gaussian resolution model
01611     RooRealVar bias1("bias1","bias1",0) ;
01612     RooRealVar sigma1("sigma1","sigma1",1) ;
01613     RooGaussModel gm1("gm1","gauss model 1",dt,bias1,sigma1) ;
01614     
01615     // Construct decay(t) (x) gauss1(t)
01616     RooDecay decay_gm1("decay_gm1","decay",dt,tau,gm1,RooDecay::DoubleSided) ;
01617     
01618     // Plot p.d.f. 
01619     decay_gm1.plotOn(frame) ;
01620     
01621     
01622     // B - p h y s i c s   p d f   w i t h   d o u b l e   G a u s s i a n   r e s o l u t i o n
01623     // ------------------------------------------------------------------------------------------
01624     
01625     // Build another gaussian resolution model
01626     RooRealVar bias2("bias2","bias2",0) ;
01627     RooRealVar sigma2("sigma2","sigma2",5) ;
01628     RooGaussModel gm2("gm2","gauss model 2",dt,bias2,sigma2) ;
01629     
01630     // Build a composite resolution model f*gm1+(1-f)*gm2
01631     RooRealVar gm1frac("gm1frac","fraction of gm1",0.5) ;
01632     RooAddModel gmsum("gmsum","sum of gm1 and gm2",RooArgList(gm1,gm2),gm1frac) ;
01633     
01634     // Construct decay(t) (x) (f*gm1 + (1-f)*gm2)
01635     RooDecay decay_gmsum("decay_gmsum","decay",dt,tau,gmsum,RooDecay::DoubleSided) ;
01636     
01637     // Plot p.d.f. (red)
01638     decay_gmsum.plotOn(frame,LineColor(kRed)) ;
01639     
01640     regPlot(frame,"rf209_plot1") ;
01641 
01642     return kTRUE ;
01643 
01644   } 
01645 } ;
01646 /////////////////////////////////////////////////////////////////////////
01647 //
01648 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #301
01649 // 
01650 // Multi-dimensional p.d.f.s through composition, e.g. substituting a 
01651 // p.d.f parameter with a function that depends on other observables
01652 // 
01653 // pdf = gauss(x,f(y),s) with f(y) = a0 + a1*y
01654 // 
01655 //
01656 // 07/2008 - Wouter Verkerke 
01657 //
01658 /////////////////////////////////////////////////////////////////////////
01659 
01660 #ifndef __CINT__
01661 #include "RooGlobalFunc.h"
01662 #endif
01663 #include "RooRealVar.h"
01664 #include "RooDataSet.h"
01665 #include "RooGaussian.h"
01666 #include "RooPolyVar.h"
01667 #include "RooPlot.h"
01668 #include "TCanvas.h"
01669 #include "TH1.h"
01670 using namespace RooFit ;
01671 
01672 
01673 
01674 class TestBasic301 : public RooFitTestUnit
01675 {
01676 public: 
01677   TestBasic301(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Composition extension of basic p.d.f",refFile,writeRef,verbose) {} ;
01678   Bool_t testCode() {
01679 
01680   // 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 )
01681   // -----------------------------------------------------------------------
01682 
01683   // Create observables
01684   RooRealVar x("x","x",-5,5) ;
01685   RooRealVar y("y","y",-5,5) ;
01686 
01687   // Create function f(y) = a0 + a1*y
01688   RooRealVar a0("a0","a0",-0.5,-5,5) ;
01689   RooRealVar a1("a1","a1",-0.5,-1,1) ;
01690   RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
01691 
01692   // Creat gauss(x,f(y),s)
01693   RooRealVar sigma("sigma","width of gaussian",0.5) ;
01694   RooGaussian model("model","Gaussian with shifting mean",x,fy,sigma) ;  
01695 
01696 
01697   // S a m p l e   d a t a ,   p l o t   d a t a   a n d   p d f   o n   x   a n d   y 
01698   // ---------------------------------------------------------------------------------
01699 
01700   // Generate 10000 events in x and y from model
01701   RooDataSet *data = model.generate(RooArgSet(x,y),10000) ;
01702 
01703   // Plot x distribution of data and projection of model on x = Int(dy) model(x,y)
01704   RooPlot* xframe = x.frame() ;
01705   data->plotOn(xframe) ;
01706   model.plotOn(xframe) ; 
01707 
01708   // Plot x distribution of data and projection of model on y = Int(dx) model(x,y)
01709   RooPlot* yframe = y.frame() ;
01710   data->plotOn(yframe) ;
01711   model.plotOn(yframe) ; 
01712 
01713   // Make two-dimensional plot in x vs y
01714   TH1* hh_model = model.createHistogram("hh_model",x,Binning(50),YVar(y,Binning(50))) ;
01715   hh_model->SetLineColor(kBlue) ;
01716 
01717 
01718   regPlot(xframe,"rf301_plot1") ;
01719   regPlot(yframe,"rf302_plot2") ;
01720   regTH(hh_model,"rf302_model2d") ;
01721 
01722   delete data ;
01723   return kTRUE ;
01724   }
01725 } ;
01726 
01727 
01728 //////////////////////////////////////////////////////////////////////////
01729 //
01730 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #302
01731 // 
01732 //  Utility functions classes available for use in tailoring
01733 //  of composite (multidimensional) pdfs
01734 //
01735 //
01736 // 07/2008 - Wouter Verkerke 
01737 // 
01738 /////////////////////////////////////////////////////////////////////////
01739 
01740 #ifndef __CINT__
01741 #include "RooGlobalFunc.h"
01742 #endif
01743 #include "RooRealVar.h"
01744 #include "RooDataSet.h"
01745 #include "RooGaussian.h"
01746 #include "TCanvas.h"
01747 #include "RooPlot.h"
01748 #include "RooFormulaVar.h"
01749 #include "RooAddition.h"
01750 #include "RooProduct.h"
01751 #include "RooPolyVar.h"
01752 #include "TCanvas.h"
01753 #include "TH1.h"
01754 
01755 using namespace RooFit ;
01756 
01757 
01758 class TestBasic302 : public RooFitTestUnit
01759 {
01760 public: 
01761   TestBasic302(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Sum and product utility functions",refFile,writeRef,verbose) {} ;
01762   Bool_t testCode() {
01763 
01764   // C r e a t e   o b s e r v a b l e s ,   p a r a m e t e r s 
01765   // -----------------------------------------------------------
01766 
01767   // Create observables
01768   RooRealVar x("x","x",-5,5) ;
01769   RooRealVar y("y","y",-5,5) ;
01770 
01771   // Create parameters
01772   RooRealVar a0("a0","a0",-1.5,-5,5) ;
01773   RooRealVar a1("a1","a1",-0.5,-1,1) ;
01774   RooRealVar sigma("sigma","width of gaussian",0.5) ;
01775 
01776 
01777   // U s i n g   R o o F o r m u l a V a r   t o   t a i l o r   p d f 
01778   // -----------------------------------------------------------------------
01779 
01780   // Create interpreted function f(y) = a0 - a1*sqrt(10*abs(y))
01781   RooFormulaVar fy_1("fy_1","a0-a1*sqrt(10*abs(y))",RooArgSet(y,a0,a1)) ;
01782 
01783   // Create gauss(x,f(y),s)
01784   RooGaussian model_1("model_1","Gaussian with shifting mean",x,fy_1,sigma) ;  
01785 
01786 
01787 
01788   // U s i n g   R o o P o l y V a r   t o   t a i l o r   p d f
01789   // -----------------------------------------------------------------------
01790 
01791   // Create polynomial function f(y) = a0 + a1*y
01792   RooPolyVar fy_2("fy_2","fy_2",y,RooArgSet(a0,a1)) ;
01793 
01794   // Create gauss(x,f(y),s)
01795   RooGaussian model_2("model_2","Gaussian with shifting mean",x,fy_2,sigma) ;  
01796 
01797 
01798 
01799   // U s i n g   R o o A d d i t i o n   t o   t a i l o r   p d f 
01800   // -----------------------------------------------------------------------
01801 
01802   // Create sum function f(y) = a0 + y
01803   RooAddition fy_3("fy_3","a0+y",RooArgSet(a0,y)) ;
01804 
01805   // Create gauss(x,f(y),s)
01806   RooGaussian model_3("model_3","Gaussian with shifting mean",x,fy_3,sigma) ;  
01807 
01808 
01809 
01810   // U s i n g   R o o P r o d u c t   t o   t a i l o r   p d f 
01811   // -----------------------------------------------------------------------
01812 
01813   // Create product function f(y) = a1*y
01814   RooProduct fy_4("fy_4","a1*y",RooArgSet(a1,y)) ;
01815 
01816   // Create gauss(x,f(y),s)
01817   RooGaussian model_4("model_4","Gaussian with shifting mean",x,fy_4,sigma) ;  
01818 
01819 
01820 
01821   // P l o t   a l l   p d f s 
01822   // ----------------------------
01823 
01824   // Make two-dimensional plots in x vs y
01825   TH1* hh_model_1 = model_1.createHistogram("hh_model_1",x,Binning(50),YVar(y,Binning(50))) ;
01826   TH1* hh_model_2 = model_2.createHistogram("hh_model_2",x,Binning(50),YVar(y,Binning(50))) ;
01827   TH1* hh_model_3 = model_3.createHistogram("hh_model_3",x,Binning(50),YVar(y,Binning(50))) ;
01828   TH1* hh_model_4 = model_4.createHistogram("hh_model_4",x,Binning(50),YVar(y,Binning(50))) ;
01829   hh_model_1->SetLineColor(kBlue) ;
01830   hh_model_2->SetLineColor(kBlue) ;
01831   hh_model_3->SetLineColor(kBlue) ;
01832   hh_model_4->SetLineColor(kBlue) ;
01833 
01834   regTH(hh_model_1,"rf202_model2d_1") ;
01835   regTH(hh_model_2,"rf202_model2d_2") ;
01836   regTH(hh_model_3,"rf202_model2d_3") ;
01837   regTH(hh_model_4,"rf202_model2d_4") ;
01838 
01839   return kTRUE ;
01840   }
01841 } ;
01842 /////////////////////////////////////////////////////////////////////////
01843 //
01844 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #303
01845 // 
01846 // Use of tailored p.d.f as conditional p.d.fs.s
01847 // 
01848 // pdf = gauss(x,f(y),sx | y ) with f(y) = a0 + a1*y
01849 // 
01850 //
01851 // 07/2008 - Wouter Verkerke 
01852 //
01853 /////////////////////////////////////////////////////////////////////////
01854 
01855 #ifndef __CINT__
01856 #include "RooGlobalFunc.h"
01857 #endif
01858 #include "RooRealVar.h"
01859 #include "RooDataSet.h"
01860 #include "RooGaussian.h"
01861 #include "RooPolyVar.h"
01862 #include "RooProdPdf.h"
01863 #include "RooPlot.h"
01864 #include "TRandom.h"
01865 #include "TCanvas.h"
01866 #include "TH1.h"
01867 using namespace RooFit ;
01868 
01869 
01870 class TestBasic303 : public RooFitTestUnit
01871 {
01872 public: 
01873 
01874 RooDataSet* makeFakeDataXY() 
01875 {
01876   RooRealVar x("x","x",-10,10) ;
01877   RooRealVar y("y","y",-10,10) ;
01878   RooArgSet coord(x,y) ;
01879 
01880   RooDataSet* d = new RooDataSet("d","d",RooArgSet(x,y)) ;
01881 
01882   for (int i=0 ; i<10000 ; i++) {
01883     Double_t tmpy = gRandom->Gaus(0,10) ;
01884     Double_t tmpx = gRandom->Gaus(0.5*tmpy,1) ;
01885     if (fabs(tmpy)<10 && fabs(tmpx)<10) {
01886       x = tmpx ;
01887       y = tmpy ;
01888       d->add(coord) ;
01889     }
01890       
01891   }
01892 
01893   return d ;
01894 }
01895 
01896 
01897 
01898   TestBasic303(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Conditional use of F(x|y)",refFile,writeRef,verbose) {} ;
01899   Bool_t testCode() {
01900 
01901   // 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 )
01902   // -----------------------------------------------------------------------
01903 
01904   // Create observables
01905   RooRealVar x("x","x",-10,10) ;
01906   RooRealVar y("y","y",-10,10) ;
01907 
01908   // Create function f(y) = a0 + a1*y
01909   RooRealVar a0("a0","a0",-0.5,-5,5) ;
01910   RooRealVar a1("a1","a1",-0.5,-1,1) ;
01911   RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
01912 
01913   // Creat gauss(x,f(y),s)
01914   RooRealVar sigma("sigma","width of gaussian",0.5,0.1,2.0) ;
01915   RooGaussian model("model","Gaussian with shifting mean",x,fy,sigma) ;  
01916 
01917 
01918   // Obtain fake external experimental dataset with values for x and y
01919   RooDataSet* expDataXY = makeFakeDataXY() ;
01920 
01921 
01922 
01923   // 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 )  
01924   // ---------------------------------------------------------------------------------------------
01925 
01926   // Make subset of experimental data with only y values
01927   RooDataSet* expDataY= (RooDataSet*) expDataXY->reduce(y) ;
01928 
01929   // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
01930   RooDataSet *data = model.generate(x,ProtoData(*expDataY)) ;
01931 
01932 
01933 
01934   // 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
01935   // ---------------------------------------------------------------------------------------------
01936 
01937   model.fitTo(*expDataXY,ConditionalObservables(y)) ;
01938   
01939 
01940 
01941   // 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
01942   // ---------------------------------------------------------------------------------------------
01943 
01944   // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
01945   RooPlot* xframe = x.frame() ;
01946   expDataXY->plotOn(xframe) ;
01947   model.plotOn(xframe,ProjWData(*expDataY)) ; 
01948 
01949 
01950   // Speed up (and approximate) projection by using binned clone of data for projection
01951   RooAbsData* binnedDataY = expDataY->binnedClone() ;
01952   model.plotOn(xframe,ProjWData(*binnedDataY),LineColor(kCyan),LineStyle(kDotted),Name("Alt1")) ;
01953 
01954 
01955   // Show effect of projection with too coarse binning
01956   ((RooRealVar*)expDataY->get()->find("y"))->setBins(5) ;
01957   RooAbsData* binnedDataY2 = expDataY->binnedClone() ;
01958   model.plotOn(xframe,ProjWData(*binnedDataY2),LineColor(kRed),Name("Alt2")) ;
01959 
01960 
01961   regPlot(xframe,"rf303_plot1") ;
01962 
01963   delete binnedDataY ;
01964   delete binnedDataY2 ;
01965   delete expDataXY ;
01966   delete expDataY ;
01967   delete data ;
01968 
01969   return kTRUE ;
01970   }
01971 } ;
01972 
01973 
01974 
01975 
01976 /////////////////////////////////////////////////////////////////////////
01977 //
01978 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #304
01979 // 
01980 // Simple uncorrelated multi-dimensional p.d.f.s
01981 //
01982 // pdf = gauss(x,mx,sx) * gauss(y,my,sy) 
01983 //
01984 //
01985 // 07/2008 - Wouter Verkerke 
01986 //
01987 /////////////////////////////////////////////////////////////////////////
01988 
01989 #ifndef __CINT__
01990 #include "RooGlobalFunc.h"
01991 #endif
01992 #include "RooRealVar.h"
01993 #include "RooDataSet.h"
01994 #include "RooGaussian.h"
01995 #include "RooProdPdf.h"
01996 #include "TCanvas.h"
01997 #include "RooPlot.h"
01998 using namespace RooFit ;
01999 
02000 
02001 
02002 class TestBasic304 : public RooFitTestUnit
02003 {
02004 public: 
02005   TestBasic304(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Product operator p.d.f. with uncorrelated terms",refFile,writeRef,verbose) {} ;
02006   Bool_t testCode() {
02007 
02008   // C r e a t e   c o m p o n e n t   p d f s   i n   x   a n d   y 
02009   // ----------------------------------------------------------------
02010 
02011   // Create two p.d.f.s gaussx(x,meanx,sigmax) gaussy(y,meany,sigmay) and its variables
02012   RooRealVar x("x","x",-5,5) ;
02013   RooRealVar y("y","y",-5,5) ;
02014 
02015   RooRealVar meanx("mean1","mean of gaussian x",2) ;
02016   RooRealVar meany("mean2","mean of gaussian y",-2) ;
02017   RooRealVar sigmax("sigmax","width of gaussian x",1) ;
02018   RooRealVar sigmay("sigmay","width of gaussian y",5) ;
02019 
02020   RooGaussian gaussx("gaussx","gaussian PDF",x,meanx,sigmax) ;  
02021   RooGaussian gaussy("gaussy","gaussian PDF",y,meany,sigmay) ;  
02022 
02023 
02024 
02025   // C o n s t r u c t   u n c o r r e l a t e d   p r o d u c t   p d f
02026   // -------------------------------------------------------------------
02027 
02028   // Multiply gaussx and gaussy into a two-dimensional p.d.f. gaussxy
02029   RooProdPdf  gaussxy("gaussxy","gaussx*gaussy",RooArgList(gaussx,gaussy)) ;
02030 
02031 
02032 
02033   // S a m p l e   p d f ,   p l o t   p r o j e c t i o n   o n   x   a n d   y  
02034   // ---------------------------------------------------------------------------
02035 
02036   // Generate 10000 events in x and y from gaussxy
02037   RooDataSet *data = gaussxy.generate(RooArgSet(x,y),10000) ;
02038 
02039   // Plot x distribution of data and projection of gaussxy on x = Int(dy) gaussxy(x,y)
02040   RooPlot* xframe = x.frame(Title("X projection of gauss(x)*gauss(y)")) ;
02041   data->plotOn(xframe) ;
02042   gaussxy.plotOn(xframe) ; 
02043 
02044   // Plot x distribution of data and projection of gaussxy on y = Int(dx) gaussxy(x,y)
02045   RooPlot* yframe = y.frame(Title("Y projection of gauss(x)*gauss(y)")) ;
02046   data->plotOn(yframe) ;
02047   gaussxy.plotOn(yframe) ; 
02048 
02049   regPlot(xframe,"rf304_plot1") ;
02050   regPlot(yframe,"rf304_plot2") ;
02051 
02052   delete data ;
02053 
02054   return kTRUE ;
02055   }
02056 } ;
02057 
02058 
02059 
02060 /////////////////////////////////////////////////////////////////////////
02061 //
02062 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #305
02063 // 
02064 // Multi-dimensional p.d.f.s with conditional p.d.fs in product
02065 // 
02066 // pdf = gauss(x,f(y),sx | y ) * gauss(y,ms,sx)    with f(y) = a0 + a1*y
02067 // 
02068 //
02069 // 07/2008 - Wouter Verkerke 
02070 //
02071 /////////////////////////////////////////////////////////////////////////
02072 
02073 #ifndef __CINT__
02074 #include "RooGlobalFunc.h"
02075 #endif
02076 #include "RooRealVar.h"
02077 #include "RooDataSet.h"
02078 #include "RooGaussian.h"
02079 #include "RooPolyVar.h"
02080 #include "RooProdPdf.h"
02081 #include "RooPlot.h"
02082 #include "TCanvas.h"
02083 #include "TH1.h"
02084 using namespace RooFit ;
02085 
02086 
02087 
02088 class TestBasic305 : public RooFitTestUnit
02089 {
02090 public: 
02091   TestBasic305(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Product operator p.d.f. with conditional term",refFile,writeRef,verbose) {} ;
02092   Bool_t testCode() {
02093 
02094   // C r e a t e   c o n d i t i o n a l   p d f   g x ( x | y ) 
02095   // -----------------------------------------------------------
02096 
02097   // Create observables
02098   RooRealVar x("x","x",-5,5) ;
02099   RooRealVar y("y","y",-5,5) ;
02100 
02101   // Create function f(y) = a0 + a1*y
02102   RooRealVar a0("a0","a0",-0.5,-5,5) ;
02103   RooRealVar a1("a1","a1",-0.5,-1,1) ;
02104   RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
02105 
02106   // Create gaussx(x,f(y),sx)
02107   RooRealVar sigmax("sigma","width of gaussian",0.5) ;
02108   RooGaussian gaussx("gaussx","Gaussian in x with shifting mean in y",x,fy,sigmax) ;  
02109 
02110 
02111 
02112   // C r e a t e   p d f   g y ( y ) 
02113   // -----------------------------------------------------------
02114 
02115   // Create gaussy(y,0,5)
02116   RooGaussian gaussy("gaussy","Gaussian in y",y,RooConst(0),RooConst(3)) ;
02117 
02118 
02119 
02120   // C r e a t e   p r o d u c t   g x ( x | y ) * g y ( y )
02121   // -------------------------------------------------------
02122 
02123   // Create gaussx(x,sx|y) * gaussy(y)
02124   RooProdPdf model("model","gaussx(x|y)*gaussy(y)",gaussy,Conditional(gaussx,x)) ;
02125 
02126 
02127 
02128   // S a m p l e ,   f i t   a n d   p l o t   p r o d u c t   p d f
02129   // ---------------------------------------------------------------
02130 
02131   // Generate 1000 events in x and y from model
02132   RooDataSet *data = model.generate(RooArgSet(x,y),10000) ;
02133 
02134   // Plot x distribution of data and projection of model on x = Int(dy) model(x,y)
02135   RooPlot* xframe = x.frame() ;
02136   data->plotOn(xframe) ;
02137   model.plotOn(xframe) ; 
02138 
02139   // Plot x distribution of data and projection of model on y = Int(dx) model(x,y)
02140   RooPlot* yframe = y.frame() ;
02141   data->plotOn(yframe) ;
02142   model.plotOn(yframe) ; 
02143 
02144   // Make two-dimensional plot in x vs y
02145   TH1* hh_model = model.createHistogram("hh_model_rf305",x,Binning(50),YVar(y,Binning(50))) ;
02146   hh_model->SetLineColor(kBlue) ;
02147 
02148   regTH(hh_model,"rf305_model2d") ;
02149   regPlot(xframe,"rf305_plot1") ;
02150   regPlot(yframe,"rf305_plot2") ;
02151 
02152   delete data ;
02153 
02154   return kTRUE ;
02155   
02156   }
02157 } ;
02158 
02159 
02160 
02161 //////////////////////////////////////////////////////////////////////////
02162 //
02163 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #306
02164 // 
02165 // Complete example with use of conditional p.d.f. with per-event errors
02166 //
02167 //
02168 //
02169 // 07/2008 - Wouter Verkerke 
02170 // 
02171 /////////////////////////////////////////////////////////////////////////
02172 
02173 #ifndef __CINT__
02174 #include "RooGlobalFunc.h"
02175 #endif
02176 #include "RooRealVar.h"
02177 #include "RooDataSet.h"
02178 #include "RooGaussian.h"
02179 #include "RooGaussModel.h"
02180 #include "RooDecay.h"
02181 #include "RooLandau.h"
02182 #include "RooPlot.h"
02183 #include "TCanvas.h"
02184 #include "TH2D.h"
02185 using namespace RooFit ;
02186 
02187 
02188 
02189 class TestBasic306 : public RooFitTestUnit
02190 {
02191 public: 
02192   TestBasic306(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Conditional use of per-event error p.d.f. F(t|dt)",refFile,writeRef,verbose) {} ;
02193   Bool_t testCode() {
02194 
02195   // B - p h y s i c s   p d f   w i t h   p e r - e v e n t  G a u s s i a n   r e s o l u t i o n
02196   // ----------------------------------------------------------------------------------------------
02197 
02198   // Observables
02199   RooRealVar dt("dt","dt",-10,10) ;
02200   RooRealVar dterr("dterr","per-event error on dt",0.01,10) ;
02201 
02202   // Build a gaussian resolution model scaled by the per-event error = gauss(dt,bias,sigma*dterr)
02203   RooRealVar bias("bias","bias",0,-10,10) ;
02204   RooRealVar sigma("sigma","per-event error scale factor",1,0.1,10) ;
02205   RooGaussModel gm("gm1","gauss model scaled bt per-event error",dt,bias,sigma,dterr) ;
02206 
02207   // Construct decay(dt) (x) gauss1(dt|dterr)
02208   RooRealVar tau("tau","tau",1.548) ;
02209   RooDecay decay_gm("decay_gm","decay",dt,tau,gm,RooDecay::DoubleSided) ;
02210 
02211 
02212 
02213   // C o n s t r u c t   f a k e   ' e x t e r n a l '   d a t a    w i t h   p e r - e v e n t   e r r o r
02214   // ------------------------------------------------------------------------------------------------------
02215 
02216   // Use landau p.d.f to get somewhat realistic distribution with long tail
02217   RooLandau pdfDtErr("pdfDtErr","pdfDtErr",dterr,RooConst(1),RooConst(0.25)) ;
02218   RooDataSet* expDataDterr = pdfDtErr.generate(dterr,10000) ;
02219 
02220 
02221 
02222   // S a m p l e   d a t a   f r o m   c o n d i t i o n a l   d e c a y _ g m ( d t | d t e r r )
02223   // ---------------------------------------------------------------------------------------------
02224 
02225   // Specify external dataset with dterr values to use decay_dm as conditional p.d.f.
02226   RooDataSet* data = decay_gm.generate(dt,ProtoData(*expDataDterr)) ;
02227 
02228   
02229 
02230   // F i t   c o n d i t i o n a l   d e c a y _ d m ( d t | d t e r r )
02231   // ---------------------------------------------------------------------
02232 
02233   // Specify dterr as conditional observable
02234   decay_gm.fitTo(*data,ConditionalObservables(dterr)) ;
02235 
02236 
02237   
02238   // P l o t   c o n d i t i o n a l   d e c a y _ d m ( d t | d t e r r )
02239   // ---------------------------------------------------------------------
02240 
02241 
02242   // Make two-dimensional plot of conditional p.d.f in (dt,dterr)
02243   TH1* hh_decay = decay_gm.createHistogram("hh_decay",dt,Binning(50),YVar(dterr,Binning(50))) ;
02244   hh_decay->SetLineColor(kBlue) ;
02245 
02246 
02247   // Plot decay_gm(dt|dterr) at various values of dterr
02248   RooPlot* frame = dt.frame(Title("Slices of decay(dt|dterr) at various dterr")) ;
02249   for (Int_t ibin=0 ; ibin<100 ; ibin+=20) {
02250     dterr.setBin(ibin) ;
02251     decay_gm.plotOn(frame,Normalization(5.),Name(Form("curve_slice_%d",ibin))) ;
02252   }
02253 
02254 
02255   // Make projection of data an dt
02256   RooPlot* frame2 = dt.frame(Title("Projection of decay(dt|dterr) on dt")) ;
02257   data->plotOn(frame2) ;
02258 
02259   // Make projection of decay(dt|dterr) on dt. 
02260   //
02261   // Instead of integrating out dterr, make a weighted average of curves
02262   // at values dterr_i as given in the external dataset. 
02263   // (The kTRUE argument bins the data before projection to speed up the process)
02264   decay_gm.plotOn(frame2,ProjWData(*expDataDterr,kTRUE)) ;
02265 
02266 
02267   regTH(hh_decay,"rf306_model2d") ;
02268   regPlot(frame,"rf306_plot1") ;
02269   regPlot(frame2,"rf306_plot2") ;
02270 
02271   delete expDataDterr ;
02272   delete data ;
02273 
02274   return kTRUE ;
02275   }
02276 } ;
02277 
02278 //////////////////////////////////////////////////////////////////////////
02279 //
02280 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #307
02281 // 
02282 // Complete example with use of full p.d.f. with per-event errors
02283 //
02284 //
02285 //
02286 // 07/2008 - Wouter Verkerke 
02287 // 
02288 /////////////////////////////////////////////////////////////////////////
02289 
02290 #ifndef __CINT__
02291 #include "RooGlobalFunc.h"
02292 #endif
02293 #include "RooRealVar.h"
02294 #include "RooDataSet.h"
02295 #include "RooGaussian.h"
02296 #include "RooGaussModel.h"
02297 #include "RooDecay.h"
02298 #include "RooLandau.h"
02299 #include "RooProdPdf.h"
02300 #include "RooHistPdf.h"
02301 #include "RooPlot.h"
02302 #include "TCanvas.h"
02303 #include "TH1.h"
02304 using namespace RooFit ;
02305 
02306 
02307 class TestBasic307 : public RooFitTestUnit
02308 {
02309 public: 
02310   TestBasic307(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Full per-event error p.d.f. F(t|dt)G(dt)",refFile,writeRef,verbose) {} ;
02311   Bool_t testCode() {
02312 
02313   // B - p h y s i c s   p d f   w i t h   p e r - e v e n t  G a u s s i a n   r e s o l u t i o n
02314   // ----------------------------------------------------------------------------------------------
02315 
02316   // Observables
02317   RooRealVar dt("dt","dt",-10,10) ;
02318   RooRealVar dterr("dterr","per-event error on dt",0.1,10) ;
02319 
02320   // Build a gaussian resolution model scaled by the per-event error = gauss(dt,bias,sigma*dterr)
02321   RooRealVar bias("bias","bias",0,-10,10) ;
02322   RooRealVar sigma("sigma","per-event error scale factor",1,0.1,10) ;
02323   RooGaussModel gm("gm1","gauss model scaled bt per-event error",dt,bias,sigma,dterr) ;
02324 
02325   // Construct decay(dt) (x) gauss1(dt|dterr)
02326   RooRealVar tau("tau","tau",1.548) ;
02327   RooDecay decay_gm("decay_gm","decay",dt,tau,gm,RooDecay::DoubleSided) ;
02328 
02329 
02330 
02331   // C o n s t r u c t   e m p i r i c a l   p d f   f o r   p e r - e v e n t   e r r o r
02332   // -----------------------------------------------------------------
02333 
02334   // Use landau p.d.f to get empirical distribution with long tail
02335   RooLandau pdfDtErr("pdfDtErr","pdfDtErr",dterr,RooConst(1),RooConst(0.25)) ;
02336   RooDataSet* expDataDterr = pdfDtErr.generate(dterr,10000) ;
02337 
02338   // Construct a histogram pdf to describe the shape of the dtErr distribution
02339   RooDataHist* expHistDterr = expDataDterr->binnedClone() ;
02340   RooHistPdf pdfErr("pdfErr","pdfErr",dterr,*expHistDterr) ;
02341 
02342 
02343   // C o n s t r u c t   c o n d i t i o n a l   p r o d u c t   d e c a y _ d m ( d t | d t e r r ) * p d f ( d t e r r )
02344   // ----------------------------------------------------------------------------------------------------------------------
02345 
02346   // Construct production of conditional decay_dm(dt|dterr) with empirical pdfErr(dterr)
02347   RooProdPdf model("model","model",pdfErr,Conditional(decay_gm,dt)) ;
02348 
02349   // (Alternatively you could also use the landau shape pdfDtErr)
02350   //RooProdPdf model("model","model",pdfDtErr,Conditional(decay_gm,dt)) ;
02351 
02352   
02353 
02354   // S a m p l e,   f i t   a n d   p l o t   p r o d u c t   m o d e l 
02355   // ------------------------------------------------------------------
02356 
02357   // Specify external dataset with dterr values to use model_dm as conditional p.d.f.
02358   RooDataSet* data = model.generate(RooArgSet(dt,dterr),10000) ;
02359 
02360   
02361 
02362   // F i t   c o n d i t i o n a l   d e c a y _ d m ( d t | d t e r r )
02363   // ---------------------------------------------------------------------
02364 
02365   // Specify dterr as conditional observable
02366   model.fitTo(*data) ;
02367 
02368 
02369   
02370   // P l o t   c o n d i t i o n a l   d e c a y _ d m ( d t | d t e r r )
02371   // ---------------------------------------------------------------------
02372 
02373 
02374   // Make projection of data an dt
02375   RooPlot* frame = dt.frame(Title("Projection of model(dt|dterr) on dt")) ;
02376   data->plotOn(frame) ;
02377   model.plotOn(frame) ;
02378 
02379 
02380   regPlot(frame,"rf307_plot1") ;
02381 
02382   delete expDataDterr ;
02383   delete expHistDterr ;
02384   delete data ;
02385   
02386   return kTRUE ;
02387 
02388   }
02389 } ;
02390 /////////////////////////////////////////////////////////////////////////
02391 //
02392 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #308
02393 // 
02394 // Examples on normalization of p.d.f.s,
02395 // integration of p.d.fs, construction
02396 // of cumulative distribution functions from p.d.f.s
02397 // in two dimensions
02398 //
02399 // 07/2008 - Wouter Verkerke 
02400 //
02401 /////////////////////////////////////////////////////////////////////////
02402 
02403 #ifndef __CINT__
02404 #include "RooGlobalFunc.h"
02405 #endif
02406 #include "RooRealVar.h"
02407 #include "RooGaussian.h"
02408 #include "RooProdPdf.h"
02409 #include "RooAbsReal.h"
02410 #include "RooPlot.h"
02411 #include "TCanvas.h"
02412 #include "TH1.h"
02413 using namespace RooFit ;
02414 
02415 
02416 class TestBasic308 : public RooFitTestUnit
02417 {
02418 public: 
02419   TestBasic308(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Normalization of p.d.f.s in 2D",refFile,writeRef,verbose) {} ;
02420   Bool_t testCode() {
02421 
02422   // S e t u p   m o d e l 
02423   // ---------------------
02424 
02425   // Create observables x,y
02426   RooRealVar x("x","x",-10,10) ;
02427   RooRealVar y("y","y",-10,10) ;
02428 
02429   // Create p.d.f. gaussx(x,-2,3), gaussy(y,2,2) 
02430   RooGaussian gx("gx","gx",x,RooConst(-2),RooConst(3)) ;
02431   RooGaussian gy("gy","gy",y,RooConst(+2),RooConst(2)) ;
02432 
02433   // Create gxy = gx(x)*gy(y)
02434   RooProdPdf gxy("gxy","gxy",RooArgSet(gx,gy)) ;
02435 
02436 
02437 
02438   // R e t r i e v e   r a w  &   n o r m a l i z e d   v a l u e s   o f   R o o F i t   p . d . f . s
02439   // --------------------------------------------------------------------------------------------------
02440 
02441   // Return 'raw' unnormalized value of gx
02442   regValue(gxy.getVal(),"rf308_gxy") ;
02443   
02444   // Return value of gxy normalized over x _and_ y in range [-10,10]
02445   RooArgSet nset_xy(x,y) ;
02446   regValue(gxy.getVal(&nset_xy),"rf308_gx_Norm[x,y]") ;
02447 
02448   // Create object representing integral over gx
02449   // which is used to calculate  gx_Norm[x,y] == gx / gx_Int[x,y]
02450   RooAbsReal* igxy = gxy.createIntegral(RooArgSet(x,y)) ;
02451   regValue(igxy->getVal(),"rf308_gx_Int[x,y]") ;
02452 
02453   // NB: it is also possible to do the following
02454 
02455   // Return value of gxy normalized over x in range [-10,10] (i.e. treating y as parameter)
02456   RooArgSet nset_x(x) ;
02457   regValue(gxy.getVal(&nset_x),"rf308_gx_Norm[x]") ;
02458 
02459   // Return value of gxy normalized over y in range [-10,10] (i.e. treating x as parameter)
02460   RooArgSet nset_y(y) ;
02461   regValue(gxy.getVal(&nset_y),"rf308_gx_Norm[y]") ;
02462 
02463 
02464 
02465   // I n t e g r a t e   n o r m a l i z e d   p d f   o v e r   s u b r a n g e
02466   // ----------------------------------------------------------------------------
02467 
02468   // Define a range named "signal" in x from -5,5
02469   x.setRange("signal",-5,5) ;
02470   y.setRange("signal",-3,3) ;
02471   
02472   // Create an integral of gxy_Norm[x,y] over x and y in range "signal"
02473   // This is the fraction of of p.d.f. gxy_Norm[x,y] which is in the
02474   // range named "signal"
02475   RooAbsReal* igxy_sig = gxy.createIntegral(RooArgSet(x,y),NormSet(RooArgSet(x,y)),Range("signal")) ;
02476   regValue(igxy_sig->getVal(),"rf308_gx_Int[x,y|signal]_Norm[x,y]") ;
02477 
02478 
02479 
02480   // C o n s t r u c t   c u m u l a t i v e   d i s t r i b u t i o n   f u n c t i o n   f r o m   p d f
02481   // -----------------------------------------------------------------------------------------------------
02482 
02483   // Create the cumulative distribution function of gx
02484   // i.e. calculate Int[-10,x] gx(x') dx'
02485   RooAbsReal* gxy_cdf = gxy.createCdf(RooArgSet(x,y)) ;
02486   
02487   // Plot cdf of gx versus x
02488   TH1* hh_cdf = gxy_cdf->createHistogram("hh_cdf",x,Binning(40),YVar(y,Binning(40))) ;
02489   
02490   regTH(hh_cdf,"rf308_cdf") ;
02491 
02492   delete igxy_sig ;
02493   delete igxy ;
02494   delete gxy_cdf ;
02495 
02496   return kTRUE ;
02497   }
02498 } ;
02499 //////////////////////////////////////////////////////////////////////////
02500 //
02501 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #309
02502 // 
02503 // Projecting p.d.f and data slices in discrete observables 
02504 //
02505 //
02506 //
02507 // 07/2008 - Wouter Verkerke 
02508 // 
02509 /////////////////////////////////////////////////////////////////////////
02510 
02511 #ifndef __CINT__
02512 #include "RooGlobalFunc.h"
02513 #endif
02514 #include "RooRealVar.h"
02515 #include "RooDataSet.h"
02516 #include "RooGaussModel.h"
02517 #include "RooDecay.h"
02518 #include "RooBMixDecay.h"
02519 #include "RooCategory.h"
02520 #include "TCanvas.h"
02521 #include "RooPlot.h"
02522 using namespace RooFit ;
02523 
02524 
02525 class TestBasic310 : public RooFitTestUnit
02526 {
02527 public: 
02528   TestBasic310(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Data and p.d.f projection in category slice",refFile,writeRef,verbose) {} ;
02529   Bool_t testCode() {
02530 
02531   // C r e a t e   B   d e c a y   p d f   w it h   m i x i n g 
02532   // ----------------------------------------------------------
02533 
02534   // Decay time observables
02535   RooRealVar dt("dt","dt",-20,20) ;
02536 
02537   // Discrete observables mixState (B0tag==B0reco?) and tagFlav (B0tag==B0(bar)?)
02538   RooCategory mixState("mixState","B0/B0bar mixing state") ;
02539   RooCategory tagFlav("tagFlav","Flavour of the tagged B0") ;
02540 
02541   // Define state labels of discrete observables
02542   mixState.defineType("mixed",-1) ;
02543   mixState.defineType("unmixed",1) ;
02544   tagFlav.defineType("B0",1) ;
02545   tagFlav.defineType("B0bar",-1) ;
02546 
02547   // Model parameters
02548   RooRealVar dm("dm","delta m(B)",0.472,0.,1.0) ;
02549   RooRealVar tau("tau","B0 decay time",1.547,1.0,2.0) ;
02550   RooRealVar w("w","Flavor Mistag rate",0.03,0.0,1.0) ;
02551   RooRealVar dw("dw","Flavor Mistag rate difference between B0 and B0bar",0.01) ;
02552 
02553   // Build a gaussian resolution model
02554   RooRealVar bias1("bias1","bias1",0) ;
02555   RooRealVar sigma1("sigma1","sigma1",0.01) ;  
02556   RooGaussModel gm1("gm1","gauss model 1",dt,bias1,sigma1) ;
02557 
02558   // Construct a decay pdf, smeared with single gaussian resolution model
02559   RooBMixDecay bmix_gm1("bmix","decay",dt,mixState,tagFlav,tau,dm,w,dw,gm1,RooBMixDecay::DoubleSided) ;
02560   
02561   // Generate BMixing data with above set of event errors
02562   RooDataSet *data = bmix_gm1.generate(RooArgSet(dt,tagFlav,mixState),20000) ;
02563 
02564 
02565 
02566   // P l o t   f u l l   d e c a y   d i s t r i b u t i o n 
02567   // ----------------------------------------------------------
02568 
02569   // Create frame, plot data and pdf projection (integrated over tagFlav and mixState)
02570   RooPlot* frame = dt.frame(Title("Inclusive decay distribution")) ;
02571   data->plotOn(frame) ;
02572   bmix_gm1.plotOn(frame) ;
02573 
02574 
02575 
02576   // P l o t   d e c a y   d i s t r .   f o r   m i x e d   a n d   u n m i x e d   s l i c e   o f   m i x S t a t e
02577   // ------------------------------------------------------------------------------------------------------------------
02578 
02579   // Create frame, plot data (mixed only)
02580   RooPlot* frame2 = dt.frame(Title("Decay distribution of mixed events")) ;
02581   data->plotOn(frame2,Cut("mixState==mixState::mixed")) ;
02582 
02583   // Position slice in mixState at "mixed" and plot slice of pdf in mixstate over data (integrated over tagFlav)
02584   bmix_gm1.plotOn(frame2,Slice(mixState,"mixed")) ;
02585 
02586   // Create frame, plot data (unmixed only)
02587   RooPlot* frame3 = dt.frame(Title("Decay distribution of unmixed events")) ;
02588   data->plotOn(frame3,Cut("mixState==mixState::unmixed")) ;
02589 
02590   // Position slice in mixState at "unmixed" and plot slice of pdf in mixstate over data (integrated over tagFlav)
02591   bmix_gm1.plotOn(frame3,Slice(mixState,"unmixed")) ;
02592 
02593 
02594   regPlot(frame,"rf310_plot1") ;
02595   regPlot(frame2,"rf310_plot2") ;
02596   regPlot(frame3,"rf310_plot3") ;
02597 
02598   delete data ;
02599 
02600   return kTRUE ;
02601   }
02602 } ;
02603 //////////////////////////////////////////////////////////////////////////
02604 //
02605 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #310
02606 // 
02607 // Projecting p.d.f and data ranges in continuous observables
02608 //
02609 //
02610 //
02611 // 07/2008 - Wouter Verkerke 
02612 // 
02613 /////////////////////////////////////////////////////////////////////////
02614 
02615 #ifndef __CINT__
02616 #include "RooGlobalFunc.h"
02617 #endif
02618 #include "RooRealVar.h"
02619 #include "RooDataSet.h"
02620 #include "RooGaussian.h"
02621 #include "RooProdPdf.h"
02622 #include "RooAddPdf.h"
02623 #include "RooPolynomial.h"
02624 #include "TCanvas.h"
02625 #include "RooPlot.h"
02626 using namespace RooFit ;
02627 
02628 
02629 class TestBasic311 : public RooFitTestUnit
02630 {
02631 public: 
02632   TestBasic311(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Data and p.d.f projection in sub range",refFile,writeRef,verbose) {} ;
02633   Bool_t testCode() {
02634 
02635   // C r e a t e   3 D   p d f   a n d   d a t a 
02636   // -------------------------------------------
02637 
02638   // Create observables
02639   RooRealVar x("x","x",-5,5) ;
02640   RooRealVar y("y","y",-5,5) ;
02641   RooRealVar z("z","z",-5,5) ;
02642 
02643   // Create signal pdf gauss(x)*gauss(y)*gauss(z) 
02644   RooGaussian gx("gx","gx",x,RooConst(0),RooConst(1)) ;
02645   RooGaussian gy("gy","gy",y,RooConst(0),RooConst(1)) ;
02646   RooGaussian gz("gz","gz",z,RooConst(0),RooConst(1)) ;
02647   RooProdPdf sig("sig","sig",RooArgSet(gx,gy,gz)) ;
02648 
02649   // Create background pdf poly(x)*poly(y)*poly(z) 
02650   RooPolynomial px("px","px",x,RooArgSet(RooConst(-0.1),RooConst(0.004))) ;
02651   RooPolynomial py("py","py",y,RooArgSet(RooConst(0.1),RooConst(-0.004))) ;
02652   RooPolynomial pz("pz","pz",z) ;
02653   RooProdPdf bkg("bkg","bkg",RooArgSet(px,py,pz)) ;
02654 
02655   // Create composite pdf sig+bkg
02656   RooRealVar fsig("fsig","signal fraction",0.1,0.,1.) ;
02657   RooAddPdf model("model","model",RooArgList(sig,bkg),fsig) ;
02658 
02659   RooDataSet* data = model.generate(RooArgSet(x,y,z),20000) ;
02660 
02661 
02662 
02663   // P r o j e c t   p d f   a n d   d a t a   o n   x
02664   // -------------------------------------------------
02665 
02666   // Make plain projection of data and pdf on x observable
02667   RooPlot* frame = x.frame(Title("Projection of 3D data and pdf on X"),Bins(40)) ;
02668   data->plotOn(frame) ;
02669   model.plotOn(frame) ;
02670   
02671 
02672 
02673   // P r o j e c t   p d f   a n d   d a t a   o n   x   i n   s i g n a l   r a n g e
02674   // ----------------------------------------------------------------------------------
02675   
02676   // Define signal region in y and z observables
02677   y.setRange("sigRegion",-1,1) ;
02678   z.setRange("sigRegion",-1,1) ;
02679 
02680   // Make plot frame
02681   RooPlot* frame2 = x.frame(Title("Same projection on X in signal range of (Y,Z)"),Bins(40)) ;
02682 
02683   // Plot subset of data in which all observables are inside "sigRegion"
02684   // For observables that do not have an explicit "sigRegion" range defined (e.g. observable)
02685   // an implicit definition is used that is identical to the full range (i.e. [-5,5] for x)
02686   data->plotOn(frame2,CutRange("sigRegion")) ;
02687 
02688   // Project model on x, integrating projected observables (y,z) only in "sigRegion"
02689   model.plotOn(frame2,ProjectionRange("sigRegion")) ;
02690 
02691 
02692   regPlot(frame,"rf311_plot1") ;
02693   regPlot(frame2,"rf312_plot2") ;
02694 
02695   delete data ;
02696 
02697   return kTRUE ;
02698   }
02699 } ;
02700 //////////////////////////////////////////////////////////////////////////
02701 //
02702 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #312
02703 // 
02704 // Performing fits in multiple (disjoint) ranges in one or more dimensions
02705 //
02706 //
02707 //
02708 // 07/2008 - Wouter Verkerke 
02709 // 
02710 /////////////////////////////////////////////////////////////////////////
02711 
02712 #ifndef __CINT__
02713 #include "RooGlobalFunc.h"
02714 #endif
02715 #include "RooRealVar.h"
02716 #include "RooDataSet.h"
02717 #include "RooGaussian.h"
02718 #include "RooProdPdf.h"
02719 #include "RooAddPdf.h"
02720 #include "RooPolynomial.h"
02721 #include "TCanvas.h"
02722 #include "RooPlot.h"
02723 #include "RooFitResult.h"
02724 using namespace RooFit ;
02725 
02726 
02727 class TestBasic312 : public RooFitTestUnit
02728 {
02729 public: 
02730   TestBasic312(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Fit in multiple rectangular ranges",refFile,writeRef,verbose) {} ;
02731   Bool_t testCode() {
02732 
02733   // C r e a t e   2 D   p d f   a n d   d a t a 
02734   // -------------------------------------------
02735 
02736   // Define observables x,y
02737   RooRealVar x("x","x",-10,10) ;
02738   RooRealVar y("y","y",-10,10) ;
02739 
02740   // Construct the signal pdf gauss(x)*gauss(y)
02741   RooRealVar mx("mx","mx",1,-10,10) ;
02742   RooRealVar my("my","my",1,-10,10) ;
02743 
02744   RooGaussian gx("gx","gx",x,mx,RooConst(1)) ;
02745   RooGaussian gy("gy","gy",y,my,RooConst(1)) ;
02746 
02747   RooProdPdf sig("sig","sig",gx,gy) ;
02748 
02749   // Construct the background pdf (flat in x,y)
02750   RooPolynomial px("px","px",x) ;
02751   RooPolynomial py("py","py",y) ;
02752   RooProdPdf bkg("bkg","bkg",px,py) ;
02753 
02754   // Construct the composite model sig+bkg
02755   RooRealVar f("f","f",0.,1.) ;
02756   RooAddPdf model("model","model",RooArgList(sig,bkg),f) ;
02757 
02758   // Sample 10000 events in (x,y) from the model
02759   RooDataSet* modelData = model.generate(RooArgSet(x,y),10000) ;
02760 
02761 
02762 
02763   // D e f i n e   s i g n a l   a n d   s i d e b a n d   r e g i o n s
02764   // -------------------------------------------------------------------
02765 
02766   // Construct the SideBand1,SideBand2,Signal regions
02767   //
02768   //                    |
02769   //      +-------------+-----------+                 
02770   //      |             |           |             
02771   //      |    Side     |   Sig     |        
02772   //      |    Band1    |   nal     |             
02773   //      |             |           |            
02774   //    --+-------------+-----------+--   
02775   //      |                         |       
02776   //      |           Side          |        
02777   //      |           Band2         |            
02778   //      |                         |          
02779   //      +-------------+-----------+            
02780   //                    |                       
02781 
02782   x.setRange("SB1",-10,+10) ;
02783   y.setRange("SB1",-10,0) ;
02784 
02785   x.setRange("SB2",-10,0) ;
02786   y.setRange("SB2",0,+10) ;
02787 
02788   x.setRange("SIG",0,+10) ;
02789   y.setRange("SIG",0,+10) ;
02790 
02791   x.setRange("FULL",-10,+10) ;
02792   y.setRange("FULL",-10,+10) ;
02793 
02794 
02795   // P e r f o r m   f i t s   i n   i n d i v i d u a l   s i d e b a n d   r e g i o n s
02796   // -------------------------------------------------------------------------------------
02797 
02798   // Perform fit in SideBand1 region (RooAddPdf coefficients will be interpreted in full range)
02799   RooFitResult* r_sb1 = model.fitTo(*modelData,Range("SB1"),Save()) ;
02800 
02801   // Perform fit in SideBand2 region (RooAddPdf coefficients will be interpreted in full range)
02802   RooFitResult* r_sb2 = model.fitTo(*modelData,Range("SB2"),Save()) ;
02803 
02804 
02805 
02806   // P e r f o r m   f i t s   i n   j o i n t    s i d e b a n d   r e g i o n s
02807   // -----------------------------------------------------------------------------
02808 
02809   // Now perform fit to joint 'L-shaped' sideband region 'SB1|SB2' 
02810   // (RooAddPdf coefficients will be interpreted in full range)
02811   RooFitResult* r_sb12 = model.fitTo(*modelData,Range("SB1,SB2"),Save()) ;
02812 
02813 
02814   regResult(r_sb1,"rf312_fit_sb1") ;
02815   regResult(r_sb2,"rf312_fit_sb2") ;
02816   regResult(r_sb12,"rf312_fit_sb12") ;
02817 
02818   delete modelData ;
02819 
02820   return kTRUE ;
02821 
02822   }
02823 } ;
02824 //////////////////////////////////////////////////////////////////////////
02825 //
02826 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #313
02827 // 
02828 // Working with parameterized ranges to define non-rectangular regions
02829 // for fitting and integration
02830 //
02831 //
02832 //
02833 // 07/2008 - Wouter Verkerke 
02834 // 
02835 /////////////////////////////////////////////////////////////////////////
02836 
02837 #ifndef __CINT__
02838 #include "RooGlobalFunc.h"
02839 #endif
02840 #include "RooRealVar.h"
02841 #include "RooDataSet.h"
02842 #include "RooGaussian.h"
02843 #include "RooPolynomial.h"
02844 #include "RooProdPdf.h"
02845 #include "TCanvas.h"
02846 #include "RooPlot.h"
02847 using namespace RooFit ;
02848 
02849 
02850 class TestBasic313 : public RooFitTestUnit
02851 {
02852 public: 
02853   TestBasic313(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Integration over non-rectangular regions",refFile,writeRef,verbose) {} ;
02854   Bool_t testCode() {
02855 
02856   // C r e a t e   3 D   p d f 
02857   // -------------------------
02858 
02859   // Define observable (x,y,z)
02860   RooRealVar x("x","x",0,10) ;
02861   RooRealVar y("y","y",0,10) ;
02862   RooRealVar z("z","z",0,10) ;
02863 
02864   // Define 3 dimensional pdf
02865   RooRealVar z0("z0","z0",-0.1,1) ;
02866   RooPolynomial px("px","px",x,RooConst(0)) ;
02867   RooPolynomial py("py","py",y,RooConst(0)) ;
02868   RooPolynomial pz("pz","pz",z,z0) ;
02869   RooProdPdf pxyz("pxyz","pxyz",RooArgSet(px,py,pz)) ;
02870 
02871 
02872 
02873   // D e f i n e d   n o n - r e c t a n g u l a r   r e g i o n   R   i n   ( x , y , z ) 
02874   // -------------------------------------------------------------------------------------
02875 
02876   //
02877   // R = Z[0 - 0.1*Y^2] * Y[0.1*X - 0.9*X] * X[0 - 10]
02878   //
02879 
02880   // Construct range parameterized in "R" in y [ 0.1*x, 0.9*x ]
02881   RooFormulaVar ylo("ylo","0.1*x",x) ;
02882   RooFormulaVar yhi("yhi","0.9*x",x) ;
02883   y.setRange("R",ylo,yhi) ;
02884 
02885   // Construct parameterized ranged "R" in z [ 0, 0.1*y^2 ]
02886   RooFormulaVar zlo("zlo","0.0*y",y) ;
02887   RooFormulaVar zhi("zhi","0.1*y*y",y) ;
02888   z.setRange("R",zlo,zhi) ;
02889 
02890 
02891 
02892   // C a l c u l a t e   i n t e g r a l   o f   n o r m a l i z e d   p d f   i n   R 
02893   // ----------------------------------------------------------------------------------
02894 
02895   // Create integral over normalized pdf model over x,y,z in "R" region
02896   RooAbsReal* intPdf = pxyz.createIntegral(RooArgSet(x,y,z),RooArgSet(x,y,z),"R") ;
02897 
02898   // Plot value of integral as function of pdf parameter z0
02899   RooPlot* frame = z0.frame(Title("Integral of pxyz over x,y,z in region R")) ;
02900   intPdf->plotOn(frame) ;
02901 
02902 
02903   regPlot(frame,"rf313_plot1") ;
02904 
02905   delete intPdf ;
02906 
02907   return kTRUE;
02908   }
02909 } ;
02910 //////////////////////////////////////////////////////////////////////////
02911 //
02912 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #314
02913 // 
02914 // Working with parameterized ranges in a fit. This an example of a
02915 // fit with an acceptance that changes per-event 
02916 //
02917 //  pdf = exp(-t/tau) with t[tmin,5]
02918 //
02919 //  where t and tmin are both observables in the dataset
02920 //
02921 // 07/2008 - Wouter Verkerke 
02922 // 
02923 /////////////////////////////////////////////////////////////////////////
02924 
02925 #ifndef __CINT__
02926 #include "RooGlobalFunc.h"
02927 #endif
02928 #include "RooRealVar.h"
02929 #include "RooDataSet.h"
02930 #include "RooGaussian.h"
02931 #include "RooExponential.h"
02932 #include "TCanvas.h"
02933 #include "RooPlot.h"
02934 #include "RooFitResult.h"
02935 
02936 using namespace RooFit ;
02937 
02938 
02939 class TestBasic314 : public RooFitTestUnit
02940 {
02941 public: 
02942   TestBasic314(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Fit with non-rectangular observable boundaries",refFile,writeRef,verbose) {} ;
02943   Bool_t testCode() {
02944 
02945   // D e f i n e   o b s e r v a b l e s   a n d   d e c a y   p d f 
02946   // ---------------------------------------------------------------
02947 
02948   // Declare observables
02949   RooRealVar t("t","t",0,5) ;
02950   RooRealVar tmin("tmin","tmin",0,0,5) ;
02951 
02952   // Make parameterized range in t : [tmin,5]
02953   t.setRange(tmin,RooConst(t.getMax())) ;
02954 
02955   // Make pdf
02956   RooRealVar tau("tau","tau",-1.54,-10,-0.1) ;
02957   RooExponential model("model","model",t,tau) ;
02958 
02959 
02960 
02961   // C r e a t e   i n p u t   d a t a 
02962   // ------------------------------------
02963 
02964   // Generate complete dataset without acceptance cuts (for reference)  
02965   RooDataSet* dall = model.generate(t,10000) ;
02966 
02967   // Generate a (fake) prototype dataset for acceptance limit values
02968   RooDataSet* tmp = RooGaussian("gmin","gmin",tmin,RooConst(0),RooConst(0.5)).generate(tmin,5000) ;
02969 
02970   // Generate dataset with t values that observe (t>tmin)
02971   RooDataSet* dacc = model.generate(t,ProtoData(*tmp)) ;
02972 
02973 
02974 
02975   // F i t   p d f   t o   d a t a   i n   a c c e p t a n c e   r e g i o n
02976   // -----------------------------------------------------------------------
02977 
02978   RooFitResult* r = model.fitTo(*dacc,Save()) ;
02979 
02980 
02981  
02982   // P l o t   f i t t e d   p d f   o n   f u l l   a n d   a c c e p t e d   d a t a 
02983   // ---------------------------------------------------------------------------------
02984 
02985   // Make plot frame, add datasets and overlay model
02986   RooPlot* frame = t.frame(Title("Fit to data with per-event acceptance")) ;
02987   dall->plotOn(frame,MarkerColor(kRed),LineColor(kRed)) ;
02988   model.plotOn(frame) ;
02989   dacc->plotOn(frame,Name("dacc")) ;
02990 
02991   // Print fit results to demonstrate absence of bias
02992   regResult(r,"rf314_fit") ;
02993   regPlot(frame,"rf314_plot1") ;
02994 
02995   delete tmp ;
02996   delete dacc ;
02997   delete dall ;
02998 
02999   return kTRUE;
03000   }
03001 } ;
03002 
03003 
03004 //////////////////////////////////////////////////////////////////////////
03005 //
03006 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #315
03007 // 
03008 // Marginizalization of multi-dimensional p.d.f.s through integration
03009 //
03010 //
03011 //
03012 // 07/2008 - Wouter Verkerke 
03013 // 
03014 /////////////////////////////////////////////////////////////////////////
03015 
03016 #ifndef __CINT__
03017 #include "RooGlobalFunc.h"
03018 #endif
03019 #include "RooRealVar.h"
03020 #include "RooDataSet.h"
03021 #include "RooGaussian.h"
03022 #include "RooProdPdf.h"
03023 #include "RooPolyVar.h"
03024 #include "TH1.h"
03025 #include "TCanvas.h"
03026 #include "RooPlot.h"
03027 #include "RooNumIntConfig.h"
03028 #include "RooConstVar.h"
03029 using namespace RooFit ;
03030 
03031 
03032 
03033 class TestBasic315 : public RooFitTestUnit
03034 {
03035 public: 
03036   TestBasic315(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("P.d.f. marginalization through integration",refFile,writeRef,verbose) {} ;
03037   Bool_t testCode() {
03038 
03039     // C r e a t e   p d f   m ( x , y )  =  g x ( x | y ) * g ( y )
03040     // --------------------------------------------------------------
03041     
03042     // Increase default precision of numeric integration
03043     // as this exercise has high sensitivity to numeric integration precision
03044     RooAbsPdf::defaultIntegratorConfig()->setEpsRel(1e-8) ;
03045     RooAbsPdf::defaultIntegratorConfig()->setEpsAbs(1e-8) ;
03046     
03047     // Create observables
03048     RooRealVar x("x","x",-5,5) ;
03049     RooRealVar y("y","y",-2,2) ;
03050     
03051     // Create function f(y) = a0 + a1*y
03052     RooRealVar a0("a0","a0",0) ;
03053     RooRealVar a1("a1","a1",-1.5,-3,1) ;
03054     RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
03055     
03056     // Create gaussx(x,f(y),sx)
03057     RooRealVar sigmax("sigmax","width of gaussian",0.5) ;
03058     RooGaussian gaussx("gaussx","Gaussian in x with shifting mean in y",x,fy,sigmax) ;  
03059     
03060     // Create gaussy(y,0,2)
03061     RooGaussian gaussy("gaussy","Gaussian in y",y,RooConst(0),RooConst(2)) ;
03062     
03063     // Create gaussx(x,sx|y) * gaussy(y)
03064     RooProdPdf model("model","gaussx(x|y)*gaussy(y)",gaussy,Conditional(gaussx,x)) ;
03065     
03066     
03067     
03068     // M a r g i n a l i z e   m ( x , y )   t o   m ( x ) 
03069     // ----------------------------------------------------
03070     
03071     // modelx(x) = Int model(x,y) dy
03072     RooAbsPdf* modelx = model.createProjection(y) ;
03073     
03074     
03075     
03076     // U s e   m a r g i n a l i z e d   p . d . f .   a s   r e g u l a r   1 - D   p . d . f .
03077     // ------------------------------------------------------------------------------------------
03078     
03079     // Sample 1000 events from modelx
03080     RooAbsData* data = modelx->generateBinned(x,1000) ;
03081     
03082     // Fit modelx to toy data
03083     modelx->fitTo(*data) ;
03084     
03085     // Plot modelx over data
03086     RooPlot* frame = x.frame(40) ;
03087     data->plotOn(frame) ;
03088     modelx->plotOn(frame) ;
03089     
03090     regPlot(frame,"rf315_frame") ;
03091 
03092     delete data ;
03093     delete modelx ;
03094 
03095     return kTRUE ;
03096   }
03097 } ;
03098 
03099 
03100 
03101 
03102 
03103 //////////////////////////////////////////////////////////////////////////
03104 //
03105 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #316
03106 // 
03107 // Using the likelihood ratio techique to construct a signal enhanced
03108 // one-dimensional projection of a multi-dimensional p.d.f.
03109 //
03110 //
03111 //
03112 // 07/2008 - Wouter Verkerke 
03113 // 
03114 /////////////////////////////////////////////////////////////////////////
03115 
03116 #ifndef __CINT__
03117 #include "RooGlobalFunc.h"
03118 #endif
03119 #include "RooRealVar.h"
03120 #include "RooDataSet.h"
03121 #include "RooGaussian.h"
03122 #include "RooPolynomial.h"
03123 #include "RooAddPdf.h"
03124 #include "RooProdPdf.h"
03125 #include "TCanvas.h"
03126 #include "RooPlot.h"
03127 using namespace RooFit ;
03128 
03129 
03130 class TestBasic316 : public RooFitTestUnit
03131 {
03132 public: 
03133   TestBasic316(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Likelihood ratio projection plot",refFile,writeRef,verbose) {} ;
03134   Bool_t testCode() {
03135 
03136   // C r e a t e   3 D   p d f   a n d   d a t a 
03137   // -------------------------------------------
03138 
03139   // Create observables
03140   RooRealVar x("x","x",-5,5) ;
03141   RooRealVar y("y","y",-5,5) ;
03142   RooRealVar z("z","z",-5,5) ;
03143 
03144   // Create signal pdf gauss(x)*gauss(y)*gauss(z) 
03145   RooGaussian gx("gx","gx",x,RooConst(0),RooConst(1)) ;
03146   RooGaussian gy("gy","gy",y,RooConst(0),RooConst(1)) ;
03147   RooGaussian gz("gz","gz",z,RooConst(0),RooConst(1)) ;
03148   RooProdPdf sig("sig","sig",RooArgSet(gx,gy,gz)) ;
03149 
03150   // Create background pdf poly(x)*poly(y)*poly(z) 
03151   RooPolynomial px("px","px",x,RooArgSet(RooConst(-0.1),RooConst(0.004))) ;
03152   RooPolynomial py("py","py",y,RooArgSet(RooConst(0.1),RooConst(-0.004))) ;
03153   RooPolynomial pz("pz","pz",z) ;
03154   RooProdPdf bkg("bkg","bkg",RooArgSet(px,py,pz)) ;
03155 
03156   // Create composite pdf sig+bkg
03157   RooRealVar fsig("fsig","signal fraction",0.1,0.,1.) ;
03158   RooAddPdf model("model","model",RooArgList(sig,bkg),fsig) ;
03159 
03160   RooDataSet* data = model.generate(RooArgSet(x,y,z),20000) ;
03161 
03162 
03163 
03164   // P r o j e c t   p d f   a n d   d a t a   o n   x
03165   // -------------------------------------------------
03166 
03167   // Make plain projection of data and pdf on x observable
03168   RooPlot* frame = x.frame(Title("Projection of 3D data and pdf on X"),Bins(40)) ;
03169   data->plotOn(frame) ;
03170   model.plotOn(frame) ;
03171   
03172 
03173 
03174   // D e f i n e   p r o j e c t e d   s i g n a l   l i k e l i h o o d   r a t i o
03175   // ----------------------------------------------------------------------------------
03176 
03177   // Calculate projection of signal and total likelihood on (y,z) observables
03178   // i.e. integrate signal and composite model over x
03179   RooAbsPdf* sigyz = sig.createProjection(x) ;
03180   RooAbsPdf* totyz = model.createProjection(x) ;
03181 
03182   // Construct the log of the signal / signal+background probability 
03183   RooFormulaVar llratio_func("llratio","log10(@0)-log10(@1)",RooArgList(*sigyz,*totyz)) ;
03184 
03185 
03186 
03187   // P l o t   d a t a   w i t h   a   L L r a t i o   c u t 
03188   // -------------------------------------------------------
03189 
03190   // Calculate the llratio value for each event in the dataset
03191   data->addColumn(llratio_func) ;
03192 
03193   // Extract the subset of data with large signal likelihood
03194   RooDataSet* dataSel = (RooDataSet*) data->reduce(Cut("llratio>0.7")) ;
03195 
03196   // Make plot frame
03197   RooPlot* frame2 = x.frame(Title("Same projection on X with LLratio(y,z)>0.7"),Bins(40)) ;
03198 
03199   // Plot select data on frame
03200   dataSel->plotOn(frame2) ;
03201 
03202 
03203 
03204   // M a k e   M C   p r o j e c t i o n   o f   p d f   w i t h   s a m e   L L r a t i o   c u t 
03205   // ---------------------------------------------------------------------------------------------
03206 
03207   // Generate large number of events for MC integration of pdf projection
03208   RooDataSet* mcprojData = model.generate(RooArgSet(x,y,z),10000) ;
03209 
03210   // Calculate LL ratio for each generated event and select MC events with llratio)0.7
03211   mcprojData->addColumn(llratio_func) ;
03212   RooDataSet* mcprojDataSel = (RooDataSet*) mcprojData->reduce(Cut("llratio>0.7")) ;
03213     
03214   // Project model on x, integrating projected observables (y,z) with Monte Carlo technique
03215   // on set of events with the same llratio cut as was applied to data
03216   model.plotOn(frame2,ProjWData(*mcprojDataSel)) ;
03217 
03218 
03219   regPlot(frame,"rf316_plot1") ;
03220   regPlot(frame2,"rf316_plot2") ;
03221   
03222   delete data ;
03223   delete dataSel ;
03224   delete mcprojData ;
03225   delete mcprojDataSel ;
03226   delete sigyz ;
03227   delete totyz ;
03228 
03229   return kTRUE ;
03230   }  
03231 } ;
03232 //////////////////////////////////////////////////////////////////////////
03233 //
03234 // 'DATA AND CATEGORIES' RooFit tutorial macro #402
03235 // 
03236 // Tools for manipulation of (un)binned datasets
03237 //
03238 //
03239 //
03240 // 07/2008 - Wouter Verkerke 
03241 // 
03242 /////////////////////////////////////////////////////////////////////////
03243 
03244 #ifndef __CINT__
03245 #include "RooGlobalFunc.h"
03246 #endif
03247 #include "RooRealVar.h"
03248 #include "RooDataSet.h"
03249 #include "RooDataHist.h"
03250 #include "RooGaussian.h"
03251 #include "RooCategory.h"
03252 #include "TCanvas.h"
03253 #include "RooPlot.h"
03254 #include "TFile.h"
03255 using namespace RooFit ;
03256 
03257 
03258 class TestBasic402 : public RooFitTestUnit
03259 {
03260 public: 
03261   TestBasic402(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Basic operations on datasets",refFile,writeRef,verbose) {} ;
03262   Bool_t testCode() {
03263 
03264   // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
03265   // many properties and inherit from a common abstract base class
03266   // (RooAbsData), that provides an interface for all operations
03267   // that can be performed regardless of the data format
03268 
03269   RooRealVar  x("x","x",-10,10) ;
03270   RooRealVar  y("y","y", 0, 40) ;
03271   RooCategory c("c","c") ;
03272   c.defineType("Plus",+1) ;
03273   c.defineType("Minus",-1) ;
03274 
03275 
03276 
03277   // B a s i c   O p e r a t i o n s   o n   u n b i n n e d   d a t a s e t s 
03278   // --------------------------------------------------------------
03279 
03280   // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
03281   RooDataSet d("d","d",RooArgSet(x,y,c)) ;
03282 
03283   // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to 
03284   // the variables they are constructed from. Instead they are attached to an internal 
03285   // clone of the supplied set of arguments
03286 
03287   // Fill d with dummy values
03288   Int_t i ;
03289   for (i=0 ; i<1000 ; i++) {
03290     x = i/50 - 10 ;
03291     y = sqrt(1.0*i) ;
03292     c.setLabel((i%2)?"Plus":"Minus") ;
03293 
03294     // We must explicitly refer to x,y,c here to pass the values because
03295     // d is not linked to them (as explained above)
03296     d.add(RooArgSet(x,y,c)) ;
03297   }
03298 
03299 
03300   // R e d u c i n g ,   A p p e n d i n g   a n d   M e r g i n g
03301   // -------------------------------------------------------------
03302 
03303   // The reduce() function returns a new dataset which is a subset of the original
03304   RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ; 
03305   RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;   
03306   RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ; 
03307   RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ; 
03308 
03309   regValue(d3->numEntries(),"rf403_nd3") ;
03310   regValue(d4->numEntries(),"rf403_nd4") ;
03311 
03312   // The merge() function adds two data set column-wise
03313   d1->merge(d2) ;
03314 
03315   // The append() function addes two datasets row-wise
03316   d1->append(*d3) ;
03317 
03318   regValue(d1->numEntries(),"rf403_nd1") ;
03319 
03320   
03321 
03322 
03323   // O p e r a t i o n s   o n   b i n n e d   d a t a s e t s 
03324   // ---------------------------------------------------------
03325 
03326   // A binned dataset can be constructed empty, from an unbinned dataset, or
03327   // from a ROOT native histogram (TH1,2,3)
03328 
03329   // The binning of real variables (like x,y) is done using their fit range
03330   // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
03331   // Category dimensions of binned datasets get one bin per defined category state
03332   x.setBins(10) ;
03333   y.setBins(10) ;
03334   RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
03335 
03336   RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
03337   dh.plotOn(yframe) ; // plot projection of 2D binned data on y
03338 
03339   // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
03340   //
03341   // All reduce() methods are interfaced in RooAbsData. All reduction techniques
03342   // demonstrated on unbinned datasets can be applied to binned datasets as well.
03343   RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
03344 
03345   // Add dh2 to yframe and redraw
03346   dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed),Name("dh2")) ;
03347 
03348   regPlot(yframe,"rf402_plot1") ;
03349 
03350   delete d1 ;
03351   delete d2 ;
03352   delete d3 ;
03353   delete d4 ;
03354   delete dh2 ;
03355   return kTRUE ;
03356   }
03357 } ;
03358 //////////////////////////////////////////////////////////////////////////
03359 //
03360 // 'DATA AND CATEGORIES' RooFit tutorial macro #403
03361 // 
03362 // Using weights in unbinned datasets
03363 //
03364 //
03365 //
03366 // 07/2008 - Wouter Verkerke 
03367 // 
03368 /////////////////////////////////////////////////////////////////////////
03369 
03370 #ifndef __CINT__
03371 #include "RooGlobalFunc.h"
03372 #endif
03373 #include "RooRealVar.h"
03374 #include "RooDataSet.h"
03375 #include "RooDataHist.h"
03376 #include "RooGaussian.h"
03377 #include "RooFormulaVar.h"
03378 #include "RooGenericPdf.h"
03379 #include "RooPolynomial.h"
03380 #include "RooChi2Var.h"
03381 #include "RooMinuit.h"
03382 #include "TCanvas.h"
03383 #include "RooPlot.h"
03384 #include "RooFitResult.h"
03385 using namespace RooFit ;
03386 
03387 
03388 class TestBasic403 : public RooFitTestUnit
03389 {
03390 public: 
03391   TestBasic403(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Fits with weighted datasets",refFile,writeRef,verbose) {} ;
03392   Bool_t testCode() {
03393 
03394   // C r e a t e   o b s e r v a b l e   a n d   u n w e i g h t e d   d a t a s e t 
03395   // -------------------------------------------------------------------------------
03396 
03397   // Declare observable
03398   RooRealVar x("x","x",-10,10) ;
03399   x.setBins(40) ;
03400 
03401   // Construction a uniform pdf
03402   RooPolynomial p0("px","px",x) ;
03403 
03404   // Sample 1000 events from pdf
03405   RooDataSet* data = p0.generate(x,1000) ;
03406 
03407  
03408 
03409   // C a l c u l a t e   w e i g h t   a n d   m a k e   d a t a s e t   w e i g h t e d 
03410   // -----------------------------------------------------------------------------------
03411 
03412   // Construct formula to calculate (fake) weight for events
03413   RooFormulaVar wFunc("w","event weight","(x*x+10)",x) ;
03414 
03415   // Add column with variable w to previously generated dataset
03416   RooRealVar* w = (RooRealVar*) data->addColumn(wFunc) ;
03417 
03418   // Instruct dataset d in interpret w as event weight rather than as observable
03419   RooDataSet dataw(data->GetName(),data->GetTitle(),data,*data->get(),0,w->GetName()) ;
03420   //data->setWeightVar(*w) ;
03421 
03422 
03423   // U n b i n n e d   M L   f i t   t o   w e i g h t e d   d a t a 
03424   // ---------------------------------------------------------------
03425 
03426   // Construction quadratic polynomial pdf for fitting
03427   RooRealVar a0("a0","a0",1) ;
03428   RooRealVar a1("a1","a1",0,-1,1) ;
03429   RooRealVar a2("a2","a2",1,0,10) ;
03430   RooPolynomial p2("p2","p2",x,RooArgList(a0,a1,a2),0) ;
03431 
03432   // Fit quadratic polynomial to weighted data
03433 
03434   // NOTE: Maximum likelihood fit to weighted data does in general 
03435   //       NOT result in correct error estimates, unless individual
03436   //       event weights represent Poisson statistics themselves.
03437   //       In general, parameter error reflect precision of SumOfWeights
03438   //       events rather than NumEvents events. See comparisons below
03439   RooFitResult* r_ml_wgt = p2.fitTo(dataw,Save()) ;
03440 
03441 
03442 
03443   // P l o t   w e i g h e d   d a t a   a n d   f i t   r e s u l t 
03444   // ---------------------------------------------------------------
03445 
03446   // Construct plot frame
03447   RooPlot* frame = x.frame(Title("Unbinned ML fit, binned chi^2 fit to weighted data")) ;
03448 
03449   // Plot data using sum-of-weights-squared error rather than Poisson errors
03450   dataw.plotOn(frame,DataError(RooAbsData::SumW2)) ;
03451 
03452   // Overlay result of 2nd order polynomial fit to weighted data
03453   p2.plotOn(frame) ;
03454 
03455 
03456 
03457   // M L  F i t   o f   p d f   t o   e q u i v a l e n t  u n w e i g h t e d   d a t a s e t
03458   // -----------------------------------------------------------------------------------------
03459   
03460   // Construct a pdf with the same shape as p0 after weighting
03461   RooGenericPdf genPdf("genPdf","x*x+10",x) ;
03462 
03463   // Sample a dataset with the same number of events as data
03464   RooDataSet* data2 = genPdf.generate(x,1000) ;
03465 
03466   // Sample a dataset with the same number of weights as data
03467   RooDataSet* data3 = genPdf.generate(x,43000) ;
03468 
03469   // Fit the 2nd order polynomial to both unweighted datasets and save the results for comparison
03470   RooFitResult* r_ml_unw10 = p2.fitTo(*data2,Save()) ;
03471   RooFitResult* r_ml_unw43 = p2.fitTo(*data3,Save()) ;
03472 
03473 
03474   // C h i 2   f i t   o f   p d f   t o   b i n n e d   w e i g h t e d   d a t a s e t
03475   // ------------------------------------------------------------------------------------
03476 
03477   // Construct binned clone of unbinned weighted dataset
03478   RooDataHist* binnedData = dataw.binnedClone() ;
03479 
03480   // Perform chi2 fit to binned weighted dataset using sum-of-weights errors
03481   // 
03482   // NB: Within the usual approximations of a chi2 fit, a chi2 fit to weighted
03483   // data using sum-of-weights-squared errors does give correct error
03484   // estimates
03485   RooChi2Var chi2("chi2","chi2",p2,*binnedData,DataError(RooAbsData::SumW2)) ;
03486   RooMinuit m(chi2) ;
03487   m.migrad() ;
03488   m.hesse() ;
03489 
03490   // Plot chi^2 fit result on frame as well
03491   RooFitResult* r_chi2_wgt = m.save() ;
03492   p2.plotOn(frame,LineStyle(kDashed),LineColor(kRed),Name("p2_alt")) ;
03493 
03494 
03495 
03496   // C o m p a r e   f i t   r e s u l t s   o f   c h i 2 , M L   f i t s   t o   ( u n ) w e i g h t e d   d a t a 
03497   // ---------------------------------------------------------------------------------------------------------------
03498 
03499   // Note that ML fit on 1Kevt of weighted data is closer to result of ML fit on 43Kevt of unweighted data 
03500   // than to 1Kevt of unweighted data, whereas the reference chi^2 fit with SumW2 error gives a result closer to
03501   // that of an unbinned ML fit to 1Kevt of unweighted data. 
03502 
03503   regResult(r_ml_unw10,"rf403_ml_unw10") ;
03504   regResult(r_ml_unw43,"rf403_ml_unw43") ;
03505   regResult(r_ml_wgt  ,"rf403_ml_wgt") ;
03506   regResult(r_chi2_wgt ,"rf403_ml_chi2") ;
03507   regPlot(frame,"rf403_plot1") ;
03508   
03509   delete binnedData ;
03510   delete data ;
03511   delete data2 ;
03512   delete data3 ;
03513 
03514   return kTRUE ;
03515   }
03516 } ;
03517 //////////////////////////////////////////////////////////////////////////
03518 //
03519 // 'DATA AND CATEGORIES' RooFit tutorial macro #404
03520 // 
03521 // Working with RooCategory objects to describe discrete variables
03522 //
03523 //
03524 //
03525 // 07/2008 - Wouter Verkerke 
03526 // 
03527 /////////////////////////////////////////////////////////////////////////
03528 
03529 #ifndef __CINT__
03530 #include "RooGlobalFunc.h"
03531 #endif
03532 #include "RooRealVar.h"
03533 #include "RooDataSet.h"
03534 #include "RooPolynomial.h"
03535 #include "RooCategory.h"
03536 #include "Roo1DTable.h"
03537 #include "RooGaussian.h"
03538 #include "TCanvas.h"
03539 #include "RooPlot.h"
03540 using namespace RooFit ;
03541 
03542 
03543 class TestBasic404 : public RooFitTestUnit
03544 {
03545 public: 
03546   TestBasic404(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Categories basic functionality",refFile,writeRef,verbose) {} ;
03547   Bool_t testCode() {
03548 
03549   // C o n s t r u c t    a   c a t e g o r y   w i t h   l a b e l s 
03550   // ----------------------------------------------------------------
03551 
03552   // Define a category with labels only
03553   RooCategory tagCat("tagCat","Tagging category") ;
03554   tagCat.defineType("Lepton") ;
03555   tagCat.defineType("Kaon") ;
03556   tagCat.defineType("NetTagger-1") ;
03557   tagCat.defineType("NetTagger-2") ;
03558 
03559 
03560 
03561   // C o n s t r u c t    a   c a t e g o r y   w i t h   l a b e l s   a n d   i n d e c e s
03562   // ----------------------------------------------------------------------------------------
03563 
03564   // Define a category with explicitly numbered states
03565   RooCategory b0flav("b0flav","B0 flavour eigenstate") ;
03566   b0flav.defineType("B0",-1) ;
03567   b0flav.defineType("B0bar",1) ;
03568 
03569 
03570 
03571   // G e n e r a t e   d u m m y   d a t a  f o r   t a b u l a t i o n   d e m o 
03572   // ----------------------------------------------------------------------------
03573   
03574   // Generate a dummy dataset 
03575   RooRealVar x("x","x",0,10) ;
03576   RooDataSet *data = RooPolynomial("p","p",x).generate(RooArgSet(x,b0flav,tagCat),10000) ;
03577 
03578 
03579 
03580   // P r i n t   t a b l e s   o f   c a t e g o r y   c o n t e n t s   o f   d a t a s e t s
03581   // ------------------------------------------------------------------------------------------
03582 
03583   // Tables are equivalent of plots for categories
03584   Roo1DTable* btable = data->table(b0flav) ;
03585 
03586   // Create table for subset of events matching cut expression
03587   Roo1DTable* ttable = data->table(tagCat,"x>8.23") ;
03588 
03589   // Create table for all (tagCat x b0flav) state combinations
03590   Roo1DTable* bttable = data->table(RooArgSet(tagCat,b0flav)) ;
03591 
03592   // Retrieve number of events from table
03593   // Number can be non-integer if source dataset has weighed events
03594   Double_t nb0 = btable->get("B0") ;
03595   regValue(nb0,"rf404_nb0") ;
03596 
03597   // Retrieve fraction of events with "Lepton" tag
03598   Double_t fracLep = ttable->getFrac("Lepton") ;
03599   regValue(fracLep,"rf404_fracLep") ;  
03600 
03601 
03602   // D e f i n i n g   r a n g e s   f o r   p l o t t i n g ,   f i t t i n g   o n   c a t e g o r i e s
03603   // ------------------------------------------------------------------------------------------------------
03604 
03605   // Define named range as comma separated list of labels
03606   tagCat.setRange("good","Lepton,Kaon") ;
03607 
03608   // Or add state names one by one
03609   tagCat.addToRange("soso","NetTagger-1") ;
03610   tagCat.addToRange("soso","NetTagger-2") ;
03611   
03612   // Use category range in dataset reduction specification
03613   RooDataSet* goodData = (RooDataSet*) data->reduce(CutRange("good")) ;
03614   Roo1DTable* gtable = goodData->table(tagCat) ;
03615   
03616 
03617   regTable(btable,"rf404_btable") ;
03618   regTable(ttable,"rf404_ttable") ;
03619   regTable(bttable,"rf404_bttable") ;
03620   regTable(gtable,"rf404_gtable") ;
03621 
03622 
03623   delete goodData ;
03624   delete data ;
03625   return kTRUE ;
03626 
03627   }
03628 
03629 } ;
03630 //////////////////////////////////////////////////////////////////////////
03631 //
03632 // 'DATA AND CATEGORIES' RooFit tutorial macro #405
03633 // 
03634 // Demonstration of real-->discrete mapping functions
03635 //
03636 //
03637 //
03638 // 07/2008 - Wouter Verkerke 
03639 // 
03640 /////////////////////////////////////////////////////////////////////////
03641 
03642 #ifndef __CINT__
03643 #include "RooGlobalFunc.h"
03644 #endif
03645 #include "RooRealVar.h"
03646 #include "RooDataSet.h"
03647 #include "RooGaussian.h"
03648 #include "RooCategory.h"
03649 #include "RooThresholdCategory.h"
03650 #include "RooBinningCategory.h"
03651 #include "Roo1DTable.h"
03652 #include "RooArgusBG.h"
03653 #include "TCanvas.h"
03654 #include "RooPlot.h"
03655 #include "RooRealConstant.h"
03656 using namespace RooFit ;
03657 
03658 
03659 class TestBasic405 : public RooFitTestUnit
03660 {
03661 public: 
03662   TestBasic405(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Real-to-category functions",refFile,writeRef,verbose) {} ;
03663   Bool_t testCode() {
03664 
03665 
03666   // D e f i n e   p d f   i n   x ,   s a m p l e   d a t a s e t   i n   x 
03667   // ------------------------------------------------------------------------
03668 
03669 
03670   // Define a dummy PDF in x 
03671   RooRealVar x("x","x",0,10) ;
03672   RooArgusBG a("a","argus(x)",x,RooRealConstant::value(10),RooRealConstant::value(-1)) ;
03673 
03674   // Generate a dummy dataset 
03675   RooDataSet *data = a.generate(x,10000) ;
03676 
03677 
03678 
03679   // C r e a t e   a   t h r e s h o l d   r e a l - > c a t   f u n c t i o n
03680   // --------------------------------------------------------------------------
03681 
03682   // A RooThresholdCategory is a category function that maps regions in a real-valued 
03683   // input observable observables to state names. At construction time a 'default'
03684   // state name must be specified to which all values of x are mapped that are not
03685   // otherwise assigned
03686   RooThresholdCategory xRegion("xRegion","region of x",x,"Background") ;
03687 
03688   // Specify thresholds and state assignments one-by-one. 
03689   // Each statement specifies that all values _below_ the given value 
03690   // (and above any lower specified threshold) are mapped to the
03691   // category state with the given name
03692   //
03693   // Background | SideBand | Signal | SideBand | Background
03694   //           4.23       5.23     8.23       9.23 
03695   xRegion.addThreshold(4.23,"Background") ;
03696   xRegion.addThreshold(5.23,"SideBand") ;
03697   xRegion.addThreshold(8.23,"Signal") ;
03698   xRegion.addThreshold(9.23,"SideBand") ; 
03699 
03700 
03701 
03702   // U s e   t h r e s h o l d   f u n c t i o n   t o   p l o t   d a t a   r e g i o n s
03703   // -------------------------------------------------------------------------------------
03704 
03705   // Add values of threshold function to dataset so that it can be used as observable
03706   data->addColumn(xRegion) ;
03707 
03708   // Make plot of data in x
03709   RooPlot* xframe = x.frame(Title("Demo of threshold and binning mapping functions")) ;
03710   data->plotOn(xframe) ;
03711 
03712   // Use calculated category to select sideband data
03713   data->plotOn(xframe,Cut("xRegion==xRegion::SideBand"),MarkerColor(kRed),LineColor(kRed),Name("data_cut")) ;
03714 
03715 
03716 
03717   // C r e a t e   a   b i n n i n g    r e a l - > c a t   f u n c t i o n
03718   // ----------------------------------------------------------------------
03719 
03720   // A RooBinningCategory is a category function that maps bins of a (named) binning definition 
03721   // in a real-valued input observable observables to state names. The state names are automatically
03722   // constructed from the variable name, the binning name and the bin number. If no binning name
03723   // is specified the default binning is mapped
03724 
03725   x.setBins(10,"coarse") ;
03726   RooBinningCategory xBins("xBins","coarse bins in x",x,"coarse") ;
03727 
03728 
03729 
03730   // U s e   b i n n i n g   f u n c t i o n   f o r   t a b u l a t i o n   a n d   p l o t t i n g
03731   // -----------------------------------------------------------------------------------------------
03732 
03733   // Print table of xBins state multiplicity. Note that xBins does not need to be an observable in data
03734   // it can be a function of observables in data as well
03735   Roo1DTable* xbtable = data->table(xBins) ;
03736 
03737   // Add values of xBins function to dataset so that it can be used as observable
03738   RooCategory* xb = (RooCategory*) data->addColumn(xBins) ;
03739 
03740   // Define range "alt" as including bins 1,3,5,7,9 
03741   xb->setRange("alt","x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9") ;
03742   
03743   // Construct subset of data matching range "alt" but only for the first 5000 events and plot it on the fram
03744   RooDataSet* dataSel = (RooDataSet*) data->reduce(CutRange("alt"),EventRange(0,5000)) ;
03745 //   dataSel->plotOn(xframe,MarkerColor(kGreen),LineColor(kGreen),Name("data_sel")) ;
03746 
03747 
03748   regTable(xbtable,"rf405_xbtable") ;
03749   regPlot(xframe,"rf405_plot1") ;
03750   
03751   delete data ;
03752   delete dataSel ;
03753 
03754   return kTRUE ;
03755 
03756   }
03757 
03758 } ;
03759 //////////////////////////////////////////////////////////////////////////
03760 //
03761 // 'DATA AND CATEGORIES' RooFit tutorial macro #406
03762 // 
03763 // Demonstration of discrete-->discrete (invertable) functions
03764 //
03765 //
03766 //
03767 // 07/2008 - Wouter Verkerke 
03768 // 
03769 /////////////////////////////////////////////////////////////////////////
03770 
03771 #ifndef __CINT__
03772 #include "RooGlobalFunc.h"
03773 #endif
03774 #include "RooRealVar.h"
03775 #include "RooDataSet.h"
03776 #include "RooPolynomial.h"
03777 #include "RooCategory.h"
03778 #include "RooMappedCategory.h"
03779 #include "RooMultiCategory.h"
03780 #include "RooSuperCategory.h"
03781 #include "Roo1DTable.h"
03782 #include "TCanvas.h"
03783 #include "RooPlot.h"
03784 using namespace RooFit ;
03785 
03786 
03787 class TestBasic406 : public RooFitTestUnit
03788 {
03789 public: 
03790   TestBasic406(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Category-to-category functions",refFile,writeRef,verbose) {} ;
03791   Bool_t testCode() {
03792 
03793   // C o n s t r u c t  t w o   c a t e g o r i e s
03794   // ----------------------------------------------
03795 
03796   // Define a category with labels only
03797   RooCategory tagCat("tagCat","Tagging category") ;
03798   tagCat.defineType("Lepton") ;
03799   tagCat.defineType("Kaon") ;
03800   tagCat.defineType("NetTagger-1") ;
03801   tagCat.defineType("NetTagger-2") ;
03802 
03803   // Define a category with explicitly numbered states
03804   RooCategory b0flav("b0flav","B0 flavour eigenstate") ;
03805   b0flav.defineType("B0",-1) ;
03806   b0flav.defineType("B0bar",1) ;
03807 
03808   // Construct a dummy dataset with random values of tagCat and b0flav
03809   RooRealVar x("x","x",0,10) ;
03810   RooPolynomial p("p","p",x) ;
03811   RooDataSet* data = p.generate(RooArgSet(x,b0flav,tagCat),10000) ;
03812 
03813 
03814 
03815   // C r e a t e   a   c a t - > c a t   m  a p p i n g   c a t e g o r y 
03816   // ---------------------------------------------------------------------
03817 
03818   // A RooMappedCategory is category->category mapping function based on string expression
03819   // The constructor takes an input category an a default state name to which unassigned
03820   // states are mapped
03821   RooMappedCategory tcatType("tcatType","tagCat type",tagCat,"Cut based") ;
03822 
03823   // Enter fully specified state mappings
03824   tcatType.map("Lepton","Cut based") ;
03825   tcatType.map("Kaon","Cut based") ;
03826 
03827   // Enter a wilcard expression mapping
03828   tcatType.map("NetTagger*","Neural Network") ;
03829 
03830   // Make a table of the mapped category state multiplicit in data
03831   Roo1DTable* mtable = data->table(tcatType) ;
03832 
03833 
03834 
03835   // C r e a t e   a   c a t   X   c a t   p r o d u c t   c a t e g o r y 
03836   // ----------------------------------------------------------------------
03837 
03838   // A SUPER-category is 'product' of _lvalue_ categories. The state names of a super
03839   // category is a composite of the state labels of the input categories
03840   RooSuperCategory b0Xtcat("b0Xtcat","b0flav X tagCat",RooArgSet(b0flav,tagCat)) ;
03841 
03842   // Make a table of the product category state multiplicity in data
03843   Roo1DTable* stable = data->table(b0Xtcat) ;
03844 
03845   // Since the super category is an lvalue, assignment is explicitly possible
03846   b0Xtcat.setLabel("{B0bar;Lepton}") ;
03847 
03848 
03849 
03850   // A MULTI-category is a 'product' of any category (function). The state names of a super
03851   // category is a composite of the state labels of the input categories
03852   RooMultiCategory b0Xttype("b0Xttype","b0flav X tagType",RooArgSet(b0flav,tcatType)) ;
03853   
03854   // Make a table of the product category state multiplicity in data
03855   Roo1DTable* xtable = data->table(b0Xttype) ;
03856 
03857   regTable(mtable,"rf406_mtable") ;
03858   regTable(stable,"rf406_stable") ;
03859   regTable(xtable,"rf406_xtable") ;
03860 
03861   delete data ;
03862 
03863   return kTRUE ;
03864   }
03865 
03866 } ;
03867 //////////////////////////////////////////////////////////////////////////
03868 //
03869 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #501
03870 // 
03871 // Using simultaneous p.d.f.s to describe simultaneous fits to multiple
03872 // datasets
03873 //
03874 //
03875 //
03876 // 07/2008 - Wouter Verkerke 
03877 // 
03878 /////////////////////////////////////////////////////////////////////////
03879 
03880 #ifndef __CINT__
03881 #include "RooGlobalFunc.h"
03882 #endif
03883 #include "RooRealVar.h"
03884 #include "RooDataSet.h"
03885 #include "RooGaussian.h"
03886 #include "RooChebychev.h"
03887 #include "RooAddPdf.h"
03888 #include "RooSimultaneous.h"
03889 #include "RooCategory.h"
03890 #include "TCanvas.h"
03891 #include "RooPlot.h"
03892 using namespace RooFit ;
03893 
03894 
03895 class TestBasic501 : public RooFitTestUnit
03896 {
03897 public: 
03898   TestBasic501(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Simultaneous p.d.f. operator",refFile,writeRef,verbose) {} ;
03899   Bool_t testCode() {
03900 
03901   // C r e a t e   m o d e l   f o r   p h y s i c s   s a m p l e
03902   // -------------------------------------------------------------
03903 
03904   // Create observables
03905   RooRealVar x("x","x",-8,8) ;
03906 
03907   // Construct signal pdf
03908   RooRealVar mean("mean","mean",0,-8,8) ;
03909   RooRealVar sigma("sigma","sigma",0.3,0.1,10) ;
03910   RooGaussian gx("gx","gx",x,mean,sigma) ;
03911 
03912   // Construct background pdf
03913   RooRealVar a0("a0","a0",-0.1,-1,1) ;
03914   RooRealVar a1("a1","a1",0.004,-1,1) ;
03915   RooChebychev px("px","px",x,RooArgSet(a0,a1)) ;
03916 
03917   // Construct composite pdf
03918   RooRealVar f("f","f",0.2,0.,1.) ;
03919   RooAddPdf model("model","model",RooArgList(gx,px),f) ;
03920 
03921 
03922 
03923   // C r e a t e   m o d e l   f o r   c o n t r o l   s a m p l e
03924   // --------------------------------------------------------------
03925 
03926   // Construct signal pdf. 
03927   // NOTE that sigma is shared with the signal sample model
03928   RooRealVar mean_ctl("mean_ctl","mean_ctl",-3,-8,8) ;
03929   RooGaussian gx_ctl("gx_ctl","gx_ctl",x,mean_ctl,sigma) ;
03930 
03931   // Construct the background pdf
03932   RooRealVar a0_ctl("a0_ctl","a0_ctl",-0.1,-1,1) ;
03933   RooRealVar a1_ctl("a1_ctl","a1_ctl",0.5,-0.1,1) ;
03934   RooChebychev px_ctl("px_ctl","px_ctl",x,RooArgSet(a0_ctl,a1_ctl)) ;
03935 
03936   // Construct the composite model
03937   RooRealVar f_ctl("f_ctl","f_ctl",0.5,0.,1.) ;
03938   RooAddPdf model_ctl("model_ctl","model_ctl",RooArgList(gx_ctl,px_ctl),f_ctl) ;
03939 
03940 
03941 
03942   // G e n e r a t e   e v e n t s   f o r   b o t h   s a m p l e s 
03943   // ---------------------------------------------------------------
03944 
03945   // Generate 1000 events in x and y from model
03946   RooDataSet *data = model.generate(RooArgSet(x),100) ;
03947   RooDataSet *data_ctl = model_ctl.generate(RooArgSet(x),2000) ;
03948 
03949 
03950 
03951   // C r e a t e   i n d e x   c a t e g o r y   a n d   j o i n   s a m p l e s 
03952   // ---------------------------------------------------------------------------
03953 
03954   // Define category to distinguish physics and control samples events
03955   RooCategory sample("sample","sample") ;
03956   sample.defineType("physics") ;
03957   sample.defineType("control") ;
03958 
03959   // Construct combined dataset in (x,sample)
03960   RooDataSet combData("combData","combined data",x,Index(sample),Import("physics",*data),Import("control",*data_ctl)) ;
03961 
03962 
03963 
03964   // C o n s t r u c t   a   s i m u l t a n e o u s   p d f   i n   ( x , s a m p l e )
03965   // -----------------------------------------------------------------------------------
03966 
03967   // Construct a simultaneous pdf using category sample as index
03968   RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
03969 
03970   // Associate model with the physics state and model_ctl with the control state
03971   simPdf.addPdf(model,"physics") ;
03972   simPdf.addPdf(model_ctl,"control") ;
03973 
03974 
03975 
03976   // P e r f o r m   a   s i m u l t a n e o u s   f i t
03977   // ---------------------------------------------------
03978 
03979   // Perform simultaneous fit of model to data and model_ctl to data_ctl
03980   simPdf.fitTo(combData) ;
03981 
03982 
03983 
03984   // P l o t   m o d e l   s l i c e s   o n   d a t a    s l i c e s 
03985   // ----------------------------------------------------------------
03986 
03987   // Make a frame for the physics sample
03988   RooPlot* frame1 = x.frame(Bins(30),Title("Physics sample")) ;
03989 
03990   // Plot all data tagged as physics sample
03991   combData.plotOn(frame1,Cut("sample==sample::physics")) ;
03992 
03993   // Plot "physics" slice of simultaneous pdf. 
03994   // NBL You _must_ project the sample index category with data using ProjWData 
03995   // as a RooSimultaneous makes no prediction on the shape in the index category 
03996   // and can thus not be integrated
03997   simPdf.plotOn(frame1,Slice(sample,"physics"),ProjWData(sample,combData)) ;
03998   simPdf.plotOn(frame1,Slice(sample,"physics"),Components("px"),ProjWData(sample,combData),LineStyle(kDashed)) ;
03999 
04000   // The same plot for the control sample slice
04001   RooPlot* frame2 = x.frame(Bins(30),Title("Control sample")) ;
04002   combData.plotOn(frame2,Cut("sample==sample::control")) ;
04003   simPdf.plotOn(frame2,Slice(sample,"control"),ProjWData(sample,combData)) ;
04004   simPdf.plotOn(frame2,Slice(sample,"control"),Components("px_ctl"),ProjWData(sample,combData),LineStyle(kDashed)) ;
04005 
04006 
04007   regPlot(frame1,"rf501_plot1") ;
04008   regPlot(frame2,"rf501_plot2") ;
04009 
04010   delete data ;
04011   delete data_ctl ;
04012 
04013   return kTRUE ;
04014 
04015   }
04016 
04017 } ;
04018 //////////////////////////////////////////////////////////////////////////
04019 //
04020 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #501
04021 // 
04022 // Using simultaneous p.d.f.s to describe simultaneous fits to multiple
04023 // datasets
04024 //
04025 //
04026 //
04027 // 07/2008 - Wouter Verkerke 
04028 // 
04029 /////////////////////////////////////////////////////////////////////////
04030 
04031 #ifndef __CINT__
04032 #include "RooGlobalFunc.h"
04033 #endif
04034 #include "RooWorkspace.h"
04035 #include "RooProdPdf.h"
04036 #include "RooRealVar.h"
04037 #include "RooDataSet.h"
04038 #include "RooGaussian.h"
04039 #include "RooGaussModel.h"
04040 #include "RooAddModel.h"
04041 #include "RooDecay.h"
04042 #include "RooChebychev.h"
04043 #include "RooAddPdf.h"
04044 #include "RooSimultaneous.h"
04045 #include "RooCategory.h"
04046 #include "TCanvas.h"
04047 #include "RooPlot.h"
04048 using namespace RooFit ;
04049 
04050 
04051 class TestBasic599 : public RooFitTestUnit
04052 {
04053 public: 
04054   TestBasic599(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Workspace and p.d.f. persistence",refFile,writeRef,verbose) {} ;
04055   Bool_t testCode() {
04056 
04057     if (_write) {
04058             
04059       RooWorkspace *w = new RooWorkspace("TestBasic11_ws") ;
04060 
04061       regWS(w,"Basic11_ws") ;
04062 
04063       // Build Gaussian PDF in X
04064       RooRealVar x("x","x",-10,10) ;
04065       RooRealVar meanx("meanx","mean of gaussian",-1) ;
04066       RooRealVar sigmax("sigmax","width of gaussian",3) ;
04067       RooGaussian gaussx("gaussx","gaussian PDF",x,meanx,sigmax) ;  
04068 
04069       // Build Gaussian PDF in Y
04070       RooRealVar y("y","y",-10,10) ;
04071       RooRealVar meany("meany","mean of gaussian",-1) ;
04072       RooRealVar sigmay("sigmay","width of gaussian",3) ;
04073       RooGaussian gaussy("gaussy","gaussian PDF",y,meany,sigmay) ;  
04074 
04075       // Make product of X and Y
04076       RooProdPdf gaussxy("gaussxy","gaussx*gaussy",RooArgSet(gaussx,gaussy)) ;
04077 
04078       // Make flat bkg in X and Y
04079       RooPolynomial flatx("flatx","flatx",x) ;
04080       RooPolynomial flaty("flaty","flaty",x) ;
04081       RooProdPdf flatxy("flatxy","flatx*flaty",RooArgSet(flatx,flaty)) ;
04082 
04083       // Make sum of gaussxy and flatxy
04084       RooRealVar frac("frac","frac",0.5,0.,1.) ;
04085       RooAddPdf sumxy("sumxy","sumxy",RooArgList(gaussxy,flatxy),frac) ;
04086 
04087       // Store p.d.f in workspace
04088       w->import(gaussx) ;
04089       w->import(gaussxy,RenameConflictNodes("set2")) ;
04090       w->import(sumxy,RenameConflictNodes("set3")) ;
04091 
04092       // Make reference plot of GaussX
04093       RooPlot* frame1 = x.frame() ;
04094       gaussx.plotOn(frame1) ;
04095       regPlot(frame1,"Basic11_gaussx_framex") ;
04096       
04097       // Make reference plots for GaussXY
04098       RooPlot* frame2 = x.frame() ;
04099       gaussxy.plotOn(frame2) ;
04100       regPlot(frame2,"Basic11_gaussxy_framex") ;
04101       
04102       RooPlot* frame3 = y.frame() ;
04103       gaussxy.plotOn(frame3) ;
04104       regPlot(frame3,"Basic11_gaussxy_framey") ;
04105       
04106       // Make reference plots for SumXY
04107       RooPlot* frame4 = x.frame() ;
04108       sumxy.plotOn(frame4) ;
04109       regPlot(frame4,"Basic11_sumxy_framex") ;
04110       
04111       RooPlot* frame5 = y.frame() ;
04112       sumxy.plotOn(frame5) ;
04113       regPlot(frame5,"Basic11_sumxy_framey") ;
04114 
04115       // Analytically convolved p.d.f.s
04116 
04117       // Build a simple decay PDF
04118       RooRealVar dt("dt","dt",-20,20) ;
04119       RooRealVar tau("tau","tau",1.548) ;
04120       
04121       // Build a gaussian resolution model
04122       RooRealVar bias1("bias1","bias1",0) ;
04123       RooRealVar sigma1("sigma1","sigma1",1) ;
04124       RooGaussModel gm1("gm1","gauss model 1",dt,bias1,sigma1) ;
04125       
04126       // Construct a decay PDF, smeared with single gaussian resolution model
04127       RooDecay decay_gm1("decay_gm1","decay",dt,tau,gm1,RooDecay::DoubleSided) ;
04128           
04129       // Build another gaussian resolution model
04130       RooRealVar bias2("bias2","bias2",0) ;
04131       RooRealVar sigma2("sigma2","sigma2",5) ;
04132       RooGaussModel gm2("gm2","gauss model 2",dt,bias2,sigma2) ;
04133       
04134       // Build a composite resolution model
04135       RooRealVar gm1frac("gm1frac","fraction of gm1",0.5) ;
04136       RooAddModel gmsum("gmsum","sum of gm1 and gm2",RooArgList(gm1,gm2),gm1frac) ;
04137     
04138       // Construct a decay PDF, smeared with double gaussian resolution model
04139       RooDecay decay_gmsum("decay_gmsum","decay",dt,tau,gmsum,RooDecay::DoubleSided) ;
04140 
04141       w->import(decay_gm1) ;
04142       w->import(decay_gmsum,RenameConflictNodes("set3")) ;
04143 
04144       RooPlot* frame6 = dt.frame() ;
04145       decay_gm1.plotOn(frame6) ;
04146       regPlot(frame6,"Basic11_decay_gm1_framedt") ;
04147     
04148       RooPlot* frame7 = dt.frame() ;
04149       decay_gmsum.plotOn(frame7) ;
04150       regPlot(frame7,"Basic11_decay_gmsum_framedt") ;
04151 
04152       // Construct simultaneous p.d.f
04153       RooCategory cat("cat","cat") ;
04154       cat.defineType("A") ;
04155       cat.defineType("B") ;
04156       RooSimultaneous sim("sim","sim",cat) ;
04157       sim.addPdf(gaussxy,"A") ;
04158       sim.addPdf(flatxy,"B") ;
04159 
04160       w->import(sim,RenameConflictNodes("set4")) ;
04161 
04162       // Make plot with dummy dataset for index projection
04163       RooDataHist dh("dh","dh",cat) ;
04164       cat.setLabel("A") ;
04165       dh.add(cat) ;
04166       cat.setLabel("B") ;
04167       dh.add(cat) ;
04168       
04169       RooPlot* frame8 = x.frame() ;
04170       sim.plotOn(frame8,ProjWData(cat,dh),Project(cat)) ;
04171       
04172       regPlot(frame8,"Basic11_sim_framex") ;
04173       
04174     
04175     } else {
04176 
04177       RooWorkspace* w = getWS("Basic11_ws") ;
04178       if (!w) return kFALSE ;
04179 
04180       // Retrieve p.d.f from workspace
04181       RooAbsPdf* gaussx = w->pdf("gaussx") ;
04182 
04183       // Make test plot and offer for comparison against ref plot
04184       RooPlot* frame1 = w->var("x")->frame() ;
04185       gaussx->plotOn(frame1) ;
04186       regPlot(frame1,"Basic11_gaussx_framex") ;
04187       
04188       // Retrieve p.d.f from workspace
04189       RooAbsPdf* gaussxy = w->pdf("gaussxy") ;
04190 
04191       // Make test plot and offer for comparison against ref plot
04192       RooPlot* frame2 = w->var("x")->frame() ;
04193       gaussxy->plotOn(frame2) ;
04194       regPlot(frame2,"Basic11_gaussxy_framex") ;
04195 
04196       // Make test plot and offer for comparison against ref plot
04197       RooPlot* frame3 = w->var("y")->frame() ;
04198       gaussxy->plotOn(frame3) ;
04199       regPlot(frame3,"Basic11_gaussxy_framey") ;
04200 
04201       // Retrieve p.d.f from workspace
04202       RooAbsPdf* sumxy = w->pdf("sumxy") ;
04203 
04204       // Make test plot and offer for comparison against ref plot
04205       RooPlot* frame4 = w->var("x")->frame() ;
04206       sumxy->plotOn(frame4) ;
04207       regPlot(frame4,"Basic11_sumxy_framex") ;
04208 
04209       // Make test plot and offer for comparison against ref plot
04210       RooPlot* frame5 = w->var("y")->frame() ;
04211       sumxy->plotOn(frame5) ;
04212       regPlot(frame5,"Basic11_sumxy_framey") ;
04213 
04214       // Retrieve p.d.f from workspace
04215       RooAbsPdf* decay_gm1 = w->pdf("decay_gm1") ;
04216 
04217       // Make test plot and offer for comparison against ref plot
04218       RooPlot* frame6 = w->var("dt")->frame() ;
04219       decay_gm1->plotOn(frame6) ;
04220       regPlot(frame6,"Basic11_decay_gm1_framedt") ;
04221 
04222       // Retrieve p.d.f from workspace
04223       RooAbsPdf* decay_gmsum = w->pdf("decay_gmsum") ;
04224 
04225       // Make test plot and offer for comparison against ref plot
04226       RooPlot* frame7 = w->var("dt")->frame() ;
04227       decay_gmsum->plotOn(frame7) ;
04228       regPlot(frame7,"Basic11_decay_gmsum_framedt") ;
04229 
04230       // Retrieve p.d.f. from workspace
04231       RooAbsPdf* sim = w->pdf("sim") ;
04232       RooCategory* cat = w->cat("cat") ;
04233 
04234       // Make plot with dummy dataset for index projection
04235       RooPlot* frame8 = w->var("x")->frame() ;
04236 
04237       RooDataHist dh("dh","dh",*cat) ;
04238       cat->setLabel("A") ;
04239       dh.add(*cat) ;
04240       cat->setLabel("B") ;
04241       dh.add(*cat) ;
04242       
04243       sim->plotOn(frame8,ProjWData(*cat,dh),Project(*cat)) ;
04244 
04245       regPlot(frame8,"Basic11_sim_framex") ;
04246       
04247     }
04248 
04249     // "Workspace persistence"
04250     return kTRUE ;
04251   }
04252 } ;
04253 /////////////////////////////////////////////////////////////////////////
04254 //
04255 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #601
04256 // 
04257 // Interactive minimization with MINUIT
04258 //
04259 //
04260 // 07/2008 - Wouter Verkerke 
04261 //
04262 /////////////////////////////////////////////////////////////////////////
04263 
04264 #ifndef __CINT__
04265 #include "RooGlobalFunc.h"
04266 #endif
04267 #include "RooRealVar.h"
04268 #include "RooDataSet.h"
04269 #include "RooGaussian.h"
04270 #include "RooProdPdf.h"
04271 #include "RooAddPdf.h"
04272 #include "RooMinuit.h"
04273 #include "RooNLLVar.h"
04274 #include "RooFitResult.h"
04275 #include "RooPlot.h"
04276 #include "TCanvas.h"
04277 #include "TH1.h"
04278 using namespace RooFit ;
04279 
04280 
04281 class TestBasic601 : public RooFitTestUnit
04282 {
04283 public: 
04284   TestBasic601(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Interactive Minuit",refFile,writeRef,verbose) {} ;
04285   Bool_t testCode() {
04286 
04287   // S e t u p   p d f   a n d   l i k e l i h o o d 
04288   // -----------------------------------------------
04289 
04290   // Observable
04291   RooRealVar x("x","x",-20,20) ;
04292 
04293   // Model (intentional strong correlations)
04294   RooRealVar mean("mean","mean of g1 and g2",0) ;
04295   RooRealVar sigma_g1("sigma_g1","width of g1",3) ;
04296   RooGaussian g1("g1","g1",x,mean,sigma_g1) ;
04297 
04298   RooRealVar sigma_g2("sigma_g2","width of g2",4,3.0,6.0) ;
04299   RooGaussian g2("g2","g2",x,mean,sigma_g2) ;
04300 
04301   RooRealVar frac("frac","frac",0.5,0.0,1.0) ;
04302   RooAddPdf model("model","model",RooArgList(g1,g2),frac) ;
04303 
04304   // Generate 1000 events
04305   RooDataSet* data = model.generate(x,1000) ;
04306   
04307   // Construct unbinned likelihood
04308   RooNLLVar nll("nll","nll",model,*data) ;
04309 
04310 
04311   // I n t e r a c t i v e   m i n i m i z a t i o n ,   e r r o r   a n a l y s i s
04312   // -------------------------------------------------------------------------------
04313 
04314   // Create MINUIT interface object
04315   RooMinuit m(nll) ;
04316 
04317   // Call MIGRAD to minimize the likelihood
04318   m.migrad() ;
04319 
04320   // Run HESSE to calculate errors from d2L/dp2
04321   m.hesse() ;
04322 
04323   // Run MINOS on sigma_g2 parameter only
04324   m.minos(sigma_g2) ;
04325 
04326 
04327   // S a v i n g   r e s u l t s ,   c o n t o u r   p l o t s 
04328   // ---------------------------------------------------------
04329 
04330   // Save a snapshot of the fit result. This object contains the initial
04331   // fit parameters, the final fit parameters, the complete correlation
04332   // matrix, the EDM, the minimized FCN , the last MINUIT status code and
04333   // the number of times the RooFit function object has indicated evaluation
04334   // problems (e.g. zero probabilities during likelihood evaluation)
04335   RooFitResult* r = m.save() ;
04336 
04337 
04338   // C h a n g e   p a r a m e t e r   v a l u e s ,   f l o a t i n g
04339   // -----------------------------------------------------------------
04340 
04341   // At any moment you can manually change the value of a (constant)
04342   // parameter
04343   mean = 0.3 ;
04344   
04345   // Rerun MIGRAD,HESSE
04346   m.migrad() ;
04347   m.hesse() ;
04348 
04349   // Now fix sigma_g2
04350   sigma_g2.setConstant(kTRUE) ;
04351 
04352   // Rerun MIGRAD,HESSE
04353   m.migrad() ;
04354   m.hesse() ;
04355 
04356   RooFitResult* r2 = m.save() ;
04357 
04358   regResult(r,"rf601_r") ;
04359   regResult(r2,"rf601_r2") ;
04360 
04361   delete data ;
04362 
04363   return kTRUE ;
04364   } 
04365 } ;
04366 //////////////////////////////////////////////////////////////////////////
04367 //
04368 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #602
04369 // 
04370 // Setting up a binning chi^2 fit
04371 //
04372 //
04373 //
04374 // 07/2008 - Wouter Verkerke 
04375 // 
04376 /////////////////////////////////////////////////////////////////////////
04377 
04378 #ifndef __CINT__
04379 #include "RooGlobalFunc.h"
04380 #endif
04381 #include "RooRealVar.h"
04382 #include "RooDataSet.h"
04383 #include "RooGaussian.h"
04384 #include "RooChebychev.h"
04385 #include "RooAddPdf.h"
04386 #include "RooChi2Var.h"
04387 #include "RooMinuit.h"
04388 #include "TCanvas.h"
04389 #include "RooPlot.h"
04390 using namespace RooFit ;
04391 
04392 
04393 class TestBasic602 : public RooFitTestUnit
04394 {
04395 public: 
04396   TestBasic602(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Chi2 minimization",refFile,writeRef,verbose) {} ;
04397   Bool_t testCode() {
04398 
04399   // S e t u p   m o d e l
04400   // ---------------------
04401 
04402   // Declare observable x
04403   RooRealVar x("x","x",0,10) ;
04404 
04405   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
04406   RooRealVar mean("mean","mean of gaussians",5) ;
04407   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
04408   RooRealVar sigma2("sigma2","width of gaussians",1) ;
04409 
04410   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
04411   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
04412   
04413   // Build Chebychev polynomial p.d.f.  
04414   RooRealVar a0("a0","a0",0.5,0.,1.) ;
04415   RooRealVar a1("a1","a1",-0.2,0.,1.) ;
04416   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
04417 
04418   // Sum the signal components into a composite signal p.d.f.
04419   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
04420   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
04421 
04422   // Sum the composite signal and background 
04423   RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
04424   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
04425 
04426 
04427   // C r e a t e   b i n n e d   d a t a s e t
04428   // -----------------------------------------
04429 
04430   RooDataSet* d = model.generate(x,10000) ;
04431   RooDataHist* dh = d->binnedClone() ;
04432 
04433 
04434   // Construct a chi^2 of the data and the model,
04435   // which is the input probability density scaled
04436   // by the number of events in the dataset
04437   RooChi2Var chi2("chi2","chi2",model,*dh) ;
04438 
04439   // Use RooMinuit interface to minimize chi^2
04440   RooMinuit m(chi2) ;
04441   m.migrad() ;
04442   m.hesse() ;
04443 
04444   RooFitResult* r = m.save() ;
04445 
04446   regResult(r,"rf602_r") ;
04447   
04448   delete d ;
04449   delete dh ;
04450 
04451   return kTRUE ;
04452   }
04453 } ;
04454 /////////////////////////////////////////////////////////////////////////
04455 //
04456 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #604
04457 // 
04458 // Fitting with constraints
04459 //
04460 //
04461 // 07/2008 - Wouter Verkerke 
04462 //
04463 /////////////////////////////////////////////////////////////////////////
04464 
04465 #ifndef __CINT__
04466 #include "RooGlobalFunc.h"
04467 #endif
04468 #include "RooRealVar.h"
04469 #include "RooDataSet.h"
04470 #include "RooGaussian.h"
04471 #include "RooPolynomial.h"
04472 #include "RooAddPdf.h"
04473 #include "RooProdPdf.h"
04474 #include "RooFitResult.h"
04475 #include "RooPlot.h"
04476 #include "TCanvas.h"
04477 #include "TH1.h"
04478 using namespace RooFit ;
04479 
04480 
04481 class TestBasic604 : public RooFitTestUnit
04482 {
04483 public: 
04484   TestBasic604(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Auxiliary observable constraints",refFile,writeRef,verbose) {} ;
04485   Bool_t testCode() {
04486 
04487   // C r e a t e   m o d e l  a n d   d a t a s e t 
04488   // ----------------------------------------------
04489 
04490   // Construct a Gaussian p.d.f
04491   RooRealVar x("x","x",-10,10) ;
04492 
04493   RooRealVar m("m","m",0,-10,10) ;
04494   RooRealVar s("s","s",2,0.1,10) ;
04495   RooGaussian gauss("gauss","gauss(x,m,s)",x,m,s) ;
04496 
04497   // Construct a flat p.d.f (polynomial of 0th order)
04498   RooPolynomial poly("poly","poly(x)",x) ;
04499 
04500   // Construct model = f*gauss + (1-f)*poly
04501   RooRealVar f("f","f",0.5,0.,1.) ;
04502   RooAddPdf model("model","model",RooArgSet(gauss,poly),f) ;
04503 
04504   // Generate small dataset for use in fitting below
04505   RooDataSet* d = model.generate(x,50) ;
04506 
04507 
04508 
04509   // C r e a t e   c o n s t r a i n t   p d f 
04510   // -----------------------------------------
04511 
04512   // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
04513   RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.8),RooConst(0.1)) ;
04514 
04515 
04516 
04517   // M E T H O D   1   -   A d d   i n t e r n a l   c o n s t r a i n t   t o   m o d e l 
04518   // -------------------------------------------------------------------------------------
04519 
04520   // Multiply constraint term with regular p.d.f using RooProdPdf
04521   // Specify in fitTo() that internal constraints on parameter f should be used
04522 
04523   // Multiply constraint with p.d.f
04524   RooProdPdf modelc("modelc","model with constraint",RooArgSet(model,fconstraint)) ;
04525   
04526   // Fit modelc without use of constraint term
04527   RooFitResult* r1 = modelc.fitTo(*d,Save()) ;
04528 
04529   // Fit modelc with constraint term on parameter f
04530   RooFitResult* r2 = modelc.fitTo(*d,Constrain(f),Save()) ;
04531 
04532 
04533 
04534   // M E T H O D   2   -     S p e c i f y   e x t e r n a l   c o n s t r a i n t   w h e n   f i t t i n g
04535   // -------------------------------------------------------------------------------------------------------
04536 
04537   // Construct another Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
04538   RooGaussian fconstext("fconstext","fconstext",f,RooConst(0.2),RooConst(0.1)) ;
04539 
04540   // Fit with external constraint
04541   RooFitResult* r3 = model.fitTo(*d,ExternalConstraints(fconstext),Save()) ;
04542 
04543 
04544   regResult(r1,"rf604_r1") ;
04545   regResult(r2,"rf604_r2") ;
04546   regResult(r3,"rf604_r3") ;
04547 
04548   delete d ;
04549   
04550   return kTRUE ;
04551   }
04552 } ;
04553 //////////////////////////////////////////////////////////////////////////
04554 //
04555 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #605
04556 // 
04557 // Working with the profile likelihood estimator
04558 //
04559 //
04560 //
04561 // 07/2008 - Wouter Verkerke 
04562 // 
04563 /////////////////////////////////////////////////////////////////////////
04564 
04565 #ifndef __CINT__
04566 #include "RooGlobalFunc.h"
04567 #endif
04568 #include "RooRealVar.h"
04569 #include "RooDataSet.h"
04570 #include "RooGaussian.h"
04571 #include "RooAddPdf.h"
04572 #include "RooNLLVar.h"
04573 #include "RooProfileLL.h"
04574 #include "RooMinuit.h"
04575 #include "TCanvas.h"
04576 #include "RooPlot.h"
04577 using namespace RooFit ;
04578 
04579 
04580 class TestBasic605 : public RooFitTestUnit
04581 {
04582 public: 
04583   TestBasic605(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Profile Likelihood operator",refFile,writeRef,verbose) {} ;
04584   Bool_t testCode() {
04585 
04586   // C r e a t e   m o d e l   a n d   d a t a s e t 
04587   // -----------------------------------------------
04588 
04589   // Observable
04590   RooRealVar x("x","x",-20,20) ;
04591 
04592   // Model (intentional strong correlations)
04593   RooRealVar mean("mean","mean of g1 and g2",0,-10,10) ;
04594   RooRealVar sigma_g1("sigma_g1","width of g1",3) ;
04595   RooGaussian g1("g1","g1",x,mean,sigma_g1) ;
04596 
04597   RooRealVar sigma_g2("sigma_g2","width of g2",4,3.0,6.0) ;
04598   RooGaussian g2("g2","g2",x,mean,sigma_g2) ;
04599 
04600   RooRealVar frac("frac","frac",0.5,0.0,1.0) ;
04601   RooAddPdf model("model","model",RooArgList(g1,g2),frac) ;
04602 
04603   // Generate 1000 events
04604   RooDataSet* data = model.generate(x,1000) ;
04605   
04606 
04607 
04608   // C o n s t r u c t   p l a i n   l i k e l i h o o d
04609   // ---------------------------------------------------
04610 
04611   // Construct unbinned likelihood
04612   RooNLLVar nll("nll","nll",model,*data) ;
04613 
04614   // Minimize likelihood w.r.t all parameters before making plots
04615   RooMinuit(nll).migrad() ;
04616 
04617   // Plot likelihood scan frac 
04618   RooPlot* frame1 = frac.frame(Bins(10),Range(0.01,0.95),Title("LL and profileLL in frac")) ;
04619   nll.plotOn(frame1,ShiftToZero()) ;
04620 
04621   // Plot likelihood scan in sigma_g2
04622   RooPlot* frame2 = sigma_g2.frame(Bins(10),Range(3.3,5.0),Title("LL and profileLL in sigma_g2")) ;
04623   nll.plotOn(frame2,ShiftToZero()) ;
04624 
04625 
04626 
04627   // C o n s t r u c t   p r o f i l e   l i k e l i h o o d   i n   f r a c
04628   // -----------------------------------------------------------------------
04629 
04630   // The profile likelihood estimator on nll for frac will minimize nll w.r.t
04631   // all floating parameters except frac for each evaluation
04632   RooProfileLL pll_frac("pll_frac","pll_frac",nll,frac) ;
04633 
04634   // Plot the profile likelihood in frac
04635   pll_frac.plotOn(frame1,LineColor(kRed)) ;
04636 
04637   // Adjust frame maximum for visual clarity
04638   frame1->SetMinimum(0) ;
04639   frame1->SetMaximum(3) ;
04640 
04641 
04642 
04643   // C o n s t r u c t   p r o f i l e   l i k e l i h o o d   i n   s i g m a _ g 2 
04644   // -------------------------------------------------------------------------------
04645 
04646   // The profile likelihood estimator on nll for sigma_g2 will minimize nll
04647   // w.r.t all floating parameters except sigma_g2 for each evaluation
04648   RooProfileLL pll_sigmag2("pll_sigmag2","pll_sigmag2",nll,sigma_g2) ;
04649 
04650   // Plot the profile likelihood in sigma_g2
04651   pll_sigmag2.plotOn(frame2,LineColor(kRed)) ;
04652 
04653   // Adjust frame maximum for visual clarity
04654   frame2->SetMinimum(0) ;
04655   frame2->SetMaximum(3) ;
04656 
04657 
04658   regPlot(frame1,"rf605_plot1") ;
04659   regPlot(frame2,"rf605_plot2") ;
04660 
04661   delete data ;
04662 
04663   return kTRUE ;
04664   }
04665 } ;
04666 
04667 //////////////////////////////////////////////////////////////////////////
04668 //
04669 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #606
04670 // 
04671 // Understanding and customizing error handling in likelihood evaluations
04672 //
04673 //
04674 //
04675 // 07/2008 - Wouter Verkerke 
04676 // 
04677 /////////////////////////////////////////////////////////////////////////
04678 
04679 #ifndef __CINT__
04680 #include "RooGlobalFunc.h"
04681 #endif
04682 #include "RooRealVar.h"
04683 #include "RooDataSet.h"
04684 #include "RooArgusBG.h"
04685 #include "RooNLLVar.h"
04686 #include "TCanvas.h"
04687 #include "RooPlot.h"
04688 using namespace RooFit ;
04689 
04690 
04691 class TestBasic606 : public RooFitTestUnit
04692 {
04693 public: 
04694   TestBasic606(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("NLL error handling",refFile,writeRef,verbose) {} ;
04695   Bool_t testCode() {
04696 
04697   // C r e a t e   m o d e l  a n d   d a t a s e t 
04698   // ----------------------------------------------
04699 
04700   // Observable
04701   RooRealVar m("m","m",5.20,5.30) ;
04702 
04703   // Parameters
04704   RooRealVar m0("m0","m0",5.291,5.20,5.30) ;
04705   RooRealVar k("k","k",-30,-50,-10) ;
04706 
04707   // Pdf
04708   RooArgusBG argus("argus","argus",m,m0,k) ;
04709 
04710   // Sample 1000 events in m from argus
04711   RooDataSet* data = argus.generate(m,1000) ;
04712 
04713 
04714 
04715   // P l o t   m o d e l   a n d   d a t a 
04716   // --------------------------------------
04717 
04718   RooPlot* frame1 = m.frame(Bins(40),Title("Argus model and data")) ;
04719   data->plotOn(frame1) ;
04720   argus.plotOn(frame1) ;
04721 
04722 
04723 
04724   // F i t   m o d e l   t o   d a t a 
04725   // ---------------------------------
04726 
04727   argus.fitTo(*data,PrintEvalErrors(10),Warnings(kFALSE)) ;    
04728   m0.setError(0.1) ;
04729   argus.fitTo(*data,PrintEvalErrors(0),EvalErrorWall(kFALSE),Warnings(kFALSE)) ;    
04730 
04731 
04732 
04733   // P l o t   l i k e l i h o o d   a s   f u n c t i o n   o f   m 0 
04734   // ------------------------------------------------------------------
04735 
04736   // Construct likelihood function of model and data
04737   RooNLLVar nll("nll","nll",argus,*data) ;
04738 
04739   // Plot likelihood in m0 in range that includes problematic values
04740   // In this configuration the number of errors per likelihood point 
04741   // evaluated for the curve is shown. A positive number in PrintEvalErrors(N)
04742   // will show details for up to N events. By default the values for likelihood
04743   // evaluations with errors are shown normally (unlike fitting), but the shape
04744   // of the curve can be erratic in these regions.
04745 
04746   RooPlot* frame2 = m0.frame(Range(5.288,5.293),Title("-log(L) scan vs m0")) ;
04747   nll.plotOn(frame2,PrintEvalErrors(0),ShiftToZero(),LineColor(kRed),Precision(1e-4)) ; 
04748 
04749 
04750   // Plot likelihood in m0 in range that includes problematic values
04751   // In this configuration no messages are printed for likelihood evaluation errors,
04752   // but if an likelihood value evaluates with error, the corresponding value
04753   // on the curve will be set to the value given in EvalErrorValue().
04754 
04755   RooPlot* frame3 = m0.frame(Range(5.288,5.293),Title("-log(L) scan vs m0, problematic regions masked")) ;
04756   nll.plotOn(frame3,PrintEvalErrors(-1),ShiftToZero(),EvalErrorValue(nll.getVal()+10),LineColor(kRed)) ; 
04757 
04758 
04759   regPlot(frame1,"rf606_plot1") ;
04760   regPlot(frame2,"rf606_plot2") ;
04761   regPlot(frame3,"rf606_plot3") ;
04762 
04763   delete data ;
04764   return kTRUE ;
04765 
04766   }
04767 } ;
04768 //////////////////////////////////////////////////////////////////////////
04769 //
04770 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #607
04771 // 
04772 // Demonstration of options of the RooFitResult class
04773 //
04774 //
04775 //
04776 // 07/2008 - Wouter Verkerke 
04777 // 
04778 /////////////////////////////////////////////////////////////////////////
04779 
04780 #ifndef __CINT__
04781 #include "RooGlobalFunc.h"
04782 #endif
04783 #include "RooRealVar.h"
04784 #include "RooDataSet.h"
04785 #include "RooGaussian.h"
04786 #include "RooAddPdf.h"
04787 #include "RooChebychev.h"
04788 #include "RooFitResult.h"
04789 #include "TCanvas.h"
04790 #include "RooPlot.h"
04791 #include "TFile.h"
04792 #include "TStyle.h"
04793 #include "TH2.h"
04794 
04795 using namespace RooFit ;
04796 
04797 
04798 class TestBasic607 : public RooFitTestUnit
04799 {
04800 public: 
04801   TestBasic607(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Fit Result functionality",refFile,writeRef,verbose) {} ;
04802   Bool_t testCode() {
04803 
04804   // C r e a t e   p d f ,   d a t a
04805   // --------------------------------
04806 
04807   // Declare observable x
04808   RooRealVar x("x","x",0,10) ;
04809 
04810   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
04811   RooRealVar mean("mean","mean of gaussians",5,-10,10) ;
04812   RooRealVar sigma1("sigma1","width of gaussians",0.5,0.1,10) ;
04813   RooRealVar sigma2("sigma2","width of gaussians",1,0.1,10) ;
04814 
04815   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
04816   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
04817   
04818   // Build Chebychev polynomial p.d.f.  
04819   RooRealVar a0("a0","a0",0.5,0.,1.) ;
04820   RooRealVar a1("a1","a1",-0.2) ;
04821   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
04822 
04823   // Sum the signal components into a composite signal p.d.f.
04824   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
04825   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
04826 
04827   // Sum the composite signal and background 
04828   RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
04829   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
04830 
04831   // Generate 1000 events
04832   RooDataSet* data = model.generate(x,1000) ;
04833 
04834 
04835 
04836   // F i t   p d f   t o   d a t a ,   s a v e   f i t r e s u l t 
04837   // -------------------------------------------------------------
04838 
04839   // Perform fit and save result
04840   RooFitResult* r = model.fitTo(*data,Save()) ;
04841 
04842 
04843   // V i s u a l i z e   c o r r e l a t i o n   m a t r i x
04844   // -------------------------------------------------------
04845 
04846   // Construct 2D color plot of correlation matrix
04847   gStyle->SetOptStat(0) ;
04848   gStyle->SetPalette(1) ;
04849   TH2* hcorr = r->correlationHist() ;
04850 
04851 
04852   // Sample dataset with parameter values according to distribution
04853   // of covariance matrix of fit result
04854   RooDataSet randPars("randPars","randPars",r->floatParsFinal()) ;
04855   for (Int_t i=0 ; i<10000 ; i++) {
04856     randPars.add(r->randomizePars()) ;    
04857   }
04858 
04859   // make histogram of 2D distribution in sigma1 vs sig1frac
04860   TH1* hhrand = randPars.createHistogram("hhrand",sigma1,Binning(35,0.25,0.65),YVar(sig1frac,Binning(35,0.3,1.1))) ;
04861 
04862   regTH(hcorr,"rf607_hcorr") ;
04863   regTH(hhrand,"rf607_hhand") ;
04864 
04865   delete data ;
04866   delete r ;
04867 
04868   return kTRUE ;
04869 
04870   }
04871 } ;
04872 
04873 
04874 //////////////////////////////////////////////////////////////////////////
04875 //
04876 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #609
04877 // 
04878 // Setting up a chi^2 fit to an unbinned dataset with X,Y,err(Y)
04879 // values (and optionally err(X) values)
04880 //
04881 //
04882 //
04883 // 07/2008 - Wouter Verkerke 
04884 // 
04885 /////////////////////////////////////////////////////////////////////////
04886 
04887 
04888 #ifndef __CINT__
04889 #include "RooGlobalFunc.h"
04890 #endif
04891 #include "RooRealVar.h"
04892 #include "RooDataSet.h"
04893 #include "RooPolyVar.h"
04894 #include "RooChi2Var.h"
04895 #include "RooMinuit.h"
04896 #include "TCanvas.h"
04897 #include "RooPlot.h"
04898 #include "TRandom.h"
04899 
04900 using namespace RooFit ;
04901 
04902 class TestBasic609 : public RooFitTestUnit
04903 {
04904 public: 
04905   TestBasic609(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Chi^2 fit to X-Y dataset",refFile,writeRef,verbose) {} ;
04906   Bool_t testCode() {
04907 
04908   // C r e a t e   d a t a s e t   w i t h   X   a n d   Y   v a l u e s
04909   // -------------------------------------------------------------------
04910 
04911   // Make weighted XY dataset with asymmetric errors stored
04912   // The StoreError() argument is essential as it makes
04913   // the dataset store the error in addition to the values
04914   // of the observables. If errors on one or more observables
04915   // are asymmetric, one can store the asymmetric error
04916   // using the StoreAsymError() argument
04917 
04918   RooRealVar x("x","x",-11,11) ;
04919   RooRealVar y("y","y",-10,200) ;
04920   RooDataSet dxy("dxy","dxy",RooArgSet(x,y),StoreError(RooArgSet(x,y))) ;
04921 
04922   // Fill an example dataset with X,err(X),Y,err(Y) values
04923   for (int i=0 ; i<=10 ; i++) {
04924 
04925     // Set X value and error
04926     x = -10 + 2*i;
04927     x.setError( i<5 ? 0.5/1. : 1.0/1. ) ;
04928     
04929     // Set Y value and error 
04930     y = x.getVal() * x.getVal() + 4*fabs(RooRandom::randomGenerator()->Gaus()) ;
04931     y.setError(sqrt(y.getVal())) ;
04932 
04933     dxy.add(RooArgSet(x,y)) ;    
04934   }
04935 
04936 
04937 
04938   // P e r f o r m   c h i 2   f i t   t o   X + / - d x   a n d   Y + / - d Y   v a l u e s
04939   // ---------------------------------------------------------------------------------------
04940 
04941   // Make fit function
04942   RooRealVar a("a","a",0.0,-10,10) ;
04943   RooRealVar b("b","b",0.0,-100,100) ;
04944   RooPolyVar f("f","f",x,RooArgList(b,a,RooConst(1))) ;
04945 
04946   // Plot dataset in X-Y interpretation
04947   RooPlot* frame = x.frame(Title("Chi^2 fit of function set of (X#pmdX,Y#pmdY) values")) ;
04948   dxy.plotOnXY(frame,YVar(y)) ;
04949 
04950   // Fit chi^2 using X and Y errors 
04951   f.chi2FitTo(dxy,YVar(y)) ;
04952 
04953   // Overlay fitted function
04954   f.plotOn(frame) ;
04955   
04956   // Alternative: fit chi^2 integrating f(x) over ranges defined by X errors, rather
04957   // than taking point at center of bin
04958   f.chi2FitTo(dxy,YVar(y),Integrate(kTRUE)) ;
04959 
04960   // Overlay alternate fit result
04961   f.plotOn(frame,LineStyle(kDashed),LineColor(kRed),Name("alternate")) ;  
04962 
04963   regPlot(frame,"rf609_frame") ;  
04964 
04965   return kTRUE ;
04966   }
04967 } ;
04968 
04969 
04970 
04971 //////////////////////////////////////////////////////////////////////////
04972 //
04973 // 'SPECIAL PDFS' RooFit tutorial macro #701
04974 // 
04975 // Unbinned maximum likelihood fit of an efficiency eff(x) function to 
04976 // a dataset D(x,cut), where cut is a category encoding a selection, of which
04977 // the efficiency as function of x should be described by eff(x)
04978 //
04979 // 07/2008 - Wouter Verkerke 
04980 //
04981 /////////////////////////////////////////////////////////////////////////
04982 
04983 #ifndef __CINT__
04984 #include "RooGlobalFunc.h"
04985 #endif
04986 #include "RooRealVar.h"
04987 #include "RooDataSet.h"
04988 #include "RooGaussian.h"
04989 #include "RooFormulaVar.h"
04990 #include "RooProdPdf.h"
04991 #include "RooEfficiency.h"
04992 #include "RooPolynomial.h"
04993 #include "RooCategory.h"
04994 #include "TCanvas.h"
04995 #include "RooPlot.h"
04996 using namespace RooFit ;
04997 
04998 
04999 // Elementary operations on a gaussian PDF
05000 class TestBasic701 : public RooFitTestUnit
05001 {
05002 public: 
05003   TestBasic701(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency operator p.d.f. 1D",refFile,writeRef,verbose) {} ;
05004   Bool_t testCode() {
05005 
05006   // C o n s t r u c t   e f f i c i e n c y   f u n c t i o n   e ( x ) 
05007   // -------------------------------------------------------------------
05008 
05009   // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
05010   RooRealVar x("x","x",-10,10) ;
05011 
05012   // Efficiency function eff(x;a,b) 
05013   RooRealVar a("a","a",0.4,0,1) ;
05014   RooRealVar b("b","b",5) ;
05015   RooRealVar c("c","c",-1,-10,10) ;
05016   RooFormulaVar effFunc("effFunc","(1-a)+a*cos((x-c)/b)",RooArgList(a,b,c,x)) ;
05017 
05018 
05019 
05020   // C o n s t r u c t   c o n d i t i o n a l    e f f i c i e n c y   p d f   E ( c u t | x ) 
05021   // ------------------------------------------------------------------------------------------
05022 
05023   // Acceptance state cut (1 or 0)
05024   RooCategory cut("cut","cutr") ;
05025   cut.defineType("accept",1) ;
05026   cut.defineType("reject",0) ;
05027 
05028   // Construct efficiency p.d.f eff(cut|x)
05029   RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
05030 
05031 
05032 
05033   // G e n e r a t e   d a t a   ( x ,   c u t )   f r o m   a   t o y   m o d e l 
05034   // -----------------------------------------------------------------------------
05035 
05036   // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x) 
05037   // (These are _only_ needed to generate some toy MC here to be used later)
05038   RooPolynomial shapePdf("shapePdf","shapePdf",x,RooConst(-0.095)) ;
05039   RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
05040 
05041   // Generate some toy data from model
05042   RooDataSet* data = model.generate(RooArgSet(x,cut),10000) ;
05043 
05044 
05045 
05046   // F i t   c o n d i t i o n a l   e f f i c i e n c y   p d f   t o   d a t a 
05047   // --------------------------------------------------------------------------
05048 
05049   // Fit conditional efficiency p.d.f to data
05050   effPdf.fitTo(*data,ConditionalObservables(x)) ;
05051 
05052 
05053 
05054   // P l o t   f i t t e d ,   d a t a   e f f i c i e n c y  
05055   // --------------------------------------------------------
05056 
05057   // Plot distribution of all events and accepted fraction of events on frame
05058   RooPlot* frame1 = x.frame(Bins(20),Title("Data (all, accepted)")) ;
05059   data->plotOn(frame1) ;
05060   data->plotOn(frame1,Cut("cut==cut::accept"),MarkerColor(kRed),LineColor(kRed)) ;
05061 
05062   // Plot accept/reject efficiency on data overlay fitted efficiency curve
05063   RooPlot* frame2 = x.frame(Bins(20),Title("Fitted efficiency")) ;
05064   data->plotOn(frame2,Efficiency(cut)) ; // needs ROOT version >= 5.21
05065   effFunc.plotOn(frame2,LineColor(kRed)) ;
05066 
05067 
05068   regPlot(frame1,"rf701_plot1") ;
05069   regPlot(frame2,"rf701_plot2") ;
05070 
05071   
05072   delete data ;
05073 
05074   return kTRUE ;  
05075   }
05076 } ;
05077 //////////////////////////////////////////////////////////////////////////
05078 //
05079 // 'SPECIAL PDFS' RooFit tutorial macro #702
05080 // 
05081 // Unbinned maximum likelihood fit of an efficiency eff(x) function to 
05082 // a dataset D(x,cut), where cut is a category encoding a selection whose 
05083 // efficiency as function of x should be described by eff(x)
05084 //
05085 //
05086 /////////////////////////////////////////////////////////////////////////
05087 
05088 #ifndef __CINT__
05089 #include "RooGlobalFunc.h"
05090 #endif
05091 #include "RooRealVar.h"
05092 #include "RooDataSet.h"
05093 #include "RooGaussian.h"
05094 #include "RooCategory.h"
05095 #include "RooEfficiency.h"
05096 #include "RooPolynomial.h"
05097 #include "RooProdPdf.h"
05098 #include "RooFormulaVar.h"
05099 #include "TCanvas.h"
05100 #include "TH1.h"
05101 #include "RooPlot.h"
05102 using namespace RooFit ;
05103 
05104 
05105 // Elementary operations on a gaussian PDF
05106 class TestBasic702 : public RooFitTestUnit
05107 {
05108 public: 
05109   TestBasic702(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency operator p.d.f. 2D",refFile,writeRef,verbose) {} ;
05110   Bool_t testCode() {
05111 
05112   Bool_t flat=kFALSE ;
05113 
05114   // C o n s t r u c t   e f f i c i e n c y   f u n c t i o n   e ( x , y ) 
05115   // -----------------------------------------------------------------------
05116 
05117   // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
05118   RooRealVar x("x","x",-10,10) ;
05119   RooRealVar y("y","y",-10,10) ;
05120 
05121   // Efficiency function eff(x;a,b) 
05122   RooRealVar ax("ax","ay",0.6,0,1) ;
05123   RooRealVar bx("bx","by",5) ;
05124   RooRealVar cx("cx","cy",-1,-10,10) ;
05125 
05126   RooRealVar ay("ay","ay",0.2,0,1) ;
05127   RooRealVar by("by","by",5) ;
05128   RooRealVar cy("cy","cy",-1,-10,10) ;
05129 
05130   RooFormulaVar effFunc("effFunc","((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))",RooArgList(ax,bx,cx,x,ay,by,cy,y)) ; 
05131 
05132   // Acceptance state cut (1 or 0)
05133   RooCategory cut("cut","cutr") ;
05134   cut.defineType("accept",1) ;
05135   cut.defineType("reject",0) ;
05136 
05137 
05138 
05139   // C o n s t r u c t   c o n d i t i o n a l    e f f i c i e n c y   p d f   E ( c u t | x , y ) 
05140   // ---------------------------------------------------------------------------------------------
05141 
05142   // Construct efficiency p.d.f eff(cut|x)
05143   RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
05144 
05145 
05146 
05147   // G e n e r a t e   d a t a   ( x , y , c u t )   f r o m   a   t o y   m o d e l 
05148   // -------------------------------------------------------------------------------
05149 
05150   // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x) 
05151   // (These are _only_ needed to generate some toy MC here to be used later)
05152   RooPolynomial shapePdfX("shapePdfX","shapePdfX",x,RooConst(flat?0:-0.095)) ;
05153   RooPolynomial shapePdfY("shapePdfY","shapePdfY",y,RooConst(flat?0:+0.095)) ;
05154   RooProdPdf shapePdf("shapePdf","shapePdf",RooArgSet(shapePdfX,shapePdfY)) ;
05155   RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
05156 
05157   // Generate some toy data from model
05158   RooDataSet* data = model.generate(RooArgSet(x,y,cut),10000) ;
05159 
05160 
05161 
05162   // F i t   c o n d i t i o n a l   e f f i c i e n c y   p d f   t o   d a t a 
05163   // --------------------------------------------------------------------------
05164 
05165   // Fit conditional efficiency p.d.f to data
05166   effPdf.fitTo(*data,ConditionalObservables(RooArgSet(x,y))) ;
05167 
05168 
05169 
05170   // P l o t   f i t t e d ,   d a t a   e f f i c i e n c y  
05171   // --------------------------------------------------------
05172 
05173   // Make 2D histograms of all data, selected data and efficiency function
05174   TH1* hh_data_all = data->createHistogram("hh_data_all",x,Binning(8),YVar(y,Binning(8))) ;
05175   TH1* hh_data_sel = data->createHistogram("hh_data_sel",x,Binning(8),YVar(y,Binning(8)),Cut("cut==cut::accept")) ;
05176   TH1* hh_eff      = effFunc.createHistogram("hh_eff",x,Binning(50),YVar(y,Binning(50))) ;
05177 
05178   // Some adjustsment for good visualization
05179   hh_data_all->SetMinimum(0) ;
05180   hh_data_sel->SetMinimum(0) ;
05181   hh_eff->SetMinimum(0) ;
05182   hh_eff->SetLineColor(kBlue) ;
05183 
05184   regTH(hh_data_all,"rf702_hh_data_all") ;
05185   regTH(hh_data_sel,"rf702_hh_data_sel") ;
05186   regTH(hh_eff,"rf702_hh_eff") ;
05187   
05188   delete data ;
05189 
05190   return kTRUE;
05191 
05192   }
05193 } ;
05194 //////////////////////////////////////////////////////////////////////////
05195 //
05196 // 'SPECIAL PDFS' RooFit tutorial macro #703
05197 // 
05198 // Using a product of an (acceptance) efficiency and a p.d.f as p.d.f.
05199 //
05200 //
05201 //
05202 // 07/2008 - Wouter Verkerke 
05203 // 
05204 /////////////////////////////////////////////////////////////////////////
05205 
05206 #ifndef __CINT__
05207 #include "RooGlobalFunc.h"
05208 #endif
05209 #include "RooRealVar.h"
05210 #include "RooDataSet.h"
05211 #include "RooGaussian.h"
05212 #include "RooExponential.h"
05213 #include "RooEffProd.h"
05214 #include "RooFormulaVar.h"
05215 #include "TCanvas.h"
05216 #include "RooPlot.h"
05217 using namespace RooFit ;
05218 
05219 
05220 // Elementary operations on a gaussian PDF
05221 class TestBasic703 : public RooFitTestUnit
05222 {
05223 public: 
05224   TestBasic703(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency product operator p.d.f",refFile,writeRef,verbose) {} ;
05225   Bool_t testCode() {
05226 
05227   // D e f i n e   o b s e r v a b l e s   a n d   d e c a y   p d f
05228   // ---------------------------------------------------------------
05229 
05230   // Declare observables
05231   RooRealVar t("t","t",0,5) ;
05232 
05233   // Make pdf
05234   RooRealVar tau("tau","tau",-1.54,-4,-0.1) ;
05235   RooExponential model("model","model",t,tau) ;
05236 
05237 
05238 
05239   // D e f i n e   e f f i c i e n c y   f u n c t i o n
05240   // ---------------------------------------------------
05241   
05242   // Use error function to simulate turn-on slope
05243   RooFormulaVar eff("eff","0.5*(TMath::Erf((t-1)/0.5)+1)",t) ;
05244 
05245 
05246 
05247   // D e f i n e   d e c a y   p d f   w i t h   e f f i c i e n c y 
05248   // ---------------------------------------------------------------
05249 
05250   // Multiply pdf(t) with efficiency in t
05251   RooEffProd modelEff("modelEff","model with efficiency",model,eff) ;
05252 
05253   
05254 
05255   // P l o t   e f f i c i e n c y ,   p d f  
05256   // ----------------------------------------
05257 
05258   RooPlot* frame1 = t.frame(Title("Efficiency")) ;
05259   eff.plotOn(frame1,LineColor(kRed)) ;
05260 
05261   RooPlot* frame2 = t.frame(Title("Pdf with and without efficiency")) ;
05262 
05263   model.plotOn(frame2,LineStyle(kDashed)) ;
05264   modelEff.plotOn(frame2) ;
05265 
05266 
05267 
05268   // G e n e r a t e   t o y   d a t a ,   f i t   m o d e l E f f   t o   d a t a
05269   // ------------------------------------------------------------------------------
05270 
05271   // Generate events. If the input pdf has an internal generator, the internal generator
05272   // is used and an accept/reject sampling on the efficiency is applied. 
05273   RooDataSet* data = modelEff.generate(t,10000) ;
05274 
05275   // Fit pdf. The normalization integral is calculated numerically. 
05276   modelEff.fitTo(*data) ;
05277 
05278   // Plot generated data and overlay fitted pdf
05279   RooPlot* frame3 = t.frame(Title("Fitted pdf with efficiency")) ;
05280   data->plotOn(frame3) ;
05281   modelEff.plotOn(frame3) ;
05282 
05283   
05284   regPlot(frame1,"rf703_plot1") ;
05285   regPlot(frame2,"rf703_plot2") ;
05286   regPlot(frame3,"rf703_plot3") ;
05287 
05288 
05289   delete data ;
05290   return kTRUE ;
05291   }
05292 } ;
05293 //////////////////////////////////////////////////////////////////////////
05294 //
05295 // 'SPECIAL PDFS' RooFit tutorial macro #704
05296 // 
05297 // Using a p.d.f defined by a sum of real-valued amplitude components
05298 //
05299 //
05300 //
05301 // 07/2008 - Wouter Verkerke 
05302 // 
05303 /////////////////////////////////////////////////////////////////////////
05304 
05305 #ifndef __CINT__
05306 #include "RooGlobalFunc.h"
05307 #endif
05308 #include "RooRealVar.h"
05309 #include "RooDataSet.h"
05310 #include "RooGaussian.h"
05311 #include "RooTruthModel.h"
05312 #include "RooFormulaVar.h"
05313 #include "RooRealSumPdf.h"
05314 #include "RooPolyVar.h"
05315 #include "RooProduct.h"
05316 #include "TH1.h"
05317 #include "TCanvas.h"
05318 #include "RooPlot.h"
05319 using namespace RooFit ;
05320 
05321 
05322 // Elementary operations on a gaussian PDF
05323 class TestBasic704 : public RooFitTestUnit
05324 {
05325 public: 
05326   TestBasic704(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Amplitude sum operator p.d.f",refFile,writeRef,verbose) {} ;
05327   Bool_t testCode() {
05328 
05329   // S e t u p   2 D   a m p l i t u d e   f u n c t i o n s
05330   // -------------------------------------------------------
05331 
05332   // Observables
05333   RooRealVar t("t","time",-1.,15.);
05334   RooRealVar cosa("cosa","cos(alpha)",-1.,1.);
05335 
05336   // Use RooTruthModel to obtain compiled implementation of sinh/cosh modulated decay functions
05337   RooRealVar tau("tau","#tau",1.5);  
05338   RooRealVar deltaGamma("deltaGamma","deltaGamma", 0.3);
05339   RooTruthModel tm("tm","tm",t) ;
05340   RooFormulaVar coshGBasis("coshGBasis","exp(-@0/ @1)*cosh(@0*@2/2)",RooArgList(t,tau,deltaGamma));
05341   RooFormulaVar sinhGBasis("sinhGBasis","exp(-@0/ @1)*sinh(@0*@2/2)",RooArgList(t,tau,deltaGamma));
05342   RooAbsReal* coshGConv = tm.convolution(&coshGBasis,&t);
05343   RooAbsReal* sinhGConv = tm.convolution(&sinhGBasis,&t);
05344     
05345   // Construct polynomial amplitudes in cos(a) 
05346   RooPolyVar poly1("poly1","poly1",cosa,RooArgList(RooConst(0.5),RooConst(0.2),RooConst(0.2)),0);
05347   RooPolyVar poly2("poly2","poly2",cosa,RooArgList(RooConst(1),RooConst(-0.2),RooConst(3)),0);
05348 
05349   // Construct 2D amplitude as uncorrelated product of amp(t)*amp(cosa)
05350   RooProduct  ampl1("ampl1","amplitude 1",RooArgSet(poly1,*coshGConv));
05351   RooProduct  ampl2("ampl2","amplitude 2",RooArgSet(poly2,*sinhGConv));
05352 
05353 
05354 
05355   // C o n s t r u c t   a m p l i t u d e   s u m   p d f 
05356   // -----------------------------------------------------
05357 
05358   // Amplitude strengths
05359   RooRealVar f1("f1","f1",1,0,2) ;
05360   RooRealVar f2("f2","f2",0.5,0,2) ;
05361   
05362   // Construct pdf
05363   RooRealSumPdf pdf("pdf","pdf",RooArgList(ampl1,ampl2),RooArgList(f1,f2)) ;
05364 
05365   // Generate some toy data from pdf
05366   RooDataSet* data = pdf.generate(RooArgSet(t,cosa),10000);
05367 
05368   // Fit pdf to toy data with only amplitude strength floating
05369   pdf.fitTo(*data) ;
05370 
05371 
05372 
05373   // P l o t   a m p l i t u d e   s u m   p d f 
05374   // -------------------------------------------
05375 
05376   // Make 2D plots of amplitudes
05377   TH1* hh_cos = ampl1.createHistogram("hh_cos",t,Binning(50),YVar(cosa,Binning(50))) ;
05378   TH1* hh_sin = ampl2.createHistogram("hh_sin",t,Binning(50),YVar(cosa,Binning(50))) ;
05379   hh_cos->SetLineColor(kBlue) ;
05380   hh_sin->SetLineColor(kBlue) ;
05381 
05382   
05383   // Make projection on t, plot data, pdf and its components
05384   // Note component projections may be larger than sum because amplitudes can be negative
05385   RooPlot* frame1 = t.frame();
05386   data->plotOn(frame1);
05387   pdf.plotOn(frame1);
05388   pdf.plotOn(frame1,Components(ampl1),LineStyle(kDashed));
05389   pdf.plotOn(frame1,Components(ampl2),LineStyle(kDashed),LineColor(kRed));
05390   
05391   // Make projection on cosa, plot data, pdf and its components
05392   // Note that components projection may be larger than sum because amplitudes can be negative
05393   RooPlot* frame2 = cosa.frame();
05394   data->plotOn(frame2);
05395   pdf.plotOn(frame2);
05396   pdf.plotOn(frame2,Components(ampl1),LineStyle(kDashed));
05397   pdf.plotOn(frame2,Components(ampl2),LineStyle(kDashed),LineColor(kRed));
05398   
05399 
05400   regPlot(frame1,"rf704_plot1") ;
05401   regPlot(frame2,"rf704_plot2") ;
05402   regTH(hh_cos,"rf704_hh_cos") ;
05403   regTH(hh_sin,"rf704_hh_sin") ;
05404 
05405   delete data ;
05406   delete coshGConv ;
05407   delete sinhGConv ;
05408 
05409   return kTRUE ;
05410   }
05411 } ;
05412 //////////////////////////////////////////////////////////////////////////
05413 //
05414 // 'SPECIAL PDFS' RooFit tutorial macro #705
05415 // 
05416 // Linear interpolation between p.d.f shapes using the 'Alex Read' algorithm
05417 //
05418 //
05419 //
05420 // 07/2008 - Wouter Verkerke 
05421 // 
05422 /////////////////////////////////////////////////////////////////////////
05423 
05424 #ifndef __CINT__
05425 #include "RooGlobalFunc.h"
05426 #endif
05427 #include "RooRealVar.h"
05428 #include "RooDataSet.h"
05429 #include "RooGaussian.h"
05430 #include "RooPolynomial.h"
05431 #include "RooIntegralMorph.h"
05432 #include "RooNLLVar.h"
05433 #include "TCanvas.h"
05434 #include "RooPlot.h"
05435 #include "TH1.h"
05436 using namespace RooFit ;
05437 
05438 
05439 // Elementary operations on a gaussian PDF
05440 class TestBasic705 : public RooFitTestUnit
05441 {
05442 public: 
05443 
05444   Double_t ctol() { return 5e-2 ; } // very conservative, this is a numerically difficult test
05445 
05446   TestBasic705(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Linear morph operator p.d.f.",refFile,writeRef,verbose) {} ;
05447   Bool_t testCode() {
05448 
05449   // C r e a t e   e n d   p o i n t   p d f   s h a p e s
05450   // ------------------------------------------------------
05451 
05452   // Observable
05453   RooRealVar x("x","x",-20,20) ;
05454 
05455   // Lower end point shape: a Gaussian
05456   RooRealVar g1mean("g1mean","g1mean",-10) ;
05457   RooGaussian g1("g1","g1",x,g1mean,RooConst(2)) ;
05458 
05459   // Upper end point shape: a Polynomial
05460   RooPolynomial g2("g2","g2",x,RooArgSet(RooConst(-0.03),RooConst(-0.001))) ;
05461 
05462 
05463  
05464   // C r e a t e   i n t e r p o l a t i n g   p d f 
05465   // -----------------------------------------------
05466 
05467   // Create interpolation variable
05468   RooRealVar alpha("alpha","alpha",0,1.0) ;
05469 
05470   // Specify sampling density on observable and interpolation variable
05471   x.setBins(1000,"cache") ;
05472   alpha.setBins(50,"cache") ;
05473 
05474   // Construct interpolating pdf in (x,a) represent g1(x) at a=a_min
05475   // and g2(x) at a=a_max
05476   RooIntegralMorph lmorph("lmorph","lmorph",g1,g2,x,alpha) ;
05477 
05478 
05479 
05480   // P l o t   i n t e r p o l a t i n g   p d f   a t   v a r i o u s   a l p h a 
05481   // -----------------------------------------------------------------------------
05482 
05483   // Show end points as blue curves
05484   RooPlot* frame1 = x.frame() ;
05485   g1.plotOn(frame1) ;
05486   g2.plotOn(frame1) ;
05487 
05488   // Show interpolated shapes in red
05489   alpha.setVal(0.125) ;
05490   lmorph.plotOn(frame1,LineColor(kRed),Name("alt1")) ;
05491   alpha.setVal(0.25) ;
05492   lmorph.plotOn(frame1,LineColor(kRed),Name("alt2")) ;
05493   alpha.setVal(0.375) ;
05494   lmorph.plotOn(frame1,LineColor(kRed),Name("alt3")) ;
05495   alpha.setVal(0.50) ;
05496   lmorph.plotOn(frame1,LineColor(kRed),Name("alt4")) ;
05497   alpha.setVal(0.625) ;
05498   lmorph.plotOn(frame1,LineColor(kRed),Name("alt5")) ;
05499   alpha.setVal(0.75) ;
05500   lmorph.plotOn(frame1,LineColor(kRed),Name("alt6")) ;
05501   alpha.setVal(0.875) ;
05502   lmorph.plotOn(frame1,LineColor(kRed),Name("alt7")) ;
05503   alpha.setVal(0.95) ;
05504   lmorph.plotOn(frame1,LineColor(kRed),Name("alt8")) ;
05505 
05506 
05507 
05508   // S h o w   2 D   d i s t r i b u t i o n   o f   p d f ( x , a l p h a ) 
05509   // -----------------------------------------------------------------------
05510   
05511   // Create 2D histogram
05512   TH1* hh = lmorph.createHistogram("hh",x,Binning(40),YVar(alpha,Binning(40))) ;
05513   hh->SetLineColor(kBlue) ;
05514 
05515 
05516   // F i t   p d f   t o   d a t a s e t   w i t h   a l p h a = 0 . 8 
05517   // -----------------------------------------------------------------
05518 
05519   // Generate a toy dataset at alpha = 0.8
05520   alpha=0.8 ;
05521   RooDataSet* data = lmorph.generate(x,1000) ;
05522 
05523   // Fit pdf to toy data
05524   lmorph.setCacheAlpha(kTRUE) ;
05525   lmorph.fitTo(*data) ;
05526 
05527   // Plot fitted pdf and data overlaid
05528   RooPlot* frame2 = x.frame(Bins(100)) ;
05529   data->plotOn(frame2) ;
05530   lmorph.plotOn(frame2) ; 
05531 
05532 
05533   // S c a n   - l o g ( L )   v s   a l p h a
05534   // -----------------------------------------
05535 
05536   // Show scan -log(L) of dataset w.r.t alpha
05537   RooPlot* frame3 = alpha.frame(Bins(100),Range(0.5,0.9)) ;
05538   
05539   // Make 2D pdf of histogram  
05540   RooNLLVar nll("nll","nll",lmorph,*data) ;  
05541   nll.plotOn(frame3,ShiftToZero()) ;    
05542 
05543   lmorph.setCacheAlpha(kFALSE) ;
05544 
05545 
05546   regPlot(frame1,"rf705_plot1") ;
05547   regPlot(frame2,"rf705_plot2") ;
05548   regPlot(frame3,"rf705_plot3") ;
05549   regTH(hh,"rf705_hh") ;
05550 
05551   delete data ;
05552   
05553   return kTRUE;
05554   }
05555 } ;
05556 //////////////////////////////////////////////////////////////////////////
05557 //
05558 // 'SPECIAL PDFS' RooFit tutorial macro #706
05559 // 
05560 // Histogram based p.d.f.s and functions
05561 //
05562 //
05563 //
05564 // 07/2008 - Wouter Verkerke 
05565 // 
05566 /////////////////////////////////////////////////////////////////////////
05567 
05568 #ifndef __CINT__
05569 #include "RooGlobalFunc.h"
05570 #endif
05571 #include "RooRealVar.h"
05572 #include "RooDataSet.h"
05573 #include "RooGaussian.h"
05574 #include "RooPolynomial.h"
05575 #include "RooHistPdf.h"
05576 #include "TCanvas.h"
05577 #include "RooPlot.h"
05578 using namespace RooFit ;
05579 
05580 
05581 // Elementary operations on a gaussian PDF
05582 class TestBasic706 : public RooFitTestUnit
05583 {
05584 public: 
05585   TestBasic706(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Histogram based p.d.f.s",refFile,writeRef,verbose) {} ;
05586   Bool_t testCode() {
05587 
05588   // C r e a t e   p d f   f o r   s a m p l i n g 
05589   // ---------------------------------------------
05590 
05591   RooRealVar x("x","x",0,20) ;
05592   RooPolynomial p("p","p",x,RooArgList(RooConst(0.01),RooConst(-0.01),RooConst(0.0004))) ;
05593 
05594 
05595 
05596   // C r e a t e   l o w   s t a t s   h i s t o g r a m
05597   // ---------------------------------------------------
05598 
05599   // Sample 500 events from p
05600   x.setBins(20) ;
05601   RooDataSet* data1 = p.generate(x,500) ;
05602   
05603   // Create a binned dataset with 20 bins and 500 events
05604   RooDataHist* hist1 = data1->binnedClone() ;
05605 
05606   // Represent data in dh as pdf in x
05607   RooHistPdf histpdf1("histpdf1","histpdf1",x,*hist1,0) ;
05608 
05609   // Plot unbinned data and histogram pdf overlaid
05610   RooPlot* frame1 = x.frame(Title("Low statistics histogram pdf"),Bins(100)) ;
05611   data1->plotOn(frame1) ;
05612   histpdf1.plotOn(frame1) ;  
05613   
05614 
05615   // C r e a t e   h i g h   s t a t s   h i s t o g r a m
05616   // -----------------------------------------------------
05617 
05618   // Sample 100000 events from p
05619   x.setBins(10) ;
05620   RooDataSet* data2 = p.generate(x,100000) ;
05621 
05622   // Create a binned dataset with 10 bins and 100K events
05623   RooDataHist* hist2 = data2->binnedClone() ;
05624 
05625   // Represent data in dh as pdf in x, apply 2nd order interpolation  
05626   RooHistPdf histpdf2("histpdf2","histpdf2",x,*hist2,2) ;
05627 
05628   // Plot unbinned data and histogram pdf overlaid
05629   RooPlot* frame2 = x.frame(Title("High stats histogram pdf with interpolation"),Bins(100)) ;
05630   data2->plotOn(frame2) ;
05631   histpdf2.plotOn(frame2) ;  
05632 
05633 
05634   regPlot(frame1,"rf607_plot1") ;
05635   regPlot(frame2,"rf607_plot2") ;
05636 
05637   delete data1 ;
05638   delete hist1 ;
05639   delete data2 ;
05640   delete hist2 ;
05641 
05642   return kTRUE ;
05643   }
05644 } ;
05645 //////////////////////////////////////////////////////////////////////////
05646 //
05647 // 'SPECIAL PDFS' RooFit tutorial macro #707
05648 // 
05649 // Using non-parametric (multi-dimensional) kernel estimation p.d.f.s
05650 //
05651 //
05652 //
05653 // 07/2008 - Wouter Verkerke 
05654 // 
05655 /////////////////////////////////////////////////////////////////////////
05656 
05657 #ifndef __CINT__
05658 #include "RooGlobalFunc.h"
05659 #endif
05660 #include "RooRealVar.h"
05661 #include "RooDataSet.h"
05662 #include "RooGaussian.h"
05663 #include "RooPolynomial.h"
05664 #include "RooKeysPdf.h"
05665 #include "RooNDKeysPdf.h"
05666 #include "RooProdPdf.h"
05667 #include "TCanvas.h"
05668 #include "TH1.h"
05669 #include "RooPlot.h"
05670 using namespace RooFit ;
05671 
05672 
05673 // Elementary operations on a gaussian PDF
05674 class TestBasic707 : public RooFitTestUnit
05675 {
05676 public: 
05677   TestBasic707(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Kernel estimation p.d.f.s",refFile,writeRef,verbose) {} ;
05678   Bool_t testCode() {
05679 
05680   // C r e a t e   l o w   s t a t s   1 - D   d a t a s e t 
05681   // -------------------------------------------------------
05682 
05683   // Create a toy pdf for sampling
05684   RooRealVar x("x","x",0,20) ;
05685   RooPolynomial p("p","p",x,RooArgList(RooConst(0.01),RooConst(-0.01),RooConst(0.0004))) ;
05686 
05687   // Sample 500 events from p
05688   RooDataSet* data1 = p.generate(x,200) ;
05689 
05690 
05691 
05692   // C r e a t e   1 - D   k e r n e l   e s t i m a t i o n   p d f
05693   // ---------------------------------------------------------------
05694 
05695   // Create adaptive kernel estimation pdf. In this configuration the input data
05696   // is mirrored over the boundaries to minimize edge effects in distribution
05697   // that do not fall to zero towards the edges
05698   RooKeysPdf kest1("kest1","kest1",x,*data1,RooKeysPdf::MirrorBoth) ;
05699 
05700   // An adaptive kernel estimation pdf on the same data without mirroring option
05701   // for comparison
05702   RooKeysPdf kest2("kest2","kest2",x,*data1,RooKeysPdf::NoMirror) ;
05703 
05704 
05705   // Adaptive kernel estimation pdf with increased bandwidth scale factor
05706   // (promotes smoothness over detail preservation)
05707   RooKeysPdf kest3("kest3","kest3",x,*data1,RooKeysPdf::MirrorBoth,2) ;
05708 
05709 
05710   // Plot kernel estimation pdfs with and without mirroring over data
05711   RooPlot* frame = x.frame(Title("Adaptive kernel estimation pdf with and w/o mirroring"),Bins(20)) ;
05712   data1->plotOn(frame) ;
05713   kest1.plotOn(frame) ;    
05714   kest2.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ;    
05715 
05716 
05717   // Plot kernel estimation pdfs with regular and increased bandwidth
05718   RooPlot* frame2 = x.frame(Title("Adaptive kernel estimation pdf with regular, increased bandwidth")) ;
05719   kest1.plotOn(frame2) ;    
05720   kest3.plotOn(frame2,LineColor(kMagenta)) ;    
05721 
05722 
05723 
05724   // C r e a t e   l o w   s t a t s   2 - D   d a t a s e t 
05725   // -------------------------------------------------------
05726 
05727   // Construct a 2D toy pdf for sampleing
05728   RooRealVar y("y","y",0,20) ;
05729   RooPolynomial py("py","py",y,RooArgList(RooConst(0.01),RooConst(0.01),RooConst(-0.0004))) ;
05730   RooProdPdf pxy("pxy","pxy",RooArgSet(p,py)) ;
05731   RooDataSet* data2 = pxy.generate(RooArgSet(x,y),1000) ;
05732 
05733 
05734 
05735   // C r e a t e   2 - D   k e r n e l   e s t i m a t i o n   p d f
05736   // ---------------------------------------------------------------
05737 
05738   // Create 2D adaptive kernel estimation pdf with mirroring 
05739   RooNDKeysPdf kest4("kest4","kest4",RooArgSet(x,y),*data2,"am") ;
05740 
05741   // Create 2D adaptive kernel estimation pdf with mirroring and double bandwidth
05742   RooNDKeysPdf kest5("kest5","kest5",RooArgSet(x,y),*data2,"am",2) ;
05743 
05744   // Create a histogram of the data
05745   TH1* hh_data = data2->createHistogram("hh_data",x,Binning(10),YVar(y,Binning(10))) ;
05746 
05747   // Create histogram of the 2d kernel estimation pdfs
05748   TH1* hh_pdf = kest4.createHistogram("hh_pdf",x,Binning(25),YVar(y,Binning(25))) ;
05749   TH1* hh_pdf2 = kest5.createHistogram("hh_pdf2",x,Binning(25),YVar(y,Binning(25))) ;
05750   hh_pdf->SetLineColor(kBlue) ;
05751   hh_pdf2->SetLineColor(kMagenta) ;
05752     
05753   regPlot(frame,"rf707_plot1") ;
05754   regPlot(frame2,"rf707_plot2") ;
05755   regTH(hh_data,"rf707_hhdata") ;
05756   regTH(hh_pdf,"rf707_hhpdf") ;
05757   regTH(hh_pdf2,"rf707_hhpdf2") ;
05758   
05759   delete data1 ;
05760   delete data2 ;
05761 
05762   return kTRUE ;
05763   }
05764 } ;
05765 //////////////////////////////////////////////////////////////////////////
05766 //
05767 // 'SPECIAL PDFS' RooFit tutorial macro #708
05768 // 
05769 // Special decay pdf for B physics with mixing and/or CP violation
05770 //
05771 //
05772 //
05773 // 07/2008 - Wouter Verkerke 
05774 // 
05775 /////////////////////////////////////////////////////////////////////////
05776 
05777 #ifndef __CINT__
05778 #include "RooGlobalFunc.h"
05779 #endif
05780 #include "RooRealVar.h"
05781 #include "RooDataSet.h"
05782 #include "RooGaussian.h"
05783 #include "RooCategory.h"
05784 #include "RooBMixDecay.h"
05785 #include "RooBCPEffDecay.h"
05786 #include "RooBDecay.h"
05787 #include "RooFormulaVar.h"
05788 #include "RooTruthModel.h"
05789 #include "TCanvas.h"
05790 #include "RooPlot.h"
05791 using namespace RooFit ;
05792 
05793 // Elementary operations on a gaussian PDF
05794 class TestBasic708 : public RooFitTestUnit
05795 {
05796 public: 
05797   TestBasic708(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("B Physics p.d.f.s",refFile,writeRef,verbose) {} ;
05798   Bool_t testCode() {
05799 
05800   ////////////////////////////////////////////////////
05801   // B - D e c a y   w i t h   m i x i n g          //
05802   ////////////////////////////////////////////////////
05803 
05804   // C o n s t r u c t   p d f 
05805   // -------------------------
05806   
05807   // Observable
05808   RooRealVar dt("dt","dt",-10,10) ;
05809   dt.setBins(40) ;
05810 
05811   // Parameters
05812   RooRealVar dm("dm","delta m(B0)",0.472) ;
05813   RooRealVar tau("tau","tau (B0)",1.547) ;
05814   RooRealVar w("w","flavour mistag rate",0.1) ;
05815   RooRealVar dw("dw","delta mistag rate for B0/B0bar",0.1) ;
05816 
05817   RooCategory mixState("mixState","B0/B0bar mixing state") ;
05818   mixState.defineType("mixed",-1) ;
05819   mixState.defineType("unmixed",1) ;
05820 
05821   RooCategory tagFlav("tagFlav","Flavour of the tagged B0") ;
05822   tagFlav.defineType("B0",1) ;
05823   tagFlav.defineType("B0bar",-1) ;
05824 
05825   // Use delta function resolution model
05826   RooTruthModel tm("tm","truth model",dt) ;
05827 
05828   // Construct Bdecay with mixing
05829   RooBMixDecay bmix("bmix","decay",dt,mixState,tagFlav,tau,dm,w,dw,tm,RooBMixDecay::DoubleSided) ;
05830 
05831 
05832 
05833   // P l o t   p d f   i n   v a r i o u s   s l i c e s
05834   // ---------------------------------------------------
05835 
05836   // Generate some data
05837   RooDataSet* data = bmix.generate(RooArgSet(dt,mixState,tagFlav),10000) ;
05838 
05839   // Plot B0 and B0bar tagged data separately
05840   // For all plots below B0 and B0 tagged data will look somewhat differently
05841   // if the flavor tagging mistag rate for B0 and B0 is different (i.e. dw!=0)
05842   RooPlot* frame1 = dt.frame(Title("B decay distribution with mixing (B0/B0bar)")) ;
05843 
05844   data->plotOn(frame1,Cut("tagFlav==tagFlav::B0")) ;
05845   bmix.plotOn(frame1,Slice(tagFlav,"B0")) ;
05846 
05847   data->plotOn(frame1,Cut("tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05848   bmix.plotOn(frame1,Slice(tagFlav,"B0bar"),LineColor(kCyan),Name("alt")) ;
05849 
05850 
05851   // Plot mixed slice for B0 and B0bar tagged data separately
05852   RooPlot* frame2 = dt.frame(Title("B decay distribution of mixed events (B0/B0bar)")) ;
05853 
05854   data->plotOn(frame2,Cut("mixState==mixState::mixed&&tagFlav==tagFlav::B0")) ;
05855   bmix.plotOn(frame2,Slice(tagFlav,"B0"),Slice(mixState,"mixed")) ;
05856 
05857   data->plotOn(frame2,Cut("mixState==mixState::mixed&&tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05858   bmix.plotOn(frame2,Slice(tagFlav,"B0bar"),Slice(mixState,"mixed"),LineColor(kCyan),Name("alt")) ;
05859 
05860 
05861   // Plot unmixed slice for B0 and B0bar tagged data separately
05862   RooPlot* frame3 = dt.frame(Title("B decay distribution of unmixed events (B0/B0bar)")) ;
05863 
05864   data->plotOn(frame3,Cut("mixState==mixState::unmixed&&tagFlav==tagFlav::B0")) ;
05865   bmix.plotOn(frame3,Slice(tagFlav,"B0"),Slice(mixState,"unmixed")) ;
05866 
05867   data->plotOn(frame3,Cut("mixState==mixState::unmixed&&tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05868   bmix.plotOn(frame3,Slice(tagFlav,"B0bar"),Slice(mixState,"unmixed"),LineColor(kCyan),Name("alt")) ;
05869 
05870 
05871 
05872 
05873 
05874   ///////////////////////////////////////////////////////
05875   // B - D e c a y   w i t h   C P   v i o l a t i o n //
05876   ///////////////////////////////////////////////////////
05877 
05878   // C o n s t r u c t   p d f 
05879   // -------------------------
05880   
05881   // Additional parameters needed for B decay with CPV
05882   RooRealVar CPeigen("CPeigen","CP eigen value",-1) ;
05883   RooRealVar absLambda("absLambda","|lambda|",1,0,2) ;
05884   RooRealVar argLambda("absLambda","|lambda|",0.7,-1,1) ;
05885   RooRealVar effR("effR","B0/B0bar reco efficiency ratio",1) ;
05886 
05887   // Construct Bdecay with CP violation
05888   RooBCPEffDecay bcp("bcp","bcp", dt, tagFlav, tau, dm, w, CPeigen, absLambda, argLambda, effR, dw, tm, RooBCPEffDecay::DoubleSided) ;
05889       
05890 
05891 
05892   // P l o t   s c e n a r i o   1   -   s i n ( 2 b )   =   0 . 7 ,   | l | = 1 
05893   // ---------------------------------------------------------------------------
05894 
05895   // Generate some data
05896   RooDataSet* data2 = bcp.generate(RooArgSet(dt,tagFlav),10000) ;
05897 
05898   // Plot B0 and B0bar tagged data separately
05899   RooPlot* frame4 = dt.frame(Title("B decay distribution with CPV(|l|=1,Im(l)=0.7) (B0/B0bar)")) ;
05900 
05901   data2->plotOn(frame4,Cut("tagFlav==tagFlav::B0")) ;
05902   bcp.plotOn(frame4,Slice(tagFlav,"B0")) ;
05903 
05904   data2->plotOn(frame4,Cut("tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05905   bcp.plotOn(frame4,Slice(tagFlav,"B0bar"),LineColor(kCyan),Name("alt")) ;
05906 
05907 
05908 
05909   // P l o t   s c e n a r i o   2   -   s i n ( 2 b )   =   0 . 7 ,   | l | = 0 . 7 
05910   // -------------------------------------------------------------------------------
05911 
05912   absLambda=0.7 ;
05913 
05914   // Generate some data
05915   RooDataSet* data3 = bcp.generate(RooArgSet(dt,tagFlav),10000) ;
05916 
05917   // Plot B0 and B0bar tagged data separately (sin2b = 0.7 plus direct CPV |l|=0.5)
05918   RooPlot* frame5 = dt.frame(Title("B decay distribution with CPV(|l|=0.7,Im(l)=0.7) (B0/B0bar)")) ;  
05919 
05920   data3->plotOn(frame5,Cut("tagFlav==tagFlav::B0")) ;
05921   bcp.plotOn(frame5,Slice(tagFlav,"B0")) ;
05922 
05923   data3->plotOn(frame5,Cut("tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05924   bcp.plotOn(frame5,Slice(tagFlav,"B0bar"),LineColor(kCyan),Name("alt")) ;
05925 
05926 
05927 
05928   //////////////////////////////////////////////////////////////////////////////////
05929   // G e n e r i c   B   d e c a y  w i t h    u s e r   c o e f f i c i e n t s  //
05930   //////////////////////////////////////////////////////////////////////////////////
05931 
05932   // C o n s t r u c t   p d f 
05933   // -------------------------
05934   
05935   // Model parameters
05936   RooRealVar DGbG("DGbG","DGamma/GammaAvg",0.5,-1,1);
05937   RooRealVar Adir("Adir","-[1-abs(l)**2]/[1+abs(l)**2]",0);
05938   RooRealVar Amix("Amix","2Im(l)/[1+abs(l)**2]",0.7);
05939   RooRealVar Adel("Adel","2Re(l)/[1+abs(l)**2]",0.7);
05940   
05941   // Derived input parameters for pdf
05942   RooFormulaVar DG("DG","Delta Gamma","@1/@0",RooArgList(tau,DGbG));
05943   
05944   // Construct coefficient functions for sin,cos,sinh modulations of decay distribution
05945   RooFormulaVar fsin("fsin","fsin","@0*@1*(1-2*@2)",RooArgList(Amix,tagFlav,w));
05946   RooFormulaVar fcos("fcos","fcos","@0*@1*(1-2*@2)",RooArgList(Adir,tagFlav,w));
05947   RooFormulaVar fsinh("fsinh","fsinh","@0",RooArgList(Adel));
05948   
05949   // Construct generic B decay pdf using above user coefficients
05950   RooBDecay bcpg("bcpg","bcpg",dt,tau,DG,RooConst(1),fsinh,fcos,fsin,dm,tm,RooBDecay::DoubleSided);
05951   
05952   
05953   
05954   // P l o t   -   I m ( l ) = 0 . 7 ,   R e ( l ) = 0 . 7   | l | = 1,   d G / G = 0 . 5 
05955   // -------------------------------------------------------------------------------------
05956   
05957   // Generate some data
05958   RooDataSet* data4 = bcpg.generate(RooArgSet(dt,tagFlav),10000) ;
05959   
05960   // Plot B0 and B0bar tagged data separately 
05961   RooPlot* frame6 = dt.frame(Title("B decay distribution with CPV(Im(l)=0.7,Re(l)=0.7,|l|=1,dG/G=0.5) (B0/B0bar)")) ;  
05962   
05963   data4->plotOn(frame6,Cut("tagFlav==tagFlav::B0")) ;
05964   bcpg.plotOn(frame6,Slice(tagFlav,"B0")) ;
05965   
05966   data4->plotOn(frame6,Cut("tagFlav==tagFlav::B0bar"),MarkerColor(kCyan)) ;
05967   bcpg.plotOn(frame6,Slice(tagFlav,"B0bar"),LineColor(kCyan),Name("alt")) ;
05968   
05969  
05970   regPlot(frame1,"rf708_plot1") ;
05971   regPlot(frame2,"rf708_plot2") ;
05972   regPlot(frame3,"rf708_plot3") ;
05973   regPlot(frame4,"rf708_plot4") ;
05974   regPlot(frame5,"rf708_plot5") ;
05975   regPlot(frame6,"rf708_plot6") ;
05976 
05977   delete data ;
05978   delete data2 ;
05979   delete data3 ;
05980   delete data4 ;
05981 
05982   return kTRUE ;
05983   }  
05984 } ;
05985 /////////////////////////////////////////////////////////////////////////
05986 //
05987 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #801
05988 // 
05989 // A Toy Monte Carlo study that perform cycles of
05990 // event generation and fittting
05991 //
05992 // 
05993 /////////////////////////////////////////////////////////////////////////
05994 
05995 #ifndef __CINT__
05996 #include "RooGlobalFunc.h"
05997 #endif
05998 #include "RooRealVar.h"
05999 #include "RooDataSet.h"
06000 #include "RooGaussian.h"
06001 #include "RooChebychev.h"
06002 #include "RooAddPdf.h"
06003 #include "RooMCStudy.h"
06004 #include "RooPlot.h"
06005 #include "TCanvas.h"
06006 #include "TH2.h"
06007 #include "RooFitResult.h"
06008 #include "TStyle.h"
06009 #include "TDirectory.h"
06010 
06011 using namespace RooFit ;
06012 
06013 
06014 // Elementary operations on a gaussian PDF
06015 class TestBasic801 : public RooFitTestUnit
06016 {
06017 public: 
06018   TestBasic801(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Automated MC studies",refFile,writeRef,verbose) {} ;
06019   Bool_t testCode() {
06020 
06021   // C r e a t e   m o d e l
06022   // -----------------------
06023 
06024   // Declare observable x
06025   RooRealVar x("x","x",0,10) ;
06026   x.setBins(40) ;
06027 
06028   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
06029   RooRealVar mean("mean","mean of gaussians",5,0,10) ;
06030   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
06031   RooRealVar sigma2("sigma2","width of gaussians",1) ;
06032 
06033   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
06034   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
06035   
06036   // Build Chebychev polynomial p.d.f.  
06037   RooRealVar a0("a0","a0",0.5,0.,1.) ;
06038   RooRealVar a1("a1","a1",-0.2,-1,1.) ;
06039   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
06040 
06041   // Sum the signal components into a composite signal p.d.f.
06042   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
06043   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
06044 
06045   // Sum the composite signal and background 
06046   RooRealVar nbkg("nbkg","number of background events,",150,0,1000) ;
06047   RooRealVar nsig("nsig","number of signal events",150,0,1000) ;
06048   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
06049 
06050 
06051 
06052   // C r e a t e   m a n a g e r
06053   // ---------------------------
06054 
06055   // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
06056   //
06057   // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
06058   // per sample executed, and any error message that occur during fitting
06059   //
06060   // The Extended() option has two effects: 
06061   //    1) The extended ML term is included in the likelihood and 
06062   //    2) A poisson fluctuation is introduced on the number of generated events 
06063   //
06064   // The FitOptions() given here are passed to the fitting stage of each toy experiment.
06065   // If Save() is specified, the fit result of each experiment is saved by the manager  
06066   //
06067   // A Binned() option is added in this example to bin the data between generation and fitting
06068   // to speed up the study at the expemse of some precision
06069 
06070   RooMCStudy* mcstudy = new RooMCStudy(model,x,Binned(kTRUE),Silence(),Extended(),
06071                                        FitOptions(Save(kTRUE),PrintEvalErrors(0))) ;
06072   
06073 
06074   // G e n e r a t e   a n d   f i t   e v e n t s
06075   // ---------------------------------------------
06076 
06077   // Generate and fit 100 samples of Poisson(nExpected) events
06078   mcstudy->generateAndFit(100) ;
06079 
06080 
06081 
06082   // E x p l o r e   r e s u l t s   o f   s t u d y 
06083   // ------------------------------------------------
06084 
06085   // Make plots of the distributions of mean, the error on mean and the pull of mean
06086   RooPlot* frame1 = mcstudy->plotParam(mean,Bins(40)) ;
06087   RooPlot* frame2 = mcstudy->plotError(mean,Bins(40)) ;
06088   RooPlot* frame3 = mcstudy->plotPull(mean,Bins(40),FitGauss(kTRUE)) ;
06089 
06090   // Plot distribution of minimized likelihood
06091   RooPlot* frame4 = mcstudy->plotNLL(Bins(40)) ;
06092 
06093   regPlot(frame1,"rf801_plot1") ;
06094   regPlot(frame2,"rf801_plot2") ;
06095   regPlot(frame3,"rf801_plot3") ;
06096   regPlot(frame4,"rf801_plot4") ;
06097 
06098   delete mcstudy ;
06099 
06100   return kTRUE ;
06101   }
06102 } ;
06103 /////////////////////////////////////////////////////////////////////////
06104 //
06105 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #802
06106 // 
06107 // RooMCStudy: using separate fit and generator models, using the chi^2 calculator model 
06108 //
06109 // 
06110 // 07/2008 - Wouter Verkerke 
06111 //
06112 /////////////////////////////////////////////////////////////////////////
06113 
06114 #ifndef __CINT__
06115 #include "RooGlobalFunc.h"
06116 #endif
06117 #include "RooRealVar.h"
06118 #include "RooDataSet.h"
06119 #include "RooGaussian.h"
06120 #include "RooChebychev.h"
06121 #include "RooAddPdf.h"
06122 #include "RooMCStudy.h"
06123 #include "RooChi2MCSModule.h"
06124 #include "RooPlot.h"
06125 #include "TCanvas.h"
06126 #include "TH1.h"
06127 #include "TDirectory.h"
06128 
06129 using namespace RooFit ;
06130 
06131 
06132 // Elementary operations on a gaussian PDF
06133 class TestBasic802 : public RooFitTestUnit
06134 {
06135 public: 
06136   TestBasic802(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("MC Study with chi^2 calculator",refFile,writeRef,verbose) {} ;
06137   Bool_t testCode() {
06138 
06139   // C r e a t e   m o d e l 
06140   // -----------------------
06141 
06142   // Observables, parameters
06143   RooRealVar x("x","x",-10,10) ;
06144   x.setBins(10) ;
06145   RooRealVar mean("mean","mean of gaussian",0) ;
06146   RooRealVar sigma("sigma","width of gaussian",5,1,10) ;
06147 
06148   // Create Gaussian pdf
06149   RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;  
06150 
06151 
06152 
06153   // C r e a t e   m a n a g e r  w i t h   c h i ^ 2   a d d - o n   m o d u l e
06154   // ----------------------------------------------------------------------------
06155 
06156   // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
06157   RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;
06158 
06159   // Add chi^2 calculator module to mcs
06160   RooChi2MCSModule chi2mod ;
06161   mcs->addModule(chi2mod) ;
06162 
06163   // Generate 200 samples of 1000 events
06164   mcs->generateAndFit(200,1000) ;
06165   
06166   // Fill histograms with distributions chi2 and prob(chi2,ndf) that
06167   // are calculated by RooChiMCSModule
06168 
06169   RooRealVar* chi2 = (RooRealVar*) mcs->fitParDataSet().get()->find("chi2") ;
06170   RooRealVar* prob = (RooRealVar*) mcs->fitParDataSet().get()->find("prob") ;
06171 
06172   TH1* h_chi2  = new TH1F("h_chi2","",40,0,20) ;
06173   TH1* h_prob  = new TH1F("h_prob","",40,0,1) ;
06174 
06175   mcs->fitParDataSet().fillHistogram(h_chi2,*chi2) ; 
06176   mcs->fitParDataSet().fillHistogram(h_prob,*prob) ;   
06177 
06178 
06179 
06180   // C r e a t e   m a n a g e r  w i t h   s e p a r a t e   f i t   m o d e l 
06181   // ----------------------------------------------------------------------------
06182 
06183   // Create alternate pdf with shifted mean
06184   RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
06185   RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;  
06186 
06187   // Create study manager with separate generation and fit model. This configuration
06188   // is set up to generate bad fits as the fit and generator model have different means
06189   // and the mean parameter is not floating in the fit
06190   RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;
06191 
06192   // Add chi^2 calculator module to mcs
06193   RooChi2MCSModule chi2mod2 ;
06194   mcs2->addModule(chi2mod2) ;
06195 
06196   // Generate 200 samples of 1000 events
06197   mcs2->generateAndFit(200,1000) ;
06198   
06199   // Fill histograms with distributions chi2 and prob(chi2,ndf) that
06200   // are calculated by RooChiMCSModule
06201 
06202   TH1* h2_chi2  = new TH1F("h2_chi2","",40,0,20) ;
06203   TH1* h2_prob  = new TH1F("h2_prob","",40,0,1) ;
06204   
06205   mcs2->fitParDataSet().fillHistogram(h2_chi2,*chi2) ; 
06206   mcs2->fitParDataSet().fillHistogram(h2_prob,*prob) ;   
06207 
06208   h_chi2->SetLineColor(kRed) ;
06209   h_prob->SetLineColor(kRed) ;
06210 
06211   regTH(h_chi2,"rf802_hist_chi2") ;
06212   regTH(h2_chi2,"rf802_hist2_chi2") ;
06213   regTH(h_prob,"rf802_hist_prob") ;
06214   regTH(h2_prob,"rf802_hist2_prob") ;
06215 
06216   delete mcs ;
06217   delete mcs2 ;
06218 
06219   return kTRUE ;  
06220   }
06221 } ;
06222 /////////////////////////////////////////////////////////////////////////
06223 //
06224 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #803
06225 // 
06226 // RooMCStudy: Using the randomizer and profile likelihood add-on models
06227 //
06228 // 
06229 // 07/2008 - Wouter Verkerke 
06230 //
06231 /////////////////////////////////////////////////////////////////////////
06232 
06233 #ifndef __CINT__
06234 #include "RooGlobalFunc.h"
06235 #endif
06236 #include "RooRealVar.h"
06237 #include "RooDataSet.h"
06238 #include "RooGaussian.h"
06239 #include "RooChebychev.h"
06240 #include "RooAddPdf.h"
06241 #include "RooMCStudy.h"
06242 #include "RooRandomizeParamMCSModule.h"
06243 #include "RooDLLSignificanceMCSModule.h"
06244 #include "RooPlot.h"
06245 #include "TCanvas.h"
06246 #include "TH1.h"
06247 #include "TDirectory.h"
06248 
06249 using namespace RooFit ;
06250 
06251 
06252 class TestBasic803 : public RooFitTestUnit
06253 {
06254 public: 
06255   TestBasic803(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("MC Study with param rand. and Z calc",refFile,writeRef,verbose) {} ;
06256   Bool_t testCode() {
06257 
06258   // C r e a t e   m o d e l 
06259   // -----------------------
06260 
06261   // Simulation of signal and background of top quark decaying into
06262   // 3 jets with background
06263 
06264   // Observable
06265   RooRealVar mjjj("mjjj","m(3jet) (GeV)",100,85.,350.) ;
06266 
06267   // Signal component (Gaussian)
06268   RooRealVar mtop("mtop","m(top)",162) ;
06269   RooRealVar wtop("wtop","m(top) resolution",15.2) ;
06270   RooGaussian sig("sig","top signal",mjjj,mtop,wtop) ;
06271 
06272   // Background component (Chebychev)
06273   RooRealVar c0("c0","Chebychev coefficient 0",-0.846,-1.,1.) ;
06274   RooRealVar c1("c1","Chebychev coefficient 1", 0.112, 0.,1.) ;
06275   RooRealVar c2("c2","Chebychev coefficient 2", 0.076, 0.,1.) ;
06276   RooChebychev bkg("bkg","combinatorial background",mjjj,RooArgList(c0,c1,c2)) ;
06277 
06278   // Composite model
06279   RooRealVar nsig("nsig","number of signal events",53,0,1e3) ;
06280   RooRealVar nbkg("nbkg","number of background events",103,0,5e3) ;
06281   RooAddPdf model("model","model",RooArgList(sig,bkg),RooArgList(nsig,nbkg)) ;
06282 
06283 
06284 
06285   // C r e a t e   m a n a g e r
06286   // ---------------------------
06287 
06288   // Configure manager to perform binned extended likelihood fits (Binned(),Extended()) on data generated
06289   // with a Poisson fluctuation on Nobs (Extended())
06290   RooMCStudy* mcs = new RooMCStudy(model,mjjj,Binned(),Silence(),Extended(kTRUE),
06291                                    FitOptions(Extended(kTRUE),PrintEvalErrors(-1))) ;
06292 
06293 
06294 
06295   // C u s t o m i z e   m a n a g e r
06296   // ---------------------------------
06297 
06298   // Add module that randomizes the summed value of nsig+nbkg 
06299   // sampling from a uniform distribution between 0 and 1000
06300   //
06301   // In general one can randomize a single parameter, or a 
06302   // sum of N parameters, using either a uniform or a Gaussian
06303   // distribution. Multiple randomization can be executed
06304   // by a single randomizer module
06305   
06306   RooRandomizeParamMCSModule randModule ;
06307   randModule.sampleSumUniform(RooArgSet(nsig,nbkg),50,500) ;
06308   mcs->addModule(randModule) ;  
06309 
06310 
06311   // Add profile likelihood calculation of significance. Redo each
06312   // fit while keeping parameter nsig fixed to zero. For each toy,
06313   // the difference in -log(L) of both fits is stored, as well
06314   // a simple significance interpretation of the delta(-logL)
06315   // using Dnll = 0.5 sigma^2
06316 
06317   RooDLLSignificanceMCSModule sigModule(nsig,0) ;
06318   mcs->addModule(sigModule) ;
06319 
06320 
06321 
06322   // R u n   m a n a g e r ,   m a k e   p l o t s
06323   // ---------------------------------------------
06324 
06325   mcs->generateAndFit(50) ;
06326 
06327   // Make some plots  
06328   RooRealVar* ngen    = (RooRealVar*) mcs->fitParDataSet().get()->find("ngen") ;
06329   RooRealVar* dll     = (RooRealVar*) mcs->fitParDataSet().get()->find("dll_nullhypo_nsig") ;
06330   RooRealVar* z       = (RooRealVar*) mcs->fitParDataSet().get()->find("significance_nullhypo_nsig") ;
06331   RooRealVar* nsigerr = (RooRealVar*) mcs->fitParDataSet().get()->find("nsigerr") ;
06332 
06333   TH1* dll_vs_ngen     = new TH2F("h_dll_vs_ngen"    ,"",40,0,500,40,0,50) ;
06334   TH1* z_vs_ngen       = new TH2F("h_z_vs_ngen"      ,"",40,0,500,40,0,10) ;
06335   TH1* errnsig_vs_ngen = new TH2F("h_nsigerr_vs_ngen","",40,0,500,40,0,30) ;
06336   TH1* errnsig_vs_nsig = new TH2F("h_nsigerr_vs_nsig","",40,0,200,40,0,30) ;
06337 
06338   mcs->fitParDataSet().fillHistogram(dll_vs_ngen,RooArgList(*ngen,*dll)) ;
06339   mcs->fitParDataSet().fillHistogram(z_vs_ngen,RooArgList(*ngen,*z)) ;
06340   mcs->fitParDataSet().fillHistogram(errnsig_vs_ngen,RooArgList(*ngen,*nsigerr)) ;
06341   mcs->fitParDataSet().fillHistogram(errnsig_vs_nsig,RooArgList(nsig,*nsigerr)) ;
06342 
06343   regTH(dll_vs_ngen,"rf803_dll_vs_ngen") ;
06344   regTH(z_vs_ngen,"rf803_z_vs_ngen") ;
06345   regTH(errnsig_vs_ngen,"rf803_errnsig_vs_ngen") ;
06346   regTH(errnsig_vs_nsig,"rf803_errnsig_vs_nsig") ;
06347 
06348   delete mcs ;
06349 
06350   return kTRUE ;
06351 
06352   }
06353 } ;
06354 
06355 
06356 
06357 
06358 /////////////////////////////////////////////////////////////////////////
06359 //
06360 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #804
06361 // 
06362 // Using RooMCStudy on models with constrains
06363 //
06364 // 
06365 // 07/2008 - Wouter Verkerke 
06366 //
06367 /////////////////////////////////////////////////////////////////////////
06368 
06369 #ifndef __CINT__
06370 #include "RooGlobalFunc.h"
06371 #endif
06372 #include "RooRealVar.h"
06373 #include "RooDataSet.h"
06374 #include "RooGaussian.h"
06375 #include "RooPolynomial.h"
06376 #include "RooAddPdf.h"
06377 #include "RooProdPdf.h"
06378 #include "RooMCStudy.h"
06379 #include "RooPlot.h"
06380 #include "TCanvas.h"
06381 #include "TH1.h"
06382 using namespace RooFit ;
06383 
06384 
06385 class TestBasic804 : public RooFitTestUnit
06386 {
06387 public: 
06388   TestBasic804(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("MC Studies with aux. obs. constraints",refFile,writeRef,verbose) {} ;
06389 
06390   Double_t htol() { return 0.1 ; } // numerically very difficult test
06391 
06392   Bool_t testCode() {
06393 
06394   // C r e a t e   m o d e l   w i t h   p a r a m e t e r   c o n s t r a i n t
06395   // ---------------------------------------------------------------------------
06396 
06397   // Observable
06398   RooRealVar x("x","x",-10,10) ;
06399 
06400   // Signal component
06401   RooRealVar m("m","m",0,-10,10) ;
06402   RooRealVar s("s","s",2,0.1,10) ;
06403   RooGaussian g("g","g",x,m,s) ;
06404 
06405   // Background component
06406   RooPolynomial p("p","p",x) ;
06407 
06408   // Composite model
06409   RooRealVar f("f","f",0.4,0.,1.) ;
06410   RooAddPdf sum("sum","sum",RooArgSet(g,p),f) ;
06411 
06412   // Construct constraint on parameter f
06413   RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.7),RooConst(0.1)) ;
06414 
06415   // Multiply constraint with p.d.f
06416   RooProdPdf sumc("sumc","sum with constraint",RooArgSet(sum,fconstraint)) ;
06417 
06418 
06419 
06420   // S e t u p   t o y   s t u d y   w i t h   m o d e l
06421   // ---------------------------------------------------
06422 
06423   // Perform toy study with internal constraint on f
06424   RooMCStudy mcs(sumc,x,Constrain(f),Silence(),Binned(),FitOptions(PrintLevel(-1))) ;
06425 
06426   // Run 50 toys of 2000 events.  
06427   // Before each toy is generated, a value for the f is sampled from the constraint pdf and 
06428   // that value is used for the generation of that toy.
06429   mcs.generateAndFit(50,2000) ;
06430 
06431   // Make plot of distribution of generated value of f parameter
06432   RooRealVar* f_gen = (RooRealVar*) mcs.fitParDataSet().get()->find("f_gen") ;
06433   TH1* h_f_gen = new TH1F("h_f_gen","",40,0,1) ;
06434   mcs.fitParDataSet().fillHistogram(h_f_gen,*f_gen) ;
06435 
06436   // Make plot of distribution of fitted value of f parameter
06437   RooPlot* frame1  = mcs.plotParam(f,Bins(40),Range(0.4,1)) ;
06438   frame1->SetTitle("Distribution of fitted f values") ;
06439 
06440   // Make plot of pull distribution on f
06441   RooPlot* frame2 = mcs.plotPull(f,Bins(40),Range(-3,3)) ;
06442   frame1->SetTitle("Distribution of f pull values") ;
06443 
06444   regTH(h_f_gen,"rf804_h_f_gen") ;
06445   regPlot(frame1,"rf804_plot1") ;
06446   regPlot(frame2,"rf804_plot2") ;
06447   
06448   return kTRUE ;
06449   }
06450 } ;
06451 

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