Skip to content

Commit

Permalink
Merge pull request #14494 from Dr15Jones/addMoreFunctionsToFormulaEva…
Browse files Browse the repository at this point in the history
…luator

Added Erf and Landau functions to FormulaEvaluator
  • Loading branch information
davidlange6 committed May 18, 2016
2 parents 6a90de7 + 5c796ab commit 32f0137
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
22 changes: 22 additions & 0 deletions CommonTools/Utils/src/FormulaEvaluator.cc
Expand Up @@ -15,6 +15,7 @@
#include <functional>
#include <cstdlib>
#include <cmath>
#include "TMath.h"

// user include files
#include "CommonTools/Utils/interface/FormulaEvaluator.h"
Expand Down Expand Up @@ -570,12 +571,33 @@ namespace {
static const std::string k_min("min");
static const std::string k_TMath__Max("TMath::Max");
static const std::string k_TMath__Min("TMath::Min");
static const std::string k_TMath__Erf("TMath::Erf");
static const std::string k_erf("erf");
static const std::string k_TMath__Landau("TMath::Landau");


EvaluatorInfo
FunctionFinder::createEvaluator(std::string::const_iterator iBegin, std::string::const_iterator iEnd) const {
EvaluatorInfo info;

info = checkForSingleArgFunction(iBegin, iEnd, m_expressionFinder,
k_erf, [](double iArg)->double { return std::erf(iArg); } );
if(info.evaluator.get() != nullptr) {
return info;
}

info = checkForSingleArgFunction(iBegin, iEnd, m_expressionFinder,
k_TMath__Erf, [](double iArg)->double { return std::erf(iArg); } );
if(info.evaluator.get() != nullptr) {
return info;
}

info = checkForSingleArgFunction(iBegin, iEnd, m_expressionFinder,
k_TMath__Landau, [](double iArg)->double { return TMath::Landau(iArg); } );
if(info.evaluator.get() != nullptr) {
return info;
}

info = checkForSingleArgFunction(iBegin, iEnd, m_expressionFinder,
k_log, [](double iArg)->double { return std::log(iArg); } );
if(info.evaluator.get() != nullptr) {
Expand Down
1 change: 1 addition & 0 deletions CommonTools/Utils/test/BuildFile.xml
Expand Up @@ -8,6 +8,7 @@
<use name="DataFormats/SiStripCluster"/>
<use name="CommonTools/Utils"/>
<use name="cppunit"/>
<use name="rootmath"/>
</bin>

<bin name="testCommonToolsUtilThreaded" file="testCutParserThreaded.cc">
Expand Down
63 changes: 62 additions & 1 deletion CommonTools/Utils/test/testFormulaEvaluator.cc
Expand Up @@ -11,6 +11,7 @@

#include <algorithm>
#include <cmath>
#include "TMath.h"

class testFormulaEvaluator : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(testFormulaEvaluator);
Expand All @@ -27,7 +28,7 @@ class testFormulaEvaluator : public CppUnit::TestFixture {

namespace {
bool compare(double iLHS, double iRHS) {
return std::fabs(iLHS-iRHS)< 1E-6*std::fabs(iLHS);
return std::fabs(iLHS)!=0 ? (std::fabs(iLHS-iRHS)< 1E-6*std::fabs(iLHS)) : (std::fabs(iLHS) == std::fabs(iRHS));
}
}

Expand Down Expand Up @@ -479,6 +480,30 @@ testFormulaEvaluator::checkFormulaEvaluator() {
CPPUNIT_ASSERT( f.evaluate(emptyV,emptyV) == std::pow(2.,0.3) );
}

{
reco::FormulaEvaluator f("TMath::Erf(2.)");

std::vector<double> emptyV;

CPPUNIT_ASSERT( f.evaluate(emptyV,emptyV) == TMath::Erf(2.) );
}

{
reco::FormulaEvaluator f("erf(2.)");

std::vector<double> emptyV;

CPPUNIT_ASSERT( f.evaluate(emptyV,emptyV) == std::erf(2.) );
}

{
reco::FormulaEvaluator f("TMath::Landau(3.)");

std::vector<double> emptyV;

CPPUNIT_ASSERT( f.evaluate(emptyV,emptyV) == TMath::Landau(3.) );
}

{
reco::FormulaEvaluator f("max(2,1)");

Expand Down Expand Up @@ -534,6 +559,42 @@ testFormulaEvaluator::checkFormulaEvaluator() {
CPPUNIT_ASSERT( f.evaluate(emptyV,emptyV) == -(-2.36997+0.413917*std::log(208.))/208.);
}

{
//For Jet energy corrections
reco::FormulaEvaluator f("2*TMath::Erf(4*(x-1))");

std::vector<double> x ={1.};


std::vector<double> xValues = {1., 2., 3.};
std::vector<double> emptyV;

auto func = [](double x) { return 2*TMath::Erf(4*(x-1)); };

for(auto const xv: xValues) {
x[0] = xv;
CPPUNIT_ASSERT(compare(f.evaluate(x, emptyV),func(x[0])) );
}
}

{
//For Jet energy corrections
reco::FormulaEvaluator f("2*TMath::Landau(2*(x-1))");

std::vector<double> x ={1.};


std::vector<double> xValues = {1., 2., 3.};
std::vector<double> emptyV;

auto func = [](double x) { return 2*TMath::Landau(2*(x-1)); };

for(auto const xv: xValues) {
x[0] = xv;
CPPUNIT_ASSERT(compare(f.evaluate(x, emptyV),func(x[0])) );
}
}

{
//From SimpleJetCorrector
reco::FormulaEvaluator f("([0]+([1]/((log10(x)^2)+[2])))+([3]*exp(-([4]*((log10(x)-[5])*(log10(x)-[5])))))");
Expand Down

0 comments on commit 32f0137

Please sign in to comment.