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