1212#include " FlatLutEntry.h"
1313
1414#include < Framework/Logger.h>
15+ #include < Framework/RuntimeError.h>
1516
1617#include < cstring>
1718
@@ -89,9 +90,9 @@ void FlatLutData::initialize(const lutHeader_t& header)
8990 size_t totalSize = headerSize + entriesSize;
9091
9192 mData .resize (totalSize);
92-
9393 // Write header at the beginning
9494 std::memcpy (mData .data (), &header, headerSize);
95+ updateRef ();
9596}
9697
9798size_t FlatLutData::getEntryOffset (int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
@@ -105,69 +106,95 @@ size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt
105106 return headerSize + linearIdx * sizeof (lutEntry_t);
106107}
107108
108- lutEntry_t* FlatLutData::getEntry (int nch_bin, int rad_bin, int eta_bin, int pt_bin)
109+ const lutEntry_t* FlatLutData::getEntryRef (int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
109110{
110111 size_t offset = getEntryOffset (nch_bin, rad_bin, eta_bin, pt_bin);
111- return reinterpret_cast <lutEntry_t*>(mData .data () + offset);
112+ return reinterpret_cast <const lutEntry_t*>(mDataRef .data () + offset);
112113}
113114
114- const lutEntry_t* FlatLutData::getEntry (int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
115+ lutEntry_t* FlatLutData::getEntry (int nch_bin, int rad_bin, int eta_bin, int pt_bin)
115116{
116117 size_t offset = getEntryOffset (nch_bin, rad_bin, eta_bin, pt_bin);
117- return reinterpret_cast <const lutEntry_t*>(mData .data () + offset);
118+ return reinterpret_cast <lutEntry_t*>(mData .data () + offset);
118119}
119120
120- FlatLutData FlatLutData::fromBuffer ( const uint8_t * buffer, size_t size)
121+ const lutHeader_t& FlatLutData::getHeaderRef () const
121122{
122- FlatLutData data;
123- // Validate buffer
124- if (size < sizeof (lutHeader_t)) {
125- LOG (fatal) << " Buffer too small for LUT header" ;
126- }
123+ return *reinterpret_cast <const lutHeader_t*>(mDataRef .data ());
124+ }
127125
128- const auto * header = reinterpret_cast <const lutHeader_t*>(buffer);
129- data.mNchBins = header->nchmap .nbins ;
130- data.mRadBins = header->radmap .nbins ;
131- data.mEtaBins = header->etamap .nbins ;
132- data.mPtBins = header->ptmap .nbins ;
126+ lutHeader_t& FlatLutData::getHeader ()
127+ {
128+ return *reinterpret_cast <lutHeader_t*>(mData .data ());
129+ }
133130
134- size_t expectedSize = sizeof (lutHeader_t) + static_cast <size_t >(data.mNchBins ) * data.mRadBins * data.mEtaBins * data.mPtBins * sizeof (lutEntry_t);
131+ void FlatLutData::updateRef ()
132+ {
133+ mDataRef = std::span{mData .data (), mData .size ()};
134+ }
135135
136- if (size < expectedSize) {
137- LOG (fatal) << " Buffer size mismatch: expected " << expectedSize << " , got " << size;
138- }
136+ void FlatLutData::cacheDimensions ()
137+ {
138+ auto const & header = getHeaderRef ();
139+ mNchBins = header.nchmap .nbins ;
140+ mRadBins = header.radmap .nbins ;
141+ mEtaBins = header.etamap .nbins ;
142+ mPtBins = header.ptmap .nbins ;
143+ }
139144
140- // Copy buffer
141- data.mData .resize (size);
142- std::memcpy (data.mData .data (), buffer, size);
145+ void FlatLutData::adopt (const uint8_t * buffer, size_t size)
146+ {
147+ mData .resize (size);
148+ std::memcpy (mData .data (), buffer, size);
149+ updateRef ();
150+ cacheDimensions ();
151+ }
143152
144- return data;
153+ void FlatLutData::view (const uint8_t * buffer, size_t size)
154+ {
155+ mData .clear ();
156+ mDataRef = std::span{buffer, size};
157+ cacheDimensions ();
145158}
146159
147- FlatLutData FlatLutData::fromExternalBuffer ( uint8_t * buffer, size_t size)
160+ void FlatLutData::validateBuffer ( const uint8_t * buffer, size_t size)
148161{
149- FlatLutData data;
150162 // Validate buffer
151163 if (size < sizeof (lutHeader_t)) {
152- LOG (fatal) << " Buffer too small for LUT header" ;
164+ throw framework::runtime_error_f ( " Buffer too small for LUT header: expected at least %zu, got %zu " , sizeof (lutHeader_t), size) ;
153165 }
154166
155167 const auto * header = reinterpret_cast <const lutHeader_t*>(buffer);
156- data. mNchBins = header->nchmap .nbins ;
157- data. mRadBins = header->radmap .nbins ;
158- data. mEtaBins = header->etamap .nbins ;
159- data. mPtBins = header->ptmap .nbins ;
168+ auto mNchBins = header->nchmap .nbins ;
169+ auto mRadBins = header->radmap .nbins ;
170+ auto mEtaBins = header->etamap .nbins ;
171+ auto mPtBins = header->ptmap .nbins ;
160172
161- size_t expectedSize = sizeof (lutHeader_t) + static_cast <size_t >(data. mNchBins ) * data. mRadBins * data. mEtaBins * data. mPtBins * sizeof (lutEntry_t);
173+ size_t expectedSize = sizeof (lutHeader_t) + static_cast <size_t >(mNchBins ) * mRadBins * mEtaBins * mPtBins * sizeof (lutEntry_t);
162174
163175 if (size < expectedSize) {
164- LOG (fatal) << " Buffer size mismatch: expected " << expectedSize << " got " << size;
176+ throw framework::runtime_error_f ( " Buffer size mismatch: expected %zu, got %zu " , expectedSize, size) ;
165177 }
178+ }
166179
167- // Store reference to external buffer (no copy)
168- // WARNING: Caller must ensure buffer lifetime exceeds FlatLutData usage
169- data.mData .assign (buffer, buffer + size);
180+ FlatLutData FlatLutData::AdoptFromBuffer (const uint8_t * buffer, size_t size)
181+ {
182+ validateBuffer (buffer, size);
183+ FlatLutData data;
184+
185+ // Copy buffer
186+ data.adopt (buffer, size);
187+ return data;
188+ }
170189
190+ FlatLutData FlatLutData::ViewFromBuffer (const uint8_t * buffer, size_t size)
191+ {
192+ validateBuffer (buffer, size);
193+ FlatLutData data;
194+
195+ // Store reference to external buffer
196+ // WARNING: Caller must ensure buffer lifetime exceeds FlatLutData usage
197+ data.view (buffer, size);
171198 return data;
172199}
173200
0 commit comments