Skip to content

Commit

Permalink
use checked_cast in GridJsonConverter and PortalLink tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgessinger committed Nov 29, 2024
1 parent 547db88 commit af7ba1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
21 changes: 14 additions & 7 deletions Plugins/Json/src/GridJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
#include "Acts/Plugins/Json/UtilitiesJsonConverter.hpp"
#include "Acts/Utilities/CheckedCast.hpp"
#include "Acts/Utilities/IAxis.hpp"

nlohmann::json Acts::AxisJsonConverter::toJson(const IAxis& ia) {
Expand Down Expand Up @@ -52,7 +53,8 @@ void encodeSubspace(
nlohmann::json& jGlobalToGridLocal,
const Acts::GridAccess::IGlobalToGridLocal& globalToGridLocal,
const Subspace& /*subspace*/) {
const Subspace* subspace = dynamic_cast<const Subspace*>(&globalToGridLocal);
const Subspace* subspace =
Acts::checked_cast<const Subspace*>(&globalToGridLocal);
if (subspace != nullptr) {
jGlobalToGridLocal["type"] = "subspace";
jGlobalToGridLocal["accessors"] = subspace->bValues;
Expand All @@ -65,7 +67,7 @@ void encodeTransformedSubspace(
const Acts::GridAccess::IGlobalToGridLocal& globalToGridLocal,
const Subspace& subscpace) {
const Acts::GridAccess::Affine3Transformed<Subspace>* tsubspace =
dynamic_cast<const Acts::GridAccess::Affine3Transformed<Subspace>*>(
Acts::checked_cast<const Acts::GridAccess::Affine3Transformed<Subspace>*>(
&globalToGridLocal);
if (tsubspace != nullptr) {
encodeSubspace(jGlobalToGridLocal, tsubspace->globalToGridLocal, subscpace);
Expand Down Expand Up @@ -409,35 +411,40 @@ nlohmann::json Acts::GridAccessJsonConverter::toJson(
nlohmann::json jBoundToGridLocal;

auto localSubSpace0 =
dynamic_cast<const GridAccess::LocalSubspace<0u>*>(&boundToGridLocal);
Acts::checked_cast<const GridAccess::LocalSubspace<0u>*>(
&boundToGridLocal);
if (localSubSpace0 != nullptr) {
jBoundToGridLocal["type"] = "subspace";
jBoundToGridLocal["accessors"] = localSubSpace0->accessors;
}

auto localSubSpace1 =
dynamic_cast<const GridAccess::LocalSubspace<1u>*>(&boundToGridLocal);
Acts::checked_cast<const GridAccess::LocalSubspace<1u>*>(
&boundToGridLocal);
if (localSubSpace1 != nullptr) {
jBoundToGridLocal["type"] = "subspace";
jBoundToGridLocal["accessors"] = localSubSpace1->accessors;
}

auto localSubSpace01 =
dynamic_cast<const GridAccess::LocalSubspace<0u, 1u>*>(&boundToGridLocal);
Acts::checked_cast<const GridAccess::LocalSubspace<0u, 1u>*>(
&boundToGridLocal);
if (localSubSpace01 != nullptr) {
jBoundToGridLocal["type"] = "subspace";
jBoundToGridLocal["accessors"] = localSubSpace01->accessors;
}

auto localSubSpace10 =
dynamic_cast<const GridAccess::LocalSubspace<1u, 0u>*>(&boundToGridLocal);
Acts::checked_cast<const GridAccess::LocalSubspace<1u, 0u>*>(
&boundToGridLocal);
if (localSubSpace10 != nullptr) {
jBoundToGridLocal["type"] = "subspace";
jBoundToGridLocal["accessors"] = localSubSpace10->accessors;
}

auto boundCylinderToZPhi =
dynamic_cast<const GridAccess::BoundCylinderToZPhi*>(&boundToGridLocal);
Acts::checked_cast<const GridAccess::BoundCylinderToZPhi*>(
&boundToGridLocal);
if (boundCylinderToZPhi != nullptr) {
jBoundToGridLocal["type"] = "cylinder_to_zphi";
jBoundToGridLocal["radius"] = boundCylinderToZPhi->radius;
Expand Down
64 changes: 36 additions & 28 deletions Tests/UnitTests/Core/Geometry/PortalLinkTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Acts/Surfaces/SurfaceMergingException.hpp"
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
#include "Acts/Utilities/AxisFwd.hpp"
#include "Acts/Utilities/CheckedCast.hpp"
#include "Acts/Utilities/ThrowAssert.hpp"

#include <cstdio>
Expand Down Expand Up @@ -159,7 +160,7 @@ BOOST_AUTO_TEST_CASE(Cylinder) {
Axis axis2Expected{AxisBound, -100_mm, 100_mm, 10};
BOOST_CHECK_EQUAL(*axis2, axis2Expected);

auto& concrete = dynamic_cast<
auto& concrete = Acts::checked_cast<
GridPortalLinkT<decltype(axis1Expected), decltype(axis2Expected)>&>(
*grid2dCyl1);

Expand All @@ -176,7 +177,7 @@ BOOST_AUTO_TEST_CASE(Cylinder) {
BOOST_CHECK_EQUAL(*axis1, axis1Explicit);
BOOST_CHECK_EQUAL(*axis2, axis2Expected);

auto& concrete2 = dynamic_cast<
auto& concrete2 = Acts::checked_cast<
GridPortalLinkT<decltype(axis1Explicit), decltype(axis2Expected)>&>(
*grid2dCyl1Explicit);

Expand Down Expand Up @@ -207,8 +208,8 @@ BOOST_AUTO_TEST_CASE(Cylinder) {
BOOST_CHECK_EQUAL(*axis2Phi, axis2PhiExpected);

auto& concrete3 =
dynamic_cast<GridPortalLinkT<decltype(axis1PhiExpected),
decltype(axis2PhiExpected)>&>(
Acts::checked_cast<GridPortalLinkT<decltype(axis1PhiExpected),
decltype(axis2PhiExpected)>&>(
*grid2dCylPhi);

checkAllBins(concrete3);
Expand All @@ -223,8 +224,8 @@ BOOST_AUTO_TEST_CASE(Cylinder) {
BOOST_CHECK_EQUAL(*axis2Phi, axis2PhiExpected);

auto& concrete4 =
dynamic_cast<GridPortalLinkT<decltype(axis1PhiExplicit),
decltype(axis2PhiExpected)>&>(
Acts::checked_cast<GridPortalLinkT<decltype(axis1PhiExplicit),
decltype(axis2PhiExpected)>&>(
*grid2dCylPhiExplicit);

checkAllBins(concrete4);
Expand Down Expand Up @@ -362,8 +363,9 @@ BOOST_AUTO_TEST_CASE(Disc) {
BOOST_CHECK_EQUAL(axis2->getBoundaryType(), AxisBoundaryType::Closed);

checkAllBins(
dynamic_cast<GridPortalLinkT<decltype(axisPhi1Expected),
Axis<Equidistant, Closed>>&>(*grid2d));
Acts::checked_cast<GridPortalLinkT<decltype(axisPhi1Expected),
Axis<Equidistant, Closed>>&>(
*grid2d));

Axis axis2Explicit{AxisClosed, -180_degree, 180_degree, 3};
auto grid2dExplicit = grid1->extendTo2d(&axis2Explicit);
Expand All @@ -374,8 +376,8 @@ BOOST_AUTO_TEST_CASE(Disc) {
BOOST_CHECK_EQUAL(*axis1, axis1Expected);
BOOST_CHECK_EQUAL(*axis2, axis2Explicit);

checkAllBins(dynamic_cast<GridPortalLinkT<decltype(axisPhi1Expected),
decltype(axis2Explicit)>&>(
checkAllBins(Acts::checked_cast<GridPortalLinkT<decltype(axisPhi1Expected),
decltype(axis2Explicit)>&>(
*grid2dExplicit));

auto gridPhiBinnedInR = GridPortalLink::make(
Expand All @@ -396,8 +398,8 @@ BOOST_AUTO_TEST_CASE(Disc) {
AxisBoundaryType::Bound);

checkAllBins(
dynamic_cast<GridPortalLinkT<decltype(gridPhiBinnedInRExpected),
Axis<Equidistant, Bound>>&>(
Acts::checked_cast<GridPortalLinkT<decltype(gridPhiBinnedInRExpected),
Axis<Equidistant, Bound>>&>(
*grid2dPhiNonClosed));

Axis axisPhiNonClosedExplicit{AxisBound, -45_degree, 45_degree, 3};
Expand All @@ -410,10 +412,10 @@ BOOST_AUTO_TEST_CASE(Disc) {
BOOST_CHECK_EQUAL(*grid2dPhiNonClosedExplicit->grid().axes().front(),
gridPhiBinnedInRExpected);

checkAllBins(
dynamic_cast<GridPortalLinkT<decltype(gridPhiBinnedInRExpected),
decltype(axisPhiNonClosedExplicit)>&>(
*grid2dPhiNonClosedExplicit));
checkAllBins(Acts::checked_cast<
GridPortalLinkT<decltype(gridPhiBinnedInRExpected),
decltype(axisPhiNonClosedExplicit)>&>(
*grid2dPhiNonClosedExplicit));

auto grid2dPhi = gridPhi->extendTo2d(nullptr);
BOOST_REQUIRE(grid2dPhi);
Expand All @@ -423,8 +425,9 @@ BOOST_AUTO_TEST_CASE(Disc) {
BOOST_CHECK_EQUAL(*grid2dPhi->grid().axes().back(), axisPhi1Expected);

checkAllBins(
dynamic_cast<GridPortalLinkT<decltype(axis2dPhiExpected),
decltype(axisPhi1Expected)>&>(*grid2dPhi));
Acts::checked_cast<GridPortalLinkT<decltype(axis2dPhiExpected),
decltype(axisPhi1Expected)>&>(
*grid2dPhi));

Axis axis2dPhiExplicit{AxisBound, 30_mm, 100_mm, 3};
auto grid2dPhiExplicit = gridPhi->extendTo2d(&axis2dPhiExplicit);
Expand All @@ -435,9 +438,10 @@ BOOST_AUTO_TEST_CASE(Disc) {
BOOST_CHECK_EQUAL(*grid2dPhiExplicit->grid().axes().back(),
axisPhi1Expected);

checkAllBins(dynamic_cast<GridPortalLinkT<decltype(axisPhi1Expected),
decltype(axis2dPhiExplicit)>&>(
*grid2dPhiExplicit));
checkAllBins(
Acts::checked_cast<GridPortalLinkT<decltype(axisPhi1Expected),
decltype(axis2dPhiExplicit)>&>(
*grid2dPhiExplicit));
}

BOOST_TEST_CONTEXT("2D") {
Expand Down Expand Up @@ -1465,7 +1469,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) {
using merged_type =
GridPortalLinkT<Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>;

const auto* merged = dynamic_cast<const merged_type*>(mergedPtr.get());
const auto* merged =
Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);

grid1->printContents(std::cout);
Expand Down Expand Up @@ -1503,7 +1508,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) {
using merged_type =
GridPortalLinkT<Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>;

const auto* merged = dynamic_cast<const merged_type*>(mergedPtr.get());
const auto* merged =
Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);

grid1->printContents(std::cout);
Expand Down Expand Up @@ -1655,7 +1661,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) {
GridPortalLinkT<Axis<AxisType::Equidistant, AxisBoundaryType::Bound>,
Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>;

const auto* merged = dynamic_cast<const merged_type*>(mergedPtr.get());
const auto* merged =
Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);
checkCheckerBoard(merged->grid());

Expand All @@ -1666,7 +1673,7 @@ BOOST_AUTO_TEST_CASE(BinFilling) {
mergedPtr = GridPortalLink::merge(*discPhiGrid1, *discPhiGrid2,
BinningValue::binR, *logger);

merged = dynamic_cast<const merged_type*>(mergedPtr.get());
merged = Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);

const auto* v1 = vol1.get();
Expand Down Expand Up @@ -1728,7 +1735,8 @@ BOOST_AUTO_TEST_CASE(BinFilling) {
GridPortalLinkT<Axis<AxisType::Equidistant, AxisBoundaryType::Bound>,
Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>;

const auto* merged = dynamic_cast<const merged_type*>(mergedPtr.get());
const auto* merged =
Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);

checkCheckerBoard(merged->grid());
Expand All @@ -1739,7 +1747,7 @@ BOOST_AUTO_TEST_CASE(BinFilling) {

mergedPtr =
GridPortalLink::merge(*grid1, *grid2, BinningValue::binPhi, *logger);
merged = dynamic_cast<const merged_type*>(mergedPtr.get());
merged = Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE(merged);

const auto* v1 = vol1.get();
Expand Down Expand Up @@ -2013,7 +2021,7 @@ BOOST_AUTO_TEST_CASE(PhiDirection) {
GridPortalLinkT<Axis<AxisType::Equidistant, AxisBoundaryType::Bound>,
Axis<AxisType::Equidistant, AxisBoundaryType::Bound>>;

const auto* merged = dynamic_cast<const merged_type*>(mergedPtr.get());
const auto* merged = Acts::checked_cast<const merged_type*>(mergedPtr.get());
BOOST_REQUIRE_NE(merged, nullptr);

BOOST_CHECK_EQUAL(merged->grid().axes().size(), 2);
Expand Down
5 changes: 3 additions & 2 deletions Tests/UnitTests/Plugins/Json/GridJsonConverterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Acts/Plugins/Json/GridJsonConverter.hpp"
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
#include "Acts/Utilities/CheckedCast.hpp"
#include "Acts/Utilities/GridAccessHelpers.hpp"
#include "Acts/Utilities/GridAxisGenerators.hpp"

Expand Down Expand Up @@ -257,7 +258,7 @@ namespace {
template <typename ReferenceType, typename CheckTypeUniquePtr>
bool checkType(const ReferenceType& /**unused*/,
const CheckTypeUniquePtr& g2l) {
return (dynamic_cast<const ReferenceType*>(g2l.get()) != nullptr);
return (Acts::checked_cast<const ReferenceType*>(g2l.get()) != nullptr);
}

template <typename SubspactTuple>
Expand Down Expand Up @@ -476,7 +477,7 @@ BOOST_AUTO_TEST_CASE(BoundCylinderToZPhiTest) {
BOOST_REQUIRE(boundCylinderToZPhiRead != nullptr);

const Acts::GridAccess::BoundCylinderToZPhi* bct =
dynamic_cast<const Acts::GridAccess::BoundCylinderToZPhi*>(
Acts::checked_cast<const Acts::GridAccess::BoundCylinderToZPhi*>(
boundCylinderToZPhiRead.get());

auto delegate =
Expand Down

0 comments on commit af7ba1c

Please sign in to comment.