Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:19

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2024 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Local include(s).
0009 #include "write_digitization_config.hpp"
0010 
0011 // Acts include(s).
0012 #if __has_include(<ActsPlugins/Json/ActsJson.hpp>)
0013 #include <ActsPlugins/Json/ActsJson.hpp>
0014 #include <ActsPlugins/Json/GeometryHierarchyMapJsonConverter.hpp>
0015 #include <ActsPlugins/Json/UtilitiesJsonConverter.hpp>
0016 #else
0017 #include <Acts/Plugins/Json/ActsJson.hpp>
0018 #include <Acts/Plugins/Json/GeometryHierarchyMapJsonConverter.hpp>
0019 #include <Acts/Plugins/Json/UtilitiesJsonConverter.hpp>
0020 #endif
0021 
0022 // System include(s).
0023 #include <fstream>
0024 
0025 namespace traccc {
0026 
0027 nlohmann::json module_digi_config_to_json(
0028     const Acts::GeometryIdentifier& geoId,
0029     const module_digitization_config& cfg) {
0030   static const char* geometric = "geometric";
0031   static const char* segmentation = "segmentation";
0032   static const char* binningdata_key = "binningdata";
0033 
0034   static const char* bin_value_labels[] = {"binX", "binY"};
0035 
0036   nlohmann::json entry;
0037 
0038   // Write geometry identifier fields — only write non-zero ones
0039   // ie the top most value in geometry sructure
0040   if (geoId.volume() != 0)
0041     entry["volume"] = geoId.volume();
0042   if (geoId.layer() != 0)
0043     entry["layer"] = geoId.layer();
0044   if (geoId.sensitive() != 0)
0045     entry["sensitive"] = geoId.sensitive();
0046 
0047   nlohmann::json binning_array = nlohmann::json::array();
0048 
0049   for (std::size_t dim = 0; dim < cfg.bin_edges.size(); ++dim) {
0050     const auto& edges = cfg.bin_edges[dim];
0051     int nbins = static_cast<int>(edges.size()) - 1;
0052 
0053     nlohmann::json bindata;
0054     bindata["bins"] = nbins;
0055     bindata["option"] = "open";
0056     bindata["value"] = (dim < 2) ? bin_value_labels[dim] : "binX";
0057 
0058     if (!edges.empty()) {
0059       bindata["min"] = static_cast<float>(edges.front());
0060       bindata["max"] = static_cast<float>(edges.back());
0061     }
0062 
0063     // Correctly write equidistant vs irregular binning
0064     bool equidistant = true;
0065     if (nbins > 1) {
0066       float expected_pitch =
0067           (edges.back() - edges.front()) / static_cast<float>(nbins);
0068       for (std::size_t i = 1; i < edges.size(); ++i) {
0069         if (std::abs((edges[i] - edges[i - 1]) - expected_pitch) > 1e-5f) {
0070           equidistant = false;
0071           break;
0072         }
0073       }
0074     }
0075 
0076     if (equidistant) {
0077       bindata["type"] = "equidistant";
0078     } else {
0079       bindata["type"] = "variable";
0080       bindata["edges"] = edges;
0081     }
0082 
0083     binning_array.push_back(bindata);
0084   }
0085 
0086   entry["value"][geometric][segmentation][binningdata_key] = binning_array;
0087 
0088   return entry;
0089 }
0090 
0091 nlohmann::json to_json(const digitization_config& config) {
0092   nlohmann::json json;
0093 
0094   // Top-level header — must match what Acts GeometryHierarchyMap expects
0095   json["acts-geometry-hierarchy-map"] = {
0096       {"format-version", 0},
0097       {"value-identifier", "digitization-configuration"}};
0098 
0099   nlohmann::json entries = nlohmann::json::array();
0100 
0101   for (unsigned int i = 0; i < config.size(); i++) {
0102     entries.push_back(
0103         module_digi_config_to_json(config.idAt(i), config.valueAt(i)));
0104   }
0105 
0106   json["entries"] = entries;
0107   return json;
0108 }
0109 
0110 namespace io::json {
0111 
0112 void write_digitization_config(std::string_view filename,
0113                                const digitization_config& config) {
0114   // Construct the JSON object.
0115   nlohmann::json json = traccc::to_json(config);
0116 
0117   // Open the output file. Relying on exceptions for error handling.
0118   std::ofstream outfile(filename.data(), std::ofstream::binary);
0119   outfile.exceptions(std::ofstream::failbit | std::ofstream::badbit);
0120 
0121   // Write the JSON object to the file.
0122   outfile << json.dump(4);
0123 }
0124 
0125 }  // namespace io::json
0126 }  // namespace traccc