Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:37

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 //==========================================================================
0011 /*
0012  * SegmentationParameter.h
0013  *
0014  * Helper class to hold a segmentation parameter with its description.
0015  *
0016  *  Created on: Dec 16, 2013
0017  *      Author: Christian Grefe, CERN
0018  */
0019 
0020 #ifndef DDSEGMENTATION_SEGMENTATIONPARAMETER_H
0021 #define DDSEGMENTATION_SEGMENTATIONPARAMETER_H
0022 
0023 #include <sstream>
0024 #include <string>
0025 #include <typeinfo>
0026 #include <vector>
0027 
0028 namespace dd4hep {
0029   namespace DDSegmentation {
0030 
0031 
0032     /// Helper method to split string into tokens
0033     inline std::vector<std::string> splitString(const std::string& s, char delimiter = ' ') {
0034       std::vector<std::string> elements;
0035       std::stringstream ss(s);
0036       std::string item;
0037       while (std::getline(ss, item, delimiter)) {
0038         elements.emplace_back(item);
0039       }
0040       return elements;
0041     }
0042 
0043     /// Helper class to extract type names
0044     template<typename TYPE> struct TypeName {
0045       static const char* name() {
0046         return typeid(TYPE).name();
0047       }
0048     };
0049 
0050     /// Specialization for int type
0051     template<> struct TypeName<int> {
0052       static const char* name() {
0053         return "int";
0054       }
0055     };
0056 
0057     /// Specialization for float type
0058     template<> struct TypeName<float> {
0059       static const char* name() {
0060         return "float";
0061       }
0062     };
0063 
0064     /// Specialization for double type
0065     template<> struct TypeName<double> {
0066       static const char* name() {
0067         return "double";
0068       }
0069     };
0070 
0071     /// Specialization for string type
0072     template<> struct TypeName<std::string> {
0073       static const char* name() {
0074         return "string";
0075       }
0076     };
0077 
0078     /// Specialization for int vector type
0079     template<> struct TypeName<std::vector<int> > {
0080       static const char* name() {
0081         return "intvec";
0082       }
0083     };
0084 
0085     /// Specialization for float vector type
0086     template<> struct TypeName<std::vector<float> > {
0087       static const char* name() {
0088         return "floatvec";
0089       }
0090     };
0091 
0092     /// Specialization for double vector type
0093     template<> struct TypeName<std::vector<double> > {
0094       static const char* name() {
0095         return "doublevec";
0096       }
0097     };
0098 
0099     /// Specialization for string vector type
0100     template<> struct TypeName<std::vector<std::string> > {
0101       static const char* name() {
0102         return "stringvec";
0103       }
0104     };
0105 
0106     /// Class to hold a segmentation parameter with its description
0107     class SegmentationParameter {
0108     public:
0109       /// Defines the parameter unit type (useful to convert to default set of units)
0110       enum UnitType {
0111         NoUnit, LengthUnit, AngleUnit
0112       };
0113       /// Default destructor
0114       virtual ~SegmentationParameter() {
0115       }
0116       /// Access to the parameter name
0117       const std::string& name() const {
0118         return _name;
0119       }
0120       /// Access to the parameter description
0121       const std::string& description() const {
0122         return _description;
0123       }
0124       /// Access to the unit type
0125       UnitType unitType() const {
0126         return _unitType;
0127       }
0128       /// Access to the parameter type
0129       virtual std::string type() const = 0;
0130       /// Access to the parameter value in string representation
0131       virtual std::string value() const = 0;
0132       /// Set the parameter value in string representation
0133       virtual void setValue(const std::string& value) = 0;
0134       /// Access to the parameter default value in string representation
0135       virtual std::string defaultValue() const = 0;
0136       /// Check if this parameter is optional
0137       bool isOptional() const {
0138         return _isOptional;
0139       }
0140       /// Printable version
0141       std::string toString() const {
0142         std::stringstream s;
0143         s << _name << " = " << value();
0144         if (not _description.empty()) {
0145           s << " (" << _description << ")";
0146         }
0147         return s.str();
0148       }
0149     protected:
0150       /// Default constructor used by derived classes
0151       SegmentationParameter(const std::string& nam, const std::string& desc, UnitType unitTyp = NoUnit,
0152                             bool isOpt = false) :
0153         _name(nam), _description(desc), _unitType(unitTyp), _isOptional(isOpt) {
0154       }
0155       /// The parameter name
0156       std::string _name;
0157       /// The parameter description
0158       std::string _description;
0159       /// The unit type
0160       UnitType _unitType;
0161       /// Store if parameter is optional
0162       bool _isOptional;
0163     };
0164 
0165     /// Concrete class to hold a segmentation parameter of a given type with its description
0166     template<typename TYPE> class TypedSegmentationParameter: public SegmentationParameter {
0167     public:
0168 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
0169       /// Empty constructor for ROOT persistency
0170       TypedSegmentationParameter()
0171         : SegmentationParameter("","",SegmentationParameter::NoUnit,false), _value(0), _defaultValue()  {}
0172 #endif
0173       /// Default constructor
0174       TypedSegmentationParameter(const std::string& nam, const std::string& desc, TYPE& val,
0175                                  const TYPE& default_Value, SegmentationParameter::UnitType unitTyp = SegmentationParameter::NoUnit,
0176                                  bool isOpt = false) :
0177         SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(default_Value) {
0178         *_value = default_Value;
0179       }
0180       /// Default destructor
0181       virtual ~TypedSegmentationParameter() {   }
0182 
0183       /// Access to the parameter value
0184       const TYPE& typedValue() const {
0185         return *_value;
0186       }
0187 
0188       /// Set the parameter value
0189       void setTypedValue(const TYPE& val) {
0190         *_value = val;
0191       }
0192 
0193       /// Access to the parameter default value
0194       const TYPE& typedDefaultValue() const {
0195         return _defaultValue;
0196       }
0197 
0198       /// Access to the parameter type
0199       std::string type() const {
0200         return TypeName<TYPE>::name();
0201       }
0202 
0203       /// Access to the parameter value in string representation
0204       std::string value() const {
0205         std::stringstream s;
0206         s << *_value;
0207         return s.str();
0208       }
0209 
0210       /// Set the parameter value in string representation
0211       void setValue(const std::string& val) {
0212         std::stringstream s;
0213         s << val;
0214         s >> *_value;
0215       }
0216 
0217       /// Access to the parameter default value in string representation
0218       std::string defaultValue() const {
0219         std::stringstream s;
0220         s << _defaultValue;
0221         return s.str();
0222       }
0223 
0224     protected:
0225       TYPE* _value = 0;   //!
0226       TYPE  _defaultValue;
0227     };
0228 
0229     /// Concrete class to hold a segmentation parameter of a vector of items of a given type with its description
0230     template<typename TYPE> class TypedSegmentationParameter<std::vector<TYPE> > : public SegmentationParameter {
0231     public:
0232 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
0233       /// Empty constructor for ROOT persistency
0234       TypedSegmentationParameter()
0235         : SegmentationParameter("","",SegmentationParameter::NoUnit,false), _value(0), _defaultValue()  {}
0236 #endif
0237       /// Default constructor
0238       TypedSegmentationParameter(const std::string& nam, const std::string& desc, std::vector<TYPE>& val,
0239                                  const std::vector<TYPE>& defaultVal, SegmentationParameter::UnitType unitTyp =
0240                                  SegmentationParameter::NoUnit, bool isOpt = false) :
0241         SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(defaultVal) {
0242         *_value = defaultVal;
0243       }
0244       /// Default destructor
0245       virtual ~TypedSegmentationParameter() {   }
0246 
0247       /// Access to the parameter value
0248       const std::vector<TYPE>& typedValue() const {
0249         return *_value;
0250       }
0251 
0252       /// Set the parameter value
0253       void setTypedValue(const std::vector<TYPE>& val) {
0254         *_value = val;
0255       }
0256 
0257       /// Access to the parameter default value
0258       const std::vector<TYPE>& typedDefaultValue() const {
0259         return _defaultValue;
0260       }
0261 
0262       /// Access to the parameter type
0263       std::string type() const {
0264         std::stringstream s;
0265         s << TypeName<TYPE>::name() << "vec";
0266         return s.str() ;
0267       }
0268 
0269       /// Access to the parameter value in string representation
0270       std::string value() const {
0271         std::stringstream s;
0272         for (const auto& it : *_value )  {
0273           s << it;
0274           s << " ";
0275         }
0276         return s.str();
0277       }
0278 
0279       /// Set the parameter value in string representation
0280       void setValue(const std::string& val) {
0281         std::vector<std::string> elements = splitString(val);
0282         _value->clear();
0283         for (std::vector<std::string>::const_iterator it = elements.begin(); it != elements.end(); ++it) {
0284           if (not it->empty()) {
0285             TYPE entry;
0286             std::stringstream s;
0287             s << *it;
0288             s >> entry;
0289             _value->emplace_back(entry);
0290           }
0291         }
0292       }
0293 
0294       /// Access to the parameter default value in string representation
0295       std::string defaultValue() const {
0296         std::stringstream s;
0297         typename std::vector<TYPE>::const_iterator it = _defaultValue.begin();
0298         for (; it != _defaultValue.end(); ++it) {
0299           s << *it;
0300           s << " ";
0301         }
0302         return s.str();
0303       }
0304 
0305     protected:
0306       std::vector<TYPE>* _value = 0;   //!
0307       std::vector<TYPE>  _defaultValue;
0308 
0309     };
0310 
0311   } /* namespace DDSegmentation */
0312 } /* namespace dd4hep */
0313 #endif // DDSEGMENTATION_SEGMENTATIONPARAMETER_H