CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/HiggsAnalysis/CombinedLimit/src/BestFitSigmaTestStat.cc

Go to the documentation of this file.
00001 #include "../interface/ProfiledLikelihoodRatioTestStatExt.h"
00002 #include "../interface/BestFitSigmaTestStat.h"
00003 #include "../interface/CascadeMinimizer.h"
00004 #include "../interface/CloseCoutSentry.h"
00005 #include "../interface/utils.h"
00006 #include <stdexcept>
00007 #include <RooRealVar.h>
00008 #include "../interface/RooMinimizerOpt.h"
00009 #include <RooFitResult.h>
00010 #include <RooSimultaneous.h>
00011 #include <RooCategory.h>
00012 #include <RooRandom.h>
00013 #include <Math/MinimizerOptions.h>
00014 #include <RooStats/RooStatsUtils.h>
00015 #include "../interface/ProfilingTools.h"
00016 
00017 //---- Uncomment this and run with --perfCounters to get statistics of successful and failed fits
00018 #define DEBUG_FIT_STATUS
00019 #ifdef DEBUG_FIT_STATUS
00020 #define  COUNT_ONE(x) PerfCounter::add(x);
00021 #else
00022 #define  COUNT_ONE(X) 
00023 #endif
00024 
00025 //---- Uncomment this and set some of these to 1 to get more debugging
00026 #if 0
00027 #define DBG(X,Z) if (X) { Z; }
00028 #define DBGV(X,Z) if (X>1) { Z; }
00029 #define DBG_TestStat_params 0 // ProfiledLikelihoodRatioTestStatOpt::Evaluate; 1 = dump nlls; 2 = dump params at each eval
00030 #define DBG_TestStat_NOFIT  0 // FIXME HACK: if set, don't profile the likelihood, just evaluate it
00031 #define DBG_PLTestStat_ctor 0 // dump parameters in c-tor
00032 #define DBG_PLTestStat_pars 0 // dump parameters in eval
00033 #define DBG_PLTestStat_fit  0 // dump fit result
00034 #define DBG_PLTestStat_main 1 // limited debugging (final NLLs, failed fits)
00035 #else
00036 #define DBG(X,Z) 
00037 #define DBGV(X,Z) 
00038 #endif
00039 
00040 
00041 //============================================================================================================================================
00042 BestFitSigmaTestStat::BestFitSigmaTestStat(
00043     const RooArgSet & observables,
00044     RooAbsPdf &pdf, 
00045     const RooArgSet *nuisances, 
00046     const RooArgSet & params,
00047     int verbosity)
00048 :
00049     pdf_(&pdf),
00050     verbosity_(verbosity)
00051 {
00052     params.snapshot(snap_,false);
00053     ((RooRealVar*)snap_.find(params.first()->GetName()))->setConstant(false);
00054     poi_.add(snap_);
00055     if (nuisances) { nuisances_.add(*nuisances); snap_.addClone(*nuisances, /*silent=*/true); }
00056     params_.reset(pdf_->getParameters(observables));
00057 }
00058 
00059 
00060 Double_t BestFitSigmaTestStat::Evaluate(RooAbsData& data, RooArgSet& /*nullPOI*/)
00061 {
00062     // Take snapshot of initial state, to restore it at the end 
00063     RooArgSet initialState; params_->snapshot(initialState);
00064 
00065    // Initialize parameters
00066     *params_ = snap_;
00067 
00068     // Initialize signal strength
00069     RooRealVar *rIn = (RooRealVar *) poi_.first();
00070     RooRealVar *r   = (RooRealVar *) params_->find(rIn->GetName());
00071     bool canKeepNLL = createNLL(*pdf_, data);
00072 
00073     double initialR = rIn->getVal();
00074 
00075     // Perform unconstrained minimization
00076     r->setVal(initialR == 0 ? 0.5 : 0.5*initialR); //best guess
00077     r->setConstant(false);
00078 
00079     std::cout << "Doing a fit for with " << r->GetName() << " in range [ " << r->getMin() << " , " << r->getMax() << "]" << std::endl;
00080     std::cout << "Starting point is " << r->GetName() << " = " << r->getVal() << std::endl;
00081     minNLL(/*constrained=*/false, r);
00082     double bestFitR = r->getVal();
00083     std::cout << "Fit result was " << r->GetName() << " = " << r->getVal() << std::endl;
00084 
00085     //Restore initial state, to avoid issues with ToyMCSampler
00086     *params_ = initialState;
00087 
00088     if (!canKeepNLL) nll_.reset();
00089 
00090     return bestFitR;
00091 }
00092 
00093 bool BestFitSigmaTestStat::createNLL(RooAbsPdf &pdf, RooAbsData &data) 
00094 {
00095     if (typeid(pdf) == typeid(RooSimultaneousOpt)) {
00096         if (nll_.get() == 0) nll_.reset(pdf.createNLL(data, RooFit::Constrain(nuisances_)));
00097         else ((cacheutils::CachingSimNLL&)(*nll_)).setData(data);
00098         return true;
00099     } else {
00100         nll_.reset(pdf.createNLL(data, RooFit::Constrain(nuisances_)));
00101         return false;
00102     }
00103 }
00104 
00105 double BestFitSigmaTestStat::minNLL(bool constrained, RooRealVar *r) 
00106 {
00107     CascadeMinimizer::Mode mode(constrained ? CascadeMinimizer::Constrained : CascadeMinimizer::Unconstrained);
00108     CascadeMinimizer minim(*nll_, mode, r);
00109     minim.minimize(verbosity_-2);
00110     return nll_->getVal();
00111 }