BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSPhysicalVolumeInfoRegistry.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSDebug.hh"
20#include "BDSPhysicalVolumeInfo.hh"
21#include "BDSPhysicalVolumeInfoRegistry.hh"
22
23#include "globals.hh" // geant4 globals / types
24#include "G4VPhysicalVolume.hh"
25
26#include <map>
27#include <set>
28
30
32{
33 if (!instance)
35 return instance;
36}
37
39{
41 backupSearch = backupRegister.begin();
42}
43
45{
46 for (auto& info : pvInfosForDeletion)
47 {delete info;}
48 instance = nullptr;
49}
50
51void BDSPhysicalVolumeInfoRegistry::RegisterInfo(G4VPhysicalVolume* physicalVolume,
53 G4bool isReadOutVolume,
54 G4bool isTunnel)
55{
56#ifdef BDSDEBUG
57 G4cout << __METHOD_NAME__ << "registering volume: " << physicalVolume->GetName() << G4endl;
58 G4cout << __METHOD_NAME__ << "with info: " << *info << G4endl;
59 G4cout << __METHOD_NAME__ << "is a read out volume: " << isReadOutVolume << G4endl;
60#endif
61 if (IsRegistered(physicalVolume))
62 {//uh oh - we've found it somewhere - abort
63 G4cerr << __METHOD_NAME__ << physicalVolume->GetName() << " is already registered" << G4endl;
64 return;
65 }
66
67 pvInfosForDeletion.insert(info);
68
69 // if it's a tunnel one, register and return
70 if (isTunnel)
71 {
72 tunnelRegister[physicalVolume] = info;
73 return;
74 }
75 // doesn't already exist so register it
76 if (isReadOutVolume)
77 {readOutRegister[physicalVolume] = info;}
78 else
79 {backupRegister[physicalVolume] = info;}
80#ifdef BDSDEBUG
81 G4cout << __METHOD_NAME__ << "component registered" << G4endl;
82#endif
83}
84
85void BDSPhysicalVolumeInfoRegistry::RegisterInfo(const std::set<G4VPhysicalVolume*>& physicalVolumes,
87 G4bool isReadOutVolume,
88 G4bool isTunnel)
89{
90 for (auto& pv : physicalVolumes)
91 {RegisterInfo(pv, info, isReadOutVolume, isTunnel);}
92}
93
95 G4bool isTunnel)
96{
97 if (!physicalVolume)
98 {return nullptr;}
99 if (excludedVolumes.find(physicalVolume) != excludedVolumes.end())
100 {// it was found in excluded volumes
101 return nullptr;
102 }
103 if (isTunnel)
104 {
105 tunnelSearch = tunnelRegister.find(physicalVolume);
106 if (tunnelSearch == tunnelRegister.end())
107 {return nullptr;}
108 else
109 {return tunnelSearch->second;}
110 }
111 else if (IsRegisteredToReadOutRegister(physicalVolume))
112 {return readOutSearch->second;}
113 else if (IsRegisteredToBackupRegister(physicalVolume))
114 {return backupSearch->second;}
115 else
116 {//uh oh - not found!
117#ifdef BDSDEBUG
118 G4cerr << __METHOD_NAME__ << "physical volume not found" << G4endl;
119 G4cerr << __METHOD_NAME__ << "pv name is: " << physicalVolume->GetName() << G4endl;
120#endif
121 return nullptr;
122 }
123}
124
125void BDSPhysicalVolumeInfoRegistry::RegisterExcludedPV(G4VPhysicalVolume* physicalVolume)
126{
127 excludedVolumes.insert(physicalVolume);
128}
129
130G4bool BDSPhysicalVolumeInfoRegistry::IsRegistered(G4VPhysicalVolume* physicalVolume)
131{
132 return (IsRegisteredToReadOutRegister(physicalVolume) || IsRegisteredToBackupRegister(physicalVolume));
133}
134
136{
137 readOutSearch = readOutRegister.find(physicalVolume);
138 if (readOutSearch == readOutRegister.end())
139 {return false;}
140 else
141 {return true;}
142}
143
144G4bool BDSPhysicalVolumeInfoRegistry::IsRegisteredToBackupRegister(G4VPhysicalVolume* physicalVolume)
145{
146 backupSearch = backupRegister.find(physicalVolume);
147 if (backupSearch == backupRegister.end())
148 {return false;}
149 else
150 {return true;}
151}
152
153 G4bool BDSPhysicalVolumeInfoRegistry::IsRegisteredToTunnelRegister(G4VPhysicalVolume* physicalVolume)
154 {
155 tunnelSearch = tunnelRegister.find(physicalVolume);
156 if (tunnelSearch == tunnelRegister.end())
157 {return false;}
158 else
159 {return true;}
160 }
161
162std::ostream& operator<< (std::ostream& out, BDSPhysicalVolumeInfoRegistry const &r)
163{
164 out << "Physical Volume Registry:" << G4endl;
165 out << "Read out volume register:" << G4endl;
166 for (BDSPVInfoIteratorConst itRO = r.readOutRegister.begin(); itRO != r.readOutRegister.end(); ++itRO)
167 {out << itRO->first->GetName() << "\t" << *(itRO->second) << G4endl;}
168 out << G4endl << "General volume register:" << G4endl;
169 for (BDSPVInfoIteratorConst itBU = r.backupRegister.begin(); itBU != r.backupRegister.end(); ++ itBU)
170 {out << itBU->first->GetName() << "\t" << *(itBU->second) << G4endl;}
171 return out;
172}
A registry of physical volume info instances that can be searched.
BDSPVInfoIterator tunnelSearch
Search iterator.
BDSPhysicalVolumeInfoRegistry()
Default constructor is private as singleton.
G4bool IsRegisteredToReadOutRegister(G4VPhysicalVolume *physicalVolume)
Check whether a physical volume is registered to the read out registry.
static BDSPhysicalVolumeInfoRegistry * instance
The singleton instance.
void RegisterExcludedPV(G4VPhysicalVolume *physicalVolume)
BDSPhysicalVolumeInfo * GetInfo(G4VPhysicalVolume *logicalVolume, G4bool isTunnel=false)
G4bool IsRegistered(G4VPhysicalVolume *physicalVolume)
Check whether a physical volume is registered at all.
BDSPVInfoIterator backupSearch
Search iterator.
BDSPVInfoIterator readOutSearch
Search iterator.
void RegisterInfo(G4VPhysicalVolume *physicalVolume, BDSPhysicalVolumeInfo *info, G4bool isReadOutVolume=false, G4bool isTunnel=false)
std::map< G4VPhysicalVolume *, BDSPhysicalVolumeInfo * > readOutRegister
Registry is a map - note 'register' is a protected keyword.
static BDSPhysicalVolumeInfoRegistry * Instance()
Singleton accessor.
G4bool IsRegisteredToBackupRegister(G4VPhysicalVolume *physicalVolume)
Check whether a physical volume is registered ot the general backup registry.
A class holding any information pertaining to a particular physical volume in a BDSIM geant4 model.
G4String GetName() const
Get the name of the logical volume.