Skip to content

Commit

Permalink
Merge pull request #32440 from mrodozov/change-minuit-logger-with-cms…
Browse files Browse the repository at this point in the history
…-logger

Change Minuit logger for CMS MessageLogger
  • Loading branch information
cmsbuild authored Dec 18, 2020
2 parents d12b014 + 508c123 commit 4f7462b
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions RecoLocalCalo/HcalRecAlgos/src/HybridMinimizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// Implementation file for class HybridMinimizer

#include "RecoLocalCalo/HcalRecAlgos/src/HybridMinimizer.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "Math/IFunction.h"
#include "Math/IOptions.h"
Expand Down Expand Up @@ -168,17 +169,15 @@ namespace PSFitter {
// std::cout << " add parameter " << name << " " << val << " step " << step << std::endl;

if (step <= 0) {
std::string txtmsg = "Parameter " + name + " has zero or invalid step size - consider it as constant ";
MN_INFO_MSG2("HybridMinimizer::SetVariable", txtmsg);
edm::LogInfo("HybridMinimizer::SetVariable")
.format("Parameter {} has zero or invalid step size - consider it as constant", name);
fState.Add(name, val);
} else
fState.Add(name, val, step);

unsigned int minuit2Index = fState.Index(name);
if (minuit2Index != ivar) {
std::string txtmsg("Wrong index used for the variable " + name);
MN_INFO_MSG2("HybridMinimizer::SetVariable", txtmsg);
MN_INFO_VAL2("HybridMinimizer::SetVariable", minuit2Index);
edm::LogInfo("HybridMinimizer::SetVariable").format("Wrong index used for the variable {} {}", name, minuit2Index);
return false;
}
fState.RemoveLimits(ivar);
Expand Down Expand Up @@ -267,7 +266,7 @@ namespace PSFitter {
// for Fumili the fit method function interface is required
const ROOT::Math::FitMethodFunction *fcnfunc = dynamic_cast<const ROOT::Math::FitMethodFunction *>(&func);
if (!fcnfunc) {
MN_ERROR_MSG("HybridMinimizer: Wrong Fit method function for Fumili");
edm::LogError("HybridMinimizer::SetFunction") << "HybridMinimizer: Wrong Fit method function for Fumili";
return;
}
fMinuitFCN = new ROOT::Minuit2::FumiliFCNAdapter<ROOT::Math::FitMethodFunction>(*fcnfunc, fDim, ErrorDef());
Expand All @@ -285,7 +284,7 @@ namespace PSFitter {
// for Fumili the fit method function interface is required
const ROOT::Math::FitMethodGradFunction *fcnfunc = dynamic_cast<const ROOT::Math::FitMethodGradFunction *>(&func);
if (!fcnfunc) {
MN_ERROR_MSG("HybridMinimizer: Wrong Fit method function for Fumili");
edm::LogError("HybridMinimizer::SetFunction") << "HybridMinimizer: Wrong Fit method function for Fumili";
return;
}
fMinuitFCN = new ROOT::Minuit2::FumiliFCNAdapter<ROOT::Math::FitMethodGradFunction>(*fcnfunc, fDim, ErrorDef());
Expand All @@ -296,7 +295,7 @@ namespace PSFitter {
// perform the minimization
// store a copy of FunctionMinimum
if (!fMinuitFCN) {
MN_ERROR_MSG2("HybridMinimizer::Minimize", "FCN function has not been set");
edm::LogError("HybridMinimizer::Minimize") << "FCN function has not been set";
return false;
}

Expand Down Expand Up @@ -327,7 +326,7 @@ namespace PSFitter {
*/

// internal minuit messages
MnPrint::SetLevel(PrintLevel());
//MnPrint::SetLevel(PrintLevel()); // MnPrint::SetLevel is not a static method anymore. Using it requires an object to exist

// switch off Minuit2 printing
int prev_level = (PrintLevel() <= 0) ? TurnOffPrintInfoLevel() : -2;
Expand Down Expand Up @@ -450,16 +449,15 @@ namespace PSFitter {
if (validMinimum) {
// print a warning message in case something is not ok
if (fStatus != 0 && debugLevel > 0)
MN_INFO_MSG2("HybridMinimizer::Minimize", txt);
edm::LogInfo("HybridMinimizer::Minimize") << txt;
} else {
// minimum is not valid when state is not valid and edm is over max or has passed call limits
if (fStatus == 0) {
// this should not happen
txt = "unknown failure";
fStatus = 5;
}
std::string msg = "Minimization did NOT converge, " + txt;
MN_INFO_MSG2("HybridMinimizer::Minimize", msg);
edm::LogInfo("HybridMinimizer::Minimize").format("Minimization did NOT converge, {}", txt);
}

if (debugLevel >= 1)
Expand Down Expand Up @@ -666,20 +664,20 @@ namespace PSFitter {

int debugLevel = PrintLevel();
// internal minuit messages
MnPrint::SetLevel(debugLevel);
//MnPrint::SetLevel(debugLevel);

// to run minos I need function minimum class
// redo minimization from current state
// ROOT::Minuit2::FunctionMinimum min =
// GetMinimizer()->Minimize(*GetFCN(),fState, ROOT::Minuit2::MnStrategy(strategy), MaxFunctionCalls(), Tolerance());
// fState = min.UserState();
if (fMinimum == nullptr) {
MN_ERROR_MSG("HybridMinimizer::GetMinosErrors: failed - no function minimum existing");
edm::LogError("HybridMinimizer::GetMinosErrors") << " failed - no function minimum existing";
return false;
}

if (!fMinimum->IsValid()) {
MN_ERROR_MSG("HybridMinimizer::MINOS failed due to invalid function minimum");
edm::LogError("HybridMinimizer::MINOS") << " failed due to invalid function minimum";
return false;
}

Expand Down Expand Up @@ -807,19 +805,19 @@ namespace PSFitter {
// if the errors are also zero then scan from min and max of parameter range

if (!fMinuitFCN) {
MN_ERROR_MSG2("HybridMinimizer::Scan", " Function must be set before using Scan");
edm::LogError("HybridMinimizer::Scan") << " Function must be set before using Scan";
return false;
}

if (ipar > fState.MinuitParameters().size()) {
MN_ERROR_MSG2("HybridMinimizer::Scan", " Invalid number. Minimizer variables must be set before using Scan");
edm::LogError("HybridMinimizer::Scan") << " Invalid number. Minimizer variables must be set before using Scan";
return false;
}

// switch off Minuit2 printing
int prev_level = (PrintLevel() <= 0) ? TurnOffPrintInfoLevel() : -2;

MnPrint::SetLevel(PrintLevel());
//MnPrint::SetLevel(PrintLevel());

// set the precision if needed
if (Precision() > 0)
Expand All @@ -835,7 +833,7 @@ namespace PSFitter {
RestoreGlobalPrintLevel(prev_level);

if (result.size() != nstep) {
MN_ERROR_MSG2("HybridMinimizer::Scan", " Invalid result from MnParameterScan");
edm::LogError("HybridMinimizer::Scan") << " Invalid result from MnParameterScan";
return false;
}
// sort also the returned points in x
Expand All @@ -850,7 +848,7 @@ namespace PSFitter {
// use that as new minimum
if (scan.Fval() < amin) {
if (PrintLevel() > 0)
MN_INFO_MSG2("HybridMinimizer::Scan", "A new minimum has been found");
edm::LogInfo("HybridMinimizer::Scan") << "A new minimum has been found";
fState.SetValue(ipar, scan.Parameters().Value(ipar));
}

Expand All @@ -861,12 +859,12 @@ namespace PSFitter {
// contour plot for parameter i and j
// need a valid FunctionMinimum otherwise exits
if (fMinimum == nullptr) {
MN_ERROR_MSG2("HybridMinimizer::Contour", " no function minimum existing. Must minimize function before");
edm::LogError("HybridMinimizer::Contour") << " no function minimum existing. Must minimize function before";
return false;
}

if (!fMinimum->IsValid()) {
MN_ERROR_MSG2("HybridMinimizer::Contour", "Invalid function minimum");
edm::LogError("HybridMinimizer::Contour") << "Invalid function minimum";
return false;
}
assert(fMinuitFCN);
Expand All @@ -879,7 +877,7 @@ namespace PSFitter {
// switch off Minuit2 printing (for level of 0,1)
int prev_level = (PrintLevel() <= 1) ? TurnOffPrintInfoLevel() : -2;

MnPrint::SetLevel(PrintLevel());
//MnPrint::SetLevel(PrintLevel());

// set the precision if needed
if (Precision() > 0)
Expand All @@ -893,7 +891,7 @@ namespace PSFitter {

std::vector<std::pair<double, double> > result = contour(ipar, jpar, npoints);
if (result.size() != npoints) {
MN_ERROR_MSG2("HybridMinimizer::Contour", " Invalid result from MnContours");
edm::LogError("HybridMinimizer::Contour") << " Invalid result from MnContours";
return false;
}
for (unsigned int i = 0; i < npoints; ++i) {
Expand All @@ -911,7 +909,7 @@ namespace PSFitter {
// appended in the function minimum

if (!fMinuitFCN) {
MN_ERROR_MSG2("HybridMinimizer::Hesse", "FCN function has not been set");
edm::LogError("HybridMinimizer::Hesse") << "FCN function has not been set";
return false;
}

Expand All @@ -921,7 +919,7 @@ namespace PSFitter {
// switch off Minuit2 printing
int prev_level = (PrintLevel() <= 0) ? TurnOffPrintInfoLevel() : -2;

MnPrint::SetLevel(PrintLevel());
//MnPrint::SetLevel(PrintLevel());

// set the precision if needed
if (Precision() > 0)
Expand Down Expand Up @@ -953,7 +951,7 @@ namespace PSFitter {
if (!fState.HasCovariance()) {
// if false means error is not valid and this is due to a failure in Hesse
if (PrintLevel() > 0)
MN_INFO_MSG2("HybridMinimizer::Hesse", "Hesse failed ");
edm::LogInfo("HybridMinimizer::Hesse") << "Hesse failed ";
// update minimizer error status
int hstatus = 4;
// information on error state can be retrieved only if fMinimum is available
Expand Down

0 comments on commit 4f7462b

Please sign in to comment.