Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-27 08:33:45

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 "ActsPlugins/Json/AxisSpecJsonConverter.hpp"
0010 
0011 #include "Acts/Utilities/AxisDefinitions.hpp"
0012 #include "ActsPlugins/Json/GridJsonConverter.hpp"
0013 #include "ActsPlugins/Json/UtilitiesJsonConverter.hpp"
0014 
0015 #include <array>
0016 #include <stdexcept>
0017 #include <vector>
0018 
0019 nlohmann::json Acts::AxisSpecJsonConverter::toJson(const AxisSpec& axisSpec) {
0020   nlohmann::json j;
0021 
0022   std::optional<double> min;
0023   std::optional<double> max;
0024   if (axisSpec.isEquidistant()) {
0025     const auto& params = axisSpec.asEquidistant();
0026     j["type"] = "equidistant";
0027     j["bins"] = params.nBins;
0028     min = params.min;
0029     max = params.max;
0030   } else if (axisSpec.isDeferredVariable()) {
0031     j["type"] = "deferred-variable";
0032     j["normalized_boundaries"] = axisSpec.asDeferredVariable().normalizedEdges;
0033   } else {
0034     const auto& params = axisSpec.asVariable();
0035     j["type"] = "variable";
0036     j["boundaries"] = params.edges;
0037   }
0038 
0039   // Only the properties the spec fixes are written out
0040   if (min.has_value() && max.has_value()) {
0041     j["range"] = std::array<double, 2u>({*min, *max});
0042   }
0043   if (axisSpec.boundaryType().has_value()) {
0044     j["boundary_type"] = *axisSpec.boundaryType();
0045   }
0046   if (axisSpec.direction().has_value()) {
0047     j["direction"] = *axisSpec.direction();
0048   }
0049   return j;
0050 }
0051 
0052 Acts::AxisSpec Acts::AxisSpecJsonConverter::fromJson(const nlohmann::json& j) {
0053   std::optional<AxisBoundaryType> boundaryType;
0054   if (j.contains("boundary_type")) {
0055     boundaryType = j.at("boundary_type").get<AxisBoundaryType>();
0056   }
0057   std::optional<AxisDirection> direction;
0058   if (j.contains("direction")) {
0059     direction = j.at("direction").get<AxisDirection>();
0060   }
0061 
0062   std::string type = j.at("type").get<std::string>();
0063   if (type == "equidistant") {
0064     std::optional<double> min;
0065     std::optional<double> max;
0066     if (j.contains("range")) {
0067       std::array<double, 2u> range = j.at("range");
0068       min = range.at(0);
0069       max = range.at(1);
0070     }
0071     return AxisSpec::Equidistant(j.at("bins").get<std::size_t>(), min, max,
0072                                  boundaryType, direction);
0073   }
0074   if (type == "variable") {
0075     return AxisSpec::Variable(j.at("boundaries").get<std::vector<double>>(),
0076                               boundaryType, direction);
0077   }
0078   if (type == "deferred-variable") {
0079     return AxisSpec::DeferredVariable(
0080         j.at("normalized_boundaries").get<std::vector<double>>(), boundaryType,
0081         direction);
0082   }
0083   throw std::invalid_argument(
0084       "AxisSpecJsonConverter: unknown axis spec type '" + type + "'");
0085 }
0086 
0087 nlohmann::json Acts::MultiAxisSpecJsonConverter::toJson(
0088     const MultiAxisSpec& multiAxisSpec) {
0089   nlohmann::json j = nlohmann::json::array();
0090   for (const AxisSpec& axisSpec : multiAxisSpec.axisSpecs()) {
0091     j.push_back(AxisSpecJsonConverter::toJson(axisSpec));
0092   }
0093   return j;
0094 }
0095 
0096 Acts::MultiAxisSpec Acts::MultiAxisSpecJsonConverter::fromJson(
0097     const nlohmann::json& j) {
0098   std::vector<AxisSpec> axisSpecs;
0099   axisSpecs.reserve(j.size());
0100   for (const auto& jAxis : j) {
0101     axisSpecs.push_back(AxisSpecJsonConverter::fromJson(jAxis));
0102   }
0103   return MultiAxisSpec(std::move(axisSpecs));
0104 }