Back to home page

EIC code displayed by LXR

 
 

    


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

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/UtilitiesJsonConverter.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Plugins/Json/AlgebraJsonConverter.hpp"
0013 #include "Acts/Utilities/BinningData.hpp"
0014 #include "Acts/Utilities/BinningType.hpp"
0015 
0016 #include <algorithm>
0017 #include <memory>
0018 #include <string>
0019 #include <utility>
0020 #include <vector>
0021 
0022 void Acts::to_json(nlohmann::json& j, const Acts::BinningData& bd) {
0023   // Common to all bin utilities
0024   j["min"] = bd.min;
0025   j["max"] = bd.max;
0026   j["option"] = (bd.option == Acts::open ? "open" : "closed");
0027   j["value"] = bd.binvalue;
0028   int bins = bd.bins();
0029   // Write sub bin data if present
0030   if (bd.subBinningData != nullptr) {
0031     nlohmann::json subjson;
0032     to_json(subjson, *bd.subBinningData);
0033     j["subdata"] = subjson;
0034     j["subadditive"] = bd.subBinningAdditive;
0035     // this modifies the bins as bins() returns total number in general
0036     if (bd.subBinningAdditive) {
0037       bins -= static_cast<int>(subjson["bins"]) + 1;
0038     } else {
0039       bins /= static_cast<int>(subjson["bins"]);
0040     }
0041   }
0042   // Now distinguish between equidistant / arbitrary
0043   if (bd.type == Acts::equidistant) {
0044     j["type"] = "equidistant";
0045   } else if (bd.type == Acts::arbitrary) {
0046     j["type"] = "arbitrary";
0047     j["boundaries"] = bd.boundaries();
0048   }
0049   j["bins"] = bins;
0050 }
0051 
0052 void Acts::from_json(const nlohmann::json& j, BinningData& bd) {
0053   // Common to all bin utilities
0054   float min = j["min"];
0055   float max = j["max"];
0056   int bins = j["bins"];
0057 
0058   // Support legacy format with BinningValue instead of AxisDirection,
0059   // this will anyway disappear with the removal of BinUtility
0060   AxisDirection bValue = AxisDirection::AxisX;
0061   if (j["value"].get<std::string>().substr(0, 3) == "bin") {
0062     std::string bValueStr = j["value"];
0063     if (bValueStr == "binX") {
0064       bValue = AxisDirection::AxisX;
0065     } else if (bValueStr == "binY") {
0066       bValue = AxisDirection::AxisY;
0067     } else if (bValueStr == "binZ") {
0068       bValue = AxisDirection::AxisZ;
0069     } else if (bValueStr == "binR") {
0070       bValue = AxisDirection::AxisR;
0071     } else if (bValueStr == "binPhi") {
0072       bValue = AxisDirection::AxisPhi;
0073     } else if (bValueStr == "binRPhi") {
0074       bValue = AxisDirection::AxisRPhi;
0075     } else if (bValueStr == "binH") {
0076       bValue = AxisDirection::AxisTheta;
0077     } else if (bValueStr == "binEta") {
0078       bValue = AxisDirection::AxisEta;
0079     } else if (bValueStr == "binMag") {
0080       bValue = AxisDirection::AxisMag;
0081     } else {
0082       throw std::invalid_argument("Unknown binning value name: " + bValueStr);
0083     }
0084   } else {
0085     bValue = j["value"].get<AxisDirection>();
0086   }
0087 
0088   if (bins == 1 && !(j["type"] == "arbitrary")) {
0089     bd = BinningData(bValue, min, max);
0090     return;
0091   }
0092   Acts::BinningOption bOption = (j["option"] == "open") ? open : closed;
0093   Acts::BinningType bType =
0094       (j["type"] == "equidistant") ? equidistant : arbitrary;
0095 
0096   std::unique_ptr<BinningData> subBinning = nullptr;
0097   bool subBinningAdditive = false;
0098   if (j.find("subdata") != j.end()) {
0099     subBinningAdditive = j["subadditive"];
0100   }
0101 
0102   if (bType == equidistant) {
0103     bd = BinningData(bOption, bValue, bins, min, max, std::move(subBinning),
0104                      subBinningAdditive);
0105   } else {
0106     std::vector<float> boundaries = j["boundaries"];
0107     bd = BinningData(bOption, bValue, boundaries, std::move(subBinning));
0108   }
0109 }
0110 
0111 void Acts::to_json(nlohmann::json& j, const BinUtility& bu) {
0112   nlohmann::json jbindata;
0113   for (const auto& bdata : bu.binningData()) {
0114     jbindata.push_back(nlohmann::json(bdata));
0115   }
0116   j["binningdata"] = jbindata;
0117   if (!bu.transform().isApprox(Transform3::Identity())) {
0118     nlohmann::json jtrf = Transform3JsonConverter::toJson(bu.transform());
0119     j["transform"] = jtrf;
0120   }
0121 }
0122 
0123 void Acts::from_json(const nlohmann::json& j, Acts::BinUtility& bu) {
0124   bu = Acts::BinUtility();
0125   if (j.find("transform") != j.end() && !j["transform"].empty()) {
0126     Acts::Transform3 trf = Transform3JsonConverter::fromJson(j["transform"]);
0127     bu = Acts::BinUtility(trf);
0128   }
0129   for (const auto& jdata : j["binningdata"]) {
0130     Acts::BinningData bd;
0131     from_json(jdata, bd);
0132     bu += Acts::BinUtility(bd);
0133   }
0134 }