-
Notifications
You must be signed in to change notification settings - Fork 646
Expand file tree
/
Copy pathFlatLutEntry.cxx
More file actions
270 lines (227 loc) · 7.61 KB
/
FlatLutEntry.cxx
File metadata and controls
270 lines (227 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "FlatLutEntry.h"
#include <Framework/Logger.h>
#include <Framework/RuntimeError.h>
#include <cstring>
namespace o2::delphes
{
float map_t::fracPositionWithinBin(float val) const
{
float width = (max - min) / nbins;
int bin;
float returnVal = 0.5f;
if (log) {
bin = static_cast<int>((std::log10(val) - min) / width);
returnVal = ((std::log10(val) - min) / width) - bin;
} else {
bin = static_cast<int>((val - min) / width);
returnVal = val / width - bin;
}
return returnVal;
}
int map_t::find(float val) const
{
float width = (max - min) / nbins;
int bin;
if (log) {
bin = static_cast<int>((std::log10(val) - min) / width);
} else {
bin = static_cast<int>((val - min) / width);
}
if (bin < 0) {
return 0;
}
if (bin > nbins - 1) {
return nbins - 1;
}
return bin;
}
void map_t::print() const
{
LOGF(info, "nbins = %d, min = %f, max = %f, log = %s \n", nbins, min, max, log ? "on" : "off");
}
bool lutHeader_t::check_version() const
{
return (version == LUTCOVM_VERSION);
}
void lutHeader_t::print() const
{
LOGF(info, " version: %d \n", version);
LOGF(info, " pdg: %d \n", pdg);
LOGF(info, " field: %f \n", field);
LOGF(info, " nchmap: ");
nchmap.print();
LOGF(info, " radmap: ");
radmap.print();
LOGF(info, " etamap: ");
etamap.print();
LOGF(info, " ptmap: ");
ptmap.print();
}
void FlatLutData::initialize(const lutHeader_t& header)
{
mNchBins = header.nchmap.nbins;
mRadBins = header.radmap.nbins;
mEtaBins = header.etamap.nbins;
mPtBins = header.ptmap.nbins;
size_t headerSize = sizeof(lutHeader_t);
size_t numEntries = static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins;
size_t entriesSize = numEntries * sizeof(lutEntry_t);
size_t totalSize = headerSize + entriesSize;
mData.resize(totalSize);
// Write header at the beginning
std::memcpy(mData.data(), &header, headerSize);
updateRef();
}
size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
{
size_t headerSize = sizeof(lutHeader_t);
// Linear index: nch varies slowest, pt varies fastest
// idx = nch * (rad*eta*pt) + rad * (eta*pt) + eta * pt + pt
size_t linearIdx = static_cast<size_t>(nch_bin) * (mRadBins * mEtaBins * mPtBins) + static_cast<size_t>(rad_bin) * (mEtaBins * mPtBins) + static_cast<size_t>(eta_bin) * mPtBins + static_cast<size_t>(pt_bin);
return headerSize + linearIdx * sizeof(lutEntry_t);
}
const lutEntry_t* FlatLutData::getEntryRef(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const
{
size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin);
return reinterpret_cast<const lutEntry_t*>(mDataRef.data() + offset);
}
lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin)
{
size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin);
return reinterpret_cast<lutEntry_t*>(mData.data() + offset);
}
const lutHeader_t& FlatLutData::getHeaderRef() const
{
return *reinterpret_cast<const lutHeader_t*>(mDataRef.data());
}
lutHeader_t& FlatLutData::getHeader()
{
return *reinterpret_cast<lutHeader_t*>(mData.data());
}
void FlatLutData::updateRef()
{
mDataRef = std::span{mData.data(), mData.size()};
}
void FlatLutData::cacheDimensions()
{
auto const& header = getHeaderRef();
mNchBins = header.nchmap.nbins;
mRadBins = header.radmap.nbins;
mEtaBins = header.etamap.nbins;
mPtBins = header.ptmap.nbins;
}
void FlatLutData::resetDimensions()
{
mNchBins = 0;
mRadBins = 0;
mEtaBins = 0;
mPtBins = 0;
}
void FlatLutData::adopt(const uint8_t* buffer, size_t size)
{
mData.resize(size);
std::memcpy(mData.data(), buffer, size);
updateRef();
cacheDimensions();
}
void FlatLutData::view(const uint8_t* buffer, size_t size)
{
mData.clear();
mDataRef = std::span{buffer, size};
cacheDimensions();
}
void FlatLutData::validateBuffer(const uint8_t* buffer, size_t size)
{
auto header = PreviewHeader(buffer, size);
auto mNchBins = header.nchmap.nbins;
auto mRadBins = header.radmap.nbins;
auto mEtaBins = header.etamap.nbins;
auto mPtBins = header.ptmap.nbins;
size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins * sizeof(lutEntry_t);
if (size < expectedSize) {
throw framework::runtime_error_f("Buffer size mismatch: expected %zu, got %zu", expectedSize, size);
}
}
lutHeader_t FlatLutData::PreviewHeader(const uint8_t* buffer, size_t size)
{
if (size < sizeof(lutHeader_t)) {
throw framework::runtime_error_f("Buffer too small for LUT header: expected at least %zu, got %zu", sizeof(lutHeader_t), size);
}
const auto* header = reinterpret_cast<const lutHeader_t*>(buffer);
if (!header->check_version()) {
throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, header->version);
}
return *header;
}
FlatLutData FlatLutData::AdoptFromBuffer(const uint8_t* buffer, size_t size)
{
validateBuffer(buffer, size);
FlatLutData data;
// Copy buffer
data.adopt(buffer, size);
return data;
}
FlatLutData FlatLutData::ViewFromBuffer(const uint8_t* buffer, size_t size)
{
validateBuffer(buffer, size);
FlatLutData data;
// Store reference to external buffer
// WARNING: Caller must ensure buffer lifetime exceeds FlatLutData usage
data.view(buffer, size);
return data;
}
FlatLutData FlatLutData::ViewFromBuffer(std::span<std::byte> const& span)
{
return ViewFromBuffer(reinterpret_cast<const uint8_t*>(span.data()), span.size_bytes());
}
bool FlatLutData::isLoaded() const
{
return ((!mData.empty()) || (!mDataRef.empty()));
}
lutHeader_t FlatLutData::PreviewHeader(std::ifstream& file, const char* filename)
{
lutHeader_t tempHeader;
file.read(reinterpret_cast<char*>(&tempHeader), sizeof(lutHeader_t));
if (file.gcount() != static_cast<std::streamsize>(sizeof(lutHeader_t))) {
throw framework::runtime_error_f("Failed to read LUT header from %s", filename);
}
if (!tempHeader.check_version()) {
throw framework::runtime_error_f("LUT header version mismatch: expected %d, got %d", LUTCOVM_VERSION, tempHeader.version);
}
return tempHeader;
}
FlatLutData FlatLutData::loadFromFile(std::ifstream& file, const char* filename)
{
// Read header first
lutHeader_t tempHeader = PreviewHeader(file, filename);
FlatLutData data;
// Initialize flat data structure
data.initialize(tempHeader);
// Read all entries sequentially into flat buffer
size_t headerSize = sizeof(lutHeader_t);
size_t numEntries = static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins;
size_t entriesSize = numEntries * sizeof(lutEntry_t);
file.read(reinterpret_cast<char*>(data.data() + headerSize), entriesSize);
if (file.gcount() != static_cast<std::streamsize>(entriesSize)) {
throw framework::runtime_error_f("Failed to read LUT entries from %s: expected %zu bytes, got %zu", filename, entriesSize, static_cast<size_t>(file.gcount()));
}
LOGF(info, "Successfully loaded LUT from %s: %zu entries", filename, numEntries);
return data;
}
void FlatLutData::reset()
{
mData.clear();
updateRef();
resetDimensions();
}
} // namespace o2::delphes