Skip to content

Commit 3b8469c

Browse files
Modernize code related to Surfaces and the SurfaceManager (#1498)
Co-authored-by: Juan Miguel Carceller <[email protected]> Co-authored-by: MarkusFrankATcernch <[email protected]>
1 parent 32ba686 commit 3b8469c

File tree

8 files changed

+149
-202
lines changed

8 files changed

+149
-202
lines changed

DDRec/include/DDRec/DetectorSurfaces.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ namespace dd4hep {
2525
* @date Apr, 10 2014
2626
* @version $Id$
2727
*/
28-
class DetectorSurfaces: virtual public DetElement {
28+
class DetectorSurfaces {
2929

3030
public:
31-
typedef DetElement DetElement;
32-
3331
DetectorSurfaces(const DetElement& e);
3432

35-
virtual ~DetectorSurfaces();
36-
3733
/// get the list of surfaces added to this DetElement
3834
const SurfaceList& surfaceList() { return *_sL ; }
3935

4036
protected :
41-
SurfaceList* _sL ;
37+
SurfaceList* _sL { nullptr };
4238

4339
/// initializes surfaces from VolSurfaces assigned to this DetElement in detector construction
44-
void initialize() ;
40+
void initialize(const DetElement& det) ;
4541

4642
};
4743

DDRec/include/DDRec/Surface.h

Lines changed: 82 additions & 84 deletions
Large diffs are not rendered by default.

DDRec/include/DDRec/SurfaceHelper.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ namespace dd4hep {
3232

3333
SurfaceHelper(const DetElement& e);
3434

35-
~SurfaceHelper();
35+
~SurfaceHelper() = default;
36+
SurfaceHelper(const SurfaceHelper&) = delete;
37+
SurfaceHelper& operator=(const SurfaceHelper&) = delete;
38+
SurfaceHelper(SurfaceHelper&&) = default;
39+
SurfaceHelper& operator=(SurfaceHelper&&) = delete;
3640

3741
/** Get the list of all surfaces added to this DetElement and all its daughters -
3842
* instantiate SurfaceHelper with description.world() to get all surfaces.

DDRec/include/DDRec/SurfaceManager.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,25 @@ namespace dd4hep {
4646
#else
4747
SurfaceManager() = delete ;
4848
#endif
49-
/// No copy constructor
50-
SurfaceManager(const SurfaceManager& copy) = delete;
5149

52-
/// Default destructor
53-
~SurfaceManager();
50+
~SurfaceManager() = default;
51+
SurfaceManager(const SurfaceManager&) = delete;
52+
SurfaceManager& operator=(const SurfaceManager&) = delete;
53+
SurfaceManager(SurfaceManager&&) = default;
54+
SurfaceManager& operator=(SurfaceManager&&) = default;
55+
5456

55-
/// No assignment operator
56-
SurfaceManager& operator=(const SurfaceManager& copy) = delete;
57-
5857
/** Get the maps of all surfaces associated to the given detector or
5958
* type of detectors, e.g. map("tracker") returns a map with all surfaces
6059
* assigned to tracking detectors. Returns 0 if no map exists.
6160
*/
62-
const SurfaceMap* map( const std::string name ) const ;
61+
const SurfaceMap* map( const std::string& name ) const ;
6362

6463

6564
///create a string with all available maps and their size (number of surfaces)
6665
std::string toString() const ;
6766

68-
protected :
67+
private :
6968

7069

7170
/// initialize all known surface maps

DDRec/src/DetectorSurfaces.cpp

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,52 @@ namespace dd4hep {
1616
namespace rec {
1717

1818

19+
DetectorSurfaces::DetectorSurfaces(dd4hep::DetElement const& e) {
1920

20-
DetectorSurfaces::DetectorSurfaces(dd4hep::DetElement const& e) : DetElement(e) , _sL( 0 ) {
21-
22-
initialize() ;
23-
}
24-
25-
DetectorSurfaces::~DetectorSurfaces(){
26-
// nothing to do: SurfaceList is added as extension
27-
// and is deleted automatically
21+
initialize(e) ;
2822
}
2923

30-
31-
void DetectorSurfaces::initialize() {
32-
33-
DetElement det = *this ;
24+
void DetectorSurfaces::initialize(dd4hep::DetElement const& det) {
3425

3526
const VolSurfaceList* vsL = volSurfaceList(det) ;
3627

37-
try {
38-
_sL = det.extension< SurfaceList >(false) ;
39-
if (not _sL) {
40-
_sL = det.addExtension<SurfaceList >( new SurfaceList( true ) ) ;
41-
}
42-
} catch(const std::exception& e) {
43-
_sL = det.addExtension<SurfaceList >( new SurfaceList( true ) ) ;
28+
_sL = det.extension< SurfaceList >(false) ;
29+
if (! _sL) {
30+
_sL = det.addExtension<SurfaceList >( new SurfaceList( true ) ) ;
4431
}
4532

46-
if( ! vsL->empty() && _sL->empty() ) { // only fill surfaces for this DetElement once
47-
48-
// std::cout << " detector " << det.name() << " id: " << det.id() << " has " << vsL->size() << " surfaces " << std::endl ;
49-
50-
// std::cout << " ------------------------- "
51-
// << " DetectorSurfaces::initialize() adding surfaces : "
52-
// << std::endl ;
53-
54-
for( VolSurfaceList::const_iterator it = vsL->begin() ; it != vsL->end() ; ++it ){
55-
56-
VolSurface volSurf = *it ;
57-
58-
Surface* surf = 0 ;
59-
60-
if( volSurf.type().isCylinder() )
61-
surf = new CylinderSurface( det, volSurf ) ;
62-
63-
else if( volSurf.type().isCone() )
64-
surf = new ConeSurface( det, volSurf ) ;
65-
66-
else
67-
surf = new Surface( det, volSurf ) ;
33+
if( ! _sL->empty() ) { // only fill surfaces for this DetElement once
34+
return ;
35+
}
6836

69-
// std::cout << " ------------------------- "
70-
// << " surface: " << *surf << std::endl
71-
// << " ------------------------- " << std::endl ;
72-
73-
_sL->push_back( surf ) ;
74-
75-
}
37+
// std::cout << " detector " << det.name() << " id: " << det.id() << " has " << vsL->size() << " surfaces " << std::endl ;
7638

39+
// std::cout << " ------------------------- "
40+
// << " DetectorSurfaces::initialize() adding surfaces : "
41+
// << std::endl ;
42+
43+
for( const auto& volSurf : *vsL ) {
44+
45+
Surface* surf = nullptr ;
46+
47+
if( volSurf.type().isCylinder() )
48+
surf = new CylinderSurface( det, volSurf ) ;
49+
50+
else if( volSurf.type().isCone() )
51+
surf = new ConeSurface( det, volSurf ) ;
52+
53+
else
54+
surf = new Surface( det, volSurf ) ;
55+
56+
// std::cout << " ------------------------- "
57+
// << " surface: " << *surf << std::endl
58+
// << " ------------------------- " << std::endl ;
59+
60+
_sL->push_back( surf ) ;
7761

7862
}
7963

8064
}
81-
8265

8366

8467
} // namespace

DDRec/src/Surface.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <cmath>
2020
#include <memory>
21-
#include <exception>
2221

2322
#include "TGeoMatrix.h"
2423
#include "TGeoShape.h"
@@ -70,8 +69,8 @@ namespace dd4hep {
7069
return g ;
7170
}
7271

73-
const IMaterial& VolSurfaceBase::innerMaterial() const{ return _innerMat ; }
74-
const IMaterial& VolSurfaceBase::outerMaterial() const { return _outerMat ; }
72+
const IMaterial& VolSurfaceBase::innerMaterial() const { return _innerMat ; }
73+
const IMaterial& VolSurfaceBase::outerMaterial() const { return _outerMat ; }
7574
double VolSurfaceBase::innerThickness() const { return _th_i ; }
7675
double VolSurfaceBase::outerThickness() const { return _th_o ; }
7776

@@ -507,15 +506,6 @@ namespace dd4hep {
507506

508507
//================================================================================================================
509508

510-
511-
VolSurfaceList::~VolSurfaceList(){
512-
// // delete all surfaces attached to this volume
513-
// for( VolSurfaceList::iterator i=begin(), n=end() ; i !=n ; ++i ) {
514-
// i->clear() ;
515-
// }
516-
}
517-
//=======================================================
518-
519509
SurfaceList::~SurfaceList(){
520510
if( _isOwner ) {
521511
// delete all surfaces attached to this volume
@@ -525,7 +515,7 @@ namespace dd4hep {
525515

526516
//================================================================================================================
527517

528-
VolSurfaceList* volSurfaceList( DetElement& det ) {
518+
VolSurfaceList* volSurfaceList( const DetElement& det ) {
529519
VolSurfaceList* list = det.extension< VolSurfaceList >(false);
530520
if ( !list ) {
531521
list = det.addExtension<VolSurfaceList >(new VolSurfaceList);
@@ -627,7 +617,7 @@ namespace dd4hep {
627617

628618
const IMaterial& mat = _volSurf.innerMaterial() ;
629619

630-
if( ! ( mat.Z() > 0 ) ) {
620+
if( mat.Z() <= 0 ) {
631621

632622
MaterialManager matMgr( _det.placement().volume() ) ;
633623

@@ -646,7 +636,7 @@ namespace dd4hep {
646636

647637
const IMaterial& mat = _volSurf.outerMaterial() ;
648638

649-
if( ! ( mat.Z() > 0 ) ) {
639+
if( mat.Z() <= 0 ) {
650640

651641
MaterialManager matMgr( _det.placement().volume() ) ;
652642

@@ -750,7 +740,7 @@ namespace dd4hep {
750740

751741
//---- if the volSurface is not in the DetElement's volume, we need to mutliply the path to the volume to the
752742
// DetElements world transform
753-
for( std::list<PlacedVolume>::iterator it = ++( pVList.begin() ) , n = pVList.end() ; it != n ; ++it ){
743+
for( auto it = std::next(pVList.begin()) ; it != pVList.end() ; ++it ) {
754744

755745
PlacedVolume pvol = *it ;
756746
TGeoMatrix* m = pvol->GetMatrix();

DDRec/src/SurfaceHelper.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ namespace dd4hep {
2929
initialize() ;
3030
}
3131

32-
SurfaceHelper::~SurfaceHelper(){
33-
// nothing to do
34-
}
35-
36-
3732
void SurfaceHelper::initialize() {
3833

3934
// have to populate the volume manager once in order to have
@@ -50,11 +45,9 @@ namespace dd4hep {
5045

5146
while( ! daugs.empty() ) {
5247

53-
for( std::list< DetElement >::iterator li=daugs.begin() ; li != daugs.end() ; ++li ){
54-
DetElement dau = *li ;
55-
DetElement::Children chMap = dau.children() ;
56-
for ( DetElement::Children::const_iterator it=chMap.begin() ; it != chMap.end() ; ++it ){
57-
DetElement de = (*it).second ;
48+
for( const auto& dag : daugs ){
49+
const DetElement::Children& chMap = dag.children() ;
50+
for ( const auto& [_, de] : chMap ){
5851
gdaugs.push_back( de ) ;
5952
}
6053
}
@@ -65,12 +58,8 @@ namespace dd4hep {
6558

6659
// std::cout << " **** SurfaceHelper::initialize() : # DetElements found " << dets.size() << std::endl ;
6760

68-
for( std::list< DetElement >::iterator li=dets.begin() ; li != dets.end() ; ++li ) {
61+
for( const auto& det : dets) {
6962

70-
DetElement det = (*li) ;
71-
72-
73-
7463
// create surfaces
7564
DetectorSurfaces ds( det ) ;
7665

DDRec/src/SurfaceManager.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
#include <sstream>
2020

2121
namespace dd4hep {
22-
23-
using namespace detail ;
24-
2522
namespace rec {
26-
2723

2824
SurfaceManager::SurfaceManager(const Detector& theDetector){
2925

@@ -35,12 +31,8 @@ namespace dd4hep {
3531
initialize(theDetector) ;
3632
}
3733

38-
SurfaceManager::~SurfaceManager(){
39-
// nothing to do
40-
}
41-
4234

43-
const SurfaceMap* SurfaceManager::map( const std::string name ) const {
35+
const SurfaceMap* SurfaceManager::map( const std::string& name ) const {
4436

4537
SurfaceMapsMap::const_iterator it = _map.find( name ) ;
4638

@@ -49,36 +41,32 @@ namespace dd4hep {
4941
return & it->second ;
5042
}
5143

52-
return 0 ;
44+
return nullptr ;
5345
}
5446

5547
void SurfaceManager::initialize(const Detector& description) {
5648

57-
const std::vector<std::string>& types = description.detectorTypes() ;
58-
59-
for(unsigned i=0,N=types.size();i<N;++i){
49+
for(const auto& type : description.detectorTypes()) {
6050

61-
const std::vector<DetElement>& dets = description.detectors( types[i] ) ;
51+
const std::vector<DetElement>& dets = description.detectors( type ) ;
6252

63-
for(unsigned j=0,M=dets.size();j<M;++j){
53+
for(const auto& det : dets) {
6454

65-
std::string name = dets[j].name() ;
55+
const std::string& name = det.name() ;
6656

67-
SurfaceHelper surfH( dets[j] ) ;
57+
SurfaceHelper surfH( det ) ;
6858

6959
const SurfaceList& detSL = surfH.surfaceList() ;
7060

7161
// add an empty map for this detector in case there are no surfaces attached
7262
_map.emplace(name , SurfaceMap());
7363

74-
for( SurfaceList::const_iterator it = detSL.begin() ; it != detSL.end() ; ++it ){
75-
ISurface* surf = *it ;
76-
64+
for( auto* surf : detSL ) {
7765
// enter surface into map for this detector
7866
_map[ name ].emplace(surf->id(), surf );
7967

8068
// enter surface into map for detector type
81-
_map[ types[i] ].emplace(surf->id(), surf );
69+
_map[ type ].emplace(surf->id(), surf );
8270

8371
// enter surface into world map
8472
_map[ "world" ].emplace(surf->id(), surf );

0 commit comments

Comments
 (0)