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) 2022-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 "read_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 traccc::digitization_config read_digitization_config(
0028     const nlohmann::json& json) {
0029   traccc::digitization_config result;
0030 
0031   static const char* entries_key = "entries";
0032   static const char* binningdata_key = "binningdata";
0033   static const char* geometric = "geometric";
0034   static const char* segmentation = "segmentation";
0035 
0036   std::vector<traccc::digitization_config::InputElement> elements;
0037 
0038   for (const auto& entry : json[entries_key]) {
0039     Acts::GeometryIdentifier geoId;
0040     Acts::GeometryIdentifier::Value null(0u);
0041     geoId = geoId.withVolume(entry.value("volume", null))
0042                 .withLayer(entry.value("layer", null))
0043                 .withSensitive(entry.value("sensitive", null));
0044 
0045     const auto& json_val = entry["value"];
0046     const auto& json_geom = json_val[geometric];
0047     const auto& json_segm = json_geom[segmentation];
0048     std::vector<std::vector<float>> bin_edges;
0049     unsigned char dimensions = 2;
0050     for (const auto& bindata : json_segm[binningdata_key]) {
0051       std::vector<float> bins;
0052 
0053       if (bindata["bins"].get<int>() == 1) {
0054         dimensions = 1;
0055         if (bindata["type"].get<std::string>() == "equidistant") {
0056           float pitch =
0057               (bindata["max"].get<float>() - bindata["min"].get<float>()) /
0058               bindata["bins"].get<float>();
0059           for (int i = 0; i <= bindata["bins"].get<int>(); ++i) {
0060             bins.push_back(bindata["min"].get<float>() +
0061                            static_cast<float>(i) * pitch);
0062           }
0063         } else {
0064           for (const auto& edge : bindata["edges"]) {
0065             bins.push_back(edge.get<float>());
0066           }
0067         }
0068       } else {
0069         if (bindata["type"].get<std::string>() == "equidistant") {
0070           float pitch =
0071               (bindata["max"].get<float>() - bindata["min"].get<float>()) /
0072               bindata["bins"].get<float>();
0073           for (int i = 0; i <= bindata["bins"].get<int>(); ++i) {
0074             bins.push_back(bindata["min"].get<float>() +
0075                            static_cast<float>(i) * pitch);
0076           }
0077         } else {
0078           for (const auto& edge : bindata["edges"]) {
0079             bins.push_back(edge.get<float>());
0080           }
0081         }
0082       }
0083 
0084       bin_edges.push_back(bins);
0085     }
0086 
0087     elements.push_back({geoId, {bin_edges, dimensions}});
0088   }
0089 
0090   return traccc::digitization_config(std::move(elements));
0091 }
0092 
0093 namespace io::json {
0094 
0095 digitization_config read_digitization_config(std::string_view filename) {
0096   // Open the input file. Relying on exceptions for the error handling.
0097   std::ifstream infile(filename.data(), std::ifstream::binary);
0098   infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
0099 
0100   // Read the contents of the file into a JSON object.
0101   nlohmann::json json;
0102   infile >> json;
0103 
0104   // Construct the object from the JSON configuration.
0105   return traccc::read_digitization_config(json);
0106 }
0107 
0108 }  // namespace io::json
0109 }  // namespace traccc