DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
SegmentationParameter.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 //==========================================================================
11 /*
12  * SegmentationParameter.h
13  *
14  * Helper class to hold a segmentation parameter with its description.
15  *
16  * Created on: Dec 16, 2013
17  * Author: Christian Grefe, CERN
18  */
19 
20 #ifndef DDSEGMENTATION_SEGMENTATIONPARAMETER_H
21 #define DDSEGMENTATION_SEGMENTATIONPARAMETER_H
22 
23 #include <sstream>
24 #include <string>
25 #include <typeinfo>
26 #include <vector>
27 
28 namespace dd4hep {
29  namespace DDSegmentation {
30 
31 
33  inline std::vector<std::string> splitString(const std::string& s, char delimiter = ' ') {
34  std::vector<std::string> elements;
35  std::stringstream ss(s);
36  std::string item;
37  while (std::getline(ss, item, delimiter)) {
38  elements.emplace_back(item);
39  }
40  return elements;
41  }
42 
44  template<typename TYPE> struct TypeName {
45  static const char* name() {
46  return typeid(TYPE).name();
47  }
48  };
49 
51  template<> struct TypeName<int> {
52  static const char* name() {
53  return "int";
54  }
55  };
56 
58  template<> struct TypeName<float> {
59  static const char* name() {
60  return "float";
61  }
62  };
63 
65  template<> struct TypeName<double> {
66  static const char* name() {
67  return "double";
68  }
69  };
70 
72  template<> struct TypeName<std::string> {
73  static const char* name() {
74  return "string";
75  }
76  };
77 
79  template<> struct TypeName<std::vector<int> > {
80  static const char* name() {
81  return "intvec";
82  }
83  };
84 
86  template<> struct TypeName<std::vector<float> > {
87  static const char* name() {
88  return "floatvec";
89  }
90  };
91 
93  template<> struct TypeName<std::vector<double> > {
94  static const char* name() {
95  return "doublevec";
96  }
97  };
98 
100  template<> struct TypeName<std::vector<std::string> > {
101  static const char* name() {
102  return "stringvec";
103  }
104  };
105 
108  public:
110  enum UnitType {
112  };
115  }
117  const std::string& name() const {
118  return _name;
119  }
121  const std::string& description() const {
122  return _description;
123  }
125  UnitType unitType() const {
126  return _unitType;
127  }
129  virtual std::string type() const = 0;
131  virtual std::string value() const = 0;
133  virtual void setValue(const std::string& value) = 0;
135  virtual std::string defaultValue() const = 0;
137  bool isOptional() const {
138  return _isOptional;
139  }
141  std::string toString() const {
142  std::stringstream s;
143  s << _name << " = " << value();
144  if (not _description.empty()) {
145  s << " (" << _description << ")";
146  }
147  return s.str();
148  }
149  protected:
151  SegmentationParameter(const std::string& nam, const std::string& desc, UnitType unitTyp = NoUnit,
152  bool isOpt = false) :
153  _name(nam), _description(desc), _unitType(unitTyp), _isOptional(isOpt) {
154  }
156  std::string _name;
158  std::string _description;
163  };
164 
166  template<typename TYPE> class TypedSegmentationParameter: public SegmentationParameter {
167  public:
168 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
172 #endif
173  TypedSegmentationParameter(const std::string& nam, const std::string& desc, TYPE& val,
175  const TYPE& default_Value, SegmentationParameter::UnitType unitTyp = SegmentationParameter::NoUnit,
176  bool isOpt = false) :
177  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(default_Value) {
178  *_value = default_Value;
179  }
182 
184  const TYPE& typedValue() const {
185  return *_value;
186  }
187 
189  void setTypedValue(const TYPE& val) {
190  *_value = val;
191  }
192 
194  const TYPE& typedDefaultValue() const {
195  return _defaultValue;
196  }
197 
199  std::string type() const {
200  return TypeName<TYPE>::name();
201  }
202 
204  std::string value() const {
205  std::stringstream s;
206  s << *_value;
207  return s.str();
208  }
209 
211  void setValue(const std::string& val) {
212  std::stringstream s;
213  s << val;
214  s >> *_value;
215  }
216 
218  std::string defaultValue() const {
219  std::stringstream s;
220  s << _defaultValue;
221  return s.str();
222  }
223 
224  protected:
225  TYPE* _value = 0;
227  };
228 
230  template<typename TYPE> class TypedSegmentationParameter<std::vector<TYPE> > : public SegmentationParameter {
231  public:
232 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
236 #endif
237  TypedSegmentationParameter(const std::string& nam, const std::string& desc, std::vector<TYPE>& val,
239  const std::vector<TYPE>& defaultVal, SegmentationParameter::UnitType unitTyp =
240  SegmentationParameter::NoUnit, bool isOpt = false) :
241  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(defaultVal) {
242  *_value = defaultVal;
243  }
246 
248  const std::vector<TYPE>& typedValue() const {
249  return *_value;
250  }
251 
253  void setTypedValue(const std::vector<TYPE>& val) {
254  *_value = val;
255  }
256 
258  const std::vector<TYPE>& typedDefaultValue() const {
259  return _defaultValue;
260  }
261 
263  std::string type() const {
264  std::stringstream s;
265  s << TypeName<TYPE>::name() << "vec";
266  return s.str() ;
267  }
268 
270  std::string value() const {
271  std::stringstream s;
272  for (const auto& it : *_value ) {
273  s << it;
274  s << " ";
275  }
276  return s.str();
277  }
278 
280  void setValue(const std::string& val) {
281  std::vector<std::string> elements = splitString(val);
282  _value->clear();
283  for (std::vector<std::string>::const_iterator it = elements.begin(); it != elements.end(); ++it) {
284  if (not it->empty()) {
285  TYPE entry;
286  std::stringstream s;
287  s << *it;
288  s >> entry;
289  _value->emplace_back(entry);
290  }
291  }
292  }
293 
295  std::string defaultValue() const {
296  std::stringstream s;
297  typename std::vector<TYPE>::const_iterator it = _defaultValue.begin();
298  for (; it != _defaultValue.end(); ++it) {
299  s << *it;
300  s << " ";
301  }
302  return s.str();
303  }
304 
305  protected:
306  std::vector<TYPE>* _value = 0;
307  std::vector<TYPE> _defaultValue;
308 
309  };
310 
311  } /* namespace DDSegmentation */
312 } /* namespace dd4hep */
313 #endif // DDSEGMENTATION_SEGMENTATIONPARAMETER_H
dd4hep::DDSegmentation::TypedSegmentationParameter::typedValue
const TYPE & typedValue() const
Access to the parameter value.
Definition: SegmentationParameter.h:184
dd4hep::DDSegmentation::SegmentationParameter::_unitType
UnitType _unitType
The unit type.
Definition: SegmentationParameter.h:160
dd4hep::DDSegmentation::SegmentationParameter::description
const std::string & description() const
Access to the parameter description.
Definition: SegmentationParameter.h:121
dd4hep::DDSegmentation::TypedSegmentationParameter::_defaultValue
TYPE _defaultValue
Definition: SegmentationParameter.h:226
dd4hep::DDSegmentation::TypeName< std::vector< std::string > >::name
static const char * name()
Definition: SegmentationParameter.h:101
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::defaultValue
std::string defaultValue() const
Access to the parameter default value in string representation.
Definition: SegmentationParameter.h:295
dd4hep::DDSegmentation::SegmentationParameter::LengthUnit
@ LengthUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::SegmentationParameter::AngleUnit
@ AngleUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::TypedSegmentationParameter::typedDefaultValue
const TYPE & typedDefaultValue() const
Access to the parameter default value.
Definition: SegmentationParameter.h:194
dd4hep::DDSegmentation::TypedSegmentationParameter::TypedSegmentationParameter
TypedSegmentationParameter(const std::string &nam, const std::string &desc, TYPE &val, const TYPE &default_Value, SegmentationParameter::UnitType unitTyp=SegmentationParameter::NoUnit, bool isOpt=false)
Default constructor.
Definition: SegmentationParameter.h:174
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::setValue
void setValue(const std::string &val)
Set the parameter value in string representation.
Definition: SegmentationParameter.h:280
dd4hep::DDSegmentation::TypedSegmentationParameter::value
std::string value() const
Access to the parameter value in string representation.
Definition: SegmentationParameter.h:204
dd4hep::DDSegmentation::SegmentationParameter::_name
std::string _name
The parameter name.
Definition: SegmentationParameter.h:156
dd4hep::DDSegmentation::TypeName< double >::name
static const char * name()
Definition: SegmentationParameter.h:66
dd4hep::DDSegmentation::TypeName< std::vector< float > >::name
static const char * name()
Definition: SegmentationParameter.h:87
dd4hep::DDSegmentation::SegmentationParameter
Class to hold a segmentation parameter with its description.
Definition: SegmentationParameter.h:107
dd4hep::DDSegmentation::SegmentationParameter::type
virtual std::string type() const =0
Access to the parameter type.
dd4hep::DDSegmentation::TypeName< std::vector< double > >::name
static const char * name()
Definition: SegmentationParameter.h:94
dd4hep::DDSegmentation::SegmentationParameter::_isOptional
bool _isOptional
Store if parameter is optional.
Definition: SegmentationParameter.h:162
dd4hep::DDSegmentation::SegmentationParameter::_description
std::string _description
The parameter description.
Definition: SegmentationParameter.h:158
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::type
std::string type() const
Access to the parameter type.
Definition: SegmentationParameter.h:263
dd4hep::DDSegmentation::TypeName< float >::name
static const char * name()
Definition: SegmentationParameter.h:59
dd4hep::DDSegmentation::SegmentationParameter::name
const std::string & name() const
Access to the parameter name.
Definition: SegmentationParameter.h:117
dd4hep::DDSegmentation::SegmentationParameter::toString
std::string toString() const
Printable version.
Definition: SegmentationParameter.h:141
dd4hep::DDSegmentation::SegmentationParameter::~SegmentationParameter
virtual ~SegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:114
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::typedValue
const std::vector< TYPE > & typedValue() const
Access to the parameter value.
Definition: SegmentationParameter.h:248
dd4hep::DDSegmentation::SegmentationParameter::NoUnit
@ NoUnit
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::_defaultValue
std::vector< TYPE > _defaultValue
Definition: SegmentationParameter.h:307
dd4hep::DDSegmentation::TypedSegmentationParameter::setValue
void setValue(const std::string &val)
Set the parameter value in string representation.
Definition: SegmentationParameter.h:211
dd4hep::DDSegmentation::TypedSegmentationParameter::setTypedValue
void setTypedValue(const TYPE &val)
Set the parameter value.
Definition: SegmentationParameter.h:189
dd4hep::DDSegmentation::SegmentationParameter::SegmentationParameter
SegmentationParameter(const std::string &nam, const std::string &desc, UnitType unitTyp=NoUnit, bool isOpt=false)
Default constructor used by derived classes.
Definition: SegmentationParameter.h:151
dd4hep::DDSegmentation::SegmentationParameter::isOptional
bool isOptional() const
Check if this parameter is optional.
Definition: SegmentationParameter.h:137
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::value
std::string value() const
Access to the parameter value in string representation.
Definition: SegmentationParameter.h:270
dd4hep::DDSegmentation::SegmentationParameter::setValue
virtual void setValue(const std::string &value)=0
Set the parameter value in string representation.
dd4hep::DDSegmentation::splitString
std::vector< std::string > splitString(const std::string &s, char delimiter=' ')
Helper method to split string into tokens.
Definition: SegmentationParameter.h:33
dd4hep::DDSegmentation::SegmentationParameter::defaultValue
virtual std::string defaultValue() const =0
Access to the parameter default value in string representation.
dd4hep::DDSegmentation::TypedSegmentationParameter::type
std::string type() const
Access to the parameter type.
Definition: SegmentationParameter.h:199
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::~TypedSegmentationParameter
virtual ~TypedSegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:245
dd4hep::DDSegmentation::TypeName< int >::name
static const char * name()
Definition: SegmentationParameter.h:52
dd4hep::DDSegmentation::TypedSegmentationParameter::_value
TYPE * _value
Definition: SegmentationParameter.h:225
dd4hep::DDSegmentation::TypeName::name
static const char * name()
Definition: SegmentationParameter.h:45
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::typedDefaultValue
const std::vector< TYPE > & typedDefaultValue() const
Access to the parameter default value.
Definition: SegmentationParameter.h:258
std
Definition: Plugins.h:30
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::DDSegmentation::TypedSegmentationParameter
Concrete class to hold a segmentation parameter of a given type with its description.
Definition: SegmentationParameter.h:166
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::setTypedValue
void setTypedValue(const std::vector< TYPE > &val)
Set the parameter value.
Definition: SegmentationParameter.h:253
dd4hep::DDSegmentation::TypedSegmentationParameter::~TypedSegmentationParameter
virtual ~TypedSegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:181
dd4hep::DDSegmentation::TypeName< std::string >::name
static const char * name()
Definition: SegmentationParameter.h:73
dd4hep::DDSegmentation::TypeName
Helper class to extract type names.
Definition: SegmentationParameter.h:44
dd4hep::DDSegmentation::TypedSegmentationParameter::defaultValue
std::string defaultValue() const
Access to the parameter default value in string representation.
Definition: SegmentationParameter.h:218
dd4hep::DDSegmentation::SegmentationParameter::UnitType
UnitType
Defines the parameter unit type (useful to convert to default set of units)
Definition: SegmentationParameter.h:110
dd4hep::DDSegmentation::TypeName< std::vector< int > >::name
static const char * name()
Definition: SegmentationParameter.h:80
dd4hep::DDSegmentation::SegmentationParameter::value
virtual std::string value() const =0
Access to the parameter value in string representation.
dd4hep::DDSegmentation::SegmentationParameter::unitType
UnitType unitType() const
Access to the unit type.
Definition: SegmentationParameter.h:125