BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSSamplerRegistry.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 "BDSSampler.hh"
20#include "BDSSamplerRegistry.hh"
21
22#include "globals.hh"
23#include "G4Transform3D.hh"
24
25#include "CLHEP/Units/SystemOfUnits.h"
26
27#include <map>
28#include <string>
29#include <utility>
30#include <vector>
31
33
35{
36 if (!instance)
38 return instance;
39}
40
42 numberOfEntries(0)
43{
44 samplerTypeToCategory = { {BDSSamplerType::plane, BDSSamplerType::plane},
45 {BDSSamplerType::cylinder, BDSSamplerType::cylinder},
46 {BDSSamplerType::cylinderforward, BDSSamplerType::cylinder},
47 {BDSSamplerType::sphere, BDSSamplerType::sphere},
48 {BDSSamplerType::sphereforward, BDSSamplerType::sphere} };
49 samplerIDsPerType[BDSSamplerType::plane] = std::vector<G4int>();
50 samplerIDsPerType[BDSSamplerType::cylinder] = std::vector<G4int>();
51 samplerIDsPerType[BDSSamplerType::sphere] = std::vector<G4int>();
52}
53
54BDSSamplerRegistry::~BDSSamplerRegistry()
55{
56 for (auto s : samplerObjects)
57 {delete s;}
58 instance = nullptr;
59}
60
61G4int BDSSamplerRegistry::RegisterSampler(const G4String& name,
62 BDSSampler* sampler,
63 const G4Transform3D& transform,
64 G4double S,
65 const BDSBeamlineElement* element,
66 BDSSamplerType type,
67 G4double radius)
68{
69 samplerObjects.insert(sampler);
70 G4String uniqueName = name;
71 auto result = existingNames.find(name);
72 if (result == existingNames.end())
73 {// wasn't found - introduce it
74 existingNames[name] = 1;
75 }
76 else
77 {
78 uniqueName = name + "_" + std::to_string(existingNames[name]);
79 existingNames[name]++;
80 }
81 BDSSamplerPlacementRecord info = BDSSamplerPlacementRecord(name, sampler, transform, S, element, uniqueName, type, radius);
82 return RegisterSampler(info);
83}
84
86{
87 infos.push_back(info);
88
89 G4int index = numberOfEntries; // copy the number of entries / the index of this entry
91
92 // cache the ID for this type of sampler
93 samplerIDsPerType[samplerTypeToCategory[info.Type()]].push_back(index);
94 return index;
95}
96
97std::vector<G4String> BDSSamplerRegistry::GetNames() const
98{
99 std::vector<G4String> names;
100 for (const auto& info : infos)
101 {names.push_back(info.Name());}
102 return names;
103}
104
105std::vector<G4String> BDSSamplerRegistry::GetUniqueNames() const
106{
107 std::vector<G4String> names;
108 for (const auto& info : infos)
109 {names.push_back(info.UniqueName());}
110 return names;
111}
112
113std::vector<G4String> BDSSamplerRegistry::GetUniqueNamesPlane() const
114{
115 std::vector<G4String> names;
116 for (const auto& info: infos)
117 {
118 if (info.Type() == BDSSamplerType::plane)
119 {names.push_back(info.UniqueName());}
120 }
121 return names;
122}
123
124std::vector<G4String> BDSSamplerRegistry::GetUniqueNamesCylinder() const
125{
126 std::vector<G4String> names;
127 for (const auto& info: infos)
128 {
129 if (info.Type() == BDSSamplerType::cylinder || info.Type() == BDSSamplerType::cylinderforward)
130 {names.push_back(info.UniqueName());}
131 }
132 return names;
133}
134
135std::vector<G4String> BDSSamplerRegistry::GetUniqueNamesSphere() const
136{
137 std::vector<G4String> names;
138 for (const auto& info: infos)
139 {
140 if (info.Type() == BDSSamplerType::sphere || info.Type() == BDSSamplerType::sphereforward)
141 {names.push_back(info.UniqueName());}
142 }
143 return names;
144}
145
146std::map<std::string, double> BDSSamplerRegistry::GetUniqueNameToRadiusCylinder() const
147{
148 std::map<std::string, double> result;
149 for (const auto& info: infos)
150 {
151 if (info.Type() == BDSSamplerType::cylinder || info.Type() == BDSSamplerType::cylinderforward)
152 {result[static_cast<std::string>(info.UniqueName())] = info.Radius()/CLHEP::m;}
153 }
154 return result;
155}
156
157std::map<std::string, double> BDSSamplerRegistry::GetUniqueNameToRadiusSphere() const
158{
159 std::map<std::string, double> result;
160 for (const auto& info: infos)
161 {
162 if (info.Type() == BDSSamplerType::sphere || info.Type() == BDSSamplerType::sphereforward)
163 {result[static_cast<std::string>(info.UniqueName())] = info.Radius()/CLHEP::m;}
164 }
165 return result;
166}
167
168std::vector<std::pair<G4String, G4double> > BDSSamplerRegistry::GetUniquePlaneNamesAndSPosition() const
169{
170 std::vector<std::pair<G4String, G4double> > result;
171 for (const auto& info : infos)
172 {
173 if (info.Type() == BDSSamplerType::plane)
174 {result.emplace_back(std::make_pair(info.UniqueName(), info.SPosition()));}
175 }
176 return result;
177}
A class that holds a fully constructed BDSAcceleratorComponent as well as any information relevant to...
Information about a registered sampler.
BDSSamplerType Type() const
Accessor.
G4String Name() const
Accessor.
G4double SPosition() const
Accessor.
G4String UniqueName() const
Accessor.
G4double Radius() const
Accessor.
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.
std::map< std::string, double > GetUniqueNameToRadiusSphere() const
For output in standard C++ types. Also in m for units.
std::map< G4String, G4int > existingNames
static BDSSamplerRegistry * Instance()
Accessor for registry.
std::vector< G4String > GetUniqueNames() const
Access all the unique names at once.
InfoVector infos
Storage of registered information.
static BDSSamplerRegistry * instance
Singleton instance.
std::vector< G4String > GetNames() const
Access all names at once.
BDSSamplerRegistry()
Private constructor to enforce singleton pattern.
std::set< BDSSampler * > samplerObjects
Cache of unique sampler objects for memory management.
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)
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:36