Back to home page

EIC code displayed by LXR

 
 

    


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

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 // Framework include files
0015 #include <DD4hep/OpaqueDataBinder.h>
0016 #include <DD4hep/Conditions.h>
0017 #include <DD4hep/detail/ConditionsInterna.h>
0018 
0019 // C/C++ include files
0020 #include <set>
0021 #include <map>
0022 #include <list>
0023 #include <vector>
0024 
0025 namespace {
0026 
0027   /// Helper class to bind string values to C++ data objects (primitive or complex)
0028   template <typename T, typename Q> bool __bind__(const dd4hep::detail::ValueBinder&, T& object, const std::string& val, const Q*)
0029   {  object.template bind<Q>(val);                 return true;  }
0030 
0031   /// Helper class to bind string values to a STL vector of data objects (primitive or complex)
0032   template <typename T, typename Q> bool __bind__(const dd4hep::detail::VectorBinder&, T& object, const std::string& val, const Q*)
0033   {  object.template bind<std::vector<Q> >(val);   return true;  }
0034 
0035   /// Helper class to bind string values to a STL list of data objects (primitive or complex)
0036   template <typename T, typename Q> bool __bind__(const dd4hep::detail::ListBinder&, T& object, const std::string& val, const Q*)
0037   {  object.template bind<std::list<Q> >(val);     return true;  }
0038 
0039   /// Helper class to bind string values to a STL set of data objects (primitive or complex)
0040   template <typename T, typename Q> bool __bind__(const dd4hep::detail::SetBinder&, T& object, const std::string& val, const Q*)
0041   {  object.template bind<std::set<Q> >(val);      return true;  }
0042 
0043   /// Helper class to bind STL map objects
0044   template <typename T, typename Q> bool __bind__(const dd4hep::detail::MapBinder&, T& object, const Q*)
0045   {  object.template bind<Q>();                    return true;  }
0046 
0047 }
0048 
0049 /// Namespace for the AIDA detector description toolkit
0050 namespace dd4hep {
0051 
0052   /// DD4hep internal namespace declaration for utilities and implementation details
0053   namespace detail  {
0054 
0055     /// Binding function for scalar items. See the implementation function for the concrete instantiations
0056     template <typename BINDER, typename T> 
0057     bool OpaqueDataBinder::bind(const BINDER& b, T& object, const std::string& typ, const std::string& val)  {
0058 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0059       if ( typ.substr(0,4) == "char" )
0060         return __bind__(b,object,val,Primitive<char>::null_pointer());
0061       else if ( typ.substr(0,13) == "unsigned char" )
0062         return __bind__(b,object,val,Primitive<unsigned char>::null_pointer());
0063       else if ( typ.substr(0,5) == "short" )
0064         return __bind__(b,object,val,Primitive<short>::null_pointer());
0065       else if ( typ.substr(0,14) == "unsigned short" )
0066         return __bind__(b,object,val,Primitive<unsigned short>::null_pointer());
0067       else if ( typ.substr(0,12) == "unsigned int" )
0068         return __bind__(b,object,val,Primitive<unsigned int>::null_pointer());
0069       else if ( typ.substr(0,13) == "unsigned long" )
0070         return __bind__(b,object,val,Primitive<unsigned long>::null_pointer());
0071 #else
0072       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0073       if ( typ.substr(0,4) == "char" )
0074         return __bind__(b,object,val,Primitive<int>::null_pointer());
0075       else if ( typ.substr(0,5) == "short" )
0076         return __bind__(b,object,val,Primitive<int>::null_pointer());
0077 #endif
0078       else if ( typ.substr(0,3) == "int" )
0079         return __bind__(b,object,val,Primitive<int>::null_pointer());
0080       else if ( typ.substr(0,4) == "long" ) 
0081         return __bind__(b,object,val,Primitive<long>::null_pointer());
0082       else if ( typ.substr(0,5) == "float" )
0083         return __bind__(b,object,val,Primitive<float>::null_pointer());
0084       else if ( typ.substr(0,6) == "double" )
0085         return __bind__(b,object,val,Primitive<double>::null_pointer());
0086       else if ( typ.substr(0,6) == "string" )
0087         return __bind__(b,object,val,Primitive<std::string>::null_pointer());
0088       else if ( typ == "std::string" )
0089         return __bind__(b,object,val,Primitive<std::string>::null_pointer());
0090       else if ( typ == "Histo1D" )
0091         return __bind__(b,object,val,Primitive<std::string>::null_pointer());
0092       else if ( typ == "Histo2D" )
0093         return __bind__(b,object,val,Primitive<std::string>::null_pointer());
0094       else
0095         printout(INFO,"OpaqueDataBinder","++ Unknown conditions parameter type:%s val:%s",typ.c_str(),val.c_str());
0096       return __bind__(b,object,val,Primitive<std::string>::null_pointer());
0097     }
0098     template bool OpaqueDataBinder::bind<ValueBinder,OpaqueDataBlock>(        const ValueBinder& b, OpaqueDataBlock& object,
0099                                                                               const std::string& typ, const std::string& val);
0100     template bool OpaqueDataBinder::bind<VectorBinder,OpaqueDataBlock>(       const VectorBinder& b, OpaqueDataBlock& object,
0101                                                                               const std::string& typ, const std::string& val);
0102     template bool OpaqueDataBinder::bind<ListBinder,OpaqueDataBlock>(         const ListBinder& b, OpaqueDataBlock& object,
0103                                                                               const std::string& typ, const std::string& val);
0104     template bool OpaqueDataBinder::bind<SetBinder,OpaqueDataBlock>(          const SetBinder& b, OpaqueDataBlock& object,
0105                                                                               const std::string& typ, const std::string& val);
0106 
0107     template bool OpaqueDataBinder::bind<ValueBinder,Condition>(  const ValueBinder& b, Condition& object,
0108                                                                   const std::string& typ, const std::string& val);
0109     template bool OpaqueDataBinder::bind<VectorBinder,Condition>( const VectorBinder& b, Condition& object,
0110                                                                   const std::string& typ, const std::string& val);
0111     template bool OpaqueDataBinder::bind<ListBinder,Condition>(   const ListBinder& b, Condition& object,
0112                                                                   const std::string& typ, const std::string& val);
0113     template bool OpaqueDataBinder::bind<SetBinder,Condition>(    const SetBinder& b, Condition& object,
0114                                                                   const std::string& typ, const std::string& val);
0115   
0116     /// Binding function for sequences (unmapped STL containers)
0117     template <typename T> 
0118     bool OpaqueDataBinder::bind_sequence(T& object, const std::string& typ, const std::string& val)
0119     {
0120       std::size_t idx = typ.find('[');
0121       std::size_t idq = typ.find(']');
0122       std::string value_type = typ.substr(idx+1,idq-idx-1);
0123       if ( typ.substr(0,6) == "vector" )
0124         return bind(VectorBinder(), object, value_type, val);
0125       else if ( typ.substr(0,4) == "list" )
0126         return bind(ListBinder(), object, value_type, val);
0127       else if ( typ.substr(0,3) == "set" )
0128         return bind(SetBinder(), object, value_type, val);
0129       else if ( idx == std::string::npos && idq == std::string::npos )
0130         return bind(ValueBinder(), object, value_type, val);
0131       return false;
0132     }
0133   
0134     template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
0135     static void emplace_map_item(const BINDER&,
0136                                  OBJECT& object,
0137                                  const KEY& k,
0138                                  const std::string& val,
0139                                  const VAL*)
0140     {
0141       typedef std::map<KEY,VAL> map_t;
0142       map_t& m = object.template get<map_t>();
0143       VAL v;
0144       if ( !BasicGrammar::instance<VAL>().fromString(&v, val) )  {
0145         except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
0146       }
0147       m.emplace(k,v);
0148     }
0149 
0150     template<typename BINDER, typename OBJECT, typename KEY> 
0151     static void emplace_map_key(const BINDER& b,
0152                                 OBJECT& object,
0153                                 const std::string& key_val,
0154                                 const std::string& val_type,
0155                                 const std::string& val,
0156                                 const KEY*)
0157     {
0158       KEY key;
0159       BasicGrammar::instance<KEY>().fromString(&key, key_val);
0160       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0161       if ( val_type.substr(0,4) == "char" )
0162         emplace_map_item(b, object, key, val, (int*)0);
0163       else if ( val_type.substr(0,5) == "short" )
0164         emplace_map_item(b, object, key, val, (int*)0);
0165       else if ( val_type.substr(0,3) == "int" )
0166         emplace_map_item(b, object, key, val, (int*)0);
0167       else if ( val_type.substr(0,4) == "long" )
0168         emplace_map_item(b, object, key, val, (long*)0);
0169       else if ( val_type.substr(0,5) == "float" )
0170         emplace_map_item(b, object, key, val, (float*)0);
0171       else if ( val_type.substr(0,6) == "double" )
0172         emplace_map_item(b, object, key, val, (double*)0);
0173       else if ( val_type.substr(0,6) == "string" )
0174         emplace_map_item(b, object, key, val, (std::string*)0);
0175       else if ( val_type == "std::string" )
0176         emplace_map_item(b, object, key, val, (std::string*)0);
0177       else {
0178         printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
0179                  val_type.c_str(),val.c_str());
0180         emplace_map_item(b, object, key, val, (std::string*)0);
0181       }
0182     }
0183 
0184     template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
0185     static void emplace_map_pair(const BINDER&,
0186                                  OBJECT& object,
0187                                  const std::string& data,
0188                                  const KEY*,
0189                                  const VAL*)
0190     {
0191       typedef std::map<KEY,VAL> map_t;
0192       std::pair<KEY,VAL> entry;
0193       map_t& m = object.template get<map_t>();
0194       if ( !BasicGrammar::instance<std::pair<KEY,VAL> >().fromString(&entry,data) )  {
0195         except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
0196       }
0197       m.emplace(entry);
0198     }
0199 
0200     template<typename BINDER, typename OBJECT, typename KEY> 
0201     static void emplace_map_data(const BINDER& b,
0202                                  OBJECT& object,
0203                                  const std::string& val_type,
0204                                  const std::string& pair_data,
0205                                  const KEY*)
0206     {
0207       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0208       if ( val_type.substr(0,4) == "char" )
0209         emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
0210       else if ( val_type.substr(0,5) == "short" )
0211         emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
0212       else if ( val_type.substr(0,3) == "int" )
0213         emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
0214       else if ( val_type.substr(0,4) == "long" )
0215         emplace_map_pair(b, object, pair_data, (KEY*)0, (long*)0);
0216       else if ( val_type.substr(0,5) == "float" )
0217         emplace_map_pair(b, object, pair_data, (KEY*)0, (float*)0);
0218       else if ( val_type.substr(0,6) == "double" )
0219         emplace_map_pair(b, object, pair_data, (KEY*)0, (double*)0);
0220       else if ( val_type.substr(0,6) == "string" )
0221         emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
0222       else if ( val_type == "std::string" )
0223         emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
0224       else {
0225         printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
0226                  val_type.c_str(),pair_data.c_str());
0227         emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
0228       }
0229     }
0230 
0231     template<typename BINDER, typename OBJECT, typename KEY> 
0232     static void bind_mapping(const BINDER& b, const std::string& val_type, OBJECT& object, const KEY*)   {
0233       if ( val_type.substr(0,3) == "int" )
0234         __bind__(b,object, (std::map<KEY,int>*)0);
0235 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0236       else if ( val_type.substr(0,12) == "unsigned int" )
0237         __bind__(b,object, (std::map<KEY,unsigned int>*)0);
0238       else if ( val_type.substr(0,4) == "char" )
0239         __bind__(b,object, (std::map<KEY,char>*)0);
0240       else if ( val_type.substr(0,13) == "unsigned char" )
0241         __bind__(b,object, (std::map<KEY,unsigned char>*)0);
0242       else if ( val_type.substr(0,5) == "short" )
0243         __bind__(b,object, (std::map<KEY,short>*)0);
0244       else if ( val_type.substr(0,14) == "unsigned short" )
0245         __bind__(b,object, (std::map<KEY,unsigned short>*)0);
0246       else if ( val_type.substr(0,13) == "unsigned long" )
0247         __bind__(b,object, (std::map<KEY,unsigned long>*)0);
0248 #else
0249       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0250       else if ( val_type.substr(0,4) == "char" )
0251         __bind__(b,object, (std::map<KEY,int>*)0);
0252       else if ( val_type.substr(0,5) == "short" )
0253         __bind__(b,object, (std::map<KEY,int>*)0);
0254 #endif
0255       else if ( val_type.substr(0,4) == "long" )
0256         __bind__(b,object, (std::map<KEY,long>*)0);
0257       else if ( val_type.substr(0,5) == "float" )
0258         __bind__(b,object, (std::map<KEY,float>*)0);
0259       else if ( val_type.substr(0,6) == "double" )
0260         __bind__(b,object, (std::map<KEY,double>*)0);
0261       else if ( val_type.substr(0,6) == "string" )
0262         __bind__(b,object, (std::map<KEY,std::string>*)0);
0263       else if ( val_type == "std::string" )
0264         __bind__(b,object, (std::map<KEY,std::string>*)0);
0265       else {
0266         __bind__(b,object, (std::map<KEY,std::string>*)0);
0267       }
0268     }
0269   
0270     /// Binding function for STL maps
0271     template <typename BINDER, typename OBJECT> 
0272     bool OpaqueDataBinder::bind_map(const BINDER& b, OBJECT& object,
0273                                     const std::string& key_type, const std::string& val_type)   {
0274       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0275       if ( key_type.substr(0,3) == "int" )
0276         bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
0277 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0278       else if ( key_type.substr(0,4) == "char" )
0279         bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
0280       else if ( key_type.substr(0,5) == "short" )
0281         bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
0282       else if ( key_type.substr(0,4) == "long" )
0283         bind_mapping(b, val_type, object, Primitive<long>::null_pointer());
0284       else if ( key_type.substr(0,5) == "float" )
0285         bind_mapping(b, val_type, object, Primitive<float>::null_pointer());
0286       else if ( key_type.substr(0,6) == "double" )
0287         bind_mapping(b, val_type, object, Primitive<double>::null_pointer());
0288 #endif
0289       else if ( key_type.substr(0,6) == "string" )
0290         bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
0291       else if ( key_type == "std::string" )
0292         bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
0293       else {
0294         printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
0295         bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
0296       }
0297       return true;
0298     }
0299 
0300     /// Filling function for STL maps.
0301     template <typename BINDER, typename OBJECT>
0302     bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
0303                                       const std::string& key_type, const std::string& key,
0304                                       const std::string& val_type, const std::string& val)
0305     {
0306       if ( key_type.substr(0,3) == "int" )
0307         emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
0308 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0309       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0310       else if ( key_type.substr(0,4) == "char" )
0311         emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
0312       else if ( key_type.substr(0,5) == "short" )
0313         emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
0314       else if ( key_type.substr(0,4) == "long" )
0315         emplace_map_key(b, object, key, val_type, val, Primitive<long>::null_pointer());
0316       else if ( key_type.substr(0,5) == "float" )
0317         emplace_map_key(b, object, key, val_type, val, Primitive<float>::null_pointer());
0318       else if ( key_type.substr(0,6) == "double" )
0319         emplace_map_key(b, object, key, val_type, val, Primitive<double>::null_pointer());
0320 #endif
0321       else if ( key_type.substr(0,6) == "string" )
0322         emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
0323       else if ( key_type == "std::string" )
0324         emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
0325       else {
0326         printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
0327         emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
0328       }
0329       return true;
0330     }
0331 
0332     /// Filling function for STL maps.
0333     template <typename BINDER, typename OBJECT> 
0334     bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
0335                                       const std::string& key_type, const std::string& val_type,
0336                                       const std::string& pair_data)
0337     {
0338       if ( key_type.substr(0,3) == "int" )
0339         emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
0340 #if defined(DD4HEP_HAVE_ALL_PARSERS)
0341       // Short and char is not part of the standard dictionaries. Fall back to 'int'.
0342       else if ( key_type.substr(0,4) == "char" )
0343         emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
0344       else if ( key_type.substr(0,5) == "short" )
0345         emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
0346       else if ( key_type.substr(0,4) == "long" )
0347         emplace_map_data(b, object, val_type, pair_data, Primitive<long>::null_pointer());
0348       else if ( key_type.substr(0,5) == "float" )
0349         emplace_map_data(b, object, val_type, pair_data, Primitive<float>::null_pointer());
0350       else if ( key_type.substr(0,6) == "double" )
0351         emplace_map_data(b, object, val_type, pair_data, Primitive<double>::null_pointer());
0352 #endif
0353       else if ( key_type.substr(0,6) == "string" )
0354         emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
0355       else if ( key_type == "std::string" )
0356         emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
0357       else {
0358         printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
0359         emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
0360       }
0361       return true;
0362     }
0363 
0364     template bool OpaqueDataBinder::bind_sequence<OpaqueDataBlock>(OpaqueDataBlock& object,const std::string& typ,const std::string& val);
0365     template bool OpaqueDataBinder::bind_map<MapBinder,OpaqueDataBlock>(    const MapBinder& b, OpaqueDataBlock& object,
0366                                                                             const std::string& typ,const std::string& val);
0367     template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>(  const MapBinder& b, OpaqueDataBlock& object,
0368                                                                             const std::string& key_type, const std::string& key,
0369                                                                             const std::string& val_type, const std::string& val);
0370     template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>(  const MapBinder& b, OpaqueDataBlock& object,
0371                                                                             const std::string& key_type, const std::string& val_type,
0372                                                                             const std::string& pair_data);
0373 
0374     /// Instantiation for Conditions:
0375     template bool OpaqueDataBinder::bind_sequence<Condition>(   Condition& object,
0376                                                                 const std::string& typ,const std::string& val);
0377     /// Conditions binding function for STL maps
0378     template <> bool OpaqueDataBinder::bind_map(const MapBinder& b, Condition& object,
0379                                                 const std::string& key_type, const std::string& val_type)
0380     {    return bind_map(b, object->data, key_type, val_type);  }
0381 
0382     /// Conditions: Filling function for STL maps.
0383     template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Condition& object,
0384                                                   const std::string& key_type, const std::string& key,
0385                                                   const std::string& val_type, const std::string& val)
0386     {    return insert_map(b, object->data, key_type, key, val_type, val);    }
0387 
0388     /// Conditions: Filling function for STL maps.
0389     template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Condition& object,
0390                                                   const std::string& key_type, const std::string& val_type, const std::string& pair_data)
0391     {    return insert_map(b, object->data, key_type, val_type, pair_data);   }  
0392   }
0393 }