BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSAperturePointsLoader.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 "BDSAperturePointsLoader.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSUtilities.hh"
23
24#include "G4String.hh"
25#include "G4TwoVector.hh"
26#include "G4Types.hh"
27#include "G4UnitsTable.hh"
28#include "G4Version.hh"
29
30#include <algorithm>
31#include <array>
32#include <regex>
33#include <sstream>
34#include <string>
35#include <vector>
36
37#ifdef USE_GZSTREAM
38#include "src-external/gzstream/gzstream.h"
39#endif
40
41BDSAperturePointsCache* BDSAperturePointsCache::instance = nullptr;
42
43template <class T>
45{;}
46
47template <class T>
49{;}
50
51template <class T>
52std::vector<G4TwoVector>* BDSAperturePointsLoader<T>::Load(const G4String& fileName,
53 G4double unit) const
54{
55 G4String functionName = "BDSAperturePointsLoader::Load> ";
56 T file;
57
58 if (fileName.empty())
59 {throw BDSException(functionName, "empty file name given");}
60
61 file.open(fileName);
62
63 // test if file is valid
64#ifdef USE_GZSTREAM
65 bool validFile = file.rdbuf()->is_open();
66#else
67 bool validFile = file.is_open();
68#endif
69
70 if (!validFile)
71 {throw BDSException(functionName, "Cannot open file \"" + fileName + "\"");}
72 else
73 {G4cout << functionName << "loading \"" << fileName << "\"" << G4endl;}
74
75 std::string line;
76 auto result = new std::vector<G4TwoVector>();
77 std::array<G4double, 2> values({0,0});
78 G4int lineNo = 1;
79 std::regex commentLine("^\\s*[\\#!]");
80 while (std::getline(file, line))
81 {
82 // skip a line if it's only whitespace
83 if (std::all_of(line.begin(), line.end(), isspace))
84 {
85 lineNo += 1;
86 continue;
87 }
88 // or a comment
89 if (std::regex_search(line, commentLine))
90 {
91 lineNo += 1;
92 continue;
93 }
94
95 std::vector<G4String> wordsInLine = BDS::SplitOnWhiteSpace(line);
96
97 if ((G4int)wordsInLine.size() != 2)
98 {throw BDSException(functionName, "invalid number of coordinates on line " + std::to_string(lineNo));}
99
100 for (G4int i = 0; i < (G4int)values.size(); i++)
101 {
102 G4double coord = 0;
103 try
104 {coord = std::stod(wordsInLine[i]);}
105 catch (std::exception& e)
106 {throw BDSException(functionName, "Error on line> " + std::to_string(lineNo) + "> cannot convert to number");}
107 (values[i]) = coord * unit;
108 }
109
110 result->emplace_back(G4TwoVector(values[0], values[1]));
111
112 lineNo += 1;
113 }
114
115 file.close();
116
117 G4cout << functionName << "loaded " << result->size() << " points" << G4endl;
118
119 if (result->size() < 3)
120 {throw BDSException(functionName, "require at least 3 points to make an enclosed volume");}
121
122 return result;
123}
124
125std::vector<G4TwoVector>* BDS::LoadAperturePoints(const G4String& fileName,
126 const G4String& unit)
127{
128 G4String functionName = "BDS::LoadAperturePoints>"; // namespaced functions don't print so well
129
130 G4double unitAsNumber = 1.0;
131 if (!unit.empty())
132 {
133#if G4VERSION_NUMBER > 1029
134 if (!G4UnitDefinition::IsUnitDefined(unit))
135 {throw BDSException(functionName, "no such unit \"" + unit + "\"");}
136 else
137 {unitAsNumber = G4UnitDefinition::GetValueOf(unit);}
138#else
139 G4String unitLower = BDS::LowerCase(unit);
140 if (unitLower == "mm")
141 {unitAsNumber = 1.0;}
142 else if (unitLower == "cm")
143 {unitAsNumber = 10;}
144 else if (unitLower == "m")
145 {unitAsNumber = 1000;}
146 else
147 {throw BDSException(functionName, "Unknown unit \"" + unit + "\" (for this version of Geant4/BDSIM) - use one of (mm, cm, m)");}
148#endif
149 }
150
151 auto cachedResult = BDSAperturePointsCache::Instance()->FindCachedFile(fileName+unit); // can return nullptr
152 if (cachedResult)
153 {return cachedResult;}
154
155 G4String fullFilePath = BDS::GetFullPath(fileName);
156 if (!BDS::FileExists(fullFilePath))
157 {throw BDSException(functionName, "no such file: \"" + fullFilePath + "\"");}
158
159 std::vector<G4TwoVector>* result;
160 if (fullFilePath.rfind("gz") != std::string::npos)
161 {
162#ifdef USE_GZSTREAM
164 result = loader.Load(fullFilePath, unitAsNumber);
165#else
166 throw BDSException(functionName, "Compressed file loading - but BDSIM not compiled with ZLIB.");
167#endif
168 }
169 else
170 {
172 result = loader.Load(fullFilePath, unitAsNumber);
173 }
174
175 BDSAperturePointsCache::Instance()->CacheFile(fileName+unit, result);
176
177 return result;
178}
179
180
182{
183 if (!instance)
184 {instance = new BDSAperturePointsCache();}
185 return instance;
186}
187
188BDSAperturePointsCache::~BDSAperturePointsCache()
189{
191}
192
194{
195 for (auto& v : cachedFiles)
196 {delete v.second;}
197 cachedFiles.clear();
198}
199
200std::vector<G4TwoVector>* BDSAperturePointsCache::FindCachedFile(const G4String& fileNameAndUnits) const
201{
202 std::vector<G4TwoVector>* result = nullptr;
203 auto search = cachedFiles.find(fileNameAndUnits);
204 if (search != cachedFiles.end())
205 {return search->second;}
206 else
207 {return result;}
208}
209
210void BDSAperturePointsCache::CacheFile(const G4String& fileNameAndUnits,
211 std::vector<G4TwoVector>* contents)
212{
213 auto search = cachedFiles.find(fileNameAndUnits);
214 if (search != cachedFiles.end())
215 {
216 if (contents != search->second)
217 {throw BDSException(__METHOD_NAME__, "overwriting cache of aperture points with different contents for file name: "+fileNameAndUnits);}
218 return;
219 }
220 else
221 {cachedFiles[fileNameAndUnits] = contents;}
222}
223
224
226
227#ifdef USE_GZSTREAM
229#endif
A holder for loaded aperture points files.
void CacheFile(const G4String &fileNameAndUnits, std::vector< G4TwoVector > *contents)
Add an entry to the cache.
static BDSAperturePointsCache * Instance()
Access the singleton instance.
void ClearCachedFiles()
Delete all cached points from memory and clear the map of files loaded.
std::vector< G4TwoVector > * FindCachedFile(const G4String &fileNameAndUnits) const
Retrieve a cached files. Will return nullptr if not found.
A loader for up to set of XY points for an aperture.
std::vector< G4TwoVector > * Load(const G4String &fileName, G4double unit=1.0) const
General exception with possible name of object and message.
Definition: BDSException.hh:35
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
std::vector< G4TwoVector > * LoadAperturePoints(const G4String &fileName, const G4String &unit="")
G4bool FileExists(const G4String &filename)
Checks if filename exists.
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.