From 27e852f904d4c193db462e2991608acd1152793e Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 19 Oct 2022 11:42:24 +0200 Subject: [PATCH] Remove unnecessary duplicate tests and make comparison more robust --- src/cpp/src/TESTS/test_tracks.cc | 58 ++-------------------------- src/cpp/src/TESTS/test_trackstate.cc | 15 ++++++- src/cpp/src/TESTS/tutil.h | 28 ++++++++++++-- 3 files changed, 41 insertions(+), 60 deletions(-) diff --git a/src/cpp/src/TESTS/test_tracks.cc b/src/cpp/src/TESTS/test_tracks.cc index 0deb3f6f4..0bcdd29de 100644 --- a/src/cpp/src/TESTS/test_tracks.cc +++ b/src/cpp/src/TESTS/test_tracks.cc @@ -229,7 +229,9 @@ int main(int /*argc*/, char** /*argv*/ ){ double trkPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*(i+j) * .2; trkTrueMomentum = {trkPx, trkPy, trkPz}; } - MYTEST(trkRecoMomentum, trkTrueMomentum, "getTrackMomentum"); + std::stringstream failMsg; + failMsg << " getTrackMomentum : [" << trkRecoMomentum << "] != [" << trkTrueMomentum << "]"; + MYTEST(approxEqArray(trkRecoMomentum, trkTrueMomentum), failMsg.str()); ++j ; } @@ -271,17 +273,6 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( ref[k] , float(k+1) , ss.str() ) ; } - //Test of the getTrackMomentum - std::array trkRecoMomentum = UTIL::getTrackMomentum(trk, 3.5); - std::array trkTrueMomentum = {0., 0., 0.}; - if (trk->getOmega() != 0.){ - double trkPx = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::cos((i+j) * .3); - double trkPy = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::sin((i+j) * .3); - double trkPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*(i+j) * .2; - trkTrueMomentum = {trkPx, trkPy, trkPz}; - } - MYTEST(trkRecoMomentum, trkTrueMomentum, "getTrackMomentum"); - ++j ; } @@ -330,17 +321,6 @@ int main(int /*argc*/, char** /*argv*/ ){ ss << " ref[" << k << "] " ; MYTEST( ref[k] , float(k+1) , ss.str() ) ; } - - //Test of the getTrackMomentum - std::array trkRecoMomentum = UTIL::getTrackMomentum(trk, 3.5); - std::array trkTrueMomentum = {0., 0., 0.}; - if (trk->getOmega() != 0.){ - double trkPx = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::cos((i+j) * .3); - double trkPy = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::sin((i+j) * .3); - double trkPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*(i+j) * .2; - trkTrueMomentum = {trkPx, trkPy, trkPz}; - } - MYTEST(trkRecoMomentum, trkTrueMomentum, "getTrackMomentum"); // more than one trackstate const TrackStateVec& trackstates = trk->getTrackStates() ; @@ -372,16 +352,6 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ; } - //Test of the getTrackMomentum - std::array tsRecoMomentum = UTIL::getTrackMomentum(trackstates[k], 3.5); - std::array tsTrueMomentum = {0., 0., 0.}; - if (trackstates[k]->getOmega() != 0.){ - double tsPx = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*std::cos((i+j+k) * .3); - double tsPy = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*std::sin((i+j+k) * .3); - double tsPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*(i+j+k) * .2; - tsTrueMomentum = {tsPx, tsPy, tsPz}; - } - MYTEST(tsRecoMomentum, tsTrueMomentum, "getTrackMomentum"); } @@ -416,17 +386,6 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( ref[k] , float(k+1) , ss.str() ) ; } - //Test of the getTrackMomentum - std::array trkcRecoMomentum = UTIL::getTrackMomentum(trkc, 3.5); - std::array trkcTrueMomentum = {0., 0., 0.}; - if (trkc->getOmega() != 0.){ - double trkcPx = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::cos((i+j) * .3); - double trkcPy = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*std::sin((i+j) * .3); - double trkcPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j) * .1)*(i+j) * .2; - trkcTrueMomentum = {trkcPx, trkcPy, trkcPz}; - } - MYTEST(trkcRecoMomentum, trkcTrueMomentum, "getTrackMomentum"); - // more than one trackstate const TrackStateVec& trackstatesc = trkc->getTrackStates() ; @@ -454,17 +413,6 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ; } - //Test of the getTrackMomentum - std::array tscRecoMomentum = UTIL::getTrackMomentum(trackstatesc[k], 3.5); - std::array tscTrueMomentum = {0., 0., 0.}; - if (trackstatesc[k]->getOmega() != 0.){ - double tscPx = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*std::cos((i+j+k) * .3); - double tscPy = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*std::sin((i+j+k) * .3); - double tscPz = (1e-6 * 299.792458 * 3.5) / std::abs((i+j+k) * .1)*(i+j+k) * .2; - tscTrueMomentum = {tscPx, tscPy, tscPz}; - } - MYTEST(tscRecoMomentum, tscTrueMomentum, "getTrackMomentum"); - } // ---------- end test copies ------------------------------- diff --git a/src/cpp/src/TESTS/test_trackstate.cc b/src/cpp/src/TESTS/test_trackstate.cc index 47a41fc72..314294755 100644 --- a/src/cpp/src/TESTS/test_trackstate.cc +++ b/src/cpp/src/TESTS/test_trackstate.cc @@ -7,6 +7,7 @@ #include "EVENT/TrackState.h" #include "IMPL/TrackStateImpl.h" +#include "UTIL/TrackTools.h" //#include "UTIL/Operators.h" @@ -41,7 +42,8 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( a.getPhi(), float( .0 ), "getPhi" ) ; MYTEST( a.getOmega(), float( .0 ), "getOmega" ) ; - + // Omega == 0, will yield zero momentum as there is no reasonable value in that case + MYTEST( getTrackMomentum(&a, 3.5), std::array{0, 0, 0}, "getTrackMomentum" ); MYTEST.LOG( "test constructor with arguments" ); @@ -65,6 +67,17 @@ int main(int /*argc*/, char** /*argv*/ ){ MYTEST( b.getPhi(), float( .2 ), "getPhi" ) ; MYTEST( b.getOmega(), float( .3 ), "getOmega" ) ; + const std::array trueMomentum = { + 1e-6 * 299.7922458 * 3.5 / .3 * std::cos(.2), // pt * cos(phi), with pt = c * B / omega + 1e-6 * 299.7922458 * 3.5 / .3 * std::sin(.2), // pt * sin(phi) + 1e-6 * 299.7922458 * 3.5 / .3 * .5 // pt * tanL + }; + + std::stringstream failMsg; + const auto tsMomentum = getTrackMomentum(&b, 3.5); + failMsg << " getTrackMomentum : [" << tsMomentum << "] != << [" << trueMomentum << "]"; + MYTEST(approxEqArray(tsMomentum, trueMomentum), failMsg.str()); + MYTEST.LOG( "test default copy constructor" ); diff --git a/src/cpp/src/TESTS/tutil.h b/src/cpp/src/TESTS/tutil.h index 4db73debb..a710afc64 100644 --- a/src/cpp/src/TESTS/tutil.h +++ b/src/cpp/src/TESTS/tutil.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include class TEST{ @@ -35,10 +37,10 @@ class TEST{ return ; } -// void operator()(bool cond, const std::string msg) { -// if ( ! cond ) FAILED( msg ) ; -// return ; -// } + void operator()(bool cond, const std::string msg) { + if ( ! cond ) FAILED( msg ) ; + return ; + } void FAILED( const std::string& msg ){ @@ -75,3 +77,21 @@ std::ostream& operator<<(std::ostream& os, const std::array& arr) { } return os << "]"; } + +bool approxEqual(double lhs, double rhs) { + // Following a similar, but slightly simplified approach as Catch2::Approx here + constexpr double epsilon = std::numeric_limits::epsilon() * 100; + const double margin = std::fabs(lhs) * epsilon; + return (lhs + margin >= rhs) && (rhs + margin >= lhs); +} + +template +bool approxEqArray(const std::array& arr1, const std::array& arr2, ApproxComp&& comp=approxEqual) { + for (size_t i = 0; i < N; ++i) { + if (!comp(arr1[i], arr2[i])) { + return false; + } + } + return true; +} +