CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SequentialMinimizer.h
Go to the documentation of this file.
1 #ifndef HiggsAnalysis_CombinedLimit_SequentialMinimizer_h
2 #define HiggsAnalysis_CombinedLimit_SequentialMinimizer_h
3 
4 struct RooAbsReal;
5 struct RooRealVar;
6 #include <vector>
7 #include <memory>
8 #include <Math/Minimizer.h>
9 
10 namespace cmsmath {
11 
14  MinimizerContext(const ROOT::Math::IMultiGenFunction *function) : func(function), x(func->NDim()), nCalls(0) {}
15  // convenience methods
16  double eval() const { nCalls++; return (*func)(&x[0]); }
17  double setAndEval(unsigned int i, double xi) const { x[i] = xi; return eval(); }
18  double cleanEval(unsigned int i, double xi) const { double x0 = x[i]; x[i] = xi; double y = eval(); x[i] = x0; return y; }
19  // data, fixed
20  const ROOT::Math::IMultiGenFunction * func;
21  // data, mutable
22  mutable std::vector<double> x;
23  mutable unsigned int nCalls;
24  };
25 
27  public:
28  OneDimMinimizer() : f_(0), idx_(0) {}
29  OneDimMinimizer(const MinimizerContext &ctx, unsigned int idx) :
30  f_(&ctx), idx_(idx) {}
31  OneDimMinimizer(const MinimizerContext &ctx, unsigned int idx, double xmin, double xmax, double xstep, const std::string &name) :
32  f_(&ctx), idx_(idx), name_(name), xmin_(xmin), xmax_(xmax), xstep_(xstep) {}
33 
34  const std::string & name() const { return name_; }
35  const char * cname() const { return name_.c_str(); }
36  double max() const { return xmax_; }
37  double min() const { return xmin_; }
38  double step() const { return xstep_; }
39 
40  bool isInit() const { return f_ != 0; }
41  void init(const MinimizerContext &ctx, unsigned int idx, double xmin, double xmax, double xstep, const std::string &name) {
42  f_ = &ctx; idx_ = idx;
43  xmin_ = xmin; xmax_ = xmax; xstep_ = xstep;
44  name_ = name;
45  }
46  void initUnbound(const MinimizerContext &ctx, unsigned int idx, double xstep, const std::string &name) {
48  }
49  // out of line to avoid including TString everywhere
50  void initDefault(const MinimizerContext &ctx, unsigned int idx) ;
51 
54  bool minimize(int steps=1, double ytol=0, double xtol = 0);
55 
63  enum ImproveRet { Unchanged = 2, Done = 1, NotDone = 0 };
64  ImproveRet improve(int steps=1, double ytol=0, double xtol = 0, bool force=true);
65 
66  void moveTo(double x) ;
67  private:
68  // Function
70  // Index of myself
71  unsigned int idx_;
72  // My name
73  std::string name_;
74 
75  // Point (x and y)
76  double xi_[3], yi_[3];
77 
78  // Bounds and step
79  double xmin_, xmax_, xstep_;
80 
83  bool doloop(int steps, double ytol, double xtol) ;
84 
86  void seek() ;
87 
90  void reseek() ;
91 
93  void goldenBisection();
94 
96  bool parabolaStep();
97 
99  double parabolaFit();
100 
102  inline double &x() { return f_->x[idx_]; }
103  inline double eval() { return f_->eval(); }
104  inline double eval(double x) { return f_->cleanEval(idx_, x); }
105 
106  inline void assign(int to, int from) { xi_[to] = xi_[from]; yi_[to] = yi_[from]; }
107  };
108 
109  class SequentialMinimizer : public ROOT::Math::Minimizer {
110  public:
111  SequentialMinimizer(const char *name=0) : ROOT::Math::Minimizer() {}
112 
114  virtual void Clear() ;
115 
117  virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func) ;
118 
120  virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step) ;
121 
123  virtual bool SetLimitedVariable(unsigned int ivar, const std::string & name, double val, double step, double lower, double upper) ;
124 
126  virtual bool SetFixedVariable(unsigned int ivar, const std::string & name, double val) ;
127 
129  virtual bool Minimize() ;
130 
132  virtual double MinValue() const { return minValue_; }
133 
135  virtual double Edm() const { return edm_; }
136 
138  virtual const double * X() const { return & func_->x[0]; }
139 
141  virtual const double * MinGradient() const { return 0; }
142 
144  virtual unsigned int NCalls() const { return func_->nCalls; }
145 
148  virtual unsigned int NDim() const { return nDim_; }
149 
152  virtual unsigned int NFree() const { return nFree_; }
153 
155  virtual bool ProvidesError() const { return false; }
156 
158  virtual const double * Errors() const { return 0; }
159 
160  virtual double CovMatrix(unsigned int i, unsigned int j) const { return 0; }
161 
162  // these have to be public for ROOT to handle
164  struct Worker : public OneDimMinimizer {
168  };
169  protected:
170  bool minimize(int smallsteps=5);
171  bool improve(int smallsteps=5);
172  bool doFullMinim();
173 
174  std::auto_ptr<MinimizerContext> func_;
175  unsigned int nDim_, nFree_;
176 
177  // status information
178  double minValue_;
179  double edm_;
180 
181  // Workers
182  std::vector<Worker> workers_;
184 
185  // ROOT::Math::Minimizer for strategy 2
186  std::auto_ptr<ROOT::Math::Minimizer> fullMinimizer_;
187  std::vector<int> subspaceIndices_;
188 
189  };
190 
191 } // namespace
192 #endif
OneDimMinimizer(const MinimizerContext &ctx, unsigned int idx)
std::vector< double > x
int i
Definition: DBlmapReader.cc:9
Definition: vlib.h:257
bool minimize(int steps=1, double ytol=0, double xtol=0)
virtual bool SetLimitedVariable(unsigned int ivar, const std::string &name, double val, double step, double lower, double upper)
set upper/lower limited variable (override if minimizer supports them )
void seek()
search for a triplet of points bracketing the maximum. return false in case of errors ...
list step
Definition: launcher.py:15
bool minimize(int smallsteps=5)
void assign(int to, int from)
virtual bool Minimize()
method to perform the minimization
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)
set the function to minimize
const ROOT::Math::IMultiGenFunction * func
virtual unsigned int NFree() const
virtual const double * MinGradient() const
return pointer to gradient values at the minimum
void init(const MinimizerContext &ctx, unsigned int idx, double xmin, double xmax, double xstep, const std::string &name)
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)
set free variable
void initDefault(const MinimizerContext &ctx, unsigned int idx)
MinimizerContext(const ROOT::Math::IMultiGenFunction *function)
void initUnbound(const MinimizerContext &ctx, unsigned int idx, double xstep, const std::string &name)
OneDimMinimizer(const MinimizerContext &ctx, unsigned int idx, double xmin, double xmax, double xstep, const std::string &name)
std::vector< Worker > workers_
ImproveRet improve(int steps=1, double ytol=0, double xtol=0, bool force=true)
virtual unsigned int NDim() const
virtual bool ProvidesError() const
minimizer provides error and error matrix
const double infinity
virtual bool SetFixedVariable(unsigned int ivar, const std::string &name, double val)
set fixed variable (override if minimizer supports them )
int j
Definition: DBlmapReader.cc:9
double cleanEval(unsigned int i, double xi) const
bool improve(int smallsteps=5)
virtual void Clear()
reset for consecutive minimizations - implement if needed
const char * cname() const
double setAndEval(unsigned int i, double xi) const
std::auto_ptr< MinimizerContext > func_
static std::string from(" from ")
bool doloop(int steps, double ytol, double xtol)
bool parabolaStep()
do the parabola fit
SequentialMinimizer(const char *name=0)
Basic struct to call a function.
void goldenBisection()
do the golden bisection
std::auto_ptr< ROOT::Math::Minimizer > fullMinimizer_
const std::string & name() const
virtual double Edm() const
return expected distance reached from the minimum
double & x()
evaluate function at x
virtual const double * X() const
return pointer to X values at the minimum
double parabolaFit()
do the parabola fit
const MinimizerContext * f_
virtual const double * Errors() const
return errors at the minimum
virtual double CovMatrix(unsigned int i, unsigned int j) const
virtual unsigned int NCalls() const
number of function calls to reach the minimum
virtual double MinValue() const
return minimum function value