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_EMCAL_CTFCODER_H
17#define O2_EMCAL_CTFCODER_H
18
19#include <algorithm>
20#include <iterator>
21#include <string>
22#include <array>
27
28class TTree;
29
30namespace o2
31{
32namespace emcal
33{
34
36{
37 public:
38 CTFCoder(o2::ctf::CTFCoderBase::OpType op) : o2::ctf::CTFCoderBase(op, CTF::getNBlocks(), o2::detectors::DetID::EMC) {}
39 ~CTFCoder() final = default;
40
42 template <typename VEC>
43 o2::ctf::CTFIOSize encode(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cell>& cellData);
44
46 template <typename VTRG, typename VCELL>
47 o2::ctf::CTFIOSize decode(const CTF::base& ec, VTRG& trigVec, VCELL& cellVec);
48
49 void createCoders(const std::vector<char>& bufVec, o2::ctf::CTFCoderBase::OpType op) final;
50
51 private:
52 template <typename VEC>
53 o2::ctf::CTFIOSize encode_impl(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cell>& cellData);
54 void appendToTree(TTree& tree, CTF& ec);
55 void readFromTree(TTree& tree, int entry, std::vector<TriggerRecord>& trigVec, std::vector<Cell>& cellVec);
56 void assignDictVersion(o2::ctf::CTFDictHeader& h) const final;
57 std::vector<TriggerRecord> mTrgDataFilt;
58 std::vector<Cell> mCellDataFilt;
59};
60
62template <typename VEC>
63o2::ctf::CTFIOSize CTFCoder::encode(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cell>& cellData)
64{
65 if (mIRFrameSelector.isSet()) { // preselect data
66 mTrgDataFilt.clear();
67 mCellDataFilt.clear();
68 for (const auto& trig : trigData) {
69 if (mIRFrameSelector.check(trig.getBCData()) >= 0) {
70 mTrgDataFilt.push_back(trig);
71 auto cellIt = cellData.begin() + trig.getFirstEntry();
72 auto& trigC = mTrgDataFilt.back();
73 trigC.setDataRange((int)mCellDataFilt.size(), trig.getNumberOfObjects());
74 std::copy(cellIt, cellIt + trig.getNumberOfObjects(), std::back_inserter(mCellDataFilt));
75 }
76 }
77 return encode_impl(buff, mTrgDataFilt, mCellDataFilt);
78 }
79 return encode_impl(buff, trigData, cellData);
80}
81
82template <typename VEC>
83o2::ctf::CTFIOSize CTFCoder::encode_impl(VEC& buff, const gsl::span<const TriggerRecord>& trigData, const gsl::span<const Cell>& cellData)
84{
86 // what to do which each field: see o2::ctd::Metadata explanation
87 constexpr MD optField[CTF::getNBlocks()] = {
88 MD::EENCODE_OR_PACK, // BLC_bcIncTrig
89 MD::EENCODE_OR_PACK, // BLC_orbitIncTrig
90 MD::EENCODE_OR_PACK, // BLC_entriesTrig
91 MD::EENCODE_OR_PACK, // BLC_towerID
92 MD::EENCODE_OR_PACK, // BLC_time
93 MD::EENCODE_OR_PACK, // BLC_energy
94 MD::EENCODE_OR_PACK, // BLC_status
95 // extra slot was added in the end
96 MD::EENCODE_OR_PACK // BLC_trigger
97 };
98
99 CTFHelper helper(trigData, cellData);
100
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 ENCODEEMC(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 += ENCODEEMC(helper.begin_bcIncTrig(), helper.end_bcIncTrig(), CTF::BLC_bcIncTrig, 0);
116 iosize += ENCODEEMC(helper.begin_orbitIncTrig(), helper.end_orbitIncTrig(), CTF::BLC_orbitIncTrig, 0);
117 iosize += ENCODEEMC(helper.begin_entriesTrig(), helper.end_entriesTrig(), CTF::BLC_entriesTrig, 0);
118
119 iosize += ENCODEEMC(helper.begin_towerID(), helper.end_towerID(), CTF::BLC_towerID, 0);
120 iosize += ENCODEEMC(helper.begin_time(), helper.end_time(), CTF::BLC_time, 0);
121 iosize += ENCODEEMC(helper.begin_energy(), helper.end_energy(), CTF::BLC_energy, 0);
122 iosize += ENCODEEMC(helper.begin_status(), helper.end_status(), CTF::BLC_status, 0);
123 // extra slot was added in the end
124 iosize += ENCODEEMC(helper.begin_trigger(), helper.end_trigger(), CTF::BLC_trigger, 0);
125 // clang-format on
126 CTF::get(buff.data())->print(getPrefix(), mVerbosity);
127 finaliseCTFOutput<CTF>(buff);
128 iosize.rawIn = sizeof(TriggerRecord) * trigData.size() + sizeof(Cell) * cellData.size();
129 return iosize;
130}
131
133template <typename VTRG, typename VCELL>
134o2::ctf::CTFIOSize CTFCoder::decode(const CTF::base& ec, VTRG& trigVec, VCELL& cellVec)
135{
136 const auto& header = ec.getHeader();
137 checkDictVersion(static_cast<const o2::ctf::CTFDictHeader&>(header));
139 std::vector<int32_t> orbitInc;
140 std::vector<int16_t> bcInc;
141 std::vector<uint16_t> entries, energy, cellTime, tower, trigger;
142 std::vector<uint8_t> status;
143
144 o2::ctf::CTFIOSize iosize;
145#define DECODEEMCAL(part, slot) ec.decode(part, int(slot), mCoders[int(slot)])
146 // clang-format off
147 iosize += DECODEEMCAL(bcInc, CTF::BLC_bcIncTrig);
148 iosize += DECODEEMCAL(orbitInc, CTF::BLC_orbitIncTrig);
149 iosize += DECODEEMCAL(entries, CTF::BLC_entriesTrig);
150 iosize += DECODEEMCAL(tower, CTF::BLC_towerID);
151
152 iosize += DECODEEMCAL(cellTime, CTF::BLC_time);
153 iosize += DECODEEMCAL(energy, CTF::BLC_energy);
154 iosize += DECODEEMCAL(status, CTF::BLC_status);
155 // extra slot was added in the end
156 iosize += DECODEEMCAL(trigger, CTF::BLC_trigger);
157 // triggers were added later, in old data they are absent:
158 if (trigger.empty()) {
159 trigger.resize(header.nTriggers);
160 }
161 //
162 // clang-format on
163 //
164 trigVec.clear();
165 cellVec.clear();
166 trigVec.reserve(header.nTriggers);
167 status.reserve(header.nCells);
168
170 if (header.majorVersion == 1 && header.minorVersion == 1) {
172 } else if (header.majorVersion == 1 && header.minorVersion == 2) {
174 }
175
176 uint32_t firstEntry = 0, cellCount = 0;
177 o2::InteractionRecord ir(header.firstBC, header.firstOrbit);
178 bool checkIROK = (mBCShift == 0); // need to check if CTP offset correction does not make the local time negative ?
179 // Cell cell;
180 TriggerRecord trg;
181 for (uint32_t itrig = 0; itrig < header.nTriggers; itrig++) {
182 // restore TrigRecord
183 if (orbitInc[itrig]) { // non-0 increment => new orbit
184 ir.bc = bcInc[itrig]; // bcInc has absolute meaning
185 ir.orbit += orbitInc[itrig];
186 } else {
187 ir.bc += bcInc[itrig];
188 }
189 if (checkIROK || canApplyBCShift(ir)) { // correction will be ok
190 checkIROK = true;
191 } else { // correction would make IR prior to mFirstTFOrbit, skip
192 cellCount += entries[itrig];
193 continue;
194 }
195 firstEntry = cellVec.size();
196
197 for (uint16_t ic = 0; ic < entries[itrig]; ic++) {
198 cellVec.emplace_back(tower[cellCount], energy[cellCount], cellTime[cellCount], status[cellCount], encodingversion);
199 cellCount++;
200 }
201 trg.setBCData(ir - mBCShift);
202 trg.setDataRange(firstEntry, entries[itrig]);
203 trg.setTriggerBitsCompressed(trigger[itrig]);
204 trigVec.emplace_back(trg);
205 }
206 assert(cellCount == header.nCells);
207 iosize.rawIn = sizeof(TriggerRecord) * trigVec.size() + sizeof(Cell) * cellVec.size();
208 return iosize;
209}
210
211} // namespace emcal
212} // namespace o2
213
214#endif // O2_EMCAL_CTFCODER_H
Declarations for CTFCoderBase class (support of external dictionaries)
Definitions for EMC CTF data.
#define ENCODEEMC(beg, end, slot, bits)
#define DECODEEMCAL(part, slot)
Helper for EMCAL CTF creation.
uint32_t op
Class for time synchronization of RawReader instances.
void checkDictVersion(const CTFDictHeader &h) const
ctf::ANSHeader mANSVersion
std::string getPrefix() const
o2::utils::IRFrameSelector mIRFrameSelector
bool canApplyBCShift(const o2::InteractionRecord &ir, long shift) 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
~CTFCoder() final=default
CTFCoder(o2::ctf::CTFCoderBase::OpType op)
Definition CTFCoder.h:38
void createCoders(const std::vector< char > &bufVec, o2::ctf::CTFCoderBase::OpType op) final
Definition CTFCoder.cxx:40
o2::ctf::CTFIOSize decode(const CTF::base &ec, VTRG &trigVec, VCELL &cellVec)
entropy decode data from buffer with CTF
Definition CTFCoder.h:134
o2::ctf::CTFIOSize encode(VEC &buff, const gsl::span< const TriggerRecord > &trigData, const gsl::span< const Cell > &cellData)
entropy-encode data to buffer with CTF
Definition CTFCoder.h:63
EMCAL compressed cell information.
Definition Cell.h:59
Header for data corresponding to the same hardware trigger.
void setBCData(const BCData &data)
void setDataRange(int firstentry, int nentries)
void setTriggerBitsCompressed(uint16_t triggerbits)
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 triggers and cells of the TF
Definition CTF.h:41
@ BLC_entriesTrig
Definition CTF.h:46
@ BLC_status
Definition CTF.h:50
@ BLC_trigger
Definition CTF.h:52
@ BLC_energy
Definition CTF.h:49
@ BLC_towerID
Definition CTF.h:47
@ BLC_bcIncTrig
Definition CTF.h:44
@ BLC_orbitIncTrig
Definition CTF.h:45
o2::InteractionRecord ir(0, 0)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))