16#ifndef _ALICEO2_CTFCODER_BASE_H_
17#define _ALICEO2_CTFCODER_BASE_H_
43class ProcessingContext;
70 template <
typename SPAN>
77 template <
typename CTF>
80 template <
typename CTF>
86 LOG_IF(warning, renormedHistogram.empty()) << fmt::format(
"Empty dictionary provided for slot {}, {} will assume literal symbols only", slot, (
op ==
OpType::Encoder ?
"encoding" :
"decoding"));
107 throw std::runtime_error(
"unsupported ANS version");
126 template <
typename T>
127 static bool readFromTree(TTree&
tree,
const std::string brname, T& dest,
int ev = 0);
130 template <
typename CTF>
133 template <
typename CTF,
typename BUF>
136 template <
typename CTF>
172 return diff < 0 ? true : diff >= shift;
176 template <
typename source_IT>
179 template <
typename source_T>
182 template <
typename source_T>
188 template <
typename CTF>
214 auto* br =
tree.GetBranch(brname.c_str());
215 if (br && br->GetEntries() > ev) {
217 br->SetAddress(&
ptr);
226template <
typename CTF>
229 auto buff = readDictionaryFromFile<CTF>(dictPath, mayFail);
234 throw std::runtime_error(
"Failed to create CTF dictionaty");
240template <
typename CTF>
243 std::vector<char> bufVec;
252template <
typename CTF>
255 std::vector<char> bufVec;
256 std::unique_ptr<TFile> fileDict;
257 if (std::filesystem::exists(dictPath)) {
258 fileDict.reset(TFile::Open(dictPath.c_str()));
260 if (!fileDict || fileDict->IsZombie()) {
261 std::string errstr = fmt::format(
"CTF dictionary file {} for detector {} is absent", dictPath,
mDet.
getName());
263 LOGP(info,
"{}, will assume dictionary stored in CTF", errstr);
265 throw std::runtime_error(errstr);
269 std::unique_ptr<TTree>
tree;
270 std::unique_ptr<std::vector<char>> bv((std::vector<char>*)fileDict->GetObjectChecked(
o2::base::NameConf::CCDBOBJECT.data(),
"std::vector<char>"));
276 auto v = loadDictionaryFromTree<CTF>(
tree.get());
282 if (dictHeader.det !=
mDet) {
284 LOGP(error,
"{} contains dictionary vector for {}, expected {}", dictPath, dictHeader.det.getName(),
mDet.
getName());
293 LOGP(info,
"{}, will assume dictionary stored in CTF",
mDet.
getName());
295 LOGP(fatal,
"CTF dictionary file for detector {} is empty",
mDet.
getName());
302template <
typename CTF>
319 const std::string ansVersionString = ic.
options().
get<std::string>(
"ans-version");
320 if (!ansVersionString.empty()) {
322 LOGP(info,
"parsing ansVersionString {} into {}", ansVersionString,
static_cast<std::string
>(
mANSVersion));
324 throw std::invalid_argument(fmt::format(
"Invalid ANS Version {}", ansVersionString));
336 LOGP(info,
"Internal per-TF CTF Dict will be created");
343template <
typename CTF,
typename BUF>
346 auto eeb = CTF::get(
buffer.data());
348 buffer.resize(eeb->size());
354template <
typename CTF>
359 const auto* dict = (std::vector<char>*)obj;
361 LOGP(info,
"Empty dictionary object fetched from CCDB, internal per-TF CTF Dict will be created");
363 std::vector<char> bufVec;
365 auto v = loadDictionaryFromTree<CTF>(
reinterpret_cast<TTree*
>(obj));
376 auto bcshift = trigOffsParam.customOffset[
mDet.getID()];
379 LOGP(info,
"Decoded IRs will be augmented by {} BCs, discarded if become prior to 1st orbit", bcshift);
382 LOGP(alarm,
"Decoding with {} BCs shift is requested, but the {} does not support this operation, ignoring request", bcshift,
mDet.
getName());
389template <
typename IT>
392 using source_type =
typename std::iterator_traits<IT>::value_type;
393 const size_t nSamples = std::distance(samplesBegin, samplesEnd);
394 return estimateBufferSize<source_type>(slot, nSamples);
397template <
typename source_T>
401 std::any& coder =
mCoders[slot];
402 if (coder.has_value()) {
403 const size_t alphabetRangeBits = [
this, &coder]() {
405 const auto& encoder = std::any_cast<const rans::compat::encoder_type<source_T>&>(coder);
409 const auto& encoder = std::any_cast<const rans::denseEncoder_type<source_T>&>(coder);
413 throw std::runtime_error(
"unsupported ANS version");
Class to check if give InteractionRecord or IRFrame is selected by the external IRFrame vector.
Definition of the Names Generator class.
Class for time synchronization of RawReader instances.
static constexpr std::string_view CCDBOBJECT
static constexpr std::string_view CTFDICT
static const TriggerOffsetsParam & Instance()
size_t getIRFrameSelMarginBwd() const
ctf::ANSHeader & getANSVersion()
void createCoder(OpType op, const o2::rans::RenormedDenseHistogram< S > &renormedHistogram, int slot)
static bool readFromTree(TTree &tree, const std::string brname, T &dest, int ev=0)
void setTrigOffsBinding(const std::string &s)
void setSupportBCShifts(bool v=true)
void updateTimeDependentParams(o2::framework::ProcessingContext &pc, bool askTree=false)
long getIRFrameSelShift() const
size_t mIRFrameSelMarginFwd
void setDictBinding(const std::string &s)
void init(o2::framework::InitContext &ic)
const DetID getDet() const
CTFCoderBase(OpType op, int n, DetID det, float memFactor=1.f, const std::string &ctfdictOpt="none")
void setMemMarginFactor(float v)
void setANSVersion(const ctf::ANSHeader &ansVersion) noexcept
const ctf::ANSHeader & getANSVersion() const noexcept
std::vector< char > readDictionaryFromFile(const std::string &dictPath, bool mayFail=false)
void setSelectedIRFrames(const SPAN &sp)
bool canApplyBCShift(const o2::InteractionRecord &ir) const
bool getSupportBCShifts() const
void setBCShift(int64_t n)
const std::string & getDictBinding() const
float getMemMarginFactor() const
std::vector< std::any > mCoders
o2::utils::IRFrameSelector & getIRFramesSelector()
std::string mTrigOffsBinding
const CTFDictHeader & getExtDictHeader() const
std::vector< char > loadDictionaryFromTree(TTree *tree)
bool isTreeDictionary(const void *buff) const
bool finaliseCCDB(o2::framework::ConcreteDataMatcher &matcher, void *obj)
CTFCoderBase(int n, DetID det, float memFactor=1.f, const std::string &ctfdictOpt="none")
void checkDictVersion(const CTFDictHeader &h) const
const std::string & getTrigOffsBinding() const
ctf::ANSHeader mANSVersion
size_t getIRFrameSelMarginFwd() const
std::string getPrefix() const
size_t mIRFrameSelMarginBwd
virtual void createCoders(const std::vector< char > &bufVec, o2::ctf::CTFCoderBase::OpType op)=0
auto getFirstTFOrbit() const
size_t finaliseCTFOutput(BUF &buffer)
size_t estimateBufferSize(size_t slot, source_IT samplesBegin, source_IT samplesEnd)
void createCodersFromFile(const std::string &dictPath, o2::ctf::CTFCoderBase::OpType op, bool mayFail=false)
o2::utils::IRFrameSelector mIRFrameSelector
void setFirstTFOrbit(uint32_t n)
virtual ~CTFCoderBase()=default
virtual void assignDictVersion(CTFDictHeader &h) const
size_t estimateBufferSize(size_t slot, const std::vector< source_T > &samples)
bool canApplyBCShift(const o2::InteractionRecord &ir, long shift) const
Static class with identifiers, bitmasks and names for ALICE detectors.
static constexpr const char * getName(ID id)
names of defined detectors
static constexpr o2h::DataOrigin getDataOrigin(ID id)
bool hasOption(const char *key) const
bool isSet(const char *key) const
T get(const char *key) const
ConfigParamRegistry const & options()
static constexpr decltype(auto) fromRenormed(const RenormedHistogramConcept< container_T > &renormed)
static constexpr decltype(auto) fromRenormed(const RenormedHistogramConcept< container_T > &renormed)
static constexpr decltype(auto) fromRenormed(const RenormedHistogramConcept< container_T > &renormed)
static constexpr decltype(auto) fromRenormed(const RenormedDenseHistogram< source_T > &renormed)
void setSelectedIRFrames(const SPAN &sp, size_t bwd=0, size_t fwd=0, long shift=0, bool removeOverlaps=true)
functionality to maintain compatibility with previous version of this library
bool match(const std::vector< std::string > &queries, const char *pattern)
static factory classes for building histograms, encoders and decoders.
public interface for building and renorming histograms from source data.
ANSHeader ansVersionFromString(const std::string &ansVersionString)
constexpr ANSHeader ANSVersionCompat
constexpr ANSHeader ANSVersion1
constexpr ANSHeader ANSVersionUnspecified
size_t calculateMaxBufferSizeB(size_t nElements, size_t rangeBits)
constexpr uint32_t getRangeBits(T min, T max) noexcept
auto makeHistogramView(container_T &container, std::ptrdiff_t offset) noexcept -> HistogramView< decltype(std::begin(container))>
HistogramView< Hist_IT > trim(const HistogramView< Hist_IT > &buffer)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
int64_t differenceInBC(const InteractionRecord &other) const
static std::string concat_string(Ts const &... ts)
o2::InteractionRecord ir(0, 0)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))