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);
51
53 std::unique_ptr<MessagesPerRoute> read(fair::mq::Device* device, const std::vector<o2f::OutputRoute>& outputRoutes, const std::string& rawChannel, size_t slice, bool sup0xccdb, int verbosity);
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 // helper to make sure written chunks are buffered, only allow pointers
80 template <typename pointer,
81 typename = std::enable_if_t<std::is_pointer<pointer>::value>>
82 bool read_advance(pointer pPtr, std::uint64_t pLen)
83 {
84 if (!mFileMap.is_open()) {
85 return false;
86 }
87
88 assert(mFileMapOffset <= mFileSize);
89 const std::uint64_t lToRead = std::min(pLen, mFileSize - mFileMapOffset);
90
91 if (lToRead != pLen) {
92 LOGP(error, "FileReader: request to read beyond the file end. pos={} size={} len={}",
93 mFileMapOffset, mFileSize, pLen);
94 LOGP(error, "Closing the file {}. The read data is invalid.", mFileName);
95 mFileMap.close();
96 mFileMapOffset = 0;
97 mFileSize = 0;
98 return false;
99 }
100
101 std::memcpy(reinterpret_cast<char*>(pPtr), mFileMap.data() + mFileMapOffset, lToRead);
102 mFileMapOffset += lToRead;
103 return true;
104 }
105
106 // return the pointer
107 unsigned char* peek() const
108 {
109 return const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(mFileMap.data() + mFileMapOffset));
110 }
111
112 inline bool ignore_nbytes(const std::size_t pLen)
113 {
114 const std::size_t lToIgnore = std::min(pLen, std::size_t(mFileSize - mFileMapOffset));
115 if (pLen != lToIgnore) {
116 LOGP(error, "FileReader: request to ignore bytes beyond the file end. pos={} size={} len={}",
117 mFileMapOffset, mFileSize, pLen);
118 LOGP(error, "Closing the file {}. The read data is invalid.", mFileName);
119 mFileMap.close();
120 mFileMapOffset = 0;
121 mFileSize = 0;
122 return false;
123 }
124
125 mFileMapOffset += lToIgnore;
126 assert(mFileMapOffset <= mFileSize);
127 return true;
128 }
129
130 std::size_t getHeaderStackSize();
131 o2::header::Stack getHeaderStack(std::size_t& pOrigsize);
132
133 // flags for upgrading DataHeader versions
134 static std::uint64_t sStfId; // TODO: add id to files metadata
135};
136} // namespace rawdd
137} // namespace o2
138
139#endif /* ALICEO2_SUBTIMEFRAME_FILE_READER_RAWDD_H_ */
#define verbosity
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, bool sup0xccdb, int verbosity)
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 PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
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 ...
a move-only header stack with serialized headers This is the flat buffer where all the headers in a m...
Definition Stack.h:36