include/G4OCCT/G4OCCTSensitiveDetectorMap.hh
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later | ||
| 2 | // Copyright (C) 2026 G4OCCT Contributors | ||
| 3 | |||
| 4 | /// @file G4OCCTSensitiveDetectorMap.hh | ||
| 5 | /// @brief Declaration of G4OCCTSensitiveDetectorMap. | ||
| 6 | |||
| 7 | #ifndef G4OCCT_G4OCCTSensitiveDetectorMap_hh | ||
| 8 | #define G4OCCT_G4OCCTSensitiveDetectorMap_hh | ||
| 9 | |||
| 10 | #include <G4String.hh> | ||
| 11 | |||
| 12 | #include <cstddef> | ||
| 13 | #include <utility> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | class G4VSensitiveDetector; | ||
| 17 | |||
| 18 | /** | ||
| 19 | * @brief Maps volume name patterns to Geant4 G4VSensitiveDetector objects. | ||
| 20 | * | ||
| 21 | * Provides an ordered lookup table from volume name patterns to the | ||
| 22 | * corresponding Geant4 `G4VSensitiveDetector` pointers. Two matching | ||
| 23 | * strategies are supported (checked in insertion order; first match wins): | ||
| 24 | * | ||
| 25 | * 1. **Exact match** — `volumeName == pattern` | ||
| 26 | * 2. **Prefix match** — `volumeName` starts with `pattern + "_"` and the | ||
| 27 | * remaining suffix consists entirely of decimal digits. This handles | ||
| 28 | * Geant4's `MakeUniqueName` deduplication convention (e.g. `"Absorber_1"`, | ||
| 29 | * `"Absorber_2"` are both matched by the pattern `"Absorber"`). | ||
| 30 | * | ||
| 31 | * Most volumes in a detector are not sensitive, so `Resolve()` returns | ||
| 32 | * `nullptr` for unmatched names rather than throwing a fatal error. | ||
| 33 | * | ||
| 34 | * ### Usage | ||
| 35 | * ```cpp | ||
| 36 | * G4OCCTSensitiveDetectorMap sdMap; | ||
| 37 | * sdMap.Add("Absorber", absorberSD); | ||
| 38 | * sdMap.Add("Gap", gapSD); | ||
| 39 | * | ||
| 40 | * G4VSensitiveDetector* sd = sdMap.Resolve("Absorber_1"); // returns absorberSD | ||
| 41 | * G4VSensitiveDetector* no = sdMap.Resolve("World"); // returns nullptr | ||
| 42 | * ``` | ||
| 43 | */ | ||
| 44 | class G4OCCTSensitiveDetectorMap { | ||
| 45 | public: | ||
| 46 | 23 | G4OCCTSensitiveDetectorMap() = default; | |
| 47 | |||
| 48 | /** | ||
| 49 | * Register a mapping from a volume name pattern to a sensitive detector. | ||
| 50 | * | ||
| 51 | * If @p pattern is already registered the previous entry is silently | ||
| 52 | * overwritten. | ||
| 53 | * | ||
| 54 | * @param pattern Volume name pattern (case-sensitive). Used for both exact | ||
| 55 | * and prefix matching (see class documentation). | ||
| 56 | * @param sd Non-null pointer to the sensitive detector. A null pointer | ||
| 57 | * triggers a fatal `G4Exception` with code `G4OCCT_SDMap000`. | ||
| 58 | */ | ||
| 59 | void Add(const G4String& pattern, G4VSensitiveDetector* sd); | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Look up the sensitive detector for a given volume name. | ||
| 63 | * | ||
| 64 | * Checks each registered entry in insertion order and returns the first | ||
| 65 | * match (exact or prefix). Returns `nullptr` if no entry matches — this | ||
| 66 | * is the expected result for non-sensitive volumes. | ||
| 67 | * | ||
| 68 | * @param volumeName Geant4 logical volume name to resolve. | ||
| 69 | * @return Pointer to the matching `G4VSensitiveDetector`, or `nullptr` if | ||
| 70 | * no entry matches. | ||
| 71 | */ | ||
| 72 | G4VSensitiveDetector* Resolve(const G4String& volumeName) const; | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Return the number of registered entries. | ||
| 76 | */ | ||
| 77 | 7 | std::size_t Size() const { return fEntries.size(); } | |
| 78 | |||
| 79 | private: | ||
| 80 | std::vector<std::pair<G4String, G4VSensitiveDetector*>> fEntries; | ||
| 81 | }; | ||
| 82 | |||
| 83 | #endif // G4OCCT_G4OCCTSensitiveDetectorMap_hh | ||
| 84 |