Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:00

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 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 #include <DD4hep/detail/Handle.inl>
0015 #include <DD4hep/InstanceCount.h>
0016 #include <DD4hep/Printout.h>
0017 #include <Evaluator/Evaluator.h>
0018 
0019 /// C/C++ include files
0020 #include <iostream>
0021 #include <iomanip>
0022 #include <climits>
0023 #include <cstring>
0024 #include <cstdio>
0025 
0026 #if !defined(WIN32) && !defined(__ICC)
0027 #include <cxxabi.h>
0028 #endif
0029 
0030 namespace dd4hep {
0031   dd4hep::tools::Evaluator& evaluator();
0032 }
0033 
0034 namespace {
0035   const dd4hep::tools::Evaluator& eval(dd4hep::evaluator());
0036 }
0037 
0038 using namespace dd4hep;
0039 
0040 namespace   {
0041 
0042   /// Set true for backwards compatibility
0043   static bool s_allow_variable_redefine = true;
0044 
0045   ///
0046   void check_evaluation(const std::string& value, std::pair<int,double> res, std::stringstream& err)   {
0047     if ( res.first != tools::Evaluator::OK) {
0048       throw std::runtime_error("dd4hep: "+err.str()+" : value="+value+" [Evaluation error]");
0049     }
0050   }
0051 }
0052 
0053 namespace dd4hep  {
0054 
0055   /// Steer redefinition of variable re-definition during expression evaluation. returns old value
0056   bool set_allow_variable_redefine(bool value)    {
0057     bool tmp = s_allow_variable_redefine;
0058     s_allow_variable_redefine = value;
0059     return tmp;
0060   }
0061 
0062   std::pair<int, double> _toFloatingPoint(const std::string& value)   {
0063     std::stringstream err;
0064     auto result = eval.evaluate(value, err);
0065     check_evaluation(value, result, err);
0066     return result;
0067   }
0068 
0069   std::pair<int, double> _toInteger(const std::string& value)    {
0070     std::string s(value);
0071     size_t idx = s.find("(int)");
0072     if (idx != std::string::npos)
0073       s.erase(idx, 5);
0074     idx = s.find("(long)");
0075     if (idx != std::string::npos)
0076       s.erase(idx, 6);
0077     while (s[0] == ' ')
0078       s.erase(0, 1);
0079     return _toFloatingPoint(s);
0080   }
0081 
0082   short _toShort(const std::string& value) {
0083     return (short) _toInteger(value).second;
0084   }
0085 
0086   unsigned short _toUShort(const std::string& value) {
0087     return (unsigned short) _toInteger(value).second;
0088   }
0089 
0090   int _toInt(const std::string& value) {
0091     return (int) _toInteger(value).second;
0092   }
0093 
0094   unsigned int _toUInt(const std::string& value) {
0095     return (unsigned int) _toInteger(value).second;
0096   }
0097 
0098   long _toLong(const std::string& value) {
0099     return (long) _toInteger(value).second;
0100   }
0101 
0102   unsigned long _toULong(const std::string& value) {
0103     return (unsigned long) _toInteger(value).second;
0104   }
0105 
0106   bool _toBool(const std::string& value) {
0107     return value == "true" || value == "yes" || value == "True";
0108   }
0109 
0110   /// String conversions: string to float value
0111   float _toFloat(const std::string& value) {
0112     return (float) _toFloatingPoint(value).second;
0113   }
0114 
0115   /// String conversions: string to double value
0116   double _toDouble(const std::string& value) {
0117     return _toFloatingPoint(value).second;
0118   }
0119 
0120   /// Generic type conversion from string to primitive value  \ingroup DD4HEP_CORE
0121   template <typename T> T _toType(const std::string& value)    {
0122     notImplemented("Value "+value+" cannot be converted to type "+typeName(typeid(T)));
0123     return T();
0124   }
0125 
0126   /// Generic type conversion from string to primitive value
0127   template <> bool _toType<bool>(const std::string& value)  {
0128     return _toBool(value);
0129   }
0130 
0131   /// Generic type conversion from string to primitive value
0132   template <> short _toType<short>(const std::string& value)  {
0133     return _toShort(value);
0134   }
0135 
0136   /// Generic type conversion from string to primitive value
0137   template <> unsigned short _toType<unsigned short>(const std::string& value)  {
0138     return (unsigned short)_toShort(value);
0139   }
0140 
0141   /// Generic type conversion from string to primitive value
0142   template <> int _toType<int>(const std::string& value)  {
0143     return _toInt(value);
0144   }
0145 
0146   /// Generic type conversion from string to primitive value
0147   template <> unsigned int _toType<unsigned int>(const std::string& value)  {
0148     return (unsigned int)_toInt(value);
0149   }
0150 
0151   /// Generic type conversion from string to primitive value
0152   template <> long _toType<long>(const std::string& value)  {
0153     return _toLong(value);
0154   }
0155 
0156   /// Generic type conversion from string to primitive value
0157   template <> unsigned long _toType<unsigned long>(const std::string& value)  {
0158     return (unsigned long)_toLong(value);
0159   }
0160 
0161   /// Generic type conversion from string to primitive value
0162   template <> float _toType<float>(const std::string& value)  {
0163     return _toFloat(value);
0164   }
0165 
0166   /// Generic type conversion from string to primitive value
0167   template <> double _toType<double>(const std::string& value)  {
0168     return _toDouble(value);
0169   }
0170 
0171   /// Generic type conversion from string to primitive value
0172   template <> std::string _toType<std::string>(const std::string& value)  {
0173     return value;
0174   }
0175 
0176   template <> char _multiply<char>(const std::string& left, const std::string& right) {
0177     double val = _toDouble(left + "*" + right);
0178     if ( val >= double(SCHAR_MIN) && val <= double(SCHAR_MAX) )
0179       return (char) (int)val;
0180     except("_multiply<char>",
0181            "Multiplication %e = %s * %s out of bounds for conversion to char.",
0182            val, left.c_str(), right.c_str());
0183     return 0;
0184   }
0185 
0186   template <> unsigned char _multiply<unsigned char>(const std::string& left, const std::string& right) {
0187     double val = _toDouble(left + "*" + right);
0188     if ( val >= 0 && val <= double(UCHAR_MAX) )
0189       return (unsigned char) (int)val;
0190     except("_multiply<unsigned char>",
0191            "Multiplication %e = %s * %s out of bounds for conversion to unsigned char.",
0192            val, left.c_str(), right.c_str());
0193     return 0;
0194   }
0195 
0196   template <> short _multiply<short>(const std::string& left, const std::string& right) {
0197     double val = _toDouble(left + "*" + right);
0198     if ( val >= double(SHRT_MIN) && val <= double(SHRT_MAX) )
0199       return (short) val;
0200     except("_multiply<short>",
0201            "Multiplication %e = %s * %s out of bounds for conversion to short.",
0202            val, left.c_str(), right.c_str());
0203     return 0;
0204   }
0205 
0206   template <> unsigned short _multiply<unsigned short>(const std::string& left, const std::string& right) {
0207     double val = _toDouble(left + "*" + right);
0208     if ( val >= 0 && val <= double(USHRT_MAX) )
0209       return (unsigned short)val;
0210     except("_multiply<unsigned short>",
0211            "Multiplication %e = %s * %s out of bounds for conversion to unsigned short.",
0212            val, left.c_str(), right.c_str());
0213     return 0;
0214   }
0215 
0216   template <> int _multiply<int>(const std::string& left, const std::string& right) {
0217     return (int) _toDouble(left + "*" + right);
0218   }
0219 
0220   template <> unsigned int _multiply<unsigned int>(const std::string& left, const std::string& right) {
0221     return (unsigned int) _toDouble(left + "*" + right);
0222   }
0223 
0224   template <> long _multiply<long>(const std::string& left, const std::string& right) {
0225     return (long) _toDouble(left + "*" + right);
0226   }
0227 
0228   template <> unsigned long _multiply<unsigned long>(const std::string& left, const std::string& right) {
0229     return (unsigned long) _toDouble(left + "*" + right);
0230   }
0231 
0232   template <> float _multiply<float>(const std::string& left, const std::string& right) {
0233     return _toFloat(left + "*" + right);
0234   }
0235 
0236   template <> double _multiply<double>(const std::string& left, const std::string& right) {
0237     return _toDouble(left + "*" + right);
0238   }
0239 
0240   void _toDictionary(const std::string& name, const std::string& value) {
0241     _toDictionary(name, value, "number");
0242   }
0243 
0244   /// Enter name value pair to the dictionary.  \ingroup DD4HEP_CORE
0245   void _toDictionary(const std::string& name, const std::string& value, const std::string& typ)   {
0246     if ( typ == "string" )  {
0247       eval.setEnviron(name.c_str(),value.c_str());
0248       return;
0249     }
0250     else  {
0251       int status;
0252       std::stringstream err;
0253       std::string n = name, v = value;
0254       size_t idx = v.find("(int)");
0255       if (idx != std::string::npos)
0256         v.erase(idx, 5);
0257       idx = v.find("(float)");
0258       if (idx != std::string::npos)
0259         v.erase(idx, 7);
0260       while (v[0] == ' ')
0261         v.erase(0, 1);
0262       auto result = eval.evaluate(v, err);
0263       check_evaluation(v, result, err);
0264       err.str("");
0265       status = eval.setVariable(n, result.second, err);
0266       if ( status != tools::Evaluator::OK )   {
0267         std::stringstream err_msg;
0268         err_msg << "name=" << name << " value=" << value
0269                 << "  " << err.str() << " [setVariable error]";
0270         if ( status == tools::Evaluator::WARNING_EXISTING_VARIABLE )   {
0271           if ( s_allow_variable_redefine )
0272             printout(WARNING,"Evaluator","+++ Overwriting variable: "+err_msg.str());
0273           else
0274             except("Evaluator","+++ Overwriting variable: "+err_msg.str());
0275         }
0276       }
0277     }
0278   }
0279 
0280   /// Evaluate string constant using environment stored in the evaluator
0281   std::string _getEnviron(const std::string& env)   {    
0282     // We are trying to deal with the case when several variables are being replaced in the string.   
0283     size_t current_index = 0;
0284     std::stringstream processed_variable;
0285     while (true) {
0286       // Looking for the start of a variable use, with the syntax
0287       // "path1/path2/${VAR1}/path3/${VAR2}"
0288       size_t id1 = env.find("${", current_index);
0289       // variable start found, do a greedy search for the variable end 
0290       if (id1 == std::string::npos) {
0291         // In this case we did not find the ${ to indicate a start of variable,
0292         // we just copy the rest of the variable to the stringstream and exit
0293         processed_variable << env.substr(current_index);
0294         break;
0295       }
0296       size_t id2 = env.find("}", id1);
0297       if (id2 == std::string::npos) {
0298         std::runtime_error("dd4hep: Syntax error, bad variable syntax: " + env); 
0299       }
0300       processed_variable << env.substr(current_index, id1 -current_index );
0301       std::string v   = env.substr(id1, id2-id1+1);
0302       std::stringstream err;
0303       auto   ret = eval.getEnviron(v, err);
0304       // Checking that the variable lookup worked
0305       if ( ret.first != tools::Evaluator::OK) {
0306         std::cerr << v << ": " << err.str() << std::endl;
0307         throw std::runtime_error("dd4hep: Severe error during environment lookup of " + v + " " + err.str());
0308       }
0309       // Now adding the variable
0310       processed_variable << ret.second;
0311       current_index = id2 + 1;       
0312     }
0313     return processed_variable.str();
0314   }
0315 
0316   /// String manipulations: Remove unconditionally all white spaces
0317   std::string remove_whitespace(const std::string& v)    {
0318     std::string value;
0319     value.reserve(v.length()+1);
0320     for(const char* p = v.c_str(); *p; ++p)   {
0321       if ( !::isspace(*p) ) value += *p;
0322     }
0323     return value;
0324   }
0325 
0326   template <typename T> static inline std::string __to_string(T value, const char* fmt) {
0327     char text[128];
0328     ::snprintf(text, sizeof(text), fmt, value);
0329     return text;
0330   }
0331 
0332   std::string _toString(bool value) {
0333     return value ? "true" : "false";
0334   }
0335 
0336   std::string _toString(short value, const char* fmt) {
0337     return __to_string((int)value, fmt);
0338   }
0339 
0340   std::string _toString(int value, const char* fmt) {
0341     return __to_string(value, fmt);
0342   }
0343 
0344   std::string _toString(unsigned long value, const char* fmt) {
0345     return __to_string(value, fmt);
0346   }
0347 
0348   std::string _toString(float value, const char* fmt) {
0349     return __to_string(value, fmt);
0350   }
0351 
0352   std::string _toString(double value, const char* fmt) {
0353     return __to_string(value, fmt);
0354   }
0355 
0356   std::string _ptrToString(const void* value, const char* fmt) {
0357     return __to_string(value, fmt);
0358   }
0359 
0360   // ==================================================================================
0361   static long s_numVerifies = 0;
0362 
0363   long num_object_validations() {
0364     return s_numVerifies;
0365   }
0366   void increment_object_validations() {
0367     ++s_numVerifies;
0368   }
0369   void warning_deprecated_xml_factory(const char* name)   {
0370     const char* edge = "++++++++++++++++++++++++++++++++++++++++++";
0371     size_t len = std::strlen(name);
0372     std::cerr << edge << edge << edge << std::endl;
0373     std::cerr << "++  The usage of the factory: \"" << name << "\" is DEPRECATED due to naming conventions."
0374               << std::setw(53-len) << std::right << "++" << std::endl;
0375     std::cerr << "++  Please use \"DD4hep_" << name << "\" instead." << std::setw(93-len) << std::right << "++" << std::endl;
0376     std::cerr << edge << edge << edge << std::endl;
0377   }
0378 }
0379 
0380 #include <DDSegmentation/Segmentation.h>
0381 typedef DDSegmentation::Segmentation _Segmentation;
0382 namespace dd4hep {
0383   template <> void Handle<_Segmentation>::assign(_Segmentation* s, const std::string& n, const std::string&) {
0384     this->m_element = s;
0385     s->setName(n);
0386   }
0387   template <> const char* Handle<_Segmentation>::name() const {
0388     return this->m_element ? this->m_element->name().c_str() : "";
0389   }
0390   template class dd4hep::Handle<_Segmentation>;
0391 }
0392 
0393 #include <DD4hep/Detector.h>
0394 #include <TMap.h>
0395 #include <TColor.h>
0396 
0397 template class dd4hep::Handle<NamedObject>;
0398 DD4HEP_SAFE_CAST_IMPLEMENTATION(NamedObject,NamedObject)
0399 
0400 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(Detector);
0401 DD4HEP_INSTANTIATE_HANDLE_CODE(RAW,TObject,NamedObject);
0402 DD4HEP_INSTANTIATE_HANDLE_CODE(NONE,TNamed,TObject,NamedObject);
0403 
0404 #include <TGeoMedium.h>
0405 #include <TGeoMaterial.h>
0406 #include <TGeoElement.h>
0407 DD4HEP_INSTANTIATE_HANDLE(TGeoElement);
0408 DD4HEP_INSTANTIATE_HANDLE(TGeoMaterial);
0409 DD4HEP_INSTANTIATE_HANDLE(TGeoMedium);
0410 
0411 #include <TGeoMatrix.h>
0412 DD4HEP_INSTANTIATE_HANDLE(TGeoMatrix);
0413 DD4HEP_INSTANTIATE_HANDLE(TGeoRotation);
0414 DD4HEP_INSTANTIATE_HANDLE(TGeoTranslation);
0415 DD4HEP_INSTANTIATE_HANDLE(TGeoIdentity);
0416 DD4HEP_INSTANTIATE_HANDLE(TGeoCombiTrans);
0417 DD4HEP_INSTANTIATE_HANDLE(TGeoGenTrans);
0418 
0419 #include <TGeoNode.h>
0420 DD4HEP_INSTANTIATE_HANDLE_CODE(RAW,TGeoAtt);
0421 DD4HEP_INSTANTIATE_HANDLE_CODE(RAW,TAtt3D);
0422 DD4HEP_INSTANTIATE_HANDLE_CODE(RAW,TAttLine);
0423 DD4HEP_INSTANTIATE_HANDLE(TGeoNode,TGeoAtt);
0424 DD4HEP_INSTANTIATE_HANDLE(TGeoNodeMatrix);
0425 DD4HEP_INSTANTIATE_HANDLE(TGeoNodeOffset);
0426 
0427 // Shapes (needed by "Shapes.cpp")
0428 #include <TGeoBBox.h>
0429 #include <TGeoPcon.h>
0430 #include <TGeoPgon.h>
0431 #include <TGeoTube.h>
0432 #include <TGeoCone.h>
0433 #include <TGeoArb8.h>
0434 #include <TGeoTrd1.h>
0435 #include <TGeoTrd2.h>
0436 #include <TGeoParaboloid.h>
0437 #include <TGeoSphere.h>
0438 #include <TGeoTorus.h>
0439 #include <TGeoTessellated.h>
0440 #include <TGeoBoolNode.h>
0441 #include <TGeoVolume.h>
0442 #include <TGeoScaledShape.h>
0443 #include <TGeoCompositeShape.h>
0444 #include <TGeoShapeAssembly.h>
0445 #include <DD4hep/detail/ShapesInterna.h>
0446 DD4HEP_INSTANTIATE_HANDLE(TGeoVolumeAssembly,TGeoVolume,TGeoAtt);
0447 DD4HEP_INSTANTIATE_HANDLE(TGeoVolumeMulti,TGeoVolume,TGeoAtt);
0448 DD4HEP_INSTANTIATE_HANDLE(TGeoVolume,TGeoAtt,TAttLine,TAtt3D);
0449 DD4HEP_INSTANTIATE_HANDLE(TGeoShape);
0450 DD4HEP_INSTANTIATE_HANDLE(TGeoBBox,TGeoShape);
0451 
0452 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoCone);
0453 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoArb8);
0454 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoConeSeg);
0455 //DD4HEP_INSTANTIATE_SHAPE_HANDLE(MyConeSeg);
0456 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoParaboloid);
0457 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoPcon);
0458 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoPgon,TGeoPcon);
0459 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoXtru);
0460 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoScaledShape);
0461 
0462 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTube);
0463 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoHype,TGeoTube);
0464 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoEltu,TGeoTube);
0465 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTubeSeg,TGeoTube);
0466 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoCtub,TGeoTubeSeg,TGeoTube);
0467 using dd4hep::TwistedTubeObject;
0468 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TwistedTubeObject,TGeoTubeSeg);
0469 
0470 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTrap,TGeoArb8);
0471 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoGtra,TGeoArb8);
0472 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTrd1);
0473 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTrd2);
0474 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoSphere);
0475 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTorus);
0476 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoTessellated);
0477 
0478 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoHalfSpace);
0479 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoShapeAssembly);
0480 DD4HEP_INSTANTIATE_SHAPE_HANDLE(TGeoCompositeShape);
0481 
0482 // Volume Placements (needed by "Volumes.cpp")
0483 #include <TGeoPhysicalNode.h>
0484 DD4HEP_INSTANTIATE_HANDLE(TGeoPhysicalNode);
0485 
0486 #include <TGeoBoolNode.h>
0487 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoUnion);
0488 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoIntersection);
0489 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoSubtraction);
0490 
0491 // Replicated Volumes (needed by "Volumes.cpp")
0492 #include <TGeoPatternFinder.h>
0493 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternFinder);
0494 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternX);
0495 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternY);
0496 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternZ);
0497 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaX);
0498 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaY);
0499 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternParaZ);
0500 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternTrapZ);
0501 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternCylR);
0502 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternCylPhi);
0503 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphR);
0504 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphTheta);
0505 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternSphPhi);
0506 DD4HEP_INSTANTIATE_HANDLE_UNNAMED(TGeoPatternHoneycomb);