Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hist/hist/inc/TFormula.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class TFormula : public TNamed
#ifdef R__HAS_VECCORE
ROOT::Double_v EvalParVec(const ROOT::Double_v *x, const Double_t *params = nullptr) const;
#endif
TString GetExpFormula(Option_t *option = "", const char *fl_format = "%g") const;
TString GetExpFormula(Option_t *option = "") const;
TString GetGradientFormula() const;
TString GetHessianFormula() const;
TString GetUniqueFuncName() const {
Expand Down
40 changes: 25 additions & 15 deletions hist/hist/src/TFormula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include "ROOT/StringUtils.hxx"

#include <array>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <memory>
#include <unordered_map>
#include <functional>
#include <set>
#include <sstream>
#include <unordered_map>

using std::map, std::pair, std::make_pair, std::list, std::max, std::string;

Expand All @@ -39,6 +41,17 @@ using std::map, std::pair, std::make_pair, std::list, std::max, std::string;

ClassImp(TFormula);

namespace {

std::string doubleToString(double val)
{
std::stringstream ss;
ss << std::setprecision(std::numeric_limits<double>::max_digits10) << val;
return ss.str();
}

} // namespace

/** \class TFormula TFormula.h "inc/TFormula.h"
\ingroup Hist
The Formula class
Expand Down Expand Up @@ -2341,8 +2354,7 @@ void TFormula::ProcessFormula(TString &formula)
map<TString, Double_t>::iterator constIt = fConsts.find(fun.GetName());
if (constIt != fConsts.end()) {
TString pattern = TString::Format("{%s}", fun.GetName());
TString value = TString::Format("%lf", (*constIt).second);
formula.ReplaceAll(pattern, value);
formula.ReplaceAll(pattern, doubleToString(constIt->second));
fun.fFound = true;
// std::cout << "constant with name " << fun.GetName() << " is found " << std::endl;
continue;
Expand Down Expand Up @@ -3606,12 +3618,8 @@ void TFormula::ReInitializeEvalMethod() {
/// - If option = "P" replace the parameter names with their values
/// - If option = "CLING" return the actual expression used to build the function passed to cling
/// - If option = "CLINGP" replace in the CLING expression the parameter with their values
/// @param fl_format specifies the printf floating point precision when option
/// contains "p". Default is `%g` (6 decimals). If you need more precision,
/// change e.g. to `%.9f`, or `%a` for a lossless representation.
/// @see https://cplusplus.com/reference/cstdio/printf/

TString TFormula::GetExpFormula(Option_t *option, const char *fl_format) const
TString TFormula::GetExpFormula(Option_t *option) const
{
TString opt(option);
if (opt.IsNull() || TestBit(TFormula::kLambda) ) return fFormula;
Expand Down Expand Up @@ -3647,9 +3655,10 @@ TString TFormula::GetExpFormula(Option_t *option, const char *fl_format) const
TString parNumbName = clingFormula(i+2,j-i-2);
int parNumber = parNumbName.Atoi();
assert(parNumber < fNpar);
TString replacement = TString::Format(fl_format, GetParameter(parNumber));
clingFormula.Replace(i,j-i+1, replacement );
i += replacement.Length();
std::stringstream ss;
std::string replacement = doubleToString(GetParameter(parNumber));
clingFormula.Replace(i,j-i+1, replacement);
i += replacement.size();
}
i++;
}
Expand All @@ -3669,9 +3678,10 @@ TString TFormula::GetExpFormula(Option_t *option, const char *fl_format) const
return expFormula;
}
TString parName = expFormula(i+1,j-i-1);
TString replacement = TString::Format(fl_format, GetParameter(parName));
expFormula.Replace(i,j-i+1, replacement );
i += replacement.Length();
std::stringstream ss;
std::string replacement = doubleToString(GetParameter(parName));
expFormula.Replace(i,j-i+1, replacement);
i += replacement.size();
}
i++;
}
Expand Down
2 changes: 1 addition & 1 deletion test/TFormulaParsingTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ bool test16() {
// test GetExpFormula
TFormula f("f","[2] + [0]*x + [1]*x*x");
f.SetParameters(1,2,3);
return (f.GetExpFormula("CLING P", "%f") == TString("3.000000+1.000000*x[0]+2.000000*x[0]*x[0] "));
return (f.GetExpFormula("CLING P") == TString("3+1*x[0]+2*x[0]*x[0] "));
}

bool test17() {
Expand Down
Loading