DDCompactView

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#ifndef DetectorDescription_DDCMS_DDCompactView_h
#define DetectorDescription_DDCMS_DDCompactView_h

// -*- C++ -*-
//
// Package:    DetectorDescription/Core
// Class:      DDCompactView
//
/**\class DDCompactView

 Description: DD Compact View Facade

 Implementation:
     The DDCompactView facade serves as a launching point for a broader
     refactor of monolithic or tightly-coupled systems in favor of more
     loosely-coupled code.
*/
//
// Original Author:  Ianna Osborne
//         Created:  Wed, 22 May 2019 12:51:22 GMT
//
//

#include "DetectorDescription/DDCMS/interface/DDDetector.h"
#include <DD4hep/SpecParRegistry.h>

namespace cms {
  using DDSpecParRegistry = dd4hep::SpecParRegistry;
  using DDSpecParRefs = dd4hep::SpecParRefs;

  class DDCompactView {
  public:
    DDCompactView(const cms::DDDetector& det) : m_det(det) {}
    const cms::DDDetector* detector() const { return &m_det; }
    DDSpecParRegistry const& specpars() const { return m_det.specpars(); }
    template <typename T>
    std::vector<T> getVector(const std::string&) const;

    template <typename T>
    T const& get(const std::string&) const;
    template <typename T>
    T const& get(const std::string&, const std::string&) const;

  private:
    const cms::DDDetector& m_det;
  };

  /* Helper: For a given node, get the values associated to a given parameter, from the XMLs SpecPar sections.
 * NB: The same parameter can appear several times WITHIN the same SpecPar section (hence, we have a std::vector).
 * WARNING: This stops at the first relevant SpecPar section encountered.
 * Hence, if A GIVEN NODE HAS SEVERAL SPECPAR XML SECTIONS RE-DEFINING THE SAME PARAMETER,
 * only the first XML SpecPar block will be considered.
 */
  template <typename T>
  std::vector<T> getAllParameterValuesFromSpecParSections(const cms::DDSpecParRegistry& allSpecParSections,
                                                          const std::string& nodePath,
                                                          const std::string& parameterName) {
    cms::DDSpecParRefs filteredSpecParSections;
    allSpecParSections.filter(filteredSpecParSections, parameterName);
    for (const auto& mySpecParSection : filteredSpecParSections) {
      if (mySpecParSection.second->hasPath(nodePath)) {
        return mySpecParSection.second->value<std::vector<T>>(parameterName);
      }
    }

    return std::vector<T>();
  }

  /* Helper: For a given node, get the value associated to a given parameter, from the XMLs SpecPar sections.
 * This is the parameterValueIndex-th value (within a XML SpecPar block.) of the desired parameter. 
 */
  template <typename T>
  T getParameterValueFromSpecParSections(const cms::DDSpecParRegistry& allSpecParSections,
                                         const std::string& nodePath,
                                         const std::string& parameterName,
                                         const unsigned int parameterValueIndex) {
    const std::vector<T>& allParameterValues =
        getAllParameterValuesFromSpecParSections<T>(allSpecParSections, nodePath, parameterName);
    if (parameterValueIndex < allParameterValues.size()) {
      return allParameterValues.at(parameterValueIndex);
    }
    return T();
  }

}  // namespace cms

#endif