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