Project
Loading...
Searching...
No Matches
RawFileReader.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
18
19#include <iostream>
21#include "Headers/RDHAny.h"
22#include "DPLUtils/RawParser.h"
24
25namespace o2
26{
27namespace mid
28{
29bool RawFileReader::init(const char* inFilename, bool readContinuous)
30{
32 mFile.open(inFilename, std::ios::binary);
33 if (!mFile.is_open()) {
34 std::cerr << "Cannot open the " << inFilename << " file !";
35 mState = 2;
36 return false;
37 }
38 mReadContinuous = readContinuous;
39 return true;
40}
41
42void RawFileReader::read(size_t nBytes)
43{
45 size_t currentIndex = mBytes.size();
46 mBytes.resize(currentIndex + nBytes);
47 mFile.read(reinterpret_cast<char*>(&(mBytes[currentIndex])), nBytes);
48}
49
51{
53 mBytes.clear();
54}
55
56void RawFileReader::setCustomPayloadSize(uint16_t memorySize, uint16_t offsetToNext)
57{
62 rdh.word1 |= offsetToNext;
63 rdh.word1 |= (memorySize << 16);
64 setCustomRDH(rdh);
65}
66
67bool RawFileReader::replaceRDH(size_t headerIndex)
68{
72 if (o2::raw::RDHUtils::getOffsetToNext(mCustomRDH) > 0) {
73 header::RAWDataHeader* rdh = reinterpret_cast<header::RAWDataHeader*>(&mBytes[headerIndex]);
74 *rdh = mCustomRDH;
75 return true;
76 }
77 return false;
78}
79
80bool RawFileReader::readHB(bool sendCompleteHBs)
81{
83 if (mState != 0) {
84 return false;
85 }
86 bool isHBClosed = false;
87 while (!isHBClosed) {
88 // Read header
89 size_t headerIndex = mBytes.size();
90 read(sHeaderSize);
91
92 // The check on the eof needs to be placed here and not at the beginning of the function.
93 // The reason is that the eof flag is set if we try to read after the eof
94 // But, since we know the size, we read up to the last character.
95 // So we turn on the eof flag only if we try to read past the last data.
96 // Of course, we resized the mBytes before trying to read.
97 // Since we read 0, we need to remove the last bytes
98 if (mFile.eof()) {
99 mBytes.resize(headerIndex);
100 if (mReadContinuous) {
101 mFile.clear();
102 mFile.seekg(0, std::ios::beg);
103 read(sHeaderSize);
104 } else {
105 mState = 1;
106 return false;
107 }
108 }
109 replaceRDH(headerIndex);
110 // We use the buffer only to correctly initialize the RDH
111 o2::framework::RawParser parser(mBytes.data(), mBytes.size());
112 auto lastIt = parser.begin();
113 for (auto it = parser.begin(), end = parser.end(); it != end; ++it) {
114 lastIt = it;
115 }
116 auto const* rdhPtr = reinterpret_cast<const o2::header::RDHAny*>(lastIt.raw());
117 isHBClosed = o2::raw::RDHUtils::getStop(rdhPtr);
118 auto offsetNext = o2::raw::RDHUtils::getOffsetToNext(rdhPtr);
119 if (offsetNext > sHeaderSize) {
120 read(offsetNext - sHeaderSize);
121 }
122 if (!sendCompleteHBs) {
123 break;
124 }
125 }
126 return true;
127}
128
129} // namespace mid
130} // namespace o2
MID raw file reader.
Definition of the RAW Data Header.
Generic parser for consecutive raw pages.
const_iterator begin() const
Definition RawParser.h:618
const_iterator end() const
Definition RawParser.h:623
void setCustomPayloadSize(uint16_t memorySize=0x2000, uint16_t offsetToNext=0x2000)
bool readHB(bool sendCompleteHBs=false)
void setCustomRDH(const header::RAWDataHeader &rdh)
GLuint GLuint end
Definition glcorearb.h:469
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...