14#include <fmt/format.h>
17#include <fairlogger/Logger.h>
29 mErrorCounter.mErrorRegister2 = 0;
30 mErrorCounter.mErrorRegister3 = 0;
33 mAltroConfig.mWord1 = 0;
34 mAltroConfig.mWord2 = 0;
35 mIsInitialized =
false;
40 const int MIN_FWVERSION = 2;
41 const int MAX_FWVERSION = 2;
42 if ((trailerword >> 30) != 3) {
45 auto firmwarevesion = (trailerword >> 16) & 0xFF;
46 auto trailerSize = (trailerword & 0x7F);
47 if (firmwarevesion < MIN_FWVERSION || firmwarevesion > MAX_FWVERSION) {
50 if (trailerSize < 2) {
53 if (firmwarevesion == 2) {
54 if (trailerSize < 9) {
64 int index = payloadwords.size();
65 auto word = payloadwords[--
index];
66 if ((word >> 30) != 3) {
69 mFirmwareVersion = (word >> 16) & 0xFF;
71 mRCUId = (
int)((word >> 7) & 0x1FF);
72 int trailerSize = (word & 0x7F);
74 if (trailerSize < 2) {
77 mTrailerSize = trailerSize;
80 int foundTrailerWords = 0;
81 for (; trailerSize > 0; trailerSize--) {
82 word = payloadwords[--
index];
83 if ((word >> 30) != 2) {
87 int parCode = (word >> 26) & 0xF;
88 int parData = word & 0x3FFFFFF;
93 mFECERRA = ((parData >> 13) & 0x1FFF) << 7;
94 mFECERRB = ((parData & 0x1FFF)) << 7;
98 mErrorCounter.mErrorRegister2 = parData & 0x1FF;
102 mErrorCounter.mErrorRegister3 = parData & 0x1FFFFFF;
106 mActiveFECsA = parData & 0xFFFF;
110 mActiveFECsB = parData & 0xFFFF;
114 mAltroConfig.mWord1 = parData & 0xFFFFF;
118 mAltroConfig.mWord2 = parData & 0x1FFFFFF;
121 LOG(warning) <<
"RCU trailer: Undefined parameter code " << parCode <<
" in word " <<
index <<
" (0x" << std::hex << word << std::dec <<
"), ignoring word";
126 auto lastword = payloadwords[--
index];
127 if (lastword >> 30 == 2) {
128 mPayloadSize = lastword & 0x3FFFFFF;
131 if (foundTrailerWords + 1 < mTrailerSize) {
134 mIsInitialized =
true;
139 uint8_t fq = mAltroConfig.mSampleTime;
164 switch (timesample) {
177 mAltroConfig.mSampleTime = sample;
179 mAltroConfig.mL1Phase = (triggertime % timesample) / 25;
186 if (
phase >= tSample) {
194 std::vector<uint32_t> encoded;
195 encoded.emplace_back(mPayloadSize | 2 << 30);
196 encoded.emplace_back(mFECERRB >> 7 | (mFECERRA >> 7) << 13 | 1 << 26 | 2 << 30);
197 encoded.emplace_back(mErrorCounter.mErrorRegister2 | 2 << 26 | 2 << 30);
198 encoded.emplace_back(mErrorCounter.mErrorRegister3 | 3 << 26 | 2 << 30);
199 encoded.emplace_back(mActiveFECsA | 4 << 26 | 2 << 30);
200 encoded.emplace_back(mActiveFECsB | 5 << 26 | 2 << 30);
201 encoded.emplace_back(mAltroConfig.mWord1 | 6 << 26 | 2 << 30);
202 encoded.emplace_back(mAltroConfig.mWord2 | 7 << 26 | 2 << 30);
204 uint32_t lasttrailerword = 3 << 30 | mFirmwareVersion << 16 | mRCUId << 7 | (encoded.size() + 1);
205 encoded.emplace_back(lasttrailerword);
212 std::vector<std::string> errors;
213 double timesample = -1., l1phase = -1.;
217 errors.push_back(e.
what());
222 errors.push_back(e.
what());
225 stream <<
"RCU trailer (Format version 2):\n"
226 <<
"==================================================\n"
227 <<
"RCU ID: " << mRCUId <<
"\n"
228 <<
"Firmware version: " <<
int(mFirmwareVersion) <<
"\n"
229 <<
"Trailer size: " << mTrailerSize <<
"\n"
230 <<
"Payload size: " << mPayloadSize <<
"\n"
231 <<
"FECERRA: 0x" << std::hex << mFECERRA <<
"\n"
232 <<
"FECERRB: 0x" << std::hex << mFECERRB <<
"\n"
233 <<
"ERRREG2: 0x" << std::hex << mErrorCounter.mErrorRegister2 <<
"\n"
234 <<
"ERRREG3: 0x" << std::hex << mErrorCounter.mErrorRegister3 <<
"\n"
237 <<
"Active FECs (branch A): 0x" << std::hex << mActiveFECsA <<
"\n"
238 <<
"Active FECs (branch B): 0x" << std::hex << mActiveFECsB <<
"\n"
240 <<
"Polarity: " << (
getPolarity() ?
"yes" :
"no") <<
"\n"
252 <<
"AltroCFG1: 0x" << std::hex << mAltroConfig.mWord1 <<
"\n"
253 <<
"AltroCFG2: 0x" << std::hex << mAltroConfig.mWord2 <<
"\n"
254 <<
"Sampling time: " << std::dec << timesample <<
" ns\n"
255 <<
"L1 Phase: " << std::dec << l1phase <<
" ns (" << mAltroConfig.mL1Phase <<
")\n"
256 << std::dec << std::fixed;
259 <<
"-------------------------------------------------\n";
260 for (
const auto& e : errors) {
264 stream <<
"==================================================\n";
Header to collect LHC related constants.
Error handling of the RCU trailer.
const char * what() const noexcept override
Access to the error message.
@ SIZE_INVALID
Invalid trailer size.
@ SAMPLINGFREQ_INVALID
Invalid sampling frequency.
@ DECODING_INVALID
Invalid words during decoding.
@ L1PHASE_INVALID
Invalid L1 phase.
Information stored in the RCU trailer.
void reset()
Reset the RCU trailer.
uint16_t getNumberOfChannelAddressMismatch() const
Get the number of channels with address mismatch.
bool hasZeroSuppression() const
Check whether zero suppression has been applied.
void setTimeSamplePhaseNS(uint64_t triggertime, uint64_t timesample)
Set the time sample length and L1 phase based on the trigger time.
static RCUTrailer constructFromPayloadWords(const gsl::span< const uint32_t > payloadwords)
Decode RCU trailer from payload.
uint16_t getNumberOfPostsamples() const
Get the number of postsamples (after zero suppression)
uint16_t getBaselineCorrection() const
Get baseline correction method.
uint16_t getNumberOfSamplesPerChannel() const
Get the number of samples per channel.
void printStream(std::ostream &stream) const
Prints the contents of the RCU trailer data.
bool hasSecondBaselineCorr() const
Check if second baseline correction is applied.
uint16_t getNumberOfPretriggerSamples() const
Get the number of pretrigger samples.
uint16_t getGlitchFilter() const
Get the glitch filter.
static bool checkLastTrailerWord(uint32_t trailerword)
Check whether the word is a valid last trailer word.
void constructFromRawPayload(const gsl::span< const uint32_t > payloadwords)
Decode RCU trailer from the 32-bit words in the raw buffer.
uint16_t getNumberOfNonZeroSuppressedPresamples() const
Get the number of presamples before zero suppression.
double getL1PhaseNS() const
Access to the L1 phase.
bool isSparseReadout() const
Check whether readout is in sparse mode.
uint16_t getNumberOfAltroBuffers() const
Get the number of ALTRO buffers.
double getTimeSampleNS() const
Access to the sampling time.
std::vector< uint32_t > encode() const
Encode RCU trailer as array of DDL (32-bit) words.
bool getPolarity() const
Check polarity setting.
uint16_t getNumberOfNonZeroSuppressedPostsamples() const
Get the number of postsamples before zero suppression.
uint16_t getNumberOfPresamples() const
Get the number of presamples (after zero suppression)
uint16_t getNumberOfChannelLengthMismatch() const
Get the number of channels with length mismatch.
constexpr double LHCBunchSpacingNS
std::ostream & operator<<(std::ostream &stream, const Cell &cell)
Stream operator for EMCAL cell.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"