forked from mcoquet642/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTFCoderBase.h
More file actions
109 lines (90 loc) · 3.28 KB
/
CTFCoderBase.h
File metadata and controls
109 lines (90 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file CTFCoderBase.h
/// \brief Declarations for CTFCoderBase class (support of external dictionaries)
/// \author ruben.shahoyan@cern.ch
#ifndef _ALICEO2_CTFCODER_BASE_H_
#define _ALICEO2_CTFCODER_BASE_H_
#include <memory>
#include <TFile.h>
#include <TTree.h>
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsCommonDataFormats/NameConf.h"
#include "DetectorsCommonDataFormats/CTFDictHeader.h"
#include "rANS/rans.h"
namespace o2
{
namespace ctf
{
/// this is a base class for particular detector CTF coder/decoder, provides common
/// interface to create external entropy encoders/decoders
using DetID = o2::detectors::DetID;
class CTFCoderBase
{
public:
enum class OpType : int { Encoder,
Decoder };
CTFCoderBase() = delete;
CTFCoderBase(int n, DetID det) : mCoders(n), mDet(det) {}
std::unique_ptr<TFile> loadDictionaryTreeFile(const std::string& dictPath, bool mayFail = false);
template <typename CTF>
std::vector<char> readDictionaryFromFile(const std::string& dictPath, bool mayFail = false)
{
std::vector<char> bufVec;
auto fileDict = loadDictionaryTreeFile(dictPath, mayFail);
if (fileDict) {
std::unique_ptr<TTree> tree((TTree*)fileDict->Get(std::string(o2::base::NameConf::CTFDICT).c_str()));
CTF::readFromTree(bufVec, *tree.get(), mDet.getName());
if (bufVec.size()) {
mExtHeader = static_cast<CTFDictHeader&>(CTF::get(bufVec.data())->getHeader());
LOGP(INFO, "Found {} {} in {}", mDet.getName(), mExtHeader.asString(), dictPath);
}
}
return bufVec;
}
template <typename S>
void createCoder(OpType op, const o2::rans::FrequencyTable& freq, uint8_t probabilityBits, int slot)
{
if (!freq.size()) {
LOG(WARNING) << "Empty dictionary provided for slot " << slot << ", " << (op == OpType::Encoder ? "encoding" : "decoding") << " will assume literal symbols only";
}
switch (op) {
case OpType::Encoder:
mCoders[slot].reset(new o2::rans::LiteralEncoder64<S>(freq, probabilityBits));
break;
case OpType::Decoder:
mCoders[slot].reset(new o2::rans::LiteralDecoder64<S>(freq, probabilityBits));
break;
}
}
void clear()
{
for (auto c : mCoders) {
c.reset();
}
}
protected:
std::string getPrefix() const { return o2::utils::Str::concat_string(mDet.getName(), "_CTF: "); }
void assignDictVersion(CTFDictHeader& h) const
{
if (mExtHeader.isValidDictTimeStamp()) {
h = mExtHeader;
}
}
void checkDictVersion(const CTFDictHeader& h) const;
std::vector<std::shared_ptr<void>> mCoders; // encoders/decoders
DetID mDet;
CTFDictHeader mExtHeader; // external dictionary header
ClassDefNV(CTFCoderBase, 1);
};
} // namespace ctf
} // namespace o2
#endif