18#ifndef USEFULFUNCTIONS
19#define USEFULFUNCTIONS
29#include <initializer_list>
36#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
37#define __DBGMSG__ __FILENAME__ << ": " << __LINE__ << "\t"
47 return std::sqrt(
dot(p, p) + 1.0);
62 double value = std::sqrt(std::pow(Ekin / mass + 1.0, 2) - 1.0);
63 if (value < std::numeric_limits<double>::epsilon())
64 value = std::sqrt(2 * Ekin / mass);
75 std::string timeUnit(
" [ps]");
78 if (std::abs(time) > 1000) {
80 timeUnit = std::string(
" [ns]");
82 if (std::abs(time) > 1000) {
84 timeUnit = std::string(
" [ms]");
86 }
else if (std::abs(time) < 1.0) {
88 timeUnit = std::string(
" [fs]");
91 std::stringstream timeOutput;
92 timeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << time << timeUnit;
93 return timeOutput.str();
98 std::string sposUnit(
" [m]");
100 if (std::abs(spos) < 1.0) {
102 sposUnit = std::string(
" [mm]");
105 if (std::abs(spos) < 1.0) {
107 sposUnit = std::string(
" [um]");
110 std::stringstream positionOutput;
111 positionOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << spos << sposUnit;
112 return positionOutput.str();
117 std::string sposUnit(
" [m]");
118 double maxPos = std::abs(spos(0));
119 for (
unsigned int i = 1; i < 3u; ++ i) {
120 maxPos = std::max(maxPos, std::abs(spos(i)));
123 std::stringstream positionOutput;
128 sposUnit = std::string(
" [mm]");
134 sposUnit = std::string(
" [um]");
137 positionOutput << std::fixed << std::setprecision(precision)
139 << std::setw(precision + 7) << spos(0) <<
" , "
140 << std::setw(precision + 7) << spos(1) <<
" , "
141 << std::setw(precision + 7) << spos(2)
143 return positionOutput.str();
148 std::string energyUnit(
" [MeV]");
149 double energy = energyInMeV;
151 if (energy > 1000.0) {
153 energyUnit = std::string(
" [GeV]");
154 }
else if (energy < 1.0) {
156 energyUnit = std::string(
" [keV]");
159 energyUnit = std::string(
" [eV]");
163 std::stringstream energyOutput;
164 energyOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << energy << energyUnit;
166 return energyOutput.str();
171 std::string chargeUnit(
" [fC]");
175 if (std::abs(charge) > 1000.0) {
177 chargeUnit = std::string(
" [pC]");
180 if (std::abs(charge) > 1000.0) {
182 chargeUnit = std::string(
" [nC]");
185 if (std::abs(charge) > 1000.0) {
187 chargeUnit = std::string(
" [uC]");
190 std::stringstream chargeOutput;
191 chargeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << charge << chargeUnit;
193 return chargeOutput.str();
202 if (angle >= 0.0)
return angle;
208 else return (angle >=
min || angle <=
max);
211 std::string
toUpper(
const std::string& str);
227 template<
class IteratorIn,
class IteratorOut>
228 void toString(IteratorIn first, IteratorIn last, IteratorOut out);
230 template <
typename T>
241 unsigned int rewindLinesSDDS(
const std::string& fileName,
double maxSPos,
bool checkForTime =
true);
243 std::string
base64_encode(
const std::string& string_to_encode);
246 template<
typename T,
typename A>
247 T*
c_data(std::vector<T,A>& v) {
return v.empty() ?
static_cast<T*
>(0) : &(v[0]); }
249 template<
typename T,
typename A>
250 T
const*
c_data(std::vector<T,A>
const& v) {
return v.empty() ?
static_cast<T const*
>(0) : &(v[0]); }
255 static_assert(std::is_integral<T>::value,
"Util::toStringWithThousandSep: T must be of integer type");
257 unsigned int powers = std::floor(std::max(0.0,
258 std::log(std::abs((
double)value)) / std::log(10.0))
260 powers -= powers % 3u;
262 std::ostringstream ret;
264 while (powers >= 3u) {
265 T multiplicator = std::pow(T(10), powers);
266 T pre = value / multiplicator;
268 ret << std::setw(3) << std::setfill(
'0') << pre << sep;
272 value -= pre * multiplicator;
279 ret << std::setw(3) << std::setfill(
'0') << value;
287template<
class IteratorIn,
class IteratorOut>
289 std::transform(first, last, out, [](
auto d) {
290 std::ostringstream stm;
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
constexpr double two_pi
The value of.
std::string combineFilePath(std::initializer_list< std::string > ilist)
bool angleBetweenAngles(const double angle, const double min, const double max)
check if angle (in rad and in range [0,2pi]) is within [min, max]
std::string doubleVectorToString(const std::vector< double > &v)
std::string getChargeString(double charge, unsigned int precision=3)
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
std::string boolVectorToUpperString(const std::vector< bool > &b)
double getKineticEnergy(Vector_t p, double mass)
void toString(IteratorIn first, IteratorIn last, IteratorOut out)
double convertMomentumEVoverCToBetaGamma(double p, double mass)
double getBetaGamma(double Ekin, double mass)
std::string toUpper(const std::string &str)
T * c_data(std::vector< T, A > &v)
std::string base64_decode(std::string const &encoded_string)
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
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
void checkInt(double real, std::string name, double tolerance)
std::string getTimeString(double time, unsigned int precision=3)
std::string getGitRevision()
std::string base64_encode(const std::string &string_to_encode)
Vector_t getBeta(Vector_t p)
double getGamma(Vector_t p)
std::string boolToUpperString(const bool &b)
std::string getLengthString(double spos, unsigned int precision=3)
double angle_0to2pi(double angle)
convert angle (in rad) to [0,2pi) range, from https://stackoverflow.com/a/29721295
bool isAllDigits(const std::string &str)
std::string toStringWithThousandSep(T value, char sep='\'')
KahanAccumulation & operator+=(double value)
Vektor< double, 3 > Vector_t