Skip to content

Commit

Permalink
feat: allow custom precision for volume grid building (#2609)
Browse files Browse the repository at this point in the history
This PR allows to set some numerical precision for the volume grid binning, which removes some odd cases when writing out to Json and reading into Detray.
  • Loading branch information
asalzburger authored Nov 9, 2023
1 parent 9f7767c commit e8ff45a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
21 changes: 15 additions & 6 deletions Core/include/Acts/Detector/detail/CylindricalDetectorHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ DetectorComponent::PortalContainer wrapInZR(
///
/// @param gctx the geometry context of the call
/// @param volumes the volumes at input
/// @param precision the precision to be used for (optionally)
/// @param logLevel is the screen logging level
///
/// @return extracted boundary values
template <typename volume_container_t>
std::array<std::vector<ActsScalar>, 3u> rzphiBoundaries(
const GeometryContext& gctx, const volume_container_t& volumes,
double precision = 0.,
Acts::Logging::Level logLevel = Acts::Logging::INFO) {
// The local logger
ACTS_LOCAL_LOGGER(getDefaultLogger("CylindricalDetectorHelper", logLevel));
Expand All @@ -187,6 +189,13 @@ std::array<std::vector<ActsScalar>, 3u> rzphiBoundaries(

// The return boundaries
std::array<std::set<ActsScalar>, 3u> uniqueBoundaries;
auto insertWithPrecision = [&](std::size_t is, ActsScalar value) -> void {
if (precision == 0.) {
uniqueBoundaries[is].insert(value);
return;
}
uniqueBoundaries[is].insert(std::round(value / precision) * precision);
};

// Loop over the volumes and collect boundaries
for (const auto& v : volumes) {
Expand All @@ -207,12 +216,12 @@ std::array<std::vector<ActsScalar>, 3u> rzphiBoundaries(
ActsScalar phiMin = phiCenter - phiSector;
ActsScalar phiMax = phiCenter + phiSector;
// Fill the sets
uniqueBoundaries[0].insert(rMin);
uniqueBoundaries[0].insert(rMax);
uniqueBoundaries[1].insert(zMin);
uniqueBoundaries[1].insert(zMax);
uniqueBoundaries[2].insert(phiMin);
uniqueBoundaries[2].insert(phiMax);
insertWithPrecision(0u, rMin);
insertWithPrecision(0u, rMax);
insertWithPrecision(1u, zMin);
insertWithPrecision(1u, zMax);
insertWithPrecision(2u, phiMin);
insertWithPrecision(2u, phiMax);
}
}

Expand Down
25 changes: 24 additions & 1 deletion Tests/UnitTests/Core/Detector/CylindricalDetectorHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,8 @@ BOOST_AUTO_TEST_CASE(RZPhiBoundaries) {
std::vector<std::shared_ptr<DetectorVolume>> volumes = {
innerV, middleLV, middleDV, middleUV, middleRV, outerV};

auto boundaries = rzphiBoundaries(tContext, volumes, Acts::Logging::VERBOSE);
auto boundaries =
rzphiBoundaries(tContext, volumes, 0., Acts::Logging::VERBOSE);
BOOST_CHECK(boundaries.size() == 3u);
// Check the r boundaries
std::vector<ActsScalar> rBoundaries = {0., 20., 40., 60., 120.};
Expand All @@ -657,4 +658,26 @@ BOOST_AUTO_TEST_CASE(RZPhiBoundaries) {
BOOST_CHECK(boundaries[2u].size() == 2u);
}

BOOST_AUTO_TEST_CASE(RZPhiBoundariesWithTolerance) {
auto innerB = std::make_unique<CylinderVolumeBounds>(0., 20., 100);
auto innerV = DetectorVolumeFactory::construct(
portalGenerator, tContext, "Inner", Transform3::Identity(),
std::move(innerB), tryAllPortals());

auto outerB = std::make_unique<CylinderVolumeBounds>(20.001, 100., 100);
auto outerV = DetectorVolumeFactory::construct(
portalGenerator, tContext, "Inner", Transform3::Identity(),
std::move(outerB), tryAllPortals());

std::vector<std::shared_ptr<DetectorVolume>> volumes = {innerV, outerV};

auto boundariesWoTol =
rzphiBoundaries(tContext, volumes, 0., Acts::Logging::VERBOSE);
BOOST_CHECK_EQUAL(boundariesWoTol[0u].size(), 4u);

auto boundariesWTol =
rzphiBoundaries(tContext, volumes, 0.01, Acts::Logging::VERBOSE);
BOOST_CHECK_EQUAL(boundariesWTol[0u].size(), 3u);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e8ff45a

Please sign in to comment.