Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-25 07:56:34

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 #pragma once
0010 
0011 #include "Acts/Navigation/DetectorVolumeFinders.hpp"
0012 #include "Acts/Navigation/NavigationDelegates.hpp"
0013 #include "Acts/Navigation/PortalNavigation.hpp"
0014 #include "Acts/Utilities/AxisDefinitions.hpp"
0015 #include "Acts/Utilities/Grid.hpp"
0016 #include "ActsPlugins/Json/DetrayJsonHelper.hpp"
0017 #include "ActsPlugins/Json/IndexedGridJsonHelper.hpp"
0018 
0019 #include <vector>
0020 
0021 namespace Acts::DetectorVolumeFinderJsonConverter {
0022 
0023 /// @brief Convert the single delegate if it is of the type of the reference
0024 ///
0025 /// @note It will do nothing if the type does not match
0026 ///
0027 /// @param jIndexedVolumes the json object to be filled
0028 /// @param delegate the delegate to be translated
0029 /// @param detray indicates if detray json format is to be written out
0030 /// @param refInstance is a reference instance of potential type casting
0031 template <typename instance_type>
0032 void convert(nlohmann::json& jIndexedVolumes,
0033              const Experimental::ExternalNavigationDelegate& delegate,
0034              bool detray, [[maybe_unused]] const instance_type& refInstance) {
0035   using GridType = typename instance_type::template grid_type<std::size_t>;
0036   // Defining a Delegate type
0037   using DelegateType = Experimental::IndexedGridNavigation<
0038       Experimental::IExternalNavigation, GridType,
0039       Experimental::IndexedDetectorVolumeExtractor,
0040       Experimental::DetectorVolumeFiller>;
0041   // Get the instance
0042   const auto* instance = delegate.instance();
0043   auto castedDelegate = dynamic_cast<const DelegateType*>(instance);
0044   if (castedDelegate != nullptr) {
0045     jIndexedVolumes = IndexedGridJsonHelper::convertImpl<DelegateType>(
0046         *castedDelegate, detray, false);
0047     if (detray) {
0048       jIndexedVolumes["volume_link"] = 1;
0049       nlohmann::json jAccLink;
0050       jAccLink["type"] =
0051           DetrayJsonHelper::accelerationLink(castedDelegate->casts);
0052       jAccLink["index"] = 0;
0053       jIndexedVolumes["acc_link"] = jAccLink;
0054     } else {
0055       jIndexedVolumes["type"] = "IndexedVolumes";
0056     }
0057   }
0058 }
0059 
0060 /// @brief Unrolling function for catching the right instance
0061 ///
0062 /// @param jIndexedVolumes the json object to be filled
0063 /// @param delegate the delegate to be translated
0064 /// @param detray indicate if this is a detray json to be written out
0065 template <typename... Args>
0066 void unrollConvert(nlohmann::json& jIndexedVolumes,
0067                    const Experimental::ExternalNavigationDelegate& delegate,
0068                    bool detray, TypeList<Args...> /*unused*/) {
0069   (convert(jIndexedVolumes, delegate, detray, Args{}), ...);
0070 }
0071 
0072 /// Convert a volume finder
0073 ///
0074 /// @param delegate the delegate to be translated
0075 /// @param detray indicate if this is a detray json to be written out
0076 ///
0077 /// @note this is the entry point of the conversion, i.e. top of the
0078 /// unrolling loop
0079 ///
0080 /// @note detray json format can not be read back in with Acts
0081 ///
0082 /// @return a json object
0083 static inline nlohmann::json toJson(
0084     const Experimental::ExternalNavigationDelegate& delegate,
0085     bool detray = false) {
0086   // Convert if dynamic cast happens to work
0087   nlohmann::json jIndexedVolumes;
0088   unrollConvert(jIndexedVolumes, delegate, detray,
0089                 GridAxisGenerators::PossibleAxes{});
0090   // Return the newly filled ones
0091   return jIndexedVolumes;
0092 }
0093 
0094 /// @brief Convert to a delegate from json
0095 ///
0096 /// @param jVolumeFinder the json file to read from
0097 ///
0098 /// @return the connected navigation delegate
0099 Experimental::ExternalNavigationDelegate fromJson(
0100     const nlohmann::json& jVolumeFinder);
0101 
0102 }  // namespace Acts::DetectorVolumeFinderJsonConverter