CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RooMinimizerOpt.cc
Go to the documentation of this file.
1 #include "../interface/RooMinimizerOpt.h"
2 
3 #include <stdexcept>
4 #include <RooRealVar.h>
5 #include <RooAbsPdf.h>
6 #include <RooMsgService.h>
7 
8 #include <Math/MinimizerOptions.h>
9 
10 RooMinimizerOpt::RooMinimizerOpt(RooAbsReal& function) :
11  RooMinimizer(function)
12 {
13  delete _fcn;
14  _fcn = new RooMinimizerFcnOpt(_func,this,_verbose);
15  setEps(ROOT::Math::MinimizerOptions::DefaultTolerance());
16 }
17 
18 Double_t
20 {
21  if (_theFitter == 0) throw std::logic_error("Must have done a fit before calling edm()");
22  return _theFitter->Result().Edm();
23 }
24 
25 
26 
27 
28 RooMinimizerFcnOpt::RooMinimizerFcnOpt(RooAbsReal *funct, RooMinimizer *context, bool verbose) :
29  RooMinimizerFcn(funct, context, verbose)
30 {
31  _vars.resize(_floatParamList->getSize());
32  std::vector<RooRealVar *>::iterator itv = _vars.begin();
33  RooLinkedListIter iter = _floatParamList->iterator();
34  for (TObject *a = iter.Next(); a != 0; a = iter.Next(), ++itv) {
35  RooRealVar *rrv = dynamic_cast<RooRealVar *>(a);
36  if (rrv == 0) throw std::logic_error(Form("Float param not a RooRealVar but a %s", a->ClassName()));
37  *itv = rrv;
38  }
39 }
40 
41 ROOT::Math::IBaseFunctionMultiDim*
43 {
44  return new RooMinimizerFcnOpt(_funct,_context,_verbose);
45 }
46 
47 double
48 RooMinimizerFcnOpt::DoEval(const double * x) const
49 {
50  // Set the parameter values for this iteration
51  for (int index = 0; index < _nDim; index++) {
52  if (_logfile) (*_logfile) << x[index] << " " ;
53  RooRealVar* par = _vars[index];
54  if (par->getVal()!=x[index]) {
55  if (_verbose) cout << par->GetName() << "=" << x[index] << ", " ;
56  par->setVal(x[index]);
57  }
58  }
59 
60  // Calculate the function for these parameters
61  double fvalue = _funct->getVal();
62  if (RooAbsPdf::evalError() || RooAbsReal::numEvalErrors()>0) {
63 
64  if (_printEvalErrors>=0) {
65 
66  if (_doEvalErrorWall) {
67  oocoutW(_context,Minimization) << "RooMinimizerFcn: Minimized function has error status." << endl
68  << "Returning maximum FCN so far (" << _maxFCN
69  << ") to force MIGRAD to back out of this region. Error log follows" << endl ;
70  } else {
71  oocoutW(_context,Minimization) << "RooMinimizerFcn: Minimized function has error status but is ignored" << endl ;
72  }
73 
74  TIterator* iter = _floatParamList->createIterator() ;
75  RooRealVar* var ;
76  Bool_t first(kTRUE) ;
77  ooccoutW(_context,Minimization) << "Parameter values: " ;
78  while((var=(RooRealVar*)iter->Next())) {
79  if (first) { first = kFALSE ; } else ooccoutW(_context,Minimization) << ", " ;
80  ooccoutW(_context,Minimization) << var->GetName() << "=" << var->getVal() ;
81  }
82  delete iter ;
83  ooccoutW(_context,Minimization) << endl ;
84 
85  RooAbsReal::printEvalErrors(ooccoutW(_context,Minimization),_printEvalErrors) ;
86  ooccoutW(_context,Minimization) << endl ;
87  }
88 
89  if (_doEvalErrorWall) {
90  fvalue = _maxFCN ;
91  }
92 
93  RooAbsPdf::clearEvalError() ;
94  RooAbsReal::clearEvalErrorLog() ;
95  _numBadNLL++ ;
96  } else if (fvalue>_maxFCN) {
97  _maxFCN = fvalue ;
98  }
99 
100  // Optional logging
101  if (_logfile)
102  (*_logfile) << setprecision(15) << fvalue << setprecision(4) << endl;
103  if (_verbose) {
104  cout << "\nprevFCN = " << setprecision(10)
105  << fvalue << setprecision(4) << " " ;
106  cout.flush() ;
107  }
108 
109  return fvalue;
110 }
Definition: vlib.h:257
std::vector< RooRealVar * > _vars
RooMinimizerOpt(RooAbsReal &function)
virtual ROOT::Math::IBaseFunctionMultiDim * Clone() const
bool first
Definition: L1TdeRCT.cc:94
RooMinimizerFcnOpt(RooAbsReal *funct, RooMinimizer *context, bool verbose=false)
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
Definition: DDAxes.h:10
virtual double DoEval(const double *x) const