BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSSamplerRegistry.hh
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#ifndef BDSSAMPLERREGISTRY_H
19#define BDSSAMPLERREGISTRY_H
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSSamplerPlacementRecord.hh"
23#include "BDSSamplerType.hh"
24
25#include "globals.hh" // geant4 types / globals
26#include "G4Transform3D.hh"
27
28#include <map>
29#include <set>
30#include <utility>
31#include <vector>
32
34class BDSSampler;
35
58{
59private:
61 typedef std::vector<BDSSamplerPlacementRecord> InfoVector;
62
65
66public:
69
71
85 G4int RegisterSampler(const G4String& name,
86 BDSSampler* sampler,
87 const G4Transform3D& transform = G4Transform3D(),
88 G4double S = -1000,
89 const BDSBeamlineElement* element = nullptr,
90 BDSSamplerType type = BDSSamplerType::plane,
91 G4double radius = 0);
92
94
96 typedef InfoVector::iterator iterator;
97 typedef InfoVector::const_iterator const_iterator;
98 iterator begin() {return infos.begin();}
99 iterator end() {return infos.end();}
100 const_iterator begin() const {return infos.begin();}
101 const_iterator end() const {return infos.end();}
102 G4bool empty() const {return infos.empty();}
104
106 inline G4String GetName(G4int index) const;
107 inline G4String GetNameUnique(G4int index) const;
108 inline BDSSampler* GetSampler(G4int index) const;
109 inline G4Transform3D GetTransform(G4int index) const;
110 inline G4Transform3D GetTransformInverse(G4int index) const;
111 inline G4double GetSPosition(G4int index) const;
112 inline const BDSSamplerPlacementRecord& GetInfo(G4int index) const {return infos.at(index);}
113 inline G4int GetBeamlineIndex(G4int index) const;
115
117 std::vector<G4String> GetNames() const;
118
120 std::vector<G4String> GetUniqueNames() const;
121 std::vector<G4String> GetUniqueNamesPlane() const;
122 std::vector<G4String> GetUniqueNamesCylinder() const;
123 std::vector<G4String> GetUniqueNamesSphere() const;
124
126 std::map<std::string, double> GetUniqueNameToRadiusCylinder() const;
127 std::map<std::string, double> GetUniqueNameToRadiusSphere() const;
129
130 inline std::vector<G4int> GetSamplerIDsPlane() const {return samplerIDsPerType.at(BDSSamplerType::plane);}
131 inline std::vector<G4int> GetSamplerIDsCylinder() const {return samplerIDsPerType.at(BDSSamplerType::cylinder);}
132 inline std::vector<G4int> GetSamplerIDsSphere() const {return samplerIDsPerType.at(BDSSamplerType::sphere);}
133
135 std::vector<std::pair<G4String, G4double> > GetUniquePlaneNamesAndSPosition() const;
136
138 inline G4int NumberOfExistingSamplers() const;
139 inline size_t size() const;
140
141 inline G4bool SafeIndex(G4int index) const {return index < (G4int)infos.size();}
142
143private:
146
149
153
157 std::map<G4String, G4int> existingNames;
158
160 std::set<BDSSampler*> samplerObjects;
161
163 std::map<BDSSamplerType, BDSSamplerType> samplerTypeToCategory;
164 std::map<BDSSamplerType, std::vector<G4int>> samplerIDsPerType;
165};
166
167inline G4String BDSSamplerRegistry::GetName(G4int index) const
168{return SafeIndex(index) ? infos.at(index).Name() : throw BDSException(__METHOD_NAME__, "invalid index");}
169
170inline G4String BDSSamplerRegistry::GetNameUnique(G4int index) const
171{return SafeIndex(index) ? infos.at(index).UniqueName() : throw BDSException(__METHOD_NAME__, "invalid index");}
172
174{return SafeIndex(index) ? infos.at(index).Sampler() : throw BDSException(__METHOD_NAME__, "invalid index");}
175
176inline G4Transform3D BDSSamplerRegistry::GetTransform(G4int index) const
177{return SafeIndex(index) ? infos.at(index).Transform() : throw BDSException(__METHOD_NAME__, "invalid index");}
178
179inline G4Transform3D BDSSamplerRegistry::GetTransformInverse(G4int index) const
180{return SafeIndex(index) ? infos.at(index).TransformInverse() : throw BDSException(__METHOD_NAME__, "invalid index");}
181
182inline G4double BDSSamplerRegistry::GetSPosition(G4int index) const
183{return SafeIndex(index) ? infos.at(index).SPosition() : throw BDSException(__METHOD_NAME__, "invalid index");}
184
187
188inline size_t BDSSamplerRegistry::size() const
189{return infos.size();}
190
191inline G4int BDSSamplerRegistry::GetBeamlineIndex(G4int index) const
192{return SafeIndex(index) ? infos.at(index).BeamlineIndex() : throw BDSException(__METHOD_NAME__, "invalid index");}
193
194#endif
A class that holds a fully constructed BDSAcceleratorComponent as well as any information relevant to...
General exception with possible name of object and message.
Information about a registered sampler.
Associated information for the placement of a sampler.
std::vector< std::pair< G4String, G4double > > GetUniquePlaneNamesAndSPosition() const
Access all the unique names and their corresponding s position at once.
std::map< BDSSamplerType, BDSSamplerType > samplerTypeToCategory
Map to reduce 'forward' sampler types to simple sampler types for keeping a record of IDs.
G4Transform3D GetTransform(G4int index) const
Accessor.
InfoVector::iterator iterator
Iterator mechanics.
InfoVector::const_iterator const_iterator
Iterator mechanics.
std::map< std::string, double > GetUniqueNameToRadiusSphere() const
For output in standard C++ types. Also in m for units.
iterator begin()
Iterator mechanics.
iterator end()
Iterator mechanics.
BDSSampler * GetSampler(G4int index) const
Accessor.
G4int GetBeamlineIndex(G4int index) const
Accessor.
std::vector< BDSSamplerPlacementRecord > InfoVector
Typedefs up first so we can declare public iterators.
std::map< G4String, G4int > existingNames
static BDSSamplerRegistry * Instance()
Accessor for registry.
const_iterator begin() const
Iterator mechanics.
G4bool empty() const
Iterator mechanics.
G4double GetSPosition(G4int index) const
Accessor.
std::vector< G4String > GetUniqueNames() const
Access all the unique names at once.
const BDSSamplerPlacementRecord & GetInfo(G4int index) const
Accessor.
InfoVector infos
Storage of registered information.
static BDSSamplerRegistry * instance
Singleton instance.
G4int NumberOfExistingSamplers() const
Get number of registered samplers.
std::vector< G4String > GetNames() const
Access all names at once.
G4String GetNameUnique(G4int index) const
Accessor.
BDSSamplerRegistry()
Private constructor to enforce singleton pattern.
std::set< BDSSampler * > samplerObjects
Cache of unique sampler objects for memory management.
const_iterator end() const
Iterator mechanics.
G4int RegisterSampler(const G4String &name, BDSSampler *sampler, const G4Transform3D &transform=G4Transform3D(), G4double S=-1000, const BDSBeamlineElement *element=nullptr, BDSSamplerType type=BDSSamplerType::plane, G4double radius=0)
G4String GetName(G4int index) const
Accessor.
G4Transform3D GetTransformInverse(G4int index) const
Accessor.
std::map< std::string, double > GetUniqueNameToRadiusCylinder() const
For output in standard C++ types. Also in m for units.
Base class and registry of sampler instances.
Definition BDSSampler.hh:35