Skip to content

Commit

Permalink
[RF] Simplify logic in RooFormulaArgStreamer
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Andrew Owen authored and guitargeek committed Dec 21, 2024
1 parent 863830f commit aa11bd7
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions roofit/hs3/src/JSONFactories_RooFitCore.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -545,25 +545,15 @@ class RooFormulaArgStreamer : public RooFit::JSONIO::Exporter {
const RooArg_t *pdf = static_cast<const RooArg_t *>(func);
elem["type"] << key();
TString expression(pdf->expression());
std::vector<std::pair<std::size_t, const RooAbsArg*>> paramsWithIndex;
paramsWithIndex.reserve(pdf->nParameters());
for (size_t i = 0; i < pdf->nParameters(); ++i) {
paramsWithIndex.emplace_back(i, pdf->getParameter(i));
}
std::sort(paramsWithIndex.begin(), paramsWithIndex.end());
// If the tokens follow the "x[#]" convention, the square braces enclosing each number
// ensures that there is a unique mapping between the token and parameter name
for (auto [idx, par] : paramsWithIndex) {
expression.ReplaceAll(("x[" + std::to_string(idx) + "]").c_str(), par->GetName());
}
// If the tokens follow the "@#" convention, the numbers are not enclosed by braces.
// So there may be tokens with numbers whose lower place value forms a subset string of ones with a higher place value,
// e.g. "@1" is a subset of "@10".
// So the names of these parameters must be applied descending from the highest place value
// in order to ensure each parameter name is uniquely applied to its token.
for (auto it = paramsWithIndex.rbegin(); it != paramsWithIndex.rend(); ++it) {
const RooAbsArg* par = it->second;
std::size_t idx = it->first;
// So there may be tokens with numbers whose lower place value forms a subset string of ones with a higher place
// value, e.g. "@1" is a subset of "@10". So the names of these parameters must be applied descending from the
// highest place value in order to ensure each parameter name is uniquely applied to its token.
for (size_t idx = pdf->nParameters(); idx--;) {
const RooAbsArg *par = pdf->getParameter(idx);
expression.ReplaceAll(("x[" + std::to_string(idx) + "]").c_str(), par->GetName());
expression.ReplaceAll(("@" + std::to_string(idx)).c_str(), par->GetName());
}
elem["expression"] << expression.Data();
Expand Down

0 comments on commit aa11bd7

Please sign in to comment.