71using std::sregex_iterator, std::ostream;
79 for (std::size_t i = 0; i < formula.size(); ++i) {
80 if (match && !
isdigit(formula[i])) {
81 formula.insert(formula.begin() + i,
']');
84 }
else if (!match && formula[i] ==
'@') {
86 formula.insert(formula.begin() + i + 1,
'[');
98 for (std::string::size_type pos{};
inOut.npos != (pos =
inOut.find(
what.data(), pos,
what.length()));
99 pos +=
with.length()) {
109 static const std::regex
r{
"\\b"};
110 std::vector<bool> out(s.size() + 1);
112 for (
auto i = std::sregex_iterator(s.begin(), s.end(),
r); i != std::sregex_iterator(); ++i) {
114 m.position()] =
true;
118 out[s.size()] =
true;
127 for (
unsigned int i = 0; i <
varList.size(); ++i) {
134 for (std::string::size_type pos{}; formula.npos != (pos = formula.find(
varName.data(), pos,
varName.length()));
137 std::string::size_type next = pos +
varName.length();
146 if (next < formula.size() && (formula[next] ==
'[' || formula[next] ==
']')) {
168 <<
"Preprocessing formula: replace named references: " <<
varName <<
" --> " <<
replacement <<
"\n\t"
169 << formula << std::endl;
178 for (
unsigned int i = 0; i <
nArgs; ++i) {
179 const auto& var = args[i];
181 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
184 std::string
replacement = std::string(
"[") + var.GetName() +
"]";
196 for (
unsigned int i = 0; i <
nArgs; ++i) {
198 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
255 std::unique_ptr<TFormula>
newTF;
256 if (
other._tFormula) {
257 newTF = std::make_unique<TFormula>(*
other._tFormula);
258 newTF->SetName(GetName());
269std::string RooFormula::processFormula(std::string formula)
const {
276 cxcoutD(InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
277 << formula << std::endl;
280 static const std::regex
categoryReg(
"(\\w+)::(\\w+)");
285 const std::string
fullMatch = (*matchIt)[0];
286 const std::string
catName = (*matchIt)[1];
287 const std::string
catState = (*matchIt)[2];
291 cxcoutD(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
292 <<
"' but a category '" <<
catName <<
"' cannot be found in the input variables." << std::endl;
297 coutE(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
298 <<
"' but the category '" <<
catName <<
"' does not seem to have the state '" <<
catState <<
"'." << std::endl;
299 throw std::invalid_argument(formula);
306 cxcoutD(InputArguments) <<
"-- End of category tags --"<< std::endl;
313 cxcoutD(InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formula << std::endl;
318 cxcoutD(InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formula << std::endl;
323 cxcoutD(InputArguments) <<
"Final formula:\n\t" << formula << std::endl;
333 for (std::size_t i = 0; i <
_varIsUsed.size(); ++i) {
359 if (arg->getStringAttribute(
"origName")) {
366 coutE(LinkStateMgmt) <<
__func__ <<
": cannot find replacement for " << arg->GetName() << std::endl;
381double RooFormula::eval(
const RooArgSet* nset)
const
384 coutF(Eval) <<
__func__ <<
" (" << GetName() <<
"): Formula didn't compile: " << GetTitle() << std::endl;
385 std::string
what =
"Formula ";
387 what +=
" didn't compile.";
388 throw std::runtime_error(
what);
391 std::vector<double> pars;
393 for (
unsigned int i = 0; i <
_origList.size(); ++i) {
396 pars.push_back(cat.getCurrentIndex());
399 pars.push_back(
real.getVal(nset));
413 for (
int i = 0; i <
nPars; i++) {
421 std::vector<double> pars(
nPars);
422 for (
size_t i = 0; i <
output.size(); i++) {
435 os <<
indent <<
"--- RooFormula ---" << std::endl;
436 os <<
indent <<
" Formula: '" << GetTitle() <<
"'" << std::endl;
446void RooFormula::installFormulaOrThrow(
const std::string& formula) {
449 cxcoutD(InputArguments) <<
"RooFormula '" << GetName() <<
"' will be compiled as "
453 <<
"\n with the parameters " <<
_origList << std::endl;
458 std::stringstream
msg;
459 msg <<
"RooFormula '" << GetName() <<
"' did not compile or is invalid."
460 <<
"\nInput:\n\t" << formula
463 throw std::runtime_error(
msg.str());
474 std::stringstream
msg;
475 msg <<
"TFormula interprets the formula " << formula <<
" as " <<
theFormula->GetNdim()+
nullDim <<
"-dimensional with undefined variable(s) {";
476 for (
auto i=0; i <
nullDim; ++i) {
479 msg <<
"}, which could not be supplied by RooFit."
480 <<
"\nThe formula must be modified, or those variables must be supplied in the list of variables." << std::endl;
482 throw std::invalid_argument(
msg.str());
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
const_iterator begin() const
const_iterator end() const
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
A space to attach TBranches.
Abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
Abstract base class for objects that represent a real value and implements functionality common to al...
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
std::span< double > output()
The TNamed class is the base class for all named ROOT classes.
Mother of all ROOT objects.
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)