forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCTFCoder.h
More file actions
258 lines (229 loc) · 10.1 KB
/
CTFCoder.h
File metadata and controls
258 lines (229 loc) · 10.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 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 EMCAL data
#ifndef O2_EMCAL_CTFCODER_H
#define O2_EMCAL_CTFCODER_H
#include <algorithm>
#include <iterator>
#include <string>
#include <array>
#include "DataFormatsEMCAL/CTF.h"
#include "DetectorsCommonDataFormats/DetID.h"
#include "DetectorsBase/CTFCoderBase.h"
#include "rANS/rans.h"
#include "EMCALReconstruction/CTFHelper.h"
class TTree;
namespace o2
{
namespace emcal
{
class CTFCoder : public o2::ctf::CTFCoderBase
{
public:
CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::EMC) {}
~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 CellCompressed>& cellData);
// repack energy old format -> new format
template <typename T, typename U>
T repackE(T packedE, U packedStatus);
/// entropy decode data from buffer with CTF
template <typename VTRG, typename VCELL>
o2::ctf::CTFIOSize decode(const CTF::base& ec, VTRG& trigVec, VCELL& cellVec);
void createCoders(const std::vector<char>& bufVec, o2::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 CellCompressed>& cellDComprCellCompressedata);
void appendToTree(TTree& tree, CTF& ec);
void readFromTree(TTree& tree, int entry, std::vector<TriggerRecord>& trigVec, std::vector<Cell>& cellVec);
std::vector<TriggerRecord> mTrgDataFilt;
std::vector<CellCompressed> mCellDataFilt;
};
/// 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 CellCompressed>& cellData)
{
if (mIRFrameSelector.isSet()) { // preselect data
mTrgDataFilt.clear();
mCellDataFilt.clear();
for (const auto& trig : trigData) {
if (mIRFrameSelector.check(trig.getBCData()) >= 0) {
mTrgDataFilt.push_back(trig);
auto cellIt = cellData.begin() + trig.getFirstEntry();
auto& trigC = mTrgDataFilt.back();
trigC.setDataRange((int)mCellDataFilt.size(), trig.getNumberOfObjects());
std::copy(cellIt, cellIt + trig.getNumberOfObjects(), std::back_inserter(mCellDataFilt));
}
}
return encode_impl(buff, mTrgDataFilt, mCellDataFilt);
}
return encode_impl(buff, trigData, cellData);
}
template <typename VEC>
o2::ctf::CTFIOSize CTFCoder::encode_impl(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const CellCompressed>& cellData)
{
using MD = o2::ctf::Metadata::OptStore;
// what to do which each field: see o2::ctd::Metadata explanation
constexpr MD optField[CTF::getNBlocks()] = {
MD::EENCODE, // BLC_bcIncTrig
MD::EENCODE, // BLC_orbitIncTrig
MD::EENCODE, // BLC_entriesTrig
MD::EENCODE, // BLC_towerID
MD::EENCODE, // BLC_time
MD::EENCODE, // BLC_energy
MD::EENCODE, // BLC_status
// extra slot was added in the end
MD::EENCODE, // BLC_trigger
MD::EENCODE // BLC_chi2
};
CTFHelper helper(trigData, cellData);
// 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->getANSHeader().majorVersion = 0;
ec->getANSHeader().minorVersion = 2;
// at every encoding the buffer might be autoexpanded, so we don't work with fixed pointer ec
o2::ctf::CTFIOSize iosize;
#define ENCODEEMC(beg, end, slot, bits) CTF::get(buff.data())->encode(beg, end, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)].get(), getMemMarginFactor());
// clang-format off
iosize += ENCODEEMC(helper.begin_bcIncTrig(), helper.end_bcIncTrig(), CTF::BLC_bcIncTrig, 0);
iosize += ENCODEEMC(helper.begin_orbitIncTrig(), helper.end_orbitIncTrig(), CTF::BLC_orbitIncTrig, 0);
iosize += ENCODEEMC(helper.begin_entriesTrig(), helper.end_entriesTrig(), CTF::BLC_entriesTrig, 0);
iosize += ENCODEEMC(helper.begin_towerID(), helper.end_towerID(), CTF::BLC_towerID, 0);
iosize += ENCODEEMC(helper.begin_time(), helper.end_time(), CTF::BLC_time, 0);
iosize += ENCODEEMC(helper.begin_energy(), helper.end_energy(), CTF::BLC_energy, 0);
iosize += ENCODEEMC(helper.begin_status(), helper.end_status(), CTF::BLC_status, 0);
// extra slot was added in the end
iosize += ENCODEEMC(helper.begin_trigger(), helper.end_trigger(), CTF::BLC_trigger, 0);
iosize += ENCODEEMC(helper.begin_chi2(), helper.end_chi2(), CTF::BLC_chi2, 0);
// clang-format on
CTF::get(buff.data())->print(getPrefix(), mVerbosity);
finaliseCTFOutput<CTF>(buff);
iosize.rawIn = sizeof(TriggerRecord) * trigData.size() + sizeof(CellCompressed) * cellData.size();
return iosize;
}
// repack cell energy (needed for old version of CTF (subversion 1)
template <typename T, typename U>
T CTFCoder::repackE(T packedE, U packedStatus)
{
T repackedE = packedE;
double energyTmp = packedE * CellCompressed::ENERGY_RESOLUTION_OLD;
double truncatedEnergyTmp = energyTmp;
if (truncatedEnergyTmp < 0.) {
truncatedEnergyTmp = 0.;
} else if (truncatedEnergyTmp > CellCompressed::ENERGY_TRUNCATION) {
truncatedEnergyTmp = CellCompressed::ENERGY_TRUNCATION;
}
switch (static_cast<o2::emcal::ChannelType_t>(packedStatus)) {
case o2::emcal::ChannelType_t::HIGH_GAIN: {
repackedE = static_cast<T>(std::round(truncatedEnergyTmp / CellCompressed::ENERGY_RESOLUTION_HG));
break;
}
case o2::emcal::ChannelType_t::LOW_GAIN: {
repackedE = static_cast<T>(std::round(truncatedEnergyTmp / CellCompressed::ENERGY_RESOLUTION_LG));
break;
}
case o2::emcal::ChannelType_t::TRU: {
repackedE = static_cast<T>(std::round(truncatedEnergyTmp / CellCompressed::ENERGY_RESOLUTION_TRU));
break;
}
case o2::emcal::ChannelType_t::LEDMON: {
repackedE = static_cast<T>(std::round(truncatedEnergyTmp / CellCompressed::ENERGY_RESOLUTION_LEDMON));
break;
}
}
return repackedE;
}
/// decode entropy-encoded clusters to standard compact clusters
template <typename VTRG, typename VCELL>
o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VTRG& trigVec, VCELL& cellVec)
{
const auto& header = ec.getHeader();
checkDictVersion(static_cast<const o2::ctf::CTFDictHeader&>(header));
ec.print(getPrefix(), mVerbosity);
std::vector<uint16_t> bcInc, entries, energy, cellTime, tower, trigger, chi2;
std::vector<uint32_t> orbitInc;
std::vector<uint8_t> status;
o2::ctf::CTFIOSize iosize;
#define DECODEEMCAL(part, slot) ec.decode(part, int(slot), mCoders[int(slot)].get())
// clang-format off
iosize += DECODEEMCAL(bcInc, CTF::BLC_bcIncTrig);
iosize += DECODEEMCAL(orbitInc, CTF::BLC_orbitIncTrig);
iosize += DECODEEMCAL(entries, CTF::BLC_entriesTrig);
iosize += DECODEEMCAL(tower, CTF::BLC_towerID);
iosize += DECODEEMCAL(cellTime, CTF::BLC_time);
iosize += DECODEEMCAL(energy, CTF::BLC_energy);
iosize += DECODEEMCAL(status, CTF::BLC_status);
// extra slot was added in the end
iosize += DECODEEMCAL(trigger, CTF::BLC_trigger);
iosize += DECODEEMCAL(chi2, CTF::BLC_chi2);
// triggers were added later, in old data they are absent:
if (trigger.empty()) {
trigger.resize(header.nTriggers);
}
//
// clang-format on
//
trigVec.clear();
cellVec.clear();
trigVec.reserve(header.nTriggers);
status.reserve(header.nCells);
uint32_t firstEntry = 0, cellCount = 0;
o2::InteractionRecord ir(header.firstBC, header.firstOrbit);
CellCompressed cellCompressed;
Cell cell;
TriggerRecord trg;
// perform a check on dict we have old CTF with different energy compression
Bool_t isVersion1 = kFALSE;
if (header.majorVersion == 0 && header.minorVersion == 1) {
isVersion1 = kTRUE;
}
uint16_t packedEnergy; // packed energy
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];
}
firstEntry = cellVec.size();
for (uint16_t ic = 0; ic < entries[itrig]; ic++) {
// if old verion of CTF, do conversion to energy and then repack before setting packed content
if (isVersion1) {
packedEnergy = repackE(energy[cellCount], status[cellCount]);
} else { // new CTFs don't require any repacking
packedEnergy = energy[cellCount];
}
cellCompressed.setPacked(tower[cellCount], cellTime[cellCount], packedEnergy, status[cellCount], chi2[cellCount]);
cell.setAll(cellCompressed.getTower(), cellCompressed.getEnergy(), cellCompressed.getTimeStamp(), cellCompressed.getType(), cellCompressed.getChi2());
cellVec.emplace_back(cell);
cellCount++;
}
trg.setBCData(ir);
trg.setDataRange(firstEntry, entries[itrig]);
trg.setTriggerBitsCompressed(trigger[itrig]);
trigVec.emplace_back(trg);
}
assert(cellCount == header.nCells);
iosize.rawIn = sizeof(TriggerRecord) * trigVec.size() + sizeof(Cell) * cellVec.size();
return iosize;
}
} // namespace emcal
} // namespace o2
#endif // O2_EMCAL_CTFCODER_H