Skip to content

Commit 767460a

Browse files
ferdymercurydpiparo
authored andcommitted
[hist] allow specifying the floating point precision in GetExpFormula
Fixes #10769
1 parent b4cd47f commit 767460a

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

hist/hist/inc/TFormula.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class TFormula : public TNamed
247247
#ifdef R__HAS_VECCORE
248248
ROOT::Double_v EvalParVec(const ROOT::Double_v *x, const Double_t *params = nullptr) const;
249249
#endif
250-
TString GetExpFormula(Option_t *option="") const;
250+
TString GetExpFormula(Option_t *option = "", const char *fl_format = "%g") const;
251251
TString GetGradientFormula() const;
252252
TString GetHessianFormula() const;
253253
TString GetUniqueFuncName() const {

hist/hist/inc/v5/TFormula.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class TFormula : public TNamed {
237237
virtual Int_t GetNdim() const {return fNdim;}
238238
virtual Int_t GetNpar() const {return fNpar;}
239239
virtual Int_t GetNumber() const {return fNumber;}
240-
virtual TString GetExpFormula(Option_t *option="") const;
240+
virtual TString GetExpFormula(Option_t *option = "", const char *fl_format = "%g") const;
241241
Double_t GetParameter(Int_t ipar) const;
242242
Double_t GetParameter(const char *name) const;
243243
virtual Double_t *GetParameters() const {return fParams;}

hist/hist/src/TFormula.cxx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3599,8 +3599,12 @@ void TFormula::ReInitializeEvalMethod() {
35993599
/// - If option = "P" replace the parameter names with their values
36003600
/// - If option = "CLING" return the actual expression used to build the function passed to cling
36013601
/// - If option = "CLINGP" replace in the CLING expression the parameter with their values
3602+
/// @param fl_format specifies the printf floating point precision when option
3603+
/// contains "p". Default is `%g` (6 decimals). If you need more precision,
3604+
/// change e.g. to `%.9f`, or `%a` for a lossless representation.
3605+
/// @see https://cplusplus.com/reference/cstdio/printf/
36023606

3603-
TString TFormula::GetExpFormula(Option_t *option) const
3607+
TString TFormula::GetExpFormula(Option_t *option, const char *fl_format) const
36043608
{
36053609
TString opt(option);
36063610
if (opt.IsNull() || TestBit(TFormula::kLambda) ) return fFormula;
@@ -3636,7 +3640,7 @@ TString TFormula::GetExpFormula(Option_t *option) const
36363640
TString parNumbName = clingFormula(i+2,j-i-2);
36373641
int parNumber = parNumbName.Atoi();
36383642
assert(parNumber < fNpar);
3639-
TString replacement = TString::Format("%f",GetParameter(parNumber));
3643+
TString replacement = TString::Format(fl_format,GetParameter(parNumber));
36403644
clingFormula.Replace(i,j-i+1, replacement );
36413645
i += replacement.Length();
36423646
}
@@ -3658,7 +3662,7 @@ TString TFormula::GetExpFormula(Option_t *option) const
36583662
return expFormula;
36593663
}
36603664
TString parName = expFormula(i+1,j-i-1);
3661-
TString replacement = TString::Format("%g",GetParameter(parName));
3665+
TString replacement = TString::Format(fl_format, GetParameter(parName));
36623666
expFormula.Replace(i,j-i+1, replacement );
36633667
i += replacement.Length();
36643668
}

hist/hist/src/TFormula_v5.cxx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,8 +3018,12 @@ Double_t TFormula::EvalParOld(const Double_t *x, const Double_t *uparams)
30183018
/// if expression in formula is: "[0]*(x>-[1])+[2]*exp(-[3]*x)"
30193019
/// and parameters are 3.25,-4.01,4.44,-0.04, GetExpFormula("p") will return:
30203020
/// "(3.25*(x>+4.01))+(4.44*exp(+0.04*x))"
3021+
/// @param fl_format specifies the printf floating point precision when option
3022+
/// contains "p". Default is `%g` (6 decimals). If you need more precision,
3023+
/// change e.g. to `%.9f`, or `%a` for a lossless representation.
3024+
/// @see https://cplusplus.com/reference/cstdio/printf/
30213025

3022-
TString TFormula::GetExpFormula(Option_t *option) const
3026+
TString TFormula::GetExpFormula(Option_t *option, const char *fl_format) const
30233027
{
30243028
if (fNoper>0) {
30253029
TString* tab=new TString[fNoper];
@@ -3193,7 +3197,7 @@ TString TFormula::GetExpFormula(Option_t *option) const
31933197
char pbv[100];
31943198
for (j=0;j<fNpar;j++) {
31953199
snprintf(pb,sizeof(pb),"[%d]",j);
3196-
snprintf(pbv,100,"%g",fParams[j]);
3200+
snprintf(pbv, 100, fl_format, fParams[j]);
31973201
ret.ReplaceAll(pb,pbv);
31983202
}
31993203
ret.ReplaceAll("--","+");

0 commit comments

Comments
 (0)