File indexing completed on 2025-08-06 08:10:48
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "ActsExamples/Io/Json/JsonDigitizationConfig.hpp"
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/Plugins/Json/UtilitiesJsonConverter.hpp"
0014 #include "Acts/Utilities/BinningData.hpp"
0015 #include "ActsExamples/Digitization/Smearers.hpp"
0016 #include "ActsExamples/Framework/RandomNumbers.hpp"
0017 #include "ActsFatras/Digitization/UncorrelatedHitSmearer.hpp"
0018
0019 #include <cstddef>
0020 #include <fstream>
0021 #include <initializer_list>
0022 #include <stdexcept>
0023 #include <utility>
0024 #include <vector>
0025
0026 namespace ActsExamples {
0027 namespace {
0028 void to_json(nlohmann::json& j, const ActsFatras::SingleParameterSmearFunction<
0029 ActsExamples::RandomEngine>& f) {
0030
0031 auto gauss = f.target<const Digitization::Gauss>();
0032 if (gauss != nullptr) {
0033 j["type"] = "Gauss";
0034 j["mean"] = 0;
0035 j["stddev"] = gauss->sigma;
0036 return;
0037 }
0038
0039 auto gaussT = f.target<const Digitization::GaussTrunc>();
0040 if (gaussT != nullptr) {
0041 j["type"] = "GaussTrunc";
0042 j["mean"] = 0;
0043 j["stddev"] = gaussT->sigma;
0044 j["range"] = gaussT->range;
0045 return;
0046 }
0047
0048 auto gaussC = f.target<const Digitization::GaussClipped>();
0049 if (gaussC != nullptr) {
0050 j["type"] = "GaussClipped";
0051 j["mean"] = 0;
0052 j["stddev"] = gaussC->sigma;
0053 j["range"] = gaussC->range;
0054 j["max_attempts"] = gaussC->maxAttemps;
0055 return;
0056 }
0057
0058 auto uniform = f.target<const Digitization::Uniform>();
0059 if (uniform != nullptr) {
0060 j["type"] = "Uniform";
0061 j["bindata"] = nlohmann::json(uniform->binningData);
0062 return;
0063 }
0064
0065 auto digital = f.target<const Digitization::Digital>();
0066 if (digital != nullptr) {
0067 j["type"] = "Digitial";
0068 j["bindata"] = nlohmann::json(digital->binningData);
0069 return;
0070 }
0071
0072 auto exact = f.target<const Digitization::Digital>();
0073 if (exact != nullptr) {
0074 j["type"] = "Exact";
0075 return;
0076 }
0077
0078 throw std::runtime_error("Unable to serialize smearer");
0079 }
0080
0081 void from_json(
0082 const nlohmann::json& j,
0083 ActsFatras::SingleParameterSmearFunction<ActsExamples::RandomEngine>& f) {
0084 std::string sType = j["type"];
0085
0086 if (sType == "Gauss") {
0087 f = Digitization::Gauss(j["stddev"]);
0088 } else if (sType == "GaussTrunc") {
0089 Acts::ActsScalar sigma = j["stddev"];
0090 std::pair<Acts::ActsScalar, Acts::ActsScalar> range = j["range"];
0091 f = Digitization::GaussTrunc(sigma, range);
0092 } else if (sType == "GaussClipped") {
0093 Acts::ActsScalar sigma = j["stddev"];
0094 std::pair<Acts::ActsScalar, Acts::ActsScalar> range = j["range"];
0095 f = Digitization::GaussClipped(sigma, range);
0096 } else if (sType == "Uniform") {
0097 Acts::BinningData bd;
0098 from_json(j["bindata"], bd);
0099 f = Digitization::Uniform(std::move(bd));
0100 } else if (sType == "Digitial") {
0101 Acts::BinningData bd;
0102 from_json(j["bindata"], bd);
0103 f = Digitization::Digital(std::move(bd));
0104 } else if (sType == "Exact") {
0105 f = Digitization::Exact{};
0106 } else {
0107 throw std::invalid_argument("Unknown smearer type '" + sType + "'");
0108 }
0109 }
0110
0111 }
0112 }
0113
0114 void ActsExamples::to_json(nlohmann::json& j,
0115 const ActsExamples::ParameterSmearingConfig& psc) {
0116 j["index"] = psc.index;
0117 to_json(j, psc.smearFunction);
0118 }
0119
0120 void ActsExamples::from_json(const nlohmann::json& j,
0121 ActsExamples::ParameterSmearingConfig& psc) {
0122 psc.index = static_cast<Acts::BoundIndices>(j["index"]);
0123 from_json(j, psc.smearFunction);
0124 }
0125
0126 void ActsExamples::to_json(nlohmann::json& j,
0127 const ActsExamples::GeometricConfig& gdc) {
0128 std::vector<std::size_t> indices;
0129 for (const auto& idx : gdc.indices) {
0130 indices.push_back(static_cast<std::size_t>(idx));
0131 }
0132 j["indices"] = indices;
0133 j["segmentation"] = nlohmann::json(gdc.segmentation);
0134 j["thickness"] = gdc.thickness;
0135 j["threshold"] = gdc.threshold;
0136 j["digital"] = gdc.digital;
0137 to_json(j["charge-smearing"], gdc.chargeSmearer);
0138 }
0139
0140 void ActsExamples::from_json(const nlohmann::json& j,
0141 ActsExamples::GeometricConfig& gdc) {
0142 for (const auto& jidx : j["indices"]) {
0143 gdc.indices.push_back(static_cast<Acts::BoundIndices>(jidx));
0144 }
0145 from_json(j["segmentation"], gdc.segmentation);
0146 gdc.thickness = j["thickness"];
0147 gdc.threshold = j["threshold"];
0148 gdc.digital = j["digital"];
0149 from_json(j["charge-smearing"], gdc.chargeSmearer);
0150 }
0151
0152 void ActsExamples::to_json(nlohmann::json& j,
0153 const ActsExamples::SmearingConfig& sdc) {
0154 for (const auto& sc : sdc) {
0155 j.push_back(nlohmann::json(sc));
0156 }
0157 }
0158
0159 void ActsExamples::from_json(const nlohmann::json& j,
0160 ActsExamples::SmearingConfig& sdc) {
0161 for (const auto& jpsc : j) {
0162 ActsExamples::ParameterSmearingConfig psc;
0163 from_json(jpsc, psc);
0164 sdc.push_back(psc);
0165 }
0166 }
0167
0168 void ActsExamples::to_json(nlohmann::json& j,
0169 const ActsExamples::DigiComponentsConfig& dc) {
0170 if (!dc.geometricDigiConfig.indices.empty()) {
0171 j["geometric"] = nlohmann::json(dc.geometricDigiConfig);
0172 }
0173 if (!dc.smearingDigiConfig.empty()) {
0174 j["smearing"] = nlohmann::json(dc.smearingDigiConfig);
0175 }
0176 }
0177
0178 void ActsExamples::from_json(const nlohmann::json& j,
0179 ActsExamples::DigiComponentsConfig& dc) {
0180 if (j.find("geometric") != j.end()) {
0181 nlohmann::json jgdc = j["geometric"];
0182 from_json(jgdc, dc.geometricDigiConfig);
0183 }
0184 if (j.find("smearing") != j.end()) {
0185 nlohmann::json jsdc = j["smearing"];
0186 from_json(jsdc, dc.smearingDigiConfig);
0187 }
0188 }
0189
0190 Acts::GeometryHierarchyMap<ActsExamples::DigiComponentsConfig>
0191 ActsExamples::readDigiConfigFromJson(const std::string& path) {
0192 nlohmann::json djson;
0193 if (path.empty()) {
0194 return Acts::GeometryHierarchyMap<ActsExamples::DigiComponentsConfig>();
0195 }
0196 std::ifstream infile(path, std::ifstream::in | std::ifstream::binary);
0197
0198 infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0199 infile >> djson;
0200 return DigiConfigConverter("digitization-configuration").fromJson(djson);
0201 }
0202
0203 void ActsExamples::writeDigiConfigToJson(
0204 const Acts::GeometryHierarchyMap<DigiComponentsConfig>& cfg,
0205 const std::string& path) {
0206 std::ofstream outfile(path, std::ofstream::out | std::ofstream::binary);
0207
0208 outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0209 outfile << DigiConfigConverter("digitization-configuration")
0210 .toJson(cfg, nullptr)
0211 .dump(2);
0212 }