86 for (
unsigned int i = 0; i < collection.
size(); ++i) {
94void convertArobaseReferences(std::string &formula)
97 for (std::size_t i = 0; i < formula.size(); ++i) {
98 if (match && !isdigit(formula[i])) {
99 formula.insert(formula.begin() + i,
']');
102 }
else if (!match && formula[i] ==
'@') {
104 formula.insert(formula.begin() + i + 1,
'[');
116 for (std::string::size_type pos{}; inout.npos != (pos = inout.find(
what.data(), pos,
what.length()));
117 pos += with.length()) {
118 inout.replace(pos,
what.length(), with.data(), with.length());
125std::vector<bool> getWordBoundaryFlags(std::string
const &
s)
127 static const std::regex
r{
"\\b"};
128 std::vector<bool> out(
s.size() + 1);
130 for (
auto i = std::sregex_iterator(
s.begin(),
s.end(),
r); i != std::sregex_iterator(); ++i) {
132 out[
m.position()] =
true;
136 out[
s.size()] =
true;
142void replaceVarNamesWithIndexStyle(std::string &formula,
RooArgList const &varList)
144 std::vector<bool> isWordBoundary = getWordBoundaryFlags(formula);
145 for (
unsigned int i = 0; i < varList.
size(); ++i) {
148 std::stringstream replacementStream;
149 replacementStream <<
"x[" << i <<
"]";
150 std::string replacement = replacementStream.str();
152 for (std::string::size_type pos{}; formula.npos != (pos = formula.find(varName.data(), pos, varName.length()));
153 pos += replacement.size()) {
155 std::string::size_type next = pos + varName.length();
159 if (!isWordBoundary[pos] || !isWordBoundary[next])
164 if (next < formula.size() && (formula[next] ==
'[' || formula[next] ==
']')) {
172 std::size_t nOld = varName.length();
173 std::size_t nNew = replacement.size();
174 auto wbIter = isWordBoundary.begin() + pos;
176 isWordBoundary.insert(wbIter + nOld, nNew - nOld,
false);
177 else if (nNew < nOld)
178 isWordBoundary.erase(wbIter + nNew, wbIter + nOld);
181 formula.replace(pos, varName.length(), replacement);
185 <<
"Preprocessing formula: replace named references: " << varName <<
" --> " << replacement <<
"\n\t"
210 bool checkVariables) :
221 <<
" but only " << useList <<
" seem to be in use."
222 <<
"\n inputs: " << formula << std::endl;
247#if !defined(__GNUC__) || defined(__clang__) || (__GNUC__ > 4) || ( __GNUC__ == 4 && __GNUC_MINOR__ > 8)
248#define ROOFORMULA_HAVE_STD_REGEX
252#ifdef ROOFORMULA_HAVE_STD_REGEX
265 cxcoutD(
InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
269 static const std::regex categoryReg(
"(\\w+)::(\\w+)");
270 std::map<std::string, int> categoryStates;
271 for (sregex_iterator matchIt = sregex_iterator(formula.begin(), formula.end(), categoryReg);
272 matchIt != sregex_iterator(); ++matchIt) {
273 assert(matchIt->size() == 3);
274 const std::string fullMatch = (*matchIt)[0];
275 const std::string catName = (*matchIt)[1];
276 const std::string catState = (*matchIt)[2];
281 <<
"' but a category '" << catName <<
"' cannot be found in the input variables." << endl;
285 if (!catVariable->hasLabel(catState)) {
287 <<
"' but the category '" << catName <<
"' does not seem to have the state '" << catState <<
"'." << endl;
288 throw std::invalid_argument(formula);
290 const int catNum = catVariable->lookupIndex(catState);
292 categoryStates[fullMatch] = catNum;
293 cxcoutD(
InputArguments) <<
"\n\t" << fullMatch <<
"\tname=" << catName <<
"\tstate=" << catState <<
"=" << catNum;
298 for (
const auto& catState : categoryStates) {
299 replaceAll(formula, catState.first, std::to_string(catState.second));
302 cxcoutD(
InputArguments) <<
"Preprocessing formula step 2: replace category tags\n\t" << formula << endl;
305 convertArobaseReferences(formula);
307 cxcoutD(
InputArguments) <<
"Preprocessing formula step 3: replace '@'-references\n\t" << formula << endl;
310 replaceVarNamesWithIndexStyle(formula,
_origList);
325 const std::string formula(
_tFormula->GetTitle());
327 std::set<unsigned int> matchedOrdinals;
328 static const std::regex newOrdinalRegex(
"\\bx\\[([0-9]+)\\]");
329 for (sregex_iterator matchIt = sregex_iterator(formula.begin(), formula.end(), newOrdinalRegex);
330 matchIt != sregex_iterator(); ++matchIt) {
331 assert(matchIt->size() == 2);
332 std::stringstream matchString((*matchIt)[1]);
336 matchedOrdinals.insert(i);
339 for (
unsigned int i : matchedOrdinals) {
353 std::stringstream regexStr;
354 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
355 std::regex regex(regexStr.str());
357 std::string replacement = std::string(
"[") + var.GetName() +
"]";
358 internalRepr = std::regex_replace(internalRepr, regex, replacement);
375 }
catch (std::runtime_error&
e) {
377 <<
"\n" <<
e.what() << endl;
399 bool errorStat =
false;
406 if (arg->getStringAttribute(
"origName")) {
412 }
else if (mustReplaceAll) {
413 coutE(
LinkStateMgmt) << __func__ <<
": cannot find replacement for " << arg->GetName() << endl;
436 std::string
what =
"Formula ";
438 what +=
" didn't compile.";
439 throw std::runtime_error(
what);
442 std::vector<double> pars;
447 pars.push_back(cat.getCurrentIndex());
450 pars.push_back(real.getVal(nset));
461 std::string
what =
"Formula ";
463 what +=
" didn't compile.";
464 throw std::runtime_error(
what);
467 std::vector<RooBatchCompute::BracketAdapterWithMask> valueAdapters;
468 std::vector<RooSpan<const double>> inputSpans;
471 auto realArg =
static_cast<const RooAbsReal*
>(arg);
472 auto batch = realArg->
getValues(inputData, nset);
473 assert(!batch.empty());
474 nData = std::max(nData, batch.size());
475 valueAdapters.emplace_back(batch[0], batch);
476 inputSpans.push_back(std::move(batch));
483 for (std::size_t i=0; i < nData; ++i) {
491 pars[j] = valueAdapters[j][i];
504 std::vector<RooSpan<const double>> inputSpans(nPars);
505 for (
int i=0; i<nPars; i++)
508 std::vector<double> pars(nPars);
509 for (
size_t i=0; i<nEvents; i++)
511 for (
int j=0; j<nPars; j++) pars[j] = inputSpans[j].
size()>1 ? inputSpans[j][i] : inputSpans[j][0];
521 os <<
indent <<
"--- RooFormula ---" << endl;
535 os << const_cast<RooFormula*>(
this)->eval(0) ;
571 os <<
"[ actualVars=";
573 os <<
" " << arg->GetName();
586 <<
"\n\t" << processedFormula
589 <<
"\n with the parameters " <<
_origList << endl;
591 auto theFormula = std::make_unique<TFormula>(
GetName(), processedFormula.c_str(),
false);
593 if (!theFormula || !theFormula->IsValid()) {
594 std::stringstream msg;
595 msg <<
"RooFormula '" <<
GetName() <<
"' did not compile or is invalid."
596 <<
"\nInput:\n\t" << formula
597 <<
"\nPassed over to TFormula:\n\t" << processedFormula << std::endl;
599 throw std::runtime_error(msg.str());
602 if (theFormula && theFormula->GetNdim() != 1) {
605 bool haveProblem =
false;
606 std::stringstream msg;
607 msg <<
"TFormula interprets the formula " << formula <<
" as " << theFormula->GetNdim() <<
"-dimensional with the variable(s) {";
608 for (
int i=1; i < theFormula->GetNdim(); ++i) {
609 const TString varName = theFormula->GetVarName(i);
614 msg << theFormula->GetVarName(i) <<
",";
617 msg <<
"}, which could not be supplied by RooFit."
618 <<
"\nThe formula must be modified, or those variables must be supplied in the list of variables." << std::endl;
620 throw std::invalid_argument(msg.str());
628#ifndef ROOFORMULA_HAVE_STD_REGEX
642 TString formulaTString = formula.c_str();
644 cxcoutD(
InputArguments) <<
"Preprocessing formula step 1: find category tags (catName::catState) in "
645 << formulaTString.
Data() << endl;
648 TPRegexp categoryReg(
"(\\w+)::(\\w+)");
649 std::map<std::string, int> categoryStates;
652 std::unique_ptr<TObjArray> matches(categoryReg.MatchS(formulaTString,
"",
offset, 3));
653 if (matches->GetEntries() == 0)
656 std::string fullMatch =
static_cast<TObjString*
>(matches->At(0))->GetString().Data();
657 std::string catName =
static_cast<TObjString*
>(matches->At(1))->GetString().Data();
658 std::string catState =
static_cast<TObjString*
>(matches->At(2))->GetString().Data();
664 <<
"' but a category '" << catName <<
"' cannot be found in the input variables." << endl;
668 const RooCatType* catType = catVariable->lookupType(catState.c_str(),
false);
671 <<
"' but the category '" << catName <<
"' does not seem to have the state '" << catState <<
"'." << endl;
672 throw std::invalid_argument(formula);
674 const int catNum = catType->
getVal();
676 categoryStates[fullMatch] = catNum;
677 cxcoutD(
InputArguments) <<
"\n\t" << fullMatch <<
"\tname=" << catName <<
"\tstate=" << catState <<
"=" << catNum;
682 for (
const auto& catState : categoryStates) {
683 std::stringstream replacement;
684 replacement << catState.second;
685 formulaTString.
ReplaceAll(catState.first.c_str(), replacement.str().c_str());
694 nsub = ordinalRegex.Substitute(formulaTString,
"x[$1]");
703 regex += var.GetName();
704 regex +=
"\\b([^\\[\\]]|$)";
707 std::stringstream replacement;
708 replacement <<
"x[" << i <<
"]$1";
711 nsub2 = findParameterRegex.Substitute(formulaTString, replacement.str().c_str());
715 << var.GetName() <<
" --> " << replacement.str()
716 <<
"\n\t" << formulaTString.
Data() << endl;
721 return formulaTString.
Data();
735 std::set<unsigned int> matchedOrdinals;
736 TPRegexp newOrdinalRegex(
"\\bx\\[([0-9]+)\\]");
739 std::unique_ptr<TObjArray> matches(newOrdinalRegex.MatchS(formulaTString,
"",
offset, 2));
740 if (matches->GetEntries() == 0)
743 std::string fullMatch =
static_cast<TObjString*
>(matches->At(0))->GetString().Data();
744 std::string ordinal =
static_cast<TObjString*
>(matches->At(1))->GetString().Data();
747 std::stringstream matchString(ordinal.c_str());
751 matchedOrdinals.insert(i);
754 for (
unsigned int i : matchedOrdinals) {
766 TString internalReprT = internalRepr.c_str();
770 std::stringstream regexStr;
771 regexStr <<
"x\\[" << i <<
"\\]|@" << i;
772 TPRegexp regex(regexStr.str().c_str());
774 std::string replacement = std::string(
"[") + var.GetName() +
"]";
775 regex.Substitute(internalReprT, replacement.c_str());
778 return internalReprT.
Data();
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
static void indent(ostringstream &buf, int indent_level)
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
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
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 nameChange) const
Find the new server in the specified set that matches the old server.
A space to attach TBranches.
virtual value_type getCurrentIndex() const
Return index number of current state.
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
virtual bool replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return true for success.
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
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.
const char * GetName() const override
Returns name of object.
const char * GetTitle() const override
Returns title of object.
Collectable string class.
Mother of all ROOT objects.
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
basic_string_view< char > string_view
std::map< DataKey, RooSpan< const double > > DataMap
static constexpr double s
This struct enables passing computation data around between elements of a computation graph.
RooSpan< double > makeBatch(const RooAbsArg *owner, std::size_t size)
Create a writable batch.