From ab71ecdf3867a3f788be7c75f3842e792e3d76da Mon Sep 17 00:00:00 2001 From: Parth Date: Thu, 8 Apr 2021 21:36:46 +0530 Subject: [PATCH] Refactor TFormula::Eval() to reduce code duplication --- hist/hist/inc/TFormula.h | 21 +++++++++++++++++---- hist/hist/src/TFormula.cxx | 36 ------------------------------------ 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/hist/hist/inc/TFormula.h b/hist/hist/inc/TFormula.h index 6ad9f7b670093..28663f9879281 100644 --- a/hist/hist/inc/TFormula.h +++ b/hist/hist/inc/TFormula.h @@ -199,10 +199,8 @@ class TFormula : public TNamed Int_t Compile(const char *expression=""); void Copy(TObject &f1) const override; void Clear(Option_t * option="") override; - Double_t Eval(Double_t x) const; - Double_t Eval(Double_t x, Double_t y) const; - Double_t Eval(Double_t x, Double_t y , Double_t z) const; - Double_t Eval(Double_t x, Double_t y , Double_t z , Double_t t ) const; + template + Double_t Eval(Args... args) const; Double_t EvalPar(const Double_t *x, const Double_t *params = nullptr) const; /// Generate gradient computation routine with respect to the parameters. @@ -314,4 +312,19 @@ void TFormula::SetParNames(Args &&...args) if(!name.empty()) SetParName(i++, name.c_str()); } } + +//////////////////////////////////////////////////////////////////////////////// +/// Set first 1, 2, 3 or 4 variables (e.g. x, y, z and t) +/// and evaluate formula. + +template +Double_t TFormula::Eval(Args... args) const +{ + if (sizeof...(args) > 4) { + Error("Eval", "Eval() only support setting upto 4 variables"); + } + double xxx[] = {static_cast(args)...}; + return EvalPar(xxx, nullptr); +}; + #endif diff --git a/hist/hist/src/TFormula.cxx b/hist/hist/src/TFormula.cxx index a5f4f0c0b0daf..1add76d5e6240 100644 --- a/hist/hist/src/TFormula.cxx +++ b/hist/hist/src/TFormula.cxx @@ -3360,42 +3360,6 @@ ROOT::Double_v TFormula::EvalParVec(const ROOT::Double_v *x, const Double_t *par } #endif -//////////////////////////////////////////////////////////////////////////////// -/// Sets first 4 variables (e.g. x, y, z, t) and evaluate formula. - -Double_t TFormula::Eval(Double_t x, Double_t y, Double_t z, Double_t t) const -{ - double xxx[4] = {x,y,z,t}; - return EvalPar(xxx, nullptr); // takes care of case where formula is vectorized -} - -//////////////////////////////////////////////////////////////////////////////// -/// Sets first 3 variables (e.g. x, y, z) and evaluate formula. - -Double_t TFormula::Eval(Double_t x, Double_t y , Double_t z) const -{ - double xxx[3] = {x,y,z}; - return EvalPar(xxx, nullptr); -} - -//////////////////////////////////////////////////////////////////////////////// -/// Sets first 2 variables (e.g. x and y) and evaluate formula. - -Double_t TFormula::Eval(Double_t x, Double_t y) const -{ - double xxx[2] = {x,y}; - return EvalPar(xxx, nullptr); -} - -//////////////////////////////////////////////////////////////////////////////// -/// Sets first variable (e.g. x) and evaluate formula. - -Double_t TFormula::Eval(Double_t x) const -{ - double * xxx = &x; - return EvalPar(xxx, nullptr); -} - //////////////////////////////////////////////////////////////////////////////// /// Evaluate formula. /// If formula is not ready to execute(missing parameters/variables),