Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DD4hepConversionHelpers.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DD4hepConversionHelpers.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
13 
14 #include <DD4hep/DetElement.h>
15 #include <DD4hep/DetFactoryHelper.h>
16 #include <DD4hep/Objects.h>
17 #include <DDRec/DetectorData.h>
18 
19 namespace Acts {
20 
27 template <typename T>
28 T getParam(const std::string& key, dd4hep::DetElement& elt) {
29  auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
30  if (params == nullptr) {
31  throw std::runtime_error{"Detector Element has no VariantParameters"};
32  }
33  return params->get<T>(key);
34 }
35 
39 inline dd4hep::rec::VariantParameters& getParams(dd4hep::DetElement& elt) {
40  auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
41  if (params == nullptr) {
42  throw std::runtime_error{"Detector Element has no VariantParameters"};
43  }
44  return *params;
45 }
46 
50 inline const dd4hep::rec::VariantParameters& getParams(
51  const dd4hep::DetElement& elt) {
52  const auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
53  if (params == nullptr) {
54  throw std::runtime_error{"Detector Element has no VariantParameters"};
55  }
56  return *params;
57 }
58 
67 template <typename T>
68 T getParamOr(const std::string& key, const dd4hep::DetElement& elt,
69  T alternative) {
70  auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
71  if (params == nullptr) {
72  return alternative;
73  }
74  return params->value_or<T>(key, alternative);
75 }
76 
82 inline bool hasParam(const std::string& key, dd4hep::DetElement& elt) {
83  auto* params = elt.extension<dd4hep::rec::VariantParameters>(false);
84  if (params == nullptr) {
85  return false;
86  }
87  return params->contains(key);
88 }
89 
93 inline bool hasParams(dd4hep::DetElement& elt) {
94  return elt.extension<dd4hep::rec::VariantParameters>(false) != nullptr;
95 }
96 
108 template <typename value_type>
109 value_type getAttrValueOr(const dd4hep::xml::Component& node,
110  const std::string& attrName,
111  const value_type& fallbackValue) {
112  if (node.hasAttr(dd4hep::xml::Strng_t(attrName.c_str()))) {
113  return node.attr<value_type>(attrName.c_str());
114  } else {
115  return fallbackValue;
116  }
117 }
118 
128 template <typename value_type>
129 std::vector<value_type> extractSeries(const dd4hep::DetElement& dd4hepElement,
130  const std::string& bname,
131  const value_type& unitConversion = 1) {
132  std::vector<value_type> series = {};
133 
134  int fallBack = 0;
135  int nVals = getParamOr<int>(bname + "_n", dd4hepElement, fallBack);
136  series.reserve(nVals);
137  for (auto ib = 0; ib < nVals; ++ib) {
138  auto val = unitConversion *
139  getParamOr<value_type>(bname + "_" + std::to_string(ib),
140  dd4hepElement, 0.);
141  series.push_back(val);
142  }
143  return series;
144 }
145 
153 inline Transform3 extractTransform(const dd4hep::DetElement& dd4hepElement,
154  const std::string& bname,
155  const ActsScalar unitConversion = 1.) {
156  Transform3 transform = Transform3::Identity();
157  ActsScalar x =
158  unitConversion * getParamOr<ActsScalar>(bname + "_x", dd4hepElement, 0.);
159  ActsScalar y =
160  unitConversion * getParamOr<ActsScalar>(bname + "_y", dd4hepElement, 0.);
161  ActsScalar z =
162  unitConversion * getParamOr<ActsScalar>(bname + "_z", dd4hepElement, 0.);
163  transform.pretranslate(Vector3(x, y, z));
164  return transform;
165 }
166 
167 } // namespace Acts