CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
GoodnessOfFit Class Reference

#include <GoodnessOfFit.h>

Inheritance diagram for GoodnessOfFit:
LimitAlgo

Public Member Functions

virtual void applyOptions (const boost::program_options::variables_map &vm)
 
 GoodnessOfFit ()
 
virtual const std::string & name () const
 
virtual bool run (RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint)
 
virtual bool runSaturatedModel (RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint)
 
- Public Member Functions inherited from LimitAlgo
virtual void applyDefaultOptions ()
 
 LimitAlgo ()
 
 LimitAlgo (const char *desc)
 
const
boost::program_options::options_description & 
options () const
 
virtual void setNToys (const int)
 
virtual void setToyNumber (const int)
 
virtual ~LimitAlgo ()
 

Protected Member Functions

RooAbsPdf * makeSaturatedPdf (RooAbsData &data)
 

Protected Attributes

std::vector< RooAbsData * > tempData_
 
- Protected Attributes inherited from LimitAlgo
boost::program_options::options_description options_
 

Static Protected Attributes

static std::string algo_
 
static bool fixedMu_ = false
 
static std::string minimizerAlgo_ = "Minuit2"
 
static int minimizerStrategy_ = 1
 
static float minimizerTolerance_ = 1e-4
 
static float mu_ = 0.0
 

Detailed Description

Do a ML fit of the data with background and signal+background hypothesis and print out diagnostics plots

Author
Giovanni Petrucciani (UCSD)

Definition at line 14 of file GoodnessOfFit.h.

Constructor & Destructor Documentation

GoodnessOfFit::GoodnessOfFit ( )

Definition at line 38 of file GoodnessOfFit.cc.

References algo_, minimizerAlgo_, minimizerStrategy_, minimizerTolerance_, mu_, and LimitAlgo::options_.

38  :
39  LimitAlgo("GoodnessOfFit specific options")
40 {
41  options_.add_options()
42  ("algorithm", boost::program_options::value<std::string>(&algo_), "Goodness of fit algorithm. Currently, the only option is 'saturated' (which works for binned models only)")
43  ("minimizerAlgo", boost::program_options::value<std::string>(&minimizerAlgo_)->default_value(minimizerAlgo_), "Choice of minimizer (Minuit vs Minuit2)")
44  ("minimizerTolerance", boost::program_options::value<float>(&minimizerTolerance_)->default_value(minimizerTolerance_), "Tolerance for minimizer")
45  ("minimizerStrategy", boost::program_options::value<int>(&minimizerStrategy_)->default_value(minimizerStrategy_), "Stragegy for minimizer")
46  ("fixedSignalStrength", boost::program_options::value<float>(&mu_)->default_value(mu_), "Compute the goodness of fit for a fixed signal strength. If not specified, it's left floating")
47  ;
48 }
static float minimizerTolerance_
Definition: GoodnessOfFit.h:30
LimitAlgo()
Definition: LimitAlgo.h:19
static std::string minimizerAlgo_
Definition: GoodnessOfFit.h:29
static int minimizerStrategy_
Definition: GoodnessOfFit.h:31
boost::program_options::options_description options_
Definition: LimitAlgo.h:32
static std::string algo_
Definition: GoodnessOfFit.h:27
static float mu_
Definition: GoodnessOfFit.h:33

Member Function Documentation

void GoodnessOfFit::applyOptions ( const boost::program_options::variables_map &  vm)
virtual

Reimplemented from LimitAlgo.

Definition at line 50 of file GoodnessOfFit.cc.

References algo_, gather_cfg::cout, and fixedMu_.

51 {
52  fixedMu_ = !vm["fixedSignalStrength"].defaulted();
53  if (algo_ == "saturated") std::cout << "Will use saturated models to compute goodness of fit for a binned likelihood" << std::endl;
54  else throw std::invalid_argument("GoodnessOfFit: algorithm "+algo_+" not supported");
55 }
static bool fixedMu_
Definition: GoodnessOfFit.h:34
tuple cout
Definition: gather_cfg.py:121
static std::string algo_
Definition: GoodnessOfFit.h:27
RooAbsPdf * GoodnessOfFit::makeSaturatedPdf ( RooAbsData &  data)
protected

Definition at line 136 of file GoodnessOfFit.cc.

References gather_cfg::cout, data, utils::printRDH(), run_regression::ret, and tempData_.

Referenced by runSaturatedModel().

136  {
137  if (verbose > 1) std::cout << "Generating saturated model for " << data.GetName() << std::endl;
138  RooDataHist *rdh = new RooDataHist(TString::Format("%s_binned", data.GetName()), "", *data.get(), data); tempData_.push_back(rdh);
139  if (verbose > 1) utils::printRDH(rdh);
140  RooHistPdf *hpdf = new RooHistPdf(TString::Format("%s_shape", data.GetName()), "", *rdh->get(), *rdh);
141  RooConstVar *norm = new RooConstVar(TString::Format("%s_norm", data.GetName()), "", data.sumEntries());
142  // we use RooAddPdf because this works with CachingNLL
143  RooAddPdf *ret = new RooAddPdf(TString::Format("%s_saturated", data.GetName()), "", RooArgList(*hpdf), RooArgList(*norm));
144  ret->addOwnedComponents(RooArgSet(*norm));
145  ret->addOwnedComponents(RooArgSet(*hpdf));
146  return ret;
147 }
void printRDH(RooAbsData *data)
Definition: utils.cc:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< RooAbsData * > tempData_
Definition: GoodnessOfFit.h:38
tuple cout
Definition: gather_cfg.py:121
virtual const std::string& GoodnessOfFit::name ( void  ) const
inlinevirtual

Implements LimitAlgo.

Definition at line 18 of file GoodnessOfFit.h.

18  {
19  static const std::string name("GoodnessOfFit");
20  return name;
21  }
virtual const std::string & name() const
Definition: GoodnessOfFit.h:18
bool GoodnessOfFit::run ( RooWorkspace *  w,
RooStats::ModelConfig *  mc_s,
RooStats::ModelConfig *  mc_b,
RooAbsData &  data,
double &  limit,
double &  limitErr,
const double *  hint 
)
virtual

Implements LimitAlgo.

Definition at line 57 of file GoodnessOfFit.cc.

References algo_, fixedMu_, minimizerAlgo_, minimizerTolerance_, mu_, alignCSCRings::r, and runSaturatedModel().

57  {
59 
60  RooRealVar *r = dynamic_cast<RooRealVar *>(mc_s->GetParametersOfInterest()->first());
61  if (fixedMu_) { r->setVal(mu_); r->setConstant(true); }
62  if (algo_ == "saturated") return runSaturatedModel(w, mc_s, mc_b, data, limit, limitErr, hint);
63  return false;
64 }
static bool fixedMu_
Definition: GoodnessOfFit.h:34
static float minimizerTolerance_
Definition: GoodnessOfFit.h:30
virtual bool runSaturatedModel(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint)
static std::string minimizerAlgo_
Definition: GoodnessOfFit.h:29
Setup Minimizer configuration on creation, reset the previous one on destruction. ...
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static std::string algo_
Definition: GoodnessOfFit.h:27
T w() const
static float mu_
Definition: GoodnessOfFit.h:33
bool GoodnessOfFit::runSaturatedModel ( RooWorkspace *  w,
RooStats::ModelConfig *  mc_s,
RooStats::ModelConfig *  mc_b,
RooAbsData &  data,
double &  limit,
double &  limitErr,
const double *  hint 
)
virtual

Definition at line 66 of file GoodnessOfFit.cc.

References CloseCoutSentry::clear(), createBeamHaloJobs::constraints, gather_cfg::cout, RecoTau_DiTaus_pt_20-420_cfg::datasets, utils::factorizePdf(), i, makeSaturatedPdf(), minimizerStrategy_, n, pileupCalc::nbins, and tempData_.

Referenced by run().

66  {
67  RooAbsPdf *pdf_nominal = mc_s->GetPdf();
68  // now I need to make the saturated pdf
69  std::auto_ptr<RooAbsPdf> saturated;
70  // factorize away constraints anyway
71  RooArgList constraints;
72  RooAbsPdf *obsOnlyPdf = utils::factorizePdf(*mc_s->GetObservables(), *pdf_nominal, constraints);
73  // case 1:
74  RooSimultaneous *sim = dynamic_cast<RooSimultaneous *>(obsOnlyPdf);
75  if (sim) {
76  RooAbsCategoryLValue *cat = (RooAbsCategoryLValue *) sim->indexCat().Clone();
77  std::auto_ptr<TList> datasets(data.split(*cat, true));
78  int nbins = cat->numBins((const char *)0);
79  RooArgSet newPdfs;
80  TString satname = TString::Format("%s_saturated", sim->GetName());
81  RooSimultaneous *satsim = (typeid(*sim) == typeid(RooSimultaneousOpt)) ? new RooSimultaneousOpt(satname, "", *cat) : new RooSimultaneous(satname, "", *cat);
82  for (int ic = 0, nc = nbins; ic < nc; ++ic) {
83  cat->setBin(ic);
84  RooAbsPdf *pdfi = sim->getPdf(cat->getLabel());
85  if (pdfi == 0) continue;
86  RooAbsData *datai = (RooAbsData *) datasets->FindObject(cat->getLabel());
87  if (datai == 0) throw std::runtime_error(std::string("Error: missing dataset for category label ")+cat->getLabel());
88  RooAbsPdf *saturatedPdfi = makeSaturatedPdf(*datai);
89  //delete datai;
90  if (constraints.getSize() > 0) {
91  RooArgList terms(constraints); terms.add(*saturatedPdfi);
92  RooProdPdf *prodpdf = new RooProdPdf(TString::Format("%s_constr", saturatedPdfi->GetName()), "", terms);
93  prodpdf->addOwnedComponents(RooArgSet(*saturatedPdfi));
94  saturatedPdfi = prodpdf;
95  }
96  satsim->addPdf(*saturatedPdfi, cat->getLabel());
97  satsim->addOwnedComponents(RooArgSet(*saturatedPdfi));
98  }
99  saturated.reset(satsim);
100  } else {
101  RooAbsPdf *saturatedPdfi = makeSaturatedPdf(data);
102  if (constraints.getSize() > 0) {
103  RooArgList terms(constraints); terms.add(*saturatedPdfi);
104  RooProdPdf *prodpdf = new RooProdPdf(TString::Format("%s_constr", saturatedPdfi->GetName()), "", terms);
105  prodpdf->addOwnedComponents(RooArgSet(*saturatedPdfi));
106  saturatedPdfi = prodpdf;
107  }
108  saturated.reset(saturatedPdfi);
109  }
110 
111  CloseCoutSentry sentry(verbose < 2);
112  // let's assume fits converge, for a while
113  const RooCmdArg &minim = RooFit::Minimizer(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(),
114  ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
115  std::auto_ptr<RooFitResult> result_nominal(pdf_nominal->fitTo(data, RooFit::Save(1), minim, RooFit::Strategy(minimizerStrategy_), RooFit::Hesse(0), RooFit::Constrain(*mc_s->GetNuisanceParameters())));
116  std::auto_ptr<RooFitResult> result_saturated(saturated->fitTo(data, RooFit::Save(1), minim, RooFit::Strategy(minimizerStrategy_), RooFit::Hesse(0), RooFit::Constrain(*mc_s->GetNuisanceParameters())));
117  sentry.clear();
118 
119  saturated.reset();
120  for (int i = 0, n = tempData_.size(); i < n; ++i) delete tempData_[i];
121  tempData_.clear();
122 
123  if (result_nominal.get() == 0) return false;
124  if (result_saturated.get() == 0) return false;
125 
126  double nll_nominal = result_nominal->minNll();
127  double nll_saturated = result_saturated->minNll();
128  if (fabs(nll_nominal) > 1e10 || fabs(nll_saturated) > 1e10) return false;
129  limit = 2*(nll_nominal-nll_saturated);
130 
131  std::cout << "\n --- GoodnessOfFit --- " << std::endl;
132  std::cout << "Best fit test statistic: " << limit << std::endl;
133  return true;
134 }
int i
Definition: DBlmapReader.cc:9
RooAbsPdf * makeSaturatedPdf(RooAbsData &data)
Definition: sim.h:19
RooAbsPdf * factorizePdf(const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &constraints)
Definition: utils.cc:80
static int minimizerStrategy_
Definition: GoodnessOfFit.h:31
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< RooAbsData * > tempData_
Definition: GoodnessOfFit.h:38
tuple cout
Definition: gather_cfg.py:121

Member Data Documentation

std::string GoodnessOfFit::algo_
staticprotected

Definition at line 27 of file GoodnessOfFit.h.

Referenced by applyOptions(), GoodnessOfFit(), and run().

bool GoodnessOfFit::fixedMu_ = false
staticprotected

Definition at line 34 of file GoodnessOfFit.h.

Referenced by applyOptions(), and run().

std::string GoodnessOfFit::minimizerAlgo_ = "Minuit2"
staticprotected

Definition at line 29 of file GoodnessOfFit.h.

Referenced by GoodnessOfFit(), and run().

int GoodnessOfFit::minimizerStrategy_ = 1
staticprotected

Definition at line 31 of file GoodnessOfFit.h.

Referenced by GoodnessOfFit(), and runSaturatedModel().

float GoodnessOfFit::minimizerTolerance_ = 1e-4
staticprotected

Definition at line 30 of file GoodnessOfFit.h.

Referenced by GoodnessOfFit(), and run().

float GoodnessOfFit::mu_ = 0.0
staticprotected

Definition at line 33 of file GoodnessOfFit.h.

Referenced by GoodnessOfFit(), and run().

std::vector<RooAbsData*> GoodnessOfFit::tempData_
mutableprotected

Definition at line 38 of file GoodnessOfFit.h.

Referenced by makeSaturatedPdf(), and runSaturatedModel().