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) Royal Holloway,
3University of London 2001 - 2024.
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#ifndef BDSUTILITIES_H
20#define BDSUTILITIES_H
21
22#include "globals.hh" // geant4 globals / types
23#include "G4String.hh"
24#include "G4ThreeVector.hh"
25#include "G4TwoVector.hh"
26
27#include <limits>
28#include <map>
29#include <string>
30#include <unordered_map>
31#include <utility>
32#include <vector>
33
34class BDSExtent;
35
36class G4UserLimits;
37
38namespace CLHEP {
39 class HepRotation;
40}
41typedef CLHEP::HepRotation G4RotationMatrix;
42
51namespace BDS
52{
54 struct non_alpha
55 {
56 G4bool operator()(char c);
57 };
58
60 G4bool StrContains(const G4String& str, const G4String& test);
61
63 G4int StrCompare(const G4String& str, const G4String&, G4String::caseCompare mode=G4String::ignoreCase);
64
66 G4String LowerCase(const G4String& str);
67
70 enum class StringStripType
71 {
72 leading,
73 trailing,
74 both
75 };
77 G4String StrStrip(const G4String& str,
78 char ch,
79 StringStripType stripType = StringStripType::both);
80
82 G4String PrepareSafeName(G4String name);
83
86 G4int CalculateOrientation(G4double angle);
87
89 std::pair<G4ThreeVector,G4ThreeVector> CalculateFaces(G4double angleInIn,
90 G4double angleOutIn);
91
94 void EnsureInLimits(G4double& value, G4double lowerLimit, G4double upperLimit);
95
97 G4bool FileExists(const G4String& filename);
98
100 G4bool DirectoryExists(const G4String& path);
101
103 std::string GetCurrentDir();
104
107 std::string GetBDSIMExecPath();
108
116 G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false);
117
120 void SplitPathAndFileName(const G4String& filePath,
121 G4String& path,
122 G4String& filename);
123
125 void SplitFileAndExtension(const G4String& fileName,
126 G4String& file,
127 G4String& extension);
128
131 void HandleAborts(int signal_number);
132
137 G4bool IsFinite(G4double value,
138 G4double tolerance = std::numeric_limits<double>::epsilon());
139
141 G4bool IsFinite(const G4ThreeVector& variable,
142 G4double tolerance = std::numeric_limits<double>::epsilon());
143
145 G4bool NonZero(G4double value);
146
150 G4bool IsFiniteStrength(G4double variable);
151
153 G4bool IsInteger(const char* s, int& convertedInteger);
154
156 G4bool IsNumber(const char* s, double& convertedNumber);
157
158 template <typename T>
159 G4int Sign(T val)
160 {return G4int((T(0) < val) - (val < T(0)));}
161
162 inline G4String BoolToString(G4bool in)
163 {return in ? "true" : "false";}
164
167 void PrintRotationMatrix(G4RotationMatrix* rm, G4String keyName = "unknown");
168
170 G4bool Geant4EnvironmentIsSet();
171
175 void CheckHighPrecisionDataExists(const G4String& physicsListName);
176
179 void CheckLowEnergyNeutronDataExists(const G4String& phhysicsListName);
180
182 G4double GetParameterValueDouble(G4String spec, G4String name);
183 G4String GetParameterValueString(G4String spec, G4String name);
184 G4int GetParameterValueInt(G4String spec, G4String name);
186
188 std::vector<G4String> SplitOnWhiteSpace(const G4String& input);
189
191 G4TwoVector Rotate(const G4TwoVector& vec, const G4double& angle);
192
196 G4bool WillIntersect(const G4ThreeVector& incomingNormal,
197 const G4ThreeVector& outgoingNormal,
198 const G4double& zSeparation,
199 const BDSExtent& incomingExtent,
200 const BDSExtent& outgoingExtent);
201
205 G4bool WillIntersect(const G4double& angleIn,
206 const G4double& angleOut,
207 const G4double& horizontalWidth,
208 const G4double& length);
209
213 G4double GetZOfPointOnPlane(const G4ThreeVector& normal, G4double x, G4double y);
214
219 G4ThreeVector RotateToReferenceFrame(G4ThreeVector faceNormal, G4double fullAngle);
220
223 std::pair<G4String, G4String> SplitOnColon(const G4String& formatAndPath);
224
231 G4UserLimits* CreateUserLimits(G4UserLimits* defaultUL,
232 G4double length,
233 G4double fraction = 1.6);
234
236 G4double GetMemoryUsage();
237
239 std::map<G4String, G4String> GetUserParametersMap(const G4String& userParameters,
240 char delimiter = ':');
241
245 template <typename K, typename V>
246 V MapGetWithDefault(const std::map <K,V>& m, const K& key, const V& defaultValue)
247 {
248 typename std::map<K,V>::const_iterator it = m.find(key);
249 return it == m.end() ? defaultValue : it->second;
250 }
251 template <typename K, typename V>
252 V MapGetWithDefault(const std::unordered_map <K,V>& m, const K& key, const V& defaultValue)
253 {
254 typename std::unordered_map<K,V>::const_iterator it = m.find(key);
255 return it == m.end() ? defaultValue : it->second;
256 }
257
260 G4int VerboseEventStop(G4int verboseEventStart,
261 G4int verboseEventContinueFor);
262
264 G4bool VerboseThisEvent(G4int eventIndex,
265 G4int eventStart,
266 G4int eventStop);
267
269 G4double Rigidity(G4double momentumMagnitude,
270 G4double charge);
271
273 inline G4bool StartsWith(const std::string& expression,
274 const std::string& prefix) {return expression.size() >= prefix.size() &&
275 expression.rfind(prefix, 0) == 0;}
276
278 inline G4bool EndsWith(const std::string& expression,
279 const std::string& suffix) {return expression.size() >= suffix.size() &&
280 expression.compare(expression.size() - suffix.size(), suffix.size(), suffix) == 0;}
281
283 G4double CalculateSafeAngledVolumeLength(G4double angleIn,
284 G4double angleOut,
285 G4double length,
286 G4double containerWidth,
287 G4double containerHeight=0);
288
290 G4double CalculateSafeAngledVolumeLength(G4ThreeVector inputfaceIn,
291 G4ThreeVector outputfaceIn,
292 G4double length,
293 G4double containerWidth,
294 G4double containerHeight=0);
295
297 G4double ArcLengthFromChordLength(G4double chordLength, G4double angle);
298}
299
300#endif
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:39
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)
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.