Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Functions
UUtils Namespace Reference

Data Structures

struct  CompareAsc
 
struct  CompareDesc
 

Functions

short Sign (short a, short b)
 
int Sign (int a, int b)
 
long Sign (long a, long b)
 
float Sign (float a, float b)
 
double Sign (double a, double b)
 
double Infinity ()
 
double ASin (double)
 
double ACos (double)
 
double ATan (double)
 
double ATan2 (double, double)
 
void Exception (const char *originOfException, const char *exceptionCode, ExceptionSeverity severity, int level, const char *description)
 
bool AreEqualAbs (double af, double bf, double epsilon)
 
bool AreEqualRel (double af, double bf, double relPrec)
 
long LocMin (long n, const double *a)
 
long LocMax (long n, const double *a)
 
void TransformLimits (UVector3 &min, UVector3 &max, const UTransform3D &transformation)
 
double Random (double min=0.0, double max=1.0)
 
int SaveVectorToExternalFile (const std::vector< double > &vector, const std::string &filename)
 
int SaveVectorToExternalFile (const std::vector< UVector3 > &vector, const std::string &filename)
 
int SaveVectorToExternalFile (const std::vector< int > &vector, const std::string &filename)
 
std::string ToString (int number)
 
std::string ToString (double number)
 
int FileSize (const std::string &filePath)
 
int StrPos (const std::string &haystack, const std::string &needle)
 
double GetRadiusInRing (double rmin, double rmax)
 
template<class T >
sqr (const T &x)
 
bool StrEnds (std::string const &fullString, std::string const &ending)
 

Function Documentation

double UUtils::ACos ( double  x)
inline

Definition at line 218 of file UUtils.hh.

Referenced by UVector3::Angle(), and UVector3::Theta().

219 {
220  if (x < -1.) return kPi;
221  if (x > 1.) return 0;
222  return std::acos(x);
223 }
bool UUtils::AreEqualAbs ( double  af,
double  bf,
double  epsilon 
)
inline

Definition at line 76 of file UUtils.hh.

77  {
78  //return true if absolute difference between af and bf is less than epsilon
79  return std::abs(af - bf) < epsilon;
80  }
bool UUtils::AreEqualRel ( double  af,
double  bf,
double  relPrec 
)
inline

Definition at line 81 of file UUtils.hh.

82  {
83  //return true if relative difference between af and bf is less than relPrec
84  return std::abs(af - bf) <= 0.5 * relPrec * (std::abs(af) + std::abs(bf));
85  }
double UUtils::ASin ( double  x)
inline

Definition at line 211 of file UUtils.hh.

212 {
213  if (x < -1.) return -kPi / 2;
214  if (x > 1.) return kPi / 2;
215  return std::asin(x);
216 }
double UUtils::ATan ( double  )
inline
double UUtils::ATan2 ( double  y,
double  x 
)
inline

Definition at line 226 of file UUtils.hh.

Referenced by UVector3::Phi().

227 {
228  if (x != 0) return std::atan2(y, x);
229  if (y == 0) return 0;
230  if (y > 0) return kPi / 2;
231  else return -kPi / 2;
232 }
void UUtils::Exception ( const char *  originOfException,
const char *  exceptionCode,
ExceptionSeverity  severity,
int  level,
const char *  description 
)

Definition at line 177 of file UUtils.cc.

References Error, FatalError, FatalErrorInArguments, and Warning.

Referenced by UTubs::ApproxSurfaceNormal(), UTrd::CheckAndSetAllParameters(), UGenericPolycone::Create(), UReduciblePolygon::Create(), UPolyhedra::Create(), UPolyPhiFace::Diagnose(), UOrb::DistanceToIn(), UOrb::DistanceToOut(), UTet::DistanceToOut(), UTrd::DistanceToOut(), UTubs::DistanceToOut(), UCons::DistanceToOut(), USphere::DistanceToOut(), UPolyhedra::Init(), UPolycone::Init(), g4mini::main(), g4vrmlview::main(), UTrd::Normal(), UTubs::Normal(), UCons::Normal(), USphere::Normal(), UPolyhedra::Reset(), UOrb::SafetyFromInside(), UTrd::SafetyFromInside(), UTubs::SafetyFromInside(), USphere::SafetyFromInside(), UCons::SafetyFromInside(), UPolycone::SetOriginalParameters(), UBox::SetXHalfLength(), UBox::SetYHalfLength(), UBox::SetZHalfLength(), UPolyPhiFace::Triangulate(), UBox::UBox(), UCons::UCons(), UOrb::UOrb(), UPolycone::UPolycone(), USphere::USphere(), UTet::UTet(), and UTubs::UTubs().

183 {
184  bool toBeAborted = true;
185  static const std::string es_banner
186  = "\n-------- EEEE ------- UException-START -------- EEEE -------\n";
187  static const std::string ee_banner
188  = "\n-------- EEEE ------- UException-END --------- EEEE -------\n";
189  static const std::string ws_banner
190  = "\n-------- WWWW ------- UException-START -------- WWWW -------\n";
191  static const std::string we_banner
192  = "\n-------- WWWW -------- UException-END --------- WWWW -------\n";
193  std::ostringstream message;
194  message << "\n*** ExceptionHandler is not defined ***\n"
195  << "*** Exception : " << exceptionCode << std::endl
196  << " issued by : " << originOfException << std::endl
197  << description << std::endl;
198  switch (severity)
199  {
200  case FatalError:
201  std::cerr << es_banner << message.str() << "*** Fatal Exception ***"
202  << ee_banner << std::endl;
203  break;
205  std::cerr << es_banner << message.str() << "*** Fatal Error In Argument ***"
206  << ee_banner << std::endl;
207  break;
208  case Error:
209  std::cerr << es_banner << message.str() << "*** Error ***" << level
210  << ee_banner << std::endl;
211  break;
212  case Warning:
213  std::cerr << ws_banner << message.str() << "*** This is just a warning message ***"
214  << we_banner << std::endl;
215  toBeAborted = false;
216  break;
217  default:
218  std::cout << ws_banner << message.str()
219  << "*** This is just a message for your information. ***"
220  << we_banner << std::endl;
221  toBeAborted = false;
222  break;
223  }
224 
225  if (toBeAborted)
226  {
227 
228  std::cerr << std::endl << "*** GException: Aborting execution ***" << std::endl;
229  abort();
230  }
231 
232 
233 }
Definition: UUtils.hh:36
int UUtils::FileSize ( const std::string &  filePath)

Definition at line 146 of file UUtils.cc.

147 {
148  std::streampos fsize = 0;
149  std::ifstream file(filePath.c_str(), std::ios::binary);
150 
151  fsize = file.tellg();
152  file.seekg(0, std::ios::end);
153  fsize = file.tellg() - fsize;
154  file.close();
155 
156  return fsize;
157 }
double UUtils::GetRadiusInRing ( double  rmin,
double  rmax 
)
inline

Definition at line 160 of file UUtils.hh.

References Random(), and sqr().

Referenced by UTubs::GetPointOnSurface(), USphere::GetPointOnSurface(), and UCons::GetPointOnSurface().

161 {
162  // Generate radius in annular ring according to uniform area
163  //
164  if (rmin <= 0.)
165  {
166  return rmax * std::sqrt(Random());
167  }
168  if (rmin != rmax)
169  {
170  return std::sqrt(Random()
171  * (sqr(rmax) - sqr(rmin)) + sqr(rmin));
172  }
173  return rmin;
174 }
T sqr(const T &x)
Definition: templates.hh:145
double Random(double min=0.0, double max=1.0)
Definition: UUtils.cc:69
double UUtils::Infinity ( )
inline

Definition at line 177 of file UUtils.hh.

Referenced by USphere::DistanceToIn(), USphere::DistanceToOut(), and USphere::Normal().

178 {
179  // returns an infinity as defined by the IEEE standard
180  return std::numeric_limits<double>::infinity();
181 }
long UUtils::LocMax ( long  n,
const double *  a 
)
long UUtils::LocMin ( long  n,
const double *  a 
)
double UUtils::Random ( double  min = 0.0,
double  max = 1.0 
)
int UUtils::SaveVectorToExternalFile ( const std::vector< double > &  vector,
const std::string &  filename 
)

Definition at line 77 of file UUtils.cc.

78 {
79  ofstream file(filename.c_str());
80 
81  // NEW: set precision, use exponential, precision 4 digits
82  if (file.is_open())
83  {
84  int size = vector.size();
85  file.precision(16);
86  for (int i = 0; i < size; i++)
87  {
88  double value = vector[i];
89  file << value << "\n";
90  }
91  return 0;
92  }
93  return 1;
94 }
const XML_Char int const XML_Char * value
int UUtils::SaveVectorToExternalFile ( const std::vector< UVector3 > &  vector,
const std::string &  filename 
)

Definition at line 114 of file UUtils.cc.

References UVector3::x, UVector3::y, and UVector3::z.

115 {
116  ofstream file(filename.c_str());
117 
118  if (file.is_open())
119  {
120  int size = vector.size();
121  file.precision(16);
122  for (int i = 0; i < size; i++)
123  {
124  const UVector3& vec = vector[i];
125  file << vec.x << "\t" << vec.y << "\t" << vec.z << "\n";
126  }
127  return 0;
128  }
129  return 1;
130 }
double x
Definition: UVector3.hh:136
double z
Definition: UVector3.hh:138
double y
Definition: UVector3.hh:137
int UUtils::SaveVectorToExternalFile ( const std::vector< int > &  vector,
const std::string &  filename 
)

Definition at line 97 of file UUtils.cc.

98 {
99  ofstream file(filename.c_str());
100 
101  if (file.is_open())
102  {
103  int size = vector.size();
104  for (int i = 0; i < size; i++)
105  {
106  int value = vector[i];
107  file << value << "\n";
108  }
109  return 0;
110  }
111  return 1;
112 }
const XML_Char int const XML_Char * value
short UUtils::Sign ( short  a,
short  b 
)
inline

Definition at line 184 of file UUtils.hh.

Referenced by UBox::DistanceToOut(), UBox::Normal(), and G4QGSDiffractiveExcitation::String().

185 {
186  return (b >= 0) ? std::abs(a) : -std::abs(a);
187 }
int UUtils::Sign ( int  a,
int  b 
)
inline

Definition at line 189 of file UUtils.hh.

190 {
191  return (b >= 0) ? std::abs(a) : -std::abs(a);
192 }
long UUtils::Sign ( long  a,
long  b 
)
inline

Definition at line 194 of file UUtils.hh.

195 {
196  return (b >= 0) ? std::abs(a) : -std::abs(a);
197 }
float UUtils::Sign ( float  a,
float  b 
)
inline

Definition at line 199 of file UUtils.hh.

200 {
201  return (b >= 0) ? std::abs(a) : -std::abs(a);
202 }
double UUtils::Sign ( double  a,
double  b 
)
inline

Definition at line 204 of file UUtils.hh.

205 {
206  return (b >= 0) ? std::abs(a) : -std::abs(a);
207 }
template<class T >
T UUtils::sqr ( const T &  x)
inline
bool UUtils::StrEnds ( std::string const &  fullString,
std::string const &  ending 
)
inline

Definition at line 147 of file UUtils.hh.

148  {
149  if (fullString.length() >= ending.length())
150  {
151  return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
152  }
153  else
154  {
155  return false;
156  }
157  }
int UUtils::StrPos ( const std::string &  haystack,
const std::string &  needle 
)

Definition at line 160 of file UUtils.cc.

161 {
162  int sleng = haystack.length();
163  int nleng = needle.length();
164 
165  if (sleng == 0 || nleng == 0)
166  return -1;
167 
168  for (int i = 0, j = 0; i < sleng; j = 0, i++)
169  {
170  while (i + j < sleng && j < nleng && haystack[i + j] == needle[j])
171  j++;
172  if (j == nleng)
173  return i;
174  }
175  return -1;
176 }
string UUtils::ToString ( int  number)

Definition at line 132 of file UUtils.cc.

133 {
134  std::stringstream ss;
135  ss << number;
136  return ss.str();
137 }
string UUtils::ToString ( double  number)

Definition at line 139 of file UUtils.cc.

140 {
141  std::stringstream ss;
142  ss << number;
143  return ss.str();
144 }
void UUtils::TransformLimits ( UVector3 min,
UVector3 max,
const UTransform3D transformation 
)

Definition at line 29 of file UUtils.cc.

References UTransform3D::GlobalPoint(), UVector3::Set(), UVector3::x, UVector3::y, and UVector3::z.

30 {
31  // The goal of this method is to convert the quantities min and max (representing the
32  // bounding box of a given solid in its local frame) to the main frame, using
33  // "transformation"
34  UVector3 vertices[8] = // Detemination of the vertices thanks to the extension of each solid:
35  {
36  UVector3(min.x, min.y, min.z), // 1st vertice:
37  UVector3(min.x, max.y, min.z), // 2nd vertice:
38  UVector3(max.x, max.y, min.z),
39  UVector3(max.x, min.y, min.z),
40  UVector3(min.x, min.y, max.z),
41  UVector3(min.x, max.y, max.z),
42  UVector3(max.x, max.y, max.z),
43  UVector3(max.x, min.y, max.z)
44  };
45 
46  min.Set(kInfinity);
47  max.Set(-kInfinity);
48 
49  // Loop on th vertices
50  int limit = sizeof(vertices) / sizeof(UVector3);
51  for (int i = 0 ; i < limit; i++)
52  {
53  // From local frame to the gobal one:
54  // Current positions on the three axis:
55  UVector3 current = transformation.GlobalPoint(vertices[i]);
56 
57  // If need be, replacement of the min & max values:
58  if (current.x > max.x) max.x = current.x;
59  if (current.x < min.x) min.x = current.x;
60 
61  if (current.y > max.y) max.y = current.y;
62  if (current.y < min.y) min.y = current.y;
63 
64  if (current.z > max.z) max.z = current.z;
65  if (current.z < min.z) min.z = current.z;
66  }
67 }
double x
Definition: UVector3.hh:136
UVector3 GlobalPoint(const UVector3 &local) const
void Set(double xx, double yy, double zz)
Definition: UVector3.hh:245
double z
Definition: UVector3.hh:138
double y
Definition: UVector3.hh:137