CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/HiggsAnalysis/CombinedLimit/src/BayesianToyMC.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <cmath>
00003 #include "../interface/BayesianToyMC.h"
00004 #include "RooRealVar.h"
00005 #include "RooArgSet.h"
00006 #include "RooUniform.h"
00007 #include "RooWorkspace.h"
00008 #include "RooStats/BayesianCalculator.h"
00009 #include "RooStats/SimpleInterval.h"
00010 #include "RooStats/ModelConfig.h"
00011 #include "../interface/Combine.h"
00012 #include "../interface/utils.h"
00013 
00014 using namespace RooStats;
00015 
00016 int BayesianToyMC::numIters_ = 1000;
00017 std::string BayesianToyMC::integrationType_ = "toymc";
00018 unsigned int BayesianToyMC::tries_ = 1;
00019 float BayesianToyMC::hintSafetyFactor_ = 5.;
00020 
00021 BayesianToyMC::BayesianToyMC() :
00022     LimitAlgo("BayesianToyMC specific options")
00023 {
00024     options_.add_options()
00025         ("integrationType", boost::program_options::value<std::string>(&integrationType_)->default_value(integrationType_), "Integration algorithm to use")
00026         ("tries",      boost::program_options::value<unsigned int>(&tries_)->default_value(tries_), "Number of times to run the ToyMC on the same data")
00027         ("numIters,i", boost::program_options::value<int>(&numIters_)->default_value(numIters_),    "Number of iterations or calls used within iteration (0=ROOT Default)")
00028         ("hintSafetyFactor",
00029                 boost::program_options::value<float>(&hintSafetyFactor_)->default_value(hintSafetyFactor_),
00030                 "set range of integration equal to this number of times the hinted limit")
00031         ;
00032 }
00033 
00034 void BayesianToyMC::applyOptions(const boost::program_options::variables_map &vm) {
00035     if (!withSystematics) {
00036         std::cout << "BayesianToyMC: when running with no systematics, BayesianToyMC is identical to BayesianSimple." << std::endl;
00037         tries_    = 1;
00038         numIters_ = 0;
00039         integrationType_ = "";
00040     }
00041 }
00042 bool BayesianToyMC::run(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint) {
00043   RooArgSet  poi(*mc_s->GetParametersOfInterest());
00044 
00045   RooRealVar *r = dynamic_cast<RooRealVar *>(poi.first());
00046   if ((hint != 0) && (*hint > r->getMin())) {
00047     r->setMax(hintSafetyFactor_*(*hint));
00048   }
00049   double rMax = r->getMax();
00050   
00051   std::auto_ptr<RooStats::ModelConfig> mc_noNuis(0);
00052   if (!withSystematics && mc_s->GetNuisanceParameters() != 0) {
00053     mc_noNuis.reset(new RooStats::ModelConfig(w));
00054     mc_noNuis->SetPdf(*mc_s->GetPdf());
00055     mc_noNuis->SetObservables(*mc_s->GetObservables());
00056     mc_noNuis->SetParametersOfInterest(*mc_s->GetParametersOfInterest());
00057     mc_noNuis->SetPriorPdf(*mc_s->GetPriorPdf());
00058     mc_s = mc_noNuis.get();
00059   }
00060   std::auto_ptr<RooAbsPdf> nuisancePdf;
00061   for (;;) {
00062     limit = 0; limitErr = 0; bool rerun = false;
00063     for (unsigned int i = 0; i < tries_; ++i) {
00064         BayesianCalculator bcalc(data, *mc_s);
00065         bcalc.SetLeftSideTailFraction(0);
00066         bcalc.SetConfidenceLevel(cl); 
00067         if (!integrationType_.empty()) bcalc.SetIntegrationType(integrationType_.c_str());
00068         if (numIters_) bcalc.SetNumIters(numIters_);
00069         if (integrationType_ == "toymc") {
00070             if (nuisancePdf.get() == 0)  nuisancePdf.reset(utils::makeNuisancePdf(*mc_s));
00071             bcalc.ForceNuisancePdf(*nuisancePdf);
00072         }
00073         // get the interval
00074         std::auto_ptr<SimpleInterval> bcInterval(bcalc.GetInterval());
00075         if (bcInterval.get() == 0) return false;
00076         double lim = bcInterval->UpperLimit();
00077         // check against bound
00078         if (lim >= 0.5*r->getMax()) { 
00079             std::cout << "Limit " << r->GetName() << " < " << lim << "; " << r->GetName() << " max < " << r->getMax() << std::endl;
00080             if (r->getMax()/rMax > 20) return false;
00081             r->setMax(r->getMax()*2); 
00082             rerun = true; break;
00083         }
00084         // add to running sum(x) and sum(x2)
00085         limit    += lim;
00086         limitErr += lim*lim;
00087         if (tries_ > 1 && verbose > 1) std::cout << " - limit from try " << i << ": " << lim << std::endl;
00088     }
00089     if (rerun) continue;
00090     limit /= tries_; 
00091     limitErr = (tries_ > 1 ? std::sqrt((limitErr/tries_ - limit*limit)/((tries_-1)*tries_)) : 0);
00092     if (verbose > -1) {
00093         std::cout << "\n -- BayesianToyMC -- " << "\n";
00094         if (limitErr > 0) {
00095             std::cout << "Limit: " << r->GetName() << " < " << limit << " +/- " << limitErr << " @ " << cl * 100 << "% CL" << std::endl;
00096         } else {
00097             std::cout << "Limit: " << r->GetName() << " < " << limit << " @ " << cl * 100 << "% CL" << std::endl;
00098         }
00099     }
00100     break;
00101   }
00102   return true;
00103 }