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) {
118 out[s.size()] =
true;
132 std::stringstream
ss;
138 }
catch (
const std::invalid_argument &
e) {
139 std::stringstream
ssExc;
140 ssExc <<
"RooConstVar named " <<
_rooAbsArg.GetName() <<
" has name or value that "
141 <<
"cannot be converted to number";
142 throw std::invalid_argument(
ssExc.str());
143 }
catch (
const std::out_of_range &
e) {
144 std::stringstream
ssExc;
145 ssExc <<
"RooConstVar named " <<
_rooAbsArg.GetName() <<
" has numeric name or value that "
146 <<
"gets out of a double range";
147 throw std::out_of_range(
ssExc.str());
157 for (
unsigned int i = 0; i <
varList.size(); ++i) {
173 exceptionSs <<
"Variable '" <<
varName <<
"' is not a valid argument for RooFormulaVar. "
174 <<
"Variables with a name that is a number can only be of type RooConstVar "
175 <<
"and have value equal to the name";
184 for (std::string::size_type pos{}; formula.npos != (pos = formula.find(
varName.data(), pos,
varName.length()));
187 std::string::size_type next = pos +
varName.length();
196 if (next < formula.size() && (formula[next] ==
'[' || formula[next] ==
']')) {
218 <<
"Preprocessing formula: replace named references: " <<
varName <<
" --> " <<
replacement <<
"\n\t"
219 << formula << std::endl;
228 for (
unsigned int i = 0; i <
nArgs; ++i) {
229 const auto& var = args[i];
231 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
234 std::string
replacement = std::string(
"[") + var.GetName() +
"]";
246 for (
unsigned int i = 0; i <
nArgs; ++i) {
248 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
305 std::unique_ptr<TFormula>
newTF;
306 if (
other._tFormula) {
307 newTF = std::make_unique<TFormula>(*
other._tFormula);
308 newTF->SetName(GetName());
319std::string RooFormula::processFormula(std::string formula)
const {
326 cxcoutD(InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
327 << formula << std::endl;
330 static const std::regex
categoryReg(
"(\\w+)::(\\w+)");
335 const std::string
fullMatch = (*matchIt)[0];
336 const std::string
catName = (*matchIt)[1];
337 const std::string
catState = (*matchIt)[2];
341 cxcoutD(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
342 <<
"' but a category '" <<
catName <<
"' cannot be found in the input variables." << std::endl;
347 coutE(InputArguments) <<
"Formula " << GetName() <<
" uses '::' to reference a category state as '" <<
fullMatch
348 <<
"' but the category '" <<
catName <<
"' does not seem to have the state '" <<
catState <<
"'." << std::endl;
349 throw std::invalid_argument(formula);
356 cxcoutD(InputArguments) <<
"-- End of category tags --"<< std::endl;
363 cxcoutD(InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formula << std::endl;
368 cxcoutD(InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formula << std::endl;
373 cxcoutD(InputArguments) <<
"Final formula:\n\t" << formula << std::endl;
383 for (std::size_t i = 0; i <
_varIsUsed.size(); ++i) {
409 if (arg->getStringAttribute(
"origName")) {
416 coutE(LinkStateMgmt) <<
__func__ <<
": cannot find replacement for " << arg->GetName() << std::endl;
431double RooFormula::eval(
const RooArgSet* nset)
const
434 coutF(Eval) <<
__func__ <<
" (" << GetName() <<
"): Formula didn't compile: " << GetTitle() << std::endl;
435 std::string
what =
"Formula ";
437 what +=
" didn't compile.";
438 throw std::runtime_error(
what);
441 std::vector<double> pars;
443 for (
unsigned int i = 0; i <
_origList.size(); ++i) {
446 pars.push_back(cat.getCurrentIndex());
449 pars.push_back(
real.getVal(nset));
463 for (
int i = 0; i <
nPars; i++) {
471 std::vector<double> pars(
nPars);
472 for (
size_t i = 0; i <
output.size(); i++) {
485 os <<
indent <<
"--- RooFormula ---" << std::endl;
486 os <<
indent <<
" Formula: '" << GetTitle() <<
"'" << std::endl;
496void RooFormula::installFormulaOrThrow(
const std::string& formula) {
499 cxcoutD(InputArguments) <<
"RooFormula '" << GetName() <<
"' will be compiled as "
503 <<
"\n with the parameters " <<
_origList << std::endl;
508 std::stringstream
msg;
509 msg <<
"RooFormula '" << GetName() <<
"' did not compile or is invalid."
510 <<
"\nInput:\n\t" << formula
513 throw std::runtime_error(
msg.str());
524 std::stringstream
msg;
525 msg <<
"TFormula interprets the formula " << formula <<
" as " <<
theFormula->GetNdim()+
nullDim <<
"-dimensional with undefined variable(s) {";
526 for (
auto i=0; i <
nullDim; ++i) {
529 msg <<
"}, which could not be supplied by RooFit."
530 <<
"\nThe formula must be modified, or those variables must be supplied in the list of variables." << std::endl;
532 throw std::invalid_argument(
msg.str());
int Int_t
Signed integer 4 bytes (int)
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
Common abstract base class for objects that represent a value and a "shape" in RooFit.
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)