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