71using std::sregex_iterator, std::ostream;
82 for (
unsigned int i = 0; i <
collection.size(); ++i) {
93 for (std::size_t i = 0; i < formula.size(); ++i) {
94 if (match && !
isdigit(formula[i])) {
95 formula.insert(formula.begin() + i,
']');
98 }
else if (!match && formula[i] ==
'@') {
100 formula.insert(formula.begin() + i + 1,
'[');
112 for (std::string::size_type pos{};
inOut.npos != (pos =
inOut.find(
what.data(), pos,
what.length()));
113 pos +=
with.length()) {
123 static const std::regex
r{
"\\b"};
124 std::vector<bool> out(s.size() + 1);
126 for (
auto i = std::sregex_iterator(s.begin(), s.end(),
r); i != std::sregex_iterator(); ++i) {
128 m.position()] =
true;
132 out[s.size()] =
true;
141 for (
unsigned int i = 0; i <
varList.size(); ++i) {
148 for (std::string::size_type pos{}; formula.npos != (pos = formula.find(
varName.data(), pos,
varName.length()));
151 std::string::size_type next = pos +
varName.length();
160 if (next < formula.size() && (formula[next] ==
'[' || formula[next] ==
']')) {
182 <<
"Preprocessing formula: replace named references: " <<
varName <<
" --> " <<
replacement <<
"\n\t"
183 << formula << std::endl;
213 std::unique_ptr<TFormula>
newTF;
214 if (
other._tFormula) {
215 newTF = std::make_unique<TFormula>(*
other._tFormula);
216 newTF->SetName(GetName());
228std::string RooFormula::processFormula(std::string formula)
const {
235 cxcoutD(InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
236 << formula << std::endl;
239 static const std::regex
categoryReg(
"(\\w+)::(\\w+)");
244 const std::string
fullMatch = (*matchIt)[0];
245 const std::string
catName = (*matchIt)[1];
246 const std::string
catState = (*matchIt)[2];
250 cxcoutD(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
251 <<
"' but a category '" <<
catName <<
"' cannot be found in the input variables." << std::endl;
256 coutE(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
257 <<
"' but the category '" <<
catName <<
"' does not seem to have the state '" <<
catState <<
"'." << std::endl;
258 throw std::invalid_argument(formula);
265 cxcoutD(InputArguments) <<
"-- End of category tags --"<< std::endl;
272 cxcoutD(InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formula << std::endl;
277 cxcoutD(InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formula << std::endl;
282 cxcoutD(InputArguments) <<
"Final formula:\n\t" << formula << std::endl;
295 const std::string formula(
_tFormula->GetTitle());
320std::string RooFormula::reconstructFormula(std::string
internalRepr)
const {
321 for (
unsigned int i = 0; i <
_origList.size(); ++i) {
324 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
327 std::string
replacement = std::string(
"[") + var.GetName() +
"]";
335void RooFormula::dump()
const
337 printMultiline(std::cout, 0);
358 if (arg->getStringAttribute(
"origName")) {
365 coutE(LinkStateMgmt) <<
__func__ <<
": cannot find replacement for " << arg->GetName() << std::endl;
384double RooFormula::eval(
const RooArgSet* nset)
const
387 coutF(Eval) <<
__func__ <<
" (" << GetName() <<
"): Formula didn't compile: " << GetTitle() << std::endl;
388 std::string
what =
"Formula ";
390 what +=
" didn't compile.";
391 throw std::runtime_error(
what);
394 std::vector<double> pars;
396 for (
unsigned int i = 0; i <
_origList.size(); ++i) {
399 pars.push_back(cat.getCurrentIndex());
402 pars.push_back(
real.getVal(nset));
415 for (
int i = 0; i <
nPars; i++) {
420 std::vector<double> pars(
nPars);
421 for (
size_t i = 0; i <
output.size(); i++) {
434 os <<
indent <<
"--- RooFormula ---" << std::endl;
435 os <<
indent <<
" Formula: '" << GetTitle() <<
"'" << std::endl;
446void RooFormula::printValue(ostream& os)
const
455void RooFormula::printName(ostream& os)
const
464void RooFormula::printTitle(ostream& os)
const
473void RooFormula::printClassName(ostream& os)
const
482void RooFormula::printArgs(ostream& os)
const
484 os <<
"[ actualVars=";
486 os <<
" " << arg->GetName();
495void RooFormula::installFormulaOrThrow(
const std::string& formula) {
498 cxcoutD(InputArguments) <<
"RooFormula '" << GetName() <<
"' will be compiled as "
502 <<
"\n with the parameters " <<
_origList << std::endl;
507 std::stringstream
msg;
508 msg <<
"RooFormula '" << GetName() <<
"' did not compile or is invalid."
509 <<
"\nInput:\n\t" << formula
512 throw std::runtime_error(
msg.str());
519 std::stringstream
msg;
520 msg <<
"TFormula interprets the formula " << formula <<
" as " <<
theFormula->GetNdim() <<
"-dimensional with the variable(s) {";
521 for (
int i=1; i <
theFormula->GetNdim(); ++i) {
530 msg <<
"}, which could not be supplied by RooFit."
531 <<
"\nThe formula must be modified, or those variables must be supplied in the list of variables." << std::endl;
533 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
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.
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()
A 'mix-in' base class that define the standard RooFit plotting and printing methods.
The TNamed class is the base class for all named ROOT classes.
Mother of all ROOT objects.
RooCmdArg ClassName(const char *name)
void replaceAll(std::string &inOut, std::string_view what, std::string_view with)