Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4LogicalVolumeStore Class Reference

G4LogicalVolumeStore is a singleton class, acting as container for all logical volumes, with functionality derived from std::vector<T>. All logical volumes should be registered with G4LogicalVolumeStore, and removed on their destruction. The underlying container initially has a capacity of 100. A map indexed by volume names is also recorded for fast search; pointers to volumes with same name are stored in buckets. More...

#include <G4LogicalVolumeStore.hh>

Inheritance diagram for G4LogicalVolumeStore:

Public Member Functions

virtual ~G4LogicalVolumeStore ()
 G4LogicalVolumeStore (const G4LogicalVolumeStore &)=delete
G4LogicalVolumeStoreoperator= (const G4LogicalVolumeStore &)=delete
G4LogicalVolumeGetVolume (const G4String &name, G4bool verbose=true, G4bool reverseSearch=false) const
G4bool IsMapValid () const
void SetMapValid (G4bool val)
const std::map< G4String, std::vector< G4LogicalVolume * > > & GetMap () const
void UpdateMap ()

Static Public Member Functions

static void Register (G4LogicalVolume *pVolume)
static void DeRegister (G4LogicalVolume *pVolume)
static G4LogicalVolumeStoreGetInstance ()
static void SetNotifier (G4VStoreNotifier *pNotifier)
static void Clean ()

Protected Member Functions

 G4LogicalVolumeStore ()

Detailed Description

G4LogicalVolumeStore is a singleton class, acting as container for all logical volumes, with functionality derived from std::vector<T>. All logical volumes should be registered with G4LogicalVolumeStore, and removed on their destruction. The underlying container initially has a capacity of 100. A map indexed by volume names is also recorded for fast search; pointers to volumes with same name are stored in buckets.

Definition at line 64 of file G4LogicalVolumeStore.hh.

Constructor & Destructor Documentation

◆ ~G4LogicalVolumeStore()

G4LogicalVolumeStore::~G4LogicalVolumeStore ( )
virtual

Destructor: takes care to delete allocated logical volumes.

Definition at line 65 of file G4LogicalVolumeStore.cc.

66{
67 Clean(); // Delete all volumes in the store
68 G4LogicalVolume::Clean(); // Delete allocated sub-instance data
69}
static void Clean()

◆ G4LogicalVolumeStore() [1/2]

G4LogicalVolumeStore::G4LogicalVolumeStore ( const G4LogicalVolumeStore & )
delete

Copy constructor and assignment operator not allowed.

Referenced by Clean(), DeRegister(), G4LogicalVolumeStore(), GetInstance(), GetVolume(), operator=(), and Register().

◆ G4LogicalVolumeStore() [2/2]

G4LogicalVolumeStore::G4LogicalVolumeStore ( )
protected

Protected singleton constructor.

Definition at line 55 of file G4LogicalVolumeStore.cc.

57{
58 reserve(100);
59}

Member Function Documentation

◆ Clean()

void G4LogicalVolumeStore::Clean ( )
static

Deletes all volumes from the store.

Definition at line 75 of file G4LogicalVolumeStore.cc.

76{
77 // Do nothing if geometry is closed
78 //
79 if (G4GeometryManager::GetInstance()->IsGeometryClosed())
80 {
81 G4cout << "WARNING - Attempt to delete the logical volume store"
82 << " while geometry closed !" << G4endl;
83 return;
84 }
85
86 // Locks store for deletion of volumes. De-registration will be
87 // performed at this stage. G4LogicalVolumes will not de-register themselves.
88 //
89 locked = true;
90
92
93 for(const auto & pos : *store)
94 {
95 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
96 if (pos != nullptr) { pos->Lock(); delete pos; }
97 }
98
99 store->bmap.clear(); store->mvalid = false;
100 locked = false;
101 store->clear();
102}
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
static G4GeometryManager * GetInstance()
G4LogicalVolumeStore(const G4LogicalVolumeStore &)=delete
static G4LogicalVolumeStore * GetInstance()

Referenced by G4RunManager::ReinitializeGeometry(), and ~G4LogicalVolumeStore().

◆ DeRegister()

void G4LogicalVolumeStore::DeRegister ( G4LogicalVolume * pVolume)
static

Removes the logical volume 'pVolume' from the collection.

Definition at line 168 of file G4LogicalVolumeStore.cc.

169{
171 if (!locked) // Do not de-register if locked !
172 {
173 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
174 for (auto i=store->cbegin(); i!=store->cend(); ++i)
175 {
176 if (**i==*pVolume)
177 {
178 store->erase(i);
179 break;
180 }
181 }
182 const G4String& vol_name = pVolume->GetName();
183 auto it = store->bmap.find(vol_name);
184 if (it != store->bmap.cend())
185 {
186 if (it->second.size() > 1)
187 {
188 for (auto i=it->second.cbegin(); i!=it->second.cend(); ++i)
189 {
190 if (**i==*pVolume)
191 {
192 it->second.erase(i);
193 break;
194 }
195 }
196 }
197 else
198 {
199 store->bmap.erase(it);
200 }
201 }
202 }
203}
const G4String & GetName() const

Referenced by G4LogicalVolume::~G4LogicalVolume().

◆ GetInstance()

◆ GetMap()

const std::map< G4String, std::vector< G4LogicalVolume * > > & G4LogicalVolumeStore::GetMap ( ) const
inline

Returns the internal map.

Definition at line 126 of file G4LogicalVolumeStore.hh.

126{ return bmap; }

Referenced by G4VUserDetectorConstruction::SetSensitiveDetector(), and G4VUserParallelWorld::SetSensitiveDetector().

◆ GetVolume()

G4LogicalVolume * G4LogicalVolumeStore::GetVolume ( const G4String & name,
G4bool verbose = true,
G4bool reverseSearch = false ) const

Returns a pointer to the first or last volume in the collection having that 'name'. Uses the internal map for fast search and warns if a volume in the collection is not unique or not found.

Parameters
[in]nameThe name of the volume to search.
[in]verboseFlag for enabling verbosity (default true).
[in]reverseSearchFlag to enable inverse search (default false).

Definition at line 210 of file G4LogicalVolumeStore.cc.

212{
214 if (!store->mvalid) { store->UpdateMap(); }
215 auto pos = store->bmap.find(name);
216 if(pos != store->bmap.cend())
217 {
218 if ((verbose) && (pos->second.size()>1))
219 {
220 std::ostringstream message;
221 message << "There exists more than ONE logical volume in store named: "
222 << name << "!" << G4endl
223 << "Returning the first found.";
224 G4Exception("G4LogicalVolumeStore::GetVolume()",
225 "GeomMgt1001", JustWarning, message);
226 }
227 if(reverseSearch)
228 {
229 return pos->second[pos->second.size()-1];
230 }
231 return pos->second[0];
232 }
233 if (verbose)
234 {
235 std::ostringstream message;
236 message << "Volume NOT found in store !" << G4endl
237 << " Volume " << name << " NOT found in store !" << G4endl
238 << " Returning NULL pointer.";
239 G4Exception("G4LogicalVolumeStore::GetVolume()",
240 "GeomMgt1001", JustWarning, message);
241 }
242 return nullptr;
243}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
const char * name(G4int ptype)

Referenced by G4VRadioactiveDecay::DeselectAVolume(), G4GDMLReadStructure::GetVolume(), G4VRadioactiveDecay::SelectAVolume(), G4GDMLMessenger::SetNewValue(), G4VisCommandSceneAddLogicalVolume::SetNewValue(), and G4ScoringProbe::SetupGeometry().

◆ IsMapValid()

G4bool G4LogicalVolumeStore::IsMapValid ( ) const
inline

Accessor and modifier to assess validity of the internal map.

Definition at line 119 of file G4LogicalVolumeStore.hh.

119{ return mvalid; }

◆ operator=()

G4LogicalVolumeStore & G4LogicalVolumeStore::operator= ( const G4LogicalVolumeStore & )
delete

◆ Register()

void G4LogicalVolumeStore::Register ( G4LogicalVolume * pVolume)
static

Adds the logical volume 'pVolume' to the collection.

Definition at line 145 of file G4LogicalVolumeStore.cc.

146{
148 store->push_back(pVolume);
149 const G4String& vol_name = pVolume->GetName();
150 auto it = store->bmap.find(vol_name);
151 if (it != store->bmap.cend())
152 {
153 it->second.push_back(pVolume);
154 }
155 else
156 {
157 std::vector<G4LogicalVolume*> vol_vec { pVolume };
158 store->bmap.insert(std::make_pair(vol_name, vol_vec));
159 }
160 if (fgNotifier != nullptr) { fgNotifier->NotifyRegistration(); }
161 store->mvalid = true;
162}

Referenced by G4LogicalVolume::G4LogicalVolume(), and G4LogicalVolume::G4LogicalVolume().

◆ SetMapValid()

void G4LogicalVolumeStore::SetMapValid ( G4bool val)
inline

Definition at line 120 of file G4LogicalVolumeStore.hh.

120{ mvalid = val; }

Referenced by G4LogicalVolume::SetName().

◆ SetNotifier()

void G4LogicalVolumeStore::SetNotifier ( G4VStoreNotifier * pNotifier)
static

Assigns a notifier for allocation/deallocation of the logical volumes.

Definition at line 108 of file G4LogicalVolumeStore.cc.

109{
110 GetInstance();
111 fgNotifier = pNotifier;
112}

◆ UpdateMap()

void G4LogicalVolumeStore::UpdateMap ( )

Brings contents of the internal map up to date and resets validity flag.

Definition at line 118 of file G4LogicalVolumeStore.cc.

119{
120 G4AutoLock l(&mapMutex); // to avoid thread contention at initialisation
121 if (mvalid) { return; }
122 bmap.clear();
123 for(const auto & pos : *GetInstance())
124 {
125 const G4String& vol_name = pos->GetName();
126 auto it = bmap.find(vol_name);
127 if (it != bmap.cend())
128 {
129 it->second.push_back(pos);
130 }
131 else
132 {
133 std::vector<G4LogicalVolume*> vol_vec { pos };
134 bmap.insert(std::make_pair(vol_name, vol_vec));
135 }
136 }
137 mvalid = true;
138 l.unlock();
139}
G4TemplateAutoLock< G4Mutex > G4AutoLock

Referenced by GetVolume(), and G4GDMLRead::StripNames().


The documentation for this class was generated from the following files: