Skip to content

Commit

Permalink
Remove unnecessary duplicate tests and make comparison more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Oct 19, 2022
1 parent 6571b3f commit db6687f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 63 deletions.
62 changes: 4 additions & 58 deletions src/cpp/src/TESTS/test_tracks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
}
Expand Down Expand Up @@ -271,17 +273,6 @@ int main(int /*argc*/, char** /*argv*/ ){
MYTEST( ref[k] , float(k+1) , ss.str() ) ;
}

//Test of the getTrackMomentum
std::array<double, 3> trkRecoMomentum = UTIL::getTrackMomentum(trk, 3.5);
std::array<double, 3> 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 ;
}

Expand Down Expand Up @@ -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<double, 3> trkRecoMomentum = UTIL::getTrackMomentum(trk, 3.5);
std::array<double, 3> 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() ;
Expand Down Expand Up @@ -371,17 +351,6 @@ int main(int /*argc*/, char** /*argv*/ ){
ss << " ref[" << l << "] " ;
MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ;
}

//Test of the getTrackMomentum
std::array<double, 3> tsRecoMomentum = UTIL::getTrackMomentum(trackstates[k], 3.5);
std::array<double, 3> 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");
}


Expand Down Expand Up @@ -416,17 +385,6 @@ int main(int /*argc*/, char** /*argv*/ ){
MYTEST( ref[k] , float(k+1) , ss.str() ) ;
}

//Test of the getTrackMomentum
std::array<double, 3> trkcRecoMomentum = UTIL::getTrackMomentum(trkc, 3.5);
std::array<double, 3> 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() ;

Expand All @@ -453,18 +411,6 @@ int main(int /*argc*/, char** /*argv*/ ){
ss << " ref[" << l << "] " ;
MYTEST( refL[l] , float(k*(l+1)) , ss.str() ) ;
}

//Test of the getTrackMomentum
std::array<double, 3> tscRecoMomentum = UTIL::getTrackMomentum(trackstatesc[k], 3.5);
std::array<double, 3> 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 -------------------------------
Expand Down Expand Up @@ -506,8 +452,8 @@ int main(int /*argc*/, char** /*argv*/ ){
} catch( Exception &e ){
MYTEST.FAILED( e.what() );
}

return 0;

}

//=============================================================================
15 changes: 14 additions & 1 deletion src/cpp/src/TESTS/test_trackstate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "EVENT/TrackState.h"
#include "IMPL/TrackStateImpl.h"
#include "UTIL/TrackTools.h"

//#include "UTIL/Operators.h"

Expand Down Expand Up @@ -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<double, 3>{0, 0, 0}, "getTrackMomentum" );

MYTEST.LOG( "test constructor with arguments" );

Expand All @@ -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<double, 3> 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" );
Expand Down
28 changes: 24 additions & 4 deletions src/cpp/src/TESTS/tutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <array>
#include <limits>
#include <cmath>

class TEST{

Expand Down Expand Up @@ -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 ){

Expand Down Expand Up @@ -75,3 +77,21 @@ std::ostream& operator<<(std::ostream& os, const std::array<T, N>& 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<float>::epsilon() * 100;
const double margin = std::fabs(lhs) * epsilon;
return (lhs + margin >= rhs) && (rhs + margin >= lhs);
}

template<typename T, size_t N, typename ApproxComp=decltype(approxEqual)>
bool approxEqArray(const std::array<T, N>& arr1, const std::array<T, N>& arr2, ApproxComp&& comp=approxEqual) {
for (size_t i = 0; i < N; ++i) {
if (!comp(arr1[i], arr2[i])) {
return false;
}
}
return true;
}

0 comments on commit db6687f

Please sign in to comment.