Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions Plugins/Root/include/Acts/Plugins/Root/TGeoSurfaceConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/Utilities/detail/periodic.hpp"

#include <cmath>
#include <memory>
Expand Down Expand Up @@ -93,11 +95,31 @@ struct TGeoSurfaceConverter {
/// @param degree The input in degree
/// @return angle in radians
static double toRadian(double degree) {
if (degree > 180. && degree < 360.) {
degree -= 360.;
}
return degree / 180. * std::numbers::pi;
return detail::radian_sym(degree * UnitConstants::degree);
}

/// Compute the angular extent (halfPhi) and center angle (avgPhi),
/// handling periodicity and wraparound across 0°. The returned angles are in radians.
///
/// The average phi is wrapped into the range [-pi, pi).
///
/// @param deg1 Starting angle in degrees
/// @param deg2 Ending angle in degrees
/// @return pair {halfPhi, avgPhi} in radians
static std::pair<double, double> toRadianSpan(double deg1, double deg2) {
double phi1 = deg1 * UnitConstants::degree;
double phi2 = deg2 * UnitConstants::degree;

// Proper arc length with wraparound (e.g. 0° to 360° = 2π)
double span =
Acts::detail::difference_periodic(phi2, phi1, 2 * std::numbers::pi);

// Compute average phi in [−π, π) (for symmetry in bounds)
double avg = Acts::detail::radian_sym(phi1 + 0.5 * span);

return {span * 0.5, avg}; // halfPhi, avgPhi
}

};

} // namespace Acts
25 changes: 13 additions & 12 deletions Plugins/Root/src/TGeoSurfaceConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ Acts::TGeoSurfaceConverter::cylinderComponents(const TGeoShape& tgShape,
// Check if it's a segment
auto tubeSeg = dynamic_cast<const TGeoTubeSeg*>(tube);
if (tubeSeg != nullptr) {
double phi1 = toRadian(tubeSeg->GetPhi1());
double phi2 = toRadian(tubeSeg->GetPhi2());
if (std::abs(phi2 - phi1) < std::numbers::pi * (1. - s_epsilon)) {
double deg1 = tubeSeg->GetPhi1();
double deg2 = tubeSeg->GetPhi2();
std::tie(halfPhi, avgPhi) = toRadianSpan(deg1, deg2);
double span = 2.0 * halfPhi;

if (span < std::numbers::pi * (1. - s_epsilon)) {
if (!boost::starts_with(axes, "X")) {
throw std::invalid_argument(
"TGeoShape -> CylinderSurface (sectorial): can only be "
"converted "
"with "
"'(X)(y/Y)(*)' axes.");
}
halfPhi = 0.5 * (std::max(phi1, phi2) - std::min(phi1, phi2));
avgPhi = 0.5 * (phi1 + phi2);
}
}
bounds = std::make_shared<CylinderBounds>(medR, halfZ, halfPhi, avgPhi);
Expand Down Expand Up @@ -252,18 +253,18 @@ Acts::TGeoSurfaceConverter::discComponents(const TGeoShape& tgShape,
// Check if it's a segment
auto tubeSeg = dynamic_cast<const TGeoTubeSeg*>(tube);
if (tubeSeg != nullptr) {
double phi1 = toRadian(tubeSeg->GetPhi1());
double phi2 = toRadian(tubeSeg->GetPhi2());
if (std::abs(phi2 - phi1) < 2 * std::numbers::pi * (1. - s_epsilon)) {
double deg1 = tubeSeg->GetPhi1();
double deg2 = tubeSeg->GetPhi2();
std::tie(halfPhi, avgPhi) = toRadianSpan(deg1, deg2);
double span = 2.0 * halfPhi;

if (span < std::numbers::pi * (1. - s_epsilon)) {
if (!boost::starts_with(axes, "X")) {
throw std::invalid_argument(
"TGeoShape -> CylinderSurface (sectorial): can only be "
"converted "
"TGeoShape -> DiscSurface (sectorial): can only be converted "
"with "
"'(X)(y/Y)(*)' axes.");
}
halfPhi = 0.5 * (std::max(phi1, phi2) - std::min(phi1, phi2));
avgPhi = 0.5 * (phi1 + phi2);
}
}
bounds = std::make_shared<RadialBounds>(minR, maxR, halfPhi, avgPhi);
Expand Down
2 changes: 2 additions & 0 deletions Tests/UnitTests/Plugins/Root/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ if(${ROOT_VERSION} VERSION_GREATER "6.25.01")
endif()
add_unittest(TGeoParser TGeoParserTests.cpp)
add_unittest(TGeoPrimitivesHelper TGeoPrimitivesHelperTests.cpp)
add_unittest(TGeoToRadianConversionTests TGeoToRadianConversionTests.cpp)
add_unittest(TGeoToRadianSpanTests TGeoToRadianSpanTests.cpp)
71 changes: 71 additions & 0 deletions Tests/UnitTests/Plugins/Root/TGeoToRadianConversionTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include <boost/test/unit_test.hpp>

#include "Acts/Definitions/Tolerance.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/Plugins/Root/TGeoSurfaceConverter.hpp"
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"

#include <cmath>
#include <iomanip>
#include <numbers>
#include <utility>
#include <vector>

namespace Acts::Test {

BOOST_AUTO_TEST_CASE(ToRadian_WrappingCoverage) {
using std::numbers::pi;

// {input in degrees, expected radians (wrapped to [-pi, pi))}
std::vector<std::pair<double, double>> testCases = {
// In-range
{0.0, 0.0},
{90.0, pi / 2.0},
{180.0, -pi},
{270.0, -pi / 2.0},
{359.0, -1.0 * UnitConstants::degree},
{1.0, 1.0 * UnitConstants::degree},
{45.0, pi / 4.0},
{135.0, 3.0 * pi / 4.0},
{225.0, -3.0 * pi / 4.0},
{315.0, -pi / 4.0},

// Just below 360
{360.0 - 1e-10, -1e-10 * UnitConstants::degree},

// Negative values
{-1.0, -1.0 * UnitConstants::degree},
{-90.0, -pi / 2.0},
{-180.0, -pi},
{-270.0, pi / 2.0},
{-360.0, 0.0},
{-450.0, -pi / 2.0},

// > 360 values
{361.0, 1.0 * UnitConstants::degree},
{450.0, pi / 2.0},
{720.0, 0.0},
{810.0, pi / 2.0},
{1080.0 + 45.0, pi / 4.0},

// Large positive and negative inputs
{1260.0, -pi},
{-1260.0, -pi},

};

for (const auto& [degInput, expectedWrappedRad] : testCases) {
const double actual = TGeoSurfaceConverter::toRadian(degInput);
CHECK_CLOSE_ABS(actual, expectedWrappedRad, s_epsilon);
}
}

} // namespace Acts::Test
72 changes: 72 additions & 0 deletions Tests/UnitTests/Plugins/Root/TGeoToRadianSpanTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include <boost/test/unit_test.hpp>

#include "Acts/Definitions/Tolerance.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/Plugins/Root/TGeoSurfaceConverter.hpp"
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"

#include <cmath>
#include <numbers>
#include <utility>
#include <vector>

namespace Acts::Test {

BOOST_AUTO_TEST_CASE(ToRadianSpan_Correctness) {
using std::numbers::pi;

struct TestCase {
double deg1;
double deg2;
double expectedHalfSpan;
double expectedAvg;
};

std::vector<TestCase> testCases = {
// Simple cases
{0.0, 90.0, pi / 4.0, pi / 4.0},
{90.0, 180.0, pi / 4.0, 3.0 * pi / 4.0},
{0.0, 360.0, pi, 0.0},
{360.0, 720.0, pi, 0.0},
{-180.0, 180.0, 0.0, pi},

// Wraparound case (crosses 0°)
{315.0, 45.0, pi / 4.0, 0.0},

// Reverse direction (same as above, but in opposite order)
{45.0, 315.0, 3.0 * pi / 4.0, pi},

// Small wraparound
{359.0, 1.0, UnitConstants::degree, 0.0},

// Large positive range
{0.0, 1125.0, pi / 8.0, pi / 8.0},

// Large negative to large positive
{-720.0, 90.0, pi / 4.0, pi / 4.0},

// Custom case
{316.79, 403.21, 43.21 * UnitConstants::degree * 0.5,
Acts::detail::radian_sym((316.79 + 0.5 * 43.21) * UnitConstants::degree)},
};

for (const auto& [deg1, deg2, expectedHalfSpan, expectedAvg] : testCases) {
const auto [actualHalfSpan, actualAvg] =
Acts::TGeoSurfaceConverter::toRadianSpan(deg1, deg2);

BOOST_TEST_CONTEXT("deg1 = " << deg1 << ", deg2 = " << deg2) {
CHECK_CLOSE_ABS(actualHalfSpan, expectedHalfSpan, s_epsilon);
CHECK_CLOSE_ABS(actualAvg, expectedAvg, s_epsilon);
}
}
}

} // namespace Acts::Test
Loading