CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/HiggsAnalysis/CombinedLimit/src/RooMinimizerOpt.cc

Go to the documentation of this file.
00001 #include "../interface/RooMinimizerOpt.h"
00002 
00003 #include <stdexcept>
00004 #include <RooRealVar.h>
00005 #include <RooAbsPdf.h>
00006 #include <RooMsgService.h>
00007 
00008 #include <Math/MinimizerOptions.h>
00009 
00010 RooMinimizerOpt::RooMinimizerOpt(RooAbsReal& function) :
00011     RooMinimizer(function)
00012 {
00013     delete _fcn;
00014     _fcn = new RooMinimizerFcnOpt(_func,this,_verbose); 
00015     setEps(ROOT::Math::MinimizerOptions::DefaultTolerance());
00016 }
00017 
00018 Double_t
00019 RooMinimizerOpt::edm()
00020 {
00021     if (_theFitter == 0) throw std::logic_error("Must have done a fit before calling edm()");
00022     return _theFitter->Result().Edm();    
00023 }
00024 
00025 
00026 
00027 
00028 RooMinimizerFcnOpt::RooMinimizerFcnOpt(RooAbsReal *funct, RooMinimizer *context,  bool verbose) :
00029     RooMinimizerFcn(funct, context, verbose)
00030 {
00031     _vars.resize(_floatParamList->getSize());
00032     std::vector<RooRealVar *>::iterator itv = _vars.begin();
00033     RooLinkedListIter iter = _floatParamList->iterator();
00034     for (TObject *a = iter.Next(); a != 0; a = iter.Next(), ++itv) {
00035         RooRealVar *rrv = dynamic_cast<RooRealVar *>(a);
00036         if (rrv == 0) throw std::logic_error(Form("Float param not a RooRealVar but a %s", a->ClassName()));
00037         *itv = rrv; 
00038     }
00039 }
00040 
00041 ROOT::Math::IBaseFunctionMultiDim* 
00042 RooMinimizerFcnOpt::Clone() const
00043 {
00044       return new RooMinimizerFcnOpt(_funct,_context,_verbose);
00045 }
00046 
00047 double
00048 RooMinimizerFcnOpt::DoEval(const double * x) const 
00049 {
00050   // Set the parameter values for this iteration
00051   for (int index = 0; index < _nDim; index++) {
00052       if (_logfile) (*_logfile) << x[index] << " " ;
00053       RooRealVar* par = _vars[index];
00054       if (par->getVal()!=x[index]) {
00055           if (_verbose) cout << par->GetName() << "=" << x[index] << ", " ;
00056           par->setVal(x[index]);
00057       }
00058   }
00059 
00060   // Calculate the function for these parameters
00061   double fvalue = _funct->getVal();
00062   if (RooAbsPdf::evalError() || RooAbsReal::numEvalErrors()>0) {
00063 
00064     if (_printEvalErrors>=0) {
00065 
00066       if (_doEvalErrorWall) {
00067         oocoutW(_context,Minimization) << "RooMinimizerFcn: Minimized function has error status." << endl 
00068                                        << "Returning maximum FCN so far (" << _maxFCN 
00069                                        << ") to force MIGRAD to back out of this region. Error log follows" << endl ;
00070       } else {
00071         oocoutW(_context,Minimization) << "RooMinimizerFcn: Minimized function has error status but is ignored" << endl ;
00072       } 
00073 
00074       TIterator* iter = _floatParamList->createIterator() ;
00075       RooRealVar* var ;
00076       Bool_t first(kTRUE) ;
00077       ooccoutW(_context,Minimization) << "Parameter values: " ;
00078       while((var=(RooRealVar*)iter->Next())) {
00079         if (first) { first = kFALSE ; } else ooccoutW(_context,Minimization) << ", " ;
00080         ooccoutW(_context,Minimization) << var->GetName() << "=" << var->getVal() ;
00081       }
00082       delete iter ;
00083       ooccoutW(_context,Minimization) << endl ;
00084       
00085       RooAbsReal::printEvalErrors(ooccoutW(_context,Minimization),_printEvalErrors) ;
00086       ooccoutW(_context,Minimization) << endl ;
00087     } 
00088 
00089     if (_doEvalErrorWall) {
00090       fvalue = _maxFCN ;
00091     }
00092 
00093     RooAbsPdf::clearEvalError() ;
00094     RooAbsReal::clearEvalErrorLog() ;
00095     _numBadNLL++ ;
00096   } else if (fvalue>_maxFCN) {
00097     _maxFCN = fvalue ;
00098   }
00099       
00100   // Optional logging
00101   if (_logfile) 
00102     (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
00103   if (_verbose) {
00104     cout << "\nprevFCN = " << setprecision(10) 
00105          << fvalue << setprecision(4) << "  " ;
00106     cout.flush() ;
00107   }
00108 
00109   return fvalue;
00110 }