BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSUtilities.hh
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#ifndef BDSUTILITIES_H
19#define BDSUTILITIES_H
20
21#include "globals.hh" // geant4 globals / types
22#include "G4String.hh"
23#include "G4ThreeVector.hh"
24#include "G4TwoVector.hh"
25
26#include <limits>
27#include <map>
28#include <string>
29#include <unordered_map>
30#include <utility>
31#include <vector>
32
33class BDSExtent;
34
35class G4UserLimits;
36
37namespace CLHEP {
38 class HepRotation;
39}
40typedef CLHEP::HepRotation G4RotationMatrix;
41
50namespace BDS
51{
53 struct non_alpha
54 {
55 G4bool operator()(char c);
56 };
57
59 G4bool StrContains(const G4String& str, const G4String& test);
60
62 G4int StrCompare(const G4String& str, const G4String&, G4String::caseCompare mode=G4String::ignoreCase);
63
65 G4String LowerCase(const G4String& str);
66
69 enum class StringStripType
70 {
71 leading,
72 trailing,
73 both
74 };
76 G4String StrStrip(const G4String& str,
77 char ch,
78 StringStripType stripType = StringStripType::both);
79
81 G4String PrepareSafeName(G4String name);
82
85 G4int CalculateOrientation(G4double angle);
86
88 std::pair<G4ThreeVector,G4ThreeVector> CalculateFaces(G4double angleInIn,
89 G4double angleOutIn);
90
93 void EnsureInLimits(G4double& value, G4double lowerLimit, G4double upperLimit);
94
96 G4bool FileExists(const G4String& filename);
97
99 G4bool DirectoryExists(const G4String& path);
100
102 std::string GetCurrentDir();
103
106 std::string GetBDSIMExecPath();
107
109 std::string GetBDSIMLibraryPath();
110
118 G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false);
119
122 void SplitPathAndFileName(const G4String& filePath,
123 G4String& path,
124 G4String& filename);
125
127 void SplitFileAndExtension(const G4String& fileName,
128 G4String& file,
129 G4String& extension);
130
133 void HandleAborts(int signal_number);
134
139 G4bool IsFinite(G4double value,
140 G4double tolerance = std::numeric_limits<double>::epsilon());
141
143 G4bool IsFinite(const G4ThreeVector& variable,
144 G4double tolerance = std::numeric_limits<double>::epsilon());
145
147 G4bool NonZero(G4double value);
148
152 G4bool IsFiniteStrength(G4double variable);
153
155 G4bool IsInteger(const char* s, int& convertedInteger);
156
158 G4bool IsNumber(const char* s, double& convertedNumber);
159
160 template <typename T>
161 G4int Sign(T val)
162 {return G4int((T(0) < val) - (val < T(0)));}
163
164 inline G4String BoolToString(G4bool in)
165 {return in ? "true" : "false";}
166
169 void PrintRotationMatrix(G4RotationMatrix* rm, G4String keyName = "unknown");
170
172 G4bool Geant4EnvironmentIsSet();
173
177 void CheckHighPrecisionDataExists(const G4String& physicsListName);
178
181 void CheckLowEnergyNeutronDataExists(const G4String& phhysicsListName);
182
184 G4double GetParameterValueDouble(G4String spec, G4String name);
185 G4String GetParameterValueString(G4String spec, G4String name);
186 G4int GetParameterValueInt(G4String spec, G4String name);
188
190 std::vector<G4String> SplitOnWhiteSpace(const G4String& input);
191
193 G4TwoVector Rotate(const G4TwoVector& vec, const G4double& angle);
194
198 G4bool WillIntersect(const G4ThreeVector& incomingNormal,
199 const G4ThreeVector& outgoingNormal,
200 const G4double& zSeparation,
201 const BDSExtent& incomingExtent,
202 const BDSExtent& outgoingExtent);
203
207 G4bool WillIntersect(const G4double& angleIn,
208 const G4double& angleOut,
209 const G4double& horizontalWidth,
210 const G4double& length);
211
215 G4double GetZOfPointOnPlane(const G4ThreeVector& normal, G4double x, G4double y);
216
221 G4ThreeVector RotateToReferenceFrame(G4ThreeVector faceNormal, G4double fullAngle);
222
225 std::pair<G4String, G4String> SplitOnColon(const G4String& formatAndPath);
226
233 G4UserLimits* CreateUserLimits(G4UserLimits* defaultUL,
234 G4double length,
235 G4double fraction = 1.6);
236
238 G4double GetMemoryUsage();
239
241 std::map<G4String, G4String> GetUserParametersMap(const G4String& userParameters,
242 char delimiter = ':');
243
247 template <typename K, typename V>
248 V MapGetWithDefault(const std::map <K,V>& m, const K& key, const V& defaultValue)
249 {
250 typename std::map<K,V>::const_iterator it = m.find(key);
251 return it == m.end() ? defaultValue : it->second;
252 }
253 template <typename K, typename V>
254 V MapGetWithDefault(const std::unordered_map <K,V>& m, const K& key, const V& defaultValue)
255 {
256 typename std::unordered_map<K,V>::const_iterator it = m.find(key);
257 return it == m.end() ? defaultValue : it->second;
258 }
259
262 G4int VerboseEventStop(G4int verboseEventStart,
263 G4int verboseEventContinueFor);
264
266 G4bool VerboseThisEvent(G4int eventIndex,
267 G4int eventStart,
268 G4int eventStop);
269
271 G4double Rigidity(G4double momentumMagnitude,
272 G4double charge);
273
275 inline G4bool StartsWith(const std::string& expression,
276 const std::string& prefix) {return expression.size() >= prefix.size() &&
277 expression.rfind(prefix, 0) == 0;}
278
280 inline G4bool EndsWith(const std::string& expression,
281 const std::string& suffix) {return expression.size() >= suffix.size() &&
282 expression.compare(expression.size() - suffix.size(), suffix.size(), suffix) == 0;}
283
285 G4double CalculateSafeAngledVolumeLength(G4double angleIn,
286 G4double angleOut,
287 G4double length,
288 G4double containerWidth,
289 G4double containerHeight=0);
290
292 G4double CalculateSafeAngledVolumeLength(G4ThreeVector inputfaceIn,
293 G4ThreeVector outputfaceIn,
294 G4double length,
295 G4double containerWidth,
296 G4double containerHeight=0);
297
299 G4double ArcLengthFromChordLength(G4double chordLength, G4double angle);
300}
301
302#endif
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:38
Return either G4Tubs or G4CutTubs depending on flat face.
G4double Rigidity(G4double momentumMagnitude, G4double charge)
Calculate the rigidity for a total momentum and charge.
G4TwoVector Rotate(const G4TwoVector &vec, const G4double &angle)
Rotate a two vector in polar coordinates by an angle.
G4bool WillIntersect(const G4ThreeVector &incomingNormal, const G4ThreeVector &outgoingNormal, const G4double &zSeparation, const BDSExtent &incomingExtent, const BDSExtent &outgoingExtent)
G4String GetParameterValueString(G4String spec, G4String name)
Get parameter value from the specification ('spec') string.
std::pair< G4String, G4String > SplitOnColon(const G4String &formatAndPath)
void SplitFileAndExtension(const G4String &fileName, G4String &file, G4String &extension)
Split a filename.ext into filename and extension. Extension includes '.'.
std::string GetCurrentDir()
Get the current dir the program was executed from.
G4String PrepareSafeName(G4String name)
Remove white space and special characters in the name.
G4bool NonZero(G4double value)
Test whether a number is non-zero - ie abs(number) > minimum number.
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4ThreeVector RotateToReferenceFrame(G4ThreeVector faceNormal, G4double fullAngle)
V MapGetWithDefault(const std::map< K, V > &m, const K &key, const V &defaultValue)
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
G4double GetZOfPointOnPlane(const G4ThreeVector &normal, G4double x, G4double y)
G4bool EndsWith(const std::string &expression, const std::string &suffix)
Return true if a string "expression" ends with "suffix".
G4double GetMemoryUsage()
Get the current memory usage.
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4UserLimits * CreateUserLimits(G4UserLimits *defaultUL, G4double length, G4double fraction=1.6)
G4bool DirectoryExists(const G4String &path)
Check if directory exists.
void PrintRotationMatrix(G4RotationMatrix *rm, G4String keyName="unknown")
G4bool Geant4EnvironmentIsSet()
Check if the geant4 environmental variables necessary for a run are set.
void EnsureInLimits(G4double &value, G4double lowerLimit, G4double upperLimit)
std::string GetBDSIMLibraryPath()
Returns the path from which the BDSIM library is loaded.
G4bool FileExists(const G4String &filename)
Checks if filename exists.
void CheckHighPrecisionDataExists(const G4String &physicsListName)
G4bool StartsWith(const std::string &expression, const std::string &prefix)
Return true if a string "expression" starts with "prefix".
G4bool VerboseThisEvent(G4int eventIndex, G4int eventStart, G4int eventStop)
Logic of whether this event should be verbose or not. Code here so it's not duplicated.
StringStripType
void HandleAborts(int signal_number)
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.
G4double CalculateSafeAngledVolumeLength(G4double angleIn, G4double angleOut, G4double length, G4double containerWidth, G4double containerHeight=0)
Calculate safe length of an angled volume so it fills the length of its container.
G4bool IsNumber(const char *s, double &convertedNumber)
Check if character array is an integer, and returns the double by reference.
G4int VerboseEventStop(G4int verboseEventStart, G4int verboseEventContinueFor)
void CheckLowEnergyNeutronDataExists(const G4String &phhysicsListName)
G4bool IsFinite(G4double value, G4double tolerance=std::numeric_limits< double >::epsilon())
G4bool IsFiniteStrength(G4double variable)
G4double ArcLengthFromChordLength(G4double chordLength, G4double angle)
Calculate the arc length from the chord length for a given angle.
void SplitPathAndFileName(const G4String &filePath, G4String &path, G4String &filename)
std::string GetBDSIMExecPath()
G4bool IsInteger(const char *s, int &convertedInteger)
Check if character array is an integer, and returns the integer by reference.
G4int GetParameterValueInt(G4String spec, G4String name)
Get parameter value from the specification ('spec') string.
std::map< G4String, G4String > GetUserParametersMap(const G4String &userParameters, char delimiter=':')
Take one long string and split on space and then on colon. "key1:value1 key2:value2" etc.
G4int StrCompare(const G4String &str, const G4String &, G4String::caseCompare mode=G4String::ignoreCase)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4String StrStrip(const G4String &str, char ch, StringStripType stripType=StringStripType::both)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4int CalculateOrientation(G4double angle)
std::pair< G4ThreeVector, G4ThreeVector > CalculateFaces(G4double angleInIn, G4double angleOutIn)
Calculate input and output normal vector.
G4double GetParameterValueDouble(G4String spec, G4String name)
Get parameter value from the specification ('spec') string.
Logical not for isalpha UnaryPredicate as needed for string manipulations.