rs701_BayesianCalculator.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'Bayesian Calculator' RooStats tutorial macro #701
00004 // author: Gregory Schott
00005 // date Sep 2009
00006 //
00007 // This tutorial shows an example of using the BayesianCalculator class 
00008 //
00009 /////////////////////////////////////////////////////////////////////////
00010 
00011 
00012 #include "RooRealVar.h"
00013 #include "RooWorkspace.h"
00014 #include "RooDataSet.h"
00015 #include "RooPlot.h"
00016 #include "RooMsgService.h"
00017 
00018 #include "RooStats/BayesianCalculator.h"
00019 #include "RooStats/SimpleInterval.h"
00020 #include "TCanvas.h"
00021 
00022 using namespace RooFit;
00023 using namespace RooStats;
00024 
00025 void rs701_BayesianCalculator(bool useBkg = true, double confLevel = 0.90)
00026 {
00027 
00028 
00029   RooWorkspace* w = new RooWorkspace("w",true);
00030   w->factory("SUM::pdf(s[0.001,15]*Uniform(x[0,1]),b[1,0,2]*Uniform(x))");
00031   w->factory("Gaussian::prior_b(b,1,1)");
00032   w->factory("PROD::model(pdf,prior_b)");
00033   RooAbsPdf* model = w->pdf("model");  // pdf*priorNuisance
00034   RooArgSet nuisanceParameters(*(w->var("b")));
00035   
00036 
00037 
00038   RooAbsRealLValue* POI = w->var("s");
00039   RooAbsPdf* priorPOI  = (RooAbsPdf *) w->factory("Uniform::priorPOI(s)");  
00040   RooAbsPdf* priorPOI2 = (RooAbsPdf *) w->factory("GenericPdf::priorPOI2('1/sqrt(@0)',s)");
00041 
00042   w->factory("n[3]"); // observed number of events
00043   // create a data set with n observed events
00044   RooDataSet data("data","",RooArgSet(*(w->var("x")),*(w->var("n"))),"n");
00045   data.add(RooArgSet(*(w->var("x"))),w->var("n")->getVal());
00046 
00047   // to suppress messgaes when pdf goes to zero
00048   RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
00049 
00050   RooArgSet * nuisPar = 0;
00051   if (useBkg) nuisPar = &nuisanceParameters;
00052   //if (!useBkg) ((RooRealVar *)w->var("b"))->setVal(0);
00053 
00054   double size = 1.-confLevel;
00055   std::cout << "\nBayesian Result using a Flat prior " << std::endl;
00056   BayesianCalculator bcalc(data,*model,RooArgSet(*POI),*priorPOI, nuisPar);
00057   bcalc.SetTestSize(size);
00058   SimpleInterval* interval = bcalc.GetInterval();
00059   double cl = bcalc.ConfidenceLevel();
00060   std::cout << cl <<"% CL central interval: [ " << interval->LowerLimit() << " - " << interval->UpperLimit() 
00061             << " ] or " 
00062             << cl+(1.-cl)/2 << "% CL limits\n";
00063   RooPlot * plot = bcalc.GetPosteriorPlot();
00064   TCanvas * c1 = new TCanvas("c1","Bayesian Calculator Result");
00065   c1->Divide(1,2); 
00066   c1->cd(1);
00067   plot->Draw();
00068   c1->Update();
00069 
00070   std::cout << "\nBayesian Result using a 1/sqrt(s) prior  " << std::endl;
00071   BayesianCalculator bcalc2(data,*model,RooArgSet(*POI),*priorPOI2,nuisPar);
00072   bcalc2.SetTestSize(size);
00073   SimpleInterval* interval2 = bcalc2.GetInterval();
00074   cl = bcalc2.ConfidenceLevel();
00075   std::cout << cl <<"% CL central interval: [ " << interval2->LowerLimit() << " - " << interval2->UpperLimit() 
00076             << " ] or " 
00077             << cl+(1.-cl)/2 << "% CL limits\n";
00078 
00079   RooPlot * plot2 = bcalc2.GetPosteriorPlot();
00080   c1->cd(2);
00081   plot2->Draw();
00082   gPad->SetLogy();
00083   c1->Update();
00084   
00085   // observe one event while expecting one background event -> the 95% CL upper limit on s is 4.10
00086   // observe one event while expecting zero background event -> the 95% CL upper limit on s is 4.74
00087 }

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