OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Util.h
Go to the documentation of this file.
1 #ifndef USEFULFUNCTIONS
2 #define USEFULFUNCTIONS
3 
4 #include "Algorithms/Vektor.h"
6 
7 #include <string>
8 #include <cstring>
9 #include <sstream>
10 #include <type_traits>
11 #include <functional>
12 
13 // ------- DON'T DELETE: start --------
14 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
15 #define __DBGMSG__ __FILENAME__ << ": " << __LINE__ << "\t"
16 // ------- DON'T DELETE: end --------
17 
18 namespace Util {
19  std::string getGitRevision();
20 
21  double erfinv(double x);
22 
23  inline
24  double getGamma(Vector_t p) {
25  return sqrt(dot(p, p) + 1.0);
26  }
27 
28  inline
29  double getEnergy(Vector_t p, double mass) {
30  return (getGamma(p) - 1.0) * mass;
31  }
32 
33  inline
34  double getP(double E, double mass) {
35  double gamma = E / mass + 1;
36  return sqrt(std::pow(gamma, 2.0) - 1.0);
37  }
38 
39  inline
40  std::string getTimeString(double time, unsigned int precision = 3) {
41  std::string timeUnit(" [ps]");
42 
43  time *= 1e12;
44  if (std::abs(time) > 1000) {
45  time /= 1000;
46  timeUnit = std::string(" [ns]");
47 
48  if (std::abs(time) > 1000) {
49  time /= 1000;
50  timeUnit = std::string(" [ms]");
51  }
52  } else if (std::abs(time) < 1.0) {
53  time *= 1000;
54  timeUnit = std::string(" [fs]");
55  }
56 
57  std::stringstream timeOutput;
58  timeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << time << timeUnit;
59  return timeOutput.str();
60  }
61 
62  inline
63  std::string getLengthString(double spos, unsigned int precision = 3) {
64  std::string sposUnit(" [m]");
65 
66  if (std::abs(spos) < 1.0) {
67  spos *= 1000.0;
68  sposUnit = std::string(" [mm]");
69  }
70 
71  if (std::abs(spos) < 1.0) {
72  spos *= 1000.0;
73  sposUnit = std::string(" [um]");
74  }
75 
76  std::stringstream positionOutput;
77  positionOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << spos << sposUnit;
78  return positionOutput.str();
79  }
80 
81  inline
82  std::string getLengthString(Vector_t spos, unsigned int precision = 3) {
83  std::string sposUnit(" [m]");
84  double maxPos = std::abs(spos(0));
85  for (unsigned int i = 1; i < 3u; ++ i) {
86  maxPos = std::max(maxPos, std::abs(spos(i)));
87  }
88 
89  std::stringstream positionOutput;
90 
91  if (maxPos < 1.0) {
92  maxPos *= 1000.0;
93  spos *= 1000.0;
94  sposUnit = std::string(" [mm]");
95  }
96 
97  if (maxPos < 1.0) {
98  maxPos *= 1000.0;
99  spos *= 1000.0;
100  sposUnit = std::string(" [um]");
101  }
102 
103  positionOutput << std::fixed << std::setprecision(precision)
104  << "( "
105  << std::setw(precision + 7) << spos(0) << " , "
106  << std::setw(precision + 7) << spos(1) << " , "
107  << std::setw(precision + 7) << spos(2)
108  << " )" << sposUnit;
109  return positionOutput.str();
110  }
111 
112  inline
113  std::string getEnergyString(double energyInMeV, unsigned int precision = 3) {
114  std::string energyUnit(" [MeV]");
115  double energy = energyInMeV;
116 
117  if (energy > 1000.0) {
118  energy /= 1000.0;
119  energyUnit = std::string(" [GeV]");
120  } else if (energy < 1.0) {
121  energy *= 1000.0;
122  energyUnit = std::string(" [keV]");
123  if (energy < 1.0) {
124  energy *= 1000.0;
125  energyUnit = std::string(" [eV]");
126  }
127  }
128 
129  std::stringstream energyOutput;
130  energyOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << energy << energyUnit;
131 
132  return energyOutput.str();
133  }
134 
135  inline
136  std::string getChargeString(double charge, unsigned int precision = 3) {
137  std::string chargeUnit(" [fC]");
138 
139  charge *= 1e15;
140 
141  if (std::abs(charge) > 1000.0) {
142  charge /= 1000.0;
143  chargeUnit = std::string(" [pC]");
144  }
145 
146  if (std::abs(charge) > 1000.0) {
147  charge /= 1000.0;
148  chargeUnit = std::string(" [nC]");
149  }
150 
151  if (std::abs(charge) > 1000.0) {
152  charge /= 1000.0;
153  chargeUnit = std::string(" [uC]");
154  }
155 
156  std::stringstream chargeOutput;
157  chargeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << charge << chargeUnit;
158 
159  return chargeOutput.str();
160  }
161 
162  Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &elementName = "");
163 
164  std::string toUpper(const std::string &str);
165 
166  template <typename T>
167  std::string toStringWithThousandSep(T value, char sep = '\'');
168 
170  {
171  long double sum;
172  long double correction;
174 
175  KahanAccumulation& operator+=(double value);
176  };
177 
178  unsigned int rewindLinesSDDS(const std::string &fileName, double maxSPos, bool checkForTime = true);
179 
180  std::string base64_encode(const std::string &string_to_encode);//unsigned char const* , unsigned int len);
181  std::string base64_decode(std::string const& s);
182 }
183 
184 template <typename T>
185 std::string Util::toStringWithThousandSep(T value, char sep) {
186  static_assert(std::is_integral<T>::value, "Util::toStringWithThousandSep: T must be of integer type");
187 
188  unsigned int powers = std::floor(std::max(0.0,
189  std::log(std::abs((double)value)) / std::log(10.0))
190  );
191  powers -= powers % 3u;
192 
193  std::ostringstream ret;
194  unsigned int i = 0;
195  while (powers >= 3u) {
196  T multiplicator = std::pow(T(10), powers);
197  T pre = value / multiplicator;
198  if (i > 0) {
199  ret << std::setw(3) << std::setfill('0') << pre << sep;
200  } else {
201  ret << pre << sep;
202  }
203  value -= pre * multiplicator;
204 
205  powers -= 3;
206  ++ i;
207  }
208 
209  if (i > 0) {
210  ret << std::setw(3) << std::setfill('0') << value;
211  } else {
212  ret << value;
213  }
214 
215  return ret.str();
216 }
217 
218 #endif
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::string toStringWithThousandSep(T value, char sep= '\'')
Definition: Util.h:185
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &elementName)
Definition: Util.cpp:100
Definition: TSVMeta.h:24
Definition: rbendmap.h:8
double getP(double E, double mass)
Definition: Util.h:34
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
std::string getLengthString(double spos, unsigned int precision=3)
Definition: Util.h:63
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition: TpsMath.h:182
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
std::string getChargeString(double charge, unsigned int precision=3)
Definition: Util.h:136
double getGamma(Vector_t p)
Definition: Util.h:24
KahanAccumulation & operator+=(double value)
Definition: Util.cpp:143
std::string getTimeString(double time, unsigned int precision=3)
Definition: Util.h:40
long double correction
Definition: Util.h:172
std::string base64_encode(const std::string &string_to_encode)
Definition: Util.cpp:300
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
long double sum
Definition: Util.h:171
std::string getGitRevision()
Definition: Util.cpp:15
double getEnergy(Vector_t p, double mass)
Definition: Util.h:29
double erfinv(double x)
Definition: Util.cpp:39
unsigned int rewindLinesSDDS(const std::string &fileName, double maxSPos, bool checkForTime)
rewind the SDDS file such that the spos of the last step is less or equal to maxSPos ...
Definition: Util.cpp:154
std::string base64_decode(std::string const &encoded_string)
Definition: Util.cpp:344
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
Definition: Util.h:113
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:816