Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:19

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