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 
10 #include <sstream>
11 #include <span>
12 
13 namespace Muon
14 {
15  namespace nsw
16  {
18  {
22  };
23 
25  {
29  VMM_channels = 64
30  };
31 
32  namespace helper
33  {
34  uint32_t get_bits (uint32_t word, uint32_t mask, uint8_t position);
35  //uint32_t set_bits (uint32_t word, uint32_t mask, uint8_t position);
36  }
37 
38  // Helper function to replace placeholders in a string
39  template<typename T>
40  std::string format(const std::string& str, const T& arg) {
41  std::stringstream ss;
42  std::size_t pos = str.find("{}");
43  if (pos == std::string::npos) {
44  ss << str;
45  } else {
46  ss << str.substr(0, pos) << arg << str.substr(pos + 2);
47  }
48  return ss.str();
49  }
50 
51  // Variadic template
52  template<typename T, typename... Args>
53  std::string format(const std::string& str, const T& arg, const Args&... args) {
54  std::stringstream ss;
55  std::size_t pos = str.find("{}");
56  if (pos == std::string::npos) {
57  ss << str;
58  } else {
59  ss << str.substr(0, pos) << arg << format(str.substr(pos + 2), args...);
60  }
61  return ss.str();
62  }
63 
76  template <typename Target, typename Source>
77  Target bit_slice(const std::span<const Source> words, const std::size_t start, const std::size_t end) {
78  // start and end are the positions in the entire stream
79  // start and end included;
80  const auto wordSize = sizeof(Source) * 8;
81  auto s = Target{};
82  const auto n = end / wordSize;
83  const auto m = start / wordSize;
84 
85  if (end < start) {
86  throw std::runtime_error(format("End must be larger than start ({} vs {})", start, end));
87  }
88  if (n >= std::size(words)) {
89  throw std::runtime_error(format("End index ({}) out of range (number of words: {}, maximum allowed index: {})", n, std::size(words), std::size(words) - 1));
90  }
91  if (sizeof(Target) < sizeof(Source) * (n - m + 1)) {
92  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)));
93  }
94 
95  for (auto i = m; i <= n; ++i) {
96  s = (s << wordSize) + words[i];
97  }
98  s >>= (n + 1) * wordSize - (end + 1);
99  // len = end - start + 1
100  const Target mask = ((Target{1}) << (end - start + 1)) - 1;
101  s &= mask;
102  return s;
103  }
104 
115  template <typename Target, typename Source>
116  constexpr Target decode_and_advance(const std::span<const Source> words, std::size_t& start, const std::size_t size) {
117  const auto res = bit_slice<Target, Source>(words, start, start + size - 1);
118  start += size;
119  return res;
120  }
121 
133  template <typename Target, typename Source>
134  constexpr Target decode_at_loc(const std::span<const Source> words, std::size_t& start, const int offset, const std::size_t size) {
135  const auto res = bit_slice<Target, Source>(words, start + offset, start + size + offset - 1);
136  return res;
137  }
138 
143  template <class T>
144  constexpr int8_t max_bit(const T &number) {
145  constexpr int8_t num_bits = sizeof(number) * 8 - 1;
146  for (int8_t bit = num_bits; bit >= 0; --bit) {
147  if (number & (1u << bit))
148  return bit;
149  }
150  return -1;
151  }
156  template <class T>
157  constexpr int8_t min_bit(const T &number) {
158  constexpr int8_t num_bits = sizeof(number) * 8 - 1;
159  for (size_t bit = 0; bit <= num_bits; ++bit) {
160  if (number & (1u << bit))
161  return bit;
162  }
163  return -1;
164  }
165  template <class Out>
166  constexpr Out fill_bitmask(const uint8_t first_bit, const uint8_t num_bits) {
167  Out mask {0};
168  for (uint8_t bit = first_bit; bit <= first_bit + num_bits ; ++bit){
169  mask |= (1<<bit);
170  }
171  return mask;
172  }
173 
174  }
175 }
176 
178 {
179  return (word >> position) & mask;
180 }
181 
182 //inline uint32_t Muon::nsw::helper::set_bits (uint32_t word, uint32_t setbits, uint32_t mask)
183 //{
184 // return word; //TODO
185 //}
186 
187 #endif // not MUONNSWCOMMONDECODE_NSWDECODEHELPER_H
Muon::nsw::VMM_channels
@ VMM_channels
Definition: NSWDecodeHelper.h:29
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
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:144
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:575
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
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:116
Args
Definition: test_lwtnn_fastgraph.cxx:12
Muon
This class provides conversion from CSC RDO data to CSC Digits.
Definition: TrackSystemController.h:49
Muon::nsw::vmm_channels
vmm_channels
Definition: NSWDecodeHelper.h:25
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
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:166
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:27
Muon::nsw::OFFLINE_CHANNEL_TYPE_PAD
@ OFFLINE_CHANNEL_TYPE_PAD
Definition: NSWDecodeHelper.h:19
Muon::nsw::helper::get_bits
uint32_t get_bits(uint32_t word, uint32_t mask, uint8_t position)
Definition: NSWDecodeHelper.h:177
runBeamSpotCalibration.helper
helper
Definition: runBeamSpotCalibration.py:112
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
Muon::nsw::OFFLINE_CHANNEL_TYPE_WIRE
@ OFFLINE_CHANNEL_TYPE_WIRE
Definition: NSWDecodeHelper.h:21
Muon::nsw::format
std::string format(const std::string &str, const T &arg)
Definition: NSWDecodeHelper.h:40
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:26
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:157
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Muon::nsw::VMM_per_pFEB
@ VMM_per_pFEB
Definition: NSWDecodeHelper.h:28
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:77
Muon::nsw::channel_type
channel_type
Definition: NSWDecodeHelper.h:18
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
Muon::nsw::OFFLINE_CHANNEL_TYPE_STRIP
@ OFFLINE_CHANNEL_TYPE_STRIP
Definition: NSWDecodeHelper.h:20
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
str
Definition: BTagTrackIpAccessor.cxx:11
checkFileSG.words
words
Definition: checkFileSG.py:76
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:134
python.LArRawChannelBuilderCrestConfig.Source
Source
Definition: LArRawChannelBuilderCrestConfig.py:115