Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:15:18

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 "Acts/Plugins/Json/ProtoDetectorJsonConverter.hpp"
0010 
0011 #include "Acts/Detector/ProtoDetector.hpp"
0012 #include "Acts/Geometry/Extent.hpp"
0013 #include "Acts/Plugins/Json/ExtentJsonConverter.hpp"
0014 #include "Acts/Plugins/Json/SurfaceJsonConverter.hpp"
0015 #include "Acts/Surfaces/Surface.hpp"
0016 #include "Acts/Utilities/BinningData.hpp"
0017 
0018 #include <optional>
0019 #include <string>
0020 #include <vector>
0021 
0022 void Acts::to_json(nlohmann::json& j, const Acts::ProtoVolume& pv) {
0023   j["name"] = pv.name;
0024   j["extent"] = pv.extent;
0025 
0026   /// Helper m ethod to write binnings
0027   ///
0028   /// @param root the json root into which this is written
0029   /// @param binning the vector of binning data
0030   /// @param key the key for the root writing
0031   auto writeBinning = [&](nlohmann::json& root,
0032                           const std::vector<BinningData>& binning,
0033                           const std::string& key) -> void {
0034     nlohmann::json jbinning;
0035     for (const auto& bd : binning) {
0036       jbinning.push_back(bd);
0037     }
0038     root[key] = jbinning;
0039   };
0040 
0041   // The internal structure
0042   if (pv.internal.has_value()) {
0043     auto& its = pv.internal.value();
0044     nlohmann::json jinternal;
0045     if (its.layerType != Surface::SurfaceType::Other) {
0046       jinternal["layerType"] = its.layerType;
0047     }
0048     if (!its.surfaceBinning.empty()) {
0049       writeBinning(jinternal, its.surfaceBinning, "surfaceBinning");
0050     }
0051     j["internalStructure"] = jinternal;
0052   }
0053 
0054   // The container structure
0055   if (pv.container.has_value()) {
0056     auto& cts = pv.container.value();
0057     nlohmann::json jcontainer;
0058     nlohmann::json jconstituents;
0059     for (const auto& pvc : cts.constituentVolumes) {
0060       jconstituents.push_back(pvc);
0061     }
0062     jcontainer["constituents"] = jconstituents;
0063     writeBinning(jcontainer, cts.constituentBinning, "constituentBinning");
0064     jcontainer["layerContainer"] = cts.layerContainer;
0065     j["containerStructure"] = jcontainer;
0066   }
0067 }
0068 
0069 void Acts::from_json(const nlohmann::json& j, Acts::ProtoVolume& pv) {
0070   pv.name = j["name"];
0071   pv.extent = j["extent"];
0072 
0073   /// Helper method to read binnings
0074   ///
0075   /// @param root is the json root
0076   /// @param binning is the vector of binning data to be filled
0077   /// @param key is the lookup key
0078   auto readBinning = [&](const nlohmann::json& root,
0079                          std::vector<BinningData>& binning,
0080                          const std::string& key) -> void {
0081     // return if no surface binning in json
0082     if (root.find(key) == root.end() || root[key].is_null()) {
0083       return;
0084     }
0085 
0086     for (const auto& jbinning : root[key]) {
0087       binning.push_back(jbinning);
0088     }
0089   };
0090 
0091   // The internal structure
0092   if (j.find("internalStructure") != j.end() &&
0093       !j["internalStructure"].is_null()) {
0094     auto& jinternal = j["internalStructure"];
0095     Surface::SurfaceType layerType =
0096         static_cast<Surface::SurfaceType>(jinternal["layerType"]);
0097     std::vector<BinningData> surfaceBinning;
0098     readBinning(jinternal, surfaceBinning, "surfaceBinning");
0099     pv.internal = ProtoVolume::InternalStructure{layerType, surfaceBinning};
0100   }
0101 
0102   // The container structure
0103   if (j.find("containerStructure") != j.end() &&
0104       !j["containerStructure"].is_null()) {
0105     std::vector<ProtoVolume> constituentVolumes;
0106     auto& jcontainer = j["containerStructure"];
0107     for (const auto& jc : jcontainer["constituents"]) {
0108       constituentVolumes.push_back(jc);
0109     }
0110     std::vector<BinningData> constituentBinning;
0111     readBinning(jcontainer, constituentBinning, "constituentBinning");
0112     bool layerContainer = jcontainer["layerContainer"];
0113     pv.container = ProtoVolume::ContainerStructure{
0114         constituentVolumes, constituentBinning, layerContainer};
0115   }
0116 }
0117 
0118 void Acts::to_json(nlohmann::json& j, const Acts::ProtoDetector& pd) {
0119   j["name"] = pd.name;
0120   j["world"] = pd.worldVolume;
0121 }
0122 
0123 void Acts::from_json(const nlohmann::json& j, Acts::ProtoDetector& pd) {
0124   pd.name = j["name"];
0125   pd.worldVolume = j["world"];
0126 }