Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:19

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2021 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 http://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   auto bValue = j["value"].get<BinningValue>();
0058   if (bins == 1 && !(j["type"] == "arbitrary")) {
0059     bd = BinningData(bValue, min, max);
0060     return;
0061   }
0062   Acts::BinningOption bOption = (j["option"] == "open") ? open : closed;
0063   Acts::BinningType bType =
0064       (j["type"] == "equidistant") ? equidistant : arbitrary;
0065 
0066   std::unique_ptr<BinningData> subBinning = nullptr;
0067   bool subBinningAdditive = false;
0068   if (j.find("subdata") != j.end()) {
0069     subBinningAdditive = j["subadditive"];
0070   }
0071 
0072   if (bType == equidistant) {
0073     bd = BinningData(bOption, bValue, bins, min, max, std::move(subBinning),
0074                      subBinningAdditive);
0075   } else {
0076     std::vector<float> boundaries = j["boundaries"];
0077     bd = BinningData(bOption, bValue, boundaries, std::move(subBinning));
0078   }
0079 }
0080 
0081 void Acts::to_json(nlohmann::json& j, const BinUtility& bu) {
0082   nlohmann::json jbindata;
0083   for (const auto& bdata : bu.binningData()) {
0084     jbindata.push_back(nlohmann::json(bdata));
0085   }
0086   j["binningdata"] = jbindata;
0087   if (!bu.transform().isApprox(Transform3::Identity())) {
0088     nlohmann::json jtrf = Transform3JsonConverter::toJson(bu.transform());
0089     j["transform"] = jtrf;
0090   }
0091 }
0092 
0093 void Acts::from_json(const nlohmann::json& j, Acts::BinUtility& bu) {
0094   bu = Acts::BinUtility();
0095   if (j.find("transform") != j.end() && !j["transform"].empty()) {
0096     Acts::Transform3 trf = Transform3JsonConverter::fromJson(j["transform"]);
0097     bu = Acts::BinUtility(trf);
0098   }
0099   for (const auto& jdata : j["binningdata"]) {
0100     Acts::BinningData bd;
0101     from_json(jdata, bd);
0102     bu += Acts::BinUtility(bd);
0103   }
0104 }