Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:41

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2017-2018 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 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Common.hpp"
0013 
0014 #include <DD4hep/DetElement.h>
0015 #include <DD4hep/DetFactoryHelper.h>
0016 #include <DD4hep/Objects.h>
0017 #include <DDRec/DetectorData.h>
0018 
0019 namespace Acts {
0020 
0021 /// Helper function to extract a parameter value from a dd4hep detector element
0022 /// from VariantParameters
0023 /// @tparam T The value type
0024 /// @param key The key of the value to extract
0025 /// @param elt The detector element instance
0026 /// @return A copy of the value contained in the params instance
0027 template <typename T>
0028 T getParam(const std::string& key, dd4hep::DetElement& elt) {
0029   auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
0030   if (params == nullptr) {
0031     throw std::runtime_error{"Detector Element has no VariantParameters"};
0032   }
0033   return params->get<T>(key);
0034 }
0035 
0036 /// Helper function to extract a VariantParameters instance
0037 /// @param elt The detector element instance
0038 /// @return The VariantParameters instance
0039 inline dd4hep::rec::VariantParameters& getParams(dd4hep::DetElement& elt) {
0040   auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
0041   if (params == nullptr) {
0042     throw std::runtime_error{"Detector Element has no VariantParameters"};
0043   }
0044   return *params;
0045 }
0046 
0047 /// Helper function to extract a VariantParameters instance, const version
0048 /// @param elt The detector element instance
0049 /// @return The VariantParameters instance
0050 inline const dd4hep::rec::VariantParameters& getParams(
0051     const dd4hep::DetElement& elt) {
0052   const auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
0053   if (params == nullptr) {
0054     throw std::runtime_error{"Detector Element has no VariantParameters"};
0055   }
0056   return *params;
0057 }
0058 
0059 /// Get a parameter value or an alternative value if either the
0060 /// VariantParameters extension isn't set, or it doesn't contain the demanded
0061 /// key
0062 /// @tparam T The value type
0063 /// @param key The key of the value to extract
0064 /// @param elt The detector element instance
0065 /// @param alternative The value to return if no params are set of the key doesn't exist
0066 /// @return The value behind key, or @p alternative
0067 template <typename T>
0068 T getParamOr(const std::string& key, const dd4hep::DetElement& elt,
0069              T alternative) {
0070   auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
0071   if (params == nullptr) {
0072     return alternative;
0073   }
0074   return params->value_or<T>(key, alternative);
0075 }
0076 
0077 /// Check if a detector element has a key set in its VariantParameters
0078 /// @param key The key to check existence for
0079 /// @param elt The detector element instance
0080 /// @return True if the element has VariantParameters and the key exists, false if
0081 ///         either of these is not true
0082 inline bool hasParam(const std::string& key, dd4hep::DetElement& elt) {
0083   auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
0084   if (params == nullptr) {
0085     return false;
0086   }
0087   return params->contains(key);
0088 }
0089 
0090 /// Check if a detector element has VariantParameters set
0091 /// @param elt The detector element instance
0092 /// @return True if the VariantParameters exist, false if not
0093 inline bool hasParams(dd4hep::DetElement& elt) {
0094   return elt.extension<dd4hep::rec::VariantParameters>(false) != nullptr;
0095 }
0096 
0097 /// @brief Helper method to get an attribute with fallback
0098 ///
0099 /// @note the fallback value has to be provided
0100 ///
0101 /// @tparam value_type the primitive type allowed by variant parameters
0102 ///
0103 /// @param node the node object from DD4hep
0104 /// @param attrName the name of the attribute that is checked
0105 /// @param fallbackValue the fallbackValue
0106 ///
0107 /// @return either the gathered attribute or the fallback
0108 template <typename value_type>
0109 value_type getAttrValueOr(const dd4hep::xml::Component& node,
0110                           const std::string& attrName,
0111                           const value_type& fallbackValue) {
0112   if (node.hasAttr(dd4hep::xml::Strng_t(attrName.c_str()))) {
0113     return node.attr<value_type>(attrName.c_str());
0114   } else {
0115     return fallbackValue;
0116   }
0117 }
0118 
0119 /// @brief A simple helper function to extract a series
0120 ///
0121 /// @tparam value_type the primitive type allowed by variant parameters
0122 ///
0123 /// @param dd4hepElement the detector element with associated variant parameters
0124 /// @param bname The base name attribute of the variant parameter pack
0125 /// @param unitConversion is a conversion factor DD4hep -> ACTS
0126 ///
0127 /// @return the extracted series as a vector
0128 template <typename value_type>
0129 std::vector<value_type> extractSeries(const dd4hep::DetElement& dd4hepElement,
0130                                       const std::string& bname,
0131                                       const value_type& unitConversion = 1) {
0132   std::vector<value_type> series = {};
0133 
0134   int fallBack = 0;
0135   int nVals = getParamOr<int>(bname + "_n", dd4hepElement, fallBack);
0136   series.reserve(nVals);
0137   for (auto ib = 0; ib < nVals; ++ib) {
0138     auto val = unitConversion *
0139                getParamOr<value_type>(bname + "_" + std::to_string(ib),
0140                                       dd4hepElement, 0.);
0141     series.push_back(val);
0142   }
0143   return series;
0144 }
0145 
0146 /// @brief A simple helper function to extract a transform
0147 ///
0148 /// @param dd4hepElement the detector element with associated variant parameters
0149 /// @param bname The base name attribute of the variant parameter pack
0150 /// @param unitConversion is a conversion factor DD4hep -> ACTS
0151 ///
0152 /// @return a transform extracted from parameters
0153 inline Transform3 extractTransform(const dd4hep::DetElement& dd4hepElement,
0154                                    const std::string& bname,
0155                                    const ActsScalar unitConversion = 1.) {
0156   Transform3 transform = Transform3::Identity();
0157   ActsScalar x =
0158       unitConversion * getParamOr<ActsScalar>(bname + "_x", dd4hepElement, 0.);
0159   ActsScalar y =
0160       unitConversion * getParamOr<ActsScalar>(bname + "_y", dd4hepElement, 0.);
0161   ActsScalar z =
0162       unitConversion * getParamOr<ActsScalar>(bname + "_z", dd4hepElement, 0.);
0163   transform.pretranslate(Vector3(x, y, z));
0164   return transform;
0165 }
0166 
0167 }  // namespace Acts