Project
Loading...
Searching...
No Matches
Cell.cxx
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
14#include <iostream>
15#include <bitset>
16#include <cmath>
17
18using namespace o2::emcal;
19
20namespace TimeEncoding
21{
22const float TIME_SHIFT = 600.,
23 TIME_RANGE = 1500.,
25
26}
28{
29namespace v0
30{
31const float
34}
35
36namespace v1
37{
38const float
39 ENERGY_BITS = static_cast<float>(0x3FFF),
40 HGLGTRANSITION = o2::emcal::constants::EMCAL_HGLGTRANSITION * o2::emcal::constants::EMCAL_ADCENERGY,
46}
47
48namespace v2
49{
50const float
51 ENERGY_BITS = static_cast<float>(0x3FFF),
53 HGLGTRANSITION = o2::emcal::constants::OVERFLOWCUT * o2::emcal::constants::EMCAL_ADCENERGY,
60
61}
62} // namespace EnergyEncoding
63
64namespace DecodingV0
65{
66struct __attribute__((packed)) CellDataPacked {
67 uint16_t mTowerID : 15;
68 uint16_t mTime : 11;
69 uint16_t mEnergy : 14;
70 uint16_t mCellStatus : 2;
71 uint16_t mZerod : 6;
72};
73} // namespace DecodingV0
74
75Cell::Cell(short tower, float energy, float timestamp, ChannelType_t ctype) : mTowerID(tower), mEnergy(energy), mTimestamp(timestamp), mChannelType(ctype)
76{
77}
78
79Cell::Cell(uint16_t towerBits, uint16_t energyBits, uint16_t timestampBits, uint16_t channelBits, EncoderVersion version)
80{
81 initialiseFromEncoded(towerBits, timestampBits, energyBits, channelBits, version);
82}
83
85{
86 return mTowerID;
87}
88
90{
91 return encodeTime(mTimestamp);
92}
93
95{
96 uint16_t energyBits = 0;
97 switch (version) {
99 energyBits = encodeEnergyV0(mEnergy);
100 break;
101
103 energyBits = encodeEnergyV1(mEnergy, mChannelType);
104 break;
105
107 energyBits = encodeEnergyV2(mEnergy, mChannelType);
108 break;
109 }
110 return energyBits;
111}
112
114{
115 return static_cast<uint16_t>(mChannelType);
116}
117
118void Cell::setEnergyEncoded(uint16_t energyBits, uint16_t channelTypeBits, EncoderVersion version)
119{
120 switch (version) {
122 mEnergy = decodeEnergyV0(energyBits);
123 break;
125 mEnergy = decodeEnergyV1(energyBits, static_cast<ChannelType_t>(channelTypeBits));
126 break;
128 mEnergy = decodeEnergyV2(energyBits, static_cast<ChannelType_t>(channelTypeBits));
129 break;
130 }
131}
132
133void Cell::setTimestampEncoded(uint16_t timestampBits)
134{
135 mTimestamp = decodeTime(timestampBits);
136}
137
138void Cell::setTowerIDEncoded(uint16_t towerIDBits)
139{
140 mTowerID = towerIDBits;
141}
142
143void Cell::setChannelTypeEncoded(uint16_t channelTypeBits)
144{
145 mChannelType = static_cast<ChannelType_t>(channelTypeBits);
146}
147
149{
150 auto bitrepresentation = reinterpret_cast<const DecodingV0::CellDataPacked*>(bitfield);
151 mEnergy = decodeEnergyV0(bitrepresentation->mEnergy);
152 mTimestamp = decodeTime(bitrepresentation->mTime);
153 mTowerID = bitrepresentation->mTowerID;
154 mChannelType = static_cast<ChannelType_t>(bitrepresentation->mCellStatus);
155}
156
158{
159 return decodeEnergyV0(reinterpret_cast<const DecodingV0::CellDataPacked*>(bitfield)->mEnergy);
160}
161
163{
164 return decodeTime(reinterpret_cast<const DecodingV0::CellDataPacked*>(bitfield)->mTime);
165}
166
168{
169 return static_cast<ChannelType_t>(reinterpret_cast<const DecodingV0::CellDataPacked*>(bitfield)->mCellStatus);
170}
171
173{
174 return reinterpret_cast<const DecodingV0::CellDataPacked*>(bitfield)->mTowerID;
175}
176
178{
179 setEnergyEncoded(getEnergyEncoded(version), getCellTypeEncoded(), version);
180 setTimestampEncoded(getTimeStampEncoded());
181}
182
183uint16_t Cell::encodeTime(float timestamp)
184{
185 // truncate
186 auto timestampTruncated = timestamp;
187 const float TIME_MIN = -1. * TimeEncoding::TIME_SHIFT,
189 if (timestampTruncated < TIME_MIN) {
190 timestampTruncated = TIME_MIN;
191 } else if (timestampTruncated > TIME_MAX) {
192 timestampTruncated = TIME_MAX;
193 }
194 return static_cast<uint16_t>(std::round((timestampTruncated + TimeEncoding::TIME_SHIFT) / TimeEncoding::TIME_RESOLUTION));
195}
196
197uint16_t Cell::encodeEnergyV0(float energy)
198{
199 auto truncatedEnergy = energy;
200 if (truncatedEnergy < 0.) {
201 truncatedEnergy = 0.;
202 } else if (truncatedEnergy > EnergyEncoding::v0::ENERGY_TRUNCATION) {
203 truncatedEnergy = EnergyEncoding::v0::ENERGY_TRUNCATION;
204 }
205 return static_cast<uint16_t>(std::round(truncatedEnergy / EnergyEncoding::v0::ENERGY_RESOLUTION));
206}
207
208uint16_t Cell::encodeEnergyV1(float energy, ChannelType_t celltype)
209{
210 double truncatedEnergy = energy;
211 if (truncatedEnergy < 0.) {
212 truncatedEnergy = 0.;
213 } else if (truncatedEnergy > EnergyEncoding::v1::ENERGY_TRUNCATION) {
214 truncatedEnergy = EnergyEncoding::v1::ENERGY_TRUNCATION;
215 }
216 float resolutionApplied = 0., energyOffset = 0.;
217 switch (celltype) {
219 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_HG;
220 break;
221 }
223 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_LG;
224 energyOffset = EnergyEncoding::v1::HGLGTRANSITION;
225 break;
226 }
227 case ChannelType_t::TRU: {
228 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_TRU;
229 break;
230 }
232 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_LEDMON;
233 break;
234 }
235 }
236 return static_cast<uint16_t>(std::round((truncatedEnergy - energyOffset) / resolutionApplied));
237};
238
239uint16_t Cell::encodeEnergyV2(float energy, ChannelType_t celltype)
240{
241 double truncatedEnergy = energy;
242 if (truncatedEnergy < 0.) {
243 truncatedEnergy = 0.;
244 } else if (truncatedEnergy > EnergyEncoding::v2::ENERGY_TRUNCATION) {
245 truncatedEnergy = EnergyEncoding::v2::ENERGY_TRUNCATION;
246 }
247 float resolutionApplied = 0., energyOffset = 0.;
248 switch (celltype) {
250 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_HG;
251 break;
252 }
254 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_LG;
255 energyOffset = EnergyEncoding::v2::OFFSET_LG;
256 break;
257 }
258 case ChannelType_t::TRU: {
259 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_TRU;
260 break;
261 }
263 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_LEDMON;
264 break;
265 }
266 }
267 return static_cast<uint16_t>(std::round((truncatedEnergy - energyOffset) / resolutionApplied));
268};
269
270uint16_t Cell::V0toV1(uint16_t energyBits, ChannelType_t celltype)
271{
272 auto decodedEnergy = decodeEnergyV0(energyBits);
273 return encodeEnergyV1(decodedEnergy, celltype);
274}
275
276uint16_t Cell::V0toV2(uint16_t energyBits, ChannelType_t celltype)
277{
278 auto decodedEnergy = decodeEnergyV0(energyBits);
279 return encodeEnergyV2(decodedEnergy, celltype);
280}
281
282uint16_t Cell::V1toV2(uint16_t energyBits, ChannelType_t celltype)
283{
284 auto decodedEnergy = decodeEnergyV1(energyBits, celltype);
285 return encodeEnergyV2(decodedEnergy, celltype);
286}
287
288float Cell::decodeTime(uint16_t timestampBits)
289{
290 return (static_cast<float>(timestampBits) * TimeEncoding::TIME_RESOLUTION) - TimeEncoding::TIME_SHIFT;
291}
292
293float Cell::decodeEnergyV0(uint16_t energyBits)
294{
295 return static_cast<float>(energyBits) * EnergyEncoding::v0::ENERGY_RESOLUTION;
296}
297
298float Cell::decodeEnergyV1(uint16_t energyBits, ChannelType_t celltype)
299{
300 float resolutionApplied = 0.,
301 energyOffset = 0.;
302 switch (celltype) {
304 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_HG;
305 break;
306 }
308 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_LG;
309 energyOffset = EnergyEncoding::v1::HGLGTRANSITION;
310 break;
311 }
312 case ChannelType_t::TRU: {
313 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_TRU;
314 break;
315 }
317 resolutionApplied = EnergyEncoding::v1::ENERGY_RESOLUTION_LEDMON;
318 break;
319 }
320 }
321 return (static_cast<float>(energyBits) * resolutionApplied) + energyOffset;
322}
323
324float Cell::decodeEnergyV2(uint16_t energyBits, ChannelType_t celltype)
325{
326 float resolutionApplied = 0.,
327 energyOffset = 0.;
328 switch (celltype) {
330 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_HG;
331 break;
332 }
334 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_LG;
335 energyOffset = EnergyEncoding::v2::OFFSET_LG;
336 break;
337 }
338 case ChannelType_t::TRU: {
339 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_TRU;
340 break;
341 }
343 resolutionApplied = EnergyEncoding::v2::ENERGY_RESOLUTION_LEDMON;
344 break;
345 }
346 }
347 return (static_cast<float>(energyBits) * resolutionApplied) + energyOffset;
348}
349
350void Cell::PrintStream(std::ostream& stream) const
351{
352 stream << "EMCAL Cell: Type " << getType() << ", Energy " << getEnergy() << ", Time " << getTimeStamp() << ", Tower " << getTower();
353}
354
355std::ostream& o2::emcal::operator<<(std::ostream& stream, const Cell& c)
356{
357 c.PrintStream(stream);
358 return stream;
359}
uint32_t c
Definition RawData.h:2
uint32_t version
Definition RawData.h:8
EMCAL compressed cell information.
Definition Cell.h:59
static uint16_t encodeEnergyV2(float energy, ChannelType_t celltype)
Definition Cell.cxx:239
static short getTowerFromPackedBitfieldV0(const char *bitfield)
Definition Cell.cxx:172
uint16_t getCellTypeEncoded() const
Get encoded bit representation of cell type (for CTF)
Definition Cell.cxx:113
static uint16_t encodeTime(float timestamp)
Definition Cell.cxx:183
void initializeFromPackedBitfieldV0(const char *bitfield)
Definition Cell.cxx:148
static uint16_t V0toV1(uint16_t energybits, ChannelType_t celltype)
Definition Cell.cxx:270
uint16_t getTowerIDEncoded() const
Get encoded bit representation of tower ID (for CTF)
Definition Cell.cxx:84
void truncate(EncoderVersion version=EncoderVersion::EncodingV1)
Apply compression as done during writing to / reading from CTF.
Definition Cell.cxx:177
ChannelType_t getType() const
Get the type of the cell.
Definition Cell.h:125
Cell()=default
Default constructor.
static ChannelType_t getCellTypeFromPackedBitfieldV0(const char *bitfield)
Definition Cell.cxx:167
static uint16_t V1toV2(uint16_t energybits, ChannelType_t celltype)
Definition Cell.cxx:282
uint16_t getEnergyEncoded(EncoderVersion version=EncoderVersion::EncodingV2) const
Get encoded bit representation of energy (for CTF)
Definition Cell.cxx:94
static float decodeEnergyV1(uint16_t energybits, ChannelType_t celltype)
Definition Cell.cxx:298
uint16_t getTimeStampEncoded() const
Get encoded bit representation of timestamp (for CTF)
Definition Cell.cxx:89
static uint16_t encodeEnergyV1(float energy, ChannelType_t celltype)
Definition Cell.cxx:208
void initialiseFromEncoded(uint16_t towerIDBits, uint16_t timestampBits, uint16_t energyBits, uint16_t celltypeBits, EncoderVersion version=EncoderVersion::EncodingV1)
Initialize cell class from bit representation (for CTF decoding)
Definition Cell.h:172
float getEnergy() const
Get the energy of the cell.
Definition Cell.h:109
void PrintStream(std::ostream &stream) const
Definition Cell.cxx:350
float getTimeStamp() const
Get the time stamp.
Definition Cell.h:101
static float getTimeFromPackedBitfieldV0(const char *bitfield)
Definition Cell.cxx:162
static uint16_t V0toV2(uint16_t energybits, ChannelType_t celltype)
Definition Cell.cxx:276
static float getEnergyFromPackedBitfieldV0(const char *bitfield)
Definition Cell.cxx:157
static float decodeEnergyV2(uint16_t energybits, ChannelType_t celltype)
Definition Cell.cxx:324
static float decodeTime(uint16_t timestampBits)
Definition Cell.cxx:288
short getTower() const
Get the tower ID.
Definition Cell.h:93
static uint16_t encodeEnergyV0(float energy)
Definition Cell.cxx:197
static float decodeEnergyV0(uint16_t energybits)
Definition Cell.cxx:293
GLfloat v0
Definition glcorearb.h:811
GLfloat GLfloat v1
Definition glcorearb.h:812
GLuint GLuint stream
Definition glcorearb.h:1806
GLfloat GLfloat GLfloat v2
Definition glcorearb.h:813
const float ENERGY_TRUNCATION
Definition Cell.cxx:32
const float ENERGY_RESOLUTION
Definition Cell.cxx:33
const float ENERGY_RESOLUTION_TRU
Definition Cell.cxx:44
const float ENERGY_RESOLUTION_HG
Definition Cell.cxx:43
const float ENERGY_RESOLUTION_LG
Definition Cell.cxx:42
const float HGLGTRANSITION
Definition Cell.cxx:40
const float ENERGY_BITS
Definition Cell.cxx:39
const float ENERGY_TRUNCATION
Definition Cell.cxx:41
const float ENERGY_RESOLUTION_LEDMON
Definition Cell.cxx:45
const float HGLGTRANSITION
Definition Cell.cxx:53
const float ENERGY_TRUNCATION
Definition Cell.cxx:55
const float ENERGY_RESOLUTION_HG
Definition Cell.cxx:57
const float ENERGY_RESOLUTION_LEDMON
Definition Cell.cxx:59
const float ENERGY_RESOLUTION_TRU
Definition Cell.cxx:58
const float ENERGY_RESOLUTION_LG
Definition Cell.cxx:56
const float ENERGY_BITS
Definition Cell.cxx:51
const float SAFETYMARGIN
Definition Cell.cxx:52
const float OFFSET_LG
Definition Cell.cxx:54
const float TIME_RESOLUTION
Definition Cell.cxx:24
const float TIME_SHIFT
Definition Cell.cxx:22
const float TIME_RANGE
Definition Cell.cxx:23
std::ostream & operator<<(std::ostream &stream, const Cell &cell)
Stream operator for EMCAL cell.
Definition Cell.cxx:355
ChannelType_t
Type of a raw data channel.
Definition Constants.h:33
@ TRU
TRU channel.
Definition Constants.h:36
@ HIGH_GAIN
High gain channel.
Definition Constants.h:35
@ LOW_GAIN
Low gain channel.
Definition Constants.h:34
@ LEDMON
LED monitor channel.
Definition Constants.h:37