Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detray json writing fixes #2743

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void convert(nlohmann::json& jIndexedVolumes,
auto castedDelegate = dynamic_cast<const DelegateType*>(instance);
if (castedDelegate != nullptr) {
jIndexedVolumes = IndexedGridJsonHelper::convertImpl<DelegateType>(
*castedDelegate, detray);
*castedDelegate, detray, false);
if (detray) {
jIndexedVolumes["volume_link"] = 1;
nlohmann::json jAccLink;
Expand Down
18 changes: 12 additions & 6 deletions Plugins/Json/include/Acts/Plugins/Json/GridJsonConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ nlohmann::json toJson(const grid_type& grid) {
///
/// @tparam grid_type the type of the grid
/// @param grid the grid object
/// @param swapAxis - this is needed for detray
///
/// @note detray has a different offset for the
/// local indices, it starts at 0
///
/// @return a json object to represent the grid
template <typename grid_type>
nlohmann::json toJsonDetray(const grid_type& grid) {
nlohmann::json toJsonDetray(const grid_type& grid, bool swapAxis = false) {
nlohmann::json jGrid;

auto axes = grid.axes();
Expand All @@ -106,7 +110,7 @@ nlohmann::json toJsonDetray(const grid_type& grid) {
if constexpr (grid_type::DIM == 1u) {
for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
typename grid_type::index_t lbin;
lbin[0u] = ib0;
lbin[0u] = ib0 - 1u;
nlohmann::json jBin;
jBin["loc_index"] = lbin;
jBin["content"] = grid.atLocalBins(lbin);
Expand All @@ -116,11 +120,13 @@ nlohmann::json toJsonDetray(const grid_type& grid) {

// 2D connections
if constexpr (grid_type::DIM == 2u) {
for (unsigned int ib0 = 1u; ib0 <= axes[0u]->getNBins(); ++ib0) {
for (unsigned int ib1 = 1u; ib1 <= axes[1u]->getNBins(); ++ib1) {
unsigned int iaxis0 = swapAxis ? 1u : 0u;
unsigned int iaxis1 = swapAxis ? 0u : 1u;
for (unsigned int ib0 = 1u; ib0 <= axes[iaxis0]->getNBins(); ++ib0) {
for (unsigned int ib1 = 1u; ib1 <= axes[iaxis1]->getNBins(); ++ib1) {
typename grid_type::index_t lbin;
lbin[0u] = ib0;
lbin[1u] = ib1;
lbin[0u] = ib0 - 1u;
lbin[1u] = ib1 - 1u;
nlohmann::json jBin;
jBin["loc_index"] = lbin;
jBin["content"] = grid.atLocalBins(lbin);
Expand Down
14 changes: 12 additions & 2 deletions Plugins/Json/include/Acts/Plugins/Json/IndexedGridJsonHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ using namespace Experimental::detail::GridAxisGenerators;
namespace IndexedGridJsonHelper {

/// @brief The actual conversion method
///
/// @param indexGrid is the index grid to be written
/// @param detray is a flag indicating detray writout
/// @param checkSwap is a flag indicating if the axes should be swapped
template <typename index_grid>
nlohmann::json convertImpl(const index_grid& indexGrid, bool detray = false) {
nlohmann::json convertImpl(const index_grid& indexGrid, bool detray = false,
bool checkSwap = false) {
nlohmann::json jIndexedGrid;

// Axis swapping
bool swapAxes = checkSwap;

// Fill the casts
nlohmann::json jCasts;
// 1D casts
Expand All @@ -36,12 +44,14 @@ nlohmann::json convertImpl(const index_grid& indexGrid, bool detray = false) {
if constexpr (index_grid::grid_type::DIM == 2u) {
jCasts.push_back(indexGrid.casts[0u]);
jCasts.push_back(indexGrid.casts[1u]);
swapAxes = checkSwap &&
(indexGrid.casts[0u] == binZ && indexGrid.casts[1u] == binPhi);
}
jIndexedGrid["casts"] = jCasts;
jIndexedGrid["transform"] =
Transform3JsonConverter::toJson(indexGrid.transform);
if (detray) {
jIndexedGrid = GridJsonConverter::toJsonDetray(indexGrid.grid);
jIndexedGrid = GridJsonConverter::toJsonDetray(indexGrid.grid, swapAxes);
} else {
jIndexedGrid["grid"] = GridJsonConverter::toJson(indexGrid.grid);
}
Expand Down
17 changes: 17 additions & 0 deletions Plugins/Json/src/DetectorJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ nlohmann::json Acts::DetectorJsonConverter::toJsonDetray(
if (jSurfacesDelegate.is_null()) {
continue;
}
// Patch axes for cylindrical grid surfaces, axes are swapped
// at this point
auto jAccLink = jSurfacesDelegate["acc_link"];
std::size_t accLinkType = jAccLink["type"];
if (accLinkType == 4u) {
// Radial value to transfer phi to rphi
std::vector<ActsScalar> bValues = volume->volumeBounds().values();
ActsScalar rRef = 0.5 * (bValues[1] + bValues[0]);
// Get the axes
auto& jAxes = jSurfacesDelegate["axes"];
// r*phi axis is the first one
std::vector<ActsScalar> jAxesEdges = jAxes[0u]["edges"];
std::for_each(jAxesEdges.begin(), jAxesEdges.end(),
[rRef](ActsScalar& phi) { phi *= rRef; });
// Write back the patches axis edges
jSurfacesDelegate["axes"][0u]["edges"] = jAxesEdges;
}
// Colplete the grid json for detray usage
jSurfacesDelegate["volume_link"] = iv;
// jSurfacesDelegate["acc_link"] =
Expand Down
11 changes: 6 additions & 5 deletions Plugins/Json/src/PortalJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Acts/Surfaces/RegularSurface.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Utilities/Enumerate.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

#include <algorithm>
#include <iterator>
Expand Down Expand Up @@ -134,11 +135,11 @@ std::vector<nlohmann::json> Acts::PortalJsonConverter::toJsonDetray(
const auto& cast = multiLink1D->indexedUpdator.casts[0u];
const auto& transform = multiLink1D->indexedUpdator.transform;
const auto& volumes = multiLink1D->indexedUpdator.extractor.dVolumes;
if (!transform.isApprox(Transform3::Identity())) {
std::runtime_error(
"PortalJsonConverter: transformed boundary link implementation not "
"(yet) supported");
}

// Apply the correction from local to global boundaries
ActsScalar gCorr = VectorHelpers::cast(transform.translation(), cast);
std::for_each(boundaries.begin(), boundaries.end(),
[&gCorr](ActsScalar& b) { b -= gCorr; });

// Get the volume indices
auto surfaceType = surfaceAdjusted->type();
Expand Down
Loading