AmpTools
MinuitMinimizationManager.h
Go to the documentation of this file.
1 #if !defined(MINUITINTERFACE_MINUITMINIMIZATIONMANAGER_H)
2 #define MINUITINTERFACE_MINUITMINIMIZATIONMANAGER_H
3 
4 // This file is a part of MinuitInterface - a front end for the Minuit minimization
5 // package (Minuit itself was authored by Fred James, of CERN)
6 //
7 //
8 // Copyright Cornell University 1993, 1996, All Rights Reserved.
9 //
10 // This software written by Lawrence Gibbons, Cornell University.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions
14 // are met:
15 // 1. Redistributions of source code must retain the above copyright
16 // notice and author attribution, this list of conditions and the
17 // following disclaimer.
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice and author attribution, this list of conditions and the
20 // following disclaimer in the documentation and/or other materials
21 // provided with the distribution.
22 // 3. Neither the name of the University nor the names of its contributors
23 // may be used to endorse or promote products derived from this software
24 // without specific prior written permission.
25 //
26 // Creation of derivative forms of this software for commercial
27 // utilization may be subject to restriction; written permission may be
28 // obtained from Cornell University.
29 //
30 // CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way
31 // of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR
32 // WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
33 // THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS,
34 // COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS. Cornell University shall not be
35 // held liable for any liability with respect to any claim by the user or any
36 // other party arising from use of the program.
37 //
38 
39 #include <iostream>
40 #include <functional>
41 #include <string>
42 
45 #include "UpRootMinuit/URFcn.h"
46 #include "UpRootMinuit/URMinuit.h"
47 
48 // forward declarations
50 
52 {
53 public:
54 
56  kNormal = 0,
61  };
62 
67 
69 
71 
72  enum Commands { kUnknown = 0, kMigrad = 1, kMinos = 2, kHesse = 3 };
73 
74  // friends
75  friend class MinuitParameterManager;
76 
77  // con/destructors
78  MinuitMinimizationManager( int maxParameters = 50 );
80 
81  // calculate the current value of the function to be minimized
82  double evaluateFunction(); // central values
83 
84  // status of the last command attempted
85  // = 0: command executed normally
86  // 1: command is blank, ignored
87  // 2: command line unreadable, ignored
88  // 3: unknown command, ignored
89  // 4: abnormal termination (e.g., MIGRAD not converged)
90  // 5: command is a request to read PARAMETER definitions
91  // 6: 'SET INPUT' command
92  // 7: 'SET TITLE' command
93  // 8: 'SET COVAR' command
94  // 9: reserved
95  // 10: END command
96  // 11: EXIT or STOP command
97  // 12: RETURN command
98  int status() const;
99 
100  // gets a flag that indicates the last command executed
101  int lastCommand() const;
102 
103  // error matrix status:
104  // 0= not calculated at all
105  // 1= approximation only, not accurate
106  // 2= full matrix, but forced positive-definite
107  // 3= full accurate covariance matrix
108  int eMatrixStatus() const;
109 
110  // return some information about the best minimum so far
111  double bestMinimum() const;
112  double estDistToMinimum() const;
113 
114  // turn off/on derivative calculations -- when enabled, minuit will check
115  // to ensure that derivatives are being properly computed
116  // if minuit rejects user-defined derivative calculation, it can be forced
117  // to accept them by setting the flag
118  void enableDerivatives( Option optionArg = kCheckDerivativeCalc );
119  void disableDerivatives();
120 
121  bool derivativesEnabled() const { return m_derivativesEnabled; }
122 
123  // standard minimization procedures
124  void migradMinimization();
125  void minosMinimization();
126  vector< vector< double > > hesseEvaluation();
127 
128  // change the output stream for the minuit logging
129  void setLogStream( std::ostream& logStream );
130 
131  // change the internal precision of MINUIT (calls SET EPS)
132  void setPrecision( double precision );
133  double precision() const;
134 
135  // change the maximum number of iterations for certain MINUIT operations (default 5000)
136  void setMaxIterations( int maxIter );
137 
138  // retrieve the setting for maximum number of iterations
139  int maxIterations() const;
140 
141  // change the minimization strategy -- from the MINUIT manual:
142  // In the current release, this parameter can take on three integer values (0, 1, 2),
143  // and the default value is 1. Value 0 indicates to Minuit that it should economize
144  // function calls; it is intended for cases where there are many variable parameters
145  // and/or the function takes a long time to calculate and/or the user is not
146  // interested in very precise values for parameter errors. On the other hand,
147  // the value 2 indicates that Minuit is allowed to waste function calls in order
148  // to be sure that all values are precise; it is intended for cases where the function
149  // is evaluated in a very short time and/or where the parameter errors must be
150  // calculated reliably
151 
152  void setStrategy( int strategy );
153  int strategy() const;
154 
155  // serve up the parameterManager for the user to define/delete minuit parameters
156  MinuitParameterManager& parameterManager() { return m_parameterManager; }
157 
158  // but sometimes we just want to peek at the parameter manager too
159  const MinuitParameterManager& parameterManager() const { return m_parameterManager; }
160 
161  // allow the user to specify an action everytime operator() is called
162  // with a new type of flag
163  void setUserFlagFunction( void (*newFlagFunction)(int) ) {
164  m_newFlagFunction = newFlagFunction;
165  }
166 
167  // the "fcn" that URMinuit needs
168  void operator()( int &npar, double *grad, double &fval, const std::vector<double>& par, int flag);
169 
170  // ------------ static member functions ---------------------
171 
172 protected:
173  // access to minuit information
174  const std::vector<double>& minuitWorkingValues() const; // the current parameter values
175  URMinuit& minuitMinimizer(); // the URMinuit object itself
176 
177 
178 private:
179  // no defaulted copy constructor or assignment operators
181 
182  // ----------------------- member items --------------------------
183 
184  void computeDerivatives( double* grad );
185 
186  MinuitParameterManager m_parameterManager;
187  URMinuit m_fitter;
188 
189  bool m_derivativesEnabled;
190  void (*m_newFlagFunction)(int);
191 
192  int m_lastMinuitFlag;
193 
194  int m_lastCommand;
195  int m_status;
196 
197  int m_strategy;
198 
199  double m_precision;
200 
201  double m_bestMin;
202  double m_estDistToMin;
203 
204  int m_eMatrixStat;
205 
206  int m_functionCallCounter;
207 };
208 #endif
const std::vector< double > & minuitWorkingValues() const
void operator()(int &npar, double *grad, double &fval, const std::vector< double > &par, int flag)
void setUserFlagFunction(void(*newFlagFunction)(int))
Definition: URFcn.h:21
void setLogStream(std::ostream &logStream)
MinuitMinimizationManager(int maxParameters=50)
MinuitParameterManager & parameterManager()
vector< vector< double > > hesseEvaluation()
const MinuitParameterManager & parameterManager() const
void enableDerivatives(Option optionArg=kCheckDerivativeCalc)