1 #include "../interface/MaxLikelihoodFit.h"
2 #include "RooRealVar.h"
5 #include "RooDataSet.h"
6 #include "RooFitResult.h"
7 #include "RooSimultaneous.h"
9 #include "RooProdPdf.h"
10 #include "RooConstVar.h"
13 #include <RooMinimizer.h>
18 #include <RooStats/ModelConfig.h>
19 #include "../interface/Combine.h"
20 #include "../interface/ProfileLikelihood.h"
21 #include "../interface/ProfiledLikelihoodRatioTestStatExt.h"
22 #include "../interface/CloseCoutSentry.h"
23 #include "../interface/utils.h"
26 #include <Math/MinimizerOptions.h>
28 using namespace RooStats;
47 (
"minos", boost::program_options::value<std::string>(&
minos_)->default_value(
minos_),
"Compute MINOS errors for: 'none', 'poi', 'all'")
48 (
"out", boost::program_options::value<std::string>(&
out_)->default_value(
out_),
"Directory to put output in")
49 (
"plots",
"Make plots")
50 (
"rebinFactor", boost::program_options::value<float>(&
rebinFactor_)->default_value(
rebinFactor_),
"Rebin by this factor before plotting (does not affect fitting!)")
51 (
"signalPdfNames", boost::program_options::value<std::string>(&
signalPdfNames_)->default_value(
signalPdfNames_),
"Names of signal pdfs in plots (separated by ,)")
53 (
"saveNormalizations",
"Save post-fit normalizations of all components of the pdfs")
54 (
"justFit",
"Just do the S+B fit, don't do the B-only one, don't save output file")
55 (
"noErrors",
"Don't compute uncertainties on the best fit value")
56 (
"initFromBonly",
"Use the vlaues of the nuisance parameters from the background only fit as the starting point for the s+b fit")
79 name_ = vm[
"name"].defaulted() ? std::string() : vm[
"name"].as<std::string>();
91 std::cout <<
"Cannot reuse b-only fit params when running minos. Parameters will be reset when running S+B fit"<<std::endl;
97 fitOut.reset(TFile::Open((
out_+
"/mlfit"+
name_+
".root").c_str(),
"RECREATE"));
102 RooRealVar *
r =
dynamic_cast<RooRealVar *
>(mc_s->GetParametersOfInterest()->first());
107 c1 =
new TCanvas(
"c1",
"c1");
114 for (std::vector<RooPlot *>::iterator it = plots.begin(), ed = plots.end(); it != ed; ++it) {
116 c1->Print((
out_+
"/"+(*it)->GetName()+
"_prefit.png").c_str());
117 if (
fitOut.get() &&
currentToy_< 1)
fitOut->WriteTObject(*it, (std::string((*it)->GetName())+
"_prefit").c_str());
124 const RooArgSet *nuis = mc_s->GetNuisanceParameters();
125 const RooArgSet *globalObs = mc_s->GetGlobalObservables();
126 if (!
justFit_ && nuis && globalObs ) {
128 std::auto_ptr<RooDataSet> globalData(
new RooDataSet(
"globalData",
"globalData", *globalObs));
129 globalData->add(*globalObs);
130 RooFitResult *res_prefit = 0;
133 res_prefit = nuisancePdf->fitTo(*globalData,
135 RooFit::Minimizer(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(), ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str()),
137 RooFit::Minos(
minos_ ==
"all")
140 if (
fitOut.get() )
fitOut->WriteTObject(res_prefit,
"nuisances_prefit_res");
141 if (
fitOut.get() )
fitOut->WriteTObject(nuis->snapshot(),
"nuisances_prefit");
148 if (
fitOut.get() )
fitOut->WriteTObject(nuis->snapshot(),
"nuisances_prefit");
152 RooFitResult *res_b = 0, *res_s = 0;
153 const RooCmdArg &constCmdArg_s =
withSystematics ? RooFit::Constrain(*mc_s->GetNuisanceParameters()) : RooFit::NumCPU(1);
154 const RooCmdArg &minosCmdArg =
minos_ ==
"poi" ? RooFit::Minos(*mc_s->GetParametersOfInterest()) : RooFit::Minos(
minos_ !=
"none");
155 w->loadSnapshot(
"clean");
156 r->setVal(0.0); r->setConstant(
true);
159 if (
currentToy_<1)
nll.reset(mc_s->GetPdf()->createNLL(data,constCmdArg_s,RooFit::Extended(mc_s->GetPdf()->canBeExtended())));
161 double nll0 =
nll->getVal();
165 }
else if (
minos_ !=
"all") {
167 res_b =
doFit(*mc_s->GetPdf(),
data, minos, constCmdArg_s,
true,
true);
171 res_b = mc_s->GetPdf()->fitTo(data,
173 RooFit::Minimizer(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(), ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str()),
175 RooFit::Extended(mc_s->GetPdf()->canBeExtended()),
176 constCmdArg_s, minosCmdArg
183 if (
verbose > 1) res_b->Print(
"V");
194 for (std::vector<RooPlot *>::iterator it = plots.begin(), ed = plots.end(); it != ed; ++it) {
195 c1->cd(); (*it)->Draw();
196 c1->Print((
out_+
"/"+(*it)->GetName()+
"_fit_b.png").c_str());
197 if (
fitOut.get() &&
currentToy_< 1)
fitOut->WriteTObject(*it, (std::string((*it)->GetName())+
"_fit_b").c_str());
202 RooArgSet *norms =
new RooArgSet();
203 norms->setName(
"norm_fit_b");
205 if (
fitOut.get())
fitOut->WriteTObject(norms,
"norm_fit_b");
210 TH2 *
corr = res_b->correlationHist();
211 c1->SetLeftMargin(0.25); c1->SetBottomMargin(0.25);
212 corr->SetTitle(
"Correlation matrix of fit parameters");
213 gStyle->SetPaintTextFormat(res_b->floatParsFinal().getSize() > 10 ?
".1f" :
".2f");
214 gStyle->SetOptStat(0);
215 corr->SetMarkerSize(res_b->floatParsFinal().getSize() > 10 ? 2 : 1);
216 corr->Draw(
"COLZ TEXT");
217 c1->Print((
out_+
"/covariance_fit_b.png").c_str());
218 c1->SetLeftMargin(0.16); c1->SetBottomMargin(0.13);
219 if (
fitOut.get())
fitOut->WriteTObject(corr,
"covariance_fit_b");
234 RooArgList minos;
if (
minos_ ==
"poi") minos.add(*r);
239 res_s = mc_s->GetPdf()->fitTo(data,
241 RooFit::Minimizer(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(), ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str()),
243 RooFit::Extended(mc_s->GetPdf()->canBeExtended()),
244 constCmdArg_s, minosCmdArg
251 limitErr = r->getError();
252 if (
verbose > 1) res_s->Print(
"V");
267 for (std::vector<RooPlot *>::iterator it = plots.begin(), ed = plots.end(); it != ed; ++it) {
268 c1->cd(); (*it)->Draw();
269 c1->Print((
out_+
"/"+(*it)->GetName()+
"_fit_s.png").c_str());
270 if (
fitOut.get() &&
currentToy_< 1)
fitOut->WriteTObject(*it, (std::string((*it)->GetName())+
"_fit_s").c_str());
275 RooArgSet *norms =
new RooArgSet();
276 norms->setName(
"norm_fit_s");
278 if (
fitOut.get() )
fitOut->WriteTObject(norms,
"norm_fit_s");
283 TH2 *
corr = res_s->correlationHist();
284 c1->SetLeftMargin(0.25); c1->SetBottomMargin(0.25);
285 corr->SetTitle(
"Correlation matrix of fit parameters");
286 gStyle->SetPaintTextFormat(res_s->floatParsFinal().getSize() > 10 ?
".1f" :
".2f");
287 gStyle->SetOptStat(0);
288 corr->SetMarkerSize(res_s->floatParsFinal().getSize() > 10 ? 2 : 1);
289 corr->Draw(
"COLZ TEXT");
290 c1->Print((
out_+
"/covariance_fit_s.png").c_str());
291 c1->SetLeftMargin(0.16); c1->SetBottomMargin(0.13);
292 if (
fitOut.get() )
fitOut->WriteTObject(corr,
"covariance_fit_s");
303 RooRealVar *rf =
dynamic_cast<RooRealVar*
>(res_s->floatParsFinal().find(r->GetName()));
304 double bestFitVal = rf->getVal();
306 double hiErr = +(rf->hasRange(
"err68") ? rf->getMax(
"err68") - bestFitVal : rf->getAsymErrorHi());
307 double loErr = -(rf->hasRange(
"err68") ? rf->getMin(
"err68") - bestFitVal : rf->getAsymErrorLo());
308 double maxError = std::max<double>(std::max<double>(hiErr, loErr), rf->getError());
310 if (fabs(hiErr) < 0.001*maxError) hiErr = -bestFitVal + rf->getMax();
311 if (fabs(loErr) < 0.001*maxError) loErr = +bestFitVal - rf->getMin();
313 double hiErr95 = +(
do95_ && rf->hasRange(
"err95") ? rf->getMax(
"err95") - bestFitVal : 0);
314 double loErr95 = -(
do95_ && rf->hasRange(
"err95") ? rf->getMin(
"err95") - bestFitVal : 0);
316 limit = bestFitVal; limitErr = 0;
318 limit = bestFitVal - loErr; limitErr = 0;
320 limit = bestFitVal + hiErr; limitErr = 0;
329 std::cout <<
"\n --- MaxLikelihoodFit ---" << std::endl;
330 std::cout <<
"Best fit " << r->GetName() <<
": " << rf->getVal() <<
" "<< -loErr <<
"/+" << +hiErr <<
" (68% CL)" << std::endl;
332 std::cout <<
" " << r->GetName() <<
": " << rf->getVal() <<
" "<< -loErr95 <<
"/+" << +hiErr95 <<
" (95% CL)" << std::endl;
335 std::cout <<
"\n --- MaxLikelihoodFit ---" << std::endl;
344 fitOut.release()->Close();
348 bool fitreturn = (res_s!=0);
356 RooSimultaneous *
sim =
dynamic_cast<RooSimultaneous *
>(pdf);
358 RooAbsCategoryLValue &cat =
const_cast<RooAbsCategoryLValue &
>(sim->indexCat());
359 for (
int i = 0,
n = cat.numBins((
const char *)0);
i <
n; ++
i) {
361 RooAbsPdf *pdfi = sim->getPdf(cat.getLabel());
366 RooProdPdf *
prod =
dynamic_cast<RooProdPdf *
>(pdf);
368 RooArgList
list(prod->pdfList());
369 for (
int i = 0,
n =
list.getSize();
i <
n; ++
i) {
370 RooAbsPdf *pdfi = (RooAbsPdf *)
list.at(
i);
375 RooAddPdf *
add =
dynamic_cast<RooAddPdf *
>(pdf);
377 RooArgList
list(add->coefList());
378 for (
int i = 0,
n =
list.getSize();
i <
n; ++
i) {
379 RooAbsReal *coeff = (RooAbsReal *)
list.at(
i);
380 out.addOwned(*(
new RooConstVar(coeff->GetName(),
"", coeff->getVal())));
389 TIterator* iter(args->createIterator());
392 for (TObject *
a = iter->Next();
a != 0;
a = iter->Next()) {
393 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
394 std::string
name = rrv->GetName();
395 vals[
count]=rrv->getVal();
407 t_fit_b_ =
new TTree(
"tree_fit_b",
"tree_fit_b");
408 t_fit_sb_ =
new TTree(
"tree_fit_sb",
"tree_fit_sb");
425 const RooArgSet *cons = mc.GetGlobalObservables();
426 const RooArgSet *nuis = mc.GetNuisanceParameters();
432 TIterator* iter_c(cons->createIterator());
433 for (TObject *
a = iter_c->Next();
a != 0;
a = iter_c->Next()) {
434 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
435 std::string
name = rrv->GetName();
443 TIterator* iter_n(nuis->createIterator());
444 for (TObject *
a = iter_n->Next();
a != 0;
a = iter_n->Next()) {
445 RooRealVar *rrv =
dynamic_cast<RooRealVar *
>(
a);
446 std::string
name = rrv->GetName();
452 std::cout <<
"Created Branches" <<std::endl;
void createFitResultTrees(const RooStats::ModelConfig &)
virtual void applyOptions(const boost::program_options::variables_map &vm)
void applyOptionsBase(const boost::program_options::variables_map &vm)
double * nuisanceParameters_
void setFitResultTrees(const RooArgSet *, double *)
std::vector< RooPlot * > makePlots(const RooAbsPdf &pdf, const RooAbsData &data, const char *signalSel=0, const char *backgroundSel=0, float rebinFactor=1.0)
make plots, if possible
static bool saveNormalizations_
virtual void setNToys(const int)
void tdrStyle()
set style for plots
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
std::auto_ptr< TFile > fitOut
RooAbsPdf * makeNuisancePdf(RooStats::ModelConfig &model, const char *name="nuisancePdf")
static std::string backgroundPdfNames_
RooFitResult * doFit(RooAbsPdf &pdf, RooAbsData &data, RooRealVar &r, const RooCmdArg &constrain, bool doHesse=true, int ndim=1, bool reuseNLL=false)
static float rebinFactor_
static void commitPoint(bool expected, float quantile)
Save a point into the output tree. Usually if expected = false, quantile should be set to -1 (except ...
static float preFitValue_
std::auto_ptr< RooAbsReal > nll
static int minimizerStrategy_
virtual void setToyNumber(const int)
static std::string minos_
virtual bool runSpecific(RooWorkspace *w, RooStats::ModelConfig *mc_s, RooStats::ModelConfig *mc_b, RooAbsData &data, double &limit, double &limitErr, const double *hint)
static std::string signalPdfNames_
char data[epos_bytes_allocation]
double * globalObservables_
boost::program_options::options_description options_
virtual const std::string & name() const
void getNormalizations(RooAbsPdf *pdf, const RooArgSet &obs, RooArgSet &out)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run