18#include "BDSAperturePointsLoader.hh"
20#include "BDSException.hh"
21#include "BDSUtilities.hh"
24#include "G4TwoVector.hh"
26#include "G4UnitsTable.hh"
27#include "G4Version.hh"
39#include "src-external/gzstream/gzstream.h"
56 G4String functionName =
"BDSAperturePointsLoader::Load> ";
60 {
throw BDSException(functionName,
"empty file name given");}
66 bool validFile = file.rdbuf()->is_open();
68 bool validFile = file.is_open();
72 {
throw BDSException(functionName,
"Cannot open file \"" + fileName +
"\"");}
74 {G4cout << functionName <<
"loading \"" << fileName <<
"\"" << G4endl;}
77 auto result =
new std::vector<G4TwoVector>();
78 std::array<G4double, 2> values({0,0});
80 std::regex commentLine(
"^\\s*[\\#!]");
81 while (std::getline(file, line))
84 if (std::all_of(line.begin(), line.end(), isspace))
90 if (std::regex_search(line, commentLine))
98 if ((G4int)wordsInLine.size() != 2)
99 {
throw BDSException(functionName,
"invalid number of coordinates on line " + std::to_string(lineNo));}
101 for (G4int i = 0; i < (G4int)values.size(); i++)
105 {coord = std::stod(wordsInLine[i]);}
106 catch (std::exception& e)
107 {
throw BDSException(functionName,
"Error on line> " + std::to_string(lineNo) +
"> cannot convert to number");}
108 (values[i]) = coord * unit;
111 result->emplace_back(G4TwoVector(values[0], values[1]));
118 G4cout << functionName <<
"loaded " << result->size() <<
" points" << G4endl;
120 if (result->size() < 3)
121 {
throw BDSException(functionName,
"require at least 3 points to make an enclosed volume");}
127 const G4String& unit)
129 G4String functionName =
"BDS::LoadAperturePoints>";
131 G4double unitAsNumber = 1.0;
134#if G4VERSION_NUMBER > 1029
135 if (!G4UnitDefinition::IsUnitDefined(unit))
136 {
throw BDSException(functionName,
"no such unit \"" + unit +
"\"");}
138 {unitAsNumber = G4UnitDefinition::GetValueOf(unit);}
141 if (unitLower ==
"mm")
142 {unitAsNumber = 1.0;}
143 else if (unitLower ==
"cm")
145 else if (unitLower ==
"m")
146 {unitAsNumber = 1000;}
148 {
throw BDSException(functionName,
"Unknown unit \"" + unit +
"\" (for this version of Geant4/BDSIM) - use one of (mm, cm, m)");}
154 {
return cachedResult;}
158 {
throw BDSException(functionName,
"no such file: \"" + fullFilePath +
"\"");}
160 std::vector<G4TwoVector>* result;
161 if (fullFilePath.rfind(
"gz") != std::string::npos)
165 result = loader.
Load(fullFilePath, unitAsNumber);
167 throw BDSException(functionName,
"Compressed file loading - but BDSIM not compiled with ZLIB.");
173 result = loader.
Load(fullFilePath, unitAsNumber);
189BDSAperturePointsCache::~BDSAperturePointsCache()
196 for (
auto& v : cachedFiles)
203 std::vector<G4TwoVector>* result =
nullptr;
204 auto search = cachedFiles.find(fileNameAndUnits);
205 if (search != cachedFiles.end())
206 {
return search->second;}
212 std::vector<G4TwoVector>* contents)
214 auto search = cachedFiles.find(fileNameAndUnits);
215 if (search != cachedFiles.end())
217 if (contents != search->second)
218 {
throw BDSException(__METHOD_NAME__,
"overwriting cache of aperture points with different contents for file name: "+fileNameAndUnits);}
222 {cachedFiles[fileNameAndUnits] = contents;}
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.
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'.