Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooTFormulaEvaluator.cxx
Go to the documentation of this file.
1/// \cond ROOFIT_INTERNAL
2
3/*
4 * Project: RooFit
5 *
6 * Copyright (c) 2026, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
14
15#include "RooArgList.h"
16#include "RooFormulaUtils.h"
17#include "RooMsgService.h"
18
19#include "TFormula.h"
20
21#include <sstream>
22
23////////////////////////////////////////////////////////////////////////////////
24/// Construct the TFormula from the processed formula string, checking that it
25/// compiles and also fulfills the assumptions. Throws otherwise, with the
26/// original formula string appearing in the error messages.
27RooTFormulaEvaluator::RooTFormulaEvaluator(const char *name, std::string const &processedFormula,
28 std::string const &origFormula, RooArgList const &varList)
29{
30 auto theFormula = std::make_unique<TFormula>(name, processedFormula.c_str(), /*addToGlobList=*/false);
31
32 if (!theFormula->IsValid()) {
33 std::stringstream msg;
34 msg << "RooFormula '" << name << "' did not compile or is invalid."
35 << "\nInput:\n\t" << origFormula << "\nPassed over to TFormula:\n\t" << processedFormula << std::endl;
36 oocoutF(static_cast<TObject *>(nullptr), InputArguments) << msg.str();
37 throw std::runtime_error(msg.str());
38 }
39
40 if (theFormula->GetNdim() != 0) {
41 // Construct a null-formula with all index placeholders replaced by a constant.
42 TFormula nullFormula{"nullFormula",
43 RooFormulaUtils::reconstructFormula(processedFormula, varList, "1e-18").c_str(),
44 /*addToGlobList=*/false};
45 const auto nullDim = nullFormula.GetNdim();
46 if (nullDim != 0) {
47 // TFormula thinks that we have an n-dimensional formula (n>0), but it shouldn't, as
48 // these vars should have been replaced by constants in the null-formula
49 // since RooFit only uses the syntax x[0], x[1], x[2], ...
50 // This can happen e.g. with variables x,y,z,t that were not supplied in arglist.
51 std::stringstream msg;
52 msg << "TFormula interprets the formula " << origFormula << " as " << theFormula->GetNdim() + nullDim
53 << "-dimensional with undefined variable(s) {";
54 for (auto i = 0; i < nullDim; ++i) {
55 msg << nullFormula.GetVarName(i) << ",";
56 }
57 msg << "}, which could not be supplied by RooFit."
58 << "\nThe formula must be modified, or those variables must be supplied in the list of variables."
59 << std::endl;
60 oocoutF(static_cast<TObject *>(nullptr), InputArguments) << msg.str();
61 throw std::invalid_argument(msg.str());
62 }
63 }
64
65 _tFormula = std::move(theFormula);
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Copy constructor, copying the underlying TFormula.
70RooTFormulaEvaluator::RooTFormulaEvaluator(RooTFormulaEvaluator const &other)
71{
72 if (other._tFormula) {
73 _tFormula = std::make_unique<TFormula>(*other._tFormula);
74 }
75}
76
77RooTFormulaEvaluator::~RooTFormulaEvaluator() = default;
78
79////////////////////////////////////////////////////////////////////////////////
80/// Evaluate the internal TFormula.
81double RooTFormulaEvaluator::eval(const double *vars) const
82{
83 return _tFormula->EvalPar(vars);
84}
85
86std::unique_ptr<RooFormulaEvaluator> RooTFormulaEvaluator::clone() const
87{
88 return std::make_unique<RooTFormulaEvaluator>(*this);
89}
90
91/// \endcond
#define oocoutF(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:142
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
The Formula class.
Definition TFormula.h:89
Mother of all ROOT objects.
Definition TObject.h:42