CMS 3D CMS Logo

Functions

nllutils Namespace Reference

Functions

bool robustMinimize (RooAbsReal &nll, RooMinimizerOpt &minimizer, int verbosity=0)

Function Documentation

bool nllutils::robustMinimize ( RooAbsReal &  nll,
RooMinimizerOpt minimizer,
int  verbosity = 0 
)

Definition at line 383 of file ProfiledLikelihoodRatioTestStatExt.cc.

References COUNT_ONE, DBG, RooMinimizerOpt::edm(), runtimedef::get(), run_regression::ret, and ntuplemaker::status.

Referenced by CascadeMinimizer::improveOnce(), ProfileLikelihood::upperLimitBruteForce(), and ProfileLikelihood::upperLimitWithMinos().

{
    static bool do_debug = (runtimedef::get("DEBUG_MINIM") || runtimedef::get("DEBUG_PLTSO") > 1);
    double initialNll = nll.getVal();
    std::auto_ptr<RooArgSet> pars;
    bool ret = false;
    for (int tries = 0, maxtries = 4; tries <= maxtries; ++tries) {
        int status = minim.minimize(ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str(), ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
        //if (verbosity > 1) res->Print("V");
        if (status == 0 && nll.getVal() > initialNll + 0.02) {
            std::auto_ptr<RooFitResult> res(minim.save());
            //PerfCounter::add("Minimizer.save() called for false minimum"); 
            DBG(DBG_PLTestStat_main, (printf("\n  --> false minimum, status %d, cov. quality %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->covQual(), res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal())))
            if (pars.get() == 0) pars.reset(nll.getParameters((const RooArgSet*)0));
            *pars = res->floatParsInit();
            if (tries == 0) {
                COUNT_ONE("nllutils::robustMinimize: false minimum (first try)")
                DBG(DBG_PLTestStat_main, (printf("    ----> Doing a re-scan and re-trying\n")))
                minim.minimize("Minuit2","Scan");
            } else if (tries == 1) {
                COUNT_ONE("nllutils::robustMinimize: false minimum (second try)")
                DBG(DBG_PLTestStat_main, (printf("    ----> Re-trying with strategy = 1\n")))
                minim.setStrategy(1);
            } else if (tries == 2) {
                COUNT_ONE("nllutils::robustMinimize: false minimum (third try)")
                DBG(DBG_PLTestStat_main, (printf("    ----> Re-trying with strategy = 2\n")))
                minim.setStrategy(2);
            } else  {
                COUNT_ONE("nllutils::robustMinimize: false minimum (third try)")
                DBG(DBG_PLTestStat_main, (printf("    ----> Last attempt: simplex method \n")))
                status = minim.minimize("Minuit2","Simplex");
                if (nll.getVal() < initialNll + 0.02) {
                    DBG(DBG_PLTestStat_main, (printf("\n  --> success: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal())))
                    if (do_debug) printf("\n  --> success: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal());
                    COUNT_ONE("nllutils::robustMinimize: final success")
                    ret = true;
                    break;
                } else {
                    COUNT_ONE("nllutils::robustMinimize: final fail")
                    DBG(DBG_PLTestStat_main, (printf("\n  --> final fail: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal())))
                    if (do_debug) printf("\n  --> final fail: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal());
                    return false;
                }
            }
        } else if (status == 0) {  
            DBG(DBG_PLTestStat_main, (printf("\n  --> success: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal())))
            if (do_debug) printf("\n  --> success: status %d, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, initialNll, nll.getVal(), initialNll - nll.getVal());
            COUNT_ONE("nllutils::robustMinimize: final success")
            ret = true;
            break;
        } else if (tries != maxtries) {
            std::auto_ptr<RooFitResult> res(do_debug ? minim.save() : 0);
            //PerfCounter::add("Minimizer.save() called for failed minimization"); 
            if (tries > 0 && minim.edm() < 0.05*ROOT::Math::MinimizerOptions::DefaultTolerance()) {
                DBG(DBG_PLTestStat_main, (printf("\n  --> acceptable: status %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal())))
                if (do_debug) printf("\n  --> acceptable: status %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal());
                COUNT_ONE("nllutils::robustMinimize: accepting fit with bad status but good EDM")
                COUNT_ONE("nllutils::robustMinimize: final success")
                ret = true;
                break;
            }
            DBG(DBG_PLTestStat_main, (printf("\n  --> partial fail: status %d, cov. quality %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->covQual(), res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal())))
            if (tries == 1) {
                COUNT_ONE("nllutils::robustMinimize: failed first attempt")
                DBG(DBG_PLTestStat_main, (printf("    ----> Doing a re-scan first, and switching to strategy 1\n")))
                minim.minimize("Minuit2","Scan");
                minim.setStrategy(1);
            }
            if (tries == 2) {
                COUNT_ONE("nllutils::robustMinimize: failed second attempt")
                DBG(DBG_PLTestStat_main, (printf("    ----> trying with strategy = 2\n")))
                minim.minimize("Minuit2","Scan");
                minim.setStrategy(2);
            }
        } else {
            std::auto_ptr<RooFitResult> res(do_debug ? minim.save() : 0);
            DBG(DBG_PLTestStat_main, (printf("\n  --> final fail: status %d, cov. quality %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->covQual(), res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal())))
            if (do_debug) printf("\n  --> final fail: status %d, cov. quality %d, edm %10.7f, nll initial % 10.4f, nll final % 10.4f, change %10.5f\n", status, res->covQual(), res->edm(), initialNll, nll.getVal(), initialNll - nll.getVal());
            COUNT_ONE("nllutils::robustMinimize: final fail")
        }
    }
    return ret;
}