-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add export to json geometry using hierarchy map (#679)
Adding Surface writing capability to the framework.
- Loading branch information
1 parent
9efc8fc
commit c97d2af
Showing
11 changed files
with
259 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
77 changes: 77 additions & 0 deletions
77
Examples/Io/Json/include/ActsExamples/Io/Json/JsonSurfacesWriter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2021 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "ActsExamples/Framework/IWriter.hpp" | ||
#include <Acts/Geometry/TrackingGeometry.hpp> | ||
#include <Acts/Utilities/Logger.hpp> | ||
|
||
#include <limits> | ||
|
||
namespace Acts { | ||
class TrackingVolume; | ||
} | ||
|
||
namespace ActsExamples { | ||
|
||
/// Write out the geometry for detector surfaces. | ||
/// | ||
/// This writes a `detectors.json` file at the end of the run using the | ||
/// default context to determine the geometry. If configured, it also writes | ||
/// an additional file for each event using the following schema | ||
/// | ||
/// event000000001-detectors.json | ||
/// event000000002-detectors.json | ||
/// ... | ||
/// | ||
/// that uses the per-event context to determine the geometry. | ||
class JsonSurfacesWriter : public IWriter { | ||
public: | ||
struct Config { | ||
/// The tracking geometry that should be written. | ||
std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry; | ||
/// Where to place output files. | ||
std::string outputDir; | ||
/// Number of decimal digits for floating point precision in output. | ||
std::size_t outputPrecision = std::numeric_limits<float>::max_digits10; | ||
/// Write layer surfaces | ||
bool writeLayer = false; | ||
/// Write layer approach | ||
bool writeApproach = false; | ||
/// Write sensitive surfaces | ||
bool writeSensitive = true; | ||
/// Write boundary surfaces | ||
bool writeBoundary = false; | ||
/// Whether to write the per-event file. | ||
bool writePerEvent = false; | ||
}; | ||
|
||
/// Construct the geometry writer. | ||
/// | ||
/// @param cfg is the configuration object | ||
/// @param lvl is the logging level | ||
JsonSurfacesWriter(const Config& cfg, Acts::Logging::Level lvl); | ||
|
||
std::string name() const final override; | ||
|
||
/// Write geometry using the per-event context (optional). | ||
ProcessCode write(const AlgorithmContext& context) final override; | ||
|
||
/// Write geometry using the default context. | ||
ProcessCode endRun() final override; | ||
|
||
private: | ||
Config m_cfg; | ||
const Acts::TrackingVolume* m_world; | ||
std::unique_ptr<const Acts::Logger> m_logger; | ||
|
||
const Acts::Logger& logger() const { return *m_logger; } | ||
}; | ||
|
||
} // namespace ActsExamples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2021 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "ActsExamples/Io/Json/JsonSurfacesWriter.hpp" | ||
|
||
#include "Acts/Geometry/GeometryHierarchyMap.hpp" | ||
#include "Acts/Geometry/TrackingVolume.hpp" | ||
#include "Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp" | ||
#include "Acts/Plugins/Json/SurfaceJsonConverter.hpp" | ||
#include "Acts/Surfaces/Surface.hpp" | ||
#include "ActsExamples/Utilities/Paths.hpp" | ||
|
||
using namespace ActsExamples; | ||
|
||
JsonSurfacesWriter::JsonSurfacesWriter(const JsonSurfacesWriter::Config& cfg, | ||
Acts::Logging::Level lvl) | ||
: m_cfg(cfg), | ||
m_world(nullptr), | ||
m_logger(Acts::getDefaultLogger("JsonSurfacesWriter", lvl)) { | ||
if (not m_cfg.trackingGeometry) { | ||
throw std::invalid_argument("Missing tracking geometry"); | ||
} | ||
m_world = m_cfg.trackingGeometry->highestTrackingVolume(); | ||
if (not m_world) { | ||
throw std::invalid_argument("Could not identify the world volume"); | ||
} | ||
} | ||
|
||
std::string JsonSurfacesWriter::name() const { | ||
return "JsonSurfacesWriter"; | ||
} | ||
|
||
namespace { | ||
|
||
using SurfaceContainer = | ||
Acts::GeometryHierarchyMap<std::shared_ptr<const Acts::Surface>>; | ||
using SurfaceConverter = Acts::GeometryHierarchyMapJsonConverter< | ||
std::shared_ptr<const Acts::Surface>>; | ||
|
||
/// Write all child surfaces and descend into confined volumes. | ||
void collectSurfaces(std::vector<SurfaceContainer::InputElement>& cSurfaces, | ||
const Acts::TrackingVolume& volume, bool writeLayer, | ||
bool writeApproach, bool writeSensitive, | ||
bool writeBoundary) { | ||
// Process all layers that are directly stored within this volume | ||
if (volume.confinedLayers()) { | ||
for (auto layer : volume.confinedLayers()->arrayObjects()) { | ||
// We jump navigation layers | ||
if (layer->layerType() == Acts::navigation) { | ||
continue; | ||
} | ||
// Layer surface | ||
if (writeLayer) { | ||
auto layerSurfacePtr = layer->surfaceRepresentation().getSharedPtr(); | ||
cSurfaces.push_back(SurfaceContainer::InputElement{ | ||
layer->surfaceRepresentation().geometryId(), layerSurfacePtr}); | ||
} | ||
// Approach surfaces | ||
if (writeApproach and layer->approachDescriptor()) { | ||
for (auto sf : layer->approachDescriptor()->containedSurfaces()) { | ||
cSurfaces.push_back(SurfaceContainer::InputElement{ | ||
sf->geometryId(), sf->getSharedPtr()}); | ||
} | ||
} | ||
// Check for sensitive surfaces | ||
if (layer->surfaceArray() and writeSensitive) { | ||
for (auto surface : layer->surfaceArray()->surfaces()) { | ||
if (surface) { | ||
cSurfaces.push_back( | ||
SurfaceContainer::InputElement{surface->geometryId(), surface}); | ||
} | ||
} | ||
} | ||
} | ||
// This is a navigation volume, write the boundaries | ||
if (writeBoundary) { | ||
for (auto bsurface : volume.boundarySurfaces()) { | ||
const auto& bsRep = bsurface->surfaceRepresentation(); | ||
cSurfaces.push_back(SurfaceContainer::InputElement{ | ||
bsRep.geometryId(), bsRep.getSharedPtr()}); | ||
} | ||
} | ||
} | ||
// Step down into hierarchy to process all child volumnes | ||
if (volume.confinedVolumes()) { | ||
for (auto confined : volume.confinedVolumes()->arrayObjects()) { | ||
collectSurfaces(cSurfaces, *confined.get(), writeLayer, writeApproach, | ||
writeSensitive, writeBoundary); | ||
} | ||
} | ||
} | ||
} // namespace | ||
|
||
ProcessCode JsonSurfacesWriter::write(const AlgorithmContext& ctx) { | ||
if (not m_cfg.writePerEvent) { | ||
return ProcessCode::SUCCESS; | ||
} | ||
|
||
std::ofstream out; | ||
out.open(perEventFilepath(m_cfg.outputDir, "detector.json", ctx.eventNumber)); | ||
|
||
std::vector<SurfaceContainer::InputElement> cSurfaces; | ||
collectSurfaces(cSurfaces, *m_world, m_cfg.writeLayer, m_cfg.writeApproach, | ||
m_cfg.writeSensitive, m_cfg.writeBoundary); | ||
SurfaceContainer sContainer(cSurfaces); | ||
|
||
auto j = SurfaceConverter("surfaces").toJson(sContainer); | ||
out << std::setprecision(m_cfg.outputPrecision) << j.dump(2); | ||
out.close(); | ||
|
||
return ProcessCode::SUCCESS; | ||
} | ||
|
||
ProcessCode JsonSurfacesWriter::endRun() { | ||
std::ofstream out; | ||
out.open(joinPaths(m_cfg.outputDir, "detector.csv")); | ||
|
||
std::vector<SurfaceContainer::InputElement> cSurfaces; | ||
collectSurfaces(cSurfaces, *m_world, m_cfg.writeLayer, m_cfg.writeApproach, | ||
m_cfg.writeSensitive, m_cfg.writeBoundary); | ||
SurfaceContainer sContainer(cSurfaces); | ||
|
||
auto j = SurfaceConverter("surfaces").toJson(sContainer); | ||
out << std::setprecision(m_cfg.outputPrecision) << j.dump(2); | ||
out.close(); | ||
|
||
return ProcessCode::SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters