CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes
PSFitter::HybridMinimizer Class Reference

#include <HybridMinimizer.h>

Inheritance diagram for PSFitter::HybridMinimizer:

Public Types

enum  EMinimizerType {
  kMigrad, kSimplex, kCombined, kScan,
  kFumili
}
 

Public Member Functions

void Clear () override
 
bool Contour (unsigned int i, unsigned int j, unsigned int &npoints, double *xi, double *xj) override
 
double Correlation (unsigned int i, unsigned int j) const override
 
double CovMatrix (unsigned int i, unsigned int j) const override
 
int CovMatrixStatus () const override
 
double Edm () const override
 return expected distance reached from the minimum More...
 
const double * Errors () const override
 return errors at the minimum More...
 
bool GetCovMatrix (double *cov) const override
 
bool GetHessianMatrix (double *h) const override
 
bool GetMinosError (unsigned int i, double &errLow, double &errUp, int=0) override
 
double GlobalCC (unsigned int i) const override
 
bool Hesse () override
 
 HybridMinimizer (EMinimizerType type=kMigrad)
 
 HybridMinimizer (const char *type)
 
const double * MinGradient () const override
 return pointer to gradient values at the minimum More...
 
bool Minimize () override
 
double MinValue () const override
 return minimum function value More...
 
unsigned int NCalls () const override
 number of function calls to reach the minimum More...
 
unsigned int NDim () const override
 
unsigned int NFree () const override
 
void PrintResults () override
 print result of minimization More...
 
bool ProvidesError () const override
 minimizer provides error and error matrix More...
 
bool Scan (unsigned int i, unsigned int &nstep, double *x, double *y, double xmin=0, double xmax=0) override
 
bool SetFixedVariable (unsigned int, const std::string &, double) override
 set fixed variable (override if minimizer supports them ) More...
 
void SetFunction (const ROOT::Math::IMultiGenFunction &func) override
 set the function to minimize More...
 
void SetFunction (const ROOT::Math::IMultiGradFunction &func) override
 set gradient the function to minimize More...
 
bool SetLimitedVariable (unsigned int ivar, const std::string &name, double val, double step, double, double) override
 set upper/lower limited variable (override if minimizer supports them ) More...
 
bool SetLowerLimitedVariable (unsigned int ivar, const std::string &name, double val, double step, double lower) override
 set lower limit variable (override if minimizer supports them ) More...
 
void SetMinimizerType (EMinimizerType type)
 
bool SetUpperLimitedVariable (unsigned int ivar, const std::string &name, double val, double step, double upper) override
 set upper limit variable (override if minimizer supports them ) More...
 
bool SetVariable (unsigned int ivar, const std::string &name, double val, double step) override
 set free variable More...
 
bool SetVariableValue (unsigned int ivar, double val) override
 set variable More...
 
bool SetVariableValues (const double *val) override
 
const ROOT::Minuit2::MnUserParameterState & State ()
 return the minimizer state (containing values, step size , etc..) More...
 
int VariableIndex (const std::string &name) const override
 
std::string VariableName (unsigned int ivar) const override
 get name of variables (override if minimizer support storing of variable names) More...
 
const double * X () const override
 return pointer to X values at the minimum More...
 
 ~HybridMinimizer () override
 

Protected Member Functions

bool ExamineMinimum (const ROOT::Minuit2::FunctionMinimum &min)
 examine the minimum result More...
 
virtual const ROOT::Minuit2::FCNBase * GetFCN () const
 
virtual const ROOT::Minuit2::ModularFunctionMinimizer * GetMinimizer () const
 
virtual void SetMinimizer (ROOT::Minuit2::ModularFunctionMinimizer *m)
 

Private Member Functions

 HybridMinimizer (const HybridMinimizer &)
 
HybridMinimizeroperator= (const HybridMinimizer &rhs)
 

Private Attributes

unsigned int fDim
 
std::vector< double > fErrors
 
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
 
ROOT::Minuit2::FunctionMinimum * fMinimum
 
ROOT::Minuit2::FCNBase * fMinuitFCN
 
ROOT::Minuit2::MnUserParameterState fState
 
bool fUseFumili
 
std::vector< double > fValues
 

Detailed Description

Definition at line 45 of file HybridMinimizer.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

PSFitter::HybridMinimizer::HybridMinimizer ( EMinimizerType  type = kMigrad)

Default constructor

Definition at line 73 of file HybridMinimizer.cc.

References SetMinimizerType().

73  :
74  Minimizer(),
75  fDim(0),
76  fMinimizer(nullptr),
77  fMinuitFCN(nullptr),
78  fMinimum(nullptr)
79 {
80  // Default constructor implementation depending on minimizer type
82 }
type
Definition: HCALResponse.h:21
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
void SetMinimizerType(EMinimizerType type)
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
PSFitter::HybridMinimizer::HybridMinimizer ( const char *  type)

Constructor with a char (used by PM)

Definition at line 84 of file HybridMinimizer.cc.

References estimatePileup2::algoname, particleFlow_cfi::algoType, createfilelist::int, kCombined, kFumili, kMigrad, kScan, kSimplex, SetMinimizerType(), AlCaHLTBitMon_QueryRunRegistry::string, and create_public_lumi_plots::transform.

84  :
85  Minimizer(),
86  fDim(0),
87  fMinimizer(nullptr),
88  fMinuitFCN(nullptr),
89  fMinimum(nullptr)
90 {
91  // constructor from a string
92 
94  // tolower() is not an std function (Windows)
95  std::transform(algoname.begin(), algoname.end(), algoname.begin(), (int(*)(int)) tolower );
96 
98  if (algoname == "simplex") algoType = kSimplex;
99  if (algoname == "minimize" ) algoType = kCombined;
100  if (algoname == "scan" ) algoType = kScan;
101  if (algoname == "fumili" ) algoType = kFumili;
102 
103  SetMinimizerType(algoType);
104 }
type
Definition: HCALResponse.h:21
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
void SetMinimizerType(EMinimizerType type)
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
PSFitter::HybridMinimizer::~HybridMinimizer ( )
override

Destructor (no operations)

Definition at line 138 of file HybridMinimizer.cc.

References fMinimizer, fMinimum, and fMinuitFCN.

139 {
140  // Destructor implementation.
141  if (fMinimizer) delete fMinimizer;
142  if (fMinuitFCN) delete fMinuitFCN;
143  if (fMinimum) delete fMinimum;
144 }
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
PSFitter::HybridMinimizer::HybridMinimizer ( const HybridMinimizer )
private

Copy constructor

Definition at line 146 of file HybridMinimizer.cc.

146  :
147  ROOT::Math::Minimizer()
148 {
149  // Implementation of copy constructor.
150 }

Member Function Documentation

void PSFitter::HybridMinimizer::Clear ( )
override

Definition at line 160 of file HybridMinimizer.cc.

References fMinimum, and fState.

Referenced by PulseShapeFitOOTPileupCorrection::fit().

160  {
161  // delete the state in case of consecutive minimizations
162  fState = MnUserParameterState();
163  // clear also the function minimum
164  if (fMinimum) delete fMinimum;
165  fMinimum = nullptr;
166 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FunctionMinimum * fMinimum
bool PSFitter::HybridMinimizer::Contour ( unsigned int  i,
unsigned int  j,
unsigned int &  npoints,
double *  xi,
double *  xj 
)
override

find the contour points (xi,xj) of the function for parameter i and j around the minimum The contour will be find for value of the function = Min + ErrorUp();

Definition at line 847 of file HybridMinimizer.cc.

References fMinimum, fMinuitFCN, fState, mps_fire::i, npoints, PSFitter::RestoreGlobalPrintLevel(), mps_fire::result, and PSFitter::TurnOffPrintInfoLevel().

847  {
848  // contour plot for parameter i and j
849  // need a valid FunctionMinimum otherwise exits
850  if (fMinimum == nullptr) {
851  MN_ERROR_MSG2("HybridMinimizer::Contour"," no function minimum existing. Must minimize function before");
852  return false;
853  }
854 
855  if (!fMinimum->IsValid() ) {
856  MN_ERROR_MSG2("HybridMinimizer::Contour","Invalid function minimum");
857  return false;
858  }
859  assert(fMinuitFCN);
860 
861  fMinuitFCN->SetErrorDef(ErrorDef() );
862  // if error def has been changed update it in FunctionMinimum
863  if (ErrorDef() != fMinimum->Up() )
864  fMinimum->SetErrorDef(ErrorDef() );
865 
866  // switch off Minuit2 printing (for level of 0,1)
867  int prev_level = (PrintLevel() <= 1 ) ? TurnOffPrintInfoLevel() : -2;
868 
869  MnPrint::SetLevel( PrintLevel() );
870 
871  // set the precision if needed
872  if (Precision() > 0) fState.SetPrecision(Precision());
873 
874  // eventually one should specify tolerance in contours
875  MnContours contour(*fMinuitFCN, *fMinimum, Strategy() );
876 
877  if (prev_level > -2) RestoreGlobalPrintLevel(prev_level);
878 
879  std::vector<std::pair<double,double> > result = contour(ipar,jpar, npoints);
880  if (result.size() != npoints) {
881  MN_ERROR_MSG2("HybridMinimizer::Contour"," Invalid result from MnContours");
882  return false;
883  }
884  for (unsigned int i = 0; i < npoints; ++i ) {
885  x[i] = result[i].first;
886  y[i] = result[i].second;
887  }
888 
889 
890  return true;
891 
892 
893 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
static const int npoints
int TurnOffPrintInfoLevel()
void RestoreGlobalPrintLevel(int)
double PSFitter::HybridMinimizer::Correlation ( unsigned int  i,
unsigned int  j 
) const
override

return correlation coefficient between variable i and j. If the variable is fixed or const the return value is zero

Definition at line 615 of file HybridMinimizer.cc.

References funct::abs(), fDim, fState, gen::k, checklumidiff::l, mathSSE::sqrt(), and tmp.

615  {
616  // get correlation between parameter i and j
617  if ( i >= fDim || i >= fDim) return 0;
618  if ( !fState.HasCovariance() ) return 0; // no info available when minimization has failed
619  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst() ) return 0;
620  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst() ) return 0;
621  unsigned int k = fState.IntOfExt(i);
622  unsigned int l = fState.IntOfExt(j);
623  double cij = fState.IntCovariance()(k,l);
624  double tmp = std::sqrt( std::abs ( fState.IntCovariance()(k,k) * fState.IntCovariance()(l,l) ) );
625  if (tmp > 0 ) return cij/tmp;
626  return 0;
627 }
ROOT::Minuit2::MnUserParameterState fState
T sqrt(T t)
Definition: SSEVec.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int k[5][pyjets_maxn]
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double PSFitter::HybridMinimizer::CovMatrix ( unsigned int  i,
unsigned int  j 
) const
override

return covariance matrix elements if the variable is fixed or const the value is zero The ordering of the variables is the same as in errors and parameter value. This is different from the direct interface of Minuit2 or TMinuit where the values were obtained only to variable parameters

Definition at line 548 of file HybridMinimizer.cc.

References fDim, fState, gen::k, and checklumidiff::l.

548  {
549  // get value of covariance matrices (transform from external to internal indices)
550  if ( i >= fDim || i >= fDim) return 0;
551  if ( !fState.HasCovariance() ) return 0; // no info available when minimization has failed
552  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst() ) return 0;
553  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst() ) return 0;
554  unsigned int k = fState.IntOfExt(i);
555  unsigned int l = fState.IntOfExt(j);
556  return fState.Covariance()(k,l);
557 }
ROOT::Minuit2::MnUserParameterState fState
int k[5][pyjets_maxn]
int PSFitter::HybridMinimizer::CovMatrixStatus ( ) const
override

return the status of the covariance matrix status = -1 : not available (inversion failed or Hesse failed) status = 0 : available but not positive defined status = 1 : covariance only approximate status = 2 : full matrix but forced pos def status = 3 : full accurate matrix

Definition at line 957 of file HybridMinimizer.cc.

References fMinimum, and fState.

957  {
958  // return status of covariance matrix
959  //-1 - not available (inversion failed or Hesse failed)
960  // 0 - available but not positive defined
961  // 1 - covariance only approximate
962  // 2 full matrix but forced pos def
963  // 3 full accurate matrix
964 
965  if (fMinimum) {
966  // case a function minimum is available
967  if (fMinimum->HasAccurateCovar() ) return 3;
968  else if (fMinimum->HasMadePosDefCovar() ) return 2;
969  else if (fMinimum->HasValidCovariance() ) return 1;
970  else if (fMinimum->HasCovariance() ) return 0;
971  return -1;
972  }
973  else {
974  // case fMinimum is not available - use state information
975  return fState.CovarianceStatus();
976  }
977  return 0;
978 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FunctionMinimum * fMinimum
double PSFitter::HybridMinimizer::Edm ( ) const
inlineoverride

return expected distance reached from the minimum

Definition at line 136 of file HybridMinimizer.h.

References hcaldqm::quantity::fState, and X.

136 { return fState.Edm(); }
ROOT::Minuit2::MnUserParameterState fState
const double * PSFitter::HybridMinimizer::Errors ( ) const
override

return errors at the minimum

Definition at line 528 of file HybridMinimizer.cc.

References fDim, fErrors, fState, and mps_fire::i.

528  {
529  // return error at minimum (set to zero for fixed and constant params)
530  const std::vector<MinuitParameter> & paramsObj = fState.MinuitParameters();
531  if (paramsObj.empty()) return nullptr;
532  assert(fDim == paramsObj.size());
533  // be careful for multiple calls of this function. I will redo an allocation here
534  // only when size of vectors has changed (e.g. after a new minimization)
535  if (fErrors.size() != fDim) fErrors.resize( fDim );
536  for (unsigned int i = 0; i < fDim; ++i) {
537  const MinuitParameter & par = paramsObj[i];
538  if (par.IsFixed() || par.IsConst() )
539  fErrors[i] = 0;
540  else
541  fErrors[i] = par.Error();
542  }
543 
544  return &fErrors.front();
545 }
ROOT::Minuit2::MnUserParameterState fState
std::vector< double > fErrors
bool PSFitter::HybridMinimizer::ExamineMinimum ( const ROOT::Minuit2::FunctionMinimum &  min)
protected

examine the minimum result

study the function minimum

Definition at line 412 of file HybridMinimizer.cc.

References UEAnalysisJets_cfi::debugLevel, mps_check::msg, PrintResults(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by Minimize().

412  {
414 
415  // debug ( print all the states)
416  int debugLevel = PrintLevel();
417  if (debugLevel >= 3) {
418 /*
419  const std::vector<ROOT::Minuit2::MinimumState>& iterationStates = min.States();
420  std::cout << "Number of iterations " << iterationStates.size() << std::endl;
421  for (unsigned int i = 0; i < iterationStates.size(); ++i) {
422  //std::cout << iterationStates[i] << std::endl;
423  const ROOT::Minuit2::MinimumState & st = iterationStates[i];
424  std::cout << "----------> Iteration " << i << std::endl;
425  int pr = std::cout.precision(12);
426  std::cout << " FVAL = " << st.Fval() << " Edm = " << st.Edm() << " Nfcn = " << st.NFcn() << std::endl;
427  std::cout.precision(pr);
428  std::cout << " Error matrix change = " << st.Error().Dcovar() << std::endl;
429  std::cout << " Parameters : ";
430  // need to transform from internal to external
431  for (int j = 0; j < st.size() ; ++j) std::cout << " p" << j << " = " << fState.Int2ext( j, st.Vec()(j) );
432  std::cout << std::endl;
433  }
434 */
435  }
436 
437  fStatus = 0;
438  std::string txt;
439  if (min.HasMadePosDefCovar() ) {
440  txt = "Covar was made pos def";
441  fStatus = 1;
442  }
443  if (min.HesseFailed() ) {
444  txt = "Hesse is not valid";
445  fStatus = 2;
446  }
447  if (min.IsAboveMaxEdm() ) {
448  txt = "Edm is above max";
449  fStatus = 3;
450  }
451  if (min.HasReachedCallLimit() ) {
452  txt = "Reached call limit";
453  fStatus = 4;
454  }
455 
456 
457  bool validMinimum = min.IsValid();
458  if (validMinimum) {
459  // print a warning message in case something is not ok
460  if (fStatus != 0 && debugLevel > 0) MN_INFO_MSG2("HybridMinimizer::Minimize",txt);
461  }
462  else {
463  // minimum is not valid when state is not valid and edm is over max or has passed call limits
464  if (fStatus == 0) {
465  // this should not happen
466  txt = "unknown failure";
467  fStatus = 5;
468  }
469  std::string msg = "Minimization did NOT converge, " + txt;
470  MN_INFO_MSG2("HybridMinimizer::Minimize",msg);
471  }
472 
473  if (debugLevel >= 1) PrintResults();
474  return validMinimum;
475 }
T min(T a, T b)
Definition: MathUtil.h:58
tuple msg
Definition: mps_check.py:285
void PrintResults() override
print result of minimization
bool PSFitter::HybridMinimizer::GetCovMatrix ( double *  cov) const
override

Fill the passed array with the covariance matrix elements if the variable is fixed or const the value is zero. The array will be filled as cov[i *ndim + j] The ordering of the variables is the same as in errors and parameter value. This is different from the direct interface of Minuit2 or TMinuit where the values were obtained only to variable parameters

Definition at line 559 of file HybridMinimizer.cc.

References fDim, fState, mps_fire::i, gen::k, checklumidiff::l, and funct::m.

559  {
560  // get value of covariance matrices
561  if ( !fState.HasCovariance() ) return false; // no info available when minimization has failed
562  for (unsigned int i = 0; i < fDim; ++i) {
563  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst() ) {
564  for (unsigned int j = 0; j < fDim; ++j) { cov[i*fDim + j] = 0; }
565  }
566  else
567  {
568  unsigned int l = fState.IntOfExt(i);
569  for (unsigned int j = 0; j < fDim; ++j) {
570  // could probably speed up this loop (if needed)
571  int k = i*fDim + j;
572  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst() )
573  cov[k] = 0;
574  else {
575  // need to transform from external to internal indices)
576  // for taking care of the removed fixed row/columns in the Minuit2 representation
577  unsigned int m = fState.IntOfExt(j);
578  cov[k] = fState.Covariance()(l,m);
579  }
580  }
581  }
582  }
583  return true;
584 }
ROOT::Minuit2::MnUserParameterState fState
int k[5][pyjets_maxn]
virtual const ROOT::Minuit2::FCNBase* PSFitter::HybridMinimizer::GetFCN ( ) const
inlineprotectedvirtual

Definition at line 274 of file HybridMinimizer.h.

References min().

Referenced by Minimize().

274 { return fMinuitFCN; }
ROOT::Minuit2::FCNBase * fMinuitFCN
bool PSFitter::HybridMinimizer::GetHessianMatrix ( double *  h) const
override

Fill the passed array with the Hessian matrix elements The Hessian matrix is the matrix of the second derivatives and is the inverse of the covariance matrix If the variable is fixed or const the values for that variables are zero. The array will be filled as h[i *ndim + j]

Definition at line 586 of file HybridMinimizer.cc.

References fDim, fState, mps_fire::i, gen::k, checklumidiff::l, and funct::m.

586  {
587  // get value of Hessian matrix
588  // this is the second derivative matrices
589  if ( !fState.HasCovariance() ) return false; // no info available when minimization has failed
590  for (unsigned int i = 0; i < fDim; ++i) {
591  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst() ) {
592  for (unsigned int j = 0; j < fDim; ++j) { hess[i*fDim + j] = 0; }
593  }
594  else {
595  unsigned int l = fState.IntOfExt(i);
596  for (unsigned int j = 0; j < fDim; ++j) {
597  // could probably speed up this loop (if needed)
598  int k = i*fDim + j;
599  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst() )
600  hess[k] = 0;
601  else {
602  // need to transform from external to internal indices)
603  // for taking care of the removed fixed row/columns in the Minuit2 representation
604  unsigned int m = fState.IntOfExt(j);
605  hess[k] = fState.Hessian()(l,m);
606  }
607  }
608  }
609  }
610 
611  return true;
612 }
ROOT::Minuit2::MnUserParameterState fState
int k[5][pyjets_maxn]
virtual const ROOT::Minuit2::ModularFunctionMinimizer* PSFitter::HybridMinimizer::GetMinimizer ( ) const
inlineprotectedvirtual

Definition at line 270 of file HybridMinimizer.h.

Referenced by Minimize().

270 { return fMinimizer; }
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
bool PSFitter::HybridMinimizer::GetMinosError ( unsigned int  i,
double &  errLow,
double &  errUp,
int  runopt = 0 
)
override

get the minos error for parameter i, return false if Minos failed A minimizaiton must be performed befre, return false if no minimization has been done In case of Minos failed the status error is updated as following status += 10 * minosStatus where the minos status is: status = 1 : maximum number of function calls exceeded when running for lower error status = 2 : maximum number of function calls exceeded when running for upper error status = 3 : new minimum found when running for lower error status = 4 : new minimum found when running for upper error status = 5 : any other failure

Definition at line 643 of file HybridMinimizer.cc.

References UEAnalysisJets_cfi::debugLevel, fMinimum, fMinuitFCN, fState, SiStripPI::max, PSFitter::RestoreGlobalPrintLevel(), g4SimHits_cfi::Tolerance, PSFitter::TurnOffPrintInfoLevel(), and up.

643  {
644  // return the minos error for parameter i
645  // if a minimum does not exist an error is returned
646  // runopt is a flag which specifies if only lower or upper error needs to be run
647  // if runopt = 0 both, = 1 only lower, + 2 only upper errors
648  errLow = 0; errUp = 0;
649  bool runLower = runopt != 2;
650  bool runUpper = runopt != 1;
651 
652  assert( fMinuitFCN );
653 
654  // need to know if parameter is const or fixed
655  if ( fState.Parameter(i).IsConst() || fState.Parameter(i).IsFixed() ) {
656  return false;
657  }
658 
659  int debugLevel = PrintLevel();
660  // internal minuit messages
661  MnPrint::SetLevel( debugLevel );
662 
663  // to run minos I need function minimum class
664  // redo minimization from current state
665 // ROOT::Minuit2::FunctionMinimum min =
666 // GetMinimizer()->Minimize(*GetFCN(),fState, ROOT::Minuit2::MnStrategy(strategy), MaxFunctionCalls(), Tolerance());
667 // fState = min.UserState();
668  if (fMinimum == nullptr) {
669  MN_ERROR_MSG("HybridMinimizer::GetMinosErrors: failed - no function minimum existing");
670  return false;
671  }
672 
673  if (!fMinimum->IsValid() ) {
674  MN_ERROR_MSG("HybridMinimizer::MINOS failed due to invalid function minimum");
675  return false;
676  }
677 
678  fMinuitFCN->SetErrorDef(ErrorDef() );
679  // if error def has been changed update it in FunctionMinimum
680  if (ErrorDef() != fMinimum->Up() )
681  fMinimum->SetErrorDef(ErrorDef() );
682 
683  // switch off Minuit2 printing
684  int prev_level = (PrintLevel() <= 0 ) ? TurnOffPrintInfoLevel() : -2;
685 
686  // set the precision if needed
687  if (Precision() > 0) fState.SetPrecision(Precision());
688 
689 
690  ROOT::Minuit2::MnMinos minos( *fMinuitFCN, *fMinimum);
691 
692  // run MnCross
693  MnCross low;
694  MnCross up;
695  int maxfcn = MaxFunctionCalls();
696  double tol = Tolerance();
697 
698 // const char * par_name = fState.Name(i);
699 
700  // now input tolerance for migrad calls inside Minos (MnFunctionCross)
701  // before it was fixed to 0.05
702  // cut off too small tolerance (they are not needed)
703  tol = std::max(tol, 0.01);
704 
705  /*
706  if (PrintLevel() >=1) {
707  // print the real number of maxfcn used (defined in MnMinos)
708  int maxfcn_used = maxfcn;
709  if (maxfcn_used == 0) {
710  int nvar = fState.VariableParameters();
711  maxfcn_used = 2*(nvar+1)*(200 + 100*nvar + 5*nvar*nvar);
712  }
713 // std::cout << "HybridMinimizer::GetMinosError for parameter " << i << " " << par_name
714 // << " using max-calls " << maxfcn_used << ", tolerance " << tol << std::endl;
715  }
716  */
717 
718  if (runLower) low = minos.Loval(i,maxfcn,tol);
719  if (runUpper) up = minos.Upval(i,maxfcn,tol);
720 
721  ROOT::Minuit2::MinosError me(i, fMinimum->UserState().Value(i),low, up);
722 
723  if (prev_level > -2) RestoreGlobalPrintLevel(prev_level);
724 
725  // debug result of Minos
726  // print error message in Minos
727 
728 
729  if (debugLevel >= 1) {
730 /*
731  if (runLower) {
732  if (!me.LowerValid() )
733  std::cout << "Minos: Invalid lower error for parameter " << par_name << std::endl;
734  if(me.AtLowerLimit())
735  std::cout << "Minos: Parameter : " << par_name << " is at Lower limit."<<std::endl;
736  if(me.AtLowerMaxFcn())
737  std::cout << "Minos: Maximum number of function calls exceeded when running for lower error" <<std::endl;
738  if(me.LowerNewMin() )
739  std::cout << "Minos: New Minimum found while running Minos for lower error" <<std::endl;
740 
741  if (debugLevel > 1) std::cout << "Minos: Lower error for parameter " << par_name << " : " << me.Lower() << std::endl;
742 
743  }
744  if (runUpper) {
745  if (!me.UpperValid() )
746  std::cout << "Minos: Invalid upper error for parameter " << par_name << std::endl;
747  if(me.AtUpperLimit())
748  std::cout << "Minos: Parameter " << par_name << " is at Upper limit."<<std::endl;
749  if(me.AtUpperMaxFcn())
750  std::cout << "Minos: Maximum number of function calls exceeded when running for upper error" <<std::endl;
751  if(me.UpperNewMin() )
752  std::cout << "Minos: New Minimum found while running Minos for upper error" <<std::endl;
753 
754  if (debugLevel > 1) std::cout << "Minos: Upper error for parameter " << par_name << " : " << me.Upper() << std::endl;
755  }
756 */
757  }
758 
759  bool lowerInvalid = (runLower && !me.LowerValid() );
760  bool upperInvalid = (runUpper && !me.UpperValid() );
761  int mstatus = 0;
762  if (lowerInvalid || upperInvalid ) {
763  // set status accroding to bit
764  // bit 1: lower invalid Minos errors
765  // bit 2: uper invalid Minos error
766  // bit 3: invalid because max FCN
767  // bit 4 : invalid because a new minimum has been found
768  if (lowerInvalid) {
769  mstatus |= 1;
770  if (me.AtLowerMaxFcn() ) mstatus |= 4;
771  if (me.LowerNewMin() ) mstatus |= 8;
772  }
773  if(upperInvalid) {
774  mstatus |= 3;
775  if (me.AtUpperMaxFcn() ) mstatus |= 4;
776  if (me.UpperNewMin() ) mstatus |= 8;
777  }
778  //std::cout << "Error running Minos for parameter " << i << std::endl;
779  fStatus += 10*mstatus;
780  }
781 
782  errLow = me.Lower();
783  errUp = me.Upper();
784 
785  bool isValid = (runLower && me.LowerValid() ) || (runUpper && me.UpperValid() );
786  return isValid;
787 }
Definition: BitonicSort.h:8
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
int TurnOffPrintInfoLevel()
void RestoreGlobalPrintLevel(int)
double PSFitter::HybridMinimizer::GlobalCC ( unsigned int  i) const
override

get global correlation coefficient for the variable i. This is a number between zero and one which gives the correlation between the i-th variable and that linear combination of all other variables which is most strongly correlated with i. If the variable is fixed or const the return value is zero

Definition at line 629 of file HybridMinimizer.cc.

References fDim, fState, and gen::k.

629  {
630  // get global correlation coefficient for the parameter i. This is a number between zero and one which gives
631  // the correlation between the i-th parameter and that linear combination of all other parameters which
632  // is most strongly correlated with i.
633 
634  if ( i >= fDim || i >= fDim) return 0;
635  // no info available when minimization has failed or has some problems
636  if ( !fState.HasGlobalCC() ) return 0;
637  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst() ) return 0;
638  unsigned int k = fState.IntOfExt(i);
639  return fState.GlobalCC().GlobalCC()[k];
640 }
ROOT::Minuit2::MnUserParameterState fState
int k[5][pyjets_maxn]
bool PSFitter::HybridMinimizer::Hesse ( )
override

perform a full calculation of the Hessian matrix for error calculation If a valid minimum exists the calculation is done on the minimum point otherwise is performed in the current set values of parameters Status code of minimizer is updated according to the following convention (in case Hesse failed) status += 100*hesseStatus where hesse status is: status = 1 : hesse failed status = 2 : matrix inversion failed status = 3 : matrix is not pos defined

Definition at line 895 of file HybridMinimizer.cc.

References fMinimum, fMinuitFCN, fState, PSFitter::RestoreGlobalPrintLevel(), and PSFitter::TurnOffPrintInfoLevel().

895  {
896  // find Hessian (full second derivative calculations)
897  // the contained state will be updated with the Hessian result
898  // in case a function minimum exists and is valid the result will be
899  // appended in the function minimum
900 
901  if (!fMinuitFCN) {
902  MN_ERROR_MSG2("HybridMinimizer::Hesse","FCN function has not been set");
903  return false;
904  }
905 
906  int strategy = Strategy();
907  int maxfcn = MaxFunctionCalls();
908 
909  // switch off Minuit2 printing
910  int prev_level = (PrintLevel() <= 0 ) ? TurnOffPrintInfoLevel() : -2;
911 
912  MnPrint::SetLevel( PrintLevel() );
913 
914  // set the precision if needed
915  if (Precision() > 0) fState.SetPrecision(Precision());
916 
917  ROOT::Minuit2::MnHesse hesse( strategy );
918 
919  // case when function minimum exists
920  if (fMinimum ) {
921  // run hesse and function minimum will be updated with Hesse result
922  hesse( *fMinuitFCN, *fMinimum, maxfcn );
923  fState = fMinimum->UserState();
924  }
925 
926  else {
927  // run Hesse on point stored in current state (independent of function minimum validity)
928  // (x == 0)
929  fState = hesse( *fMinuitFCN, fState, maxfcn);
930  }
931 
932  if (prev_level > -2) RestoreGlobalPrintLevel(prev_level);
933 
934  if (PrintLevel() >= 3) {
935 // std::cout << "State returned from Hesse " << std::endl;
936 // std::cout << fState << std::endl;
937  }
938 
939  if (!fState.HasCovariance() ) {
940  // if false means error is not valid and this is due to a failure in Hesse
941  if (PrintLevel() > 0) MN_INFO_MSG2("HybridMinimizer::Hesse","Hesse failed ");
942  // update minimizer error status
943  int hstatus = 4;
944  // information on error state can be retrieved only if fMinimum is available
945  if (fMinimum) {
946  if (fMinimum->Error().HesseFailed() ) hstatus = 1;
947  if (fMinimum->Error().InvertFailed() ) hstatus = 2;
948  else if (!(fMinimum->Error().IsPosDef()) ) hstatus = 3;
949  }
950  fStatus += 100*hstatus;
951  return false;
952  }
953 
954  return true;
955 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
int TurnOffPrintInfoLevel()
void RestoreGlobalPrintLevel(int)
const double* PSFitter::HybridMinimizer::MinGradient ( ) const
inlineoverride

return pointer to gradient values at the minimum

Definition at line 142 of file HybridMinimizer.h.

142 { return nullptr; } // not available in Minuit2
bool PSFitter::HybridMinimizer::Minimize ( )
override

method to perform the minimization. Return false in case the minimization did not converge. In this case a status code different than zero is set (retrieved by the derived method Minimizer::Status() )"

status = 1 : Covariance was made pos defined status = 2 : Hesse is invalid status = 3 : Edm is above max status = 4 : Reached call limit status = 5 : Any other failure

Definition at line 302 of file HybridMinimizer.cc.

References ExamineMinimum(), fMinimum, fMinuitFCN, fState, GetFCN(), GetMinimizer(), min(), convertSQLiteXML::ok, PSFitter::RestoreGlobalPrintLevel(), g4SimHits_cfi::Tolerance, and PSFitter::TurnOffPrintInfoLevel().

Referenced by PulseShapeFitOOTPileupCorrection::fit().

302  {
303  // perform the minimization
304  // store a copy of FunctionMinimum
305  if (!fMinuitFCN) {
306  MN_ERROR_MSG2("HybridMinimizer::Minimize","FCN function has not been set");
307  return false;
308  }
309 
310  assert(GetMinimizer() != nullptr );
311 
312  // delete result of previous minimization
313  if (fMinimum) delete fMinimum;
314  fMinimum = nullptr;
315 
316 
317  int maxfcn = MaxFunctionCalls();
318  double tol = Tolerance();
319  int strategyLevel = Strategy();
320  fMinuitFCN->SetErrorDef(ErrorDef() );
321 
322  /*
323  if (PrintLevel() >=1) {
324  // print the real number of maxfcn used (defined in ModularFuncitonMinimizer)
325  int maxfcn_used = maxfcn;
326  if (maxfcn_used == 0) {
327  int nvar = fState.VariableParameters();
328  maxfcn_used = 200 + 100*nvar + 5*nvar*nvar;
329  }
330 // std::cout << "HybridMinimizer: Minimize with max-calls " << maxfcn_used
331 // << " convergence for edm < " << tol << " strategy "
332 // << strategyLevel << std::endl;
333  }
334  */
335 
336  // internal minuit messages
337  MnPrint::SetLevel(PrintLevel() );
338 
339  // switch off Minuit2 printing
340  int prev_level = (PrintLevel() <= 0 ) ? TurnOffPrintInfoLevel() : -2;
341 
342  // set the precision if needed
343  if (Precision() > 0) fState.SetPrecision(Precision());
344 
345  // set strategy and add extra options if needed
346  ROOT::Minuit2::MnStrategy strategy(strategyLevel);
347  ROOT::Math::IOptions * minuit2Opt = ROOT::Math::MinimizerOptions::FindDefault("Minuit2");
348  if (minuit2Opt) {
349  // set extra strategy options
350  int nGradCycles = strategy.GradientNCycles();
351  int nHessCycles = strategy.HessianNCycles();
352  int nHessGradCycles = strategy.HessianGradientNCycles();
353 
354  double gradTol = strategy.GradientTolerance();
355  double gradStepTol = strategy.GradientStepTolerance();
356  double hessStepTol = strategy.HessianStepTolerance();
357  double hessG2Tol = strategy.HessianG2Tolerance();
358 
359  minuit2Opt->GetValue("GradientNCycles",nGradCycles);
360  minuit2Opt->GetValue("HessianNCycles",nHessCycles);
361  minuit2Opt->GetValue("HessianGradientNCycles",nHessGradCycles);
362 
363  minuit2Opt->GetValue("GradientTolerance",gradTol);
364  minuit2Opt->GetValue("GradientStepTolerance",gradStepTol);
365  minuit2Opt->GetValue("HessianStepTolerance",hessStepTol);
366  minuit2Opt->GetValue("HessianG2Tolerance",hessG2Tol);
367 
368  strategy.SetGradientNCycles(nGradCycles);
369  strategy.SetHessianNCycles(nHessCycles);
370  strategy.SetHessianGradientNCycles(nHessGradCycles);
371 
372  strategy.SetGradientTolerance(gradTol);
373  strategy.SetGradientStepTolerance(gradStepTol);
374  strategy.SetHessianStepTolerance(hessStepTol);
375  strategy.SetHessianG2Tolerance(hessStepTol);
376 
377  if (PrintLevel() > 0) {
378 // std::cout << "HybridMinimizer::Minuit - Changing default stratgey options" << std::endl;
379  minuit2Opt->Print();
380  }
381 
382  }
383 
384  const ROOT::Minuit2::FCNGradientBase * gradFCN = dynamic_cast<const ROOT::Minuit2::FCNGradientBase *>( fMinuitFCN );
385  if ( gradFCN != nullptr) {
386  // use gradient
387  //SetPrintLevel(3);
388  ROOT::Minuit2::FunctionMinimum min = GetMinimizer()->Minimize(*gradFCN, fState, strategy, maxfcn, tol);
389  fMinimum = new ROOT::Minuit2::FunctionMinimum (min);
390  }
391  else {
392  ROOT::Minuit2::FunctionMinimum min = GetMinimizer()->Minimize(*GetFCN(), fState, strategy, maxfcn, tol);
393  fMinimum = new ROOT::Minuit2::FunctionMinimum (min);
394  }
395 
396  // check if Hesse needs to be run
397  if (fMinimum->IsValid() && IsValidError() && fMinimum->State().Error().Dcovar() != 0 ) {
398  // run Hesse (Hesse will add results in the last state of fMinimum
399  ROOT::Minuit2::MnHesse hesse(strategy );
400  hesse( *fMinuitFCN, *fMinimum, maxfcn);
401  }
402 
403  // -2 is the highest low invalid value for gErrorIgnoreLevel
404  if (prev_level > -2) RestoreGlobalPrintLevel(prev_level);
405 
406  fState = fMinimum->UserState();
407  bool ok = ExamineMinimum(*fMinimum);
408  //fMinimum = 0;
409  return ok;
410 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
bool ExamineMinimum(const ROOT::Minuit2::FunctionMinimum &min)
examine the minimum result
T min(T a, T b)
Definition: MathUtil.h:58
int TurnOffPrintInfoLevel()
void RestoreGlobalPrintLevel(int)
virtual const ROOT::Minuit2::ModularFunctionMinimizer * GetMinimizer() const
virtual const ROOT::Minuit2::FCNBase * GetFCN() const
double PSFitter::HybridMinimizer::MinValue ( ) const
inlineoverride

return minimum function value

Definition at line 133 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

Referenced by PulseShapeFitOOTPileupCorrection::fit().

133 { return fState.Fval(); }
ROOT::Minuit2::MnUserParameterState fState
unsigned int PSFitter::HybridMinimizer::NCalls ( ) const
inlineoverride

number of function calls to reach the minimum

Definition at line 145 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

145 { return fState.NFcn(); }
ROOT::Minuit2::MnUserParameterState fState
unsigned int PSFitter::HybridMinimizer::NDim ( ) const
inlineoverride

this is <= Function().NDim() which is the total number of variables (free+ constrained ones)

Definition at line 149 of file HybridMinimizer.h.

149 { return fDim; }
unsigned int PSFitter::HybridMinimizer::NFree ( ) const
inlineoverride

number of free variables (real dimension of the problem) this is <= Function().NDim() which is the total

Definition at line 153 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

153 { return fState.VariableParameters(); }
ROOT::Minuit2::MnUserParameterState fState
HybridMinimizer & PSFitter::HybridMinimizer::operator= ( const HybridMinimizer rhs)
private

Assignment operator

Definition at line 152 of file HybridMinimizer.cc.

153 {
154  // Implementation of assignment operator.
155  if (this == &rhs) return *this; // time saving self-test
156  return *this;
157 }
void PSFitter::HybridMinimizer::PrintResults ( )
override

print result of minimization

return reference to the objective function virtual const ROOT::Math::IGenFunction & Function() const;

Definition at line 478 of file HybridMinimizer.cc.

References fMinimum.

Referenced by ExamineMinimum().

478  {
479  // print results of minimization
480  if (!fMinimum) return;
481  if (fMinimum->IsValid() ) {
482  // valid minimum
483 /*
484  std::cout << "HybridMinimizer : Valid minimum - status = " << fStatus << std::endl;
485  int pr = std::cout.precision(18);
486  std::cout << "FVAL = " << fState.Fval() << std::endl;
487  std::cout << "Edm = " << fState.Edm() << std::endl;
488  std::cout.precision(pr);
489  std::cout << "Nfcn = " << fState.NFcn() << std::endl;
490  for (unsigned int i = 0; i < fState.MinuitParameters().size(); ++i) {
491  const MinuitParameter & par = fState.Parameter(i);
492  std::cout << par.Name() << "\t = " << par.Value() << "\t ";
493  if (par.IsFixed() ) std::cout << "(fixed)" << std::endl;
494  else if (par.IsConst() ) std::cout << "(const)" << std::endl;
495  else if (par.HasLimits() )
496  std::cout << "+/- " << par.Error() << "\t(limited)"<< std::endl;
497  else
498  std::cout << "+/- " << par.Error() << std::endl;
499  }
500 */
501  }
502  else {
503 /*
504  std::cout << "HybridMinimizer : Invalid Minimum - status = " << fStatus << std::endl;
505  std::cout << "FVAL = " << fState.Fval() << std::endl;
506  std::cout << "Edm = " << fState.Edm() << std::endl;
507  std::cout << "Nfcn = " << fState.NFcn() << std::endl;
508 */
509  }
510 }
ROOT::Minuit2::FunctionMinimum * fMinimum
bool PSFitter::HybridMinimizer::ProvidesError ( ) const
inlineoverride

minimizer provides error and error matrix

Definition at line 156 of file HybridMinimizer.h.

References h, mps_fire::i, npoints, hybridSuperClusters_cfi::xi, TrackerOfflineValidation_Dqm_cff::xmax, and TrackerOfflineValidation_Dqm_cff::xmin.

156 { return true; }
bool PSFitter::HybridMinimizer::Scan ( unsigned int  i,
unsigned int &  nstep,
double *  x,
double *  y,
double  xmin = 0,
double  xmax = 0 
)
override

scan a parameter i around the minimum. A minimization must have been done before, return false if it is not the case

Definition at line 789 of file HybridMinimizer.cc.

References fMinuitFCN, fState, mps_fire::i, PSFitter::RestoreGlobalPrintLevel(), mps_fire::result, and PSFitter::TurnOffPrintInfoLevel().

789  {
790  // scan a parameter (variable) around the minimum value
791  // the parameters must have been set before
792  // if xmin=0 && xmax == 0 by default scan around 2 sigma of the error
793  // if the errors are also zero then scan from min and max of parameter range
794 
795  if (!fMinuitFCN) {
796  MN_ERROR_MSG2("HybridMinimizer::Scan"," Function must be set before using Scan");
797  return false;
798  }
799 
800  if ( ipar > fState.MinuitParameters().size() ) {
801  MN_ERROR_MSG2("HybridMinimizer::Scan"," Invalid number. Minimizer variables must be set before using Scan");
802  return false;
803  }
804 
805  // switch off Minuit2 printing
806  int prev_level = (PrintLevel() <= 0 ) ? TurnOffPrintInfoLevel() : -2;
807 
808  MnPrint::SetLevel( PrintLevel() );
809 
810 
811  // set the precision if needed
812  if (Precision() > 0) fState.SetPrecision(Precision());
813 
814  MnParameterScan scan( *fMinuitFCN, fState.Parameters() );
815  double amin = scan.Fval(); // fcn value of the function before scan
816 
817  // first value is param value
818  std::vector<std::pair<double, double> > result = scan(ipar, nstep-1, xmin, xmax);
819 
820  if (prev_level > -2) RestoreGlobalPrintLevel(prev_level);
821 
822  if (result.size() != nstep) {
823  MN_ERROR_MSG2("HybridMinimizer::Scan"," Invalid result from MnParameterScan");
824  return false;
825  }
826  // sort also the returned points in x
827  std::sort(result.begin(), result.end() );
828 
829 
830  for (unsigned int i = 0; i < nstep; ++i ) {
831  x[i] = result[i].first;
832  y[i] = result[i].second;
833  }
834 
835  // what to do if a new minimum has been found ?
836  // use that as new minimum
837  if (scan.Fval() < amin ) {
838  if (PrintLevel() > 0) MN_INFO_MSG2("HybridMinimizer::Scan","A new minimum has been found");
839  fState.SetValue(ipar, scan.Parameters().Value(ipar) );
840 
841  }
842 
843 
844  return true;
845 }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FCNBase * fMinuitFCN
int TurnOffPrintInfoLevel()
void RestoreGlobalPrintLevel(int)
bool PSFitter::HybridMinimizer::SetFixedVariable ( unsigned int  ivar,
const std::string &  name,
double  val 
)
override

set fixed variable (override if minimizer supports them )

Definition at line 223 of file HybridMinimizer.cc.

References funct::abs(), fState, and SetVariable().

Referenced by PulseShapeFitOOTPileupCorrection::fit().

223  {
224  // add a fixed variable
225  // need a step size otherwise treated as a constant
226  // use 10%
227  double step = ( val != 0) ? 0.1 * std::abs(val) : 0.1;
228  if (!SetVariable(ivar, name, val, step ) ) {
229  ivar = fState.Index(name );
230  }
231  fState.Fix(ivar);
232  return true;
233 }
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
ROOT::Minuit2::MnUserParameterState fState
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
step
Definition: StallMonitor.cc:94
void PSFitter::HybridMinimizer::SetFunction ( const ROOT::Math::IMultiGenFunction &  func)
override

set the function to minimize

Definition at line 266 of file HybridMinimizer.cc.

References fDim, fMinuitFCN, patCandidates_cff::func, and fUseFumili.

Referenced by PulseShapeFitOOTPileupCorrection::fit().

266  {
267  // set function to be minimized
268  if (fMinuitFCN) delete fMinuitFCN;
269  fDim = func.NDim();
270  if (!fUseFumili) {
271  fMinuitFCN = new ROOT::Minuit2::FCNAdapter<ROOT::Math::IMultiGenFunction> (func, ErrorDef() );
272  }
273  else {
274  // for Fumili the fit method function interface is required
275  const ROOT::Math::FitMethodFunction * fcnfunc = dynamic_cast<const ROOT::Math::FitMethodFunction *>(&func);
276  if (!fcnfunc) {
277  MN_ERROR_MSG("HybridMinimizer: Wrong Fit method function for Fumili");
278  return;
279  }
280  fMinuitFCN = new ROOT::Minuit2::FumiliFCNAdapter<ROOT::Math::FitMethodFunction> (*fcnfunc, fDim, ErrorDef() );
281  }
282 }
ROOT::Minuit2::FCNBase * fMinuitFCN
void PSFitter::HybridMinimizer::SetFunction ( const ROOT::Math::IMultiGradFunction &  func)
override

set gradient the function to minimize

Definition at line 284 of file HybridMinimizer.cc.

References fDim, fMinuitFCN, patCandidates_cff::func, and fUseFumili.

284  {
285  // set function to be minimized
286  fDim = func.NDim();
287  if (fMinuitFCN) delete fMinuitFCN;
288  if (!fUseFumili) {
289  fMinuitFCN = new ROOT::Minuit2::FCNGradAdapter<ROOT::Math::IMultiGradFunction> (func, ErrorDef() );
290  }
291  else {
292  // for Fumili the fit method function interface is required
293  const ROOT::Math::FitMethodGradFunction * fcnfunc = dynamic_cast<const ROOT::Math::FitMethodGradFunction*>(&func);
294  if (!fcnfunc) {
295  MN_ERROR_MSG("HybridMinimizer: Wrong Fit method function for Fumili");
296  return;
297  }
298  fMinuitFCN = new ROOT::Minuit2::FumiliFCNAdapter<ROOT::Math::FitMethodGradFunction> (*fcnfunc, fDim, ErrorDef() );
299  }
300 }
ROOT::Minuit2::FCNBase * fMinuitFCN
bool PSFitter::HybridMinimizer::SetLimitedVariable ( unsigned int  ivar,
const std::string &  name,
double  val,
double  step,
double  lower,
double  upper 
)
override

set upper/lower limited variable (override if minimizer supports them )

Definition at line 216 of file HybridMinimizer.cc.

References fState, and SetVariable().

Referenced by PulseShapeFitOOTPileupCorrection::fit().

216  {
217  // add a double bound variable
218  if (!SetVariable(ivar, name, val, step) ) return false;
219  fState.SetLimits(ivar, lower, upper);
220  return true;
221 }
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
ROOT::Minuit2::MnUserParameterState fState
step
Definition: StallMonitor.cc:94
bool PSFitter::HybridMinimizer::SetLowerLimitedVariable ( unsigned int  ivar,
const std::string &  name,
double  val,
double  step,
double  lower 
)
override

set lower limit variable (override if minimizer supports them )

Definition at line 200 of file HybridMinimizer.cc.

References fState, and SetVariable().

200  {
201  // add a lower bounded variable
202  if (!SetVariable(ivar, name, val, step) ) return false;
203  fState.SetLowerLimit(ivar, lower);
204  return true;
205 }
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
ROOT::Minuit2::MnUserParameterState fState
step
Definition: StallMonitor.cc:94
virtual void PSFitter::HybridMinimizer::SetMinimizer ( ROOT::Minuit2::ModularFunctionMinimizer *  m)
inlineprotectedvirtual

Definition at line 272 of file HybridMinimizer.h.

References funct::m.

Referenced by SetMinimizerType().

272 { fMinimizer = m; }
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
void PSFitter::HybridMinimizer::SetMinimizerType ( EMinimizerType  type)

Definition at line 106 of file HybridMinimizer.cc.

References fMinimizer, fUseFumili, kCombined, kFumili, kMigrad, kScan, kSimplex, and SetMinimizer().

Referenced by PulseShapeFitOOTPileupCorrection::fit(), and HybridMinimizer().

106  {
107  // Set minimizer algorithm type
108  fUseFumili = false;
109 
110  if (fMinimizer) delete fMinimizer;
111 
112  switch (type) {
113  case kMigrad:
114  //std::cout << "HybridMinimizer: minimize using MIGRAD " << std::endl;
115  SetMinimizer( new ROOT::Minuit2::VariableMetricMinimizer() );
116  return;
117  case kSimplex:
118  //std::cout << "HybridMinimizer: minimize using SIMPLEX " << std::endl;
119  SetMinimizer( new ROOT::Minuit2::SimplexMinimizer() );
120  return;
121  case kCombined:
122  SetMinimizer( new ROOT::Minuit2::CombinedMinimizer() );
123  return;
124  case kScan:
125  SetMinimizer( new ROOT::Minuit2::ScanMinimizer() );
126  return;
127  case kFumili:
128  SetMinimizer( new ROOT::Minuit2::FumiliMinimizer() );
129  fUseFumili = true;
130  return;
131  default:
132  //migrad minimizer
133  SetMinimizer( new ROOT::Minuit2::VariableMetricMinimizer() );
134  }
135 }
type
Definition: HCALResponse.h:21
virtual void SetMinimizer(ROOT::Minuit2::ModularFunctionMinimizer *m)
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
bool PSFitter::HybridMinimizer::SetUpperLimitedVariable ( unsigned int  ivar,
const std::string &  name,
double  val,
double  step,
double  upper 
)
override

set upper limit variable (override if minimizer supports them )

Definition at line 207 of file HybridMinimizer.cc.

References fState, and SetVariable().

207  {
208  // add a upper bounded variable
209  if (!SetVariable(ivar, name, val, step) ) return false;
210  fState.SetUpperLimit(ivar, upper);
211  return true;
212 }
bool SetVariable(unsigned int ivar, const std::string &name, double val, double step) override
set free variable
ROOT::Minuit2::MnUserParameterState fState
step
Definition: StallMonitor.cc:94
bool PSFitter::HybridMinimizer::SetVariable ( unsigned int  ivar,
const std::string &  name,
double  val,
double  step 
)
override

set free variable

Definition at line 171 of file HybridMinimizer.cc.

References fState, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by SetFixedVariable(), SetLimitedVariable(), SetLowerLimitedVariable(), and SetUpperLimitedVariable().

171  {
172  // set a free variable.
173  // Add the variable if not existing otherwise set value if exists already
174  // this is implemented in MnUserParameterState::Add
175  // if index is wrong (i.e. variable already exists but with a different index return false) but
176  // value is set for corresponding variable name
177 
178 // std::cout << " add parameter " << name << " " << val << " step " << step << std::endl;
179 
180  if (step <= 0) {
181  std::string txtmsg = "Parameter " + name + " has zero or invalid step size - consider it as constant ";
182  MN_INFO_MSG2("HybridMinimizer::SetVariable",txtmsg);
183  fState.Add(name, val);
184  }
185  else
186  fState.Add(name, val, step);
187 
188  unsigned int minuit2Index = fState.Index(name );
189  if ( minuit2Index != ivar) {
190  std::string txtmsg("Wrong index used for the variable " + name);
191  MN_INFO_MSG2("HybridMinimizer::SetVariable",txtmsg);
192  MN_INFO_VAL2("HybridMinimizer::SetVariable",minuit2Index);
193  return false;
194  }
195  fState.RemoveLimits(ivar);
196 
197  return true;
198 }
ROOT::Minuit2::MnUserParameterState fState
step
Definition: StallMonitor.cc:94
bool PSFitter::HybridMinimizer::SetVariableValue ( unsigned int  ivar,
double  val 
)
override

set variable

Definition at line 249 of file HybridMinimizer.cc.

References fState.

249  {
250  // set value for variable ivar (only for existing parameters)
251  if (ivar >= fState.MinuitParameters().size() ) return false;
252  fState.SetValue(ivar, val);
253  return true;
254 }
ROOT::Minuit2::MnUserParameterState fState
bool PSFitter::HybridMinimizer::SetVariableValues ( const double *  val)
override

Definition at line 256 of file HybridMinimizer.cc.

References fState, and gen::n.

256  {
257  // set value for variable ivar (only for existing parameters)
258  unsigned int n = fState.MinuitParameters().size();
259  if (n== 0) return false;
260  for (unsigned int ivar = 0; ivar < n; ++ivar)
261  fState.SetValue(ivar, x[ivar]);
262  return true;
263 }
ROOT::Minuit2::MnUserParameterState fState
const ROOT::Minuit2::MnUserParameterState& PSFitter::HybridMinimizer::State ( )
inline

return the minimizer state (containing values, step size , etc..)

Definition at line 262 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

262 { return fState; }
ROOT::Minuit2::MnUserParameterState fState
int PSFitter::HybridMinimizer::VariableIndex ( const std::string &  name) const
override

get index of variable given a variable given a name return -1 if variable is not found

Definition at line 242 of file HybridMinimizer.cc.

References fState.

242  {
243  // return the variable index
244  // check if variable exist
245  return fState.Trafo().FindIndex(name);
246 }
ROOT::Minuit2::MnUserParameterState fState
std::string PSFitter::HybridMinimizer::VariableName ( unsigned int  ivar) const
override

get name of variables (override if minimizer support storing of variable names)

Definition at line 235 of file HybridMinimizer.cc.

References fState, and AlCaHLTBitMon_QueryRunRegistry::string.

235  {
236  // return the variable name
237  if (ivar >= fState.MinuitParameters().size() ) return std::string();
238  return fState.GetName(ivar);
239 }
ROOT::Minuit2::MnUserParameterState fState
const double * PSFitter::HybridMinimizer::X ( ) const
override

return pointer to X values at the minimum

Definition at line 512 of file HybridMinimizer.cc.

References fDim, fState, fValues, and mps_fire::i.

Referenced by svgfig.Curve.Sample::__repr__(), and PulseShapeFitOOTPileupCorrection::fit().

512  {
513  // return values at minimum
514  const std::vector<MinuitParameter> & paramsObj = fState.MinuitParameters();
515  if (paramsObj.empty()) return nullptr;
516  assert(fDim == paramsObj.size());
517  // be careful for multiple calls of this function. I will redo an allocation here
518  // only when size of vectors has changed (e.g. after a new minimization)
519  if (fValues.size() != fDim) fValues.resize(fDim);
520  for (unsigned int i = 0; i < fDim; ++i) {
521  fValues[i] = paramsObj[i].Value();
522  }
523 
524  return &fValues.front();
525 }
ROOT::Minuit2::MnUserParameterState fState
std::vector< double > fValues

Member Data Documentation

unsigned int PSFitter::HybridMinimizer::fDim
private
std::vector<double> PSFitter::HybridMinimizer::fErrors
mutableprivate

Definition at line 290 of file HybridMinimizer.h.

Referenced by Errors().

ROOT::Minuit2::ModularFunctionMinimizer* PSFitter::HybridMinimizer::fMinimizer
private

Definition at line 286 of file HybridMinimizer.h.

Referenced by SetMinimizerType(), and ~HybridMinimizer().

ROOT::Minuit2::FunctionMinimum* PSFitter::HybridMinimizer::fMinimum
private
ROOT::Minuit2::FCNBase* PSFitter::HybridMinimizer::fMinuitFCN
private
ROOT::Minuit2::MnUserParameterState PSFitter::HybridMinimizer::fState
private
bool PSFitter::HybridMinimizer::fUseFumili
private

Definition at line 282 of file HybridMinimizer.h.

Referenced by SetFunction(), and SetMinimizerType().

std::vector<double> PSFitter::HybridMinimizer::fValues
mutableprivate

Definition at line 289 of file HybridMinimizer.h.

Referenced by X().