DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Handle.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 #include "DD4hep/InstanceCount.h"
16 #include "DD4hep/Printout.h"
17 #include "DD4hep/Handle.inl"
18 #include "XML/Evaluator.h"
19 #include <iostream>
20 #include <iomanip>
21 #include <climits>
22 #include <cstring>
23 #include <cstdio>
24 
25 #if !defined(WIN32) && !defined(__ICC)
26 #include "cxxabi.h"
27 #endif
28 
29 namespace DD4hep {
31 }
32 
33 namespace {
35 }
36 
37 using namespace std;
38 using namespace DD4hep;
39 using namespace DD4hep::Geometry;
40 
41 short DD4hep::_toShort(const string& value) {
42  string s(value);
43  size_t idx = s.find("(int)");
44  if (idx != string::npos)
45  s.erase(idx, 5);
46  while (s[0] == ' ')
47  s.erase(0, 1);
48  double result = eval.evaluate(s.c_str());
49  if (eval.status() != XmlTools::Evaluator::OK) {
50  cerr << value << ": ";
51  eval.print_error();
52  throw runtime_error("DD4hep: Severe error during expression evaluation of " + value);
53  }
54  return (short) result;
55 }
56 
57 int DD4hep::_toInt(const string& value) {
58  string s(value);
59  size_t idx = s.find("(int)");
60  if (idx != string::npos)
61  s.erase(idx, 5);
62  while (s[0] == ' ')
63  s.erase(0, 1);
64  double result = eval.evaluate(s.c_str());
65  if (eval.status() != XmlTools::Evaluator::OK) {
66  cerr << value << ": ";
67  eval.print_error();
68  throw runtime_error("DD4hep: Severe error during expression evaluation of " + value);
69  }
70  return (int) result;
71 }
72 
73 long DD4hep::_toLong(const string& value) {
74  string s(value);
75  size_t idx = s.find("(int)");
76  if (idx != string::npos)
77  s.erase(idx, 5);
78  while (s[0] == ' ')
79  s.erase(0, 1);
80  double result = eval.evaluate(s.c_str());
81  if (eval.status() != XmlTools::Evaluator::OK) {
82  cerr << value << ": ";
83  eval.print_error();
84  throw runtime_error("DD4hep: Severe error during expression evaluation of " + value);
85  }
86  return (long) result;
87 }
88 
89 bool DD4hep::_toBool(const string& value) {
90  return value == "true" || value == "yes";
91 }
92 
93 float DD4hep::_toFloat(const string& value) {
94  double result = eval.evaluate(value.c_str());
95  if (eval.status() != XmlTools::Evaluator::OK) {
96  cerr << value << ": ";
97  eval.print_error();
98  throw runtime_error("DD4hep: Severe error during expression evaluation of " + value);
99  }
100  return (float) result;
101 }
102 
103 double DD4hep::_toDouble(const string& value) {
104  double result = eval.evaluate(value.c_str());
105  if (eval.status() != XmlTools::Evaluator::OK) {
106  cerr << value << ": ";
107  eval.print_error();
108  throw runtime_error("DD4hep: Severe error during expression evaluation of " + value);
109  }
110  return result;
111 }
112 
113 template <> char DD4hep::_multiply<char>(const string& left, const string& right) {
114  double val = _toDouble(left + "*" + right);
115  if ( val >= double(SCHAR_MIN) && val <= double(SCHAR_MAX) )
116  return (char) (int)val;
117  except("_multiply<char>",
118  "Multiplication %e = %s * %s out of bounds for conversion to char.",
119  val, left.c_str(), right.c_str());
120  return 0;
121 }
122 
123 template <> unsigned char DD4hep::_multiply<unsigned char>(const string& left, const string& right) {
124  double val = _toDouble(left + "*" + right);
125  if ( val >= 0 && val <= double(UCHAR_MAX) )
126  return (unsigned char) (int)val;
127  except("_multiply<char>",
128  "Multiplication %e = %s * %s out of bounds for conversion to unsigned char.",
129  val, left.c_str(), right.c_str());
130  return 0;
131 }
132 
133 template <> short DD4hep::_multiply<short>(const string& left, const string& right) {
134  double val = _toDouble(left + "*" + right);
135  if ( val >= double(SHRT_MIN) && val <= double(SHRT_MAX) )
136  return (short) val;
137  except("_multiply<char>",
138  "Multiplication %e = %s * %s out of bounds for conversion to short.",
139  val, left.c_str(), right.c_str());
140  return 0;
141 }
142 
143 template <> unsigned short DD4hep::_multiply<unsigned short>(const string& left, const string& right) {
144  double val = _toDouble(left + "*" + right);
145  if ( val >= 0 && val <= double(USHRT_MAX) )
146  return (unsigned short)val;
147  except("_multiply<char>",
148  "Multiplication %e = %s * %s out of bounds for conversion to unsigned short.",
149  val, left.c_str(), right.c_str());
150  return 0;
151 }
152 
153 template <> int DD4hep::_multiply<int>(const string& left, const string& right) {
154  return (int) _toDouble(left + "*" + right);
155 }
156 
157 template <> unsigned int DD4hep::_multiply<unsigned int>(const string& left, const string& right) {
158  return (unsigned int) _toDouble(left + "*" + right);
159 }
160 
161 template <> long DD4hep::_multiply<long>(const string& left, const string& right) {
162  return (long) _toDouble(left + "*" + right);
163 }
164 
165 template <> unsigned long DD4hep::_multiply<unsigned long>(const string& left, const string& right) {
166  return (unsigned long) _toDouble(left + "*" + right);
167 }
168 
169 template <> float DD4hep::_multiply<float>(const string& left, const string& right) {
170  return _toFloat(left + "*" + right);
171 }
172 
173 template <> double DD4hep::_multiply<double>(const string& left, const string& right) {
174  return _toDouble(left + "*" + right);
175 }
176 
177 void DD4hep::_toDictionary(const string& name, const string& value) {
178  _toDictionary(name, value, "number");
179 }
180 
182 void DD4hep::_toDictionary(const std::string& name, const std::string& value, const std::string& typ) {
183  if ( typ == "string" ) {
184  eval.setEnviron(name.c_str(),value.c_str());
185  return;
186  }
187  else {
188  string n = name, v = value;
189  size_t idx = v.find("(int)");
190  if (idx != string::npos)
191  v.erase(idx, 5);
192  idx = v.find("(float)");
193  if (idx != string::npos)
194  v.erase(idx, 7);
195  while (v[0] == ' ')
196  v.erase(0, 1);
197  double result = eval.evaluate(v.c_str());
198  if (eval.status() != XmlTools::Evaluator::OK) {
199  cerr << value << ": ";
200  eval.print_error();
201  throw runtime_error("DD4hep: Severe error during expression evaluation " + name + "=" + value);
202  }
203  eval.setVariable(n.c_str(), result);
204  }
205 }
206 
207 template <typename T> static inline string __to_string(T value, const char* fmt) {
208  char text[128];
209  ::snprintf(text, sizeof(text), fmt, value);
210  return text;
211 }
212 
213 string DD4hep::_toString(bool value) {
214  return value ? "true" : "false";
215 }
216 
217 string DD4hep::_toString(short value, const char* fmt) {
218  return __to_string((int)value, fmt);
219 }
220 
221 string DD4hep::_toString(int value, const char* fmt) {
222  return __to_string(value, fmt);
223 }
224 
225 string DD4hep::_toString(float value, const char* fmt) {
226  return __to_string(value, fmt);
227 }
228 
229 string DD4hep::_toString(double value, const char* fmt) {
230  return __to_string(value, fmt);
231 }
232 
233 string DD4hep::_ptrToString(const void* value, const char* fmt) {
234  return __to_string(value, fmt);
235 }
236 
237 namespace DD4hep {
238  static long s_numVerifies = 0;
239 
241  return s_numVerifies;
242  }
244  ++s_numVerifies;
245  }
246  void warning_deprecated_xml_factory(const char* name) {
247  const char* edge = "++++++++++++++++++++++++++++++++++++++++++";
248  size_t len = ::strlen(name);
249  cerr << edge << edge << edge << endl;
250  cerr << "++ The usage of the factory: \"" << name << "\" is DEPRECATED due to naming conventions."
251  << setw(53-len) << right << "++" << endl;
252  cerr << "++ Please use \"DD4hep_" << name << "\" instead." << setw(93-len) << right << "++" << endl;
253  cerr << edge << edge << edge << endl;
254  }
255 }
256 
259 //INSTANTIATE_UNNAMED(_Segmentation);
260 namespace DD4hep {
261  template <> void Handle<_Segmentation>::assign(_Segmentation* s, const std::string& n, const std::string&) {
262  this->m_element = s;
263  s->setName(n);
264  }
265  template <> const char* Handle<_Segmentation>::name() const {
266  return this->m_element ? this->m_element->name().c_str() : "";
267  }
268  template class DD4hep::Handle<_Segmentation>;
269 }
270 
271 #include "DD4hep/LCDD.h"
272 #include "TMap.h"
273 #include "TColor.h"
274 
278 
279 #include "TGeoMedium.h"
280 #include "TGeoMaterial.h"
281 #include "TGeoElement.h"
282 DD4HEP_INSTANTIATE_HANDLE(TGeoElement);
283 DD4HEP_INSTANTIATE_HANDLE(TGeoMaterial);
284 DD4HEP_INSTANTIATE_HANDLE(TGeoMedium);
285 
286 #include "TGeoMatrix.h"
287 DD4HEP_INSTANTIATE_HANDLE(TGeoMatrix);
288 DD4HEP_INSTANTIATE_HANDLE(TGeoRotation);
289 DD4HEP_INSTANTIATE_HANDLE(TGeoTranslation);
290 DD4HEP_INSTANTIATE_HANDLE(TGeoIdentity);
291 DD4HEP_INSTANTIATE_HANDLE(TGeoCombiTrans);
292 DD4HEP_INSTANTIATE_HANDLE(TGeoGenTrans);
293 
294 #include "TGeoNode.h"
295 DD4HEP_INSTANTIATE_HANDLE(TGeoNode);
296 DD4HEP_INSTANTIATE_HANDLE(TGeoNodeMatrix);
297 DD4HEP_INSTANTIATE_HANDLE(TGeoNodeOffset);
298 
299 // Shapes (needed by "Shapes.cpp")
300 #include "TGeoBBox.h"
301 #include "TGeoPcon.h"
302 #include "TGeoPgon.h"
303 #include "TGeoTube.h"
304 #include "TGeoCone.h"
305 #include "TGeoArb8.h"
306 #include "TGeoTrd1.h"
307 #include "TGeoTrd2.h"
308 #include "TGeoParaboloid.h"
309 #include "TGeoSphere.h"
310 #include "TGeoTorus.h"
311 #include "TGeoBoolNode.h"
312 #include "TGeoVolume.h"
313 #include "TGeoCompositeShape.h"
314 #include "TGeoShapeAssembly.h"
315 DD4HEP_INSTANTIATE_HANDLE(TGeoVolume);
316 DD4HEP_INSTANTIATE_HANDLE(TGeoBBox);
317 DD4HEP_INSTANTIATE_HANDLE(TGeoCone);
318 DD4HEP_INSTANTIATE_HANDLE(TGeoArb8);
321 DD4HEP_INSTANTIATE_HANDLE(TGeoParaboloid);
322 DD4HEP_INSTANTIATE_HANDLE(TGeoPcon);
323 DD4HEP_INSTANTIATE_HANDLE(TGeoHype);
324 DD4HEP_INSTANTIATE_HANDLE(TGeoPgon);
325 DD4HEP_INSTANTIATE_HANDLE(TGeoTube);
326 DD4HEP_INSTANTIATE_HANDLE(TGeoEltu);
327 DD4HEP_INSTANTIATE_HANDLE(TGeoTubeSeg);
328 DD4HEP_INSTANTIATE_HANDLE(TGeoTrap);
329 DD4HEP_INSTANTIATE_HANDLE(TGeoTrd1);
330 DD4HEP_INSTANTIATE_HANDLE(TGeoTrd2);
331 DD4HEP_INSTANTIATE_HANDLE(TGeoSphere);
332 DD4HEP_INSTANTIATE_HANDLE(TGeoTorus);
333 DD4HEP_INSTANTIATE_HANDLE(TGeoHalfSpace);
334 DD4HEP_INSTANTIATE_HANDLE(TGeoShape);
335 DD4HEP_INSTANTIATE_HANDLE(TGeoShapeAssembly);
336 DD4HEP_INSTANTIATE_HANDLE(TGeoCompositeShape);
337 
338 // Volume Placements (needed by "Volumes.cpp")
339 #include "TGeoPhysicalNode.h"
340 DD4HEP_INSTANTIATE_HANDLE(TGeoPhysicalNode);
341 
342 #include "TGeoBoolNode.h"
344 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoIntersection);
345 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoSubtraction);
346 
347 // Replicated Volumes (needed by "Volumes.cpp")
348 #include "TGeoPatternFinder.h"
349 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternFinder);
353 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaX);
354 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaY);
355 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaZ);
356 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternTrapZ);
357 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternCylR);
358 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternCylPhi);
359 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphR);
360 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphTheta);
361 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphPhi);
362 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternHoneycomb);
void warning_deprecated_xml_factory(const char *name)
Function tp print warning about deprecated factory usage. Used by Plugin mechanism.
Definition: Handle.cpp:246
unsigned long _multiply< unsigned long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:165
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:14
long _multiply< long >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:161
short _toShort(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:41
DDSegmentation::Segmentation _Segmentation
Definition: Handle.cpp:258
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:213
short _multiply< short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:133
unsigned short _multiply< unsigned short >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:143
unsigned char _multiply< unsigned char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:123
TGeoShape * s
Definition: Volumes.cpp:294
double _multiply< double >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:173
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
float _multiply< float >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:169
static string __to_string(T value, const char *fmt)
Definition: Handle.cpp:207
DD4HEP_INSTANTIATE_HANDLE(TNamed)
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:34
DD4HEP_INSTANTIATE_HANDLE_UNNAMED(LCDD)
bool _toBool(const std::string &value)
String conversions: string to boolean value.
Definition: Handle.cpp:89
Base class for all segmentations.
Definition: Segmentation.h:68
char _multiply< char >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:113
Intermediate class to overcome drawing probles with the TGeoTubeSeg.
Definition: Shapes.h:257
int _multiply< int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:153
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:38
unsigned int _multiply< unsigned int >(const std::string &left, const std::string &right)
Generic multiplication using the evaluator: result = left * right.
Definition: Handle.cpp:157
Evaluator of arithmetic expressions with an extendable dictionary.
Definition: Evaluator.h:38
int _toInt(const std::string &value)
String conversions: string to integer value.
Definition: Handle.cpp:57
double _toDouble(const std::string &value)
String conversions: string to double value.
Definition: Handle.cpp:103
void increment_object_validations()
Definition: Handle.cpp:243
float _toFloat(const std::string &value)
String conversions: string to float value.
Definition: Handle.cpp:93
static long s_numVerifies
Definition: Handle.cpp:238
std::string _ptrToString(const void *p, const char *fmt="%p")
Pointer to text conversion.
Definition: Handle.cpp:233
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:87
View * v
Definition: MultiView.cpp:30
long num_object_validations()
Definition: Handle.cpp:240
The main interface to the DD4hep detector description package.
Definition: LCDD.h:82
void _toDictionary(const std::string &name, const std::string &value)
Enter name value pair to the dictionary. "value" must be a numerical expression, which is evaluated...
Definition: Handle.cpp:177
virtual void setName(const std::string &value)
Set the segmentation name.
Definition: Segmentation.h:93
long _toLong(const std::string &value)
String conversions: string to long integer value.
Definition: Handle.cpp:73
XmlTools::Evaluator & evaluator()