File indexing completed on 2025-01-18 09:13:10
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <boost/test/unit_test.hpp>
0010
0011 #include "Acts/Plugins/Geant4/Geant4DetectorSurfaceFactory.hpp"
0012 #include "Acts/Plugins/Geant4/Geant4PhysicalVolumeSelectors.hpp"
0013 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0014 #include "Acts/Visualization/GeometryView3D.hpp"
0015 #include "Acts/Visualization/ObjVisualization3D.hpp"
0016
0017 #include <memory>
0018 #include <numbers>
0019 #include <string>
0020
0021 #include "G4Box.hh"
0022 #include "G4LogicalVolume.hh"
0023 #include "G4PVPlacement.hh"
0024 #include "G4RotationMatrix.hh"
0025 #include "G4SystemOfUnits.hh"
0026 #include "G4ThreeVector.hh"
0027 #include "G4Transform3D.hh"
0028 #include "G4Tubs.hh"
0029
0030 class G4VPhysicalVolume;
0031
0032 BOOST_AUTO_TEST_SUITE(Geant4Plugin)
0033
0034 BOOST_AUTO_TEST_CASE(Geant4DetecturSurfaceFactory_box) {
0035 G4Box* worldS = new G4Box("world", 100, 100, 100);
0036
0037 G4LogicalVolume* worldLV = new G4LogicalVolume(worldS, nullptr, "World");
0038
0039 G4Box* boxS = new G4Box("box", 10, 20, 20);
0040 G4LogicalVolume* boxLV = new G4LogicalVolume(boxS, nullptr, "World");
0041 G4VPhysicalVolume* boxPV = new G4PVPlacement(nullptr, G4ThreeVector(), boxLV,
0042 "Box", worldLV, false, 0, true);
0043
0044 G4Transform3D nominal;
0045
0046
0047 auto nameSelector =
0048 std::make_shared<Acts::Geant4PhysicalVolumeSelectors::NameSelector>(
0049 std::vector<std::string>{"ox"}, false);
0050
0051 Acts::Geant4DetectorSurfaceFactory::Cache cache;
0052 Acts::Geant4DetectorSurfaceFactory::Options options;
0053 options.sensitiveSurfaceSelector = nameSelector;
0054
0055 Acts::Geant4DetectorSurfaceFactory factory;
0056 factory.construct(cache, nominal, *boxPV, options);
0057
0058 BOOST_CHECK_EQUAL(cache.sensitiveSurfaces.size(), 1u);
0059 BOOST_CHECK_EQUAL(cache.passiveSurfaces.size(), 0u);
0060
0061 auto [element, surface] = cache.sensitiveSurfaces.front();
0062 BOOST_CHECK_EQUAL(surface->type(), Acts::Surface::SurfaceType::Plane);
0063 }
0064
0065 BOOST_AUTO_TEST_CASE(Geant4DetecturSurfaceFactory_Cylinder) {
0066 G4Box* worldS = new G4Box("world", 1000, 1000, 1000);
0067
0068 G4LogicalVolume* worldLV = new G4LogicalVolume(worldS, nullptr, "World");
0069
0070 G4Tubs* cylinderS =
0071 new G4Tubs("cylinder", 99, 100, 100, -std::numbers::pi * CLHEP::radian,
0072 2 * std::numbers::pi * CLHEP::radian);
0073
0074 G4LogicalVolume* cylinderLV =
0075 new G4LogicalVolume(cylinderS, nullptr, "World");
0076 G4VPhysicalVolume* cylinderPV =
0077 new G4PVPlacement(nullptr, G4ThreeVector(), cylinderLV, "Cylinder",
0078 worldLV, false, 0, true);
0079
0080 G4Transform3D nominal;
0081
0082
0083 auto nameSelector =
0084 std::make_shared<Acts::Geant4PhysicalVolumeSelectors::NameSelector>(
0085 std::vector<std::string>{"yl"}, false);
0086
0087 Acts::Geant4DetectorSurfaceFactory::Cache cache;
0088 Acts::Geant4DetectorSurfaceFactory::Options options;
0089 options.sensitiveSurfaceSelector = nameSelector;
0090
0091 Acts::Geant4DetectorSurfaceFactory factory;
0092 factory.construct(cache, nominal, *cylinderPV, options);
0093
0094 BOOST_CHECK_EQUAL(cache.sensitiveSurfaces.size(), 1u);
0095 BOOST_CHECK_EQUAL(cache.passiveSurfaces.size(), 0u);
0096
0097 auto [element, surface] = cache.sensitiveSurfaces.front();
0098 BOOST_CHECK_EQUAL(surface->type(), Acts::Surface::SurfaceType::Cylinder);
0099 }
0100
0101 BOOST_AUTO_TEST_CASE(Geant4DetecturSurfaceFactory_Transforms) {
0102 Acts::GeometryContext gctx;
0103
0104 G4Box* worldS = new G4Box("world", 1000, 1000, 1000);
0105 G4LogicalVolume* worldLV = new G4LogicalVolume(worldS, nullptr, "World");
0106 G4VPhysicalVolume* worldPV = new G4PVPlacement(
0107 nullptr, G4ThreeVector(), worldLV, "World", nullptr, false, 0, false);
0108
0109 auto vol1S = new G4Box("volume1", 25, 10, 50);
0110 auto vol1L = new G4LogicalVolume(vol1S, nullptr, "Volume1");
0111
0112 G4Transform3D transformVol1(CLHEP::HepRotationX(std::numbers::pi / 4.),
0113 G4ThreeVector(20, 0, 0));
0114
0115 [[maybe_unused]] auto vol1PV = new G4PVPlacement(
0116 transformVol1, vol1L, "Volume1", worldLV, false, 0, false);
0117
0118 auto vol2S = new G4Box("volume2", 25, 10, 50);
0119 auto vol2L = new G4LogicalVolume(vol2S, nullptr, "Volume2");
0120
0121 G4Transform3D transformVol2(CLHEP::HepRotationY(std::numbers::pi / 6.),
0122 G4ThreeVector(0, 100, 20));
0123
0124 [[maybe_unused]] auto vol2PV = new G4PVPlacement(
0125 transformVol2, vol2L, "Volume2", vol1L, false, 0, false);
0126
0127 auto vol3S = new G4Box("volume3", 25, 10, 50);
0128 auto vol3L = new G4LogicalVolume(vol3S, nullptr, "Volume3");
0129
0130 G4Transform3D transformVol3(CLHEP::HepRotationZ(std::numbers::pi / 12.),
0131 G4ThreeVector(30, 100, 0));
0132
0133 [[maybe_unused]] auto vol3PV = new G4PVPlacement(
0134 transformVol3, vol3L, "Volume3", vol2L, false, 0, false);
0135
0136
0137 auto nameSelector =
0138 std::make_shared<Acts::Geant4PhysicalVolumeSelectors::NameSelector>(
0139 std::vector<std::string>{"olume"}, false);
0140
0141 Acts::Geant4DetectorSurfaceFactory::Cache cache;
0142 Acts::Geant4DetectorSurfaceFactory::Options options;
0143 options.sensitiveSurfaceSelector = nameSelector;
0144
0145 G4Transform3D nominal;
0146
0147 Acts::Geant4DetectorSurfaceFactory factory;
0148 factory.construct(cache, nominal, *worldPV, options);
0149
0150 auto [element, surface] = cache.sensitiveSurfaces.front();
0151 BOOST_CHECK_EQUAL(surface->type(), Acts::Surface::SurfaceType::Plane);
0152
0153 auto center = surface->center(gctx);
0154 auto normal = surface->normal(gctx, center, Acts::Vector3(1, 0, 0));
0155 CHECK_CLOSE_ABS(center.x(), 45.981, 1e-3);
0156 CHECK_CLOSE_ABS(center.y(), 137.886, 1e-3);
0157 CHECK_CLOSE_ABS(center.z(), 144.957, 1e-3);
0158 CHECK_CLOSE_ABS(normal.x(), -0.224, 1e-3);
0159 CHECK_CLOSE_ABS(normal.y(), 0.592, 1e-3);
0160 CHECK_CLOSE_ABS(normal.z(), 0.775, 1e-3);
0161
0162 Acts::ObjVisualization3D obj;
0163 Acts::Vector3 origin(0, 0, 0);
0164 Acts::GeometryView3D::drawArrowForward(obj, origin, Acts::Vector3(100, 0, 0),
0165 1000, 10, {.color = {255, 0, 0}});
0166 Acts::GeometryView3D::drawArrowForward(obj, origin, Acts::Vector3(0, 100, 0),
0167 1000, 10, {.color = {0, 255, 0}});
0168 Acts::GeometryView3D::drawArrowForward(obj, origin, Acts::Vector3(0, 0, 100),
0169 1000, 10, {.color = {0, 0, 255}});
0170 Acts::GeometryView3D::drawArrowForward(obj, surface->center(gctx),
0171 surface->center(gctx) + 100 * normal,
0172 1000, 10, {.color = {0, 255, 0}});
0173 auto surfaces = cache.sensitiveSurfaces;
0174 for (const auto& [k, val] : Acts::enumerate(cache.sensitiveSurfaces)) {
0175 const auto& [el, surf] = val;
0176 Acts::ViewConfig vCfg;
0177 if (k == 0) {
0178 vCfg.color = {0, 255, 0};
0179 } else if (k == 1) {
0180 vCfg.color = {255, 0, 0};
0181 } else if (k == 2) {
0182 vCfg.color = {0, 255, 255};
0183 }
0184 Acts::GeometryView3D::drawSurface(obj, *surf, gctx,
0185 Acts::Transform3::Identity(), vCfg);
0186 }
0187
0188 obj.write("RotatedSurface.obj");
0189 }
0190
0191 BOOST_AUTO_TEST_SUITE_END()