Problem 2598 - G4LogicalSkinSurface is slow due to linear search traversal
Summary: G4LogicalSkinSurface is slow due to linear search traversal
Status: RESOLVED FIXED
Alias: None
Product: Geant4
Classification: Unclassified
Component: geometry (show other problems)
Version: 11.2
Hardware: All All
: P4 enhancement
Assignee: Gabriele Cosmo
URL:
Depends on:
Blocks:
 
Reported: 2024-03-07 22:01 CET by Wouter Deconinck
Modified: 2024-03-08 08:42 CET (History)
0 users

See Also:


Attachments
G4LogicalSkinSurface.patch, modifies source/geometry/volumes/include/G4LogicalSkinSurface.hh, source/geometry/volumes/src/G4LogicalSkinSurface.cc, source/persistency/gdml/src/G4GDMLWriteStructure.cc (3.16 KB, patch)
2024-03-07 22:01 CET, Wouter Deconinck
Details | Diff

Note You need to log in before you can comment on or make changes to this problem.
Description Wouter Deconinck 2024-03-07 22:01:49 CET
Created attachment 860 [details]
G4LogicalSkinSurface.patch, modifies source/geometry/volumes/include/G4LogicalSkinSurface.hh, source/geometry/volumes/src/G4LogicalSkinSurface.cc, source/persistency/gdml/src/G4GDMLWriteStructure.cc

We (Electron-Ion Collider) have a geometry where two features in separate detectors are 'conspiring' to result in poor performance. This both concerns optical photon propagation in Cherenkov detectors.
- In our Dual RICH detector with O(20) optical photons per event, reflections on the SiPM surfaces is important, so we model this with a skin surface for each SiPM, so O(1000).
- In our DIRC detector with O(2000) optical photons per event, each photon undergoes frequent O(100) total internal reflections, but there are only O(10) skin surfaces in this detector itself.

We noticed a slowdown in our simulations only when both these detectors are simultaneously included, but not when only one is included. We tracked this down to G4LogicalSkinSurface::GetSurface where a linear search is done over the global vector with all skin surfaces everywhere. This makes sense that the large number of skin surfaces in the vector would take a long time to traverse, and we would be calling this function frequently due to sheer number of photons.

Indeed, in the attached patch we change the vector storage model for skin surface to a map storage model (as is already the case for G4LogicalBorderSurface). This allows the use of a faster std::map::find which performs in O(log n) (instead of O(n) previously).
Comment 1 Gabriele Cosmo 2024-03-08 08:42:55 CET
Thanks for suggested correction. This is now submitted in the development branch and will part of the next release/patch!