CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FeldmanCousins.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include "../interface/FeldmanCousins.h"
3 #include "../interface/Combine.h"
4 #include <RooRealVar.h>
5 #include <RooArgSet.h>
6 #include <RooArgSet.h>
7 #include <RooWorkspace.h>
8 #include <RooDataHist.h>
9 #include <RooStats/ModelConfig.h>
10 #include <RooStats/FeldmanCousins.h>
11 #include <RooStats/PointSetInterval.h>
12 
16 
18  LimitAlgo("FeldmanCousins specific options") {
19  options_.add_options()
20  ("rAbsAcc", boost::program_options::value<float>(&rAbsAccuracy_)->default_value(rAbsAccuracy_), "Absolute accuracy on r to reach to terminate the scan")
21  ("rRelAcc", boost::program_options::value<float>(&rRelAccuracy_)->default_value(rRelAccuracy_), "Relative accuracy on r to reach to terminate the scan")
22  ("toysFactor", boost::program_options::value<float>(&toysFactor_)->default_value(toysFactor_), "Increase the toys per point by this factor w.r.t. the minimum from adaptive sampling")
23  ;
24 }
25 
26 void FeldmanCousins::applyOptions(const boost::program_options::variables_map &vm)
27 {
28 }
29 
30 bool FeldmanCousins::run(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint) {
31  RooArgSet poi(*mc_s->GetParametersOfInterest());
32  RooRealVar *r = dynamic_cast<RooRealVar *>(poi.first());
33 
34  if ((hint != 0) && (*hint > r->getMin())) {
35  r->setMax(std::min<double>(3*(*hint), r->getMax()));
36  }
37 
38  RooStats::ModelConfig modelConfig(*mc_s);
39  modelConfig.SetSnapshot(poi);
40 
41  RooStats::FeldmanCousins fc(data, modelConfig);
42  fc.FluctuateNumDataEntries(mc_s->GetPdf()->canBeExtended());
43  fc.UseAdaptiveSampling(true);
44  fc.SetConfidenceLevel(cl);
45  fc.AdditionalNToysFactor(toysFactor_);
46 
47  fc.SetNBins(10);
48  do {
49  if (verbose > 1) std::cout << "scan in range [" << r->getMin() << ", " << r->getMax() << "]" << std::endl;
50  std::auto_ptr<RooStats::PointSetInterval> fcInterval((RooStats::PointSetInterval *)fc.GetInterval());
51  if (fcInterval.get() == 0) return false;
52  if (verbose > 1) fcInterval->GetParameterPoints()->Print("V");
53  RooDataHist* parameterScan = (RooDataHist*) fc.GetPointsToScan();
54  int found = -1;
55  if (lowerLimit_) {
56  for(int i=0; i<parameterScan->numEntries(); ++i){
57  const RooArgSet * tmpPoint = parameterScan->get(i);
58  if (!fcInterval->IsInInterval(*tmpPoint)) found = i;
59  else break;
60  }
61  } else {
62  for(int i=0; i<parameterScan->numEntries(); ++i){
63  const RooArgSet * tmpPoint = parameterScan->get(i);
64  bool inside = fcInterval->IsInInterval(*tmpPoint);
65  if (inside) found = i;
66  else if (found != -1) break;
67  }
68  if (found == -1) {
69  if (verbose) std::cout << "Points are either all inside or all outside the bound." << std::endl;
70  return false;
71  }
72  }
73  double fcBefore = (found > -1 ? parameterScan->get(found)->getRealValue(r->GetName()) : r->getMin());
74  double fcAfter = (found < parameterScan->numEntries()-1 ?
75  parameterScan->get(found+1)->getRealValue(r->GetName()) : r->getMax());
76  limit = 0.5*(fcAfter+fcBefore);
77  limitErr = 0.5*(fcAfter-fcBefore);
78  if (verbose > 0) std::cout << " would be " << r->GetName() << " < " << limit << " +/- "<<limitErr << std::endl;
79  r->setMin(std::max(r->getMin(), limit-3*limitErr));
80  r->setMax(std::min(r->getMax(), limit+3*limitErr));
81  if (limitErr < 4*std::max<float>(rAbsAccuracy_, rRelAccuracy_ * limit)) { // make last scan more precise
82  fc.AdditionalNToysFactor(4*toysFactor_);
83  }
84  } while (limitErr > std::max<float>(rAbsAccuracy_, rRelAccuracy_ * limit));
85 
86  if (verbose > -1) {
87  std::cout << "\n -- FeldmanCousins++ -- \n";
88  std::cout << "Limit: " << r->GetName() << (lowerLimit_ ? "> " : "< ") << limit << " +/- " << limitErr << " @ " << cl * 100 << "% CL" << std::endl;
89  }
90  return true;
91 }
int i
Definition: DBlmapReader.cc:9
bool lowerLimit_
Definition: Combine.cc:70
#define min(a, b)
Definition: mlp_lapack.h:161
const T & max(const T &a, const T &b)
virtual bool run(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint)
float cl
Definition: Combine.cc:71
virtual void applyOptions(const boost::program_options::variables_map &vm)
static float rRelAccuracy_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple cout
Definition: gather_cfg.py:121
static float rAbsAccuracy_
static float toysFactor_
boost::program_options::options_description options_
Definition: LimitAlgo.h:31
T w() const