Skip to content

Commit

Permalink
fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Dec 13, 2024
1 parent 448ccca commit 5ab184d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 83 deletions.
157 changes: 103 additions & 54 deletions Tests/UnitTests/Core/Propagator/AtlasStepperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ static const Covariance cov = Covariance::Identity();
static const GeometryContext geoCtx;
static const MagneticFieldContext magCtx;

static const Stepper::Options options(geoCtx, magCtx);

BOOST_AUTO_TEST_SUITE(AtlasStepper)

// test state construction from parameters w/o covariance
BOOST_AUTO_TEST_CASE(ConstructState) {
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, std::nullopt,
particleHypothesis));
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, std::nullopt,
particleHypothesis);

Stepper stepper(magneticField);

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

BOOST_CHECK(!state.covTransport);
BOOST_CHECK_EQUAL(state.covariance, nullptr);
Expand All @@ -126,10 +129,15 @@ BOOST_AUTO_TEST_CASE(ConstructState) {

// test state construction from parameters w/ covariance
BOOST_AUTO_TEST_CASE(ConstructStateWithCovariance) {
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

BOOST_CHECK(state.covTransport);
BOOST_CHECK_EQUAL(*state.covariance, cov);
Expand All @@ -148,11 +156,15 @@ BOOST_AUTO_TEST_CASE(ConstructStateWithCovariance) {

// test stepper getters for particle state
BOOST_AUTO_TEST_CASE(Getters) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

CHECK_CLOSE_ABS(stepper.position(state), pos, eps);
CHECK_CLOSE_ABS(stepper.time(state), time, eps);
Expand All @@ -163,11 +175,15 @@ BOOST_AUTO_TEST_CASE(Getters) {

// test stepper update methods with bound state as input
BOOST_AUTO_TEST_CASE(UpdateFromBound) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

auto newPos4 = (pos4 + Vector4(1_mm, 2_mm, 3_mm, 20_ns)).eval();
auto newPos = newPos4.segment<3>(ePos0);
Expand Down Expand Up @@ -206,11 +222,15 @@ BOOST_AUTO_TEST_CASE(UpdateFromBound) {

// test stepper update methods with individual components as input
BOOST_AUTO_TEST_CASE(UpdateFromComponents) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

auto newPos = (pos + Vector3(1_mm, 2_mm, 3_mm)).eval();
auto newTime = time + 20_ns;
Expand All @@ -227,11 +247,16 @@ BOOST_AUTO_TEST_CASE(UpdateFromComponents) {

// test building a bound state object from the stepper state
BOOST_AUTO_TEST_CASE(BuildBound) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

// example surface at the current state position
auto plane = CurvilinearSurface(pos, unitDir).planeSurface();

Expand All @@ -251,11 +276,15 @@ BOOST_AUTO_TEST_CASE(BuildBound) {

// test building a curvilinear state object from the stepper state
BOOST_AUTO_TEST_CASE(BuildCurvilinear) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

auto&& [pars, jac, pathLength] = stepper.curvilinearState(state);
// check parameters
Expand All @@ -273,11 +302,15 @@ BOOST_AUTO_TEST_CASE(BuildCurvilinear) {

// test step method without covariance transport
BOOST_AUTO_TEST_CASE(Step) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
MockPropagatorState state(
Stepper::State(options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom,
cov, particleHypothesis)));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

MockPropagatorState state(stepper.makeState(options, cp));
state.stepping.covTransport = false;

// ensure step does not result in an error
Expand Down Expand Up @@ -306,11 +339,15 @@ BOOST_AUTO_TEST_CASE(Step) {

// test step method with covariance transport
BOOST_AUTO_TEST_CASE(StepWithCovariance) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
MockPropagatorState state(
Stepper::State(options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom,
cov, particleHypothesis)));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

MockPropagatorState state(stepper.makeState(options, cp));
state.stepping.covTransport = true;

// ensure step does not result in an error
Expand Down Expand Up @@ -342,11 +379,15 @@ BOOST_AUTO_TEST_CASE(StepWithCovariance) {

// test state reset method
BOOST_AUTO_TEST_CASE(Reset) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
MockPropagatorState state(
Stepper::State(options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom,
cov, particleHypothesis)));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

MockPropagatorState state(stepper.makeState(options, cp));
state.stepping.covTransport = true;

// ensure step does not result in an error
Expand All @@ -358,9 +399,9 @@ BOOST_AUTO_TEST_CASE(Reset) {
double newTime = 7.5;
double newCharge = 1.;
BoundSquareMatrix newCov = 8.5 * Covariance::Identity();
CurvilinearTrackParameters cp(makeVector4(newPos, newTime), unitDir,
newCharge / newAbsMom, newCov,
particleHypothesis);
cp = CurvilinearTrackParameters(makeVector4(newPos, newTime), unitDir,
newCharge / newAbsMom, newCov,
particleHypothesis);
FreeVector freeParams = transformBoundToFreeParameters(
cp.referenceSurface(), geoCtx, cp.parameters());
Direction navDir = Direction::Forward;
Expand Down Expand Up @@ -402,7 +443,7 @@ BOOST_AUTO_TEST_CASE(Reset) {
};

// Reset all possible parameters
Stepper::State stateCopy(copyState(*magneticField, state.stepping));
Stepper::State stateCopy = copyState(*magneticField, state.stepping);
BOOST_CHECK(cp.covariance().has_value());
stepper.resetState(stateCopy, cp.parameters(), *cp.covariance(),
cp.referenceSurface(), stepSize);
Expand Down Expand Up @@ -534,11 +575,15 @@ BOOST_AUTO_TEST_CASE(Reset) {
}

BOOST_AUTO_TEST_CASE(StepSize) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

stepper.updateStepSize(state, -5_cm, ConstrainedStep::navigator, true);
BOOST_CHECK_EQUAL(state.previousStepSize, stepSize);
Expand All @@ -550,11 +595,15 @@ BOOST_AUTO_TEST_CASE(StepSize) {

// test step size modification with target surfaces
BOOST_AUTO_TEST_CASE(StepSizeSurface) {
CurvilinearTrackParameters cp(pos4, unitDir, charge / absMom, cov,
particleHypothesis);

Stepper stepper(magneticField);
Stepper::State state(
options, magneticField->makeCache(magCtx),
CurvilinearTrackParameters(pos4, unitDir, charge / absMom, cov,
particleHypothesis));

Stepper::Options options(geoCtx, magCtx);
options.maxStepSize = stepSize;

Stepper::State state = stepper.makeState(options, cp);

auto distance = 10_mm;
auto target = CurvilinearSurface(pos + navDir * distance * unitDir, unitDir)
Expand Down
30 changes: 14 additions & 16 deletions Tests/UnitTests/Core/Propagator/EigenStepperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_state_test) {
double absMom = 8.;
double charge = -1.;

EigenStepper<>::Options esOptions(tgContext, mfContext);

// Test charged parameters without covariance matrix
CurvilinearTrackParameters cp(makeVector4(pos, time), dir, charge / absMom,
std::nullopt, ParticleHypothesis::pion());
EigenStepper<>::State esState(EigenStepper<>::Options(tgContext, mfContext),
bField->makeCache(mfContext), cp);

EigenStepper<> es(bField);
EigenStepper<>::State esState = es.makeState(esOptions, cp);

// Test the result & compare with the input/test for reasonable members
BOOST_CHECK_EQUAL(esState.jacToGlobal, BoundToFreeMatrix::Zero());
Expand All @@ -208,16 +208,15 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_state_test) {
// Test without charge and covariance matrix
CurvilinearTrackParameters ncp(makeVector4(pos, time), dir, 1 / absMom,
std::nullopt, ParticleHypothesis::pion0());
esState = EigenStepper<>::State(EigenStepper<>::Options(tgContext, mfContext),
bField->makeCache(mfContext), ncp);
esOptions = EigenStepper<>::Options(tgContext, mfContext);
esState = es.makeState(esOptions, ncp);
BOOST_CHECK_EQUAL(es.charge(esState), 0.);

// Test with covariance matrix
Covariance cov = 8. * Covariance::Identity();
ncp = CurvilinearTrackParameters(makeVector4(pos, time), dir, 1 / absMom, cov,
ParticleHypothesis::pion0());
esState = EigenStepper<>::State(EigenStepper<>::Options(tgContext, mfContext),
bField->makeCache(mfContext), ncp);
esState = es.makeState(esOptions, ncp);
BOOST_CHECK_NE(esState.jacToGlobal, BoundToFreeMatrix::Zero());
BOOST_CHECK(esState.covTransport);
BOOST_CHECK_EQUAL(esState.cov, cov);
Expand All @@ -242,12 +241,12 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_test) {
CurvilinearTrackParameters cp(makeVector4(pos, time), dir, charge / absMom,
cov, ParticleHypothesis::pion());

EigenStepper<>::Options options(tgContext, mfContext);
options.maxStepSize = stepSize;
EigenStepper<>::Options esOptions(tgContext, mfContext);
esOptions.maxStepSize = stepSize;

// Build the state and the stepper
EigenStepper<>::State esState(options, bField->makeCache(mfContext), cp);
// Build the stepper and the state
EigenStepper<> es(bField);
EigenStepper<>::State esState = es.makeState(esOptions, cp);

// Test the getters
CHECK_CLOSE_ABS(es.position(esState), pos, eps);
Expand Down Expand Up @@ -369,7 +368,7 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_test) {
};

// Reset all possible parameters
EigenStepper<>::State esStateCopy(copyState(*bField, ps.stepping));
EigenStepper<>::State esStateCopy = copyState(*bField, ps.stepping);
BOOST_CHECK(cp2.covariance().has_value());
es.resetState(esStateCopy, cp2.parameters(), *cp2.covariance(),
cp2.referenceSurface(), stepSize2);
Expand Down Expand Up @@ -446,8 +445,7 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_test) {
plane, tgContext, makeVector4(pos, time), dir, charge / absMom,
cov, ParticleHypothesis::pion())
.value();
esState = EigenStepper<>::State(EigenStepper<>::Options(tgContext, mfContext),
bField->makeCache(mfContext), cp);
esState = es.makeState(esOptions, bp);

// Test the intersection in the context of a surface
auto targetSurface =
Expand Down Expand Up @@ -519,8 +517,8 @@ BOOST_AUTO_TEST_CASE(eigen_stepper_test) {
// Produce some errors
auto nBfield = std::make_shared<NullBField>();
EigenStepper<> nes(nBfield);
EigenStepper<>::State nesState(EigenStepper<>::Options(tgContext, mfContext),
nBfield->makeCache(mfContext), cp);
EigenStepper<>::Options nesOptions(tgContext, mfContext);
EigenStepper<>::State nesState = nes.makeState(nesOptions, cp);
PropState nps(navDir, copyState(*nBfield, nesState));
// Test that we can reach the minimum step size
nps.options.stepping.stepTolerance = 1e-21;
Expand Down
12 changes: 5 additions & 7 deletions Tests/UnitTests/Core/Propagator/StraightLineStepperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ BOOST_AUTO_TEST_CASE(straight_line_stepper_state_test) {
double absMom = 8.;
double charge = -1.;

StraightLineStepper::Options options(tgContext, mfContext);
options.maxStepSize = stepSize;
StraightLineStepper::Options slsOptions(tgContext, mfContext);
slsOptions.maxStepSize = stepSize;

// Test charged parameters without covariance matrix
CurvilinearTrackParameters cp(makeVector4(pos, time), dir, charge / absMom,
std::nullopt, ParticleHypothesis::pion());
StraightLineStepper::State slsState(options, cp);

StraightLineStepper sls;
StraightLineStepper::State slsState = sls.makeState(slsOptions, cp);

// Test the result & compare with the input/test for reasonable members
BOOST_CHECK_EQUAL(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
Expand All @@ -101,15 +101,13 @@ BOOST_AUTO_TEST_CASE(straight_line_stepper_state_test) {
// Test without charge and covariance matrix
CurvilinearTrackParameters ncp(makeVector4(pos, time), dir, 1 / absMom,
std::nullopt, ParticleHypothesis::pion0());
slsState = StraightLineStepper::State(
StraightLineStepper::Options(tgContext, mfContext), ncp);
slsState = sls.makeState(slsOptions, ncp);

// Test with covariance matrix
Covariance cov = 8. * Covariance::Identity();
ncp = CurvilinearTrackParameters(makeVector4(pos, time), dir, 1 / absMom, cov,
ParticleHypothesis::pion0());
slsState = StraightLineStepper::State(
StraightLineStepper::Options(tgContext, mfContext), ncp);
slsState = sls.makeState(slsOptions, ncp);
BOOST_CHECK_NE(slsState.jacToGlobal, BoundToFreeMatrix::Zero());
BOOST_CHECK(slsState.covTransport);
BOOST_CHECK_EQUAL(slsState.cov, cov);
Expand Down
Loading

0 comments on commit 5ab184d

Please sign in to comment.