Skip to content

Commit

Permalink
refactor: move some implementation from header to source (#2371)
Browse files Browse the repository at this point in the history
This caused linker errors when using a installed version of Acts...
  • Loading branch information
benjaminhuth committed Aug 16, 2023
1 parent c83f8fc commit 543bd5a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 57 deletions.
1 change: 1 addition & 0 deletions Plugins/Json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(
src/ExtentJsonConverter.cpp
src/GridJsonConverter.cpp
src/IndexedSurfacesJsonConverter.cpp
src/JsonMaterialDecorator.cpp
src/MaterialMapJsonConverter.cpp
src/MaterialJsonConverter.cpp
src/PortalJsonConverter.cpp
Expand Down
60 changes: 3 additions & 57 deletions Plugins/Json/include/Acts/Plugins/Json/JsonMaterialDecorator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,71 +39,17 @@ class JsonMaterialDecorator : public IMaterialDecorator {
const std::string& jFileName,
Acts::Logging::Level level,
bool clearSurfaceMaterial = true,
bool clearVolumeMaterial = true)
: m_readerConfig(rConfig),
m_clearSurfaceMaterial(clearSurfaceMaterial),
m_clearVolumeMaterial(clearVolumeMaterial),
m_logger{getDefaultLogger("JsonMaterialDecorator", level)} {
// the material reader
Acts::MaterialMapJsonConverter jmConverter(rConfig, level);

ACTS_VERBOSE("Reading JSON material description from: " << jFileName);
std::ifstream ifj(jFileName.c_str());
if (!ifj.good()) {
throw std::runtime_error{"Unable to open input JSON material file: " +
jFileName};
}
nlohmann::json jin;

if (jFileName.find(".cbor") != std::string::npos) {
std::vector<std::uint8_t> iCbor((std::istreambuf_iterator<char>(ifj)),
std::istreambuf_iterator<char>());
jin = nlohmann::json::from_cbor(iCbor);
} else {
ifj >> jin;
}

auto maps = jmConverter.jsonToMaterialMaps(jin);
m_surfaceMaterialMap = maps.first;
m_volumeMaterialMap = maps.second;
ACTS_VERBOSE("JSON material description read complete");
}
bool clearVolumeMaterial = true);

/// Decorate a surface
///
/// @param surface the non-cost surface that is decorated
void decorate(Surface& surface) const final {
ACTS_VERBOSE("Processing surface: " << surface.geometryId());
// Clear the material if registered to do so
if (m_clearSurfaceMaterial) {
ACTS_VERBOSE("-> Clearing surface material");
surface.assignSurfaceMaterial(nullptr);
}
// Try to find the surface in the map
auto sMaterial = m_surfaceMaterialMap.find(surface.geometryId());
if (sMaterial != m_surfaceMaterialMap.end()) {
ACTS_VERBOSE("-> Found material for surface, assigning");
surface.assignSurfaceMaterial(sMaterial->second);
}
}
void decorate(Surface& surface) const final;

/// Decorate a TrackingVolume
///
/// @param volume the non-cost volume that is decorated
void decorate(TrackingVolume& volume) const final {
ACTS_VERBOSE("Processing volume: " << volume.geometryId());
// Clear the material if registered to do so
if (m_clearVolumeMaterial) {
ACTS_VERBOSE("-> Clearing volume material");
volume.assignVolumeMaterial(nullptr);
}
// Try to find the volume in the map
auto vMaterial = m_volumeMaterialMap.find(volume.geometryId());
if (vMaterial != m_volumeMaterialMap.end()) {
ACTS_VERBOSE("-> Found material for volume, assigning");
volume.assignVolumeMaterial(vMaterial->second);
}
}
void decorate(TrackingVolume& volume) const final;

private:
MaterialMapJsonConverter::Config m_readerConfig;
Expand Down
86 changes: 86 additions & 0 deletions Plugins/Json/src/JsonMaterialDecorator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This file is part of the Acts project.
//
// Copyright (C) 2017-2019 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/.

// This file is part of the Acts project.
//
// Copyright (C) 2017-2019 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 "Acts/Plugins/Json/JsonMaterialDecorator.hpp"

namespace Acts {

JsonMaterialDecorator::JsonMaterialDecorator(
const MaterialMapJsonConverter::Config& rConfig,
const std::string& jFileName, Acts::Logging::Level level,
bool clearSurfaceMaterial, bool clearVolumeMaterial)
: m_readerConfig(rConfig),
m_clearSurfaceMaterial(clearSurfaceMaterial),
m_clearVolumeMaterial(clearVolumeMaterial),
m_logger{getDefaultLogger("JsonMaterialDecorator", level)} {
// the material reader
Acts::MaterialMapJsonConverter jmConverter(rConfig, level);

ACTS_VERBOSE("Reading JSON material description from: " << jFileName);
std::ifstream ifj(jFileName.c_str());
if (!ifj.good()) {
throw std::runtime_error{"Unable to open input JSON material file: " +
jFileName};
}
nlohmann::json jin;

if (jFileName.find(".cbor") != std::string::npos) {
std::vector<std::uint8_t> iCbor((std::istreambuf_iterator<char>(ifj)),
std::istreambuf_iterator<char>());
jin = nlohmann::json::from_cbor(iCbor);
} else {
ifj >> jin;
}

auto maps = jmConverter.jsonToMaterialMaps(jin);
m_surfaceMaterialMap = maps.first;
m_volumeMaterialMap = maps.second;
ACTS_VERBOSE("JSON material description read complete");
}

void JsonMaterialDecorator::decorate(Surface& surface) const {
ACTS_VERBOSE("Processing surface: " << surface.geometryId());
// Clear the material if registered to do so
if (m_clearSurfaceMaterial) {
ACTS_VERBOSE("-> Clearing surface material");
surface.assignSurfaceMaterial(nullptr);
}
// Try to find the surface in the map
auto sMaterial = m_surfaceMaterialMap.find(surface.geometryId());
if (sMaterial != m_surfaceMaterialMap.end()) {
ACTS_VERBOSE("-> Found material for surface, assigning");
surface.assignSurfaceMaterial(sMaterial->second);
}
}

/// Decorate a TrackingVolume
///
/// @param volume the non-cost volume that is decorated
void JsonMaterialDecorator::decorate(TrackingVolume& volume) const {
ACTS_VERBOSE("Processing volume: " << volume.geometryId());
// Clear the material if registered to do so
if (m_clearVolumeMaterial) {
ACTS_VERBOSE("-> Clearing volume material");
volume.assignVolumeMaterial(nullptr);
}
// Try to find the volume in the map
auto vMaterial = m_volumeMaterialMap.find(volume.geometryId());
if (vMaterial != m_volumeMaterialMap.end()) {
ACTS_VERBOSE("-> Found material for volume, assigning");
volume.assignVolumeMaterial(vMaterial->second);
}
}
} // namespace Acts

0 comments on commit 543bd5a

Please sign in to comment.