24#include <TGeoManager.h>
26#include "Acts/Geometry/GeometryIdentifier.hpp"
27#include "Acts/Geometry/TrackingVolume.hpp"
28#include "Acts/Surfaces/Surface.hpp"
29#include "ActsPlugins/Json/JsonMaterialDecorator.hpp"
30#include "ActsPlugins/Json/MaterialMapJsonConverter.hpp"
44bool hasO2GeometryObject(std::string_view fileName)
46 TFile
file(std::string(fileName).c_str());
47 if (
file.IsZombie()) {
48 throw std::runtime_error(
"o2::acts::TrackingGeometryManager: cannot open '" + std::string(fileName) +
"'");
55TrackingGeometryManager::TrackingGeometryManager()
56 : mNominalContext(
Acts::GeometryContext::dangerouslyDefaultConstruct())
66void TrackingGeometryManager::setBuilder(std::unique_ptr<ITrackingGeometryBuilder> builder)
69 throw std::runtime_error(
70 "o2::acts::TrackingGeometryManager: cannot change the builder after the geometry was built. "
71 "Call clear() first if this is really intended.");
73 mBuilder = std::move(builder);
76void TrackingGeometryManager::clear()
81 mElementStore.clear();
84 mNDecoratedSurfaces = 0;
87void TrackingGeometryManager::build()
90 LOG(info) <<
"ACTS tracking geometry is already built, nothing to do";
94 throw std::runtime_error(
95 "o2::acts::TrackingGeometryManager: no TGeo geometry is loaded. Declare "
96 "o2::base::GRPGeomRequest::Aligned in the workflow's GRPGeomRequest and call "
97 "GRPGeomHelper::instance().checkUpdates(pc) before asking for the ACTS geometry, or use "
98 "buildFromFile() outside a workflow.");
103void TrackingGeometryManager::buildFromFile(std::string_view geomFileName)
106 LOG(info) <<
"ACTS tracking geometry is already built, nothing to do";
110 LOG(warn) <<
"A TGeo geometry is already loaded, ignoring the requested file and using it";
115 LOGP(info,
"Loading TGeo geometry from '{}'", geomFileName);
116 if (hasO2GeometryObject(geomFileName)) {
123 LOGP(warn,
"'{}' holds no '{}' or '{}' object: importing it as a plain TGeo file",
125 if (TGeoManager::Import(std::string(geomFileName).c_str()) ==
nullptr) {
126 throw std::runtime_error(
"o2::acts::TrackingGeometryManager: cannot import TGeo geometry from '" +
127 std::string(geomFileName) +
"'");
133void TrackingGeometryManager::doBuild()
135 if (mBuilder ==
nullptr) {
136 throw std::runtime_error(
137 "o2::acts::TrackingGeometryManager: no builder installed. Call setBuilder() with the "
138 "detector-specific ITrackingGeometryBuilder before requesting the geometry.");
141 LOGP(info,
"Building the ACTS tracking geometry with the '{}' builder", mBuilder->getName());
142 auto output = mBuilder->build(*gGeoManager, mNominalContext);
143 if (
output.geometry ==
nullptr) {
144 throw std::runtime_error(std::string(
"o2::acts::TrackingGeometryManager: builder '") +
145 mBuilder->getName() +
"' returned no geometry");
148 mElementStore = std::move(
output.elementStore);
149 mIndex = std::move(
output.index);
151 if (!mMaterialMapFile.empty()) {
154 applyMaterialMapImpl(*
output.geometry);
157 mGeometry = std::const_pointer_cast<const Acts::TrackingGeometry>(
output.geometry);
159 LOGP(info,
"ACTS tracking geometry ready: {} mapped sensors, {} surfaces with material",
160 mIndex.size(), mNDecoratedSurfaces);
163void TrackingGeometryManager::applyMaterialMapImpl(Acts::TrackingGeometry& geometry)
165 if (!std::filesystem::exists(mMaterialMapFile)) {
166 throw std::runtime_error(
"o2::acts::TrackingGeometryManager: material map '" + mMaterialMapFile +
170 Acts::MaterialMapJsonConverter::Config converterConfig;
171 converterConfig.context = mNominalContext;
172 const Acts::JsonMaterialDecorator decorator(converterConfig, mMaterialMapFile, mLogLevel);
174 mNDecoratedSurfaces = 0;
175 geometry.apply([&](Acts::Surface&
surface) {
177 if (
surface.surfaceMaterial() !=
nullptr) {
178 ++mNDecoratedSurfaces;
182 if (mNDecoratedSurfaces == 0) {
186 throw std::runtime_error(
187 "o2::acts::TrackingGeometryManager: material map '" + mMaterialMapFile +
188 "' matched no surface. It was produced for a different geometry structure and must be "
189 "regenerated for this one.");
193std::shared_ptr<const Acts::TrackingGeometry> TrackingGeometryManager::getShared()
201const Acts::TrackingGeometry& TrackingGeometryManager::get()
222 const auto it = mDetIndices.find(detID);
223 if (it != mDetIndices.end()) {
226 auto [inserted, ok] = mDetIndices.emplace(
227 detID,
makeSurfaceIndex(*mGeometry, mNominalContext, cache, toleranceCm, std::move(pathProvider)));
228 return inserted->second;
Definition of the GeometryManager class.
Definition of the Names Generator class.
const Acts::Surface * surface
Process-wide provider of the ACTS tracking geometry.
static bool isGeometryLoaded()
static void loadGeometry(std::string_view geomFilePath="", bool applyMisalignment=false, bool preferAlignedFile=true)
static constexpr std::string_view CCDBOBJECT
static constexpr std::string_view GEOMOBJECTNAME_FAIR
const o2::detectors::DetID & getDetID() const
SurfaceIndexMap makeSurfaceIndex(const Acts::TrackingGeometry &geometry, const Acts::GeometryContext &gctx, const o2::detectors::DetMatrixCache &cache, double toleranceCm=1e-3, SensorPathProvider pathProvider={})
std::function< std::string(int sensorID)> SensorPathProvider
Full TGeo node path of one sensor, e.g. o2::trk::GeometryTGeo::getMatrixPath().
int32_t const char * file
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"