Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 09:23:56

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 "ActsPlugins/Geant4/Geant4Converters.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0013 #include "Acts/Material/HomogeneousSurfaceMaterial.hpp"
0014 #include "Acts/Material/Material.hpp"
0015 #include "Acts/Material/MaterialSlab.hpp"
0016 #include "Acts/Surfaces/CylinderBounds.hpp"
0017 #include "Acts/Surfaces/CylinderSurface.hpp"
0018 #include "Acts/Surfaces/DiscSurface.hpp"
0019 #include "Acts/Surfaces/LineBounds.hpp"
0020 #include "Acts/Surfaces/PlaneSurface.hpp"
0021 #include "Acts/Surfaces/RadialBounds.hpp"
0022 #include "Acts/Surfaces/RectangleBounds.hpp"
0023 #include "Acts/Surfaces/StrawSurface.hpp"
0024 #include "Acts/Surfaces/TrapezoidBounds.hpp"
0025 #include "Acts/Utilities/TransformHelpers.hpp"
0026 
0027 #include <algorithm>
0028 #include <array>
0029 #include <cmath>
0030 #include <cstdlib>
0031 #include <iterator>
0032 #include <numbers>
0033 #include <stdexcept>
0034 #include <utility>
0035 
0036 #include "G4Box.hh"
0037 #include "G4LogicalVolume.hh"
0038 #include "G4Material.hh"
0039 #include "G4RotationMatrix.hh"
0040 #include "G4ThreeVector.hh"
0041 #include "G4Transform3D.hh"
0042 #include "G4Trap.hh"
0043 #include "G4Trd.hh"
0044 #include "G4Tubs.hh"
0045 #include "G4VPhysicalVolume.hh"
0046 #include "G4VSolid.hh"
0047 
0048 using namespace Acts;
0049 
0050 Transform3 ActsPlugins::Geant4AlgebraConverter::transform(
0051     const G4ThreeVector& g4Trans) {
0052   Transform3 gTransform = Transform3::Identity();
0053   Vector3 scaledTrans =
0054       Vector3(scale * g4Trans[0], scale * g4Trans[1], scale * g4Trans[2]);
0055   gTransform.pretranslate(scaledTrans);
0056   return gTransform;
0057 }
0058 
0059 Transform3 ActsPlugins::Geant4AlgebraConverter::transform(
0060     const G4RotationMatrix& g4Rot, const G4ThreeVector& g4Trans) {
0061   // Create the translation
0062   Vector3 translation(scale * g4Trans[0], scale * g4Trans[1],
0063                       scale * g4Trans[2]);
0064   // And the rotation to it
0065   RotationMatrix3 rotation;
0066   rotation << g4Rot.xx(), g4Rot.xy(), g4Rot.xz(), g4Rot.yx(), g4Rot.yy(),
0067       g4Rot.yz(), g4Rot.zx(), g4Rot.zy(), g4Rot.zz();
0068   Transform3 transform = Transform3::Identity();
0069   transform.rotate(rotation);
0070   transform.pretranslate(translation);
0071   return transform;
0072 }
0073 
0074 Transform3 ActsPlugins::Geant4AlgebraConverter::transform(
0075     const G4Transform3D& g4Trf) {
0076   auto g4Rot = g4Trf.getRotation();
0077   auto g4Trans = g4Trf.getTranslation();
0078   return transform(g4Rot, g4Trans);
0079 }
0080 
0081 Transform3 ActsPlugins::Geant4AlgebraConverter::transform(
0082     const G4VPhysicalVolume& g4PhysVol) {
0083   // Get Rotation and translation
0084   auto g4Translation = g4PhysVol.GetTranslation();
0085   auto g4Rotation = g4PhysVol.GetRotation();
0086 
0087   G4Transform3D g4Transform =
0088       (g4Rotation == nullptr)
0089           ? G4Transform3D(CLHEP::HepRotation(), g4Translation)
0090           : G4Transform3D(*g4Rotation, g4Translation);
0091 
0092   return transform(g4Transform);
0093 }
0094 
0095 std::tuple<std::shared_ptr<CylinderBounds>, double>
0096 ActsPlugins::Geant4ShapeConverter::cylinderBounds(const G4Tubs& g4Tubs) {
0097   using B = CylinderBounds;
0098 
0099   std::array<double, B::eSize> tArray = {};
0100   tArray[B::eR] =
0101       static_cast<double>(g4Tubs.GetInnerRadius() + g4Tubs.GetOuterRadius()) *
0102       0.5;
0103   tArray[B::eHalfLengthZ] = static_cast<double>(g4Tubs.GetZHalfLength());
0104   tArray[B::eHalfPhiSector] =
0105       0.5 * static_cast<double>(g4Tubs.GetDeltaPhiAngle());
0106   // Geant fiddles around with user given values, i.e. it would not allow [-PI,
0107   // +PI) as a full segment (has to be [0, 2PI)])
0108   if (std::abs(tArray[B::eHalfPhiSector] - std::numbers::pi) <
0109       std::numeric_limits<double>::epsilon()) {
0110     tArray[B::eAveragePhi] = 0.;
0111   } else {
0112     tArray[B::eAveragePhi] = static_cast<double>(g4Tubs.GetStartPhiAngle()) +
0113                              tArray[B::eHalfPhiSector];
0114   }
0115   double thickness = g4Tubs.GetOuterRadius() - g4Tubs.GetInnerRadius();
0116   auto cBounds = std::make_shared<CylinderBounds>(tArray);
0117   return {std::move(cBounds), thickness};
0118 }
0119 
0120 std::tuple<std::shared_ptr<RadialBounds>, double>
0121 ActsPlugins::Geant4ShapeConverter::radialBounds(const G4Tubs& g4Tubs) {
0122   using B = RadialBounds;
0123 
0124   std::array<double, B::eSize> tArray = {};
0125   tArray[B::eMinR] = static_cast<double>(g4Tubs.GetInnerRadius());
0126   tArray[B::eMaxR] = static_cast<double>(g4Tubs.GetOuterRadius());
0127   tArray[B::eHalfPhiSector] =
0128       0.5 * static_cast<double>(g4Tubs.GetDeltaPhiAngle());
0129   // Geant fiddles around with user given values, i.e. it would not allow [-PI,
0130   // +PI) as a full segment (has to be [0, 2PI)])
0131   if (std::abs(tArray[B::eHalfPhiSector] - std::numbers::pi) <
0132       std::numeric_limits<double>::epsilon()) {
0133     tArray[B::eAveragePhi] = 0.;
0134   } else {
0135     tArray[B::eAveragePhi] = static_cast<double>(g4Tubs.GetStartPhiAngle()) +
0136                              tArray[B::eHalfPhiSector];
0137   }
0138   double thickness = g4Tubs.GetZHalfLength() * 2;
0139   auto rBounds = std::make_shared<RadialBounds>(tArray);
0140   return {std::move(rBounds), thickness};
0141 }
0142 
0143 std::shared_ptr<LineBounds> ActsPlugins::Geant4ShapeConverter::lineBounds(
0144     const G4Tubs& g4Tubs) {
0145   auto r = static_cast<double>(g4Tubs.GetOuterRadius());
0146   auto hlZ = static_cast<double>(g4Tubs.GetZHalfLength());
0147   return std::make_shared<LineBounds>(r, hlZ);
0148 }
0149 
0150 std::tuple<std::shared_ptr<RectangleBounds>, std::array<int, 2u>, double>
0151 ActsPlugins::Geant4ShapeConverter::rectangleBounds(const G4Box& g4Box) {
0152   std::array<double, 3> hG4XYZ = {static_cast<double>(g4Box.GetXHalfLength()),
0153                                   static_cast<double>(g4Box.GetYHalfLength()),
0154                                   static_cast<double>(g4Box.GetZHalfLength())};
0155 
0156   auto minAt = std::min_element(hG4XYZ.begin(), hG4XYZ.end());
0157   std::size_t minPos = std::distance(hG4XYZ.begin(), minAt);
0158   double thickness = 2. * hG4XYZ[minPos];
0159 
0160   std::array<int, 2u> rAxes = {};
0161   switch (minPos) {
0162     case 0: {
0163       rAxes = {1, 2};
0164     } break;
0165     case 1: {
0166       if (keepAxisOrder) {
0167         rAxes = {0, -2};  // flip for right-handed
0168       } else {
0169         rAxes = {2, 0};  // cyclic positive
0170       }
0171     } break;
0172     case 2: {
0173       rAxes = {0, 1};
0174     } break;
0175     default:  // do nothing
0176       break;
0177   }
0178   auto rBounds = std::make_shared<RectangleBounds>(hG4XYZ[std::abs(rAxes[0u])],
0179                                                    hG4XYZ[std::abs(rAxes[1u])]);
0180   return {std::move(rBounds), rAxes, thickness};
0181 }
0182 
0183 std::tuple<std::shared_ptr<TrapezoidBounds>, std::array<int, 2u>, double>
0184 ActsPlugins::Geant4ShapeConverter::trapezoidBounds(const G4Trd& g4Trd) {
0185   // primary parameters
0186   double hlX0 = static_cast<double>(g4Trd.GetXHalfLength1());
0187   double hlX1 = static_cast<double>(g4Trd.GetXHalfLength2());
0188   double hlY0 = static_cast<double>(g4Trd.GetYHalfLength1());
0189   double hlY1 = static_cast<double>(g4Trd.GetYHalfLength2());
0190   double hlZ = static_cast<double>(g4Trd.GetZHalfLength());
0191 
0192   std::array<double, 3> dXYZ = {(hlX0 + hlX1) * 0.5, (hlY0 + hlY1) * 0.5, hlZ};
0193 
0194   auto minAt = std::min_element(dXYZ.begin(), dXYZ.end());
0195   std::size_t minPos = std::distance(dXYZ.begin(), minAt);
0196   double thickness = 2. * dXYZ[minPos];
0197 
0198   double halfLengthXminY = 0.;
0199   double halfLengthXmaxY = 0.;
0200   double halfLengthY = 0.;
0201 
0202   std::array<int, 2u> rAxes = {};
0203   switch (minPos) {
0204     case 0: {
0205       halfLengthXminY = hlY0;
0206       halfLengthXmaxY = hlY1;
0207       halfLengthY = hlZ;
0208       rAxes = {1, 2};
0209     } break;
0210     case 1: {
0211       halfLengthXminY = hlX0;
0212       halfLengthXmaxY = hlX1;
0213       halfLengthY = hlZ;
0214       rAxes = {0, -2};
0215     } break;
0216     case 2: {
0217       if (std::abs(hlY0 - hlY1) < std::abs(hlX0 - hlX1)) {
0218         halfLengthXminY = hlX0;
0219         halfLengthXmaxY = hlX1;
0220         halfLengthY = (hlY0 + hlY1) * 0.5;
0221         rAxes = {0, 1};
0222       } else {
0223         halfLengthXminY = hlY0;
0224         halfLengthXmaxY = hlY1;
0225         halfLengthY = (hlX0 + hlX1) * 0.5;
0226         rAxes = {-1, 0};
0227       }
0228     } break;
0229   }
0230 
0231   auto tBounds = std::make_shared<TrapezoidBounds>(
0232       halfLengthXminY, halfLengthXmaxY, halfLengthY);
0233   return {std::move(tBounds), rAxes, thickness};
0234 }
0235 
0236 std::tuple<std::shared_ptr<TrapezoidBounds>, std::array<int, 2u>, double>
0237 ActsPlugins::Geant4ShapeConverter::trapezoidBounds(const G4Trap& g4Trap) {
0238   // primary parameters
0239   auto y1 = static_cast<double>(g4Trap.GetYHalfLength1());
0240   auto y2 = static_cast<double>(g4Trap.GetYHalfLength2());
0241   auto x1 = static_cast<double>(g4Trap.GetXHalfLength1());
0242   auto x2 = static_cast<double>(g4Trap.GetXHalfLength2());
0243   auto x3 = static_cast<double>(g4Trap.GetXHalfLength3());
0244   auto x4 = static_cast<double>(g4Trap.GetXHalfLength4());
0245   auto phi = static_cast<double>(g4Trap.GetPhi());
0246   auto theta = static_cast<double>(g4Trap.GetTheta());
0247   auto z = static_cast<double>(g4Trap.GetZHalfLength());
0248 
0249   double hlX0 = (x1 + x2) * 0.5;
0250   double hlX1 = 2 * z * std::tan(theta) * std::cos(phi) + (x3 + x4) * 0.5;
0251   double hlY0 = y1;
0252   double hlY1 = y2 + 2 * z * std::tan(theta) * std::sin(phi);
0253   double hlZ = z;
0254 
0255   std::array<double, 3> dXYZ = {(hlX0 + hlX1) * 0.5, (hlY0 + hlY1) * 0.5, hlZ};
0256 
0257   auto minAt = std::ranges::min_element(dXYZ);
0258   std::size_t minPos = std::distance(dXYZ.begin(), minAt);
0259   double thickness = 2. * dXYZ[minPos];
0260 
0261   double halfLengthXminY = 0.;
0262   double halfLengthXmaxY = 0.;
0263   double halfLengthY = 0.;
0264 
0265   std::array<int, 2u> rAxes = {};
0266   switch (minPos) {
0267     case 0: {
0268       halfLengthXminY = std::min(hlY0, hlY1);
0269       halfLengthXmaxY = std::max(hlY0, hlY1);
0270       halfLengthY = hlZ;
0271       rAxes = {1, 2};
0272     } break;
0273     case 1: {
0274       halfLengthXminY = std::min(hlX0, hlX1);
0275       halfLengthXmaxY = std::max(hlX0, hlX1);
0276       halfLengthY = hlZ;
0277       rAxes = {0, -2};
0278     } break;
0279     case 2: {
0280       if (std::abs(hlY0 - hlY1) < std::abs(hlX0 - hlX1)) {
0281         halfLengthXminY = std::min(hlX0, hlX1);
0282         halfLengthXmaxY = std::max(hlX0, hlX1);
0283         halfLengthY = (hlY0 + hlY1) * 0.5;
0284         rAxes = {0, 1};
0285       } else {
0286         halfLengthXminY = std::min(hlY0, hlY1);
0287         halfLengthXmaxY = std::max(hlY0, hlY1);
0288         halfLengthY = (hlX0 + hlX1) * 0.5;
0289         rAxes = {-1, 0};
0290       }
0291     } break;
0292     default: {
0293       throw std::runtime_error("Geant4Converters: could not convert G4Trap.");
0294     }
0295   }
0296 
0297   auto tBounds = std::make_shared<TrapezoidBounds>(
0298       halfLengthXminY, halfLengthXmaxY, halfLengthY);
0299   return std::make_tuple(std::move(tBounds), rAxes, thickness);
0300 }
0301 
0302 std::tuple<std::shared_ptr<PlanarBounds>, std::array<int, 2u>, double>
0303 ActsPlugins::Geant4ShapeConverter::planarBounds(const G4VSolid& g4Solid) {
0304   const G4Box* box = dynamic_cast<const G4Box*>(&g4Solid);
0305   if (box != nullptr) {
0306     auto [rBounds, axes, thickness] = rectangleBounds(*box);
0307     return {std::move(rBounds), axes, thickness};
0308   }
0309 
0310   const G4Trd* trd = dynamic_cast<const G4Trd*>(&g4Solid);
0311   if (trd != nullptr) {
0312     auto [tBounds, axes, thickness] = trapezoidBounds(*trd);
0313     return {std::move(tBounds), axes, thickness};
0314   }
0315 
0316   std::shared_ptr<PlanarBounds> pBounds = nullptr;
0317   std::array<int, 2u> rAxes = {};
0318   double rThickness = 0.;
0319   return {std::move(pBounds), rAxes, rThickness};
0320 }
0321 
0322 namespace {
0323 Transform3 axesOriented(const Transform3& toGlobalOriginal,
0324                         const std::array<int, 2u>& axes) {
0325   const RotationMatrix3 originalRotation = toGlobalOriginal.rotation();
0326   const Vector3 colX =
0327       std::copysign(1., axes[0u]) * originalRotation.col(std::abs(axes[0u]));
0328   const Vector3 colY =
0329       std::copysign(1., axes[1u]) * originalRotation.col(std::abs(axes[1u]));
0330   const Vector3 colZ = colX.cross(colY);
0331 
0332   RotationMatrix3 orientedRotation;
0333   orientedRotation.col(0) = colX;
0334   orientedRotation.col(1) = colY;
0335   orientedRotation.col(2) = colZ;
0336 
0337   return makeTransform3(orientedRotation, toGlobalOriginal.translation());
0338 }
0339 }  // namespace
0340 
0341 std::shared_ptr<Surface> ActsPlugins::Geant4PhysicalVolumeConverter::surface(
0342     const G4VPhysicalVolume& g4PhysVol, const Transform3& toGlobal,
0343     bool convertMaterial, double compressed) {
0344   // Get the logical volume
0345   auto g4LogVol = g4PhysVol.GetLogicalVolume();
0346   auto g4Solid = g4LogVol->GetSolid();
0347 
0348   auto assignMaterial = [&](Surface& sf, double moriginal,
0349                             double mcompressed) -> void {
0350     auto g4Material = g4LogVol->GetMaterial();
0351     if (convertMaterial && g4Material != nullptr) {
0352       if (compressed < 0.) {
0353         mcompressed = moriginal;
0354       }
0355       auto surfaceMaterial = Geant4MaterialConverter{}.surfaceMaterial(
0356           *g4Material, moriginal, mcompressed);
0357       sf.assignSurfaceMaterial(std::move(surfaceMaterial));
0358     }
0359   };
0360 
0361   // Dynamic cast chain & conversion
0362   std::shared_ptr<Surface> surface = nullptr;
0363 
0364   // Into a rectangle
0365   auto g4Box = dynamic_cast<const G4Box*>(g4Solid);
0366   if (g4Box != nullptr) {
0367     if (forcedType == Surface::SurfaceType::Other ||
0368         forcedType == Surface::SurfaceType::Plane) {
0369       auto [bounds, axes, original] =
0370           Geant4ShapeConverter{}.rectangleBounds(*g4Box);
0371       auto orientedToGlobal = axesOriented(toGlobal, axes);
0372       surface = Surface::makeShared<PlaneSurface>(orientedToGlobal,
0373                                                   std::move(bounds));
0374       assignMaterial(*surface, original, compressed);
0375       return surface;
0376     } else {
0377       throw std::runtime_error("Can not convert 'G4Box' into forced shape.");
0378     }
0379   }
0380 
0381   // Into a Trapezoid - from Trd
0382   auto g4Trd = dynamic_cast<const G4Trd*>(g4Solid);
0383   if (g4Trd != nullptr) {
0384     if (forcedType == Surface::SurfaceType::Other ||
0385         forcedType == Surface::SurfaceType::Plane) {
0386       auto [bounds, axes, original] =
0387           Geant4ShapeConverter{}.trapezoidBounds(*g4Trd);
0388       auto orientedToGlobal = axesOriented(toGlobal, axes);
0389       surface = Surface::makeShared<PlaneSurface>(orientedToGlobal,
0390                                                   std::move(bounds));
0391       assignMaterial(*surface, original, compressed);
0392       return surface;
0393     } else {
0394       throw std::runtime_error("Can not convert 'G4Trd' into forced shape.");
0395     }
0396   }
0397 
0398   // Into a Trapezoid - from Trap
0399   auto g4Trap = dynamic_cast<const G4Trap*>(g4Solid);
0400   if (g4Trap != nullptr) {
0401     if (forcedType == Surface::SurfaceType::Other ||
0402         forcedType == Surface::SurfaceType::Plane) {
0403       auto [bounds, axes, original] =
0404           Geant4ShapeConverter{}.trapezoidBounds(*g4Trap);
0405       auto orientedToGlobal = axesOriented(toGlobal, axes);
0406       surface = Surface::makeShared<PlaneSurface>(orientedToGlobal,
0407                                                   std::move(bounds));
0408       assignMaterial(*surface, original, compressed);
0409       return surface;
0410     } else {
0411       throw std::runtime_error("Can not convert 'G4Trap' into forced shape.");
0412     }
0413   }
0414 
0415   // Into a Cylinder, disc or line
0416   auto g4Tubs = dynamic_cast<const G4Tubs*>(g4Solid);
0417   if (g4Tubs != nullptr) {
0418     double diffR = g4Tubs->GetOuterRadius() - g4Tubs->GetInnerRadius();
0419     double diffZ = 2 * g4Tubs->GetZHalfLength();
0420     // Detect if cylinder or disc case
0421     double original = 0.;
0422     if (forcedType == Surface::SurfaceType::Cylinder ||
0423         (diffR < diffZ && forcedType == Surface::SurfaceType::Other)) {
0424       auto [bounds, originalT] = Geant4ShapeConverter{}.cylinderBounds(*g4Tubs);
0425       original = originalT;
0426       surface =
0427           Surface::makeShared<CylinderSurface>(toGlobal, std::move(bounds));
0428     } else if (forcedType == Surface::SurfaceType::Disc ||
0429                forcedType == Surface::SurfaceType::Other) {
0430       auto [bounds, originalT] = Geant4ShapeConverter{}.radialBounds(*g4Tubs);
0431       original = originalT;
0432       surface = Surface::makeShared<DiscSurface>(toGlobal, std::move(bounds));
0433     } else if (forcedType == Surface::SurfaceType::Straw) {
0434       auto bounds = Geant4ShapeConverter{}.lineBounds(*g4Tubs);
0435       surface = Surface::makeShared<StrawSurface>(toGlobal, std::move(bounds));
0436 
0437     } else {
0438       throw std::runtime_error("Can not convert 'G4Tubs' into forced shape.");
0439     }
0440     assignMaterial(*surface, original, compressed);
0441     return surface;
0442   }
0443 
0444   return nullptr;
0445 }
0446 
0447 Material ActsPlugins::Geant4MaterialConverter::material(
0448     const G4Material& g4Material, double compression) {
0449   auto X0 = g4Material.GetRadlen();
0450   auto L0 = g4Material.GetNuclearInterLength();
0451   auto Rho = g4Material.GetDensity();
0452 
0453   // Get{A,Z} is only meaningful for single-element materials (according to
0454   // the Geant4 docs). Need to compute average manually.
0455   auto g4Elements = g4Material.GetElementVector();
0456   auto g4Fraction = g4Material.GetFractionVector();
0457   auto g4NElements = g4Material.GetNumberOfElements();
0458   double Ar = 0;
0459   double Z = 0;
0460   if (g4NElements == 1) {
0461     Ar = g4Elements->at(0)->GetN();
0462     Z = g4Material.GetZ();
0463   } else {
0464     for (std::size_t i = 0; i < g4NElements; i++) {
0465       Ar += g4Elements->at(i)->GetN() * g4Fraction[i];
0466       Z += g4Elements->at(i)->GetZ() * g4Fraction[i];
0467     }
0468   }
0469 
0470   return Material::fromMassDensity(X0 / compression, L0 / compression, Ar, Z,
0471                                    compression * Rho);
0472 }
0473 
0474 std::shared_ptr<HomogeneousSurfaceMaterial>
0475 ActsPlugins::Geant4MaterialConverter::surfaceMaterial(
0476     const G4Material& g4Material, double original, double compressed) {
0477   double compression = original / compressed;
0478   return std::make_shared<HomogeneousSurfaceMaterial>(
0479       MaterialSlab(material(g4Material, compression), compressed));
0480 }
0481 
0482 std::shared_ptr<CylinderVolumeBounds>
0483 ActsPlugins::Geant4VolumeConverter::cylinderBounds(const G4Tubs& g4Tubs) {
0484   using C = CylinderVolumeBounds;
0485 
0486   std::array<double, C::eSize> tArray = {};
0487   tArray[C::eMinR] = static_cast<double>(g4Tubs.GetInnerRadius());
0488   tArray[C::eMaxR] = static_cast<double>(g4Tubs.GetOuterRadius());
0489   tArray[C::eHalfLengthZ] = static_cast<double>(g4Tubs.GetZHalfLength());
0490   tArray[C::eHalfPhiSector] =
0491       0.5 * static_cast<double>(g4Tubs.GetDeltaPhiAngle());
0492   tArray[C::eAveragePhi] = static_cast<double>(g4Tubs.GetStartPhiAngle());
0493 
0494   return std::make_shared<CylinderVolumeBounds>(tArray);
0495 }