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

refactor!: Make nlohmann::json a public dependency #3954

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ option(ACTS_USE_SYSTEM_DETRAY "Use a system-provided detray installation" ${ACTS
option(ACTS_USE_SYSTEM_TRACCC "Use a system-provided traccc installation" ${ACTS_USE_SYSTEM_LIBS})
option(ACTS_USE_SYSTEM_VECMEM "Use a system-provided vecmem installation" ${ACTS_USE_SYSTEM_LIBS})
option(ACTS_USE_SYSTEM_ALGEBRAPLUGINS "Use a system-provided algebra-plugins installation" ${ACTS_USE_SYSTEM_LIBS})
option(ACTS_USE_SYSTEM_NLOHMANN_JSON "Use nlohmann::json provided by the system instead of the bundled version" ${ACTS_USE_SYSTEM_LIBS})
option(ACTS_USE_SYSTEM_NLOHMANN_JSON "Use nlohmann::json provided by the system instead of the bundled version" ON)
option(ACTS_USE_SYSTEM_PYBIND11 "Use a system installation of pybind11" ${ACTS_USE_SYSTEM_LIBS} )
option(ACTS_USE_SYSTEM_EIGEN3 "Use a system-provided eigen3" ON)

Expand Down Expand Up @@ -90,6 +90,7 @@ option(ACTS_RUN_CLANG_TIDY "Run clang-tidy static analysis" OFF)
option(ACTS_BUILD_DOCS "Build documentation" OFF)
option(ACTS_SETUP_BOOST "Explicitly set up Boost for the project" ON)
option(ACTS_SETUP_EIGEN3 "Explicitly set up Eigen3 for the project" ON)
option(ACTS_SETUP_NLOHMANN_JSON "Explicitly set up nlohmann_json for the project" ON)
option(ACTS_BUILD_ODD "Build the OpenDataDetector" OFF)
# profiling related options
option(ACTS_ENABLE_CPU_PROFILING "Enable CPU profiling using gperftools" OFF)
Expand Down Expand Up @@ -309,6 +310,15 @@ if(ACTS_SETUP_EIGEN3)
endif()
endif()

if(ACTS_SETUP_NLOHMANN_JSON)
if(ACTS_USE_SYSTEM_NLOHMANN_JSON)
message(STATUS "Using system installation of nlohmann::json")
find_package(nlohmann_json ${_acts_nlohmanjson_version} REQUIRED CONFIG)
else()
add_subdirectory(thirdparty/nlohmann_json)
endif()
endif()

find_package(Filesystem REQUIRED)

# the `<project_name>_VERSION` variables set by `setup(... VERSION ...)` have
Expand Down Expand Up @@ -368,13 +378,7 @@ if(ACTS_BUILD_PLUGIN_DD4HEP)
COMPONENTS DDCore DDDetectors
)
endif()
if(ACTS_BUILD_PLUGIN_JSON)
if(ACTS_USE_SYSTEM_NLOHMANN_JSON)
find_package(nlohmann_json ${_acts_nlohmanjson_version} REQUIRED CONFIG)
else()
add_subdirectory(thirdparty/nlohmann_json)
endif()
endif()

if(ACTS_BUILD_PLUGIN_GEOMODEL)
find_package(GeoModelCore CONFIG)
if(NOT GeoModelCore_FOUND)
Expand All @@ -395,6 +399,7 @@ if(ACTS_BUILD_PLUGIN_GEOMODEL)
# find other GeoModel components of EXACT same version
find_package(GeoModelIO ${GeoModelCore_VERSION} REQUIRED EXACT CONFIG)
endif()

if(ACTS_BUILD_PLUGIN_TGEO)
find_package(
ROOT
Expand Down
5 changes: 4 additions & 1 deletion Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ target_include_directories(
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(ActsCore PUBLIC Boost::boost Eigen3::Eigen)
target_link_libraries(
ActsCore
PUBLIC Boost::boost Eigen3::Eigen nlohmann_json::nlohmann_json
)

if(ACTS_PARAMETER_DEFINITIONS_HEADER)
target_compile_definitions(
Expand Down
19 changes: 19 additions & 0 deletions Core/include/Acts/Definitions/Algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <Eigen/Geometry>
#endif

#include <nlohmann/json.hpp>

namespace Acts {

/// @defgroup acts-algebra-types Vector/matrix types with a common scalar type
Expand Down Expand Up @@ -91,4 +93,21 @@ using Transform3 = Eigen::Transform<double, 3, Eigen::Affine>;

constexpr double s_transformEquivalentTolerance = 1e-9;

void to_json(nlohmann::json& j, const Transform3& t);

void from_json(const nlohmann::json& j, Transform3& t);

} // namespace Acts

NLOHMANN_JSON_NAMESPACE_BEGIN
template <>
struct adl_serializer<Acts::Transform3> {
static void to_json(json& j, const Acts::Transform3& t) {
Acts::to_json(j, t);
}

static void from_json(const json& j, Acts::Transform3& t) {
Acts::from_json(j, t);
}
};
NLOHMANN_JSON_NAMESPACE_END
6 changes: 6 additions & 0 deletions Core/include/Acts/Geometry/Extent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <string>
#include <vector>

#include <nlohmann/json_fwd.hpp>

namespace Acts {

using Envelope = std::array<double, 2>;
Expand Down Expand Up @@ -326,6 +328,10 @@ class Extent {
std::array<std::vector<double>, numBinningValues()> m_valueHistograms;
};

void to_json(nlohmann::json& j, const Extent& e);

void from_json(const nlohmann::json& j, Extent& e);

inline Range1D<double> Acts::Extent::range(BinningValue bValue) const {
return m_range[toUnderlying(bValue)];
}
Expand Down
11 changes: 11 additions & 0 deletions Core/include/Acts/Utilities/AxisFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <ostream>

#include <nlohmann/json.hpp>

namespace Acts {
/// Enum which determines how the axis handle its outer boundaries
/// possible values values
Expand All @@ -27,6 +29,11 @@ enum class AxisBoundaryType {
Closed,
};

NLOHMANN_JSON_SERIALIZE_ENUM(Acts::AxisBoundaryType,
{{Acts::AxisBoundaryType::Bound, "Bound"},
{Acts::AxisBoundaryType::Open, "Open"},
{Acts::AxisBoundaryType::Closed, "Closed"}})

/// Tag helper type for Axis constructors with class template deduction
/// @tparam bdt the boundary type
template <AxisBoundaryType bdt>
Expand Down Expand Up @@ -60,6 +67,10 @@ enum class AxisType {
Variable,
};

NLOHMANN_JSON_SERIALIZE_ENUM(Acts::AxisType,
{{Acts::AxisType::Equidistant, "Equidistant"},
{Acts::AxisType::Variable, "Variable"}})

inline std::ostream& operator<<(std::ostream& os, AxisType type) {
switch (type) {
case AxisType::Equidistant:
Expand Down
8 changes: 6 additions & 2 deletions Core/include/Acts/Utilities/BinUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class BinUtility {
///
/// @param bData is the provided binning data
/// @param tForm is the (optional) transform
BinUtility(const BinningData& bData,
const Transform3& tForm = Transform3::Identity())
explicit BinUtility(const BinningData& bData,
const Transform3& tForm = Transform3::Identity())
: m_binningData(), m_transform(tForm), m_itransform(tForm.inverse()) {
m_binningData.reserve(3);
m_binningData.push_back(bData);
Expand Down Expand Up @@ -328,4 +328,8 @@ class BinUtility {
Transform3 m_itransform; /// unique inverse transform
};

void to_json(nlohmann::json& j, const BinUtility& bu);

void from_json(const nlohmann::json& j, BinUtility& bu);

} // namespace Acts
7 changes: 7 additions & 0 deletions Core/include/Acts/Utilities/BinningData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <utility>
#include <vector>

#include <nlohmann/json_fwd.hpp>

namespace Acts {

/// @class BinningData
Expand Down Expand Up @@ -553,4 +555,9 @@ class BinningData {
return sl.str();
}
};

void to_json(nlohmann::json& j, const BinningData& bd);

void from_json(const nlohmann::json& j, BinningData& bd);

} // namespace Acts
12 changes: 12 additions & 0 deletions Core/include/Acts/Utilities/BinningType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <type_traits>
#include <vector>

#include <nlohmann/json.hpp>

namespace Acts {

/// @enum BinningType, BinningOption & BinningAccess
Expand Down Expand Up @@ -45,6 +47,16 @@ enum class BinningValue : int {
binMag = 8
};

NLOHMANN_JSON_SERIALIZE_ENUM(BinningValue, {{BinningValue::binX, "binX"},
{BinningValue::binY, "binY"},
{BinningValue::binZ, "binZ"},
{BinningValue::binR, "binR"},
{BinningValue::binPhi, "binPhi"},
{BinningValue::binRPhi, "binRPhi"},
{BinningValue::binH, "binH"},
{BinningValue::binEta, "binEta"},
{BinningValue::binMag, "binMag"}})

/// Get all possible binning values
/// @return the binning values
const std::vector<BinningValue>& allBinningValues();
Expand Down
14 changes: 14 additions & 0 deletions Core/include/Acts/Utilities/RangeXD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <sstream>
#include <string>

#include <nlohmann/json.hpp>

namespace Acts {
/// @brief An orthogonal range in an arbitrary number of dimensions
///
Expand Down Expand Up @@ -635,4 +637,16 @@ template <typename Type,
template <typename, std::size_t> typename Vector = std::array>
using Range1D = RangeXD<1, Type, Vector>;

template <typename Type>
void to_json(nlohmann::json& j, const Range1D<Type>& r) {
j["min"] = r.min();
j["max"] = r.max();
}

template <typename Type>
void from_json(const nlohmann::json& j, Range1D<Type>& r) {
r.setMin(j.at("min").template get<Type>());
r.setMax(j.at("max").template get<Type>());
}

} // namespace Acts
48 changes: 48 additions & 0 deletions Core/src/Definitions/Algebra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.

#include "Acts/Definitions/Algebra.hpp"

#include <nlohmann/json.hpp>

void Acts::to_json(nlohmann::json& j, const Acts::Transform3& t) {
auto translation = t.translation();
if (translation != Acts::Vector3(0., 0., 0)) {
std::array<double, 3> tdata = {translation.x(), translation.y(),
translation.z()};
j["translation"] = tdata;
} else {
j["translation"] = nlohmann::json();
}

auto rotation = t.rotation();
if (rotation != Acts::RotationMatrix3::Identity()) {
std::array<double, 9> rdata = {
rotation(0, 0), rotation(0, 1), rotation(0, 2),
rotation(1, 0), rotation(1, 1), rotation(1, 2),
rotation(2, 0), rotation(2, 1), rotation(2, 2)};
j["rotation"] = rdata;
} else {
j["rotation"] = nlohmann::json();
}
}

void Acts::from_json(const nlohmann::json& j, Acts::Transform3& t) {
t = Acts::Transform3::Identity();
if (j.find("rotation") != j.end() && !j["rotation"].empty()) {
std::array<double, 9> rdata = j["rotation"];
Acts::RotationMatrix3 rot;
rot << rdata[0], rdata[1], rdata[2], rdata[3], rdata[4], rdata[5], rdata[6],
rdata[7], rdata[8];
t.prerotate(rot);
}
if (j.find("translation") != j.end() && !j["translation"].empty()) {
std::array<double, 3> tdata = j["translation"];
t.pretranslate(Acts::Vector3(tdata[0], tdata[1], tdata[2]));
}
}
5 changes: 4 additions & 1 deletion Core/src/Definitions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
target_sources(ActsCore PRIVATE Common.cpp Direction.cpp ParticleData.cpp)
target_sources(
ActsCore
PRIVATE Common.cpp Direction.cpp ParticleData.cpp Algebra.cpp
)
48 changes: 48 additions & 0 deletions Core/src/Geometry/Extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,51 @@ std::ostream& Acts::operator<<(std::ostream& sl, const Extent& rhs) {
sl << rhs.toString();
return sl;
}

void Acts::to_json(nlohmann::json& j, const Acts::Extent& e) {
{
nlohmann::json jrange;
const auto& xrange = e.range();
for (auto ibv : allBinningValues()) {
if (e.constrains(ibv)) {
jrange[binningValueName(ibv)] = xrange[toUnderlying(ibv)];
}
}
j["range"] = jrange;
}

{
nlohmann::json jenvelope;
const auto& envelope = e.envelope();
for (auto ibv : allBinningValues()) {
if (envelope[ibv] != zeroEnvelope) {
jenvelope[binningValueName(ibv)] =
Range1D<double>(envelope[ibv][0], envelope[ibv][1]);
}
}
if (!jenvelope.empty()) {
j["envelope"] = jenvelope;
}
}
}

void Acts::from_json(const nlohmann::json& j, Acts::Extent& e) {
const auto& jrange = j.at("range");

for (const auto& [key, value] : jrange.items()) {
BinningValue bval = binningValueFromName(key);
e.set(bval, value.at("min"), value.at("max"));
}

if (j.contains("envelope")) {
const auto& jenvelope = j.at("envelope");
ExtentEnvelope envelope;

for (const auto& [key, value] : jenvelope.items()) {
BinningValue bval = binningValueFromName(key);
envelope[bval] = {value["min"], value["max"]};
}

e.setEnvelope(envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include "Acts/Plugins/Json/UtilitiesJsonConverter.hpp"

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
#include "Acts/Utilities/BinningData.hpp"
#include "Acts/Utilities/BinningType.hpp"

#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>

void Acts::to_json(nlohmann::json& j, const Acts::BinningData& bd) {
// Common to all bin utilities
Expand Down Expand Up @@ -77,28 +66,3 @@ void Acts::from_json(const nlohmann::json& j, BinningData& bd) {
bd = BinningData(bOption, bValue, boundaries, std::move(subBinning));
}
}

void Acts::to_json(nlohmann::json& j, const BinUtility& bu) {
nlohmann::json jbindata;
for (const auto& bdata : bu.binningData()) {
jbindata.push_back(nlohmann::json(bdata));
}
j["binningdata"] = jbindata;
if (!bu.transform().isApprox(Transform3::Identity())) {
nlohmann::json jtrf = Transform3JsonConverter::toJson(bu.transform());
j["transform"] = jtrf;
}
}

void Acts::from_json(const nlohmann::json& j, Acts::BinUtility& bu) {
bu = Acts::BinUtility();
if (j.find("transform") != j.end() && !j["transform"].empty()) {
Acts::Transform3 trf = Transform3JsonConverter::fromJson(j["transform"]);
bu = Acts::BinUtility(trf);
}
for (const auto& jdata : j["binningdata"]) {
Acts::BinningData bd;
from_json(jdata, bd);
bu += Acts::BinUtility(bd);
}
}
Loading
Loading