forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCTFCoder.h
More file actions
191 lines (168 loc) · 7.48 KB
/
CTFCoder.h
File metadata and controls
191 lines (168 loc) · 7.48 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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 CTFCoder.h
/// \author ruben.shahoyan@cern.ch
/// \brief class for entropy encoding/decoding of CPV data
#ifndef O2_CPV_CTFCODER_H
#define O2_CPV_CTFCODER_H
#include <algorithm>
#include <iterator>
#include <string>
#include <array>
#include "DataFormatsCPV/CTF.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsBase/CTFCoderBase.h"
#include "CPVReconstruction/CTFHelper.h"
class TTree;
namespace o2
{
namespace cpv
{
class CTFCoder final : public o2::ctf::CTFCoderBase
{
public:
CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::CPV) {}
~CTFCoder() final = default;
/// entropy-encode data to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize encode(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cluster>& cluData);
/// entropy decode data from buffer with CTF
template <typename VTRG, typename VCLUSTER>
o2::ctf::CTFIOSize decode(const CTF::base& ec, VTRG& trigVec, VCLUSTER& cluVec);
void createCoders(const std::vector<char>& bufVec, ctf::CTFCoderBase::OpType op) final;
private:
template <typename VEC>
o2::ctf::CTFIOSize encode_impl(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cluster>& cluData);
void appendToTree(TTree& tree, CTF& ec);
void readFromTree(TTree& tree, int entry, std::vector<TriggerRecord>& trigVec, std::vector<Cluster>& cluVec);
std::vector<TriggerRecord> mTrgDataFilt;
std::vector<Cluster> mClusDataFilt;
};
/// entropy-encode clusters to buffer with CTF
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cluster>& cluData)
{
if (mIRFrameSelector.isSet()) { // preselect data
mTrgDataFilt.clear();
mClusDataFilt.clear();
for (const auto& trig : trigData) {
if (mIRFrameSelector.check(trig.getBCData()) >= 0) {
mTrgDataFilt.push_back(trig);
auto clusIt = cluData.begin() + trig.getFirstEntry();
auto& trigC = mTrgDataFilt.back();
trigC.setDataRange((int)mClusDataFilt.size(), trig.getNumberOfObjects());
std::copy(clusIt, clusIt + trig.getNumberOfObjects(), std::back_inserter(mClusDataFilt));
}
}
return encode_impl(buff, mTrgDataFilt, mClusDataFilt);
}
return encode_impl(buff, trigData, cluData);
}
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode_impl(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cluster>& cluData)
{
using MD = o2::ctf::Metadata::OptStore;
// what to do which each field: see o2::ctd::Metadata explanation
constexpr MD optField[CTF::getNBlocks()] = {
MD::EENCODE_OR_PACK, // BLC_bcIncTrig
MD::EENCODE_OR_PACK, // BLC_orbitIncTrig
MD::EENCODE_OR_PACK, // BLC_entriesTrig
MD::EENCODE_OR_PACK, // BLC_posX
MD::EENCODE_OR_PACK, // BLC_posZ
MD::EENCODE_OR_PACK, // BLC_energy
MD::EENCODE_OR_PACK // BLC_status
};
CTFHelper helper(trigData, cluData);
// book output size with some margin
auto szIni = sizeof(CTFHeader) + helper.getSize() * 2. / 3; // will be autoexpanded if needed
buff.resize(szIni);
auto ec = CTF::create(buff);
using ECB = CTF::base;
ec->setHeader(helper.createHeader());
assignDictVersion(static_cast<o2::ctf::CTFDictHeader&>(ec->getHeader()));
ec->setANSHeader(mANSVersion);
// at every encoding the buffer might be autoexpanded, so we don't work with fixed pointer ec
o2::ctf::CTFIOSize iosize;
#define ENCODECPV(beg, end, slot, bits) CTF::get(buff.data())->encode(beg, end, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)], getMemMarginFactor());
// clang-format off
iosize += ENCODECPV(helper.begin_bcIncTrig(), helper.end_bcIncTrig(), CTF::BLC_bcIncTrig, 0);
iosize += ENCODECPV(helper.begin_orbitIncTrig(), helper.end_orbitIncTrig(), CTF::BLC_orbitIncTrig, 0);
iosize += ENCODECPV(helper.begin_entriesTrig(), helper.end_entriesTrig(), CTF::BLC_entriesTrig, 0);
iosize += ENCODECPV(helper.begin_posX(), helper.end_posX(), CTF::BLC_posX, 0);
iosize += ENCODECPV(helper.begin_posZ(), helper.end_posZ(), CTF::BLC_posZ, 0);
iosize += ENCODECPV(helper.begin_energy(), helper.end_energy(), CTF::BLC_energy, 0);
iosize += ENCODECPV(helper.begin_status(), helper.end_status(), CTF::BLC_status, 0);
// clang-format on
CTF::get(buff.data())->print(getPrefix(), mVerbosity);
finaliseCTFOutput<CTF>(buff);
iosize.rawIn = trigData.size() * sizeof(TriggerRecord) + cluData.size() * sizeof(Cluster);
return iosize;
}
/// decode entropy-encoded clusters to standard compact clusters
template <typename VTRG, typename VCLUSTER>
o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VTRG& trigVec, VCLUSTER& cluVec)
{
auto header = ec.getHeader();
checkDictVersion(static_cast<const o2::ctf::CTFDictHeader&>(header));
ec.print(getPrefix(), mVerbosity);
std::vector<int16_t> bcInc;
std::vector<int32_t> orbitInc;
std::vector<uint16_t> entries, posX, posZ;
std::vector<uint8_t> energy, status;
o2::ctf::CTFIOSize iosize;
#define DECODECPV(part, slot) ec.decode(part, int(slot), mCoders[int(slot)])
// clang-format off
iosize += DECODECPV(bcInc, CTF::BLC_bcIncTrig);
iosize += DECODECPV(orbitInc, CTF::BLC_orbitIncTrig);
iosize += DECODECPV(entries, CTF::BLC_entriesTrig);
iosize += DECODECPV(posX, CTF::BLC_posX);
iosize += DECODECPV(posZ, CTF::BLC_posZ);
iosize += DECODECPV(energy, CTF::BLC_energy);
iosize += DECODECPV(status, CTF::BLC_status);
// clang-format on
//
trigVec.clear();
cluVec.clear();
trigVec.reserve(header.nTriggers);
status.reserve(header.nClusters);
uint32_t firstEntry = 0, cluCount = 0;
o2::InteractionRecord ir(header.firstBC, header.firstOrbit);
bool checkIROK = (mBCShift == 0); // need to check if CTP offset correction does not make the local time negative ?
Cluster clu;
for (uint32_t itrig = 0; itrig < header.nTriggers; itrig++) {
// restore TrigRecord
if (orbitInc[itrig]) { // non-0 increment => new orbit
ir.bc = bcInc[itrig]; // bcInc has absolute meaning
ir.orbit += orbitInc[itrig];
} else {
ir.bc += bcInc[itrig];
}
if (checkIROK || canApplyBCShift(ir)) { // correction will be ok
checkIROK = true;
} else { // correction would make IR prior to mFirstTFOrbit, skip
cluCount += entries[itrig];
continue;
}
firstEntry = cluVec.size();
for (uint16_t ic = 0; ic < entries[itrig]; ic++) {
clu.setPacked(posX[cluCount], posZ[cluCount], energy[cluCount], status[cluCount]);
cluVec.emplace_back(clu);
cluCount++;
}
trigVec.emplace_back(ir - mBCShift, firstEntry, entries[itrig]);
}
assert(cluCount == header.nClusters);
iosize.rawIn = trigVec.size() * sizeof(TriggerRecord) + cluVec.size() * sizeof(Cluster);
return iosize;
}
} // namespace cpv
} // namespace o2
#endif // O2_CPV_CTFCODER_H