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