Project
Loading...
Searching...
No Matches
HmpidCoder2.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
18
19#include <vector>
20#include <iostream>
21#include <memory>
22
28
31
32using namespace o2::raw;
33using namespace o2::hmpid;
34using namespace o2::header;
35
38HmpidCoder2::HmpidCoder2(int numOfEquipments)
39{
40 mPadsCoded = 0;
41 mNumberOfEquipments = numOfEquipments;
42 mVerbose = 0;
43 mSkipEmptyEvents = true;
44 mPayloadBufferDimPerEquipment = ((Geo::N_SEGMENTS * (Geo::N_COLXSEGMENT * (Geo::N_DILOGICS * (Geo::N_CHANNELS + 1) + 1) + 1)) + 10);
45 mUPayloadBufferPtr = std::make_unique<uint32_t[]>(mNumberOfEquipments * mPayloadBufferDimPerEquipment);
46 mUPadMap = std::make_unique<uint32_t[]>(Geo::N_HMPIDTOTALPADS);
47 mPayloadBufferPtr = mUPayloadBufferPtr.get();
48 mPadMap = mUPadMap.get();
49 std::memset(mPadMap, 0, sizeof(uint32_t) * Geo::N_HMPIDTOTALPADS); // Zero the map for the first event
50 mBusyTime = 20000; // 1 milli sec
51 mHmpidErrorFlag = 0;
52 mHmpidFrwVersion = 9;
53}
54
60void HmpidCoder2::setDetectorSpecificFields(float BusyTime, int Error, int Version)
61{
62 uint32_t busy = (uint32_t)(BusyTime / 0.00000005);
63 mBusyTime = busy;
64 mHmpidErrorFlag = Error;
65 mHmpidFrwVersion = Version;
66 return;
67}
68
72void HmpidCoder2::setRDHFields(int eq)
73{
74 int st, en;
75 uint32_t wr = (mBusyTime << 9) | ((mHmpidErrorFlag & 0x01F) << 4) | (mHmpidFrwVersion & 0x0F);
76 st = (eq < 0 || eq >= Geo::MAXEQUIPMENTS) ? 0 : eq;
77 en = (eq < 0 || eq >= Geo::MAXEQUIPMENTS) ? Geo::MAXEQUIPMENTS : eq + 1;
78 for (int l = st; l < en; l++) {
80 RDHAny* RDHptr = link.getLastRDH();
81 if (RDHptr != nullptr) {
83 }
84 }
85 return;
86}
87
90constexpr int p2() { return (Geo::N_DILOGICS * Geo::N_CHANNELS); }
91
99int HmpidCoder2::getEquipmentPadIndex(int eq, int col, int dil, int cha)
100{
101 return (eq * p1() + col * p2() + dil * Geo::N_CHANNELS + cha);
102}
103
107void HmpidCoder2::fillTheOutputBuffer(uint32_t* padMap)
108{
109 uint32_t rowMarker, segMarker, eoeMarker, padWord;
110 uint32_t rowSize;
111 uint32_t ptr = 0;
112 int pads[Geo::MAXEQUIPMENTS];
113 int padsCount;
114 int segSize;
115
116 for (int i = 0; i < Geo::MAXEQUIPMENTS; i++) {
117 mEventSizePerEquipment[i] = 0;
118 }
119
120 for (int eq = 0; eq < mNumberOfEquipments; eq++) {
121 int startPtr = ptr;
122 padsCount = 0;
123 for (int s = 1; s <= Geo::N_SEGMENTS; s++) {
124 segSize = 0;
125 for (int c = 1; c <= Geo::N_COLXSEGMENT; c++) {
126 // ---- Pre-calculate the size of each column
127 for (int j = 0; j < Geo::N_DILOGICS; j++) {
128 pads[j] = 0;
129 }
130 rowSize = 0;
131 for (int j = 0; j < Geo::N_DILOGICS; j++) {
132 for (int k = 0; k < Geo::N_CHANNELS; k++) {
133 int idx = getEquipmentPadIndex(eq, ((s - 1) * Geo::N_COLXSEGMENT + (c - 1)), j, k);
134 if (padMap[idx] > 0) {
135 pads[j]++;
136 rowSize++;
137 padsCount++;
138 }
139 }
140 }
141 rowSize += Geo::N_DILOGICS;
142 segSize += (rowSize + 1);
143 rowMarker = 0x000036A8 | ((rowSize << 16) & 0x03ff0000);
144
145 // ---- fills the Payload Buffer
146 mPayloadBufferPtr[ptr++] = rowMarker;
147 int col = (s - 1) * Geo::N_COLXSEGMENT + c;
148 for (int d = 1; d <= Geo::N_DILOGICS; d++) {
149 for (int p = 0; p < Geo::N_CHANNELS; p++) {
150 int idx = getEquipmentPadIndex(eq, ((s - 1) * Geo::N_COLXSEGMENT + (c - 1)), (d - 1), p);
151 if (padMap[idx] > 0) {
152 padWord = ((col << 22) & 0x07c00000) | ((d << 18) & 0x003C0000) | ((p << 12) & 0x0003F000) | (padMap[idx] & 0x00000FFF);
153 mPayloadBufferPtr[ptr++] = padWord;
154 }
155 }
156 eoeMarker = 0x08000080 | ((col << 22) & 0x07c00000) | (d << 18 & 0x003C0000) | (pads[d - 1] & 0x0000007F);
157 mPayloadBufferPtr[ptr++] = eoeMarker;
158 }
159 }
160 segSize += 1;
161 segMarker = 0xAB000000 | ((segSize << 8) & 0x000fff00) | (s & 0x0000000F);
162 mPayloadBufferPtr[ptr++] = segMarker;
163 }
164 mPadsCoded += padsCount;
165 mEventPadsPerEquipment[eq] = padsCount;
166 mEventSizePerEquipment[eq] = ptr - startPtr;
167 }
168 return;
169}
170
177void HmpidCoder2::writePaginatedEvent(uint32_t orbit, uint16_t bc)
178{
179 uint32_t* ptrStartEquipment = mPayloadBufferPtr;
180 for (int eq = 0; eq < mNumberOfEquipments; eq++) {
181 int EventSize = mEventSizePerEquipment[eq];
182 LOG(debug) << "writePaginatedEvent() Eq=" << eq << " Size:" << EventSize << " Pads:" << mEventPadsPerEquipment[eq] << " Orbit:" << orbit << " BC:" << bc;
183 if (mEventPadsPerEquipment[eq] > 0 || !mSkipEmptyEvents) { // Skips the Events with 0 Pads
185 ReadOut::CruId(eq),
186 ReadOut::LnkId(eq),
187 0,
188 {bc, orbit},
189 gsl::span<char>(reinterpret_cast<char*>(ptrStartEquipment),
190 EventSize * sizeof(uint32_t)),
191 false,
192 0,
193 (uint32_t)((mBusyTime << 9) | ((mHmpidErrorFlag & 0x01F) << 4) | (mHmpidFrwVersion & 0x0F)));
194 // We fill the fields !
195 // TODO: we can fill the detector field with Simulated Data
196 setDetectorSpecificFields(0.000001 * EventSize);
197 setRDHFields(eq);
198 }
199 ptrStartEquipment += EventSize;
200 }
201 return;
202}
203
212{
213 int eq, col, dil, cha, mo, x, y, idx;
214 uint32_t orbit = ir.orbit;
215 uint16_t bc = ir.bc;
216
217 int padsCount = 0;
218 LOG(debug) << "Manage chunk Orbit :" << orbit << " BC:" << bc << " Digits size:" << digits.size();
219 for (o2::hmpid::Digit d : digits) {
220 Digit::pad2Equipment(d.getPadID(), &eq, &col, &dil, &cha); // From Digit to Hardware coords
221 eq = ReadOut::FeeId(eq); // converts the Equipment Id in Cru/Link position ref
222 idx = getEquipmentPadIndex(eq, col, dil, cha); // finally to the unique padmap index
223 if (mPadMap[idx] != 0) { // We already have the pad set
224 LOG(warning) << "Duplicated DIGIT =" << d << " (" << eq << "," << col << "," << dil << "," << cha << ")" << idx;
225 } else {
226 mPadMap[idx] = d.getCharge();
227 padsCount++;
228 }
229 }
230 fillTheOutputBuffer(mPadMap); // Fill the Buffer for all Equipments per Event
231 writePaginatedEvent(orbit, bc);
232 std::memset(mPadMap, 0, sizeof(uint32_t) * Geo::N_HMPIDTOTALPADS); // Update for the new event
233 return;
234}
235
242void HmpidCoder2::openOutputStream(const std::string& outputFileName, const std::string& fileFor)
243{
244 RAWDataHeader rdh; // by default, v6 is used currently.
245 for (int eq = 0; eq < mNumberOfEquipments; eq++) {
246 rdh.feeId = ReadOut::FeeId(eq);
247 rdh.cruID = ReadOut::CruId(eq);
248 rdh.linkID = ReadOut::LnkId(eq);
249 rdh.endPointID = 0;
250 std::string outfname;
251 if (fileFor == "link") {
252 outfname = fmt::format("{}_{}_feeid{}.raw", outputFileName, ReadOut::FlpHostName(eq), int(rdh.feeId));
253 } else if (fileFor == "flp") {
254 outfname = fmt::format("{}_{}.raw", outputFileName, ReadOut::FlpHostName(eq));
255 } else if (fileFor == "all") {
256 outfname = fmt::format("{}.raw", outputFileName);
257 } else if (fileFor == "crorcendpoint") {
258 outfname = fmt::format("{}_{}_crorc{}_{}.raw", outputFileName, ReadOut::FlpHostName(eq), int(rdh.cruID), int(rdh.linkID));
259 } else {
260 throw std::runtime_error(fmt::format("unknown raw file grouping option {}", fileFor));
261 }
262
263 mWriter.registerLink(rdh, outfname); // register the link
265 mTheRFWLinks[eq] = ap; // Store the RawFileWriter Link ID
266 }
267 return;
268}
269
272{
273 mWriter.close();
274 return;
275}
276
278void HmpidCoder2::dumpResults(const std::string& outputFileName)
279{
280 std::cout << " **** HMPID RawFile Coder : results ****" << std::endl;
281 std::cout << " Created files : " << outputFileName << std::endl;
282 std::cout << " Number of Pads coded : " << mPadsCoded << std::endl;
283 std::cout << " ----------------------------------------" << std::endl;
284}
uint64_t orbit
Definition RawEventData.h:6
uint64_t bc
Definition RawEventData.h:5
int32_t i
Header of the General Run Parameters object.
constexpr int p2()
constexpr int p1()
constexpr to accelerate the coordinates changing
Definition of the RAW Data Header.
uint32_t j
Definition RawData.h:0
uint32_t col
Definition RawData.h:4
uint32_t c
Definition RawData.h:2
Utility class to write detectors data to (multiple) raw data file(s) respecting CRU format.
TBranch * ptr
std::ostringstream debug
benchmark::State & st
HMPID Digit declaration.
Definition Digit.h:36
static void pad2Equipment(uint32_t pad, int *Equi, int *Colu, int *Dilo, int *Chan)
Definition Digit.cxx:141
static constexpr int N_HMPIDTOTALPADS
Definition Geo.h:101
static constexpr int N_COLXSEGMENT
Definition Geo.h:81
static constexpr int N_SEGMENTS
Definition Geo.h:80
static constexpr int N_DILOGICS
Definition Geo.h:83
static constexpr int N_CHANNELS
Definition Geo.h:84
static constexpr int MAXEQUIPMENTS
Definition Geo.h:79
void codeEventChunkDigits(std::vector< o2::hmpid::Digit > &digits, InteractionRecord ir)
void dumpResults(const std::string &outputFileName)
Dumps the results of the last coding.
RawFileWriter mWriter
Definition HmpidCoder2.h:63
void setDetectorSpecificFields(float BusyTime=0.001, int Error=0, int Version=9)
void openOutputStream(const std::string &outputFileName, const std::string &fileFor)
HmpidCoder2(int numOfEquipments)
void closeOutputStream()
Close and flush the output streams.
static std::string_view FlpHostName(unsigned int idx)
Definition Geo.h:148
static int FeeId(unsigned int idx)
Definition Geo.h:144
static int CruId(unsigned int idx)
Definition Geo.h:145
static int LnkId(unsigned int idx)
Definition Geo.h:146
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)
LinkData & getLinkWithSubSpec(LinkSubSpec_t ss)
GLint GLenum GLint x
Definition glcorearb.h:403
GLint y
Definition glcorearb.h:270
O2 data header classes and API, v0.1.
Definition DetID.h:49
uint32_t LinkSubSpec_t
Definition RDHUtils.h:34
uint32_t orbit
LHC orbit.
uint16_t bc
bunch crossing ID of interaction
uint32_t endPointID
bit 112 to 123: CRU ID
uint32_t linkID
bit 80 to 95: memory size
uint16_t cruID
bit 104 to 111: packet counter
uint64_t feeId
bit 8 to 15: header size
static LinkSubSpec_t getSubSpec(uint16_t cru, uint8_t link, uint8_t endpoint, uint16_t feeId, o2::header::DAQID::ID srcid=o2::header::DAQID::INVALID)
Definition RDHUtils.h:685
static void setDetectorField(H &rdh, uint32_t v, NOTPTR(H))
Definition RDHUtils.h:582
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
o2::InteractionRecord ir(0, 0)
std::vector< Digit > digits