File indexing completed on 2026-11-18 09:35:48
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsPlugins/GeoModel/GeoModelDetectorObjectFactory.hpp"
0010
0011 #include "Acts/Geometry/CuboidVolumeBounds.hpp"
0012 #include "Acts/Geometry/CutoutCylinderVolumeBounds.hpp"
0013 #include "Acts/Geometry/CylinderVolumeBounds.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "Acts/Geometry/TrapezoidVolumeBounds.hpp"
0016 #include "Acts/Utilities/TransformHelpers.hpp"
0017 #include "ActsPlugins/GeoModel/GeoModelConverters.hpp"
0018 #include "ActsPlugins/GeoModel/IGeoShapeConverter.hpp"
0019
0020 #include <algorithm>
0021 #include <iostream>
0022 #include <typeinfo>
0023
0024 #include <GeoModelHelpers/GeoShapeUtils.h>
0025 #include <GeoModelKernel/GeoBox.h>
0026 #include <GeoModelKernel/GeoPcon.h>
0027 #include <GeoModelKernel/GeoShapeShift.h>
0028 #include <GeoModelKernel/GeoShapeSubtraction.h>
0029 #include <GeoModelKernel/GeoShapeUnion.h>
0030 #include <GeoModelKernel/GeoSimplePolygonBrep.h>
0031 #include <GeoModelKernel/GeoTrd.h>
0032 #include <GeoModelKernel/GeoTube.h>
0033 #include <GeoModelKernel/GeoTubs.h>
0034
0035 using namespace Acts;
0036
0037 namespace ActsPlugins {
0038
0039 GeoModelDetectorObjectFactory::GeoModelDetectorObjectFactory(
0040 const Config &cfg, std::unique_ptr<const Logger> mlogger)
0041 : m_logger(std::move(mlogger)), m_cfg(cfg) {}
0042
0043 void GeoModelDetectorObjectFactory::construct(Cache &cache,
0044 const GeometryContext &gctx,
0045 const GeoModelTree &geoModelTree,
0046 const Options &options) {
0047 for (const std::string &q : options.queries) {
0048 ACTS_VERBOSE("Constructing detector elements for query " << q);
0049
0050 auto qFPV = geoModelTree.publisher->getPublishedVol(q);
0051
0052
0053 for (const auto &[name, physVol] : qFPV) {
0054 ACTS_DEBUG("Convert volume " << name);
0055 convertFpv(name, physVol, cache, gctx);
0056 }
0057 }
0058 }
0059
0060 void GeoModelDetectorObjectFactory::convertSensitive(
0061 const PVConstLink &geoPV, const Transform3 &transform,
0062 SurfaceBoundFactory &boundFactory,
0063 std::vector<GeoModelSensitiveSurface> &sensitives) {
0064 const GeoLogVol *logVol = geoPV->getLogVol();
0065 const GeoShape *shape = logVol->getShape();
0066 int shapeId = shape->typeID();
0067 const std::string &name = logVol->getName();
0068 std::shared_ptr<const IGeoShapeConverter> converter =
0069 geoShapesConverters(shapeId);
0070 if (converter == nullptr) {
0071 throw std::runtime_error("The converter for " + printGeoShape(shape) +
0072 " is a nullptr");
0073 }
0074
0075 auto converted =
0076 converter->toSensitiveSurface(geoPV, transform, boundFactory);
0077 if (converted.ok()) {
0078 const auto &[el, sf] = converted.value();
0079
0080 if (!el || !sf) {
0081 throw std::runtime_error(
0082 "The Detector Element or the Surface is a nullptr");
0083 }
0084 sensitives.push_back(converted.value());
0085 ACTS_VERBOSE("(successfully converted: "
0086 << name << " / " << printGeoShape(shape) << " / "
0087 << logVol->getMaterial()->getName() << ")");
0088 return;
0089 }
0090 ACTS_ERROR(name << " / " << printGeoShape(shape)
0091 << " could not be converted by any converter");
0092 }
0093
0094 std::vector<GeoChildNodeWithTrf>
0095 GeoModelDetectorObjectFactory::findAllSubVolumes(const PVConstLink &vol) const {
0096
0097 std::vector<GeoChildNodeWithTrf> subvolumes = getChildrenWithRef(vol, false);
0098 std::vector<GeoChildNodeWithTrf> sensitives;
0099 sensitives.reserve(subvolumes.size());
0100 for (auto &subvolume : subvolumes) {
0101
0102 if (matches(subvolume.nodeName, subvolume.volume)) {
0103 sensitives.push_back(subvolume);
0104 }
0105
0106 if (subvolume.volume->getNChildVols() == 0) {
0107 continue;
0108 }
0109
0110
0111 std::vector<GeoChildNodeWithTrf> senssubsubvolumes =
0112 findAllSubVolumes(subvolume.volume);
0113
0114
0115
0116
0117 std::transform(std::make_move_iterator(senssubsubvolumes.begin()),
0118 std::make_move_iterator(senssubsubvolumes.end()),
0119 std::back_inserter(sensitives),
0120 [&subvolume](GeoChildNodeWithTrf &&volume) {
0121 volume.transform = subvolume.transform * volume.transform;
0122 return volume;
0123 });
0124 }
0125 return sensitives;
0126 }
0127
0128 bool GeoModelDetectorObjectFactory::convertBox(const std::string &name) const {
0129 auto convB = std::ranges::any_of(m_cfg.convertBox, [&](const auto &n) {
0130 return name.find(n) != std::string::npos;
0131 });
0132 return convB;
0133 }
0134
0135 void GeoModelDetectorObjectFactory::convertFpv(
0136 const std::string &name, const FpvConstLink &fpv, Cache &cache,
0137 const GeometryContext & ) {
0138 const std::size_t prevSize = cache.sensitiveSurfaces.size();
0139 {
0140
0141 std::vector<GeoChildNodeWithTrf> subVolToTrf = findAllSubVolumes(fpv);
0142
0143 std::vector<GeoModelSensitiveSurface> sensitives;
0144 sensitives.reserve(subVolToTrf.size());
0145
0146 for (const auto &convertMe : subVolToTrf) {
0147
0148 const Transform3 transform = makeTransform3(fpv->getAbsoluteTransform()) *
0149 makeTransform3(convertMe.transform);
0150 convertSensitive(convertMe.volume, transform, *cache.surfBoundFactory,
0151 sensitives);
0152 }
0153
0154 if (sensitives.empty() && matches(name, fpv)) {
0155 convertSensitive(fpv, makeTransform3(fpv->getAbsoluteTransform()),
0156 *cache.surfBoundFactory, cache.sensitiveSurfaces);
0157 }
0158 cache.sensitiveSurfaces.insert(cache.sensitiveSurfaces.end(),
0159 std::make_move_iterator(sensitives.begin()),
0160 std::make_move_iterator(sensitives.end()));
0161
0162 for (auto i = prevSize; i < cache.sensitiveSurfaces.size(); ++i) {
0163 const auto &detEl = std::get<0>(cache.sensitiveSurfaces[i]);
0164 detEl->setDatabaseEntryName(name);
0165 ACTS_VERBOSE("Set database name of the DetectorElement to "
0166 << detEl->databaseEntryName());
0167 }
0168 }
0169
0170 if (convertBox(name)) {
0171 ConvertedGeoVol &convEnvelope = cache.volumeBoxFPVs.emplace_back();
0172 convEnvelope.name = name;
0173 convEnvelope.fullPhysVol = fpv;
0174 convEnvelope.volume = GeoModel::convertVolume(
0175 makeTransform3(fpv->getAbsoluteTransform()),
0176 fpv->getLogVol()->getShape(), *cache.volumeBoundFactory);
0177 std::transform(cache.sensitiveSurfaces.begin() + prevSize,
0178 cache.sensitiveSurfaces.end(),
0179 std::back_inserter(convEnvelope.surfaces),
0180 [](const GeoModelSensitiveSurface &sensitive) {
0181 return std::get<1>(sensitive);
0182 });
0183 }
0184 }
0185
0186 bool GeoModelDetectorObjectFactory::matches(const std::string &name,
0187 const PVConstLink &physvol) const {
0188 if (m_cfg.nameList.empty() && m_cfg.materialList.empty()) {
0189 return true;
0190 }
0191
0192 auto matchName = std::ranges::any_of(m_cfg.nameList, [&](const auto &n) {
0193 return name.find(n) != std::string::npos;
0194 });
0195
0196 std::string matStr = physvol->getLogVol()->getMaterial()->getName();
0197
0198 auto matchMaterial = std::ranges::any_of(
0199 m_cfg.materialList,
0200 [&](const auto &m) { return matStr.find(m) != std::string::npos; });
0201
0202 bool match = matchMaterial && matchName;
0203
0204
0205 if (m_cfg.nameList.empty()) {
0206 return matchMaterial;
0207 }
0208
0209
0210 if (m_cfg.materialList.empty() ||
0211 dynamic_pointer_cast<const GeoVFullPhysVol>(physvol)) {
0212 return matchName;
0213 }
0214 return match;
0215 }
0216 }