19#include "BDSAperturePointsLoader.hh"
21#include "BDSException.hh"
22#include "BDSUtilities.hh"
25#include "G4TwoVector.hh"
27#include "G4UnitsTable.hh"
28#include "G4Version.hh"
40#include "src-external/gzstream/gzstream.h"
57 G4String functionName =
"BDSAperturePointsLoader::Load> ";
61 {
throw BDSException(functionName,
"empty file name given");}
67 bool validFile = file.rdbuf()->is_open();
69 bool validFile = file.is_open();
73 {
throw BDSException(functionName,
"Cannot open file \"" + fileName +
"\"");}
75 {G4cout << functionName <<
"loading \"" << fileName <<
"\"" << G4endl;}
78 auto result =
new std::vector<G4TwoVector>();
79 std::array<G4double, 2> values({0,0});
81 std::regex commentLine(
"^\\s*[\\#!]");
82 while (std::getline(file, line))
85 if (std::all_of(line.begin(), line.end(), isspace))
91 if (std::regex_search(line, commentLine))
99 if ((G4int)wordsInLine.size() != 2)
100 {
throw BDSException(functionName,
"invalid number of coordinates on line " + std::to_string(lineNo));}
102 for (G4int i = 0; i < (G4int)values.size(); i++)
106 {coord = std::stod(wordsInLine[i]);}
107 catch (std::exception& e)
108 {
throw BDSException(functionName,
"Error on line> " + std::to_string(lineNo) +
"> cannot convert to number");}
109 (values[i]) = coord * unit;
112 result->emplace_back(G4TwoVector(values[0], values[1]));
119 G4cout << functionName <<
"loaded " << result->size() <<
" points" << G4endl;
121 if (result->size() < 3)
122 {
throw BDSException(functionName,
"require at least 3 points to make an enclosed volume");}
128 const G4String& unit)
130 G4String functionName =
"BDS::LoadAperturePoints>";
132 G4double unitAsNumber = 1.0;
135#if G4VERSION_NUMBER > 1029
136 if (!G4UnitDefinition::IsUnitDefined(unit))
137 {
throw BDSException(functionName,
"no such unit \"" + unit +
"\"");}
139 {unitAsNumber = G4UnitDefinition::GetValueOf(unit);}
142 if (unitLower ==
"mm")
143 {unitAsNumber = 1.0;}
144 else if (unitLower ==
"cm")
146 else if (unitLower ==
"m")
147 {unitAsNumber = 1000;}
149 {
throw BDSException(functionName,
"Unknown unit \"" + unit +
"\" (for this version of Geant4/BDSIM) - use one of (mm, cm, m)");}
155 {
return cachedResult;}
159 {
throw BDSException(functionName,
"no such file: \"" + fullFilePath +
"\"");}
161 std::vector<G4TwoVector>* result;
162 if (fullFilePath.rfind(
"gz") != std::string::npos)
166 result = loader.
Load(fullFilePath, unitAsNumber);
168 throw BDSException(functionName,
"Compressed file loading - but BDSIM not compiled with ZLIB.");
174 result = loader.
Load(fullFilePath, unitAsNumber);
190BDSAperturePointsCache::~BDSAperturePointsCache()
197 for (
auto& v : cachedFiles)
204 std::vector<G4TwoVector>* result =
nullptr;
205 auto search = cachedFiles.find(fileNameAndUnits);
206 if (search != cachedFiles.end())
207 {
return search->second;}
213 std::vector<G4TwoVector>* contents)
215 auto search = cachedFiles.find(fileNameAndUnits);
216 if (search != cachedFiles.end())
218 if (contents != search->second)
219 {
throw BDSException(__METHOD_NAME__,
"overwriting cache of aperture points with different contents for file name: "+fileNameAndUnits);}
223 {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'.