Project
Loading...
Searching...
No Matches
SACDecoder.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 ALICEO2_SACDECODER_H
17#define ALICEO2_SACDECODER_H
18
19#include <algorithm>
20#include <array>
21#include <deque>
22#include <unordered_map>
23#include <vector>
24#include <bitset>
25#include <gsl/span>
26
29
30#include "DataFormatsTPC/Defs.h"
31#include "DataFormatsTPC/SAC.h"
32
34
35namespace o2::tpc::sac
36{
37
38constexpr float ADCtoNanoAmp = 125000.f / 8388608.f;
39constexpr uint32_t ChannelsPerFE = 8;
40constexpr size_t Instances = 2;
41constexpr size_t NumberFEs = FEsPerInstance * Instances;
42
45 uint32_t timeStamp{};
46 std::array<int32_t, 8> currents{};
47
49 {
50 std::fill(currents.begin(), currents.end(), 0);
51 }
52
53 void reset()
54 {
55 timeStamp = 0;
57 }
58};
59
60struct DataPoint {
61 uint32_t time{};
62 int32_t currents[GEMSTACKS]{};
63
64 void reset()
65 {
66 time = 0;
67 std::fill(&currents[0], &currents[GEMSTACKS], 0);
68 }
69
71};
72
74 double referenceTime{-1.};
75 std::vector<DataPoint> data;
76 std::vector<std::bitset<NumberFEs>> fes;
77
78 void resize(size_t newSize)
79 {
80 data.resize(newSize);
81 fes.resize(newSize);
82 }
83
85 size_t getNGoodEntries() const
86 {
87 size_t posGood = 0;
88 while (posGood < fes.size()) {
89 if (!fes[posGood].all()) {
90 break;
91 }
92 ++posGood;
93 }
94 return posGood;
95 }
96
98 gsl::span<const DataPoint> getGoodData() const
99 {
100 return gsl::span<const DataPoint>(data.data(), data.data() + getNGoodEntries());
101 }
102
105 {
106 const auto pos = getNGoodEntries();
107 data.erase(data.begin(), data.begin() + pos);
108 fes.erase(fes.begin(), fes.begin() + pos);
109 }
110
112 void clear()
113 {
114 data.clear();
115 fes.clear();
116 }
117
119 void insertFront(const size_t entries)
120 {
121 data.insert(data.begin(), entries, DataPoint());
122 fes.insert(fes.begin(), entries, std::bitset<NumberFEs>());
123 }
124
126 void setData(const size_t pos, uint32_t time, const DecodedDataFE& decdata, const int feid);
127
129};
130
132{
133 public:
134 static constexpr std::string_view AllowedAdditionalStreams{"MRLIX"};
135 static constexpr uint32_t SampleDistance = 16;
136 static constexpr double SampleTimeMS = LHCBunchSpacingMUS * 2.5;
138
139 enum class DebugFlags {
140 PacketInfo = 0x01,
141 TimingInfo = 0x02,
142 ProcessingInfo = 0x04,
143 DumpFullStream = 0x10,
144 StreamSingleFE = 0x100,
145 StreamFinalData = 0x200,
146 };
147
148 enum class ReAlignType {
149 None = 0,
150 AlignOnly = 1,
153 };
154
155 bool process(const char* data, size_t size);
156
157 void runDecoding();
158
159 void finalize();
160
162 {
163 mDecodedData.referenceTime = time;
164 }
165
166 double getReferenceTime() const
167 {
168 return mDecodedData.referenceTime;
169 }
170
180 void setDecodeAdditional(std::string_view additional)
181 {
182 mDecodeAdditional.clear();
183 for (const auto c : additional) {
184 if (AllowedAdditionalStreams.find(c) != std::string_view::npos) {
185 mDecodeAdditional += c;
186 }
187 }
188 }
189
191 {
192 if (!mDebugStream) {
193 mDebugStream = std::make_unique<o2::utils::TreeStreamRedirector>(mDebugOutputName.data(), "recreate");
194 }
195 }
196
198 void setDebugLevel(uint32_t level = (uint32_t)DebugFlags::PacketInfo)
199 {
200 mDebugLevel = level;
201 if ((level & (uint32_t)DebugFlags::StreamSingleFE) || (level & (uint32_t)DebugFlags::StreamFinalData)) {
203 }
204 }
205
206 void clearDecodedData();
207 void streamDecodedData(bool streamAll = false);
208
209 const DecodedData& getDecodedData() const { return mDecodedData; }
210
212 ReAlignType getReAlignType() const { return mReAlignType; }
213
216 static void setNThreads(const int nThreads) { sNThreads = nThreads; }
217
219 static int getNThreads() { return sNThreads; }
220
221 private:
222 inline static int sNThreads{1};
223 size_t mCollectedDataPackets{};
224 std::array<uint32_t, Instances> mPktCountInstance{};
225 std::array<uint32_t, NumberFEs> mPktCountFEs{};
226 std::array<std::pair<uint32_t, uint32_t>, NumberFEs> mTSCountFEs;
227 std::array<std::string, NumberFEs> mDataStrings;
228 DecodedData mDecodedData;
229 std::unique_ptr<o2::utils::TreeStreamRedirector> mDebugStream;
230 std::string mDecodeAdditional;
231 std::string mDebugOutputName{"SAC_debug.root"};
232 ReAlignType mReAlignType{ReAlignType::None};
233
234 uint32_t mDebugLevel{0};
235
236 uint32_t decodeTimeStamp(const char* data);
237
239 int decodeChannels(DecodedDataFE& sacs, size_t& carry, int feid);
240 void decode(int feid);
241
242 void printPacketInfo(const sac::packet& sac);
243
244 void dumpStreams();
245
246 ClassDefNV(Decoder, 0);
247};
248
249} // namespace o2::tpc::sac
250#endif
int16_t time
Definition RawEventData.h:4
Header to collect LHC related constants.
uint16_t pos
Definition RawData.h:3
uint32_t c
Definition RawData.h:2
Sampled Analogue Currents (SACs) data format definitions.
static int getNThreads()
Definition SACDecoder.h:219
static void setNThreads(const int nThreads)
Definition SACDecoder.h:216
@ DumpFullStream
Dump the data character streams.
@ StreamSingleFE
Stream debug output for each single FE.
@ ProcessingInfo
Print some processing info.
@ PacketInfo
Print packe information.
@ StreamFinalData
Stream debug output for each single FE.
void setDebugLevel(uint32_t level=(uint32_t) DebugFlags::PacketInfo)
set a debug level, see DebugFlags
Definition SACDecoder.h:198
double getReferenceTime() const
Definition SACDecoder.h:166
static constexpr double SampleTimeMS
Internal timer sampling time in milli seconds (downsampled from LHC clock x 2500)
Definition SACDecoder.h:136
const DecodedData & getDecodedData() const
Definition SACDecoder.h:209
void setReAlignType(ReAlignType type=ReAlignType::AlignOnly)
Definition SACDecoder.h:211
void setDecodeAdditional(std::string_view additional)
Definition SACDecoder.h:180
ReAlignType getReAlignType() const
Definition SACDecoder.h:212
static constexpr uint32_t SampleDistance
Number of samples between time data stamps.
Definition SACDecoder.h:135
void streamDecodedData(bool streamAll=false)
static constexpr double SampleDistanceTimeMS
Time distance in MS between samples.
Definition SACDecoder.h:137
void setReferenceTime(double time)
Definition SACDecoder.h:161
static constexpr std::string_view AllowedAdditionalStreams
Allowed additional data streams that can be decoded with debug stream enabled.
Definition SACDecoder.h:134
@ MaxType
Largest type number.
@ None
Print packe information.
@ AlignAndFillMissing
Try re-alignment and fill missing packets with 0s.
GLsizeiptr size
Definition glcorearb.h:659
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
GLboolean * data
Definition glcorearb.h:298
GLint level
Definition glcorearb.h:275
constexpr double LHCBunchSpacingMUS
constexpr size_t NumberFEs
Total number of frontends to process.
Definition SACDecoder.h:41
constexpr uint32_t ChannelsPerFE
Channels per front-end card. One channel is one stack.
Definition SACDecoder.h:39
constexpr float ADCtoNanoAmp
125000 nA / std::pow(2,23) bits
Definition SACDecoder.h:38
constexpr size_t Instances
Number of instances to process.
Definition SACDecoder.h:40
constexpr unsigned short GEMSTACKS
Definition Defs.h:60
Marks an empty item in the context.
int32_t currents[GEMSTACKS]
Current in signed ADC values, use SACDecoder::ADCtoNanoAmp, to convert to nA.
Definition SACDecoder.h:62
uint32_t time
Reference time since epoch in ms.
Definition SACDecoder.h:61
ClassDefNV(DataPoint, 2)
Decoded data of one FE.
Definition SACDecoder.h:44
std::array< int32_t, 8 > currents
Definition SACDecoder.h:46
void insertFront(const size_t entries)
insert entries at the front
Definition SACDecoder.h:119
double referenceTime
reference time when sampling clock was started
Definition SACDecoder.h:74
void setData(const size_t pos, uint32_t time, const DecodedDataFE &decdata, const int feid)
copy decoded data from single FE and mark FE as received
size_t getNGoodEntries() const
Number of good entries, for which all FEs were decoded.
Definition SACDecoder.h:85
std::vector< std::bitset< NumberFEs > > fes
bitmask of decoded FEs
Definition SACDecoder.h:76
void clearGoodData()
clear entries where all FEs were already decoded
Definition SACDecoder.h:104
gsl::span< const DataPoint > getGoodData() const
span of good entries for which all FEs were already decoded
Definition SACDecoder.h:98
ClassDefNV(DecodedData, 1)
void clear()
clear all data
Definition SACDecoder.h:112
void resize(size_t newSize)
Definition SACDecoder.h:78
std::vector< DataPoint > data
decoded data
Definition SACDecoder.h:75