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