ATLAS Offline Software
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 <CxxUtils/bitscan.h>
14 #include <CxxUtils/ones.h>
15 
16 namespace Muon
17 {
18  namespace nsw
19  {
21  {
25  };
26 
28  {
32  VMM_channels = 64
33  };
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 CxxUtils::maxSetBit(static_cast<std::make_unsigned_t<T> >(number));
149  }
154  template <class T>
155  constexpr int8_t min_bit(const T number) {
156  if (number == 0) return -1;
157  return
158  CxxUtils::count_trailing_zeros(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 }
167 
169 {
170  return (word >> position) & mask;
171 }
172 
173 //inline uint32_t Muon::nsw::helper::set_bits (uint32_t word, uint32_t setbits, uint32_t mask)
174 //{
175 // return word; //TODO
176 //}
177 
178 #endif // not MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
Muon::nsw::VMM_channels
@ VMM_channels
Definition: NSWDecodeHelper.h:32
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
Muon::nsw::min_bit
constexpr int8_t min_bit(const T number)
Returns the most right hand bit which is set in a number.
Definition: NSWDecodeHelper.h:155
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
Muon::nsw::decode_and_advance
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.
Definition: NSWDecodeHelper.h:119
Args
Definition: test_lwtnn_fastgraph.cxx:12
Muon
This class provides conversion from CSC RDO data to CSC Digits.
Definition: TrackSystemController.h:45
Muon::nsw::vmm_channels
vmm_channels
Definition: NSWDecodeHelper.h:28
python.ElectronD3PDObject.Target
Target
Definition: ElectronD3PDObject.py:172
Muon::nsw::fill_bitmask
constexpr Out fill_bitmask(const uint8_t first_bit, const uint8_t num_bits)
Definition: NSWDecodeHelper.h:161
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
Muon::nsw::VMM_per_sFEB
@ VMM_per_sFEB
Definition: NSWDecodeHelper.h:30
Muon::nsw::OFFLINE_CHANNEL_TYPE_PAD
@ OFFLINE_CHANNEL_TYPE_PAD
Definition: NSWDecodeHelper.h:22
Muon::nsw::helper::get_bits
uint32_t get_bits(uint32_t word, uint32_t mask, uint8_t position)
Definition: NSWDecodeHelper.h:168
runBeamSpotCalibration.helper
helper
Definition: runBeamSpotCalibration.py:112
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
ones.h
Construct a bit mask.
Muon::nsw::OFFLINE_CHANNEL_TYPE_WIRE
@ OFFLINE_CHANNEL_TYPE_WIRE
Definition: NSWDecodeHelper.h:24
Muon::nsw::format
std::string format(const std::string &str, const T &arg)
Definition: NSWDecodeHelper.h:43
create_dcsc_inputs_sqlite.arg
list arg
Definition: create_dcsc_inputs_sqlite.py:48
python.selection.number
number
Definition: selection.py:20
Muon::nsw::VMM_per_MMFE8
@ VMM_per_MMFE8
Definition: NSWDecodeHelper.h:29
CxxUtils::count_trailing_zeros
constexpr unsigned count_trailing_zeros(unsigned x)
Count number of trailing zeros.
Definition: bitscan.h:72
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
CxxUtils::maxSetBit
constexpr int maxSetBit(const T x)
Returns the position (counting from least-significant-bit=0) of the most significant set bit.
Definition: bitscan.h:174
Muon::nsw::VMM_per_pFEB
@ VMM_per_pFEB
Definition: NSWDecodeHelper.h:31
Muon::nsw::bit_slice
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.
Definition: NSWDecodeHelper.h:80
Muon::nsw::channel_type
channel_type
Definition: NSWDecodeHelper.h:21
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
Muon::nsw::OFFLINE_CHANNEL_TYPE_STRIP
@ OFFLINE_CHANNEL_TYPE_STRIP
Definition: NSWDecodeHelper.h:23
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
str
Definition: BTagTrackIpAccessor.cxx:11
checkFileSG.words
words
Definition: checkFileSG.py:76
bitscan.h
Bit scanning functions.
Muon::nsw::max_bit
constexpr int8_t max_bit(const T number)
Returns the most left hand bit which is set in a number.
Definition: NSWDecodeHelper.h:147
python.CaloScaleNoiseConfig.args
args
Definition: CaloScaleNoiseConfig.py:80
Muon::nsw::decode_at_loc
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.
Definition: NSWDecodeHelper.h:137
python.LArRawChannelBuilderCrestConfig.Source
Source
Definition: LArRawChannelBuilderCrestConfig.py:115