BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSUtilities.hh
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#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 <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
115 G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false);
116
119 void SplitPathAndFileName(const G4String& filePath,
120 G4String& path,
121 G4String& filename);
122
124 void SplitFileAndExtension(const G4String& fileName,
125 G4String& file,
126 G4String& extension);
127
130 void HandleAborts(int signal_number);
131
136 G4bool IsFinite(G4double value,
137 G4double tolerance = std::numeric_limits<double>::epsilon());
138
140 G4bool IsFinite(const G4ThreeVector& variable,
141 G4double tolerance = std::numeric_limits<double>::epsilon());
142
144 G4bool NonZero(G4double value);
145
149 G4bool IsFiniteStrength(G4double variable);
150
152 G4bool IsInteger(const char* s, int& convertedInteger);
153
155 G4bool IsNumber(const char* s, double& convertedNumber);
156
157 template <typename T>
158 G4int Sign(T val)
159 {return G4int((T(0) < val) - (val < T(0)));}
160
161 inline G4String BoolToString(G4bool in)
162 {return in ? "true" : "false";}
163
166 void PrintRotationMatrix(G4RotationMatrix* rm, G4String keyName = "unknown");
167
169 G4bool Geant4EnvironmentIsSet();
170
174 void CheckHighPrecisionDataExists(const G4String& physicsListName);
175
178 void CheckLowEnergyNeutronDataExists(const G4String& phhysicsListName);
179
181 G4double GetParameterValueDouble(G4String spec, G4String name);
182 G4String GetParameterValueString(G4String spec, G4String name);
183 G4int GetParameterValueInt(G4String spec, G4String name);
185
187 std::vector<G4String> SplitOnWhiteSpace(const G4String& input);
188
190 G4TwoVector Rotate(const G4TwoVector& vec, const G4double& angle);
191
195 G4bool WillIntersect(const G4ThreeVector& incomingNormal,
196 const G4ThreeVector& outgoingNormal,
197 const G4double& zSeparation,
198 const BDSExtent& incomingExtent,
199 const BDSExtent& outgoingExtent);
200
204 G4bool WillIntersect(const G4double& angleIn,
205 const G4double& angleOut,
206 const G4double& horizontalWidth,
207 const G4double& length);
208
212 G4double GetZOfPointOnPlane(const G4ThreeVector& normal, G4double x, G4double y);
213
218 G4ThreeVector RotateToReferenceFrame(G4ThreeVector faceNormal, G4double fullAngle);
219
222 std::pair<G4String, G4String> SplitOnColon(const G4String& formatAndPath);
223
230 G4UserLimits* CreateUserLimits(G4UserLimits* defaultUL,
231 G4double length,
232 G4double fraction = 1.6);
233
235 G4double GetMemoryUsage();
236
238 std::map<G4String, G4String> GetUserParametersMap(const G4String& userParameters,
239 char delimiter = ':');
240
244 template <typename K, typename V>
245 V MapGetWithDefault(const std::map <K,V>& m, const K& key, const V& defaultValue)
246 {
247 typename std::map<K,V>::const_iterator it = m.find(key);
248 return it == m.end() ? defaultValue : it->second;
249 }
250
253 G4int VerboseEventStop(G4int verboseEventStart,
254 G4int verboseEventContinueFor);
255
257 G4bool VerboseThisEvent(G4int eventIndex,
258 G4int eventStart,
259 G4int eventStop);
260
262 G4double Rigidity(G4double momentumMagnitude,
263 G4double charge);
264
266 inline G4bool StartsWith(const std::string& expression,
267 const std::string& prefix) {return expression.size() >= prefix.size() &&
268 expression.rfind(prefix, 0) == 0;}
269
271 inline G4bool EndsWith(const std::string& expression,
272 const std::string& suffix) {return expression.size() >= suffix.size() &&
273 expression.compare(expression.size() - suffix.size(), suffix.size(), suffix) == 0;}
274
276 G4double CalculateSafeAngledVolumeLength(G4double angleIn,
277 G4double angleOut,
278 G4double length,
279 G4double containerWidth,
280 G4double containerHeight=0);
281
283 G4double CalculateSafeAngledVolumeLength(G4ThreeVector inputfaceIn,
284 G4ThreeVector outputfaceIn,
285 G4double length,
286 G4double containerWidth,
287 G4double containerHeight=0);
288}
289
290#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.
Definition: BDSUtilities.cc:66
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
Definition: BDSUtilities.hh:70
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)
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.
Definition: BDSUtilities.cc:75
G4String StrStrip(const G4String &str, char ch, StringStripType stripType=StringStripType::both)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:98
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.
Definition: BDSUtilities.hh:54