evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Reader.h
Go to the documentation of this file.
1 //
2 // Copyright 2020, Jefferson Science Associates, LLC.
3 // Subject to the terms in the LICENSE file found in the top-level directory.
4 //
5 // EPSCI Group
6 // Thomas Jefferson National Accelerator Facility
7 // 12000, Jefferson Ave, Newport News, VA 23606
8 // (757)-269-7100
9 
10 
11 #ifndef EVIO_6_0_READER_H
12 #define EVIO_6_0_READER_H
13 
14 
15 #include <cstdint>
16 #include <cstring>
17 #include <string>
18 #include <vector>
19 #include <fstream>
20 #include <ios>
21 #include <iostream>
22 #include <stdexcept>
23 #include <memory>
24 
25 
26 #include "ByteOrder.h"
27 #include "ByteBuffer.h"
28 #include "FileHeader.h"
29 #include "RecordHeader.h"
30 #include "FileEventIndex.h"
31 #include "RecordInput.h"
32 #include "EvioException.h"
33 #include "EvioNode.h"
34 #include "IBlockHeader.h"
35 #include "Util.h"
36 
37 
38 namespace evio {
39 
117  class Reader {
118 
119  friend class EvioCompactReaderV6;
120 
121  private:
122 
123 
129  class RecordPosition {
130 
131  private:
132 
134  size_t position;
135 
137  uint32_t length;
138 
140  uint32_t count;
141 
142  public:
143 
144  explicit RecordPosition(size_t pos) {
145  count = length = 0;
146  position = pos;
147  }
148 
149  RecordPosition(size_t pos, uint32_t len, uint32_t cnt) {
150  position = pos;
151  length = len;
152  count = cnt;
153  }
154 
155  RecordPosition setPosition(size_t _pos) {
156  position = _pos;
157  return *this;
158  }
159 
160  RecordPosition setLength(uint32_t _len) {
161  length = _len;
162  return *this;
163  }
164 
165  RecordPosition setCount(uint32_t _cnt) {
166  count = _cnt;
167  return *this;
168  }
169 
170  size_t getPosition() const { return position; }
171 
172  uint32_t getLength() const { return length; }
173 
174  uint32_t getCount() const { return count; }
175 
176  std::string toString() const {
177  std::stringstream ss;
178  ss << " POSITION = " << std::setw(16) << position << ", LENGTH = " << std::setw(12) <<
179  length << ", COUNT = " << std::setw(8) << count << std::endl;
180  return ss.str();
181  }
182  };
183 
184 
186  static const uint32_t headerInfoLen = 8;
187 
188 
194  std::vector<RecordPosition> recordPositions;
196  std::ifstream inStreamRandom;
198  std::string fileName {""};
200  size_t fileSize = 0;
202  FileHeader fileHeader;
204  bool fromFile = true;
205 
206 
208  std::shared_ptr<ByteBuffer> buffer = nullptr;
210  size_t bufferOffset = 0;
212  size_t bufferLimit = 0;
213 
214 
216  RecordInput inputRecordStream;
218  uint32_t currentRecordLoaded = 0;
219  // TODO: Look at this
221  std::shared_ptr<RecordHeader> firstRecordHeader = nullptr;
223  uint32_t recordNumberExpected = 1;
225  bool checkRecordNumberSequence = false;
227  FileEventIndex eventIndex;
228 
229 
231  std::string dictionaryXML {""};
233  std::shared_ptr<uint8_t> firstEvent = nullptr;
235  uint32_t firstEventSize = 0;
236 
237  // TODO: The HIPO library is NOT evio dependent!!!!
238 
240  std::vector<std::shared_ptr<EvioNode>> eventNodes;
241 
242 
244  bool closed = false;
246  bool compressed = false;
248  ByteOrder byteOrder {ByteOrder::ENDIAN_LOCAL};
251  int32_t sequentialIndex = -1;
252 
258  bool evioFormat = true;
259 
263  bool lastCalledSeqNext = false;
265  uint32_t evioVersion = 6;
266 
267 
268 
269  void setByteOrder(ByteOrder & order);
270  static uint32_t getTotalByteCounts(ByteBuffer & buf, uint32_t* info, uint32_t infoLen);
271  static uint32_t getTotalByteCounts(std::shared_ptr<ByteBuffer> & buf, uint32_t* info, uint32_t infoLen);
272  //static std::string getStringArray(ByteBuffer & buffer, int wrap, int max);
273  //static std::string getHexStringInt(int32_t value);
274 
275  public:
276 
277  Reader();
278  explicit Reader(std::string const & filename);
279  Reader(std::string const & filename, bool forceScan);
280  explicit Reader(std::shared_ptr<ByteBuffer> & buffer, bool checkRecordNumSeq = false);
281 
282  ~Reader() = default;
283 
284  void open(std::string const & filename, bool scan = true);
285  void close();
286 
287  bool isClosed() const;
288  bool isFile() const;
289 
290  std::string getFileName() const;
291  size_t getFileSize() const;
292 
293  void setBuffer(std::shared_ptr<ByteBuffer> & buf);
294  std::shared_ptr<ByteBuffer> getBuffer();
295  size_t getBufferOffset() const;
296 
297  FileHeader & getFileHeader();
298  std::shared_ptr<RecordHeader> & getFirstRecordHeader();
299 
300  ByteOrder & getByteOrder();
301  uint32_t getVersion() const;
302  bool isCompressed() const;
303  bool isEvioFormat() const;
304  std::string getDictionary();
305  bool hasDictionary() const;
306 
307  std::shared_ptr<uint8_t> & getFirstEvent(uint32_t *size);
308  uint32_t getFirstEventSize();
309  bool hasFirstEvent() const;
310 
311  uint32_t getEventCount() const;
312  uint32_t getRecordCount() const;
313 
314  std::vector<RecordPosition> & getRecordPositions();
315  std::vector<std::shared_ptr<EvioNode>> & getEventNodes();
316 
317  bool getCheckRecordNumberSequence() const;
318 
319  uint32_t getNumEventsRemaining() const;
320 
321  std::shared_ptr<uint8_t> getNextEvent(uint32_t * len);
322  std::shared_ptr<uint8_t> getPrevEvent(uint32_t * len);
323 
324  std::shared_ptr<EvioNode> getNextEventNode();
325  std::shared_ptr<ByteBuffer> readUserHeader();
326 
327  std::shared_ptr<uint8_t> getEvent(uint32_t index, uint32_t * len);
328  ByteBuffer & getEvent(ByteBuffer & buf, uint32_t index);
329  std::shared_ptr<ByteBuffer> getEvent(std::shared_ptr<ByteBuffer> & buf, uint32_t index);
330  uint32_t getEventLength(uint32_t index);
331  std::shared_ptr<EvioNode> getEventNode(uint32_t index);
332 
333  bool hasNext() const;
334  bool hasPrev() const;
335 
336  uint32_t getRecordEventCount() const;
337  uint32_t getCurrentRecord() const;
338  RecordInput & getCurrentRecordStream();
339  bool readRecord(uint32_t index);
340 
341 
342  protected:
343 
347 
348 
349  static void findRecordInfo(std::shared_ptr<ByteBuffer> & buf, uint32_t offset,
350  uint32_t* info, uint32_t infoLen);
351  static void findRecordInfo(ByteBuffer & buf, uint32_t offset,
352  uint32_t* info, uint32_t infoLen);
353 
354 
355  std::shared_ptr<ByteBuffer> scanBuffer();
356  void scanUncompressedBuffer();
357  void forceScanFile();
358  void scanFile(bool force);
359 
360  // The next 2 methods will not work on events which are not evio format data.
361  // They are included here so other classes, like EvioCompactReader and EvioReader,
362  // can use their APIs to call the new evio version 6 classes like this one.
363  // These new classes were initially designed to be data format agnostic, but adding
364  // these methods violates that.
365  std::shared_ptr<ByteBuffer> & addStructure(uint32_t eventNumber, ByteBuffer & addBuffer);
366  std::shared_ptr<ByteBuffer> & removeStructure(std::shared_ptr<EvioNode> & removeNode);
367 
368  void show() const;
369 
370  //int main(int argc, char **argv);
371 
372  };
373 
374 }
375 
376 
377 #endif //EVIO_6_0_READER_H
uint32_t getFirstEventSize()
Get size, in bytes, of byte array representing the first event.
Definition: Reader.cpp:336
std::vector< std::shared_ptr< EvioNode > > & getEventNodes()
Get a reference to the list of EvioNode objects contained in the buffer being read.
Definition: Reader.cpp:382
size_t getFileSize() const
Get the size of the file being read, in bytes.
Definition: Reader.cpp:224
bool getCheckRecordNumberSequence() const
Get whether or not record numbers are enforced to be sequential.
Definition: Reader.cpp:389
void forceScanFile()
Scan file to find all records and store their position, length, and event count.
Definition: Reader.cpp:1404
void close()
This closes the file.
Definition: Reader.cpp:129
uint32_t getEventLength(uint32_t index)
Returns the length of the event with given index.
Definition: Reader.cpp:645
std::shared_ptr< ByteBuffer > scanBuffer()
This method scans a buffer to find all records and store their position, length, and event count...
Definition: Reader.cpp:1049
bool isFile() const
Is a file being read?
Definition: Reader.cpp:155
~Reader()=default
Reader class that reads files stored in the HIPO format.
Definition: Reader.h:117
uint32_t getEventCount() const
Get the number of events in file/buffer.
Definition: Reader.cpp:360
void extractDictionaryAndFirstEvent()
Extract dictionary and first event from file/buffer if possible, else do nothing. ...
Definition: Reader.cpp:747
FileHeader & getFileHeader()
Get the file header from reading a file.
Definition: Reader.cpp:247
std::shared_ptr< ByteBuffer > & removeStructure(std::shared_ptr< EvioNode > &removeNode)
This method removes the data, represented by the given node, from the buffer.
Definition: Reader.cpp:1634
bool hasFirstEvent() const
Does this evio file/buffer have an associated first event?
Definition: Reader.cpp:348
std::shared_ptr< uint8_t > getNextEvent(uint32_t *len)
Get a byte array representing the next event from the file/buffer while sequentially reading...
Definition: Reader.cpp:412
uint32_t getVersion() const
Get the Evio format version number of the file/buffer being read.
Definition: Reader.cpp:275
bool hasDictionary() const
Does this evio file/buffer have an associated XML dictionary?
Definition: Reader.cpp:308
bool isClosed() const
Has close() been called (without reopening by calling setBuffer(std::shared_ptr&lt;ByteBuffer&gt; &amp;))...
Definition: Reader.cpp:148
uint32_t getCurrentRecord() const
Get the index of the current record.
Definition: Reader.cpp:711
std::shared_ptr< EvioNode > getEventNode(uint32_t index)
Get an EvioNode representing the specified event from the buffer.
Definition: Reader.cpp:675
Reader()
Default constructor.
Definition: Reader.cpp:22
std::shared_ptr< uint8_t > getPrevEvent(uint32_t *len)
Get a byte array representing the previous event from the sequential queue.
Definition: Reader.cpp:454
std::shared_ptr< ByteBuffer > & addStructure(uint32_t eventNumber, ByteBuffer &addBuffer)
This method adds an evio container (bank, segment, or tag segment) as the last structure contained in...
Definition: Reader.cpp:1758
std::shared_ptr< uint8_t > getEvent(uint32_t index, uint32_t *len)
Get a byte array representing the specified event from the file/buffer.
Definition: Reader.cpp:563
ByteOrder & getByteOrder()
Get the byte order of the file/buffer being read.
Definition: Reader.cpp:261
bool isEvioFormat() const
Does this file/buffer contain non-evio format events?
Definition: Reader.cpp:289
std::vector< RecordPosition > & getRecordPositions()
Returns a reference to the list of record positions in the file.
Definition: Reader.cpp:374
void setBuffer(std::shared_ptr< ByteBuffer > &buf)
This method can be used to avoid creating additional Reader objects by reusing this one with another ...
Definition: Reader.cpp:170
void scanUncompressedBuffer()
Scan buffer containing uncompressed data to find all records and store their position, length, and event count.
Definition: Reader.cpp:1259
std::shared_ptr< ByteBuffer > readUserHeader()
Reads user header of the file header/first record header of buffer.
Definition: Reader.cpp:519
size_t getBufferOffset() const
Get the beginning position of the buffer being read.
Definition: Reader.cpp:240
static void findRecordInfo(std::shared_ptr< ByteBuffer > &buf, uint32_t offset, uint32_t *info, uint32_t infoLen)
Reads data from a record header in order to determine things like the bitInfo word, various lengths, etc.
Definition: Reader.cpp:899
std::shared_ptr< EvioNode > getNextEventNode()
Get an EvioNode representing the next event from the buffer while sequentially reading.
Definition: Reader.cpp:495
bool hasPrev() const
Checks if the stream has previous event to be accessed through, getPrevEvent()
Definition: Reader.cpp:697
std::string getFileName() const
Get the name of the file being read.
Definition: Reader.cpp:217
void extractDictionaryFromFile()
Extract dictionary and first event from file if possible, else do nothing.
Definition: Reader.cpp:818
void open(std::string const &filename, bool scan=true)
Opens an input stream in binary mode.
Definition: Reader.cpp:92
uint32_t getRecordEventCount() const
Get the number of events in current record.
Definition: Reader.cpp:704
bool readRecord(uint32_t index)
Reads record from the file/buffer at the given record index.
Definition: Reader.cpp:727
std::string getDictionary()
Get the XML format dictionary if there is one.
Definition: Reader.cpp:296
bool isCompressed() const
Is the data in the file/buffer compressed?
Definition: Reader.cpp:282
std::shared_ptr< RecordHeader > & getFirstRecordHeader()
Get the first record header from reading a file/buffer.
Definition: Reader.cpp:254
uint32_t getRecordCount() const
Get the number of records read from the file/buffer.
Definition: Reader.cpp:367
void scanFile(bool force)
Scans the file to index all the record positions.
Definition: Reader.cpp:1491
bool hasNext() const
Checks if the file has an event to read next.
Definition: Reader.cpp:690
This class is used to read an evio format version 6 formatted file or buffer.
Definition: EvioCompactReaderV6.h:52
void extractDictionaryFromBuffer()
Extract dictionary and first event from buffer if possible, else do nothing.
Definition: Reader.cpp:762
RecordInput & getCurrentRecordStream()
Get the current record stream.
Definition: Reader.cpp:718
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61
void show() const
Print out all record position information.
Definition: Reader.cpp:1841
std::shared_ptr< ByteBuffer > getBuffer()
Get the buffer being read, if any.
Definition: Reader.cpp:233
uint32_t getNumEventsRemaining() const
Get the number of events remaining in the file/buffer.
Definition: Reader.cpp:398
std::shared_ptr< uint8_t > & getFirstEvent(uint32_t *size)
Get a byte array representing the first event.
Definition: Reader.cpp:322