Skip to content

Commit

Permalink
Add a unit test for correctionlib external
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmith- committed Dec 17, 2021
1 parent 39546b2 commit 5bba350
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion PhysicsTools/Utilities/test/BuildFile.xml
@@ -1,10 +1,11 @@
<bin name="testPhysicsToolsUtilities" file="testPolynomial.cc,testParameter.cc,testFunctions.cc,testSimplifications.cc,testFunctionsIO.cc,testVariables.cc,testDerivatives.cc,testPrimitive.cc,testIntegral.cc,testRunner.cpp,testLumiReweight.cc">
<bin name="testPhysicsToolsUtilities" file="testPolynomial.cc,testParameter.cc,testFunctions.cc,testSimplifications.cc,testFunctionsIO.cc,testVariables.cc,testDerivatives.cc,testPrimitive.cc,testIntegral.cc,testRunner.cpp,testLumiReweight.cc,test_correctionlib.cc">
<use name="DataFormats/TrackReco"/>
<use name="DataFormats/TrackerRecHit2D"/>
<use name="DataFormats/Candidate"/>
<use name="DataFormats/PatCandidates"/>
<use name="PhysicsTools/Utilities"/>
<use name="cppunit"/>
<use name="correctionlib"/>
</bin>
<bin file="testZMassFit.cpp" name="testZMassFit">
<use name="rootminuit"/>
Expand Down
60 changes: 60 additions & 0 deletions PhysicsTools/Utilities/test/corrections.json
@@ -0,0 +1,60 @@
{
"schema_version": 2,
"description": "A few test corrections",
"corrections": [
{ "name": "test corr",
"description": null,
"version": 2,
"inputs": [
{ "name": "pt",
"type": "real",
"description": null
},
{ "name": "syst",
"type": "string",
"description": null
}
],
"output": {
"name": "a scale",
"type": "real",
"description": null
},
"generic_formulas": null,
"data": {
"nodetype": "binning",
"input": "pt",
"edges": [ 0.0, 20.0, 40.0, Infinity ],
"content": [
{ "nodetype": "category",
"input": "syst",
"content": [
{ "key": "blah", "value": 1.1 },
{ "key": "blah2", "value": 2.2 }
],
"default": null
},
{ "nodetype": "category",
"input": "syst",
"content": [
{ "key": "blah2", "value": 1.3 },
{ "key": "blah3",
"value": {
"nodetype": "formula",
"expression": "0.25*x + exp([0])",
"parser": "TFormula",
"variables": [ "pt" ],
"parameters": [ 3.1 ]
}
}
],
"default": null
},
1.0
],
"flow": "error"
}
}
],
"compound_corrections": null
}
27 changes: 27 additions & 0 deletions PhysicsTools/Utilities/test/test_correctionlib.cc
@@ -0,0 +1,27 @@
#include <cmath>
#include <cppunit/extensions/HelperMacros.h>
#include "FWCore/ParameterSet/interface/FileInPath.h"
#include "correction.h"

class test_correctionlib : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(test_correctionlib);
CPPUNIT_TEST(checkAll);
CPPUNIT_TEST_SUITE_END();

public:
void setUp() {}
void tearDown() {}
void checkAll();
};

CPPUNIT_TEST_SUITE_REGISTRATION(test_correctionlib);

void test_correctionlib::checkAll() {
edm::FileInPath testfile("PhysicsTools/Utilities/test/corrections.json");
auto cset = correction::CorrectionSet::from_file(testfile.fullPath());
CPPUNIT_ASSERT(cset->at("test corr"));
CPPUNIT_ASSERT_THROW(cset->at("nonexistent"), std::out_of_range);
auto corr = cset->at("test corr");
CPPUNIT_ASSERT(corr->evaluate({12.0, "blah"}) == 1.1);
CPPUNIT_ASSERT(corr->evaluate({31.0, "blah3"}) == 0.25 * 31.0 + std::exp(3.1));
}

0 comments on commit 5bba350

Please sign in to comment.