Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:13

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Tolerance.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/Plugins/TGeo/TGeoSurfaceConverter.hpp"
0015 #include "Acts/Surfaces/Surface.hpp"
0016 #include "Acts/Surfaces/SurfaceBounds.hpp"
0017 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0018 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0019 #include "Acts/Visualization/GeometryView3D.hpp"
0020 #include "Acts/Visualization/ObjVisualization3D.hpp"
0021 #include "Acts/Visualization/ViewConfig.hpp"
0022 
0023 #include <algorithm>
0024 #include <cstddef>
0025 #include <memory>
0026 #include <stdexcept>
0027 #include <string>
0028 #include <utility>
0029 #include <vector>
0030 
0031 #include "TGeoManager.h"
0032 #include "TGeoMaterial.h"
0033 #include "TGeoMatrix.h"
0034 #include "TGeoMedium.h"
0035 #include "TGeoTrd1.h"
0036 #include "TGeoVolume.h"
0037 #include "TView.h"
0038 
0039 namespace Acts::Test {
0040 
0041 GeometryContext tgContext = GeometryContext();
0042 
0043 ViewConfig red{.color = {200, 0, 0}};
0044 ViewConfig green{.color = {0, 200, 0}};
0045 ViewConfig blue{.color = {0, 0, 200}};
0046 
0047 /// @brief Unit test to convert a TGeoTrd2 into a Plane
0048 ///
0049 /// * The TGeoTrd2 has x/z orientation
0050 BOOST_AUTO_TEST_CASE(TGeoTrd2_xz_to_PlaneSurface) {
0051   ObjVisualization3D objVis;
0052 
0053   double hxmin = 10.;
0054   double hxmax = 30.;
0055   double ht = 1.;  // this is the half thickness
0056   double hy = 40.;
0057 
0058   new TGeoManager("trd1", "poza9");
0059   TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
0060   TGeoMedium *med = new TGeoMedium("MED", 1, mat);
0061   TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
0062   gGeoManager->SetTopVolume(top);
0063   TGeoVolume *vol =
0064       gGeoManager->MakeTrd2("Trd2", med, hxmin, hxmax, ht, ht, hy);
0065   gGeoManager->CloseGeometry();
0066 
0067   // Check the 4 possible ways
0068   std::vector<std::string> axesTypes = {"XZ*", "xZ*", "xz*", "Xz*"};
0069 
0070   std::size_t itrd = 0;
0071   for (const auto &axes : axesTypes) {
0072     auto [plane, thickness] = TGeoSurfaceConverter::toSurface(
0073         *vol->GetShape(), *gGeoIdentity, axes, 1);
0074     BOOST_REQUIRE_NE(plane, nullptr);
0075     BOOST_CHECK_EQUAL(plane->type(), Surface::Plane);
0076     CHECK_CLOSE_ABS(thickness, 2 * ht, s_epsilon);
0077 
0078     auto bounds = dynamic_cast<const TrapezoidBounds *>(&(plane->bounds()));
0079     BOOST_REQUIRE_NE(bounds, nullptr);
0080     double hXminY = bounds->get(TrapezoidBounds::eHalfLengthXnegY);
0081     double hXmaxY = bounds->get(TrapezoidBounds::eHalfLengthXposY);
0082     double hY = bounds->get(TrapezoidBounds::eHalfLengthY);
0083 
0084     CHECK_CLOSE_ABS(hxmin, std::min(hXminY, hXmaxY), s_epsilon);
0085     CHECK_CLOSE_ABS(hxmax, std::max(hXminY, hXmaxY), s_epsilon);
0086     CHECK_CLOSE_ABS(hy, hY, s_epsilon);
0087 
0088     // Check if the surface is the (negative) identity
0089     auto transform = plane->transform(tgContext);
0090     auto rotation = transform.rotation();
0091     const Vector3 offset{(-5.5 + (itrd++) * 2.5) * hxmax, 0., 0.};
0092     GeometryView3D::drawSurface(objVis, *plane, tgContext,
0093                                 Translation3{offset} * Transform3::Identity());
0094     const Vector3 center = plane->center(tgContext) + offset;
0095     GeometryView3D::drawArrowForward(
0096         objVis, center, center + 1.2 * (hXminY + hXmaxY) * rotation.col(0), 4.,
0097         2.5, red);
0098     GeometryView3D::drawArrowForward(
0099         objVis, center, center + 1.2 * hY * rotation.col(1), 4., 2.5, green);
0100     GeometryView3D::drawArrowForward(
0101         objVis, center, center + 2 * rotation.col(2), 4., 2.5, blue);
0102   }
0103   objVis.write("TGeoConversion_TGeoTrd2_xz_PlaneSurface");
0104 
0105   // Check exceptions for not allowed axis definition
0106   std::vector<std::string> notAllowed = {"XY*", "xy*", "Xy*", "xY*"};
0107   for (const auto &naxis : notAllowed) {
0108     BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vol->GetShape(),
0109                                                       *gGeoIdentity, naxis, 1),
0110                       std::invalid_argument);
0111   }
0112 }
0113 
0114 /// @brief Unit test to convert a TGeoTrd2 into a Plane
0115 ///
0116 /// * The TGeoTrd2 has y/z orientation
0117 BOOST_AUTO_TEST_CASE(TGeoTrd2_yz_to_PlaneSurface) {
0118   ObjVisualization3D objVis;
0119 
0120   double hxmin = 10.;
0121   double hxmax = 30.;
0122   double ht = 1.;  // this is the half thickness
0123   double hy = 40.;
0124 
0125   new TGeoManager("trd1", "poza9");
0126   TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
0127   TGeoMedium *med = new TGeoMedium("MED", 1, mat);
0128   TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
0129   gGeoManager->SetTopVolume(top);
0130   TGeoVolume *vol =
0131       gGeoManager->MakeTrd2("Trd2", med, ht, ht, hxmin, hxmax, hy);
0132   gGeoManager->CloseGeometry();
0133 
0134   // Check the 4 possible ways
0135   std::vector<std::string> axesTypes = {"YZ*", "yZ*", "yz*", "Yz*"};
0136 
0137   std::size_t itrd = 0;
0138   for (const auto &axes : axesTypes) {
0139     auto [plane, thickness] = TGeoSurfaceConverter::toSurface(
0140         *vol->GetShape(), *gGeoIdentity, axes, 1);
0141     BOOST_REQUIRE_NE(plane, nullptr);
0142     BOOST_CHECK_EQUAL(plane->type(), Surface::Plane);
0143     CHECK_CLOSE_ABS(thickness, 2 * ht, s_epsilon);
0144 
0145     auto bounds = dynamic_cast<const TrapezoidBounds *>(&(plane->bounds()));
0146     BOOST_REQUIRE_NE(bounds, nullptr);
0147     double hXminY = bounds->get(TrapezoidBounds::eHalfLengthXnegY);
0148     double hXmaxY = bounds->get(TrapezoidBounds::eHalfLengthXposY);
0149     double hY = bounds->get(TrapezoidBounds::eHalfLengthY);
0150 
0151     CHECK_CLOSE_ABS(hxmin, std::min(hXminY, hXmaxY), s_epsilon);
0152     CHECK_CLOSE_ABS(hxmax, std::max(hXminY, hXmaxY), s_epsilon);
0153     CHECK_CLOSE_ABS(hy, hY, s_epsilon);
0154 
0155     // Check if the surface is the (negative) identity
0156     auto transform = plane->transform(tgContext);
0157     auto rotation = transform.rotation();
0158     const Vector3 offset{(-5.5 + (itrd++) * 2.5) * hxmax, 0., 0.};
0159     GeometryView3D::drawSurface(objVis, *plane, tgContext,
0160                                 Translation3{offset} * Transform3::Identity());
0161     const Vector3 center = plane->center(tgContext) + offset;
0162     GeometryView3D::drawArrowForward(
0163         objVis, center, center + 1.2 * (hXminY + hXmaxY) * rotation.col(0), 4.,
0164         2.5, red);
0165     GeometryView3D::drawArrowForward(
0166         objVis, center, center + 1.2 * hY * rotation.col(1), 4., 2.5, green);
0167     GeometryView3D::drawArrowForward(
0168         objVis, center, center + 2 * rotation.col(2), 4., 2.5, blue);
0169   }
0170   objVis.write("TGeoConversion_TGeoTrd2_yz_PlaneSurface");
0171 
0172   // Check exceptions for not allowed axis definition
0173   std::vector<std::string> notAllowed = {"YX*", "yx*", "yX*", "Yx*"};
0174   for (const auto &naxis : notAllowed) {
0175     BOOST_CHECK_THROW(TGeoSurfaceConverter::toSurface(*vol->GetShape(),
0176                                                       *gGeoIdentity, naxis, 1),
0177                       std::invalid_argument);
0178   }
0179 }
0180 
0181 }  // namespace Acts::Test