Skip to content

Commit a2acd4b

Browse files
GalinBistrev2guitargeek
authored andcommitted
[RF] Freeze parameters in RooMinimizer after setting category states
1 parent e928d42 commit a2acd4b

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

roofit/roofitcore/src/RooMinimizer.cxx

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ void RooMinimizer::determineStatus(bool fitterReturnValue)
332332
/// \param[in] alg Fit algorithm to use. (Optional)
333333
int RooMinimizer::minimize(const char *type, const char *alg)
334334
{
335-
FreezeDisconnectedParametersRAII freeze(this, *_fcn);
336335

337336
if (_cfg.timingAnalysis) {
338337
#ifdef ROOFIT_MULTIPROCESS
@@ -778,6 +777,7 @@ void RooMinimizer::profileStop()
778777

779778
ROOT::Math::IMultiGenFunction *RooMinimizer::getMultiGenFcn() const
780779
{
780+
781781
return _fcn->getMultiGenFcn();
782782
}
783783

@@ -908,19 +908,19 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
908908
{
909909
// fit a user provided FCN function
910910
// create fit parameter settings
911+
911912
// Check number of parameters
912913
unsigned int npar = fcn.NDim();
913914
if (npar == 0) {
914915
coutE(Minimization) << "RooMinimizer::fitFCN(): FCN function has zero parameters" << std::endl;
915916
return false;
916917
}
917918

918-
// initiate the minimizer
919+
// initiate the minimizer
919920
initMinimizer();
920921

921922
// Identify floating RooCategory parameters
922923
RooArgSet floatingCats;
923-
924924
for (auto arg : _fcn->allParams()) {
925925
if (arg->isCategory() && !arg->isConstant())
926926
floatingCats.add(*arg);
@@ -942,7 +942,8 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
942942
}
943943

944944
if (nPdfs == 0) {
945-
945+
coutI(Minimization) << "[fitFCN] No discrete parameters, performing continuous minimization only" << std::endl;
946+
FreezeDisconnectedParametersRAII freeze(this, *_fcn);
946947
bool isValid = _minimizer->Minimize();
947948
if (!_result)
948949
_result = std::make_unique<FitResult>();
@@ -953,8 +954,7 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
953954
}
954955

955956
// set also new parameter values and errors in FitConfig
956-
957-
// Prepare discrete indices
957+
// Prepare discrete indices
958958
std::vector<int> maxIndices;
959959
for (auto *cat : pdfIndices)
960960
maxIndices.push_back(cat->size());
@@ -963,11 +963,10 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
963963
std::map<std::vector<int>, double> nllMap;
964964
std::vector<int> bestIndices(nPdfs, 0);
965965
double bestNLL = 1e30;
966-
bool improved = true;
967966

967+
bool improved = true;
968968
while (improved) {
969969
improved = false;
970-
971970
auto combos = generateOrthogonalCombinations(maxIndices);
972971
reorderCombinations(combos, maxIndices, bestIndices);
973972

@@ -984,7 +983,7 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
984983
wasConst[i] = pdfIndices[i]->isConstant();
985984
pdfIndices[i]->setConstant(true);
986985
}
987-
986+
FreezeDisconnectedParametersRAII freeze(this, *_fcn);
988987
_minimizer->Minimize();
989988

990989
for (size_t i = 0; i < nPdfs; ++i)
@@ -1002,36 +1001,49 @@ bool RooMinimizer::fitFCN(const ROOT::Math::IMultiGenFunction &fcn)
10021001
}
10031002
}
10041003

1005-
for (size_t i = 0; i < nPdfs; ++i)
1004+
for (size_t i = 0; i < nPdfs; ++i) {
10061005
pdfIndices[i]->setIndex(bestIndices[i]);
1006+
pdfIndices[i]->setConstant(true);
1007+
}
1008+
1009+
FreezeDisconnectedParametersRAII freeze(this, *_fcn);
1010+
_minimizer->Minimize();
10071011

1008-
coutI(Minimization) << "All NLL Values per Combination:\n" << std::endl;
1012+
coutI(Minimization) << "All NLL Values per Combination:" << std::endl;
10091013
for (const auto &entry : nllMap) {
10101014
const auto &combo = entry.first;
10111015
double val = entry.second;
1012-
coutI(Minimization) << "Combo: [" << std::endl;
1013-
for (size_t i = 0; i < combo.size(); ++i)
1014-
std::cout << combo[i] << (i + 1 < combo.size() ? ", " : "");
1015-
coutI(Minimization) << "], NLL: " << val << "\n" << std::endl;
1016+
1017+
std::stringstream ss;
1018+
ss << "Combo: [";
1019+
for (size_t i = 0; i < combo.size(); ++i) {
1020+
ss << combo[i];
1021+
if (i + 1 < combo.size())
1022+
ss << ", ";
1023+
}
1024+
ss << "], NLL: " << val;
1025+
1026+
coutI(Minimization) << ss.str() << std::endl;
10161027
}
10171028

1018-
coutI(Minimization) << "DP Best Indices: [" << std::endl;
1029+
std::stringstream ssBest;
1030+
ssBest << "DP Best Indices: [";
10191031
for (size_t i = 0; i < bestIndices.size(); ++i) {
1020-
coutI(Minimization) << bestIndices[i] << std::endl;
1032+
ssBest << bestIndices[i];
10211033
if (i + 1 < bestIndices.size())
1022-
coutI(Minimization) << ", " << std::endl;
1034+
ssBest << ", ";
10231035
}
1024-
coutI(Minimization) << "], NLL = " << bestNLL << "\n" << std::endl;
1036+
ssBest << "], NLL = " << bestNLL;
1037+
1038+
coutI(Minimization) << ssBest.str() << std::endl;
10251039

1026-
// Fill FitResult and update FitConfig
10271040
if (!_result)
10281041
_result = std::make_unique<FitResult>();
10291042
fillResult(true);
10301043
updateFitConfig();
10311044

10321045
return true;
10331046
}
1034-
10351047
bool RooMinimizer::calculateHessErrors()
10361048
{
10371049
// compute the Hesse errors according to configuration

0 commit comments

Comments
 (0)