Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:18

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