Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 07:51:59

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