Project
Loading...
Searching...
No Matches
CTFCoder.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef O2_MCH_CTFCODER_H
17#define O2_MCH_CTFCODER_H
18
19#include <algorithm>
20#include <iterator>
21#include <string>
22#include <array>
23#include "DataFormatsMCH/CTF.h"
28#include "MCHCTF/CTFHelper.h"
29
30class TTree;
31
32namespace o2
33{
34namespace mch
35{
36
38{
39 public:
40 CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::MCH) {}
41 ~CTFCoder() final = default;
42
44 template <typename VEC>
45 o2::ctf::CTFIOSize encode(VEC& buff, const gsl::span<const ROFRecord>& rofData, const gsl::span<const Digit>& digData);
46
48 template <typename VROF, typename VCOL>
49 o2::ctf::CTFIOSize decode(const CTF::base& ec, VROF& rofVec, VCOL& digVec);
50
51 void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) final;
52
53 private:
54 template <typename VEC>
55 o2::ctf::CTFIOSize encode_impl(VEC& buff, const gsl::span<const ROFRecord>& rofData, const gsl::span<const Digit>& digData);
56 void appendToTree(TTree& tree, CTF& ec);
57 void readFromTree(TTree& tree, int entry, std::vector<ROFRecord>& rofVec, std::vector<Digit>& digVec);
58
59 std::vector<ROFRecord> mROFRecFilt;
60 std::vector<Digit> mDigDataFilt;
61};
62
64template <typename VEC>
65o2::ctf::CTFIOSize CTFCoder::encode(VEC& buff, const gsl::span<const ROFRecord>& rofData, const gsl::span<const Digit>& digData)
66{
67 if (mIRFrameSelector.isSet()) { // preselect data
68 mROFRecFilt.clear();
69 mDigDataFilt.clear();
70 for (const auto& rof : rofData) {
71 if (mIRFrameSelector.check(rof.getBCData()) >= 0) {
72 mROFRecFilt.push_back(rof);
73 auto digIt = digData.begin() + rof.getFirstIdx();
74 auto& rofC = mROFRecFilt.back();
75 rofC.setDataRef((int)mDigDataFilt.size(), rof.getNEntries());
76 std::copy(digIt, digIt + rofC.getNEntries(), std::back_inserter(mDigDataFilt));
77 }
78 }
79 return encode_impl(buff, mROFRecFilt, mDigDataFilt);
80 }
81 return encode_impl(buff, rofData, digData);
82}
83
84template <typename VEC>
85o2::ctf::CTFIOSize CTFCoder::encode_impl(VEC& buff, const gsl::span<const ROFRecord>& rofData, const gsl::span<const Digit>& digData)
86{
88 // what to do which each field: see o2::ctd::Metadata explanation
89 constexpr MD optField[CTF::getNBlocks()] = {
90 MD::EENCODE_OR_PACK, // BLC_bcIncROF
91 MD::EENCODE_OR_PACK, // BLC_orbitIncROF
92 MD::EENCODE_OR_PACK, // BLC_nDigitsROF
93 MD::EENCODE_OR_PACK, // BLC_tfTime
94 MD::EENCODE_OR_PACK, // BLC_nSamples
95 MD::EENCODE_OR_PACK, // BLC_isSaturated
96 MD::EENCODE_OR_PACK, // BLC_detID
97 MD::EENCODE_OR_PACK, // BLC_padID
98 MD::EENCODE_OR_PACK // BLC_ADC
99 };
100 CTFHelper helper(rofData, digData);
101 // book output size with some margin
102 auto szIni = sizeof(CTFHeader) + helper.getSize() * 2. / 3; // will be autoexpanded if needed
103 buff.resize(szIni);
104
105 auto ec = CTF::create(buff);
106 using ECB = CTF::base;
107
108 ec->setHeader(helper.createHeader());
109 assignDictVersion(static_cast<o2::ctf::CTFDictHeader&>(ec->getHeader()));
110 ec->setANSHeader(mANSVersion);
111 // at every encoding the buffer might be autoexpanded, so we don't work with fixed pointer ec
112 o2::ctf::CTFIOSize iosize;
113#define ENCODEMCH(beg, end, slot, bits) CTF::get(buff.data())->encode(beg, end, int(slot), bits, optField[int(slot)], &buff, mCoders[int(slot)], getMemMarginFactor());
114 // clang-format off
115 iosize += ENCODEMCH(helper.begin_bcIncROF(), helper.end_bcIncROF(), CTF::BLC_bcIncROF, 0);
116 iosize += ENCODEMCH(helper.begin_orbitIncROF(), helper.end_orbitIncROF(), CTF::BLC_orbitIncROF, 0);
117 iosize += ENCODEMCH(helper.begin_nDigitsROF(), helper.end_nDigitsROF(), CTF::BLC_nDigitsROF, 0);
118
119 iosize += ENCODEMCH(helper.begin_tfTime(), helper.end_tfTime(), CTF::BLC_tfTime, 0);
120 iosize += ENCODEMCH(helper.begin_nSamples(), helper.end_nSamples(), CTF::BLC_nSamples, 0);
121 iosize += ENCODEMCH(helper.begin_isSaturated(), helper.end_isSaturated(), CTF::BLC_isSaturated, 0);
122 iosize += ENCODEMCH(helper.begin_detID(), helper.end_detID(), CTF::BLC_detID, 0);
123 iosize += ENCODEMCH(helper.begin_padID(), helper.end_padID(), CTF::BLC_padID, 0);
124 iosize += ENCODEMCH(helper.begin_ADC() , helper.end_ADC(), CTF::BLC_ADC, 0);
125 // clang-format on
126 CTF::get(buff.data())->print(getPrefix(), mVerbosity);
127 finaliseCTFOutput<CTF>(buff);
128 iosize.rawIn = sizeof(ROFRecord) * rofData.size() + sizeof(Digit) * digData.size();
129 return iosize;
130}
131
133template <typename VROF, typename VCOL>
134o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VROF& rofVec, VCOL& digVec)
135{
136 auto header = ec.getHeader();
137 checkDictVersion(static_cast<const o2::ctf::CTFDictHeader&>(header));
139
140 std::vector<uint16_t> nSamples;
141 std::vector<uint32_t> ADC, nDigits;
142 std::vector<int32_t> orbitInc;
143 std::vector<int32_t> tfTime;
144 std::vector<int16_t> bcInc, detID, padID;
145 std::vector<uint8_t> isSaturated;
146
147 o2::ctf::CTFIOSize iosize;
148#define DECODEMCH(part, slot) ec.decode(part, int(slot), mCoders[int(slot)])
149 // clang-format off
150 iosize += DECODEMCH(bcInc, CTF::BLC_bcIncROF);
151 iosize += DECODEMCH(orbitInc, CTF::BLC_orbitIncROF);
152 iosize += DECODEMCH(nDigits, CTF::BLC_nDigitsROF);
153
154 iosize += DECODEMCH(tfTime, CTF::BLC_tfTime);
155 iosize += DECODEMCH(nSamples, CTF::BLC_nSamples);
156 iosize += DECODEMCH(isSaturated, CTF::BLC_isSaturated);
157 iosize += DECODEMCH(detID, CTF::BLC_detID);
158 iosize += DECODEMCH(padID, CTF::BLC_padID);
159 iosize += DECODEMCH(ADC, CTF::BLC_ADC);
160 // clang-format on
161 //
162 rofVec.clear();
163 digVec.clear();
164 rofVec.reserve(header.nROFs);
165 digVec.reserve(header.nDigits);
166
167 uint32_t firstEntry = 0, rofCount = 0, digCount = 0;
168 o2::InteractionRecord ir(header.firstBC, header.firstOrbit);
169
170 for (uint32_t irof = 0; irof < header.nROFs; irof++) {
171 // restore ROFRecord
172 if (orbitInc[irof]) { // non-0 increment => new orbit
173 ir.bc = bcInc[irof]; // bcInc has absolute meaning
174 ir.orbit += orbitInc[irof];
175 } else {
176 ir.bc += bcInc[irof];
177 }
178
179 firstEntry = digVec.size();
180 for (auto ic = 0; ic < nDigits[irof]; ic++) {
181 digVec.emplace_back(Digit{detID[digCount], padID[digCount], ADC[digCount], tfTime[digCount], nSamples[digCount]});
182 digVec.back().setSaturated(isSaturated[digCount]);
183 digCount++;
184 }
185 rofVec.emplace_back(ROFRecord{ir, int(firstEntry), static_cast<int>(nDigits[irof])});
186 }
187 assert(rofVec.size() == header.nROFs);
188 assert(digCount == header.nDigits);
189 iosize.rawIn = sizeof(ROFRecord) * rofVec.size() + sizeof(Digit) * digVec.size();
190 return iosize;
191}
192
193} // namespace mch
194} // namespace o2
195
196#endif // O2_MCH_CTFCODER_H
Declarations for CTFCoderBase class (support of external dictionaries)
uint32_t op
#define ENCODEMCH(beg, end, slot, bits)
#define DECODEMCH(part, slot)
Helper for MCH CTF creation.
Definitions for MCH CTF data.
Definition of the MCH ROFrame record.
void checkDictVersion(const CTFDictHeader &h) const
ctf::ANSHeader mANSVersion
std::string getPrefix() const
o2::utils::IRFrameSelector mIRFrameSelector
virtual void assignDictVersion(CTFDictHeader &h) const
<<======================== Auxiliary classes =======================<<
static auto get(void *head)
cast arbitrary buffer head to container class. Head is supposed to respect the alignment
const H & getHeader() const
void print(const std::string &prefix="", int verbosity=1) const
print itself
EncodedBlocks< CTFHeader, N, uint32_t > base
static auto create(void *head, size_t sz)
create container from arbitrary buffer of predefined size (in bytes!!!). Head is supposed to respect ...
Static class with identifiers, bitmasks and names for ALICE detectors.
Definition DetID.h:58
o2::ctf::CTFIOSize decode(const CTF::base &ec, VROF &rofVec, VCOL &digVec)
entropy decode data from buffer with CTF
Definition CTFCoder.h:134
void createCoders(const std::vector< char > &bufVec, o2::ctf::CTFCoderBase::OpType op) final
Definition CTFCoder.cxx:40
~CTFCoder() final=default
o2::ctf::CTFIOSize encode(VEC &buff, const gsl::span< const ROFRecord > &rofData, const gsl::span< const Digit > &digData)
entropy-encode data to buffer with CTF
Definition CTFCoder.h:65
CTFCoder(o2::ctf::CTFCoderBase::OpType op)
Definition CTFCoder.h:40
MCH digit implementation.
Definition Digit.h:31
long check(o2::dataformats::IRFrame fr, size_t bwd=0, size_t fwd=0)
GLuint entry
Definition glcorearb.h:5735
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.
uint32_t orbit
LHC orbit.
uint16_t bc
bunch crossing ID of interaction
Detector header base.
wrapper for the Entropy-encoded clusters of the TF
Definition CTF.h:40
@ BLC_padID
Definition CTF.h:50
@ BLC_tfTime
Definition CTF.h:46
@ BLC_orbitIncROF
Definition CTF.h:44
@ BLC_isSaturated
Definition CTF.h:48
@ BLC_nDigitsROF
Definition CTF.h:45
@ BLC_ADC
Definition CTF.h:51
@ BLC_nSamples
Definition CTF.h:47
@ BLC_detID
Definition CTF.h:49
@ BLC_bcIncROF
Definition CTF.h:43
o2::InteractionRecord ir(0, 0)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::array< std::vector< ROFRecord >, NEvTypes > rofData