Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:05:27

0001 #ifndef DetectorDescription_DDCMS_DDCompactView_h
0002 #define DetectorDescription_DDCMS_DDCompactView_h
0003 
0004 // -*- C++ -*-
0005 //
0006 // Package:    DetectorDescription/Core
0007 // Class:      DDCompactView
0008 //
0009 /**\class DDCompactView
0010 
0011  Description: DD Compact View Facade
0012 
0013  Implementation:
0014      The DDCompactView facade serves as a launching point for a broader
0015      refactor of monolithic or tightly-coupled systems in favor of more
0016      loosely-coupled code.
0017 */
0018 //
0019 // Original Author:  Ianna Osborne
0020 //         Created:  Wed, 22 May 2019 12:51:22 GMT
0021 //
0022 //
0023 
0024 #include "DetectorDescription/DDCMS/interface/DDDetector.h"
0025 #include <DD4hep/SpecParRegistry.h>
0026 
0027 namespace cms {
0028   using DDSpecParRegistry = dd4hep::SpecParRegistry;
0029   using DDSpecParRefs = dd4hep::SpecParRefs;
0030 
0031   class DDCompactView {
0032   public:
0033     DDCompactView(const cms::DDDetector& det) : m_det(det) {}
0034     const cms::DDDetector* detector() const { return &m_det; }
0035     DDSpecParRegistry const& specpars() const { return m_det.specpars(); }
0036     template <typename T>
0037     std::vector<T> getVector(const std::string&) const;
0038 
0039     template <typename T>
0040     T const& get(const std::string&) const;
0041     template <typename T>
0042     T const& get(const std::string&, const std::string&) const;
0043 
0044   private:
0045     const cms::DDDetector& m_det;
0046   };
0047 
0048   /* Helper: For a given node, get the values associated to a given parameter, from the XMLs SpecPar sections.
0049  * NB: The same parameter can appear several times WITHIN the same SpecPar section (hence, we have a std::vector).
0050  * WARNING: This stops at the first relevant SpecPar section encountered.
0051  * Hence, if A GIVEN NODE HAS SEVERAL SPECPAR XML SECTIONS RE-DEFINING THE SAME PARAMETER,
0052  * only the first XML SpecPar block will be considered.
0053  */
0054   template <typename T>
0055   std::vector<T> getAllParameterValuesFromSpecParSections(const cms::DDSpecParRegistry& allSpecParSections,
0056                                                           const std::string& nodePath,
0057                                                           const std::string& parameterName) {
0058     cms::DDSpecParRefs filteredSpecParSections;
0059     allSpecParSections.filter(filteredSpecParSections, parameterName);
0060     for (const auto& mySpecParSection : filteredSpecParSections) {
0061       if (mySpecParSection.second->hasPath(nodePath)) {
0062         return mySpecParSection.second->value<std::vector<T>>(parameterName);
0063       }
0064     }
0065 
0066     return std::vector<T>();
0067   }
0068 
0069   /* Helper: For a given node, get the value associated to a given parameter, from the XMLs SpecPar sections.
0070  * This is the parameterValueIndex-th value (within a XML SpecPar block.) of the desired parameter. 
0071  */
0072   template <typename T>
0073   T getParameterValueFromSpecParSections(const cms::DDSpecParRegistry& allSpecParSections,
0074                                          const std::string& nodePath,
0075                                          const std::string& parameterName,
0076                                          const unsigned int parameterValueIndex) {
0077     const std::vector<T>& allParameterValues =
0078         getAllParameterValuesFromSpecParSections<T>(allSpecParSections, nodePath, parameterName);
0079     if (parameterValueIndex < allParameterValues.size()) {
0080       return allParameterValues.at(parameterValueIndex);
0081     }
0082     return T();
0083   }
0084 
0085 }  // namespace cms
0086 
0087 #endif