Skip to content

Commit 17d075e

Browse files
committed
CTF dictionaries are ready to be fetched from CCDB
1 parent e988b0c commit 17d075e

85 files changed

Lines changed: 287 additions & 556 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DataFormats/Detectors/Common/include/DetectorsCommonDataFormats/CTFDictHeader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <Rtypes.h>
2020
#include <string>
21+
#include "DetectorsCommonDataFormats/DetID.h"
2122

2223
namespace o2
2324
{
@@ -26,6 +27,7 @@ namespace ctf
2627

2728
/// Detector header base
2829
struct CTFDictHeader {
30+
o2::detectors::DetID det{};
2931
uint32_t dictTimeStamp = 0; // dictionary creation time (seconds since epoch) / hash
3032
uint8_t majorVersion = 1;
3133
uint8_t minorVersion = 0;
@@ -41,7 +43,7 @@ struct CTFDictHeader {
4143
}
4244
std::string asString() const;
4345

44-
ClassDefNV(CTFDictHeader, 1);
46+
ClassDefNV(CTFDictHeader, 2);
4547
};
4648

4749
} // namespace ctf

DataFormats/Detectors/Common/include/DetectorsCommonDataFormats/EncodedBlocks.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ class EncodedBlocks
358358
return mBlocks[i];
359359
}
360360

361+
auto getFrequencyTable(int i) const
362+
{
363+
o2::rans::FrequencyTable ft;
364+
const auto& bl = getBlock(i);
365+
const auto& md = getMetadata(i);
366+
ft.addFrequencies(bl.getDict(), bl.getDict() + bl.getNDict(), md.min, md.max);
367+
return ft;
368+
}
369+
361370
void setANSHeader(const ANSHeader& h) { mANSHeader = h; }
362371
const ANSHeader& getANSHeader() const { return mANSHeader; }
363372
ANSHeader& getANSHeader() { return mANSHeader; }

DataFormats/Detectors/Common/src/CTFDictHeader.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ std::string CTFDictHeader::asString() const
2525
std::time_t temp = dictTimeStamp;
2626
std::tm* t = std::gmtime(&temp);
2727
std::stringstream ss;
28-
ss << "Dict. v" << int(majorVersion) << '.' << int(minorVersion) << " from " << std::put_time(t, "%Y-%m-%d %I:%M:%S %p");
28+
ss << "CTF Dict for " << det.getName() << ", v" << int(majorVersion) << '.' << int(minorVersion) << " from " << std::put_time(t, "%Y-%m-%d %I:%M:%S %p");
2929
return ss.str();
3030
}

DataFormats/Detectors/ZDC/include/DataFormatsZDC/CTF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CTFHeader : public o2::ctf::CTFDictHeader {
3737
uint32_t firstOrbitEOData = 0; /// orbit of 1st end-of-orbit data
3838
uint16_t firstBC = 0; /// bc of 1st trigger
3939
std::array<uint16_t, NChannels> firstScaler{}; // inital scaler values
40-
ClassDefNV(CTFHeader, 2);
40+
ClassDefNV(CTFHeader, 3);
4141
};
4242

4343
/// wrapper for the Entropy-encoded triggers and cells of the TF

Detectors/Base/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ o2_target_root_dictionary(DetectorsBase
4545
include/DetectorsBase/MatCell.h
4646
include/DetectorsBase/MatLayerCyl.h
4747
include/DetectorsBase/MatLayerCylSet.h
48-
include/DetectorsBase/CTFCoderBase.h
4948
include/DetectorsBase/Aligner.h)
5049

5150
if(BUILD_SIMULATION)

Detectors/Base/include/DetectorsBase/CTFCoderBase.h

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "DetectorsCommonDataFormats/DetID.h"
2323
#include "DetectorsCommonDataFormats/NameConf.h"
2424
#include "DetectorsCommonDataFormats/CTFDictHeader.h"
25+
#include "DetectorsCommonDataFormats/CTFHeader.h"
2526
#include "rANS/rans.h"
2627

2728
namespace o2
@@ -43,24 +44,15 @@ class CTFCoderBase
4344

4445
CTFCoderBase() = delete;
4546
CTFCoderBase(int n, DetID det, float memFactor = 1.f) : mCoders(n), mDet(det), mMemMarginFactor(memFactor > 1.f ? memFactor : 1.f) {}
47+
virtual ~CTFCoderBase() = default;
4648

47-
std::unique_ptr<TFile> loadDictionaryTreeFile(const std::string& dictPath, bool mayFail = false);
49+
virtual void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) = 0;
4850

4951
template <typename CTF>
50-
std::vector<char> readDictionaryFromFile(const std::string& dictPath, bool mayFail = false)
51-
{
52-
std::vector<char> bufVec;
53-
auto fileDict = loadDictionaryTreeFile(dictPath, mayFail);
54-
if (fileDict) {
55-
std::unique_ptr<TTree> tree((TTree*)fileDict->Get(std::string(o2::base::NameConf::CTFDICT).c_str()));
56-
CTF::readFromTree(bufVec, *tree.get(), mDet.getName());
57-
if (bufVec.size()) {
58-
mExtHeader = static_cast<CTFDictHeader&>(CTF::get(bufVec.data())->getHeader());
59-
LOGP(info, "Found {} {} in {}", mDet.getName(), mExtHeader.asString(), dictPath);
60-
}
61-
}
62-
return bufVec;
63-
}
52+
std::vector<char> readDictionaryFromFile(const std::string& dictPath, bool mayFail = false);
53+
54+
template <typename CTF>
55+
void createCodersFromFile(const std::string& dictPath, o2::ctf::CTFCoderBase::OpType op);
6456

6557
template <typename S>
6658
void createCoder(OpType op, const o2::rans::FrequencyTable& freq, uint8_t probabilityBits, int slot)
@@ -92,6 +84,9 @@ class CTFCoderBase
9284
void setVerbosity(int v) { mVerbosity = v; }
9385
int getVerbosity() const { return mVerbosity; }
9486

87+
template <typename T>
88+
static bool readFromTree(TTree& tree, const std::string brname, T& dest, int ev = 0);
89+
9590
protected:
9691
std::string getPrefix() const { return o2::utils::Str::concat_string(mDet.getName(), "_CTF: "); }
9792
void assignDictVersion(CTFDictHeader& h) const
@@ -100,17 +95,98 @@ class CTFCoderBase
10095
h = mExtHeader;
10196
}
10297
}
98+
10399
void checkDictVersion(const CTFDictHeader& h) const;
104100

105101
std::vector<std::shared_ptr<void>> mCoders; // encoders/decoders
106102
DetID mDet;
107103
CTFDictHeader mExtHeader; // external dictionary header
108104
float mMemMarginFactor = 1.0f; // factor for memory allocation in EncodedBlocks
109105
int mVerbosity = 0;
110-
111-
ClassDefNV(CTFCoderBase, 1);
112106
};
113107

108+
///________________________________
109+
template <typename T>
110+
bool CTFCoderBase::readFromTree(TTree& tree, const std::string brname, T& dest, int ev)
111+
{
112+
auto* br = tree.GetBranch(brname.c_str());
113+
if (br && br->GetEntries() > ev) {
114+
auto* ptr = &dest;
115+
br->SetAddress(&ptr);
116+
br->GetEntry(ev);
117+
br->ResetAddress();
118+
return true;
119+
}
120+
return false;
121+
}
122+
123+
///________________________________
124+
template <typename CTF>
125+
void CTFCoderBase::createCodersFromFile(const std::string& dictPath, o2::ctf::CTFCoderBase::OpType op)
126+
{
127+
bool mayFail = true;
128+
auto buff = readDictionaryFromFile<CTF>(dictPath, mayFail);
129+
if (!buff.size()) {
130+
if (mayFail) {
131+
return;
132+
}
133+
throw std::runtime_error("Failed to create CTF dictionaty");
134+
}
135+
createCoders(buff, op);
136+
}
137+
138+
///________________________________
139+
template <typename CTF>
140+
std::vector<char> CTFCoderBase::readDictionaryFromFile(const std::string& dictPath, bool mayFail)
141+
{
142+
std::vector<char> bufVec;
143+
std::unique_ptr<TFile> fileDict(TFile::Open(dictPath.c_str()));
144+
if (!fileDict || fileDict->IsZombie()) {
145+
std::string errstr = fmt::format("CTF dictionary file {} for detector {} is absent", dictPath, mDet.getName());
146+
if (mayFail) {
147+
LOGP(info, "{}, will assume dictionary stored in CTF", errstr);
148+
} else {
149+
throw std::runtime_error(errstr);
150+
}
151+
return bufVec;
152+
}
153+
std::unique_ptr<TTree> tree((TTree*)fileDict->Get(std::string(o2::base::NameConf::CTFDICT).c_str()));
154+
std::unique_ptr<std::vector<char>> bv((std::vector<char>*)fileDict->GetObjectChecked(o2::base::NameConf::CCDBOBJECT.data(), "std::vector<char>"));
155+
if (tree) {
156+
CTFHeader ctfHeader;
157+
if (!readFromTree(*tree.get(), "CTFHeader", ctfHeader) || !ctfHeader.detectors[mDet]) {
158+
std::string errstr = fmt::format("CTF dictionary file for detector {} is absent in the tree from file {}", mDet.getName(), dictPath);
159+
if (mayFail) {
160+
LOGP(info, "{}, will assume dictionary stored in CTF", errstr);
161+
} else {
162+
throw std::runtime_error(errstr);
163+
}
164+
return bufVec;
165+
}
166+
CTF::readFromTree(bufVec, *tree.get(), mDet.getName());
167+
} else if (bv) {
168+
bufVec.swap(*bv);
169+
if (bufVec.size()) {
170+
auto dictHeader = static_cast<o2::ctf::CTFDictHeader&>(CTF::get(bufVec.data())->getHeader());
171+
if (dictHeader.det != mDet) {
172+
throw std::runtime_error(fmt::format("{} contains dictionary vector for {}, expected {}", dictPath, dictHeader.det.getName(), mDet.getName()));
173+
}
174+
}
175+
}
176+
if (bufVec.size()) {
177+
mExtHeader = static_cast<CTFDictHeader&>(CTF::get(bufVec.data())->getHeader());
178+
LOGP(info, "Found {} in {}", mExtHeader.asString(), dictPath);
179+
} else {
180+
std::string errstr = fmt::format("CTF dictionary file for detector {} is empty", mDet.getName());
181+
if (mayFail) {
182+
LOGP(info, "{}, will assume dictionary stored in CTF", errstr);
183+
} else {
184+
throw std::runtime_error(errstr);
185+
}
186+
}
187+
return bufVec;
188+
}
189+
114190
} // namespace ctf
115191
} // namespace o2
116192

Detectors/Base/src/CTFCoderBase.cxx

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,58 +13,10 @@
1313
/// \brief Defintions for CTFCoderBase class (support of external dictionaries)
1414
/// \author ruben.shahoyan@cern.ch
1515

16-
#include "DetectorsCommonDataFormats/CTFHeader.h"
1716
#include "DetectorsBase/CTFCoderBase.h"
18-
#include <filesystem>
1917

2018
using namespace o2::ctf;
2119

22-
template <typename T>
23-
bool readFromTree(TTree& tree, const std::string brname, T& dest, int ev = 0)
24-
{
25-
auto* br = tree.GetBranch(brname.c_str());
26-
if (br && br->GetEntries() > ev) {
27-
auto* ptr = &dest;
28-
br->SetAddress(&ptr);
29-
br->GetEntry(ev);
30-
br->ResetAddress();
31-
return true;
32-
}
33-
return false;
34-
}
35-
36-
std::unique_ptr<TFile> CTFCoderBase::loadDictionaryTreeFile(const std::string& dictPath, bool mayFail)
37-
{
38-
TDirectory* curd = gDirectory;
39-
std::unique_ptr<TFile> fileDict(!std::filesystem::exists(dictPath) ? nullptr : TFile::Open(dictPath.c_str()));
40-
if (!fileDict || fileDict->IsZombie()) {
41-
if (mayFail) {
42-
LOG(info) << "CTF dictionary file " << dictPath << " for detector " << mDet.getName() << " is absent, will use dictionaries stored in CTF";
43-
fileDict.reset();
44-
return std::move(fileDict);
45-
}
46-
LOG(error) << "Failed to open CTF dictionary file " << dictPath << " for detector " << mDet.getName();
47-
throw std::runtime_error("Failed to open dictionary file");
48-
}
49-
auto tnm = std::string(o2::base::NameConf::CTFDICT);
50-
std::unique_ptr<TTree> tree((TTree*)fileDict->Get(tnm.c_str()));
51-
if (!tree) {
52-
fileDict.reset();
53-
LOG(error) << "Did not find CTF dictionary tree " << tnm << " in " << dictPath;
54-
throw std::runtime_error("Did not fine CTF dictionary tree in the file");
55-
}
56-
CTFHeader ctfHeader;
57-
if (!readFromTree(*tree.get(), "CTFHeader", ctfHeader) || !ctfHeader.detectors[mDet]) {
58-
tree.reset();
59-
fileDict.reset();
60-
LOG(error) << "Did not find CTF dictionary header or Detector " << mDet.getName() << " in it";
61-
if (!mayFail) {
62-
throw std::runtime_error("did not find CTFHeader with needed detector");
63-
}
64-
}
65-
return fileDict;
66-
}
67-
6820
void CTFCoderBase::checkDictVersion(const CTFDictHeader& h) const
6921
{
7022
if (h.isValidDictTimeStamp()) { // external dictionary was used

Detectors/Base/src/DetectorsBaseLinkDef.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
#pragma link C++ class o2::base::MatBudget + ;
3131
#pragma link C++ class o2::base::MatLayerCyl + ;
3232
#pragma link C++ class o2::base::MatLayerCylSet + ;
33-
34-
#pragma link C++ class o2::ctf::CTFCoderBase + ;
35-
3633
#pragma link C++ class o2::base::Aligner + ;
3734
#pragma link C++ class o2::conf::ConfigurableParamHelper < o2::base::Aligner> + ;
3835

Detectors/CPV/reconstruction/include/CPVReconstruction/CTFCoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CTFCoder : public o2::ctf::CTFCoderBase
3737
{
3838
public:
3939
CTFCoder() : o2::ctf::CTFCoderBase(CTF::getNBlocks(), o2::detectors::DetID::CPV) {}
40-
~CTFCoder() = default;
40+
~CTFCoder() final = default;
4141

4242
/// entropy-encode data to buffer with CTF
4343
template <typename VEC>
@@ -47,7 +47,7 @@ class CTFCoder : public o2::ctf::CTFCoderBase
4747
template <typename VTRG, typename VCLUSTER>
4848
void decode(const CTF::base& ec, VTRG& trigVec, VCLUSTER& cluVec);
4949

50-
void createCoders(const std::string& dictPath, o2::ctf::CTFCoderBase::OpType op);
50+
void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) final;
5151

5252
private:
5353
void appendToTree(TTree& tree, CTF& ec);

Detectors/CPV/reconstruction/include/CPVReconstruction/CTFHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CTFHelper
3333

3434
CTFHeader createHeader()
3535
{
36-
CTFHeader h{0, 1, 0, // dummy timestamp, version 1.0
36+
CTFHeader h{o2::detectors::DetID::CPV, 0, 1, 0, // dummy timestamp, version 1.0
3737
uint32_t(mTrigData.size()), uint32_t(mCluData.size()), 0, 0};
3838
if (mTrigData.size()) {
3939
h.firstOrbit = mTrigData[0].getBCData().orbit;

0 commit comments

Comments
 (0)