diff --git a/DDCore/include/DD4hep/DetElement.h b/DDCore/include/DD4hep/DetElement.h index 4d2514df7609546f07202d6da548b28d678d7bc5..4ac645527f362e7d4a3eb98d088eb878ee770043 100644 --- a/DDCore/include/DD4hep/DetElement.h +++ b/DDCore/include/DD4hep/DetElement.h @@ -122,6 +122,9 @@ namespace dd4hep { /// Access an existing extension object from the detector element void* extension(unsigned long long int key) const; + /// Access an existing extension object from the detector element + void* extension(unsigned long long int key, bool alert) const; + /// Extend the sensitive detector element with an arbitrary structure accessible by the type template <typename IFACE, typename CONCRETE> IFACE* addExtension(CONCRETE* c) const { return (IFACE*) this->addExtension(detail::typeHash64<IFACE>(), @@ -284,7 +287,7 @@ namespace dd4hep { void* addExtension(unsigned long long int key,ExtensionEntry* entry) const; /// Access an existing extension object from the detector element - void* extension(unsigned long long int key) const; + void* extension(unsigned long long int key, bool alert) const; /// Extend the detector element with an arbitrary structure accessible by the type template <typename IFACE, typename CONCRETE> IFACE* addExtension(CONCRETE* c) const { @@ -294,7 +297,11 @@ namespace dd4hep { } /// Access extension element by the type template <typename IFACE> IFACE* extension() const { - return (IFACE*) this->extension(detail::typeHash64<IFACE>()); + return (IFACE*) this->extension(detail::typeHash64<IFACE>(),true); + } + /// Access extension element by the type + template <typename IFACE> IFACE* extension(bool alert) const { + return (IFACE*) this->extension(detail::typeHash64<IFACE>(),alert); } /// Extend the detector element with an arbitrary callback template <typename Q, typename T> diff --git a/DDCore/src/DetElement.cpp b/DDCore/src/DetElement.cpp index f0b67aea5627f0f0d4657200bb850bf2ee09d9fc..a00e9743ca84602767346daeeb17df1d589e46d1 100644 --- a/DDCore/src/DetElement.cpp +++ b/DDCore/src/DetElement.cpp @@ -69,8 +69,8 @@ void* DetElement::addExtension(unsigned long long int k,ExtensionEntry* e) const } /// Access an existing extension object from the detector element -void* DetElement::extension(unsigned long long int k) const { - return access()->extension(k); +void* DetElement::extension(unsigned long long int k, bool alert) const { + return access()->extension(k, alert); } /// Internal call to extend the detector element with an arbitrary structure accessible by the type @@ -447,3 +447,8 @@ void* SensitiveDetector::addExtension(unsigned long long int k,ExtensionEntry* e void* SensitiveDetector::extension(unsigned long long int k) const { return access()->extension(k); } + +/// Access an existing extension object from the detector element +void* SensitiveDetector::extension(unsigned long long int k, bool alert) const { + return access()->extension(k, alert); +} diff --git a/DDCore/src/ObjectExtensions.cpp b/DDCore/src/ObjectExtensions.cpp index dd288c2f515d68c41c49469d4b17d2d8fa30fcc0..3a5969bf1695f439ce2d46d0a4b2d6ab32c7917b 100644 --- a/DDCore/src/ObjectExtensions.cpp +++ b/DDCore/src/ObjectExtensions.cpp @@ -67,11 +67,18 @@ void ObjectExtensions::copyFrom(const map<unsigned long long int,ExtensionEntry* /// Add an extension object to the detector element void* ObjectExtensions::addExtension(unsigned long long int key, ExtensionEntry* e) { - auto j = extensions.find(key); - if (j == extensions.end()) { - return extensions[key] = e; + if ( e ) { + if ( e->object() ) { + auto j = extensions.find(key); + if (j == extensions.end()) { + extensions[key] = e; + return e->object(); + } + except("ObjectExtensions::addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str()); + } + except("ObjectExtensions::addExtension","Invalid extension object for key %016llX!",key); } - except("addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str()); + except("ObjectExtensions::addExtension","Invalid extension entry for key %016llX!",key); return 0; } @@ -87,7 +94,7 @@ void* ObjectExtensions::removeExtension(unsigned long long int key, bool destroy extensions.erase(j); return ptr; } - except("removeExtension","The object of type %016llX is not present.",key); + except("ObjectExtensions::removeExtension","The object of type %016llX is not present.",key); return 0; } @@ -97,8 +104,8 @@ void* ObjectExtensions::extension(unsigned long long int key) const { if (j != extensions.end()) { return (*j).second->object(); } - except("removeExtension","The object has no extension of type %016llX.",key); - return 0; + string msg = format("ObjectExtensions::extension","The object has no extension of type %016llX.",key); + throw runtime_error(msg); } /// Access an existing extension object from the detector element @@ -109,6 +116,6 @@ void* ObjectExtensions::extension(unsigned long long int key, bool alert) const } else if ( !alert ) return 0; - except("removeExtension","The object has no extension of type %016llX.",key); - return 0; + string msg = format("ObjectExtensions::extension","The object has no extension of type %016llX.",key); + throw runtime_error(msg); } diff --git a/DDRec/src/Surface.cpp b/DDRec/src/Surface.cpp index 478624858afe9ee7bd919e61b1d8e5149404ac15..b70cea3d298045ddd76fe93db9e86cc125df045a 100644 --- a/DDRec/src/Surface.cpp +++ b/DDRec/src/Surface.cpp @@ -509,19 +509,10 @@ namespace dd4hep { //================================================================================================================ VolSurfaceList* volSurfaceList( DetElement& det ) { - - - VolSurfaceList* list = 0 ; - - try { - - list = det.extension< VolSurfaceList >() ; - - } catch(const std::runtime_error& e){ - - list = det.addExtension<VolSurfaceList >( new VolSurfaceList ) ; + VolSurfaceList* list = det.extension< VolSurfaceList >(false); + if ( !list ) { + list = det.addExtension<VolSurfaceList >(new VolSurfaceList); } - return list ; } diff --git a/examples/SimpleDetector/src/AirTube_geo.cpp b/examples/SimpleDetector/src/AirTube_geo.cpp index b3c431edb0bdbf96ef5640f6348703c5ae0ad1ab..10cd9740512c438b20207f42b274c19ac08e1fd4 100644 --- a/examples/SimpleDetector/src/AirTube_geo.cpp +++ b/examples/SimpleDetector/src/AirTube_geo.cpp @@ -1,4 +1,3 @@ -// $Id: $ //==================================================================== // // Simple tube filled with air // // used for tracking purposes ... diff --git a/examples/SimpleDetector/src/ZPlanarTracker_geo.cpp b/examples/SimpleDetector/src/ZPlanarTracker_geo.cpp index dc4a30488bade9d96f00041e0ea7e6cfb1b7c8f9..897617d58153a1efd10e2452e7352865d15000cf 100644 --- a/examples/SimpleDetector/src/ZPlanarTracker_geo.cpp +++ b/examples/SimpleDetector/src/ZPlanarTracker_geo.cpp @@ -1,4 +1,3 @@ -// $Id: $ //==================================================================== // Simple tracking detector made from planar sensors that are parallel // to the z-axis. There are two materials per ladder: one sensitive @@ -204,7 +203,7 @@ static Ref_t create_element(Detector& description, xml_h e, SensitiveDetector se DetElement ladderDE( layerDE , laddername , x_det.id() ); ladderDE.setPlacement( pv ) ; - volSurfaceList( ladderDE )->push_back( surf ) ; + //volSurfaceList( ladderDE )->push_back( surf ) ; }