Skip to content

Commit 40aeb96

Browse files
committed
Modernize surfaces
Make DetectorSurfaces not inherit from DetElement, use range-based for loops, pass arguments by const reference when possible, initialize class members
1 parent 16a675d commit 40aeb96

File tree

8 files changed

+66
-114
lines changed

8 files changed

+66
-114
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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ namespace dd4hep {
290290
/** Helper function for accessing the list assigned to a DetElement - attaches
291291
* empty list if needed.
292292
*/
293-
VolSurfaceList* volSurfaceList( DetElement& det) ;
293+
VolSurfaceList* volSurfaceList( const DetElement& det) ;
294294

295295
/** std::list of VolSurfaces that takes ownership.
296296
* @author F.Gaede, DESY
@@ -299,10 +299,10 @@ namespace dd4hep {
299299
*/
300300
struct VolSurfaceList : std::list< VolSurface > {
301301

302-
VolSurfaceList() {}
302+
VolSurfaceList() = default;
303303

304304
// required c'tor for extension mechanism
305-
VolSurfaceList(DetElement& det){
305+
VolSurfaceList(const DetElement& det){
306306

307307
VolSurfaceList* sL = volSurfaceList( det ) ;
308308

@@ -311,13 +311,11 @@ namespace dd4hep {
311311

312312

313313
// required c'tor for extension mechanism
314-
VolSurfaceList(const VolSurfaceList& vsl, DetElement& /*det*/ ){
314+
VolSurfaceList(const VolSurfaceList& vsl, const DetElement& /*det*/ ){
315315

316316
this->insert( this->end() , vsl.begin() , vsl.end() ) ;
317317
}
318318

319-
virtual ~VolSurfaceList() ;
320-
321319
} ;
322320

323321

@@ -518,7 +516,7 @@ namespace dd4hep {
518516

519517
public:
520518

521-
virtual ~Surface() {}
519+
virtual ~Surface() = default;
522520

523521
/** Standard c'tor initializes the surface from the parameters of the VolSurface and the
524522
* transform (placement) of the corresponding volume, if found in DetElement

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: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,19 @@ 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)

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: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,6 @@ namespace dd4hep {
507507

508508
//================================================================================================================
509509

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-
519510
SurfaceList::~SurfaceList(){
520511
if( _isOwner ) {
521512
// delete all surfaces attached to this volume
@@ -525,7 +516,7 @@ namespace dd4hep {
525516

526517
//================================================================================================================
527518

528-
VolSurfaceList* volSurfaceList( DetElement& det ) {
519+
VolSurfaceList* volSurfaceList( const DetElement& det ) {
529520
VolSurfaceList* list = det.extension< VolSurfaceList >(false);
530521
if ( !list ) {
531522
list = det.addExtension<VolSurfaceList >(new VolSurfaceList);

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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ namespace dd4hep {
3535
initialize(theDetector) ;
3636
}
3737

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

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

4541
SurfaceMapsMap::const_iterator it = _map.find( name ) ;
4642

@@ -49,36 +45,32 @@ namespace dd4hep {
4945
return & it->second ;
5046
}
5147

52-
return 0 ;
48+
return nullptr ;
5349
}
5450

5551
void SurfaceManager::initialize(const Detector& description) {
5652

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

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

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

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

67-
SurfaceHelper surfH( dets[j] ) ;
61+
SurfaceHelper surfH( det ) ;
6862

6963
const SurfaceList& detSL = surfH.surfaceList() ;
7064

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

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

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

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

0 commit comments

Comments
 (0)