Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UtilitiesJsonConverter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file UtilitiesJsonConverter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
15 
16 #include <algorithm>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 void Acts::to_json(nlohmann::json& j, const Acts::BinningData& bd) {
23  // Common to all bin utilities
24  j["min"] = bd.min;
25  j["max"] = bd.max;
26  j["option"] = (bd.option == Acts::open ? "open" : "closed");
27  j["value"] = bd.binvalue;
28  int bins = bd.bins();
29  // Write sub bin data if present
30  if (bd.subBinningData != nullptr) {
31  nlohmann::json subjson;
32  to_json(subjson, *bd.subBinningData);
33  j["subdata"] = subjson;
34  j["subadditive"] = bd.subBinningAdditive;
35  // this modifies the bins as bins() returns total number in general
36  if (bd.subBinningAdditive) {
37  bins -= static_cast<int>(subjson["bins"]) + 1;
38  } else {
39  bins /= static_cast<int>(subjson["bins"]);
40  }
41  }
42  // Now distinguish between equidistant / arbitrary
43  if (bd.type == Acts::equidistant) {
44  j["type"] = "equidistant";
45  } else if (bd.type == Acts::arbitrary) {
46  j["type"] = "arbitrary";
47  j["boundaries"] = bd.boundaries();
48  }
49  j["bins"] = bins;
50 }
51 
52 void Acts::from_json(const nlohmann::json& j, BinningData& bd) {
53  // Common to all bin utilities
54  float min = j["min"];
55  float max = j["max"];
56  int bins = j["bins"];
57  std::string valueName = j["value"];
58  auto bValue = j["value"].get<BinningValue>();
59  if (bins == 1 and not(j["type"] == "arbitrary")) {
60  bd = BinningData(bValue, min, max);
61  return;
62  }
63  Acts::BinningOption bOption = (j["option"] == "open") ? open : closed;
64  Acts::BinningType bType =
65  (j["type"] == "equidistant") ? equidistant : arbitrary;
66 
67  std::unique_ptr<BinningData> subBinning = nullptr;
68  bool subBinningAdditive = false;
69  if (j.find("subdata") != j.end()) {
70  subBinningAdditive = j["subadditive"];
71  }
72 
73  if (bType == equidistant) {
74  bd = BinningData(bOption, bValue, bins, min, max, std::move(subBinning),
75  subBinningAdditive);
76  } else {
77  std::vector<float> boundaries = j["boundaries"];
78  bd = BinningData(bOption, bValue, boundaries, std::move(subBinning));
79  }
80 }
81 
82 void Acts::to_json(nlohmann::json& j, const BinUtility& bu) {
83  nlohmann::json jbindata;
84  for (const auto& bdata : bu.binningData()) {
85  jbindata.push_back(nlohmann::json(bdata));
86  }
87  j["binningdata"] = jbindata;
88  if (not bu.transform().isApprox(Transform3::Identity())) {
89  nlohmann::json jtrf = Transform3JsonConverter::toJson(bu.transform());
90  j["transform"] = jtrf;
91  }
92 }
93 
94 void Acts::from_json(const nlohmann::json& j, Acts::BinUtility& bu) {
95  bu = Acts::BinUtility();
96  if (j.find("transform") != j.end() and not j["transform"].empty()) {
98  bu = Acts::BinUtility(trf);
99  }
100  for (const auto& jdata : j["binningdata"]) {
102  from_json(jdata, bd);
103  bu += Acts::BinUtility(bd);
104  }
105 }