Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:17:02

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 "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   // Gauss:
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   // Truncated gauss:
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   // Clipped gauss:
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   // Uniform
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   // Digital
0065   auto digital = f.target<const Digitization::Digital>();
0066   if (digital != nullptr) {
0067     j["type"] = "Digital";
0068     j["bindata"] = nlohmann::json(digital->binningData);
0069     return;
0070   }
0071   // Exact
0072   auto exact = f.target<const Digitization::Exact>();
0073   if (exact != nullptr) {
0074     j["type"] = "Exact";
0075     j["stddev"] = exact->sigma;
0076     return;
0077   }
0078 
0079   throw std::runtime_error("Unable to serialize smearer");
0080 }
0081 
0082 void from_json(
0083     const nlohmann::json& j,
0084     ActsFatras::SingleParameterSmearFunction<ActsExamples::RandomEngine>& f) {
0085   std::string sType = j["type"];
0086 
0087   if (sType == "Gauss") {
0088     f = Digitization::Gauss(j["stddev"]);
0089   } else if (sType == "GaussTrunc") {
0090     double sigma = j["stddev"];
0091     std::pair<double, double> range = j["range"];
0092     f = Digitization::GaussTrunc(sigma, range);
0093   } else if (sType == "GaussClipped") {
0094     double sigma = j["stddev"];
0095     std::pair<double, double> range = j["range"];
0096     f = Digitization::GaussClipped(sigma, range);
0097   } else if (sType == "Uniform") {
0098     Acts::BinningData bd;
0099     from_json(j["bindata"], bd);
0100     f = Digitization::Uniform(bd);
0101   } else if (sType == "Digital") {
0102     Acts::BinningData bd;
0103     from_json(j["bindata"], bd);
0104     f = Digitization::Digital(bd);
0105   } else if (sType == "Exact") {
0106     f = Digitization::Exact(j["stddev"]);
0107   } else {
0108     throw std::invalid_argument("Unknown smearer type '" + sType + "'");
0109   }
0110 }
0111 
0112 }  // namespace
0113 }  // namespace ActsExamples
0114 
0115 void ActsExamples::to_json(nlohmann::json& j,
0116                            const ActsExamples::ParameterSmearingConfig& psc) {
0117   j["index"] = psc.index;
0118   j["forcePositiveValues"] = psc.forcePositiveValues;
0119   to_json(j, psc.smearFunction);
0120 }
0121 
0122 void ActsExamples::from_json(const nlohmann::json& j,
0123                              ActsExamples::ParameterSmearingConfig& psc) {
0124   psc.index = static_cast<Acts::BoundIndices>(j["index"]);
0125   if (j.find("forcePositiveValues") != j.end()) {
0126     psc.forcePositiveValues = j["forcePositiveValues"];
0127   }
0128   from_json(j, psc.smearFunction);
0129 }
0130 
0131 void ActsExamples::to_json(nlohmann::json& j,
0132                            const ActsExamples::GeometricConfig& gdc) {
0133   std::vector<std::size_t> indices;
0134   for (const auto& idx : gdc.indices) {
0135     indices.push_back(static_cast<std::size_t>(idx));
0136   }
0137   j["indices"] = indices;
0138   j["segmentation"] = nlohmann::json(gdc.segmentation);
0139   j["thickness"] = gdc.thickness;
0140   j["threshold"] = gdc.threshold;
0141   j["digital"] = gdc.digital;
0142   if (j.find("charge-smearing") != j.end()) {
0143     to_json(j["charge-smearing"], gdc.chargeSmearer);
0144   }
0145 }
0146 
0147 void ActsExamples::from_json(const nlohmann::json& j,
0148                              ActsExamples::GeometricConfig& gdc) {
0149   for (const auto& jidx : j["indices"]) {
0150     gdc.indices.push_back(static_cast<Acts::BoundIndices>(jidx));
0151   }
0152   from_json(j["segmentation"], gdc.segmentation);
0153   gdc.thickness = j["thickness"];
0154   gdc.threshold = j["threshold"];
0155   gdc.digital = j["digital"];
0156   if (j.find("variances") != j.end()) {
0157     /// Read the variances from the json file
0158     auto jvariances = j["variances"];
0159     for (const auto& jvar : jvariances) {
0160       auto idx =
0161           static_cast<Acts::BoundIndices>(jvar["index"].get<std::size_t>());
0162       auto vars = jvar["rms"].get<std::vector<double>>();
0163       gdc.varianceMap[idx] = vars;
0164     }
0165   }
0166   if (j.find("charge-smearing") != j.end()) {
0167     from_json(j["charge-smearing"], gdc.chargeSmearer);
0168   }
0169 }
0170 
0171 void ActsExamples::to_json(nlohmann::json& j,
0172                            const ActsExamples::SmearingConfig& sdc) {
0173   for (const auto& sc : sdc) {
0174     j.push_back(nlohmann::json(sc));
0175   }
0176 }
0177 
0178 void ActsExamples::from_json(const nlohmann::json& j,
0179                              ActsExamples::SmearingConfig& sdc) {
0180   for (const auto& jpsc : j) {
0181     ActsExamples::ParameterSmearingConfig psc;
0182     from_json(jpsc, psc);
0183     sdc.push_back(psc);
0184   }
0185 }
0186 
0187 void ActsExamples::to_json(nlohmann::json& j,
0188                            const ActsExamples::DigiComponentsConfig& dc) {
0189   if (!dc.geometricDigiConfig.indices.empty()) {
0190     j["geometric"] = nlohmann::json(dc.geometricDigiConfig);
0191   }
0192   if (!dc.smearingDigiConfig.empty()) {
0193     j["smearing"] = nlohmann::json(dc.smearingDigiConfig);
0194   }
0195 }
0196 
0197 void ActsExamples::from_json(const nlohmann::json& j,
0198                              ActsExamples::DigiComponentsConfig& dc) {
0199   if (j.find("geometric") != j.end()) {
0200     nlohmann::json jgdc = j["geometric"];
0201     from_json(jgdc, dc.geometricDigiConfig);
0202   }
0203   if (j.find("smearing") != j.end()) {
0204     nlohmann::json jsdc = j["smearing"];
0205     from_json(jsdc, dc.smearingDigiConfig);
0206   }
0207 }
0208 
0209 Acts::GeometryHierarchyMap<ActsExamples::DigiComponentsConfig>
0210 ActsExamples::readDigiConfigFromJson(const std::string& path) {
0211   nlohmann::json djson;
0212   if (path.empty()) {
0213     return Acts::GeometryHierarchyMap<ActsExamples::DigiComponentsConfig>();
0214   }
0215   std::ifstream infile(path, std::ifstream::in | std::ifstream::binary);
0216   // rely on exception for error handling
0217   infile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0218   infile >> djson;
0219   return DigiConfigConverter("digitization-configuration").fromJson(djson);
0220 }
0221 
0222 void ActsExamples::writeDigiConfigToJson(
0223     const Acts::GeometryHierarchyMap<DigiComponentsConfig>& cfg,
0224     const std::string& path) {
0225   std::ofstream outfile(path, std::ofstream::out | std::ofstream::binary);
0226   // rely on exception for error handling
0227   outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0228   outfile << DigiConfigConverter("digitization-configuration")
0229                  .toJson(cfg, nullptr)
0230                  .dump(2);
0231 }