|
| 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 | + |
| 16 | +#include <cstring> |
| 17 | + |
| 18 | +namespace o2::delphes |
| 19 | +{ |
| 20 | + |
| 21 | +float map_t::fracPositionWithinBin(float val) const |
| 22 | +{ |
| 23 | + float width = (max - min) / nbins; |
| 24 | + int bin; |
| 25 | + float returnVal = 0.5f; |
| 26 | + if (log) { |
| 27 | + bin = static_cast<int>((std::log10(val) - min) / width); |
| 28 | + returnVal = ((std::log10(val) - min) / width) - bin; |
| 29 | + } else { |
| 30 | + bin = static_cast<int>((val - min) / width); |
| 31 | + returnVal = val / width - bin; |
| 32 | + } |
| 33 | + return returnVal; |
| 34 | +} |
| 35 | + |
| 36 | +int map_t::find(float val) const |
| 37 | +{ |
| 38 | + float width = (max - min) / nbins; |
| 39 | + int bin; |
| 40 | + if (log) { |
| 41 | + bin = static_cast<int>((std::log10(val) - min) / width); |
| 42 | + } else { |
| 43 | + bin = static_cast<int>((val - min) / width); |
| 44 | + } |
| 45 | + if (bin < 0) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + if (bin > nbins - 1) { |
| 49 | + return nbins - 1; |
| 50 | + } |
| 51 | + return bin; |
| 52 | +} |
| 53 | + |
| 54 | +void map_t::print() const |
| 55 | +{ |
| 56 | + LOGF(info, "nbins = %d, min = %f, max = %f, log = %s \n", nbins, min, max, log ? "on" : "off"); |
| 57 | +} |
| 58 | + |
| 59 | +bool lutHeader_t::check_version() const |
| 60 | +{ |
| 61 | + return (version == LUTCOVM_VERSION); |
| 62 | +} |
| 63 | + |
| 64 | +void lutHeader_t::print() const |
| 65 | +{ |
| 66 | + LOGF(info, " version: %d \n", version); |
| 67 | + LOGF(info, " pdg: %d \n", pdg); |
| 68 | + LOGF(info, " field: %f \n", field); |
| 69 | + LOGF(info, " nchmap: "); |
| 70 | + nchmap.print(); |
| 71 | + LOGF(info, " radmap: "); |
| 72 | + radmap.print(); |
| 73 | + LOGF(info, " etamap: "); |
| 74 | + etamap.print(); |
| 75 | + LOGF(info, " ptmap: "); |
| 76 | + ptmap.print(); |
| 77 | +} |
| 78 | + |
| 79 | +void FlatLutData::initialize(const lutHeader_t& header) |
| 80 | +{ |
| 81 | + mNchBins = header.nchmap.nbins; |
| 82 | + mRadBins = header.radmap.nbins; |
| 83 | + mEtaBins = header.etamap.nbins; |
| 84 | + mPtBins = header.ptmap.nbins; |
| 85 | + |
| 86 | + size_t headerSize = sizeof(lutHeader_t); |
| 87 | + size_t numEntries = static_cast<size_t>(mNchBins) * mRadBins * mEtaBins * mPtBins; |
| 88 | + size_t entriesSize = numEntries * sizeof(lutEntry_t); |
| 89 | + size_t totalSize = headerSize + entriesSize; |
| 90 | + |
| 91 | + mData.resize(totalSize); |
| 92 | + |
| 93 | + // Write header at the beginning |
| 94 | + std::memcpy(mData.data(), &header, headerSize); |
| 95 | +} |
| 96 | + |
| 97 | +size_t FlatLutData::getEntryOffset(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const |
| 98 | +{ |
| 99 | + size_t headerSize = sizeof(lutHeader_t); |
| 100 | + |
| 101 | + // Linear index: nch varies slowest, pt varies fastest |
| 102 | + // idx = nch * (rad*eta*pt) + rad * (eta*pt) + eta * pt + pt |
| 103 | + 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); |
| 104 | + |
| 105 | + return headerSize + linearIdx * sizeof(lutEntry_t); |
| 106 | +} |
| 107 | + |
| 108 | +lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin) |
| 109 | +{ |
| 110 | + size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin); |
| 111 | + return reinterpret_cast<lutEntry_t*>(mData.data() + offset); |
| 112 | +} |
| 113 | + |
| 114 | +const lutEntry_t* FlatLutData::getEntry(int nch_bin, int rad_bin, int eta_bin, int pt_bin) const |
| 115 | +{ |
| 116 | + size_t offset = getEntryOffset(nch_bin, rad_bin, eta_bin, pt_bin); |
| 117 | + return reinterpret_cast<const lutEntry_t*>(mData.data() + offset); |
| 118 | +} |
| 119 | + |
| 120 | +FlatLutData FlatLutData::fromBuffer(const uint8_t* buffer, size_t size) |
| 121 | +{ |
| 122 | + FlatLutData data; |
| 123 | + // Validate buffer |
| 124 | + if (size < sizeof(lutHeader_t)) { |
| 125 | + LOG(fatal) << "Buffer too small for LUT header"; |
| 126 | + } |
| 127 | + |
| 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; |
| 133 | + |
| 134 | + size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins * sizeof(lutEntry_t); |
| 135 | + |
| 136 | + if (size < expectedSize) { |
| 137 | + LOG(fatal) << "Buffer size mismatch: expected " << expectedSize << ", got " << size; |
| 138 | + } |
| 139 | + |
| 140 | + // Copy buffer |
| 141 | + data.mData.resize(size); |
| 142 | + std::memcpy(data.mData.data(), buffer, size); |
| 143 | + |
| 144 | + return data; |
| 145 | +} |
| 146 | + |
| 147 | +FlatLutData FlatLutData::fromExternalBuffer(uint8_t* buffer, size_t size) |
| 148 | +{ |
| 149 | + FlatLutData data; |
| 150 | + // Validate buffer |
| 151 | + if (size < sizeof(lutHeader_t)) { |
| 152 | + LOG(fatal) << "Buffer too small for LUT header"; |
| 153 | + } |
| 154 | + |
| 155 | + 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; |
| 160 | + |
| 161 | + size_t expectedSize = sizeof(lutHeader_t) + static_cast<size_t>(data.mNchBins) * data.mRadBins * data.mEtaBins * data.mPtBins * sizeof(lutEntry_t); |
| 162 | + |
| 163 | + if (size < expectedSize) { |
| 164 | + LOG(fatal) << "Buffer size mismatch: expected " << expectedSize << " got " << size; |
| 165 | + } |
| 166 | + |
| 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); |
| 170 | + |
| 171 | + return data; |
| 172 | +} |
| 173 | + |
| 174 | +} // namespace o2::delphes |
0 commit comments