Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TFormula::Eval() to reduce code duplication #7804

Merged
merged 1 commit into from
Oct 16, 2023
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
21 changes: 17 additions & 4 deletions hist/hist/inc/TFormula.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename... Args>
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.
Expand Down Expand Up @@ -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 <typename... Args>
Double_t TFormula::Eval(Args... args) const
{
if (sizeof...(args) > 4) {
Error("Eval", "Eval() only support setting upto 4 variables");
}
double xxx[] = {static_cast<Double_t>(args)...};
return EvalPar(xxx, nullptr);
};

#endif
36 changes: 0 additions & 36 deletions hist/hist/src/TFormula.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading