Skip to content

Commit

Permalink
Adding unit tests for testing the output of the Model methods
Browse files Browse the repository at this point in the history
I have moved the Model_* methods to be public so that they can be unit tested. I am also tweaking the methods so that they use const references to avoid unnecessary copies.
  • Loading branch information
spaulaus authored and sztaylor89 committed Dec 2, 2016
1 parent 6163282 commit 8342231
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
24 changes: 12 additions & 12 deletions Scan/utkscan/core/include/WalkCorrector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class WalkCorrector {
* \param[in] min : The lower bound of the correction range
* \param[in] max : The upper bound of the correction range
* \param[in] par : The vector of parameters to use for the calibration */
void AddChannel(const Identifier& chanID, const std::string model,
double min, double max, const std::vector<double>& par);
void AddChannel(const Identifier &chanID, const std::string &model,
const double &min, const double &max,
const std::vector<double> &par);

/** Returns time correction that should be subtracted from
* the raw time. The channel is identified by Identifier class,
Expand All @@ -60,12 +61,6 @@ class WalkCorrector {
* \return The walk corrected value of raw */
double GetCorrection(Identifier& chanID, double raw) const;

private:
/** Map where key is a channel Identifier
* and value is a vector holding struct with calibration range
* and walk correction model and parameters. */
std::map<Identifier, std::vector<CorrectionParams> > channels_;

/** \return always 0.
* Use if you want to switch off the correction. Also not adding
* the channel to the list results in returning 0 from GetCorrection
Expand All @@ -80,7 +75,7 @@ class WalkCorrector {
* \param [in] par : the vector of parameters for calibration
* \param [in] raw : the raw value to calibrate
* \return The corrected time in pixie units */
double Model_A(const std::vector<double>& par, double raw) const;
double Model_A(const std::vector<double> &par, const double &raw) const;

/** This model was developed for the 93Br experiment
* f(x) = a0 + a1 * x + a2 * x^2 + a3 * x^3 +
Expand All @@ -106,19 +101,19 @@ class WalkCorrector {
* \return corrected time in pixie units */
double Model_B2(const std::vector<double>& par, double raw) const;

/** The correction for Small VANDLE bars
/** The correction for Small VANDLE bars
* the returned value is in ns
* \param [in] par : the vector of parameters for calibration
* \param [in] raw : the raw value to calibrate
* \return corrected time in ns */
double Model_VS(const std::vector<double>& par, double raw) const;
/** The correction for Medium VANDLE bars
/** The correction for Medium VANDLE bars
* the returned value is in ns
* \param [in] par : the vector of parameters for calibration
* \param [in] raw : the raw value to calibrate
* \return corrected time in ns */
double Model_VM(const std::vector<double>& par, double raw) const;
/** The correction for Large VANDLE bars
/** The correction for Large VANDLE bars
* the returned value is in ns
* \param [in] par : the vector of parameters for calibration
* \param [in] raw : the raw value to calibrate
Expand All @@ -136,5 +131,10 @@ class WalkCorrector {
* \param [in] raw : the raw value to calibrate
* \return corrected time in ns */
double Model_VD(const std::vector<double>& par, double raw) const;
private:
/** Map where key is a channel Identifier
* and value is a vector holding struct with calibration range
* and walk correction model and parameters. */
std::map<Identifier, std::vector<CorrectionParams> > channels_;
};
#endif
24 changes: 12 additions & 12 deletions Scan/utkscan/core/source/WalkCorrector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

using namespace std;

void WalkCorrector::AddChannel(const Identifier& chanID,
const std::string model,
double min, double max,
const std::vector<double>& par) {
void WalkCorrector::AddChannel(const Identifier &chanID,
const std::string &model,
const double &min, const double &max,
const std::vector<double> &par) {
CorrectionParams cf;

unsigned required_parameters = 0;
Expand Down Expand Up @@ -158,8 +158,8 @@ double WalkCorrector::Model_None() const {
return(0.0);
}

double WalkCorrector::Model_A(const std::vector<double>& par,
double raw) const {
double WalkCorrector::Model_A(const std::vector<double> &par,
const double &raw) const {
return(par[0] +
par[1] / (par[2] + raw) +
par[3] * exp(-raw / par[4]));
Expand All @@ -181,11 +181,11 @@ double WalkCorrector::Model_B2(const std::vector<double>& par,
double WalkCorrector::Model_VS(const std::vector<double> &par,
double raw) const {
if(raw < 175)
return(1.09099*log(raw)-7.76641);
return(1.09099*log(raw)-7.76641);
if(raw > 3700)
return(0.0);
return(-(9.13743e-12)*pow(raw,3.) + (1.9485e-7)*pow(raw,2.)
-0.000163286*raw-2.13918);
return(0.0);
return -(9.13743e-12)*pow(raw,3.) + (1.9485e-7)*pow(raw,2.)
-0.000163286*raw-2.13918;
}

double WalkCorrector::Model_VB(const std::vector<double> &par,
Expand All @@ -195,9 +195,9 @@ double WalkCorrector::Model_VB(const std::vector<double> &par,

double WalkCorrector::Model_VD(const std::vector<double> &par,
double raw) const {
return(92.7907602830327 * exp(-raw/186091.225414275) +
return 92.7907602830327 * exp(-raw/186091.225414275) +
0.59140785215161 * exp(raw/2068.14618331387) -
95.5388835298589);
95.5388835298589;
}

double WalkCorrector::Model_VM(const std::vector<double> &par,
Expand Down
2 changes: 1 addition & 1 deletion Scan/utkscan/core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(unittest-WalkCorrector unittest-WalkCorrector.cpp
../source/WalkCorrector.cpp)
../source/WalkCorrector.cpp ../source/Identifier.cpp)
target_link_libraries(unittest-WalkCorrector UnitTest++ ${LIBS})
install(TARGETS unittest-WalkCorrector DESTINATION bin/unittests)
88 changes: 87 additions & 1 deletion Scan/utkscan/core/tests/unittest-WalkCorrector.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,95 @@
///@file unittest-WalkCorrector.cpp
///@brief Main driver to test the Walk Corrector.
///@brief Program that will test functionality of the WalkCorrector
///@author S. V. Paulauskas
///@date November 25, 2016
#include <iostream>

#include <cmath>

#include <UnitTest++.h>

#include "Identifier.hpp"
#include "WalkCorrector.hpp"

using namespace std;

///@brief Test for the method Model_None that will always return a zero.
TEST_FIXTURE(WalkCorrector, Test_Model_None) {
CHECK(Model_None() == 0.0);
}

///@brief Test for method Model_A
TEST_FIXTURE(WalkCorrector, Test_ModelA) {
vector<double> par = {0.5,2.1,3.7,0.4,0.1};
double raw = 20.3;
double expected = par[0] + par[1] / (par[2] + raw) +
par[3] * exp(-raw / par[4]);
double result = Model_A(par, raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_B1) {
vector<double> par = {0.5,2.1,3.7,0.4};
double raw = 20.3;
double expected = par[0] + (par[1] + par[2] / (raw + 1.0)) *
exp(-raw / par[3]);
double result = Model_B1(par, raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_B2) {
vector<double> par = {0.5,2.1,3.7};
double raw = 20.3;
double expected = par[0] + par[1] * exp(-raw / par[2]);
double result = Model_B2(par, raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_VS_Low) {
vector<double> par = {0.5,2.1,3.7};
double raw = 20.3;
double expected = 1.09099*log(raw)-7.76641;
double result = Model_VS(par,raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_VS_High) {
vector<double> par = {0.5,2.1,3.7};
double raw = 4000;
double expected = 0.0;
double result = Model_VS(par,raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_VS) {
vector<double> par = {0.5,2.1,3.7};
double raw = 300.;
double expected = -(9.13743e-12)*pow(raw,3.) + (1.9485e-7)*pow(raw,2.)
-0.000163286*raw-2.13918;
double result = Model_VS(par,raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_VB) {
vector<double> par = {0.5,2.1,3.7};
double raw = 300.;
double expected = -(1.07908*log10(raw)-8.27739);
double result = Model_VB(par,raw);
CHECK(result == expected);
}

TEST_FIXTURE(WalkCorrector, Test_Model_VD) {
vector<double> par = {0.5,2.1,3.7};
double raw = 300.;
double expected = 92.7907602830327 * exp(-raw/186091.225414275) +
0.59140785215161 * exp(raw/2068.14618331387) -
95.5388835298589;
double result = Model_VD(par,raw);
CHECK(result == expected);
}

int main(int argv, char* argc[]) {
// Identifier id("unit", "test", 123);

return(UnitTest::RunAllTests());
}

0 comments on commit 8342231

Please sign in to comment.