Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:17:03

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 "ActsExamples/Io/Json/JsonSurfacesWriter.hpp"
0010 
0011 #include "Acts/Geometry/ApproachDescriptor.hpp"
0012 #include "Acts/Geometry/BoundarySurfaceT.hpp"
0013 #include "Acts/Geometry/GeometryHierarchyMap.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "Acts/Geometry/Layer.hpp"
0016 #include "Acts/Geometry/TrackingGeometry.hpp"
0017 #include "Acts/Geometry/TrackingVolume.hpp"
0018 #include "Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp"
0019 #include "Acts/Plugins/Json/SurfaceJsonConverter.hpp"
0020 #include "Acts/Plugins/Json/VolumeJsonConverter.hpp"
0021 #include "Acts/Surfaces/Surface.hpp"
0022 #include "Acts/Surfaces/SurfaceArray.hpp"
0023 #include "Acts/Utilities/BinnedArray.hpp"
0024 #include "Acts/Utilities/Logger.hpp"
0025 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0026 #include "ActsExamples/Utilities/Paths.hpp"
0027 
0028 #include <cstddef>
0029 #include <fstream>
0030 #include <iomanip>
0031 #include <sstream>
0032 #include <stdexcept>
0033 #include <string>
0034 #include <utility>
0035 #include <vector>
0036 
0037 #include <nlohmann/json.hpp>
0038 
0039 using namespace ActsExamples;
0040 
0041 JsonSurfacesWriter::JsonSurfacesWriter(const JsonSurfacesWriter::Config& config,
0042                                        Acts::Logging::Level level)
0043     : m_cfg(config),
0044       m_logger(Acts::getDefaultLogger("JsonSurfacesWriter", level)) {
0045   if (!m_cfg.trackingGeometry) {
0046     throw std::invalid_argument("Missing tracking geometry");
0047   }
0048   m_world = m_cfg.trackingGeometry->highestTrackingVolume();
0049   if (m_world == nullptr) {
0050     throw std::invalid_argument("Could not identify the world volume");
0051   }
0052 }
0053 
0054 std::string JsonSurfacesWriter::name() const {
0055   return "JsonSurfacesWriter";
0056 }
0057 
0058 namespace {
0059 
0060 using SurfaceContainer =
0061     Acts::GeometryHierarchyMap<std::shared_ptr<const Acts::Surface>>;
0062 using SurfaceConverter = Acts::GeometryHierarchyMapJsonConverter<
0063     std::shared_ptr<const Acts::Surface>>;
0064 
0065 /// Write all child surfaces and descend into confined volumes.
0066 void collectSurfaces(std::vector<SurfaceContainer::InputElement>& cSurfaces,
0067                      const Acts::TrackingVolume& volume, bool writeLayer,
0068                      bool writeApproach, bool writeSensitive,
0069                      bool writeBoundary) {
0070   // Process all layers that are directly stored within this volume
0071   if (volume.confinedLayers() != nullptr) {
0072     for (const auto& layer : volume.confinedLayers()->arrayObjects()) {
0073       // We jump navigation layers
0074       if (layer->layerType() == Acts::navigation) {
0075         continue;
0076       }
0077       // Layer surface
0078       if (writeLayer) {
0079         auto layerSurfacePtr = layer->surfaceRepresentation().getSharedPtr();
0080         cSurfaces.push_back(SurfaceContainer::InputElement{
0081             layer->surfaceRepresentation().geometryId(), layerSurfacePtr});
0082       }
0083       // Approach surfaces
0084       if (writeApproach && layer->approachDescriptor() != nullptr) {
0085         for (auto sf : layer->approachDescriptor()->containedSurfaces()) {
0086           cSurfaces.push_back(SurfaceContainer::InputElement{
0087               sf->geometryId(), sf->getSharedPtr()});
0088         }
0089       }
0090       // Check for sensitive surfaces
0091       if (layer->surfaceArray() != nullptr && writeSensitive) {
0092         for (const auto& surface : layer->surfaceArray()->surfaces()) {
0093           if (surface != nullptr) {
0094             cSurfaces.push_back(SurfaceContainer::InputElement{
0095                 surface->geometryId(), surface->getSharedPtr()});
0096           }
0097         }
0098       }
0099     }
0100     // This is a navigation volume, write the boundaries
0101     if (writeBoundary) {
0102       for (const auto& bsurface : volume.boundarySurfaces()) {
0103         const auto& bsRep = bsurface->surfaceRepresentation();
0104         cSurfaces.push_back(SurfaceContainer::InputElement{
0105             bsRep.geometryId(), bsRep.getSharedPtr()});
0106       }
0107     }
0108   }
0109   // Step down into hierarchy to process all child volumnes
0110   if (volume.confinedVolumes()) {
0111     for (const auto& confined : volume.confinedVolumes()->arrayObjects()) {
0112       collectSurfaces(cSurfaces, *confined.get(), writeLayer, writeApproach,
0113                       writeSensitive, writeBoundary);
0114     }
0115   }
0116 }
0117 }  // namespace
0118 
0119 ProcessCode JsonSurfacesWriter::write(const AlgorithmContext& ctx) {
0120   if (!m_cfg.writePerEvent) {
0121     return ProcessCode::SUCCESS;
0122   }
0123 
0124   std::ofstream out;
0125   out.open(perEventFilepath(m_cfg.outputDir, "detector.json", ctx.eventNumber));
0126 
0127   std::vector<SurfaceContainer::InputElement> cSurfaces;
0128   collectSurfaces(cSurfaces, *m_world, m_cfg.writeLayer, m_cfg.writeApproach,
0129                   m_cfg.writeSensitive, m_cfg.writeBoundary);
0130   SurfaceContainer sContainer(cSurfaces);
0131 
0132   if (!m_cfg.writeOnlyNames) {
0133     auto j = SurfaceConverter("surfaces").toJson(sContainer, nullptr);
0134     out << std::setprecision(m_cfg.outputPrecision) << j.dump(2);
0135     out.close();
0136   } else {
0137     using NamedContainer = Acts::GeometryHierarchyMap<std::string>;
0138     using NamedConverter = Acts::GeometryHierarchyMapJsonConverter<std::string>;
0139 
0140     std::vector<std::pair<Acts::GeometryIdentifier, std::string>> namedEntries;
0141     for (std::size_t is = 0; is < sContainer.size(); ++is) {
0142       Acts::GeometryIdentifier geometryId = sContainer.idAt(is);
0143       std::stringstream geoTypeName;
0144       geoTypeName << geometryId;
0145       namedEntries.push_back({geometryId, geoTypeName.str()});
0146     }
0147     NamedContainer nContainer(namedEntries);
0148     auto j = NamedConverter("surface_types").toJson(nContainer, nullptr);
0149     out << j.dump(2);
0150     out.close();
0151   }
0152 
0153   return ProcessCode::SUCCESS;
0154 }
0155 
0156 ProcessCode JsonSurfacesWriter::finalize() {
0157   std::ofstream out;
0158   out.open(joinPaths(m_cfg.outputDir, "detector.csv"));
0159 
0160   std::vector<SurfaceContainer::InputElement> cSurfaces;
0161   collectSurfaces(cSurfaces, *m_world, m_cfg.writeLayer, m_cfg.writeApproach,
0162                   m_cfg.writeSensitive, m_cfg.writeBoundary);
0163   SurfaceContainer sContainer(cSurfaces);
0164 
0165   auto j = SurfaceConverter("surfaces").toJson(sContainer, nullptr);
0166   out << std::setprecision(m_cfg.outputPrecision) << j.dump(2);
0167   out.close();
0168 
0169   return ProcessCode::SUCCESS;
0170 }