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>
213 auto* br =
tree.GetBranch(brname.c_str());
214 if (br && br->GetEntries() > ev) {
216 br->SetAddress(&
ptr);
225template <
typename CTF>
228 auto buff = readDictionaryFromFile<CTF>(dictPath, mayFail);
233 throw std::runtime_error(
"Failed to create CTF dictionaty");
239template <
typename CTF>
242 std::vector<char> bufVec;
251template <
typename CTF>
254 std::vector<char> bufVec;
255 std::unique_ptr<TFile> fileDict;
256 if (std::filesystem::exists(dictPath)) {
257 fileDict.reset(TFile::Open(dictPath.c_str()));
259 if (!fileDict || fileDict->IsZombie()) {
260 std::string errstr = fmt::format(
"CTF dictionary file {} for detector {} is absent", dictPath,
mDet.
getName());
262 LOGP(info,
"{}, will assume dictionary stored in CTF", errstr);
264 throw std::runtime_error(errstr);
268 std::unique_ptr<TTree>
tree;
269 std::unique_ptr<std::vector<char>> bv((std::vector<char>*)fileDict->GetObjectChecked(
o2::base::NameConf::CCDBOBJECT.data(),
"std::vector<char>"));
275 auto v = loadDictionaryFromTree<CTF>(
tree.get());
281 if (dictHeader.det !=
mDet) {
283 LOGP(error,
"{} contains dictionary vector for {}, expected {}", dictPath, dictHeader.det.getName(),
mDet.
getName());
292 LOGP(info,
"{}, will assume dictionary stored in CTF",
mDet.
getName());
294 LOGP(fatal,
"CTF dictionary file for detector {} is empty",
mDet.
getName());
301template <
typename CTF>
318 const std::string ansVersionString = ic.
options().
get<std::string>(
"ans-version");
319 if (!ansVersionString.empty()) {
321 LOGP(info,
"parsing ansVersionString {} into {}", ansVersionString,
static_cast<std::string
>(
mANSVersion));
323 throw std::invalid_argument(fmt::format(
"Invalid ANS Version {}", ansVersionString));
328 auto dict = ic.
options().
get<std::string>(
"ctf-dict");
329 if (dict.empty() || dict ==
"ccdb") {
332 if (dict !=
"none") {
333 createCodersFromFile<CTF>(dict,
mOpType);
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)
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)
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
CTFCoderBase(int n, DetID det, float memFactor=1.f)
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()))