Project
Loading...
Searching...
No Matches
SubTimeFrameFileReader.h
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
12// Adapthed with minimal changes from Gvozden Nescovic code to read sTFs files created by DataDistribution
13
14#ifndef ALICEO2_SUBTIMEFRAME_FILE_READER_RAWDD_H_
15#define ALICEO2_SUBTIMEFRAME_FILE_READER_RAWDD_H_
16
18#include <Headers/DataHeader.h>
19#include <Headers/STFHeader.h>
21#include <Headers/Stack.h>
22#include <fairmq/Parts.h>
23#include <fairmq/Device.h>
25#include <boost/filesystem.hpp>
26#include <boost/iostreams/device/mapped_file.hpp>
27#include <fstream>
28#include <vector>
29#include <unordered_map>
30
31namespace o2f = o2::framework;
32
33namespace o2
34{
35
36namespace rawdd
37{
38
42using MessagesPerRoute = std::unordered_map<std::string, std::unique_ptr<fair::mq::Parts>>;
43
45{
46 public:
47
49 SubTimeFrameFileReader(const std::string& pFileName, o2::detectors::DetID::mask_t detMask, int verb, bool sup0xccdb, bool repaireHeaders, bool rejectDistSTF);
51
53 std::unique_ptr<MessagesPerRoute> read(fair::mq::Device* device, const std::vector<o2f::OutputRoute>& outputRoutes, const std::string& rawChannel, size_t slice);
54
56 inline std::uint64_t position() const { return mFileMapOffset; }
57
59 inline void set_position(std::uint64_t pPos)
60 {
61 const std::uint64_t lPos = std::min(pPos, mFileSize);
62 assert(pPos == lPos);
63 mFileMapOffset = lPos;
64 }
65
67 inline bool eof() const { return mFileMapOffset == mFileSize; }
68
70 inline std::uint64_t size() const { return mFileSize; }
71
72 private:
73 std::string mFileName;
74 std::unordered_map<o2::header::DataOrigin, bool> mDetOrigMap;
75 boost::iostreams::mapped_file_source mFileMap;
76 std::uint64_t mFileMapOffset = 0;
77 std::uint64_t mFileSize = 0;
78
79 int mVerbosity = 0;
80 bool mSup0xccdb = true;
81 bool mRepaireHeaders = true;
82 bool mRejectDistSTF = true;
83
84 const std::string describeHeader(const o2::header::DataHeader& hd, bool full = false) const;
85
86 // helper to make sure written chunks are buffered, only allow pointers
87 template <typename pointer,
88 typename = std::enable_if_t<std::is_pointer<pointer>::value>>
89 bool read_advance(pointer pPtr, std::uint64_t pLen)
90 {
91 if (!mFileMap.is_open()) {
92 return false;
93 }
94
95 assert(mFileMapOffset <= mFileSize);
96 const std::uint64_t lToRead = std::min(pLen, mFileSize - mFileMapOffset);
97
98 if (lToRead != pLen) {
99 LOGP(error, "FileReader: request to read beyond the file end. pos={} size={} len={}",
100 mFileMapOffset, mFileSize, pLen);
101 LOGP(error, "Closing the file {}. The read data is invalid.", mFileName);
102 mFileMap.close();
103 mFileMapOffset = 0;
104 mFileSize = 0;
105 return false;
106 }
107
108 std::memcpy(reinterpret_cast<char*>(pPtr), mFileMap.data() + mFileMapOffset, lToRead);
109 mFileMapOffset += lToRead;
110 return true;
111 }
112
113 // return the pointer
114 unsigned char* peek() const
115 {
116 return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mFileMap.data() + mFileMapOffset));
117 }
118
119 inline bool ignore_nbytes(const std::size_t pLen)
120 {
121 const std::size_t lToIgnore = std::min(pLen, std::size_t(mFileSize - mFileMapOffset));
122 if (pLen != lToIgnore) {
123 LOGP(error, "FileReader: request to ignore bytes beyond the file end. pos={} size={} len={}",
124 mFileMapOffset, mFileSize, pLen);
125 LOGP(error, "Closing the file {}. The read data is invalid.", mFileName);
126 mFileMap.close();
127 mFileMapOffset = 0;
128 mFileSize = 0;
129 return false;
130 }
131
132 mFileMapOffset += lToIgnore;
133 assert(mFileMapOffset <= mFileSize);
134 return true;
135 }
136
137 std::size_t getHeaderStackSize();
138 o2::header::Stack getHeaderStack(std::size_t& pOrigsize);
139
140 // flags for upgrading DataHeader versions
141 static std::uint64_t sStfId; // TODO: add id to files metadata
142};
143} // namespace rawdd
144} // namespace o2
145
146#endif /* ALICEO2_SUBTIMEFRAME_FILE_READER_RAWDD_H_ */
std::uint64_t position() const
Tell the current position of the file.
bool eof() const
Is the stream position at EOF.
std::unique_ptr< MessagesPerRoute > read(fair::mq::Device *device, const std::vector< o2f::OutputRoute > &outputRoutes, const std::string &rawChannel, size_t slice)
Read a single TF from the file.
std::uint64_t size() const
Tell the size of the file.
void set_position(std::uint64_t pPos)
Set the current position of the file.
GLenum void ** pointer
Definition glcorearb.h:805
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
std::unordered_map< std::string, std::unique_ptr< fair::mq::Parts > > MessagesPerRoute
SubTimeFrameFileReader.
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
the main header struct
Definition DataHeader.h:620
a move-only header stack with serialized headers This is the flat buffer where all the headers in a m...
Definition Stack.h:33