12 #if __cplusplus >= 201103L 13 #define ROOT_CPLUSPLUS11 1 28 #include <unordered_map> 42 #pragma optimize("",off) 154 char ops[] = {
'+',
'^',
'-',
'/',
'*',
'<',
'>',
'|',
'&',
'!',
'=',
'?'};
155 Int_t opsLen =
sizeof(ops)/
sizeof(
char);
156 for(
Int_t i = 0; i < opsLen; ++i)
164 char brackets[] = {
')',
'(',
'{',
'}'};
165 Int_t bracketsLen =
sizeof(brackets)/
sizeof(
char);
166 for(
Int_t i = 0; i < bracketsLen; ++i)
174 return !IsBracket(c) && !IsOperator(c) && c !=
',' && c !=
' ';
179 return name ==
"x" || name ==
"z" || name ==
"y" || name ==
"t";
186 if ( (formula[i] ==
'e' || formula[i] ==
'E') && (i > 0 && i < formula.Length()-1) ) {
188 if ( (isdigit(formula[i-1]) || formula[i-1] ==
'.') && ( isdigit(formula[i+1]) || formula[i+1] ==
'+' || formula[i+1] ==
'-' ) )
197 if ( (formula[i] ==
'x' || formula[i] ==
'X') && (i > 0 && i < formula.Length()-1) && formula[i-1] ==
'0') {
198 if (isdigit(formula[i+1]) )
200 static char hex_values[12] = {
'a',
'A',
'b',
'B',
'c',
'C',
'd',
'D',
'e',
'E',
'f',
'F'};
201 for (
int jjj = 0; jjj < 12; ++jjj) {
202 if (formula[i+1] == hex_values[jjj])
220 if ( a[0] ==
'p' && a.Length() > 1) {
221 if ( b[0] ==
'p' && b.Length() > 1) {
223 TString lhs =
a(1,a.Length()-1);
224 TString rhs =
b(1,b.Length()-1);
225 if (lhs.IsDigit() && rhs.IsDigit() )
226 return (lhs.Atoi() < rhs.Atoi() );
233 if ( b[0] ==
'p' && b.Length() > 1)
238 if (a.IsDigit() && b.IsDigit() )
239 return (a.Atoi() < b.Atoi() );
251 fReadyToExecute =
false;
252 fClingInitialized =
false;
253 fAllParametersSetted =
false;
260 fLambdaPtr =
nullptr;
266 if (strlen(name)!=1)
return false;
267 for (
auto const & specialName : {
"x",
"y",
"z",
"t"}){
268 if (strcmp(name,specialName)==0)
return true;
281 gROOT->GetListOfFunctions()->Remove(
this);
288 int nLinParts = fLinearParts.size();
290 for (
int i = 0; i < nLinParts; ++i)
delete fLinearParts[i];
296 fClingInput(formula),fFormula(formula)
310 if (addToGlobList &&
gROOT) {
313 old =
dynamic_cast<TFormula*
> (
gROOT->GetListOfFunctions()->FindObject(name) );
315 gROOT->GetListOfFunctions()->Remove(old);
317 Error(
"TFormula",
"The name %s is reserved as a TFormula variable name.\n",name);
319 gROOT->GetListOfFunctions()->Add(
this);
347 for (
int i = 0; i < npar; ++i) {
351 assert (
fNpar == npar);
361 if (addToGlobList &&
gROOT) {
364 old =
dynamic_cast<TFormula*
> (
gROOT->GetListOfFunctions()->FindObject(name) );
366 gROOT->GetListOfFunctions()->Remove(old);
368 Error(
"TFormula",
"The name %s is reserved as a TFormula variable name.\n",name);
370 gROOT->GetListOfFunctions()->Add(
this);
375 Error(
"TFormula",
"Syntax error in building the lambda expression %s", formula );
406 Error(
"TFormula",
"Syntax error in building the lambda expression %s",
fFormula.Data() );
422 gROOT->GetListOfFunctions()->Remove(old);
425 Error(
"TFormula",
"The name %s is reserved as a TFormula variable name.\n",formula.
GetName());
427 gROOT->GetListOfFunctions()->Add(
this);
446 std::string lambdaExpression = formula;
462 TString lambdaName =
TString::Format(
"lambda__id%zu", hasher(lambdaExpression) );
466 TString lineExpr =
TString::Format(
"std::function<double(double*,double*)> %s = %s ;",lambdaName.Data(), lambdaExpression.c_str() );
489 TString formula = expression;
490 if (formula.IsNull() ) {
492 if (formula.IsNull() ) formula =
GetTitle();
495 if (formula.IsNull() )
return -1;
507 return (ret) ? 0 : 1;
517 return (ret) ? 0 : 1;
544 for (
int i = 0; i < nLinParts; ++i)
delete fnew.
fLinearParts[i];
551 for (
int i = 0; i < nLinParts; ++i) {
555 linearOld->
Copy(*linearNew);
578 Error(
"TFormula",
"Syntax error in building the lambda expression %s",
fFormula.Data() );
620 for (
int i = 0; i < nLinParts; ++i)
delete fLinearParts[i];
638 TString prototypeArguments =
"";
641 prototypeArguments.Append(
"Double_t*");
643 if(hasVariables && hasParameters)
645 prototypeArguments.Append(
",");
649 prototypeArguments.Append(
"Double_t*");
655 Error(
"Eval",
"Can't find %s function prototype with arguments %s",
fClingName.Data(),prototypeArguments.Data());
699 const TString defvars[] = {
"x",
"y",
"z",
"t"};
708 const pair<TString,TString> funShortcuts[] =
709 { {
"sin",
"TMath::Sin" },
710 {
"cos",
"TMath::Cos" }, {
"exp",
"TMath::Exp"}, {
"log",
"TMath::Log"}, {
"log10",
"TMath::Log10"},
711 {
"tan",
"TMath::Tan"}, {
"sinh",
"TMath::SinH"}, {
"cosh",
"TMath::CosH"},
712 {
"tanh",
"TMath::TanH"}, {
"asin",
"TMath::ASin"}, {
"acos",
"TMath::ACos"},
713 {
"atan",
"TMath::ATan"}, {
"atan2",
"TMath::ATan2"}, {
"sqrt",
"TMath::Sqrt"},
714 {
"ceil",
"TMath::Ceil"}, {
"floor",
"TMath::Floor"}, {
"pow",
"TMath::Power"},
715 {
"binomial",
"TMath::Binomial"},{
"abs",
"TMath::Abs"},
716 {
"min",
"TMath::Min"},{
"max",
"TMath::Max"},{
"sign",
"TMath::Sign" },
720 std::vector<TString> defvars2(10);
721 for (
int i = 0; i < 9; ++i)
724 for(
auto var : defvars)
726 int pos =
fVars.size();
739 for(
auto con : defconsts)
741 fConsts[con.first] = con.second;
743 for(
auto fun : funShortcuts)
799 Int_t polPos = formula.Index(
"pol");
800 while(polPos !=
kNPOS)
803 Bool_t defaultVariable =
false;
805 Int_t openingBracketPos = formula.Index(
'(',polPos);
806 Bool_t defaultCounter = openingBracketPos ==
kNPOS;
807 Bool_t defaultDegree =
true;
808 Int_t degree,counter;
814 sdegree = formula(polPos + 3,openingBracketPos - polPos - 3);
815 if (!sdegree.IsDigit() ) defaultCounter =
true;
817 if (!defaultCounter) {
818 degree = sdegree.Atoi();
819 counter = TString(formula(openingBracketPos+1,formula.Index(
')',polPos) - openingBracketPos)).Atoi();
823 Int_t temp = polPos+3;
824 while(temp < formula.Length() && isdigit(formula[temp]))
826 defaultDegree =
false;
829 degree = TString(formula(polPos+3,temp - polPos - 3)).Atoi();
834 if(polPos - 1 < 0 || !
IsFunctionNameChar(formula[polPos-1]) || formula[polPos-1] ==
':' )
837 defaultVariable =
true;
841 Int_t tmp = polPos - 1;
846 variable = formula(tmp + 1, polPos - (tmp+1));
848 Int_t param = counter + 1;
853 replacement.Append(
TString::Format(
"+[%d]*%s^%d",param,variable.Data(),tmp));
861 replacement.Insert(0,
'(');
862 replacement.Append(
')');
865 if(defaultCounter && !defaultDegree)
867 pattern =
TString::Format(
"%spol%d",(defaultVariable ?
"" : variable.Data()),degree);
869 else if(defaultCounter && defaultDegree)
871 pattern =
TString::Format(
"%spol",(defaultVariable ?
"" : variable.Data()));
875 pattern =
TString::Format(
"%spol%d(%d)",(defaultVariable ?
"" : variable.Data()),degree,counter);
878 if (!formula.Contains(pattern)) {
879 Error(
"HandlePolN",
"Error handling polynomial function - expression is %s - trying to replace %s with %s ", formula.Data(), pattern.Data(), replacement.Data() );
882 if (formula == pattern) {
887 formula.ReplaceAll(pattern,replacement);
888 polPos = formula.Index(
"pol");
910 map< pair<TString,Int_t> ,pair<TString,TString> > functions;
911 functions.insert(make_pair(make_pair(
"gaus",1),make_pair(
"[0]*exp(-0.5*(({V0}-[1])/[2])*(({V0}-[1])/[2]))",
"[0]*exp(-0.5*(({V0}-[1])/[2])*(({V0}-[1])/[2]))/(sqrt(2*pi)*[2])")));
912 functions.insert(make_pair(make_pair(
"landau",1),make_pair(
"[0]*TMath::Landau({V0},[1],[2],false)",
"[0]*TMath::Landau({V0},[1],[2],true)")));
913 functions.insert(make_pair(make_pair(
"expo",1),make_pair(
"exp([0]+[1]*{V0})",
"")));
914 functions.insert(make_pair(make_pair(
"crystalball",1),make_pair(
"[0]*ROOT::Math::crystalball_function({V0},[3],[4],[2],[1])",
"[0]*ROOT::Math::crystalball_pdf({V0},[3],[4],[2],[1])")));
915 functions.insert(make_pair(make_pair(
"breitwigner",1),make_pair(
"[0]*ROOT::Math::breitwigner_pdf({V0},[2],[1])",
"[0]*ROOT::Math::breitwigner_pdf({V0},[2],[4],[1])")));
917 functions.insert(make_pair(make_pair(
"cheb0" ,1),make_pair(
"ROOT::Math::Chebyshev0({V0},[0])",
"")));
918 functions.insert(make_pair(make_pair(
"cheb1" ,1),make_pair(
"ROOT::Math::Chebyshev1({V0},[0],[1])",
"")));
919 functions.insert(make_pair(make_pair(
"cheb2" ,1),make_pair(
"ROOT::Math::Chebyshev2({V0},[0],[1],[2])",
"")));
920 functions.insert(make_pair(make_pair(
"cheb3" ,1),make_pair(
"ROOT::Math::Chebyshev3({V0},[0],[1],[2],[3])",
"")));
921 functions.insert(make_pair(make_pair(
"cheb4" ,1),make_pair(
"ROOT::Math::Chebyshev4({V0},[0],[1],[2],[3],[4])",
"")));
922 functions.insert(make_pair(make_pair(
"cheb5" ,1),make_pair(
"ROOT::Math::Chebyshev5({V0},[0],[1],[2],[3],[4],[5])",
"")));
923 functions.insert(make_pair(make_pair(
"cheb6" ,1),make_pair(
"ROOT::Math::Chebyshev6({V0},[0],[1],[2],[3],[4],[5],[6])",
"")));
924 functions.insert(make_pair(make_pair(
"cheb7" ,1),make_pair(
"ROOT::Math::Chebyshev7({V0},[0],[1],[2],[3],[4],[5],[6],[7])",
"")));
925 functions.insert(make_pair(make_pair(
"cheb8" ,1),make_pair(
"ROOT::Math::Chebyshev8({V0},[0],[1],[2],[3],[4],[5],[6],[7],[8])",
"")));
926 functions.insert(make_pair(make_pair(
"cheb9" ,1),make_pair(
"ROOT::Math::Chebyshev9({V0},[0],[1],[2],[3],[4],[5],[6],[7],[8],[9])",
"")));
927 functions.insert(make_pair(make_pair(
"cheb10",1),make_pair(
"ROOT::Math::Chebyshev10({V0},[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10])",
"")));
929 functions.insert(make_pair(make_pair(
"gaus",2),make_pair(
"[0]*exp(-0.5*(({V0}-[1])/[2])^2 - 0.5*(({V1}-[3])/[4])^2)",
"")));
930 functions.insert(make_pair(make_pair(
"landau",2),make_pair(
"[0]*TMath::Landau({V0},[1],[2],false)*TMath::Landau({V1},[3],[4],false)",
"")));
931 functions.insert(make_pair(make_pair(
"expo",2),make_pair(
"exp([0]+[1]*{V0})",
"exp([0]+[1]*{V0}+[2]*{V1})")));
933 functions.insert(make_pair(make_pair(
"bigaus",2),make_pair(
"[0]*ROOT::Math::bigaussian_pdf({V0},{V1},[2],[4],[5],[1],[3])",
"[0]*ROOT::Math::bigaussian_pdf({V0},{V1},[2],[4],[5],[1],[3])")));
935 map<TString,Int_t> functionsNumbers;
936 functionsNumbers[
"gaus"] = 100;
937 functionsNumbers[
"bigaus"] = 102;
938 functionsNumbers[
"landau"] = 400;
939 functionsNumbers[
"expo"] = 200;
940 functionsNumbers[
"crystalball"] = 500;
943 formula.ReplaceAll(
"xygaus",
"gaus[x,y]");
944 formula.ReplaceAll(
"xgaus",
"gaus[x]");
945 formula.ReplaceAll(
"ygaus",
"gaus[y]");
946 formula.ReplaceAll(
"zgaus",
"gaus[z]");
947 formula.ReplaceAll(
"xexpo",
"expo[x]");
948 formula.ReplaceAll(
"yexpo",
"expo[y]");
949 formula.ReplaceAll(
"zexpo",
"expo[z]");
950 formula.ReplaceAll(
"xylandau",
"landau[x,y]");
951 formula.ReplaceAll(
"xyexpo",
"expo[x,y]");
953 const char * defaultVariableNames[] = {
"x",
"y",
"z"};
955 for(map<pair<TString,Int_t>,pair<TString,TString> >::iterator it = functions.begin(); it != functions.end(); ++it)
958 TString funName = it->first.first;
959 Int_t funDim = it->first.second;
960 Int_t funPos = formula.Index(funName);
965 while(funPos !=
kNPOS)
969 Int_t lastFunPos = funPos + funName.Length();
972 Int_t iposBefore = funPos - 1;
974 if (iposBefore >= 0) {
975 assert( iposBefore < formula.Length() );
976 if (isalpha(formula[iposBefore] ) ) {
978 funPos = formula.Index(funName,lastFunPos);
983 Bool_t isNormalized =
false;
984 if (lastFunPos < formula.Length() ) {
986 isNormalized = (formula[lastFunPos] ==
'n');
987 if (isNormalized) lastFunPos += 1;
988 if (lastFunPos < formula.Length() ) {
989 char c = formula[lastFunPos];
992 if ( isalnum(c ) || ( !
IsOperator(c ) && c !=
'(' && c !=
')' && c !=
'[' && c !=
']' ) ) {
994 funPos = formula.Index(funName,lastFunPos);
1006 TString varList =
"";
1007 Bool_t defaultVariables =
false;
1010 Int_t openingBracketPos = funPos + funName.Length() + (isNormalized ? 1 : 0);
1012 if(openingBracketPos > formula.Length() || formula[openingBracketPos] !=
'[')
1015 variables.resize(dim);
1016 for (
Int_t idim = 0; idim < dim; ++idim)
1017 variables[idim] = defaultVariableNames[idim];
1018 defaultVariables =
true;
1023 closingBracketPos = formula.Index(
']',openingBracketPos);
1024 varList = formula(openingBracketPos+1,closingBracketPos - openingBracketPos - 1);
1025 dim = varList.CountChar(
',') + 1;
1026 variables.resize(dim);
1028 TString varName =
"";
1029 for(
Int_t i = 0 ; i < varList.Length(); ++i)
1033 varName.Append(varList[i]);
1035 if(varList[i] ==
',')
1037 variables[Nvar] = varName;
1044 variables[Nvar] = varName;
1051 pair<TString,Int_t> key = make_pair(funName,dim);
1052 if(functions.find(key) == functions.end())
1054 Error(
"PreProcessFormula",
"Dimension of function %s is detected to be of dimension %d and is not compatible with existing pre-defined function which has dim %d",
1055 funName.Data(),dim,funDim);
1059 funPos = formula.Index(funName,lastFunPos);
1064 Int_t openingParenthesisPos = (closingBracketPos ==
kNPOS) ? openingBracketPos : closingBracketPos + 1;
1065 bool defaultCounter = (openingParenthesisPos > formula.Length() || formula[openingParenthesisPos] !=
'(');
1076 counter = TString(formula(openingParenthesisPos+1,formula.Index(
')',funPos) - openingParenthesisPos -1)).Atoi();
1080 TString body = (isNormalized ? it->second.second : it->second.first);
1081 if(isNormalized && body ==
"")
1083 Error(
"PreprocessFormula",
"%d dimension function %s has no normalized form.",it->first.second,funName.Data());
1086 for(
int i = 0 ; i < body.Length() ; ++i)
1092 Int_t num = TString(body(i,body.Index(
'}',i) - i)).Atoi();
1093 TString variable = variables[num];
1096 body.Replace(i, pattern.Length(),variable,variable.Length());
1097 i += variable.Length()-1;
1099 else if(body[i] ==
'[')
1103 while(tmp < body.Length() && body[tmp] !=
']')
1107 Int_t num = TString(body(i+1,tmp - 1 - i)).Atoi();
1111 body.Replace(i+1,tmp - 1 - i,replacement,replacement.Length());
1112 i += replacement.Length() + 1;
1116 if(defaultCounter && defaultVariables)
1120 (isNormalized ?
"n" :
""));
1122 if(!defaultCounter && defaultVariables)
1126 (isNormalized ?
"n" :
""),
1129 if(defaultCounter && !defaultVariables)
1133 (isNormalized ?
"n":
""),
1136 if(!defaultCounter && !defaultVariables)
1140 (isNormalized ?
"n" :
""),
1144 TString replacement = body;
1147 if (
fNumber == 0 && formula.Length() <= (pattern.Length()-funPos) +1 ) {
1148 fNumber = functionsNumbers[funName] + 10*(dim-1);
1153 formula.Replace(funPos,pattern.Length(),replacement,replacement.Length());
1155 funPos = formula.Index(funName);
1168 Int_t caretPos = formula.Last(
'^');
1169 while(caretPos !=
kNPOS)
1173 Int_t temp = caretPos;
1176 if(formula[temp] ==
')')
1180 while(depth != 0 && temp > 0)
1182 if(formula[temp] ==
')')
1184 if(formula[temp] ==
'(')
1188 if (depth == 0) temp++;
1198 assert(temp+1 >= 0);
1199 Int_t leftPos = temp+1;
1200 left = formula(leftPos, caretPos - leftPos);
1206 if (temp >= formula.Length() ) {
1207 Error(
"HandleExponentiation",
"Invalid position of operator ^");
1210 if(formula[temp] ==
'(')
1214 while(depth != 0 && temp < formula.Length())
1216 if(formula[temp] ==
')')
1218 if(formula[temp] ==
'(')
1226 if (formula[temp] ==
'-' || formula[temp] ==
'+' ) temp++;
1231 while(temp < formula.Length() && ( (depth > 0) || !
IsOperator(formula[temp]) ) )
1237 if (temp < formula.Length() && formula[temp] ==
'(') depth++;
1238 if (temp < formula.Length() && formula[temp] ==
')') {
1246 right = formula(caretPos + 1, (temp - 1) - caretPos );
1250 TString replacement =
TString::Format(
"pow(%s,%s)",left.Data(),right.Data());
1254 formula.Replace(leftPos,pattern.Length(),replacement,replacement.Length());
1256 caretPos = formula.Last(
'^');
1264 Int_t linPos = formula.Index(
"@");
1265 if (linPos ==
kNPOS )
return;
1266 Int_t NofLinParts = formula.CountChar((
int)
'@');
1267 assert(NofLinParts > 0);
1271 while(linPos !=
kNPOS)
1279 while(temp >= 0 && formula[temp] !=
'@')
1283 left = formula(temp+1,linPos - (temp +1));
1286 while(temp < formula.Length() && formula[temp] !=
'@')
1290 TString right = formula(linPos+1,temp - (linPos+1));
1293 TString replacement = (first) ?
TString::Format(
"([%d]*(%s))+([%d]*(%s))",Nlinear,left.Data(),Nlinear+1,right.Data()) :
TString::Format(
"+([%d]*(%s))",Nlinear,right.Data());
1294 Nlinear += (first) ? 2 : 1;
1296 formula.ReplaceAll(pattern,replacement);
1304 linPos = formula.Index(
"@");
1318 formula.ReplaceAll(
"**",
"^");
1319 formula.ReplaceAll(
"++",
"@");
1320 formula.ReplaceAll(
" ",
"");
1328 formula.ReplaceAll(
"--",
"- -");
1329 formula.ReplaceAll(
"++",
"+ +");
1377 for(
Int_t i = 0 ; i < formula.Length(); ++i )
1382 if(formula[i] ==
'[')
1387 while(formula[i] !=
']' && i < formula.Length())
1389 param.Append(formula[i++]);
1394 int paramIndex = -1;
1395 if (param.IsDigit() ) {
1396 paramIndex = param.Atoi();
1397 param.Insert(0,
'p');
1400 for (
int idx = 0; idx <= paramIndex; ++idx) {
1409 param.ReplaceAll(
"\\s",
" ");
1413 formula.Replace(tmp,i - tmp, replacement,replacement.Length());
1419 if (formula[i] ==
'\"') {
1423 }
while(formula[i] !=
'\"');
1432 while ( !
IsOperator(formula[i]) && i < formula.Length() ) {
1441 if(isalpha(formula[i]) && !
IsOperator(formula[i]))
1448 if (formula[i] ==
':' && ( (i+1) < formula.Length() ) ) {
1449 if ( formula[i+1] ==
':' ) {
1459 name.Append(formula[i++]);
1462 if(formula[i] ==
'(')
1465 if(formula[i] ==
')')
1473 while(depth != 0 && i < formula.Length())
1477 case '(': depth++;
break;
1478 case ')': depth--;
break;
1479 case ',':
if(depth == 1) args++;
break;
1483 body.Append(formula[i++]);
1486 Int_t originalBodyLen = body.Length();
1488 formula.Replace(i-originalBodyLen,originalBodyLen,body,body.Length());
1489 i += body.Length() - originalBodyLen;
1501 obj =
gROOT->GetListOfFunctions()->FindObject(name);
1506 TF1 *
f1 =
dynamic_cast<TF1*
> (obj);
1521 std::vector<TString> newNames;
1524 newNames.resize(f->
GetNpar() );
1526 for (
int jpar = f->
GetNpar()-1; jpar >= 0; --jpar ) {
1529 if ( pj[0] ==
'p' && TString(pj(1,pj.Length())).IsDigit() ) {
1533 replacementFormula.ReplaceAll(oldName,newName);
1534 newNames[jpar] = newName;
1545 for (
int jpar = 0; jpar < f->
GetNpar(); ++jpar) {
1546 if (nparOffset> 0) {
1548 assert((
int) newNames.size() == f->
GetNpar() );
1556 replacementFormula.Insert(0,
'(');
1557 replacementFormula.Insert(replacementFormula.Length(),
')');
1558 formula.Replace(i-name.Length(),name.Length(), replacementFormula, replacementFormula.Length());
1560 i += replacementFormula.Length()-name.Length();
1571 formula.Replace(i-name.Length(),name.Length(),replacement,replacement.Length());
1604 for(list<TFormulaFunction>::iterator funcsIt =
fFuncs.begin(); funcsIt !=
fFuncs.end(); ++funcsIt)
1617 TString shortcut = it->first;
1618 TString full = it->second;
1621 Ssiz_t index = formula.Index(shortcut,0);
1622 while ( index !=
kNPOS) {
1625 Ssiz_t i2 = index + shortcut.Length();
1626 if ( (index > 0) && (isalpha( formula[index-1] ) || formula[index-1] ==
':' )) {
1627 index = formula.Index(shortcut,i2);
1630 if (i2 < formula.Length() && formula[i2] !=
'(') {
1631 index = formula.Index(shortcut,i2);
1635 formula.Replace(index, shortcut.Length(), full);
1636 Ssiz_t inext = index + full.Length();
1637 index = formula.Index(shortcut,inext);
1641 if(fun.
fName.Contains(
"::"))
1645 size_t index = name.rfind(
"::");
1646 assert(index != std::string::npos);
1647 TString className = fun.
fName(0,fun.
fName(0,index).Length());
1648 TString functionName = fun.
fName(index + 2, fun.
fName.Length());
1654 TIter next(methodList);
1656 while ((p = (
TMethod*) next()))
1658 if (strcmp(p->
GetName(),functionName.Data()) == 0 &&
1685 Info(
"TFormula",
"Could not find %s function with %d argument(s)",fun.
GetName(),fun.
GetNargs());
1705 formula.ReplaceAll(pattern,replacement);
1710 map<TString,TFormulaVariable>::iterator varsIt =
fVars.find(fun.
GetName());
1711 if(varsIt!=
fVars.end())
1714 TString
name = (*varsIt).second.GetName();
1715 Double_t value = (*varsIt).second.fValue;
1719 if(!
fVars[name].fFound)
1724 int varDim = (*varsIt).second.fArrayPos;
1725 if (varDim >=
fNdim) {
1730 if (
v.second.fArrayPos < varDim && !
v.second.fFound ) {
1732 v.second.fFound =
true;
1739 TString replacement =
TString::Format(
"x[%d]",(*varsIt).second.fArrayPos);
1740 formula.ReplaceAll(pattern,replacement);
1750 TString funname = fun.
GetName();
1751 if (funname.Contains(
"x[") && funname.Contains(
"]") ) {
1752 TString sdigit = funname(2,funname.Index(
"]") );
1753 int digit = sdigit.Atoi();
1754 if (digit >=
fNdim) {
1757 for (
int j = 0; j <
fNdim; ++j) {
1761 fVars[vname].fFound =
true;
1770 formula.ReplaceAll(pattern,funname);
1781 if(formula.Index(pattern) !=
kNPOS)
1786 formula.ReplaceAll(pattern,replacement);
1797 map<TString,Double_t>::iterator constIt =
fConsts.find(fun.
GetName());
1802 formula.ReplaceAll(pattern,value);
1825 if (hasParameters && ! hasVariables) {
1828 hasVariables =
true;
1830 Bool_t hasBoth = hasVariables && hasParameters;
1831 Bool_t inputIntoCling = (formula.Length() > 0);
1832 if (inputIntoCling) {
1836 std::string inputFormula = std::string(formula);
1840 TString argumentsPrototype =
1841 TString::Format(
"%s%s%s",(hasVariables ?
"Double_t *x" :
""), (hasBoth ?
"," :
""),
1842 (hasParameters ?
"Double_t *p" :
""));
1856 inputIntoCling =
false;
1875 if(inputIntoCling) {
1896 Bool_t allFunctorsMatched =
true;
1897 for(list<TFormulaFunction>::iterator it =
fFuncs.begin(); it !=
fFuncs.end(); it++)
1901 allFunctorsMatched =
false;
1902 if (it->GetNargs() == 0)
1903 Error(
"ProcessFormula",
"\"%s\" has not been matched in the formula expression",it->GetName() );
1905 Error(
"ProcessFormula",
"Could not find %s function with %d argument(s)",it->GetName(),it->GetNargs());
1908 if (!allFunctorsMatched) {
1916 auto itvar =
fVars.begin();
1919 if ( ! itvar->second.fFound ) {
1921 itvar =
fVars.erase(itvar);
1926 while( itvar !=
fVars.end() );
2005 if (i < 0 || i >= n ) {
2006 Error(
"GetLinearPart",
"Formula %s has only %d linear parts - requested %d",
GetName(),n,i);
2058 Bool_t anyNewVar =
false;
2059 for(
Int_t i = 0 ; i < size; ++i)
2062 const TString & vname = vars[i];
2074 Int_t multiplier = 2;
2089 if(anyNewVar && !
fFormula.IsNull())
2101 Error(
"SetName",
"The name \'%s\' is reserved as a TFormula variable name.\n" 2102 "\tThis function will not be renamed.",name);
2106 auto listOfFunctions =
gROOT->GetListOfFunctions();
2107 TObject* thisAsFunctionInList =
nullptr;
2109 if (listOfFunctions){
2110 thisAsFunctionInList = listOfFunctions->
FindObject(
this);
2111 if (thisAsFunctionInList) listOfFunctions->Remove(thisAsFunctionInList);
2114 if (thisAsFunctionInList) listOfFunctions->Add(thisAsFunctionInList);
2127 for(
Int_t i = 0; i < size; ++i)
2129 pair<TString,Double_t>
v = vars[i];
2132 fVars[v.first].fValue = v.second;
2137 Error(
"SetVariables",
"Variable %s is not defined.",v.first.Data());
2147 TString
sname(name);
2150 Error(
"GetVariable",
"Variable %s is not defined.",sname.Data());
2153 return fVars.find(sname)->second.fValue;
2160 TString
sname(name);
2163 Error(
"GetVarNumber",
"Variable %s is not defined.",sname.Data());
2166 return fVars.find(sname)->second.fArrayPos;
2175 if (ivar < 0 || ivar >=
fNdim)
return "";
2178 for (
auto &
v :
fVars) {
2179 if (
v.second.fArrayPos == ivar)
return v.first;
2181 Error(
"GetVarName",
"Variable with index %d not found !!",ivar);
2193 Error(
"SetVariable",
"Variable %s is not defined.",name.Data());
2237 auto ret =
fParams.insert(std::make_pair(name,pos));
2243 if (ret.first ==
fParams.begin() )
2246 auto previous = (ret.first);
2248 pos = previous->second + 1;
2257 Warning(
"inserting parameter %s at pos %d when vector size is %d \n",name.Data(),pos,(int)
fClingParameters.size() );
2266 for (
auto it = ret.first; it !=
fParams.end(); ++it ) {
2274 if (processFormula) {
2301 Error(
"GetParameter",
"Parameter %s is not defined.",name);
2316 Error(
"GetParameter",
"wrong index used - use GetParameter(name)");
2324 if (ipar < 0 || ipar >=
fNpar)
return "";
2328 if (p.second == ipar)
return p.first.Data();
2330 Error(
"GetParName",
"Parameter with index %d not found !!",ipar);
2364 Error(
"SetParameter",
"Parameter %s is not defined.",name.Data());
2371 for(map<TString,TFormulaVariable>::iterator it = fParams.begin(); it != fParams.end(); it++)
2373 if(!it->second.fFound)
2391 for(
Int_t i = 0 ; i < size ; ++i)
2393 pair<TString,Double_t> p = params[i];
2396 Error(
"SetParameters",
"Parameter %s is not defined",p.first.Data());
2399 fParams[p.first].fValue = p.second;
2400 fParams[p.first].fFound =
true;
2404 for(map<TString,TFormulaVariable>::iterator it =
fParams.begin(); it !=
fParams.end(); it++)
2406 if(!it->second.fFound)
2417 if(!params || size < 0 || size >
fNpar)
return;
2420 Warning(
"SetParameters",
"size is not same of cling parameter size %d - %d",size,
int(
fClingParameters.size()) );
2421 for(
Int_t i = 0; i < size; ++i)
2463 if (param < 0 || param >=
fNpar)
return;
2470 const char *name4,
const char *name5,
const char *name6,
const char *name7,
2471 const char *name8,
const char *name9,
const char *name10)
2488 if (ipar < 0 || ipar >
fNpar) {
2489 Error(
"SetParName",
"Wrong Parameter index %d ",ipar);
2495 if (it.second == ipar) {
2497 fParams.erase(oldName);
2498 fParams.insert(std::make_pair(name, ipar) );
2502 if (oldName.IsNull() ) {
2503 Error(
"SetParName",
"Parameter %d is not existing.",ipar);
2514 if (!formula.IsNull() ) {
2516 for(list<TFormulaFunction>::iterator it =
fFuncs.begin(); it !=
fFuncs.end(); ++it)
2518 if(oldName == it->GetName())
2527 Error(
"SetParName",
"Parameter %s is not defined.",oldName.Data());
2531 TString newName =
name;
2532 newName.ReplaceAll(
" ",
"\\s");
2535 formula.ReplaceAll(pattern,replacement);
2543 return DoEval(x, params);
2550 double xxx[4] = {
x,
y,
z,t};
2558 double xxx[3] = {
x,
y,z};
2566 double xxx[2] = {
x,y};
2589 Error(
"Eval",
"Formula is invalid and not ready to execute ");
2590 for(
auto it =
fFuncs.begin(); it !=
fFuncs.end(); ++it)
2595 printf(
"%s is unknown.\n",fun.
GetName());
2601 std::function<double(double *, double *)> & fptr = * ( (std::function<double(double *, double *)> *)
fLambdaPtr);
2604 double *
v =
const_cast<double*
>(
x);
2605 double * p = (params) ? const_cast<double*>(params) :
const_cast<double*
>(
fClingParameters.data());
2610 Error(
"Eval",
"Formula is invalid or not properly initialized - try calling TFormula::Compile");
2612 #ifdef EVAL_IS_NOT_CONST 2623 double * vars = (
x) ? const_cast<double*>(x) :
const_cast<double*
>(
fClingVariables.data());
2626 (*fFuncPtr)(0, 1, args, &
result);
2628 double * pars = (params) ? const_cast<double*>(params) :
const_cast<double*
>(
fClingParameters.data());
2630 (*fFuncPtr)(0, 2, args, &
result);
2643 TString opt(option);
2652 if (opt.Contains(
"CLING") ) {
2654 std::size_t found = clingFunc.find(
"return");
2655 std::size_t found2 = clingFunc.rfind(
";");
2656 if (found == std::string::npos || found2 == std::string::npos) {
2657 Error(
"GetExpFormula",
"Invalid Cling expression - return default formula expression");
2660 TString clingFormula =
fClingInput(found+7,found2-found-7);
2662 if (!opt.Contains(
"P"))
return clingFormula;
2665 while (i < clingFormula.Length()-2 ) {
2667 if (clingFormula[i] ==
'p' && clingFormula[i+1] ==
'[' && isdigit(clingFormula[i+2]) ) {
2669 while ( isdigit(clingFormula[j]) ) { j++;}
2670 if (clingFormula[j] !=
']') {
2671 Error(
"GetExpFormula",
"Parameters not found - invalid expression - return default cling formula");
2672 return clingFormula;
2674 TString parNumbName = clingFormula(i+2,j-i-2);
2675 int parNumber = parNumbName.Atoi();
2676 assert(parNumber <
fNpar);
2678 clingFormula.Replace(i,j-i+1, replacement );
2679 i += replacement.Length();
2683 return clingFormula;
2685 if (opt.Contains(
"P") ) {
2689 while (i < expFormula.Length()-2 ) {
2691 if (expFormula[i] ==
'[') {
2693 while ( expFormula[j] !=
']' ) { j++;}
2694 if (expFormula[j] !=
']') {
2695 Error(
"GetExpFormula",
"Parameter names not found - invalid expression - return default formula");
2698 TString parName = expFormula(i+1,j-i-1);
2700 expFormula.Replace(i,j-i+1, replacement );
2701 i += replacement.Length();
2707 Warning(
"GetExpFormula",
"Invalid option - return defult formula expression");
2716 printf(
" Formula expression: \n");
2717 printf(
"\t%s \n",
fFormula.Data() );
2718 TString opt(option);
2723 if (opt.Contains(
"V") ) {
2725 printf(
"List of Variables: \n");
2727 for (
int ivar = 0; ivar <
fNdim ; ++ivar) {
2732 printf(
"List of Parameters: \n");
2737 for (
int ipar = 0; ipar <
fNpar ; ++ipar) {
2741 printf(
"Expression passed to Cling:\n");
2746 Warning(
"Print",
"Formula is not ready to execute. Missing parameters/variables");
2747 for(list<TFormulaFunction>::const_iterator it =
fFuncs.begin(); it !=
fFuncs.end(); ++it)
2752 printf(
"%s is unknown.\n",fun.
GetName());
2776 void TFormula::Streamer(
TBuffer &
b)
2782 if (v <= 8 && v > 3 && v != 6) {
2795 Error(
"Streamer",
"Old formula read from file is NOT valid");
2841 if (
fNpar != (
int) parValues.size() ) {
2842 Error(
"Streamer",
"number of parameters computed (%d) is not same as the stored parameters (%d)",
fNpar,
int(parValues.size()) );
2854 assert(
fNpar == (
int) parValues.size() );
2857 if (
fParams.size() != paramMap.size() ) {
2858 Warning(
"Streamer",
"number of parameters list found (%zu) is not same as the stored one (%zu) - use re-created list",
fParams.size(),paramMap.size()) ;
2876 gROOT->GetListOfFunctions()->Add(
this);
2879 Error(
"Streamer",
"Formula read from file is NOT ready to execute");
2887 Error(
"Streamer",
"Reading version %d is not supported",v);
virtual const char * GetName() const
Returns name of object.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Double_t Floor(Double_t x)
virtual TFormula * GetFormula()
static double p3(double t, double a, double b, double c, double d)
virtual CallFuncIFacePtr_t CallFunc_IFacePtr(CallFunc_t *) const
CallFunc_t * GetCallFunc() const
Bool_t TestBit(UInt_t f) const
virtual void SetName(const char *name)
Set the name of the TNamed.
Buffer base class used for serializing objects.
R__EXTERN TVirtualMutex * gROOTMutex
const TList * GetListOfAllPublicMethods(Bool_t load=kTRUE)
Returns a list of all public methods of this class and its base classes.
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
The TNamed class is the base class for all named ROOT classes.
Double_t Log10(Double_t x)
virtual Bool_t Declare(const char *code)=0
static double p2(double t, double a, double b, double c)
void(* Generic_t)(void *, int, void **, void *)
std::vector< std::vector< double > > Data
Method or function calling interface.
virtual void Delete(Option_t *option="")
Delete this object.
Int_t GetNargs() const
Number of function arguments.
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
The ROOT global object gROOT contains a list of all defined classes.
static double p1(double t, double a, double b)
Bool_t IsValid() const
Return true if the method call has been properly initialized and is usable.
void InitWithPrototype(TClass *cl, const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Initialize the method invocation environment.
#define R__LOCKGUARD2(mutex)
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Mother of all ROOT objects.
you should not use this method at all Int_t Int_t z
Global functions class (global functions are obtained from CINT).
virtual void Copy(TObject &named) const
Copy this to obj.
Short_t Max(Short_t a, Short_t b)
Each ROOT class (see TClass) has a linked list of methods.
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
R__EXTERN TInterpreter * gCling
Int_t GetNargsOpt() const
Number of function optional (default) arguments.
void variables(TString dataset, TString fin="TMVA.root", TString dirName="InputVariables_Id", TString title="TMVA Input Variables", Bool_t isRegression=kFALSE, Bool_t useTMVAStyle=kTRUE)
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual const char * GetTitle() const
Returns title of object.