Skip to content

Commit

Permalink
Merge pull request #16736 from Dr15Jones/fixTopNodeFormulaEvaluator_8_0
Browse files Browse the repository at this point in the history
Fixed precedence order problem with FormulaEvaluator
  • Loading branch information
davidlange6 committed Nov 24, 2016
2 parents e9baaee + 8cabd8f commit 978ca14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
7 changes: 2 additions & 5 deletions CommonTools/Utils/src/FormulaEvaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ namespace {
}
//need to account for closing parenthesis
++leftEvaluatorInfo.nextParseIndex;
leftEvaluatorInfo.evaluator->setPrecedenceToParenthesis();
leftEvaluatorInfo.top->setPrecedenceToParenthesis();
} else {
//Does not start with a '('
int maxParseDistance = 0;
Expand Down Expand Up @@ -315,10 +315,7 @@ namespace {
}
}
} else {
binaryEval->setLeftEvaluator(leftEvaluatorInfo.evaluator);
if (topNode->precedence() > binaryEval->precedence()) {
topNode = fullExpression.evaluator;
}
binaryEval->setLeftEvaluator(leftEvaluatorInfo.top);
}
fullExpression.top = topNode;
return fullExpression;
Expand Down
38 changes: 38 additions & 0 deletions CommonTools/Utils/test/testFormulaEvaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void testFormulaEvaluator::checkEvaluators() {

void
testFormulaEvaluator::checkFormulaEvaluator() {

{
reco::FormulaEvaluator f("5");

Expand Down Expand Up @@ -856,5 +857,42 @@ testFormulaEvaluator::checkFormulaEvaluator() {
}
}

{
reco::FormulaEvaluator f("[2]*([3]*([4]+[5]*TMath::Log(max([0],min([1],x))))*1./([6]+[7]*100./3.*(TMath::Max(0.,1.03091-0.051154*pow(x,-0.154227))-TMath::Max(0.,1.03091-0.051154*TMath::Power(208.,-0.154227)))+[8]*((1+0.04432-1.304*pow(max(30.,min(6500.,x)),-0.4624)+(0+1.724*TMath::Log(max(30.,min(6500.,x))))/max(30.,min(6500.,x)))-(1+0.04432-1.304*pow(208.,-0.4624)+(0+1.724*TMath::Log(208.))/208.))))");

std::vector<double> v = {
55, 2510, 0.997756, 1.000155, 0.979016, 0.001834, 0.982, -0.048, 1.250
};

std::vector<double> x = {100};

auto func = [&v](double x) { return v[2]*(v[3]*(v[4]+v[5]*TMath::Log(std::max(v[0],std::min(v[1],x))))*1./(v[6]+v[7]*100./3.*(TMath::Max(0.,1.03091-0.051154*std::pow(x,-0.154227))-TMath::Max(0.,1.03091-0.051154*TMath::Power(208.,-0.154227)))+v[8]*((1+0.04432-1.304*std::pow(std::max(30.,std::min(6500.,x)),-0.4624)+(0+1.724*TMath::Log(std::max(30.,std::min(6500.,x))))/std::max(30.,std::min(6500.,x)))-(1+0.04432-1.304*std::pow(208.,-0.4624)+(0+1.724*TMath::Log(208.))/208.)))); };

CPPUNIT_ASSERT(compare(f.evaluate(x,v), func(x[0])) );
}

{
std::vector<std::pair<std::string,double> > formulas = {
{"(1+0.04432+(1.724+100.))-1", 101.76832},
{"(1+(1.724+100.)+0.04432)-1", 101.76832},
{"((1.724+100.)+1+0.04432)-1", 101.76832},
{"(1+0.04432+1.724/100.)-1", .06156},
{"(1+1.724/100.+0.04432)-1", .06156},
{"(1.724/100.+1+0.04432)-1", .06156},
{"(1+0.04432+(1.724/100.))-1", (1+0.04432+(1.724/100.))-1 },
{"(1+(1.724/100.)+0.04432)-1", (1+0.04432+(1.724/100.))-1 },
{"((1.724/100.)+1+0.04432)-1", (1+0.04432+(1.724/100.))-1 },
{"0.997756*(1.000155*(0.979016+0.001834*TMath::Log(max(55.,min(2510.,100.))))*1./(0.982+-0.048*100./3.*(TMath::Max(0.,1.03091-0.051154*pow(100.,-0.154227))-TMath::Max(0.,1.03091-0.051154*TMath::Power(208.,-0.154227)))+1.250*((1+0.04432-1.304*pow(max(30.,min(6500.,100.)),-0.4624)+(0+1.724*TMath::Log(max(30.,min(6500.,100.))))/max(30.,min(6500.,100.)))-(1+0.04432-1.304*pow(208.,-0.4624)+(0+1.724*TMath::Log(208.))/208.))))",
0.997756*(1.000155*(0.979016+0.001834*TMath::Log(std::max(55.,std::min(2510.,100.))))*1./(0.982+-0.048*100./3.*(TMath::Max(0.,1.03091-0.051154*std::pow(100.,-0.154227))-TMath::Max(0.,1.03091-0.051154*TMath::Power(208.,-0.154227)))+1.250*((1+0.04432-1.304*std::pow(std::max(30.,std::min(6500.,100.)),-0.4624)+(0+1.724*TMath::Log(std::max(30.,std::min(6500.,100.))))/std::max(30.,std::min(6500.,100.)))-(1+0.04432-1.304*std::pow(208.,-0.4624)+(0+1.724*TMath::Log(208.))/208.)))) }
};

std::vector<double> x = {};
std::vector<double> v = {};
for(auto const& form_val : formulas) {
reco::FormulaEvaluator f(form_val.first);

CPPUNIT_ASSERT(compare( f.evaluate(x,v),form_val.second));
}
}

}

0 comments on commit 978ca14

Please sign in to comment.