|
24 | 24 |
|
25 | 25 | #include <TPDGCode.h> |
26 | 26 |
|
| 27 | +#include <sys/types.h> |
| 28 | + |
27 | 29 | #include <algorithm> |
28 | 30 | #include <cmath> |
29 | 31 | #include <concepts> |
@@ -279,6 +281,43 @@ inline float unBinLogSigned(T binned, float magMin, float magMax) |
279 | 281 | return sign * mag; |
280 | 282 | } |
281 | 283 |
|
| 284 | +template <typename T> |
| 285 | +inline int unBinSign(T binned) |
| 286 | +{ |
| 287 | + static_assert(std::is_unsigned_v<T>, "unBinSign requires an unsigned storage type"); |
| 288 | + constexpr uint64_t TotalBits = sizeof(T) * 8; |
| 289 | + constexpr T SignMask = static_cast<T>(uint64_t{1} << (TotalBits - 1)); |
| 290 | + return (binned & SignMask) ? -1 : 1; |
| 291 | +} |
| 292 | + |
| 293 | +template <typename T> |
| 294 | +inline T binLogUnsigned(float value, float magMin, float magMax) |
| 295 | +{ |
| 296 | + static_assert(std::is_unsigned_v<T>, "binLogUnsigned requires an unsigned storage type"); |
| 297 | + constexpr uint64_t TotalBits = sizeof(T) * 8; |
| 298 | + constexpr uint64_t Levels = uint64_t{1} << TotalBits; // number of representable values, e.g. 65536 for uint16_t |
| 299 | + float mag = std::clamp(value, magMin, magMax); |
| 300 | + float logLo = std::log(magMin); |
| 301 | + float logHi = std::log(magMax); |
| 302 | + float step = (logHi - logLo) / static_cast<float>(Levels - 1); |
| 303 | + auto idx = static_cast<uint64_t>(std::round((std::log(mag) - logLo) / step)); |
| 304 | + idx = std::clamp(idx, uint64_t{0}, Levels - 1); |
| 305 | + return static_cast<T>(idx); |
| 306 | +} |
| 307 | + |
| 308 | +template <typename T> |
| 309 | +inline float unBinLogUnsigned(T binned, float magMin, float magMax) |
| 310 | +{ |
| 311 | + constexpr uint64_t TotalBits = sizeof(T) * 8; |
| 312 | + constexpr uint64_t Levels = uint64_t{1} << TotalBits; |
| 313 | + uint64_t idx = binned; |
| 314 | + float logLo = std::log(magMin); |
| 315 | + float logHi = std::log(magMax); |
| 316 | + float step = (logHi - logLo) / static_cast<float>(Levels - 1); |
| 317 | + float mag = std::exp(logLo + static_cast<float>(idx) * step); |
| 318 | + return mag; |
| 319 | +} |
| 320 | + |
282 | 321 | }; // namespace utils |
283 | 322 | }; // namespace o2::analysis::femto |
284 | 323 | // |
|
0 commit comments