18 #ifndef USEFULFUNCTIONS
19 #define USEFULFUNCTIONS
29 #include <initializer_list>
33 #include <type_traits>
36 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
37 #define __DBGMSG__ __FILENAME__ << ": " << __LINE__ << "\t"
63 if (value < std::numeric_limits<double>::epsilon())
75 std::string timeUnit(
" [ps]");
80 timeUnit = std::string(
" [ns]");
84 timeUnit = std::string(
" [ms]");
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]");
102 sposUnit = std::string(
" [mm]");
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]");
119 for (
unsigned int i = 1; i < 3u; ++ 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]");
177 chargeUnit = std::string(
" [pC]");
182 chargeUnit = std::string(
" [nC]");
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;
207 if (min <= max)
return (angle >= min && angle <= max);
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]); }
253 template <
typename T>
255 static_assert(std::is_integral<T>::value,
"Util::toStringWithThousandSep: T must be of integer type");
260 powers -= powers % 3u;
262 std::ostringstream ret;
264 while (powers >= 3u) {
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;
287 template<
class IteratorIn,
class IteratorOut>
289 std::transform(first, last, out, [](
auto d) {
290 std::ostringstream stm;
Tps< T > sqrt(const Tps< T > &x)
Square root.
double angle_0to2pi(double angle)
convert angle (in rad) to [0,2pi) range, from https://stackoverflow.com/a/29721295 ...
double convertMomentumEVoverCToBetaGamma(double p, double mass)
std::string boolVectorToUpperString(const std::vector< bool > &b)
Vector_t getTaitBryantAngles(Quaternion rotation, const std::string &)
item[EANGLE] Entrance edge angle(radians).\item[ROTATION] Rotation of the magnet about its central axis(radians
constexpr double two_pi
The value of .
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
std::string boolToUpperString(const bool &b)
std::string getGitRevision()
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]
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 base64_encode(const std::string &string_to_encode)
T * c_data(std::vector< T, A > &v)
std::string toUpper(const std::string &str)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
std::string getTimeString(double time, unsigned int precision=3)
bool isAllDigits(const std::string &str)
std::string toStringWithThousandSep(T value, char sep= '\'')
Vector_t getBeta(Vector_t p)
double getKineticEnergy(Vector_t p, double mass)
std::string getEnergyString(double energyInMeV, unsigned int precision=3)
double getGamma(Vector_t p)
PETE_TBTree< FnFmod, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > fmod(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
Tps< T > log(const Tps< T > &x)
Natural logarithm.
std::string combineFilePath(std::initializer_list< std::string > ilist)
std::string getChargeString(double charge, unsigned int precision=3)
std::string doubleVectorToString(const std::vector< double > &v)
constexpr double e
The value of .
std::string getLengthString(double spos, unsigned int precision=3)
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
void checkInt(double real, std::string name, double tolerance)
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
std::string base64_decode(std::string const &encoded_string)
FLieGenerator< T, N > real(const FLieGenerator< std::complex< T >, N > &)
Take real part of a complex generator.
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
double getBetaGamma(double Ekin, double mass)
void toString(IteratorIn first, IteratorIn last, IteratorOut out)
KahanAccumulation & operator+=(double value)