85 for (
unsigned int i = 0; i < collection.
size(); ++i) {
86 output.push_back(collection[i]->InheritsFrom(RooAbsCategory::Class()));
112 bool checkVariables) :
123 <<
" but only " << useList <<
" seem to be in use."
124 <<
"\n inputs: " << formula << std::endl;
148#if !defined(__GNUC__) || defined(__clang__) || (__GNUC__ > 4) || ( __GNUC__ == 4 && __GNUC_MINOR__ > 8)
149#define ROOFORMULA_HAVE_STD_REGEX
157 cxcoutD(InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
161 std::regex categoryReg(
"(\\w+)::(\\w+)");
162 std::map<std::string, int> categoryStates;
163 for (sregex_iterator matchIt = sregex_iterator(formula.begin(), formula.end(), categoryReg);
164 matchIt != sregex_iterator(); ++matchIt) {
165 assert(matchIt->size() == 3);
166 const std::string fullMatch = (*matchIt)[0];
167 const std::string catName = (*matchIt)[1];
168 const std::string catState = (*matchIt)[2];
172 cxcoutD(InputArguments) <<
"Formula " <<
GetName() <<
" uses '::' to reference a category state as '" << fullMatch
173 <<
"' but a category '" << catName <<
"' cannot be found in the input variables." << endl;
177 if (!catVariable->hasLabel(catState)) {
178 coutE(InputArguments) <<
"Formula " <<
GetName() <<
" uses '::' to reference a category state as '" << fullMatch
179 <<
"' but the category '" << catName <<
"' does not seem to have the state '" << catState <<
"'." << endl;
180 throw std::invalid_argument(formula);
182 const int catNum = catVariable->lookupIndex(catState);
184 categoryStates[fullMatch] = catNum;
185 cxcoutD(InputArguments) <<
"\n\t" << fullMatch <<
"\tname=" << catName <<
"\tstate=" << catState <<
"=" << catNum;
187 cxcoutD(InputArguments) <<
"-- End of category tags --"<< endl;
190 for (
const auto& catState : categoryStates) {
191 std::stringstream replacement;
192 replacement << catState.second;
193 formula = std::regex_replace(formula, std::regex(catState.first), replacement.str());
196 cxcoutD(InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formula << endl;
199 std::regex ordinalRegex(
"@([0-9]+)");
200 formula = std::regex_replace(formula, ordinalRegex,
"x[$1]");
202 cxcoutD(InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formula << endl;
207 auto regex = std::string{
"\\b"} + var.
GetName();
208 regex = std::regex_replace(regex, std::regex(
"([\\[\\]\\{\\}])"),
"\\$1");
209 regex +=
"\\b(?!\\[)";
210 std::regex findParameterRegex(regex);
212 std::stringstream replacement;
213 replacement <<
"x[" << i <<
"]";
214 formula = std::regex_replace(formula, findParameterRegex, replacement.str());
216 cxcoutD(InputArguments) <<
"Preprocessing formula step 4: replace named references: "
217 << var.GetName() <<
" --> " << replacement.str()
218 <<
"\n\t" << formula << endl;
221 cxcoutD(InputArguments) <<
"Final formula:\n\t" << formula << endl;
234 const std::string formula(
_tFormula->GetTitle());
236 std::set<unsigned int> matchedOrdinals;
237 std::regex newOrdinalRegex(
"\\bx\\[([0-9]+)\\]");
238 for (sregex_iterator matchIt = sregex_iterator(formula.begin(), formula.end(), newOrdinalRegex);
239 matchIt != sregex_iterator(); ++matchIt) {
240 assert(matchIt->size() == 2);
241 std::stringstream matchString((*matchIt)[1]);
245 matchedOrdinals.insert(i);
248 for (
unsigned int i : matchedOrdinals) {
262 std::stringstream regexStr;
263 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
264 std::regex regex(regexStr.str());
266 std::string replacement = std::string(
"[") + var.GetName() +
"]";
267 internalRepr = std::regex_replace(internalRepr, regex, replacement);
285 }
catch (std::runtime_error&
e) {
286 coutE(InputArguments) << __func__ <<
": new equation doesn't compile, formula unchanged."
287 <<
"\n" <<
e.what() << endl;
309 bool errorStat =
false;
316 if (arg->getStringAttribute(
"origName")) {
322 }
else if (mustReplaceAll) {
323 coutE(LinkStateMgmt) << __func__ <<
": cannot find replacement for " << arg->GetName() << endl;
345 coutF(Eval) << __func__ <<
" (" <<
GetName() <<
"): Formula didn't compile: " <<
GetTitle() << endl;
346 std::string
what =
"Formula ";
348 what +=
" didn't compile.";
349 throw std::runtime_error(
what);
352 std::vector<double> pars;
357 pars.push_back(cat.getCurrentIndex());
360 pars.push_back(real.getVal(nset));
370 coutF(Eval) << __func__ <<
" (" <<
GetName() <<
"): Formula didn't compile: " <<
GetTitle() << endl;
371 std::string
what =
"Formula ";
373 what +=
" didn't compile.";
374 throw std::runtime_error(
what);
377 std::vector<RooBatchCompute::BracketAdapterWithMask> valueAdapters;
378 std::vector<RooSpan<const double>> inputSpans;
381 auto realArg =
static_cast<const RooAbsReal*
>(arg);
382 auto batch = realArg->
getValues(inputData, nset);
383 assert(!batch.empty());
384 nData = std::max(nData, batch.size());
385 valueAdapters.emplace_back(batch[0], batch);
386 inputSpans.push_back(std::move(batch));
393 for (std::size_t i=0; i < nData; ++i) {
401 pars[j] = valueAdapters[j][i];
416 os <<
indent <<
"--- RooFormula ---" << endl;
430 os << const_cast<RooFormula*>(
this)->eval(0) ;
457 os << IsA()->GetName() ;
466 os <<
"[ actualVars=";
468 os <<
" " << arg->GetName();
480 cxcoutD(InputArguments) <<
"RooFormula '" <<
GetName() <<
"' will be compiled as "
481 <<
"\n\t" << processedFormula
484 <<
"\n with the parameters " <<
_origList << endl;
486 auto theFormula = std::make_unique<TFormula>(
GetName(), processedFormula.c_str(),
false);
488 if (!theFormula || !theFormula->IsValid()) {
489 std::stringstream msg;
490 msg <<
"RooFormula '" <<
GetName() <<
"' did not compile or is invalid."
491 <<
"\nInput:\n\t" << formula
492 <<
"\nPassed over to TFormula:\n\t" << processedFormula << std::endl;
493 coutF(InputArguments) << msg.str();
494 throw std::runtime_error(msg.str());
497 if (theFormula && theFormula->GetNdim() != 1) {
500 bool haveProblem =
false;
501 std::stringstream msg;
502 msg <<
"TFormula interprets the formula " << formula <<
" as " << theFormula->GetNdim() <<
"-dimensional with the variable(s) {";
503 for (
int i=1; i < theFormula->GetNdim(); ++i) {
504 const TString varName = theFormula->GetVarName(i);
509 msg << theFormula->GetVarName(i) <<
",";
512 msg <<
"}, which could not be supplied by RooFit."
513 <<
"\nThe formula must be modified, or those variables must be supplied in the list of variables." << std::endl;
514 coutF(InputArguments) << msg.str();
515 throw std::invalid_argument(msg.str());
523#ifndef ROOFORMULA_HAVE_STD_REGEX
537 TString formulaTString = formula.c_str();
539 cxcoutD(InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
540 << formulaTString.
Data() << endl;
543 TPRegexp categoryReg(
"(\\w+)::(\\w+)");
544 std::map<std::string, int> categoryStates;
547 std::unique_ptr<TObjArray> matches(categoryReg.MatchS(formulaTString,
"", offset, 3));
548 if (matches->GetEntries() == 0)
551 std::string fullMatch =
static_cast<TObjString*
>(matches->At(0))->GetString().Data();
552 std::string catName =
static_cast<TObjString*
>(matches->At(1))->GetString().Data();
553 std::string catState =
static_cast<TObjString*
>(matches->At(2))->GetString().Data();
554 offset = formulaTString.
Index(categoryReg, offset) + fullMatch.size();
558 cxcoutD(InputArguments) <<
"Formula " <<
GetName() <<
" uses '::' to reference a category state as '" << fullMatch
559 <<
"' but a category '" << catName <<
"' cannot be found in the input variables." << endl;
563 const RooCatType* catType = catVariable->lookupType(catState.c_str(),
false);
565 coutE(InputArguments) <<
"Formula " <<
GetName() <<
" uses '::' to reference a category state as '" << fullMatch
566 <<
"' but the category '" << catName <<
"' does not seem to have the state '" << catState <<
"'." << endl;
567 throw std::invalid_argument(formula);
569 const int catNum = catType->
getVal();
571 categoryStates[fullMatch] = catNum;
572 cxcoutD(InputArguments) <<
"\n\t" << fullMatch <<
"\tname=" << catName <<
"\tstate=" << catState <<
"=" << catNum;
573 }
while (offset != -1);
574 cxcoutD(InputArguments) <<
"-- End of category tags --"<< endl;
577 for (
const auto& catState : categoryStates) {
578 std::stringstream replacement;
579 replacement << catState.second;
580 formulaTString.
ReplaceAll(catState.first.c_str(), replacement.str().c_str());
583 cxcoutD(InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formulaTString.
Data() << endl;
589 nsub = ordinalRegex.Substitute(formulaTString,
"x[$1]");
592 cxcoutD(InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formulaTString.
Data() << endl;
598 regex += var.GetName();
599 regex +=
"\\b([^[]|$)";
602 std::stringstream replacement;
603 replacement <<
"x[" << i <<
"]$1";
606 nsub2 = findParameterRegex.Substitute(formulaTString, replacement.str().c_str());
609 cxcoutD(InputArguments) <<
"Preprocessing formula step 4: replace named references: "
610 << var.GetName() <<
" --> " << replacement.str()
611 <<
"\n\t" << formulaTString.
Data() << endl;
614 cxcoutD(InputArguments) <<
"Final formula:\n\t" << formulaTString << endl;
616 return formulaTString.
Data();
629 std::set<unsigned int> matchedOrdinals;
630 TPRegexp newOrdinalRegex(
"\\bx\\[([0-9]+)\\]");
633 std::unique_ptr<TObjArray> matches(newOrdinalRegex.MatchS(formulaTString,
"", offset, 2));
634 if (matches->GetEntries() == 0)
637 std::string fullMatch =
static_cast<TObjString*
>(matches->At(0))->GetString().Data();
638 std::string ordinal =
static_cast<TObjString*
>(matches->At(1))->GetString().Data();
639 offset = formulaTString.
Index(newOrdinalRegex, offset) + fullMatch.size();
641 std::stringstream matchString(ordinal.c_str());
645 matchedOrdinals.insert(i);
646 }
while (offset != -1);
648 for (
unsigned int i : matchedOrdinals) {
660 TString internalReprT = internalRepr.c_str();
664 std::stringstream regexStr;
665 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
666 TPRegexp regex(regexStr.str().c_str());
668 std::string replacement = std::string(
"[") + var.GetName() +
"]";
669 regex.Substitute(internalReprT, replacement.c_str());
672 return internalReprT.
Data();
static void indent(ostringstream &buf, int indent_level)
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
RooAbsArg * findNewServer(const RooAbsCollection &newSet, Bool_t nameChange) const
Find the new server in the specified set that matches the old server.
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
virtual value_type getCurrentIndex() const
Return index number of current state.
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Storage_t::size_type size() const
const char * GetName() const
Returns name of object.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
virtual RooSpan< const double > getValues(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
by this change, please consult the release notes for ROOT 6.24 for guidance on how to make this trans...
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state.
RooPlotable is a 'mix-in' base class that define the standard RooFit plotting and printing methods.
A simple container to hold a batch of data values.
The TNamed class is the base class for all named ROOT classes.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
virtual const char * GetTitle() const
Returns title of object.
virtual const char * GetName() const
Returns name of object.
Collectable string class.
const char * Data() const
TString & ReplaceAll(const TString &s1, const TString &s2)
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
This struct enables passing computation data around between elements of a computation graph.
RooSpan< double > makeBatch(const RooAbsReal *owner, std::size_t size)
Create a writable batch.
static void output(int code)