-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDecoder.cxx
More file actions
422 lines (345 loc) · 12.5 KB
/
Decoder.cxx
File metadata and controls
422 lines (345 loc) · 12.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// 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 "TOFReconstruction/Decoder.h"
#include <iostream>
#include <chrono>
#include <algorithm>
#include "CommonConstants/LHCConstants.h"
#include "TString.h"
#include <fairlogger/Logger.h>
#include "DetectorsRaw/RDHUtils.h"
//#define VERBOSE
namespace o2
{
namespace tof
{
namespace compressed
{
using RDHUtils = o2::raw::RDHUtils;
Decoder::Decoder()
{
for (int i = 0; i < NCRU; i++) {
mIntegratedBytes[i] = 0.;
mBuffer[i] = nullptr;
mCruIn[i] = false;
}
clearCounts();
}
bool Decoder::open(const std::string name)
{
int nfileopened = NCRU;
int fullsize = 0;
for (int i = 0; i < NCRU; i++) {
std::string nametmp;
nametmp.append(Form("cru%02d", i));
nametmp.append(name);
if (mFile[i].is_open()) {
std::cout << "Warning: a file (" << i << ") was already open, closing" << std::endl;
mFile[i].close();
}
mFile[i].open(nametmp.c_str(), std::fstream::in | std::fstream::binary);
if (!mFile[i].is_open()) {
std::cout << "Cannot open " << nametmp << std::endl;
nfileopened--;
mSize[i] = 0;
mCruIn[i] = false;
} else {
mFile[i].seekg(0, mFile[i].end);
mSize[i] = mFile[i].tellg();
mFile[i].seekg(0);
fullsize += mSize[i];
mCruIn[i] = true;
}
}
if (!nfileopened) {
std::cerr << "No streams available" << std::endl;
return true;
}
// mBufferLocal.resize(fullsize);
printf("Full input buffer size = %d byte\n", fullsize);
char* pos = new char[fullsize]; //mBufferLocal.data();
for (int i = 0; i < NCRU; i++) {
if (!mCruIn[i]) {
continue;
}
mBuffer[i] = pos;
// read content of infile
mFile[i].read(mBuffer[i], mSize[i]);
mUnion[i] = reinterpret_cast<Union_t*>(mBuffer[i]);
mUnionEnd[i] = reinterpret_cast<Union_t*>(mBuffer[i] + mSize[i] - 1);
pos += mSize[i];
}
printf("Number of TOF compressed streamers = %d/%d\n", nfileopened, NCRU);
close();
return false;
}
bool Decoder::close()
{
for (int i = 0; i < NCRU; i++) {
if (mFile[i].is_open()) {
mFile[i].close();
}
}
return false;
}
void Decoder::clear()
{
reset();
clearCounts();
mPatterns.clear();
mCratePatterns.clear();
mCrateHeaderData.clear();
mErrors.clear();
mDiagnosticFrequency.clear();
}
void Decoder::InsertDigit(int icrate, int itrm, int itdc, int ichain, int channel, uint32_t orbit, uint16_t bunchid, int time_ext, int tdc, int tot)
{
DigitInfo digitInfo;
if (itrm == 3 && ((icrate % 2 == 0) || (itdc / 3 > 0))) { // Signal not coming from a TOF pad but -probably- from a TOF OR signal
// add operation on LTM (Trigger) if needed (not used) if (crate % 2 == 1)
return;
}
fromRawHit2Digit(icrate, itrm, itdc, ichain, channel, orbit, bunchid, time_ext + tdc, tot, digitInfo);
if (digitInfo.channel < 0) { // check needed for the moment. To be removed once debugged
return;
}
mChannelCounts[digitInfo.channel]++;
mHitDecoded++;
uint64_t isnext = digitInfo.bcAbs * Geo::BC_IN_WINDOW_INV;
if (isnext >= uint64_t(MAXWINDOWS)) { // accumulate all digits which are not in the first windows
insertDigitInFuture(digitInfo.channel, digitInfo.tdc, digitInfo.tot, digitInfo.bcAbs, 0, digitInfo.orbit, digitInfo.bc);
} else {
std::vector<Strip>* cstrip = mStripsCurrent; // first window
if (isnext) {
cstrip = mStripsNext[isnext - 1]; // next window
}
UInt_t istrip = digitInfo.channel / Geo::NPADS;
// add digit
fillDigitsInStrip(cstrip, digitInfo.channel, digitInfo.tdc, digitInfo.tot, digitInfo.bcAbs, istrip);
}
}
void Decoder::readTRM(int icru, int icrate, uint32_t orbit, uint16_t bunchid)
{
if (orbit < mFirstIR.orbit || (orbit == mFirstIR.orbit && bunchid < mFirstIR.bc)) {
mFirstIR.orbit = orbit;
mFirstIR.bc = bunchid;
}
if (mVerbose) {
printTRMInfo(icru);
}
int nhits = mUnion[icru]->frameHeader.numberOfHits;
int time_ext = mUnion[icru]->frameHeader.frameID << 13;
int itrm = mUnion[icru]->frameHeader.trmID;
int deltaBC = mUnion[icru]->frameHeader.deltaBC;
if (deltaBC != 0) {
printf("DeltaBC = %d\n", deltaBC);
}
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
DigitInfo digitInfo;
for (int i = 0; i < nhits; i++) {
if (icrate % 2 == 1 && itrm == 3 && mUnion[icru]->packedHit.tdcID / 3 > 0) { // Signal not coming from a TOF pad but -probably- from a TOF OR signal
// add operation on LTM (Trigger) if needed (not used)
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
continue;
}
fromRawHit2Digit(icrate, itrm, mUnion[icru]->packedHit.tdcID, mUnion[icru]->packedHit.chain, mUnion[icru]->packedHit.channel, orbit, bunchid,
time_ext + mUnion[icru]->packedHit.time, mUnion[icru]->packedHit.tot, digitInfo);
if (digitInfo.channel < 0) { // check needed for the moment. To be removed once debugged
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
continue;
}
mChannelCounts[digitInfo.channel]++;
mHitDecoded++;
if (mVerbose) {
printHitInfo(icru);
}
uint64_t isnext = digitInfo.bcAbs * Geo::BC_IN_WINDOW_INV;
if (isnext >= MAXWINDOWS) { // accumulate all digits which are not in the first windows
insertDigitInFuture(digitInfo.channel, digitInfo.tdc, digitInfo.tot, digitInfo.bcAbs, 0, digitInfo.orbit, digitInfo.bc);
} else {
std::vector<Strip>* cstrip = mStripsCurrent; // first window
if (isnext) {
cstrip = mStripsNext[isnext - 1]; // next window
}
UInt_t istrip = digitInfo.channel / Geo::NPADS;
// add digit
fillDigitsInStrip(cstrip, digitInfo.channel, digitInfo.tdc, digitInfo.tot, digitInfo.bcAbs, istrip);
}
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
}
}
void Decoder::fromRawHit2Digit(int icrate, int itrm, int itdc, int ichain, int channel, uint32_t orbit, uint16_t bunchid, int tdc, int tot, Decoder::DigitInfo& dinfo)
{
// convert raw info in digit info (channel, tdc, tot, bc)
// tdc = packetHit.time + (frameHeader.frameID << 13)
int echannel = Geo::getECHFromIndexes(icrate, itrm, ichain, itdc, channel);
dinfo.channel = Geo::getCHFromECH(echannel);
if (dinfo.channel < 0) { // it should not happen!
LOG(debug) << "No valid channel for icrate = " << icrate << ", itrm = " << itrm << ", ichain = " << ichain << ", itdc = " << itdc << ", channel = " << channel;
}
dinfo.tot = tot;
dinfo.bcAbs = uint64_t(orbit) * o2::tof::Geo::BC_IN_ORBIT + bunchid + tdc / 1024;
dinfo.tdc = tdc % 1024;
dinfo.orbit = orbit;
dinfo.bc = bunchid;
}
char* Decoder::nextPage(void* current, int shift)
{
char* point = reinterpret_cast<char*>(current);
point += shift;
return point;
}
bool Decoder::decode() // return a vector of digits in a TOF readout window
{
mReadoutWindowCurrent = 0;
mFirstIR.orbit = 0;
mFirstIR.bc = 0;
#ifdef VERBOSE
if (mVerbose)
std::cout << "-------- START DECODE EVENTS IN THE HB ----------------------------------------" << std::endl;
#endif
auto start = std::chrono::high_resolution_clock::now();
// start from the beginning of the timeframe
// loop over CRUs
for (int icru = 0; icru < NCRU; icru++) {
if (!mCruIn[icru]) {
continue; // no data stream available for this cru
}
printf("decoding cru %d\n", icru);
while (mUnion[icru] < mUnionEnd[icru]) { // read all the buffer
// read open RDH
mRDH = reinterpret_cast<o2::header::RAWDataHeader*>(mUnion[icru]);
if (mVerbose) {
printRDH();
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// note that RDH continue is not yet considered as option (to be added)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// move after RDH
char* shift = reinterpret_cast<char*>(mRDH);
auto rdhsz = RDHUtils::getHeaderSize(*mRDH);
mUnion[icru] = reinterpret_cast<Union_t*>(shift + rdhsz);
mIntegratedBytes[icru] += rdhsz;
if (mUnion[icru] >= mUnionEnd[icru]) {
continue; // end of data stream reac
}
for (int window = 0; window < Geo::NWINDOW_IN_ORBIT; window++) {
// read Crate Header
int bunchid = mUnion[icru]->crateHeader.bunchID;
int icrate = mUnion[icru]->crateHeader.drmID;
if (mVerbose) {
printCrateInfo(icru);
}
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
//read Orbit
int orbit = mUnion[icru]->crateOrbit.orbitID;
if (mVerbose) {
printf("%d) orbit ID = %d -- bunch ID = %d\n", icrate, orbit, bunchid);
}
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
while (!mUnion[icru]->frameHeader.mustBeZero) {
readTRM(icru, icrate, orbit, bunchid);
}
// read Crate Trailer
if (mVerbose) {
printCrateTrailerInfo(icru);
}
auto ndw = mUnion[icru]->crateTrailer.numberOfDiagnostics;
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
// loop over number of diagnostic words
for (int idw = 0; idw < ndw; ++idw) {
mUnion[icru]++;
mIntegratedBytes[icru] += 4;
}
}
// read close RDH
mRDH = reinterpret_cast<o2::header::RAWDataHeader*>(nextPage(mRDH, RDHUtils::getMemorySize(*mRDH)));
if (mVerbose) {
printRDH();
}
mIntegratedBytes[icru] += RDHUtils::getHeaderSize(*mRDH);
// go to next page
mUnion[icru] = reinterpret_cast<Union_t*>(nextPage(mRDH, RDHUtils::getMemorySize(*mRDH)));
}
}
// since digits are not yet divided in tof readout window we need to do it
// flushOutputContainer does the job
fillWindows();
fillDiagnosticFrequency();
return false;
}
void Decoder::fillWindows()
{
std::vector<Digit> digTemp;
flushOutputContainer(digTemp);
}
void Decoder::printCrateInfo(int icru) const
{
printf("___CRATE HEADER____\n");
printf("DRM ID = %d\n", mUnion[icru]->crateHeader.drmID);
printf("Bunch ID = %d\n", mUnion[icru]->crateHeader.bunchID);
printf("Slot part. mask = %d\n", mUnion[icru]->crateHeader.slotPartMask);
printf("Must be ONE = %d\n", mUnion[icru]->crateHeader.mustBeOne);
printf("___________________\n");
}
void Decoder::printCrateTrailerInfo(int icru) const
{
printf("___CRATE TRAILER___\n");
printf("Event counter = %d\n", mUnion[icru]->crateTrailer.eventCounter);
printf("Number of diagnostics = %d\n", mUnion[icru]->crateTrailer.numberOfDiagnostics);
printf("Must be ONE = %d\n", mUnion[icru]->crateTrailer.mustBeOne);
printf("___________________\n");
}
void Decoder::printTRMInfo(int icru) const
{
printf("______TRM_INFO_____\n");
printf("TRM ID = %d\n", mUnion[icru]->frameHeader.trmID);
printf("Frame ID = %d\n", mUnion[icru]->frameHeader.frameID);
printf("N. hits = %d\n", mUnion[icru]->frameHeader.numberOfHits);
printf("DeltaBC = %d\n", mUnion[icru]->frameHeader.deltaBC);
printf("Must be Zero = %d\n", mUnion[icru]->frameHeader.mustBeZero);
printf("___________________\n");
}
void Decoder::printHitInfo(int icru) const
{
printf("______HIT_INFO_____\n");
printf("TDC ID = %d\n", mUnion[icru]->packedHit.tdcID);
printf("CHAIN ID = %d\n", mUnion[icru]->packedHit.chain);
printf("CHANNEL ID = %d\n", mUnion[icru]->packedHit.channel);
printf("TIME = %d\n", mUnion[icru]->packedHit.time);
printf("TOT = %d\n", mUnion[icru]->packedHit.tot);
printf("___________________\n");
}
void Decoder::printRDH() const
{
printf("______RDH_INFO_____\n");
int v = RDHUtils::getVersion(*mRDH);
printf("VERSION = %d\n", v);
if (v == 4) {
printf("BLOCK LENGTH = %d\n", int(RDHUtils::getBlockLength(mRDH)));
}
printf("HEADER SIZE = %d\n", int(RDHUtils::getHeaderSize(*mRDH)));
printf("MEMORY SIZE = %d\n", int(RDHUtils::getMemorySize(*mRDH)));
printf("PACKET COUNTER= %d\n", int(RDHUtils::getPacketCounter(*mRDH)));
printf("CRU ID = %d\n", int(RDHUtils::getCRUID(*mRDH)));
printf("LINK ID = %d\n", int(RDHUtils::getLinkID(*mRDH)));
printf("___________________\n");
}
} // namespace compressed
} // namespace tof
} // namespace o2