18#ifndef USEFULFUNCTIONS
19#define USEFULFUNCTIONS
30#include <initializer_list>
39#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
40#define __DBGMSG__ __FILENAME__ << ": " << __LINE__ << "\t"
50 return std::sqrt(
dot(p, p) + 1.0);
65 double value = std::sqrt(std::pow(Ekin / mass + 1.0, 2) - 1.0);
66 if (value < std::numeric_limits<double>::epsilon())
67 value = std::sqrt(2 * Ekin / mass);
78 std::string timeUnit(
" [ps]");
81 if (std::abs(time) > 1000) {
83 timeUnit = std::string(
" [ns]");
85 if (std::abs(time) > 1000) {
87 timeUnit = std::string(
" [ms]");
89 }
else if (std::abs(time) < 1.0) {
91 timeUnit = std::string(
" [fs]");
94 std::stringstream timeOutput;
95 timeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << time << timeUnit;
96 return timeOutput.str();
101 std::string sposUnit(
" [m]");
103 if (std::abs(spos) < 1.0) {
105 sposUnit = std::string(
" [mm]");
108 if (std::abs(spos) < 1.0) {
110 sposUnit = std::string(
" [um]");
113 std::stringstream positionOutput;
114 positionOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << spos << sposUnit;
115 return positionOutput.str();
120 std::string sposUnit(
" [m]");
121 double maxPos = std::abs(spos(0));
122 for (
unsigned int i = 1; i < 3u; ++ i) {
123 maxPos = std::max(maxPos, std::abs(spos(i)));
126 std::stringstream positionOutput;
131 sposUnit = std::string(
" [mm]");
137 sposUnit = std::string(
" [um]");
140 positionOutput << std::fixed << std::setprecision(precision)
142 << std::setw(precision + 7) << spos(0) <<
" , "
143 << std::setw(precision + 7) << spos(1) <<
" , "
144 << std::setw(precision + 7) << spos(2)
146 return positionOutput.str();
151 std::string energyUnit(
" [MeV]");
152 double energy = energyInMeV;
154 if (energy > 1000.0) {
156 energyUnit = std::string(
" [GeV]");
157 }
else if (energy < 1.0) {
159 energyUnit = std::string(
" [keV]");
162 energyUnit = std::string(
" [eV]");
166 std::stringstream energyOutput;
167 energyOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << energy << energyUnit;
169 return energyOutput.str();
174 std::string chargeUnit(
" [fC]");
178 if (std::abs(charge) > 1000.0) {
180 chargeUnit = std::string(
" [pC]");
183 if (std::abs(charge) > 1000.0) {
185 chargeUnit = std::string(
" [nC]");
188 if (std::abs(charge) > 1000.0) {
190 chargeUnit = std::string(
" [uC]");
193 std::stringstream chargeOutput;
194 chargeOutput << std::fixed << std::setw(precision + 2) << std::setprecision(precision) << charge << chargeUnit;
196 return chargeOutput.str();
205 if (angle >= 0.0)
return angle;
211 else return (angle >=
min || angle <=
max);
214 std::string
toUpper(
const std::string& str);
230 template<
class IteratorIn,
class IteratorOut>
231 void toString(IteratorIn first, IteratorIn last, IteratorOut out);
233 template <
typename T>
236 template<
typename Enum, std::
size_t N>
238 const std::array<std::pair<Enum, std::string_view>, N>& map,
239 Enum defaultEnum)
noexcept {
240 for (
const auto& [e, s] : map) {
241 if (s == str)
return e;
246 template<
typename Enum, std::
size_t N>
248 const std::array<std::pair<Enum, std::string_view>, N>& map,
249 std::string_view defaultStr)
noexcept {
250 for (
const auto& [key, s] : map) {
251 if (key == e)
return s;
264 unsigned int rewindLinesSDDS(
const std::string& fileName,
double maxSPos,
bool checkForTime =
true);
266 std::string
base64_encode(
const std::string& string_to_encode);
269 template<
typename T,
typename A>
270 T*
c_data(std::vector<T,A>& v) {
return v.empty() ?
static_cast<T*
>(0) : &(v[0]); }
272 template<
typename T,
typename A>
273 T
const*
c_data(std::vector<T,A>
const& v) {
return v.empty() ?
static_cast<T const*
>(0) : &(v[0]); }
278 static_assert(std::is_integral<T>::value,
"Util::toStringWithThousandSep: T must be of integer type");
280 unsigned int powers = std::floor(std::max(0.0,
281 std::log(std::abs((
double)value)) / std::log(10.0))
283 powers -= powers % 3u;
285 std::ostringstream ret;
287 while (powers >= 3u) {
288 T multiplicator = std::pow(T(10), powers);
289 T pre = value / multiplicator;
291 ret << std::setw(3) << std::setfill(
'0') << pre << sep;
295 value -= pre * multiplicator;
302 ret << std::setw(3) << std::setfill(
'0') << value;
310template<
class IteratorIn,
class IteratorOut>
312 std::transform(first, last, out, [](
auto d) {
313 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
constexpr Enum stringToEnum(std::string_view str, const std::array< std::pair< Enum, std::string_view >, N > &map, Enum defaultEnum) noexcept
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
constexpr std::string_view enumToString(Enum e, const std::array< std::pair< Enum, std::string_view >, N > &map, std::string_view defaultStr) noexcept
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)