Project
Loading...
Searching...
No Matches
Encoder.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
16
17#include "MIDRaw/Encoder.h"
18
21#include "MIDRaw/GBTMapper.h"
22#include "MIDRaw/Utils.h"
23#include <fmt/format.h>
24
25namespace o2
26{
27namespace mid
28{
29
30void Encoder::init(std::string_view outDir, std::string_view fileFor, int verbosity, std::vector<ROBoardConfig> configurations)
31{
33
34 auto linkUniqueIds = mFEEIdConfig.getConfiguredLinkUniqueIDs();
35
36 // Initialises the GBT link encoders
37 for (auto& linkUniqueId : linkUniqueIds) {
38 auto gbtUniqueId = mFEEIdConfig.getGBTUniqueId(linkUniqueId);
39 std::vector<ROBoardConfig> gbtConfigs;
40 for (auto& cfg : configurations) {
41 if (gbtmapper::isBoardInGBT(cfg.boardId, gbtUniqueId)) {
42 gbtConfigs.emplace_back(cfg);
43 }
44 }
45 mGBTEncoders[gbtUniqueId].setConfig(gbtUniqueId, gbtConfigs);
46 }
47
48 // Initializes the output link
49 auto ir = getOrbitIR(0);
50 mRawWriter.setVerbosity(verbosity);
51 for (uint16_t cruId = 0; cruId < 2; ++cruId) {
52 for (uint8_t epId = 0; epId < 2; ++epId) {
53 uint16_t feeId = 2 * cruId + epId;
54 std::string outFileLink = fmt::format("{}/MID", outDir);
55 if (fileFor != "all") { // single file for all links
56 outFileLink += "_alio2-cr1-flp159";
57 if (fileFor != "flp") {
58 outFileLink += fmt::format("_cru{}_{}", cruId, epId);
59 if (fileFor != "cruendpoint") {
60 outFileLink += fmt::format("_lnk{}_feeid{}", raw::sUserLogicLinkID, feeId);
61 if (fileFor != "link") {
62 throw std::runtime_error("invalid option provided for file grouping");
63 }
64 }
65 }
66 }
67 outFileLink += ".raw";
68 mRawWriter.registerLink(feeId, cruId, raw::sUserLogicLinkID, epId, outFileLink);
69
70 for (auto gbtUniqueId : mFEEIdConfig.getGBTUniqueIdsInLink(feeId)) {
71 // Initializes the trigger response to be added to the empty HBs
72 mGBTEncoders[gbtUniqueId].processTrigger(ir, raw::sORB);
73 mGBTEncoders[gbtUniqueId].flush(mOrbitResponse[feeId], ir);
74 }
75
76 mOrbitResponseWord[feeId] = mOrbitResponse[feeId];
77 completeWord(mOrbitResponseWord[feeId]);
78 }
79 }
80
81 mRawWriter.setEmptyPageCallBack(this);
82}
83
84void Encoder::emptyHBFMethod(const o2::header::RDHAny* rdh, std::vector<char>& toAdd) const
85{
87 toAdd = mOrbitResponseWord[o2::raw::RDHUtils::getFEEID(rdh)];
88}
89
90void Encoder::onOrbitChange(uint32_t orbit)
91{
93 auto ir = getOrbitIR(orbit);
94 for (uint16_t feeId = 0; feeId < 4; ++feeId) {
95 // Write the data corresponding to the previous orbit
96 writePayload(feeId, ir);
97 }
98}
99
100void Encoder::completeWord(std::vector<char>& buffer)
101{
103 size_t dataSize = buffer.size();
104 size_t cruWord = 2 * o2::raw::RDHUtils::GBTWord128;
105 size_t modulo = dataSize % cruWord;
106 if (modulo) {
107 dataSize += cruWord - modulo;
108 buffer.resize(dataSize, static_cast<char>(0));
109 }
110}
111
112void Encoder::writePayload(uint16_t feeId, const InteractionRecord& ir, bool onlyNonEmpty)
113{
115
116 std::vector<char> buf = mOrbitResponse[feeId];
117 for (auto& gbtUniqueId : mFEEIdConfig.getGBTUniqueIdsInLink(feeId)) {
118 if (!mGBTEncoders[gbtUniqueId].isEmpty()) {
119 mGBTEncoders[gbtUniqueId].flush(buf, ir);
120 }
121 }
122 if (onlyNonEmpty && buf.size() == mOrbitResponse[feeId].size()) {
123 return;
124 }
125
126 // Add the orbit response
127 completeWord(buf);
128 mRawWriter.addData(feeId, feeId / 2, raw::sUserLogicLinkID, feeId % 2, ir, buf);
129}
130
131void Encoder::finalize(bool closeFile)
132{
134 initIR();
135 auto ir = getOrbitIR(mLastIR.orbit);
136 auto nextIr = getOrbitIR(mLastIR.orbit + 1);
137 for (uint16_t feeId = 0; feeId < 4; ++feeId) {
138 // Write the last payload
139 writePayload(feeId, ir, true);
140 // Since the regional response comes after few clocks,
141 // we might have the corresponding regional cards in the next orbit.
142 // If this is the case, we flush all data of the next orbit
143 writePayload(feeId, nextIr, true);
144 }
145 if (closeFile) {
146 mRawWriter.close();
147 }
148}
149
150void Encoder::process(gsl::span<const ColumnData> data, InteractionRecord ir, EventType eventType)
151{
153
154 // The CTP trigger arrives to the electronics with a delay
155 if (ir.differenceInBC(mRawWriter.getHBFUtils().getFirstSampledTFIR()) < mElectronicsDelay.localToBC) {
156 // Due to the delay, these data would arrive in the TF before the first sampled one.
157 // We therefore reject them.
158 return;
159 }
160 applyElectronicsDelay(ir.orbit, ir.bc, -mElectronicsDelay.localToBC);
161
162 initIR();
163
164 if (ir.orbit != mLastIR.orbit) {
165 onOrbitChange(mLastIR.orbit);
166 }
167
168 // Converts ColumnData to ROBoards
169 mConverter.process(data);
170
171 mGBTMap.clear();
172
173 // Group local boards according to the GBT link they belong
174 for (auto& item : mConverter.getDataMap()) {
175 auto feeId = gbtmapper::getGBTIdFromUniqueLocId(item.first);
176 mGBTMap[feeId].emplace_back(item.second);
177 }
178
179 // Process the GBT links
180 for (auto& item : mGBTMap) {
181 mGBTEncoders[item.first].process(item.second, ir);
182 }
183 mLastIR = ir;
184}
185
186void Encoder::initIR()
187{
188 if (mLastIR.isDummy()) {
189 mLastIR = mRawWriter.getHBFUtils().getFirstSampledTFIR();
190 }
191}
192
193} // namespace mid
194} // namespace o2
#define verbosity
MID raw data encoder.
Raw data utilities for MID.
uint64_t orbit
Definition RawEventData.h:6
MID GBT mapping.
const std::unordered_map< uint8_t, ROBoard > getDataMap() const
Gets the output data as a map.
void process(gsl::span< const ColumnData > data, bool allowEmpty=false)
void finalize(bool closeFile=true)
Definition Encoder.cxx:131
void init(std::string_view outDir=".", std::string_view fileFor="all", int verbosity=0, std::vector< ROBoardConfig > configurations=makeDefaultROBoardConfig())
Definition Encoder.cxx:30
void emptyHBFMethod(const o2::header::RDHAny *rdh, std::vector< char > &toAdd) const
Definition Encoder.cxx:84
void process(gsl::span< const ColumnData > data, InteractionRecord ir, EventType eventType=EventType::Standard)
Definition Encoder.cxx:150
std::vector< uint32_t > getConfiguredLinkUniqueIDs() const
const std::vector< uint16_t > & getGBTUniqueIdsInLink(uint16_t feeId) const
Definition FEEIdConfig.h:36
uint16_t getGBTUniqueId(uint32_t linkUniqueId) const
void addData(uint16_t feeid, uint16_t cru, uint8_t lnk, uint8_t endpoint, const IR &ir, const gsl::span< char > data, bool preformatted=false, uint32_t trigger=0, uint32_t detField=0)
LinkData & registerLink(uint16_t fee, uint16_t cru, uint8_t link, uint8_t endpoint, std::string_view outFileName)
void setEmptyPageCallBack(const T *t)
const HBFUtils & getHBFUtils() const
GLuint buffer
Definition glcorearb.h:655
GLenum GLsizei dataSize
Definition glcorearb.h:3994
GLboolean * data
Definition glcorearb.h:298
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
uint16_t getGBTIdFromUniqueLocId(uint8_t uniqueLocId)
Definition GBTMapper.h:32
bool isBoardInGBT(uint8_t uniqueLocId, uint16_t gbtUniqueId)
Definition GBTMapper.h:37
void applyElectronicsDelay(uint32_t &orbit, uint16_t &bc, int16_t delay, uint16_t maxBunches=constants::lhc::LHCMaxBunches)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
uint32_t orbit
LHC orbit.
uint16_t bc
bunch crossing ID of interaction
int64_t differenceInBC(const InteractionRecord &other) const
int16_t localToBC
Delay between collision BC and local clock.
IR getFirstSampledTFIR() const
get TF and HB (abs) for this IR
Definition HBFUtils.h:74
static constexpr int GBTWord128
Definition RDHUtils.h:52
o2::InteractionRecord ir(0, 0)