|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// \file CTFCoder.h |
| 12 | +/// \author ruben.shahoyan@cern.ch |
| 13 | +/// \brief class for entropy encoding/decoding of HMPID data |
| 14 | + |
| 15 | +#ifndef O2_HMPID_CTFCODER_H |
| 16 | +#define O2_HMPID_CTFCODER_H |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <iterator> |
| 20 | +#include <string> |
| 21 | +#include <array> |
| 22 | +#include "DataFormatsHMP/CTF.h" |
| 23 | +#include "DetectorsCommonDataFormats/DetID.h" |
| 24 | +#include "DetectorsBase/CTFCoderBase.h" |
| 25 | +#include "rANS/rans.h" |
| 26 | +#include "HMPIDReconstruction/CTFHelper.h" |
| 27 | + |
| 28 | +class TTree; |
| 29 | + |
| 30 | +namespace o2 |
| 31 | +{ |
| 32 | +namespace hmpid |
| 33 | +{ |
| 34 | + |
| 35 | +class CTFCoder : public o2::ctf::CTFCoderBase |
| 36 | +{ |
| 37 | + public: |
| 38 | + CTFCoder() : o2::ctf::CTFCoderBase(CTF::getNBlocks(), o2::detectors::DetID::HMP) {} |
| 39 | + ~CTFCoder() = default; |
| 40 | + |
| 41 | + /// entropy-encode data to buffer with CTF |
| 42 | + template <typename VEC> |
| 43 | + void encode(VEC& buff, const gsl::span<const Trigger>& trigData, const gsl::span<const Digit>& digData); |
| 44 | + |
| 45 | + /// entropy decode data from buffer with CTF |
| 46 | + template <typename VTRG, typename VDIG> |
| 47 | + void decode(const CTF::base& ec, VTRG& trigVec, VDIG& digVec); |
| 48 | + |
| 49 | + void createCoders(const std::string& dictPath, o2::ctf::CTFCoderBase::OpType op); |
| 50 | + |
| 51 | + private: |
| 52 | + void appendToTree(TTree& tree, CTF& ec); |
| 53 | + void readFromTree(TTree& tree, int entry, std::vector<Trigger>& trigVec, std::vector<Digit>& digVec); |
| 54 | +}; |
| 55 | + |
| 56 | +/// entropy-encode digits and to buffer with CTF |
| 57 | +template <typename VEC> |
| 58 | +void CTFCoder::encode(VEC& buff, const gsl::span<const Trigger>& trigData, const gsl::span<const Digit>& digData) |
| 59 | +{ |
| 60 | + using MD = o2::ctf::Metadata::OptStore; |
| 61 | + // what to do which each field: see o2::ctd::Metadata explanation |
| 62 | + constexpr MD optField[CTF::getNBlocks()] = { |
| 63 | + MD::EENCODE, // BLC_bcIncTrig |
| 64 | + MD::EENCODE, // BLC_orbitIncTrig |
| 65 | + MD::EENCODE, // BLC_entriesDig |
| 66 | + MD::EENCODE, // BLC_ChID |
| 67 | + MD::EENCODE, // BLC_Q |
| 68 | + MD::EENCODE, // BLC_Ph |
| 69 | + MD::EENCODE, // BLC_X |
| 70 | + MD::EENCODE // BLC_Y |
| 71 | + }; |
| 72 | + |
| 73 | + CTFHelper helper(trigData, digData); |
| 74 | + |
| 75 | + // book output size with some margin |
| 76 | + auto szIni = sizeof(CTFHeader) + helper.getSize() * 2. / 3; // will be autoexpanded if needed |
| 77 | + buff.resize(szIni); |
| 78 | + |
| 79 | + auto ec = CTF::create(buff); |
| 80 | + using ECB = CTF::base; |
| 81 | + |
| 82 | + ec->setHeader(helper.createHeader()); |
| 83 | + ec->getANSHeader().majorVersion = 0; |
| 84 | + ec->getANSHeader().minorVersion = 1; |
| 85 | + // at every encoding the buffer might be autoexpanded, so we don't work with fixed pointer ec |
| 86 | +#define ENCODEHMP(beg, end, slot, bits) CTF::get(buff.data())->encode(beg, end, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)].get()); |
| 87 | + // clang-format off |
| 88 | + ENCODEHMP(helper.begin_bcIncTrig(), helper.end_bcIncTrig(), CTF::BLC_bcIncTrig, 0); |
| 89 | + ENCODEHMP(helper.begin_orbitIncTrig(), helper.end_orbitIncTrig(), CTF::BLC_orbitIncTrig, 0); |
| 90 | + ENCODEHMP(helper.begin_entriesDig(), helper.end_entriesDig(), CTF::BLC_entriesDig, 0); |
| 91 | + |
| 92 | + ENCODEHMP(helper.begin_ChID(), helper.end_ChID(), CTF::BLC_ChID, 0); |
| 93 | + ENCODEHMP(helper.begin_Q(), helper.end_Q(), CTF::BLC_Q, 0); |
| 94 | + ENCODEHMP(helper.begin_Ph(), helper.end_Ph(), CTF::BLC_Ph, 0); |
| 95 | + ENCODEHMP(helper.begin_X(), helper.end_X(), CTF::BLC_X, 0); |
| 96 | + ENCODEHMP(helper.begin_Y(), helper.end_Y(), CTF::BLC_Y, 0); |
| 97 | + |
| 98 | + // clang-format on |
| 99 | + CTF::get(buff.data())->print(getPrefix()); |
| 100 | +} |
| 101 | + |
| 102 | +/// decode entropy-encoded data to digits |
| 103 | +template <typename VTRG, typename VDIG> |
| 104 | +void CTFCoder::decode(const CTF::base& ec, VTRG& trigVec, VDIG& digVec) |
| 105 | +{ |
| 106 | + auto header = ec.getHeader(); |
| 107 | + ec.print(getPrefix()); |
| 108 | + std::vector<uint16_t> bcInc, q; |
| 109 | + std::vector<uint32_t> orbitInc, entriesDig; |
| 110 | + std::vector<uint8_t> chID, ph, x, y; |
| 111 | + |
| 112 | +#define DECODEHMP(part, slot) ec.decode(part, int(slot), mCoders[int(slot)].get()) |
| 113 | + // clang-format off |
| 114 | + DECODEHMP(bcInc, CTF::BLC_bcIncTrig); |
| 115 | + DECODEHMP(orbitInc, CTF::BLC_orbitIncTrig); |
| 116 | + DECODEHMP(entriesDig, CTF::BLC_entriesDig); |
| 117 | + |
| 118 | + DECODEHMP(chID, CTF::BLC_ChID); |
| 119 | + DECODEHMP(q, CTF::BLC_Q); |
| 120 | + DECODEHMP(ph, CTF::BLC_Ph); |
| 121 | + DECODEHMP(x, CTF::BLC_X); |
| 122 | + DECODEHMP(y, CTF::BLC_Y); |
| 123 | + // clang-format on |
| 124 | + // |
| 125 | + trigVec.clear(); |
| 126 | + digVec.clear(); |
| 127 | + trigVec.reserve(header.nTriggers); |
| 128 | + digVec.reserve(header.nDigits); |
| 129 | + |
| 130 | + uint32_t digCount = 0; |
| 131 | + o2::InteractionRecord ir(header.firstBC, header.firstOrbit); |
| 132 | + |
| 133 | + for (uint32_t itrig = 0; itrig < header.nTriggers; itrig++) { |
| 134 | + // restore TrigRecord |
| 135 | + if (orbitInc[itrig]) { // non-0 increment => new orbit |
| 136 | + ir.bc = bcInc[itrig]; // bcInc has absolute meaning |
| 137 | + ir.orbit += orbitInc[itrig]; |
| 138 | + } else { |
| 139 | + ir.bc += bcInc[itrig]; |
| 140 | + } |
| 141 | + |
| 142 | + uint32_t firstEntryDig = digVec.size(); |
| 143 | + int8_t chid = 0; |
| 144 | + for (uint32_t id = 0; id < entriesDig[itrig]; id++) { |
| 145 | + chid += chID[digCount]; // 1st digit of trigger was encoded with abs ChID, then increments |
| 146 | + auto& dig = digVec.emplace_back(chid, ph[digCount], x[digCount], y[digCount], q[digCount]); |
| 147 | + digCount++; |
| 148 | + } |
| 149 | + |
| 150 | + trigVec.emplace_back(ir, firstEntryDig, entriesDig[itrig]); |
| 151 | + } |
| 152 | + assert(digCount == header.nDigits); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace hmpid |
| 156 | +} // namespace o2 |
| 157 | + |
| 158 | +#endif // O2_HMP_CTFCODER_H |
0 commit comments