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 70 of file HybridMinimizer.cc.

References SetMinimizerType().

71  : Minimizer(), fDim(0), fMinimizer(nullptr), fMinuitFCN(nullptr), fMinimum(nullptr) {
72  // Default constructor implementation depending on minimizer type
74  }
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 76 of file HybridMinimizer.cc.

References createfilelist::int, kCombined, kFumili, kMigrad, kScan, kSimplex, SetMinimizerType(), AlCaHLTBitMon_QueryRunRegistry::string, and HcalDetIdTransform::transform().

77  : Minimizer(), fDim(0), fMinimizer(nullptr), fMinuitFCN(nullptr), fMinimum(nullptr) {
78  // constructor from a string
79 
80  std::string algoname(type);
81  // tolower() is not an std function (Windows)
82  std::transform(algoname.begin(), algoname.end(), algoname.begin(), (int (*)(int))tolower);
83 
84  EMinimizerType algoType = kMigrad;
85  if (algoname == "simplex")
86  algoType = kSimplex;
87  if (algoname == "minimize")
88  algoType = kCombined;
89  if (algoname == "scan")
90  algoType = kScan;
91  if (algoname == "fumili")
92  algoType = kFumili;
93 
94  SetMinimizerType(algoType);
95  }
type
Definition: HCALResponse.h:21
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
void SetMinimizerType(EMinimizerType type)
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
unsigned transform(const HcalDetId &id, unsigned transformCode)
PSFitter::HybridMinimizer::~HybridMinimizer ( )
override

Destructor (no operations)

Definition at line 129 of file HybridMinimizer.cc.

References fMinimizer, fMinimum, and fMinuitFCN.

129  {
130  // Destructor implementation.
131  if (fMinimizer)
132  delete fMinimizer;
133  if (fMinuitFCN)
134  delete fMinuitFCN;
135  if (fMinimum)
136  delete fMinimum;
137  }
ROOT::Minuit2::FCNBase * fMinuitFCN
ROOT::Minuit2::FunctionMinimum * fMinimum
ROOT::Minuit2::ModularFunctionMinimizer * fMinimizer
PSFitter::HybridMinimizer::HybridMinimizer ( const HybridMinimizer )
private

Copy constructor

Definition at line 139 of file HybridMinimizer.cc.

139  : ROOT::Math::Minimizer() {
140  // Implementation of copy constructor.
141  }

Member Function Documentation

void PSFitter::HybridMinimizer::Clear ( )
override

Definition at line 150 of file HybridMinimizer.cc.

References fMinimum, and fState.

Referenced by PulseShapeFitOOTPileupCorrection::fit().

150  {
151  // delete the state in case of consecutive minimizations
152  fState = MnUserParameterState();
153  // clear also the function minimum
154  if (fMinimum)
155  delete fMinimum;
156  fMinimum = nullptr;
157  }
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 860 of file HybridMinimizer.cc.

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

860  {
861  // contour plot for parameter i and j
862  // need a valid FunctionMinimum otherwise exits
863  if (fMinimum == nullptr) {
864  MN_ERROR_MSG2("HybridMinimizer::Contour", " no function minimum existing. Must minimize function before");
865  return false;
866  }
867 
868  if (!fMinimum->IsValid()) {
869  MN_ERROR_MSG2("HybridMinimizer::Contour", "Invalid function minimum");
870  return false;
871  }
872  assert(fMinuitFCN);
873 
874  fMinuitFCN->SetErrorDef(ErrorDef());
875  // if error def has been changed update it in FunctionMinimum
876  if (ErrorDef() != fMinimum->Up())
877  fMinimum->SetErrorDef(ErrorDef());
878 
879  // switch off Minuit2 printing (for level of 0,1)
880  int prev_level = (PrintLevel() <= 1) ? TurnOffPrintInfoLevel() : -2;
881 
882  MnPrint::SetLevel(PrintLevel());
883 
884  // set the precision if needed
885  if (Precision() > 0)
886  fState.SetPrecision(Precision());
887 
888  // eventually one should specify tolerance in contours
889  MnContours contour(*fMinuitFCN, *fMinimum, Strategy());
890 
891  if (prev_level > -2)
892  RestoreGlobalPrintLevel(prev_level);
893 
894  std::vector<std::pair<double, double> > result = contour(ipar, jpar, npoints);
895  if (result.size() != npoints) {
896  MN_ERROR_MSG2("HybridMinimizer::Contour", " Invalid result from MnContours");
897  return false;
898  }
899  for (unsigned int i = 0; i < npoints; ++i) {
900  x[i] = result[i].first;
901  y[i] = result[i].second;
902  }
903 
904  return true;
905  }
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, dqmdumpme::k, cmsLHEtoEOSManager::l, mathSSE::sqrt(), and createJobs::tmp.

615  {
616  // get correlation between parameter i and j
617  if (i >= fDim || i >= fDim)
618  return 0;
619  if (!fState.HasCovariance())
620  return 0; // no info available when minimization has failed
621  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst())
622  return 0;
623  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst())
624  return 0;
625  unsigned int k = fState.IntOfExt(i);
626  unsigned int l = fState.IntOfExt(j);
627  double cij = fState.IntCovariance()(k, l);
628  double tmp = std::sqrt(std::abs(fState.IntCovariance()(k, k) * fState.IntCovariance()(l, l)));
629  if (tmp > 0)
630  return cij / tmp;
631  return 0;
632  }
ROOT::Minuit2::MnUserParameterState fState
T sqrt(T t)
Definition: SSEVec.h:19
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
tmp
align.sh
Definition: createJobs.py:716
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 542 of file HybridMinimizer.cc.

References fDim, fState, dqmdumpme::k, and cmsLHEtoEOSManager::l.

542  {
543  // get value of covariance matrices (transform from external to internal indices)
544  if (i >= fDim || i >= fDim)
545  return 0;
546  if (!fState.HasCovariance())
547  return 0; // no info available when minimization has failed
548  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst())
549  return 0;
550  if (fState.Parameter(j).IsFixed() || fState.Parameter(j).IsConst())
551  return 0;
552  unsigned int k = fState.IntOfExt(i);
553  unsigned int l = fState.IntOfExt(j);
554  return fState.Covariance()(k, l);
555  }
ROOT::Minuit2::MnUserParameterState fState
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 975 of file HybridMinimizer.cc.

References fMinimum, and fState.

975  {
976  // return status of covariance matrix
977  //-1 - not available (inversion failed or Hesse failed)
978  // 0 - available but not positive defined
979  // 1 - covariance only approximate
980  // 2 full matrix but forced pos def
981  // 3 full accurate matrix
982 
983  if (fMinimum) {
984  // case a function minimum is available
985  if (fMinimum->HasAccurateCovar())
986  return 3;
987  else if (fMinimum->HasMadePosDefCovar())
988  return 2;
989  else if (fMinimum->HasValidCovariance())
990  return 1;
991  else if (fMinimum->HasCovariance())
992  return 0;
993  return -1;
994  } else {
995  // case fMinimum is not available - use state information
996  return fState.CovarianceStatus();
997  }
998  return 0;
999  }
ROOT::Minuit2::MnUserParameterState fState
ROOT::Minuit2::FunctionMinimum * fMinimum
double PSFitter::HybridMinimizer::Edm ( ) const
inlineoverride

return expected distance reached from the minimum

Definition at line 134 of file HybridMinimizer.h.

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

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

return errors at the minimum

Definition at line 521 of file HybridMinimizer.cc.

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

521  {
522  // return error at minimum (set to zero for fixed and constant params)
523  const std::vector<MinuitParameter> &paramsObj = fState.MinuitParameters();
524  if (paramsObj.empty())
525  return nullptr;
526  assert(fDim == paramsObj.size());
527  // be careful for multiple calls of this function. I will redo an allocation here
528  // only when size of vectors has changed (e.g. after a new minimization)
529  if (fErrors.size() != fDim)
530  fErrors.resize(fDim);
531  for (unsigned int i = 0; i < fDim; ++i) {
532  const MinuitParameter &par = paramsObj[i];
533  if (par.IsFixed() || par.IsConst())
534  fErrors[i] = 0;
535  else
536  fErrors[i] = par.Error();
537  }
538 
539  return &fErrors.front();
540  }
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 405 of file HybridMinimizer.cc.

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

Referenced by Minimize().

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

References fDim, fState, mps_fire::i, dqmiolumiharvest::j, dqmdumpme::k, cmsLHEtoEOSManager::l, and visualization-live-secondInstance_cfg::m.

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

Definition at line 267 of file HybridMinimizer.h.

References min().

Referenced by Minimize().

267 { 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 585 of file HybridMinimizer.cc.

References fDim, fState, mps_fire::i, dqmiolumiharvest::j, dqmdumpme::k, cmsLHEtoEOSManager::l, and visualization-live-secondInstance_cfg::m.

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

Definition at line 263 of file HybridMinimizer.h.

Referenced by Minimize().

263 { 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 650 of file HybridMinimizer.cc.

References HLT_2018_cff::debugLevel, fMinimum, fMinuitFCN, fState, LaserClient_cfi::low, SiStripPI::max, hlt_dqm_clientPB-live_cfg::me, PSFitter::RestoreGlobalPrintLevel(), g4SimHits_cfi::Tolerance, PSFitter::TurnOffPrintInfoLevel(), and up.

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

References fDim, fState, and dqmdumpme::k.

634  {
635  // get global correlation coefficient for the parameter i. This is a number between zero and one which gives
636  // the correlation between the i-th parameter and that linear combination of all other parameters which
637  // is most strongly correlated with i.
638 
639  if (i >= fDim || i >= fDim)
640  return 0;
641  // no info available when minimization has failed or has some problems
642  if (!fState.HasGlobalCC())
643  return 0;
644  if (fState.Parameter(i).IsFixed() || fState.Parameter(i).IsConst())
645  return 0;
646  unsigned int k = fState.IntOfExt(i);
647  return fState.GlobalCC().GlobalCC()[k];
648  }
ROOT::Minuit2::MnUserParameterState fState
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 907 of file HybridMinimizer.cc.

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

907  {
908  // find Hessian (full second derivative calculations)
909  // the contained state will be updated with the Hessian result
910  // in case a function minimum exists and is valid the result will be
911  // appended in the function minimum
912 
913  if (!fMinuitFCN) {
914  MN_ERROR_MSG2("HybridMinimizer::Hesse", "FCN function has not been set");
915  return false;
916  }
917 
918  int strategy = Strategy();
919  int maxfcn = MaxFunctionCalls();
920 
921  // switch off Minuit2 printing
922  int prev_level = (PrintLevel() <= 0) ? TurnOffPrintInfoLevel() : -2;
923 
924  MnPrint::SetLevel(PrintLevel());
925 
926  // set the precision if needed
927  if (Precision() > 0)
928  fState.SetPrecision(Precision());
929 
930  ROOT::Minuit2::MnHesse hesse(strategy);
931 
932  // case when function minimum exists
933  if (fMinimum) {
934  // run hesse and function minimum will be updated with Hesse result
935  hesse(*fMinuitFCN, *fMinimum, maxfcn);
936  fState = fMinimum->UserState();
937  }
938 
939  else {
940  // run Hesse on point stored in current state (independent of function minimum validity)
941  // (x == 0)
942  fState = hesse(*fMinuitFCN, fState, maxfcn);
943  }
944 
945  if (prev_level > -2)
946  RestoreGlobalPrintLevel(prev_level);
947 
948  if (PrintLevel() >= 3) {
949  // std::cout << "State returned from Hesse " << std::endl;
950  // std::cout << fState << std::endl;
951  }
952 
953  if (!fState.HasCovariance()) {
954  // if false means error is not valid and this is due to a failure in Hesse
955  if (PrintLevel() > 0)
956  MN_INFO_MSG2("HybridMinimizer::Hesse", "Hesse failed ");
957  // update minimizer error status
958  int hstatus = 4;
959  // information on error state can be retrieved only if fMinimum is available
960  if (fMinimum) {
961  if (fMinimum->Error().HesseFailed())
962  hstatus = 1;
963  if (fMinimum->Error().InvertFailed())
964  hstatus = 2;
965  else if (!(fMinimum->Error().IsPosDef()))
966  hstatus = 3;
967  }
968  fStatus += 100 * hstatus;
969  return false;
970  }
971 
972  return true;
973  }
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 140 of file HybridMinimizer.h.

140 { 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 295 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().

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

References hcaldqm::quantity::fState.

Referenced by PulseShapeFitOOTPileupCorrection::fit().

131 { 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 143 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

143 { 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 147 of file HybridMinimizer.h.

147 { 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 151 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

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

Assignment operator

Definition at line 143 of file HybridMinimizer.cc.

143  {
144  // Implementation of assignment operator.
145  if (this == &rhs)
146  return *this; // time saving self-test
147  return *this;
148  }
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 470 of file HybridMinimizer.cc.

References fMinimum.

Referenced by ExamineMinimum().

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

minimizer provides error and error matrix

Definition at line 154 of file HybridMinimizer.h.

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

154 { 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 803 of file HybridMinimizer.cc.

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

803  {
804  // scan a parameter (variable) around the minimum value
805  // the parameters must have been set before
806  // if xmin=0 && xmax == 0 by default scan around 2 sigma of the error
807  // if the errors are also zero then scan from min and max of parameter range
808 
809  if (!fMinuitFCN) {
810  MN_ERROR_MSG2("HybridMinimizer::Scan", " Function must be set before using Scan");
811  return false;
812  }
813 
814  if (ipar > fState.MinuitParameters().size()) {
815  MN_ERROR_MSG2("HybridMinimizer::Scan", " Invalid number. Minimizer variables must be set before using Scan");
816  return false;
817  }
818 
819  // switch off Minuit2 printing
820  int prev_level = (PrintLevel() <= 0) ? TurnOffPrintInfoLevel() : -2;
821 
822  MnPrint::SetLevel(PrintLevel());
823 
824  // set the precision if needed
825  if (Precision() > 0)
826  fState.SetPrecision(Precision());
827 
828  MnParameterScan scan(*fMinuitFCN, fState.Parameters());
829  double amin = scan.Fval(); // fcn value of the function before scan
830 
831  // first value is param value
832  std::vector<std::pair<double, double> > result = scan(ipar, nstep - 1, xmin, xmax);
833 
834  if (prev_level > -2)
835  RestoreGlobalPrintLevel(prev_level);
836 
837  if (result.size() != nstep) {
838  MN_ERROR_MSG2("HybridMinimizer::Scan", " Invalid result from MnParameterScan");
839  return false;
840  }
841  // sort also the returned points in x
842  std::sort(result.begin(), result.end());
843 
844  for (unsigned int i = 0; i < nstep; ++i) {
845  x[i] = result[i].first;
846  y[i] = result[i].second;
847  }
848 
849  // what to do if a new minimum has been found ?
850  // use that as new minimum
851  if (scan.Fval() < amin) {
852  if (PrintLevel() > 0)
853  MN_INFO_MSG2("HybridMinimizer::Scan", "A new minimum has been found");
854  fState.SetValue(ipar, scan.Parameters().Value(ipar));
855  }
856 
857  return true;
858  }
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 216 of file HybridMinimizer.cc.

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

Referenced by PulseShapeFitOOTPileupCorrection::fit().

216  {
217  // add a fixed variable
218  // need a step size otherwise treated as a constant
219  // use 10%
220  double step = (val != 0) ? 0.1 * std::abs(val) : 0.1;
221  if (!SetVariable(ivar, name, val, step)) {
222  ivar = fState.Index(name);
223  }
224  fState.Fix(ivar);
225  return true;
226  }
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 259 of file HybridMinimizer.cc.

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

Referenced by PulseShapeFitOOTPileupCorrection::fit().

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

set gradient the function to minimize

Definition at line 277 of file HybridMinimizer.cc.

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

277  {
278  // set function to be minimized
279  fDim = func.NDim();
280  if (fMinuitFCN)
281  delete fMinuitFCN;
282  if (!fUseFumili) {
283  fMinuitFCN = new ROOT::Minuit2::FCNGradAdapter<ROOT::Math::IMultiGradFunction>(func, ErrorDef());
284  } else {
285  // for Fumili the fit method function interface is required
286  const ROOT::Math::FitMethodGradFunction *fcnfunc = dynamic_cast<const ROOT::Math::FitMethodGradFunction *>(&func);
287  if (!fcnfunc) {
288  MN_ERROR_MSG("HybridMinimizer: Wrong Fit method function for Fumili");
289  return;
290  }
291  fMinuitFCN = new ROOT::Minuit2::FumiliFCNAdapter<ROOT::Math::FitMethodGradFunction>(*fcnfunc, fDim, ErrorDef());
292  }
293  }
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 207 of file HybridMinimizer.cc.

References fState, and SetVariable().

Referenced by PulseShapeFitOOTPileupCorrection::fit().

208  {
209  // add a double bound variable
210  if (!SetVariable(ivar, name, val, step))
211  return false;
212  fState.SetLimits(ivar, lower, upper);
213  return true;
214  }
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 189 of file HybridMinimizer.cc.

References fState, and SetVariable().

190  {
191  // add a lower bounded variable
192  if (!SetVariable(ivar, name, val, step))
193  return false;
194  fState.SetLowerLimit(ivar, lower);
195  return true;
196  }
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
void PSFitter::HybridMinimizer::SetMinimizerType ( EMinimizerType  type)

Definition at line 97 of file HybridMinimizer.cc.

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

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

97  {
98  // Set minimizer algorithm type
99  fUseFumili = false;
100 
101  if (fMinimizer)
102  delete fMinimizer;
103 
104  switch (type) {
105  case kMigrad:
106  //std::cout << "HybridMinimizer: minimize using MIGRAD " << std::endl;
107  SetMinimizer(new ROOT::Minuit2::VariableMetricMinimizer());
108  return;
109  case kSimplex:
110  //std::cout << "HybridMinimizer: minimize using SIMPLEX " << std::endl;
111  SetMinimizer(new ROOT::Minuit2::SimplexMinimizer());
112  return;
113  case kCombined:
114  SetMinimizer(new ROOT::Minuit2::CombinedMinimizer());
115  return;
116  case kScan:
117  SetMinimizer(new ROOT::Minuit2::ScanMinimizer());
118  return;
119  case kFumili:
120  SetMinimizer(new ROOT::Minuit2::FumiliMinimizer());
121  fUseFumili = true;
122  return;
123  default:
124  //migrad minimizer
125  SetMinimizer(new ROOT::Minuit2::VariableMetricMinimizer());
126  }
127  }
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 198 of file HybridMinimizer.cc.

References fState, and SetVariable().

199  {
200  // add a upper bounded variable
201  if (!SetVariable(ivar, name, val, step))
202  return false;
203  fState.SetUpperLimit(ivar, upper);
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
bool PSFitter::HybridMinimizer::SetVariable ( unsigned int  ivar,
const std::string &  name,
double  val,
double  step 
)
override

set free variable

Definition at line 161 of file HybridMinimizer.cc.

References fState, and AlCaHLTBitMon_QueryRunRegistry::string.

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

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

set variable

Definition at line 241 of file HybridMinimizer.cc.

References fState.

241  {
242  // set value for variable ivar (only for existing parameters)
243  if (ivar >= fState.MinuitParameters().size())
244  return false;
245  fState.SetValue(ivar, val);
246  return true;
247  }
ROOT::Minuit2::MnUserParameterState fState
bool PSFitter::HybridMinimizer::SetVariableValues ( const double *  val)
override

Definition at line 249 of file HybridMinimizer.cc.

References fState, and dqmiodumpmetadata::n.

249  {
250  // set value for variable ivar (only for existing parameters)
251  unsigned int n = fState.MinuitParameters().size();
252  if (n == 0)
253  return false;
254  for (unsigned int ivar = 0; ivar < n; ++ivar)
255  fState.SetValue(ivar, x[ivar]);
256  return true;
257  }
ROOT::Minuit2::MnUserParameterState fState
const ROOT::Minuit2::MnUserParameterState& PSFitter::HybridMinimizer::State ( )
inline

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

Definition at line 256 of file HybridMinimizer.h.

References hcaldqm::quantity::fState.

256 { 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 235 of file HybridMinimizer.cc.

References fState.

235  {
236  // return the variable index
237  // check if variable exist
238  return fState.Trafo().FindIndex(name);
239  }
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 228 of file HybridMinimizer.cc.

References fState, and AlCaHLTBitMon_QueryRunRegistry::string.

228  {
229  // return the variable name
230  if (ivar >= fState.MinuitParameters().size())
231  return std::string();
232  return fState.GetName(ivar);
233  }
ROOT::Minuit2::MnUserParameterState fState
const double * PSFitter::HybridMinimizer::X ( ) const
override

return pointer to X values at the minimum

Definition at line 504 of file HybridMinimizer.cc.

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

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

504  {
505  // return values at minimum
506  const std::vector<MinuitParameter> &paramsObj = fState.MinuitParameters();
507  if (paramsObj.empty())
508  return nullptr;
509  assert(fDim == paramsObj.size());
510  // be careful for multiple calls of this function. I will redo an allocation here
511  // only when size of vectors has changed (e.g. after a new minimization)
512  if (fValues.size() != fDim)
513  fValues.resize(fDim);
514  for (unsigned int i = 0; i < fDim; ++i) {
515  fValues[i] = paramsObj[i].Value();
516  }
517 
518  return &fValues.front();
519  }
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 282 of file HybridMinimizer.h.

Referenced by Errors().

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

Definition at line 278 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 274 of file HybridMinimizer.h.

Referenced by SetFunction(), and SetMinimizerType().

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

Definition at line 281 of file HybridMinimizer.h.

Referenced by X().