ATLAS Offline Software
Loading...
Searching...
No Matches
NSWDecodeHelper.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
5#define MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
6
7#include <stdint.h>
8#include <stdexcept>
9#include <type_traits>
10
11#include <sstream>
12#include <span>
13#include <bit>
14#include <CxxUtils/ones.h>
15
16namespace Muon
17{
18 namespace nsw
19 {
26
34
35 namespace helper
36 {
37 uint32_t get_bits (uint32_t word, uint32_t mask, uint8_t position);
38 //uint32_t set_bits (uint32_t word, uint32_t mask, uint8_t position);
39 }
40
41 // Helper function to replace placeholders in a string
42 template<typename T>
43 std::string format(const std::string& str, const T& arg) {
44 std::stringstream ss;
45 std::size_t pos = str.find("{}");
46 if (pos == std::string::npos) {
47 ss << str;
48 } else {
49 ss << str.substr(0, pos) << arg << str.substr(pos + 2);
50 }
51 return ss.str();
52 }
53
54 // Variadic template
55 template<typename T, typename... Args>
56 std::string format(const std::string& str, const T& arg, const Args&... args) {
57 std::stringstream ss;
58 std::size_t pos = str.find("{}");
59 if (pos == std::string::npos) {
60 ss << str;
61 } else {
62 ss << str.substr(0, pos) << arg << format(str.substr(pos + 2), args...);
63 }
64 return ss.str();
65 }
66
79 template <typename Target, typename Source>
80 Target bit_slice(const std::span<const Source> words, const std::size_t start, const std::size_t end) {
81 // start and end are the positions in the entire stream
82 // start and end included;
83 const auto wordSize = sizeof(Source) * 8;
84 auto s = Target{};
85 const auto n = end / wordSize;
86 const auto m = start / wordSize;
87
88 if (end < start) {
89 throw std::runtime_error(format("End must be larger than start ({} vs {})", start, end));
90 }
91 if (n >= std::size(words)) {
92 throw std::runtime_error(format("End index ({}) out of range (number of words: {}, maximum allowed index: {})", n, std::size(words), std::size(words) - 1));
93 }
94 if (sizeof(Target) < sizeof(Source) * (n - m + 1)) {
95 throw std::runtime_error(format("Target type (size {}) too small to fit result of bit_slice given start {} and end {} and source size {}", sizeof(Target), start, end, sizeof(Source)));
96 }
97
98 for (auto i = m; i <= n; ++i) {
99 s = (s << wordSize) + words[i];
100 }
101 s >>= (n + 1) * wordSize - (end + 1);
102 // len = end - start + 1
103 const Target mask = ((Target{1}) << (end - start + 1)) - 1;
104 s &= mask;
105 return s;
106 }
107
118 template <typename Target, typename Source>
119 constexpr Target decode_and_advance(const std::span<const Source> words, std::size_t& start, const std::size_t size) {
120 const auto res = bit_slice<Target, Source>(words, start, start + size - 1);
121 start += size;
122 return res;
123 }
124
136 template <typename Target, typename Source>
137 constexpr Target decode_at_loc(const std::span<const Source> words, std::size_t& start, const int offset, const std::size_t size) {
138 const auto res = bit_slice<Target, Source>(words, start + offset, start + size + offset - 1);
139 return res;
140 }
141
146 template <class T>
147 constexpr int8_t max_bit(const T number) {
148 return std::bit_width(static_cast<std::make_unsigned_t<T> >(number)) - 1;
149 }
150
154 template <class T>
155 constexpr int8_t min_bit(const T number) {
156 if (number == 0) return -1;
157 return
158 std::countr_zero(static_cast<std::make_unsigned_t<T> >(number));
159 }
160 template <class Out>
161 constexpr Out fill_bitmask(const uint8_t first_bit, const uint8_t num_bits) {
162 return CxxUtils::ones<Out> (num_bits) << first_bit;
163 }
164
165
166 uint16_t get_16bxor_crc(const uint32_t * dataPointer, uint32_t dataSize);
167
168 }
169
170}
171
172inline uint32_t Muon::nsw::helper::get_bits (uint32_t word, uint32_t mask, uint8_t position)
173{
174 return (word >> position) & mask;
175}
176
177
178inline uint16_t Muon::nsw::get_16bxor_crc(const uint32_t * dataPointer, uint32_t dataSize) {
179
180 uint16_t crc = 0;
181 // checking if last 16b are 0 padding (possibly added by swROD)
182 bool hasPadding = (dataPointer[dataSize-1] & 0xFFFF)==0; // unsafe if the CRC can be 0x0000 but it should never be the case
183 // one could simply cast the (std::uint32_t*) to a (std::uint16_t*)
184 // but then we might need to handle the word swap (system dependent)
185 for (uint32_t i = 0; i<dataSize-1; ++i) {
186 crc = crc ^ ((dataPointer[i] >> 16) & 0xFFFF);
187 crc = crc ^ ((dataPointer[i] >> 0) & 0xFFFF);
188 }
189 // last word can be: 1) 16b CRC + 16b 0padding 2) 16b of valida data + 16b CRC
190 // need to handle case 2)
191 if (!hasPadding) {crc = crc ^ ((dataPointer[dataSize-1] >> 16) & 0xFFFF);}
192
193 return crc;
194}
195
196//inline uint32_t Muon::nsw::helper::set_bits (uint32_t word, uint32_t setbits, uint32_t mask)
197//{
198// return word; //TODO
199//}
200
201#endif // not MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
std::pair< std::vector< unsigned int >, bool > res
static Double_t ss
constexpr T ones(unsigned int n)
Return a bit mask with the lower n bits set.
Definition ones.h:25
uint32_t get_bits(uint32_t word, uint32_t mask, uint8_t position)
constexpr int8_t min_bit(const T number)
Returns the most right hand bit which is set in a number.
uint16_t get_16bxor_crc(const uint32_t *dataPointer, uint32_t dataSize)
Target bit_slice(const std::span< const Source > words, const std::size_t start, const std::size_t end)
Decode bits from data of words.
constexpr int8_t max_bit(const T number)
Returns the most left hand bit which is set in a number.
constexpr Out fill_bitmask(const uint8_t first_bit, const uint8_t num_bits)
std::string format(const std::string &str, const T &arg)
constexpr Target decode_at_loc(const std::span< const Source > words, std::size_t &start, const int offset, const std::size_t size)
Decode bits from data of words at read pointer + offset and NOT advance the read pointer.
constexpr Target decode_and_advance(const std::span< const Source > words, std::size_t &start, const std::size_t size)
Decode bits from data of words and advance the read pointer.
@ OFFLINE_CHANNEL_TYPE_STRIP
@ OFFLINE_CHANNEL_TYPE_PAD
@ OFFLINE_CHANNEL_TYPE_WIRE
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
Construct a bit mask.
std::string number(const double &d, const std::string &s)
Definition utils.cxx:186