Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 08:23:36

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/ProtoAxisJsonConverter.hpp"
0010 
0011 #include "Acts/Utilities/AxisDefinitions.hpp"
0012 #include "Acts/Utilities/Diagnostics.hpp"
0013 #include "ActsPlugins/Json/GridJsonConverter.hpp"
0014 #include "ActsPlugins/Json/UtilitiesJsonConverter.hpp"
0015 
0016 // Unlike the declarations, the definitions of the deprecated converters do not
0017 // carry the deprecation attribute themselves, so they need the suppression
0018 ACTS_PUSH_IGNORE_DEPRECATED()
0019 
0020 nlohmann::json Acts::ProtoAxisJsonConverter::toJson(const Acts::ProtoAxis& pa) {
0021   nlohmann::json j;
0022   j["axis"] = AxisJsonConverter::toJson(pa.getAxis());
0023   j["autorange"] = pa.isAutorange();
0024   return j;
0025 }
0026 
0027 Acts::ProtoAxis Acts::ProtoAxisJsonConverter::fromJson(
0028     const nlohmann::json& j) {
0029   auto axisBoundaryType =
0030       j.at("axis").at("boundary_type").get<Acts::AxisBoundaryType>();
0031   if (auto axisType = j.at("axis").at("type").get<Acts::AxisType>();
0032       axisType == AxisType::Equidistant) {
0033     auto nbins = j.at("axis").at("bins").get<std::size_t>();
0034     if (nbins == 0) {
0035       throw std::invalid_argument("Number of bins must be positive");
0036     }
0037 
0038     if (j.at("autorange").get<bool>()) {
0039       return ProtoAxis(axisBoundaryType, nbins);
0040     }
0041     auto min = j.at("axis").at("range").at(0).get<double>();
0042     auto max = j.at("axis").at("range").at(1).get<double>();
0043     if (min >= max) {
0044       throw std::invalid_argument("Invalid range: min must be less than max");
0045     }
0046     return ProtoAxis(axisBoundaryType, min, max, nbins);
0047   }
0048   auto binEdges = j.at("axis").at("boundaries").get<std::vector<double>>();
0049   if (binEdges.size() < 2) {
0050     throw std::invalid_argument("At least two bin edges required");
0051   }
0052   if (!std::ranges::is_sorted(binEdges)) {
0053     throw std::invalid_argument("Bin edges must be sorted in ascending order");
0054   }
0055   return ProtoAxis(axisBoundaryType, binEdges);
0056 }
0057 
0058 ACTS_POP_IGNORE_DEPRECATED()