DD4hep  1.28.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 /*
13  * SegmentationParameter.h
14  *
15  * Helper class to hold a segmentation parameter with its description.
16  *
17  * Created on: Dec 16, 2013
18  * Author: Christian Grefe, CERN
19  */
20 
21 #ifndef DDSEGMENTATION_SEGMENTATIONPARAMETER_H
22 #define DDSEGMENTATION_SEGMENTATIONPARAMETER_H
23 
24 #include <sstream>
25 #include <string>
26 #include <typeinfo>
27 #include <vector>
28 
29 namespace dd4hep {
30  namespace DDSegmentation {
31 
32 
34  inline std::vector<std::string> splitString(const std::string& s, char delimiter = ' ') {
35  std::vector<std::string> elements;
36  std::stringstream ss(s);
37  std::string item;
38  while (std::getline(ss, item, delimiter)) {
39  elements.emplace_back(item);
40  }
41  return elements;
42  }
43 
45  template<typename TYPE> struct TypeName {
46  static const char* name() {
47  return typeid(TYPE).name();
48  }
49  };
50 
52  template<> struct TypeName<int> {
53  static const char* name() {
54  return "int";
55  }
56  };
57 
59  template<> struct TypeName<float> {
60  static const char* name() {
61  return "float";
62  }
63  };
64 
66  template<> struct TypeName<double> {
67  static const char* name() {
68  return "double";
69  }
70  };
71 
73  template<> struct TypeName<std::string> {
74  static const char* name() {
75  return "string";
76  }
77  };
78 
80  template<> struct TypeName<std::vector<int> > {
81  static const char* name() {
82  return "intvec";
83  }
84  };
85 
87  template<> struct TypeName<std::vector<float> > {
88  static const char* name() {
89  return "floatvec";
90  }
91  };
92 
94  template<> struct TypeName<std::vector<double> > {
95  static const char* name() {
96  return "doublevec";
97  }
98  };
99 
101  template<> struct TypeName<std::vector<std::string> > {
102  static const char* name() {
103  return "stringvec";
104  }
105  };
106 
109  public:
111  enum UnitType {
113  };
116  }
118  const std::string& name() const {
119  return _name;
120  }
122  const std::string& description() const {
123  return _description;
124  }
126  UnitType unitType() const {
127  return _unitType;
128  }
130  virtual std::string type() const = 0;
132  virtual std::string value() const = 0;
134  virtual void setValue(const std::string& value) = 0;
136  virtual std::string defaultValue() const = 0;
138  bool isOptional() const {
139  return _isOptional;
140  }
142  std::string toString() const {
143  std::stringstream s;
144  s << _name << " = " << value();
145  if (not _description.empty()) {
146  s << " (" << _description << ")";
147  }
148  return s.str();
149  }
150  protected:
152  SegmentationParameter(const std::string& nam, const std::string& desc, UnitType unitTyp = NoUnit,
153  bool isOpt = false) :
154  _name(nam), _description(desc), _unitType(unitTyp), _isOptional(isOpt) {
155  }
157  std::string _name;
159  std::string _description;
164  };
165 
167  template<typename TYPE> class TypedSegmentationParameter: public SegmentationParameter {
168  public:
169 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
173 #endif
174  TypedSegmentationParameter(const std::string& nam, const std::string& desc, TYPE& val,
176  const TYPE& default_Value, SegmentationParameter::UnitType unitTyp = SegmentationParameter::NoUnit,
177  bool isOpt = false) :
178  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(default_Value) {
179  *_value = default_Value;
180  }
183 
185  const TYPE& typedValue() const {
186  return *_value;
187  }
188 
190  void setTypedValue(const TYPE& val) {
191  *_value = val;
192  }
193 
195  const TYPE& typedDefaultValue() const {
196  return _defaultValue;
197  }
198 
200  std::string type() const {
201  return TypeName<TYPE>::name();
202  }
203 
205  std::string value() const {
206  std::stringstream s;
207  s << *_value;
208  return s.str();
209  }
210 
212  void setValue(const std::string& val) {
213  std::stringstream s;
214  s << val;
215  s >> *_value;
216  }
217 
219  std::string defaultValue() const {
220  std::stringstream s;
221  s << _defaultValue;
222  return s.str();
223  }
224 
225  protected:
226  TYPE* _value = 0;
228  };
229 
231  template<typename TYPE> class TypedSegmentationParameter<std::vector<TYPE> > : public SegmentationParameter {
232  public:
233 #if defined(G__ROOT) || defined(__CLANG__) || defined(__ROOTCLING__)
237 #endif
238  TypedSegmentationParameter(const std::string& nam, const std::string& desc, std::vector<TYPE>& val,
240  const std::vector<TYPE>& defaultVal, SegmentationParameter::UnitType unitTyp =
241  SegmentationParameter::NoUnit, bool isOpt = false) :
242  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(&val), _defaultValue(defaultVal) {
243  *_value = defaultVal;
244  }
247 
249  const std::vector<TYPE>& typedValue() const {
250  return *_value;
251  }
252 
254  void setTypedValue(const std::vector<TYPE>& val) {
255  *_value = val;
256  }
257 
259  const std::vector<TYPE>& typedDefaultValue() const {
260  return _defaultValue;
261  }
262 
264  std::string type() const {
265  std::stringstream s;
266  s << TypeName<TYPE>::name() << "vec";
267  return s.str() ;
268  }
269 
271  std::string value() const {
272  std::stringstream s;
273  for (const auto& it : *_value ) {
274  s << it;
275  s << " ";
276  }
277  return s.str();
278  }
279 
281  void setValue(const std::string& val) {
282  std::vector<std::string> elements = splitString(val);
283  _value->clear();
284  for (std::vector<std::string>::const_iterator it = elements.begin(); it != elements.end(); ++it) {
285  if (not it->empty()) {
286  TYPE entry;
287  std::stringstream s;
288  s << *it;
289  s >> entry;
290  _value->emplace_back(entry);
291  }
292  }
293  }
294 
296  std::string defaultValue() const {
297  std::stringstream s;
298  typename std::vector<TYPE>::const_iterator it = _defaultValue.begin();
299  for (; it != _defaultValue.end(); ++it) {
300  s << *it;
301  s << " ";
302  }
303  return s.str();
304  }
305 
306  protected:
307  std::vector<TYPE>* _value = 0;
308  std::vector<TYPE> _defaultValue;
309 
310  };
311 
312  } /* namespace DDSegmentation */
313 } /* namespace dd4hep */
314 #endif // DDSEGMENTATION_SEGMENTATIONPARAMETER_H
dd4hep::DDSegmentation::TypedSegmentationParameter::typedValue
const TYPE & typedValue() const
Access to the parameter value.
Definition: SegmentationParameter.h:185
dd4hep::DDSegmentation::SegmentationParameter::_unitType
UnitType _unitType
The unit type.
Definition: SegmentationParameter.h:161
dd4hep::DDSegmentation::SegmentationParameter::description
const std::string & description() const
Access to the parameter description.
Definition: SegmentationParameter.h:122
dd4hep::DDSegmentation::TypedSegmentationParameter::_defaultValue
TYPE _defaultValue
Definition: SegmentationParameter.h:227
dd4hep::DDSegmentation::TypeName< std::vector< std::string > >::name
static const char * name()
Definition: SegmentationParameter.h:102
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::defaultValue
std::string defaultValue() const
Access to the parameter default value in string representation.
Definition: SegmentationParameter.h:296
dd4hep::DDSegmentation::SegmentationParameter::LengthUnit
@ LengthUnit
Definition: SegmentationParameter.h:112
dd4hep::DDSegmentation::SegmentationParameter::AngleUnit
@ AngleUnit
Definition: SegmentationParameter.h:112
dd4hep::DDSegmentation::TypedSegmentationParameter::typedDefaultValue
const TYPE & typedDefaultValue() const
Access to the parameter default value.
Definition: SegmentationParameter.h:195
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:175
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::setValue
void setValue(const std::string &val)
Set the parameter value in string representation.
Definition: SegmentationParameter.h:281
dd4hep::DDSegmentation::TypedSegmentationParameter::value
std::string value() const
Access to the parameter value in string representation.
Definition: SegmentationParameter.h:205
dd4hep::DDSegmentation::SegmentationParameter::_name
std::string _name
The parameter name.
Definition: SegmentationParameter.h:157
dd4hep::DDSegmentation::TypeName< double >::name
static const char * name()
Definition: SegmentationParameter.h:67
dd4hep::DDSegmentation::TypeName< std::vector< float > >::name
static const char * name()
Definition: SegmentationParameter.h:88
dd4hep::DDSegmentation::SegmentationParameter
Class to hold a segmentation parameter with its description.
Definition: SegmentationParameter.h:108
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:95
dd4hep::DDSegmentation::SegmentationParameter::_isOptional
bool _isOptional
Store if parameter is optional.
Definition: SegmentationParameter.h:163
dd4hep::DDSegmentation::SegmentationParameter::_description
std::string _description
The parameter description.
Definition: SegmentationParameter.h:159
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::type
std::string type() const
Access to the parameter type.
Definition: SegmentationParameter.h:264
dd4hep::DDSegmentation::TypeName< float >::name
static const char * name()
Definition: SegmentationParameter.h:60
dd4hep::DDSegmentation::SegmentationParameter::name
const std::string & name() const
Access to the parameter name.
Definition: SegmentationParameter.h:118
dd4hep::DDSegmentation::SegmentationParameter::toString
std::string toString() const
Printable version.
Definition: SegmentationParameter.h:142
dd4hep::DDSegmentation::SegmentationParameter::~SegmentationParameter
virtual ~SegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:115
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::typedValue
const std::vector< TYPE > & typedValue() const
Access to the parameter value.
Definition: SegmentationParameter.h:249
dd4hep::DDSegmentation::SegmentationParameter::NoUnit
@ NoUnit
Definition: SegmentationParameter.h:112
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::_defaultValue
std::vector< TYPE > _defaultValue
Definition: SegmentationParameter.h:308
dd4hep::DDSegmentation::TypedSegmentationParameter::setValue
void setValue(const std::string &val)
Set the parameter value in string representation.
Definition: SegmentationParameter.h:212
dd4hep::DDSegmentation::TypedSegmentationParameter::setTypedValue
void setTypedValue(const TYPE &val)
Set the parameter value.
Definition: SegmentationParameter.h:190
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:152
dd4hep::DDSegmentation::SegmentationParameter::isOptional
bool isOptional() const
Check if this parameter is optional.
Definition: SegmentationParameter.h:138
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::value
std::string value() const
Access to the parameter value in string representation.
Definition: SegmentationParameter.h:271
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:34
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:200
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::~TypedSegmentationParameter
virtual ~TypedSegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:246
dd4hep::DDSegmentation::TypeName< int >::name
static const char * name()
Definition: SegmentationParameter.h:53
dd4hep::DDSegmentation::TypedSegmentationParameter::_value
TYPE * _value
Definition: SegmentationParameter.h:226
dd4hep::DDSegmentation::TypeName::name
static const char * name()
Definition: SegmentationParameter.h:46
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::typedDefaultValue
const std::vector< TYPE > & typedDefaultValue() const
Access to the parameter default value.
Definition: SegmentationParameter.h:259
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:167
dd4hep::DDSegmentation::TypedSegmentationParameter< std::vector< TYPE > >::setTypedValue
void setTypedValue(const std::vector< TYPE > &val)
Set the parameter value.
Definition: SegmentationParameter.h:254
dd4hep::DDSegmentation::TypedSegmentationParameter::~TypedSegmentationParameter
virtual ~TypedSegmentationParameter()
Default destructor.
Definition: SegmentationParameter.h:182
dd4hep::DDSegmentation::TypeName< std::string >::name
static const char * name()
Definition: SegmentationParameter.h:74
dd4hep::DDSegmentation::TypeName
Helper class to extract type names.
Definition: SegmentationParameter.h:45
dd4hep::DDSegmentation::TypedSegmentationParameter::defaultValue
std::string defaultValue() const
Access to the parameter default value in string representation.
Definition: SegmentationParameter.h:219
dd4hep::DDSegmentation::SegmentationParameter::UnitType
UnitType
Defines the parameter unit type (useful to convert to default set of units)
Definition: SegmentationParameter.h:111
dd4hep::DDSegmentation::TypeName< std::vector< int > >::name
static const char * name()
Definition: SegmentationParameter.h:81
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:126