-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathRCUTrailer.cxx
More file actions
244 lines (227 loc) · 9.09 KB
/
Copy pathRCUTrailer.cxx
File metadata and controls
244 lines (227 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <cfloat>
#include <cmath>
#include <iostream>
#include <fmt/format.h>
#include "CommonConstants/LHCConstants.h"
#include "EMCALBase/RCUTrailer.h"
using namespace o2::emcal;
void RCUTrailer::reset()
{
mRCUId = -1;
mFirmwareVersion = 0;
mTrailerSize = 0;
mPayloadSize = 0;
mFECERRA = 0;
mFECERRB = 0;
mErrorCounter.mErrorRegister2 = 0;
mErrorCounter.mErrorRegister3 = 0;
mActiveFECsA = 0;
mActiveFECsB = 0;
mAltroConfig.mWord1 = 0;
mAltroConfig.mWord2 = 0;
mIsInitialized = false;
}
void RCUTrailer::constructFromRawPayload(const gsl::span<const uint32_t> payloadwords)
{
reset();
int index = payloadwords.size();
auto word = payloadwords[--index];
if ((word >> 30) != 3) {
throw Error(Error::ErrorType_t::DECODING_INVALID, "Last RCU trailer word not found!");
}
mFirmwareVersion = (word >> 16) & 0xFF;
mRCUId = (int)((word >> 7) & 0x1FF);
int trailerSize = (word & 0x7F);
if (trailerSize < 2) {
throw Error(Error::ErrorType_t::SIZE_INVALID, fmt::format("Invalid trailer size found (%d bytes) !", trailerSize * 4).data());
}
mTrailerSize = trailerSize;
trailerSize -= 2; // Cut first and last trailer words as they are handled separately
for (; trailerSize > 0; trailerSize--) {
word = payloadwords[--index];
if ((word >> 30) != 2) {
std::cerr << "Missing RCU trailer identifier pattern!\n";
continue;
}
int parCode = (word >> 26) & 0xF;
int parData = word & 0x3FFFFFF;
// std::cout << "Found trailer word 0x" << std::hex << word << "(Par code: " << std::dec << parCode << ", Par data: 0x" << std::hex << parData << std::dec << ")";
switch (parCode) {
case 1:
// ERR_REG1
mFECERRA = ((parData >> 13) & 0x1FFF) << 7;
mFECERRB = ((parData & 0x1FFF)) << 7;
break;
case 2:
// ERR_REG2
mErrorCounter.mErrorRegister2 = parData & 0x1FF;
break;
case 3:
// ERR_REG3
mErrorCounter.mErrorRegister3 = parData & 0x1FFFFFF;
break;
case 4:
// FEC_RO_A
mActiveFECsA = parData & 0xFFFF;
break;
case 5:
// FEC_RO_B
mActiveFECsB = parData & 0xFFFF;
break;
case 6:
// RDO_CFG1
mAltroConfig.mWord1 = parData & 0xFFFFF;
break;
case 7:
// RDO_CFG2
mAltroConfig.mWord2 = parData & 0x1FFFFFF;
break;
default:
std::cerr << "Undefined parameter code " << parCode << ", ignore it !\n";
break;
}
}
mPayloadSize = payloadwords[--index] & 0x3FFFFFF;
mIsInitialized = true;
}
double RCUTrailer::getTimeSampleNS() const
{
uint8_t fq = mAltroConfig.mSampleTime;
double tSample;
switch (fq) {
case 0:
// 20 MHz
tSample = 2.0;
break;
case 1:
// 10 Mhz
tSample = 4.0;
break;
case 2:
// 5 MHz
tSample = 8.;
break;
default:
throw Error(Error::ErrorType_t::SAMPLINGFREQ_INVALID, fmt::format("Invalid sampling frequency value %d !", int(fq)).data());
}
return tSample * o2::constants::lhc::LHCBunchSpacingNS;
}
void RCUTrailer::setTimeSamplePhaseNS(uint64_t triggertime, uint64_t timesample)
{
int sample = 0;
switch (timesample) {
case 50:
sample = 0;
break;
case 100:
sample = 1;
break;
case 200:
sample = 2;
break;
default:
throw Error(Error::ErrorType_t::SAMPLINGFREQ_INVALID, fmt::format("invalid time sample: %f", timesample).data());
};
mAltroConfig.mSampleTime = sample;
// calculate L1 phase
mAltroConfig.mL1Phase = (triggertime % timesample) / 25;
}
double RCUTrailer::getL1PhaseNS() const
{
double tSample = getTimeSampleNS(),
phase = static_cast<double>(mAltroConfig.mL1Phase) * o2::constants::lhc::LHCBunchSpacingNS;
if (phase >= tSample) {
throw Error(Error::ErrorType_t::L1PHASE_INVALID, fmt::format("Invalid L1 trigger phase (%e ns (phase) >= %e ns (sampling time)) !", phase, tSample).data());
}
return phase;
}
std::vector<uint32_t> RCUTrailer::encode() const
{
std::vector<uint32_t> encoded;
encoded.emplace_back(mPayloadSize | 2 << 30);
encoded.emplace_back(mFECERRB >> 7 | (mFECERRA >> 7) << 13 | 1 << 26 | 2 << 30);
encoded.emplace_back(mErrorCounter.mErrorRegister2 | 2 << 26 | 2 << 30);
encoded.emplace_back(mErrorCounter.mErrorRegister3 | 3 << 26 | 2 << 30);
encoded.emplace_back(mActiveFECsA | 4 << 26 | 2 << 30);
encoded.emplace_back(mActiveFECsB | 5 << 26 | 2 << 30);
encoded.emplace_back(mAltroConfig.mWord1 | 6 << 26 | 2 << 30);
encoded.emplace_back(mAltroConfig.mWord2 | 7 << 26 | 2 << 30);
uint32_t lasttrailerword = 3 << 30 | mFirmwareVersion << 16 | mRCUId << 7 | (encoded.size() + 1);
encoded.emplace_back(lasttrailerword);
return encoded;
}
void RCUTrailer::printStream(std::ostream& stream) const
{
std::vector<std::string> errors;
double timesample = -1., l1phase = -1.;
try {
timesample = getTimeSampleNS();
} catch (Error& e) {
errors.push_back(e.what());
}
try {
l1phase = getL1PhaseNS();
} catch (Error& e) {
errors.push_back(e.what());
}
stream << "RCU trailer (Format version 2):\n"
<< "==================================================\n"
<< "RCU ID: " << mRCUId << "\n"
<< "Firmware version: " << int(mFirmwareVersion) << "\n"
<< "Trailer size: " << mTrailerSize << "\n"
<< "Payload size: " << mPayloadSize << "\n"
<< "FECERRA: 0x" << std::hex << mFECERRA << "\n"
<< "FECERRB: 0x" << std::hex << mFECERRB << "\n"
<< "ERRREG2: 0x" << std::hex << mErrorCounter.mErrorRegister2 << "\n"
<< "ERRREG3: 0x" << std::hex << mErrorCounter.mErrorRegister3 << "\n"
<< "#channels skipped due to address mismatch: " << std::dec << getNumberOfChannelAddressMismatch() << "\n"
<< "#channels skipped due to bad block length: " << std::dec << getNumberOfChannelLengthMismatch() << "\n"
<< "Active FECs (branch A): 0x" << std::hex << mActiveFECsA << "\n"
<< "Active FECs (branch B): 0x" << std::hex << mActiveFECsB << "\n"
<< "Baseline corr: " << std::hex << getBaselineCorrection() << "\n"
<< "Polarity: " << (getPolarity() ? "yes" : "no") << "\n"
<< "Number of presamples: " << std::dec << getNumberOfPresamples() << "\n"
<< "Number of postsamples: " << std::dec << getNumberOfPostsamples() << "\n"
<< "Second baseline corr: " << (hasSecondBaselineCorr() ? "yes" : "no") << "\n"
<< "Glitch filter: " << std::dec << getGlitchFilter() << "\n"
<< "Number of non-ZS postsamples: " << std::dec << getNumberOfNonZeroSuppressedPostsamples() << "\n"
<< "Number of non-ZS presamples: " << std::dec << getNumberOfNonZeroSuppressedPresamples() << "\n"
<< "Zero suppression: " << (hasZeroSuppression() ? "yes" : "no") << "\n"
<< "Number of ALTRO buffers: " << std::dec << getNumberOfAltroBuffers() << "\n"
<< "Number of pretrigger samples: " << std::dec << getNumberOfPretriggerSamples() << "\n"
<< "Number of samples per channel: " << std::dec << getNumberOfSamplesPerChannel() << "\n"
<< "Sparse readout: " << (isSparseReadout() ? "yes" : "no") << "\n"
<< "AltroCFG1: 0x" << std::hex << mAltroConfig.mWord1 << "\n"
<< "AltroCFG2: 0x" << std::hex << mAltroConfig.mWord2 << "\n"
<< "Sampling time: " << timesample << " ns\n"
<< "L1 Phase: " << l1phase << " ns\n"
<< std::dec << std::fixed;
if (errors.size()) {
stream << "Errors: \n"
<< "-------------------------------------------------\n";
for (const auto& e : errors) {
stream << e << "\n";
}
}
stream << "==================================================\n";
}
RCUTrailer RCUTrailer::constructFromPayloadWords(const gsl::span<const uint32_t> payloadwords)
{
RCUTrailer result;
result.constructFromRawPayload(payloadwords);
return result;
}
std::ostream& o2::emcal::operator<<(std::ostream& stream, const o2::emcal::RCUTrailer& trailer)
{
trailer.printStream(stream);
return stream;
}