HybridOriginalDemo.C

Go to the documentation of this file.
00001 // Example on how to use the HybridCalculatorOriginal class
00002 // 
00003 // Author: Gregory Schott
00004 #include "RooRandom.h"
00005 #include "RooRealVar.h"
00006 #include "RooGaussian.h"
00007 #include "RooPolynomial.h"
00008 #include "RooArgSet.h"
00009 #include "RooAddPdf.h"
00010 #include "RooDataSet.h"
00011 #include "RooExtendPdf.h"
00012 #include "RooConstVar.h"
00013 
00014 #ifndef __CINT__  // problem including this file with CINT
00015 #include "RooGlobalFunc.h"
00016 #endif
00017 
00018 #include "RooStats/HybridCalculatorOriginal.h"
00019 #include "RooStats/HybridResult.h"
00020 #include "RooStats/HybridPlot.h"
00021 
00022 void HybridOriginalDemo(int ntoys = 1000)
00023 {
00024   //***********************************************************************//
00025   // This macro show an example on how to use RooStats/HybridCalculatorOriginal //
00026   //***********************************************************************//
00027   //
00028   // With this example, you should get: CL_sb = 0.130 and CL_b = 0.946
00029   // (if data had -2lnQ = -3.0742). You can compare to the expected plot:
00030   // http://www-ekp.physik.uni-karlsruhe.de/~schott/roostats/hybridplot_example.png
00031 
00032   using namespace RooFit;
00033   using namespace RooStats;
00034 
00035   /// set RooFit random seed
00036   RooRandom::randomGenerator()->SetSeed(3007);
00037 
00038   /// build the models for background and signal+background
00039   RooRealVar x("x","",-3,3);
00040   RooArgList observables(x); // variables to be generated
00041 
00042   // gaussian signal
00043 //  RooRealVar sig_mean("sig_mean","",0);
00044 //  RooRealVar sig_sigma("sig_sigma","",0.8);
00045 //  RooGaussian sig_pdf("sig_pdf","",x,sig_mean,sig_sigma);
00046   RooGaussian sig_pdf("sig_pdf","",x, RooConst(0.0),RooConst(0.8));
00047   RooRealVar sig_yield("sig_yield","",20,0,300);
00048 
00049   // flat background (extended PDF)
00050 //  RooRealVar bkg_slope("bkg_slope","",0);
00051 //  RooPolynomial bkg_pdf("bkg_pdf","",x,bkg_slope);
00052   RooPolynomial bkg_pdf("bkg_pdf","", x, RooConst(0));
00053   RooRealVar bkg_yield("bkg_yield","",40,0,300);
00054   RooExtendPdf bkg_ext_pdf("bkg_ext_pdf","",bkg_pdf,bkg_yield);
00055 
00056 //  bkg_yield.setConstant(kTRUE);
00057   sig_yield.setConstant(kTRUE);
00058 
00059   // total sig+bkg (extended PDF)
00060   RooAddPdf tot_pdf("tot_pdf","",RooArgList(sig_pdf,bkg_pdf),RooArgList(sig_yield,bkg_yield));
00061 
00062   /// build the prior PDF on the parameters to be integrated
00063   // gaussian contraint on the background yield ( N_B = 40 +/- 10  ie. 25% )
00064   RooGaussian bkg_yield_prior("bkg_yield_prior","",bkg_yield,RooConst(bkg_yield.getVal()),RooConst(10.));
00065 
00066   RooArgSet nuisance_parameters(bkg_yield); // variables to be integrated
00067 
00068   /// generate a data sample
00069   RooDataSet* data = tot_pdf.generate(observables,RooFit::Extended());
00070 
00071   //***********************************************************************//
00072 
00073   /// run HybridCalculator on those inputs
00074 
00075   // use interface from HypoTest calculator by default
00076 
00077   HybridCalculatorOriginal myHybridCalc(*data, tot_pdf , bkg_ext_pdf ,
00078                                    &nuisance_parameters, &bkg_yield_prior);
00079 
00080   // here I use the default test statistics: 2*lnQ (optional)
00081   myHybridCalc.SetTestStatistic(1);
00082   //myHybridCalc.SetTestStatistic(3); // profile likelihood ratio
00083 
00084   myHybridCalc.SetNumberOfToys(ntoys); 
00085   myHybridCalc.UseNuisance(true);
00086 
00087   // for speed up generation (do binned data) 
00088   myHybridCalc.SetGenerateBinned(false); 
00089 
00090   // calculate by running ntoys for the S+B and B hypothesis and retrieve the result
00091   HybridResult* myHybridResult = myHybridCalc.GetHypoTest(); 
00092 
00093   if (! myHybridResult) { 
00094      std::cerr << "\nError returned from Hypothesis test" << std::endl;
00095      return;
00096   }
00097 
00098   /// run 1000 toys without gaussian prior on the background yield
00099   //HybridResult* myHybridResult = myHybridCalc.Calculate(*data,1000,false);
00100 
00101   /// save the toy-MC results to file, this way splitting into sub-batch jobs is possible
00102   //TFile fileOut("some_hybridresult.root","RECREATE");
00103   //fileOut.cd();
00104   //myHybridResult.Write();
00105   //fileOut.Close();
00106 
00107   /// read the results from a file
00108   //TFile fileIn("some_hybridresult.root");
00109   //HybridResult* myOtherHybridResult = (HybridResult*) fileIn.Get("myHybridCalc");
00110 
00111   /// example on how to merge with toy-MC results obtained in another job
00112   //HybridResult* mergedHybridResult = new HybridResult("mergedHybridResult","this object holds merged results");
00113   //mergedHybridResult->Add(myHybridResult);
00114   //mergedHybridResult->Add(myOtherHybridResult);
00115   /// or
00116   //myHybridResult->Add(myOtherHybridResult);
00117 
00118   /// nice plot of the results
00119   HybridPlot* myHybridPlot = myHybridResult->GetPlot("myHybridPlot","Plot of results with HybridCalculatorOriginal",100);
00120   myHybridPlot->Draw();
00121 
00122   /// recover and display the results
00123   double clsb_data = myHybridResult->CLsplusb();
00124   double clb_data = myHybridResult->CLb();
00125   double cls_data = myHybridResult->CLs();
00126   double data_significance = myHybridResult->Significance();
00127   double min2lnQ_data = myHybridResult->GetTestStat_data();
00128 
00129   /// compute the mean expected significance from toys
00130   double mean_sb_toys_test_stat = myHybridPlot->GetSBmean();
00131   myHybridResult->SetDataTestStatistics(mean_sb_toys_test_stat);
00132   double toys_significance = myHybridResult->Significance();
00133 
00134   std::cout << "Completed HybridCalculatorOriginal example:\n";
00135   std::cout << " - -2lnQ = " << min2lnQ_data << endl;
00136   std::cout << " - CL_sb = " << clsb_data << std::endl;
00137   std::cout << " - CL_b  = " << clb_data << std::endl;
00138   std::cout << " - CL_s  = " << cls_data << std::endl;
00139   std::cout << " - significance of data  = " << data_significance << std::endl;
00140   std::cout << " - mean significance of toys  = " << toys_significance << std::endl;
00141 }
00142 
00143 

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