evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EvioReaderV4.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 
12 #ifndef EVIO_6_0_EVIOREADERV4_H
13 #define EVIO_6_0_EVIOREADERV4_H
14 
15 
16 #include <fstream>
17 #include <vector>
18 #include <memory>
19 #include <mutex>
20 #include <stdexcept>
21 
22 
23 #include "ByteOrder.h"
24 #include "ByteBuffer.h"
25 #include "IEvioReader.h"
26 #include "IBlockHeader.h"
27 #include "BlockHeaderV2.h"
28 #include "BlockHeaderV4.h"
29 #include "EventParser.h"
30 
31 
32 namespace evio {
33 
58  class EvioReaderV4 : public IEvioReader {
59 
60  public:
61 
63  static const uint32_t MAGIC_OFFSET = 28;
64 
66  static const uint32_t VERSION_OFFSET = 20;
67 
69  static const uint32_t BLOCK_SIZE_OFFSET = 0;
70 
72  static const uint32_t VERSION_MASK = 0xff;
73 
77  static const uint32_t DEFAULT_READ_BYTES = 32768 * 500; // 16384000 bytes
78 
79  private:
80 
83  uint32_t eventNumber = 0;
84 
89  int32_t eventCount = -1;
90 
92  uint32_t evioVersion = 4;
93 
96 
98  uint32_t firstBlockSize = 0;
99 
105  uint32_t blockCount = 0;
106 
108  std::shared_ptr<BlockHeaderV2> blockHeader2;
109 
111  std::shared_ptr<BlockHeaderV4> blockHeader4;
112 
116  std::shared_ptr<IBlockHeader> blockHeader;
117 
119  std::shared_ptr<IBlockHeader> firstBlockHeader;
120 
122  std::shared_ptr<BlockHeaderV2> firstBlockHeader2;
123 
125  std::shared_ptr<BlockHeaderV4> firstBlockHeader4;
126 
128  uint32_t blockNumberExpected = 1;
129 
131  bool checkBlockNumSeq = false;
132 
134  bool lastBlock = false;
135 
137  bool synchronized = false;
138 
143  std::string dictionaryXML;
144 
146  std::shared_ptr<ByteBuffer> byteBuffer;
147 
149  std::shared_ptr<EventParser> parser;
150 
152  size_t initialPosition = 0;
153 
156  std::vector<uint32_t> eventPositions;
157 
159  std::mutex mtx;
160 
161 
162  //------------------------
163  // File specific members
164  //------------------------
165 
166 
168  std::string path;
169 
171  std::ifstream file;
172 
174  size_t fileBytes = 0;
175 
177  bool swap = false;
178 
183  bool sequentialRead = false;
184 
185 
186  //------------------------
187  // EvioReader's state
188  //------------------------
189 
190 
192  bool closed = false;
193 
198  class ReaderState {
199  public:
200  bool lastBlock;
201  uint32_t eventNumber;
202  size_t filePosition;
203  size_t byteBufferLimit;
204  size_t byteBufferPosition;
205  uint32_t blockNumberExpected;
206  std::shared_ptr<BlockHeaderV2> blockHeader2;
207  std::shared_ptr<BlockHeaderV4> blockHeader4;
208  };
209 
210  ReaderState * getState();
211  void restoreState(ReaderState * state);
212 
213  //------------------------
214 
215  size_t generateEventPositions(std::shared_ptr<ByteBuffer> & byteBuffer);
216 
217  public:
218 
219  explicit EvioReaderV4(std::string const & path, bool checkBlkNumSeq = false, bool synced = false);
220  explicit EvioReaderV4(std::shared_ptr<ByteBuffer> & byteBuffer, bool checkBlkNumSeq = false, bool synced = false);
221 
222 
223  void setBuffer(std::shared_ptr<ByteBuffer> & buf) override;
224  bool isClosed() override;
225  bool checkBlockNumberSequence() override;
226  ByteOrder & getByteOrder() override;
227  uint32_t getEvioVersion() override;
228  std::string getPath() override;
229 
230  std::shared_ptr<EventParser> & getParser() override;
231  void setParser(std::shared_ptr<EventParser> & evParser) override;
232 
233  std::string getDictionaryXML() override;
234  bool hasDictionaryXML() override;
235  std::shared_ptr<EvioEvent> getFirstEvent() override;
236  bool hasFirstEvent() override;
237 
238  size_t getNumEventsRemaining() override;
239  std::shared_ptr<ByteBuffer> getByteBuffer() override ;
240  size_t fileSize() override;
241  std::shared_ptr<IBlockHeader> getFirstBlockHeader() override ;
242 
243  protected:
244 
245  void parseFirstHeader(std::shared_ptr<ByteBuffer> & headerBuf);
247 
248  private:
249 
250  void prepareForSequentialRead();
251  void prepareForBufferRead(std::shared_ptr<ByteBuffer> & buffer) const;
252 
253  void readDictionary(std::shared_ptr<ByteBuffer> & buffer);
254  std::shared_ptr<EvioEvent> getEventV4(size_t index);
255 
256  public:
257 
258  std::shared_ptr<EvioEvent> getEvent(size_t index) override ;
259  std::shared_ptr<EvioEvent> parseEvent(size_t index) override ;
260  std::shared_ptr<EvioEvent> nextEvent() override ;
261  std::shared_ptr<EvioEvent> parseNextEvent() override ;
262  void parseEvent(std::shared_ptr<EvioEvent> evioEvent) override ;
263  uint32_t getEventArray(size_t evNumber, std::vector<uint8_t> & vec) override;
264  uint32_t getEventBuffer(size_t evNumber, ByteBuffer & buf) override;
265 
266  private:
267 
268  size_t bufferBytesRemaining() const;
269  uint32_t blockBytesRemaining() const;
270  std::shared_ptr<EvioEvent> gotoEventNumber(size_t evNumber, bool parse);
271 
272  public:
273 
274  void rewind() override ;
275  ssize_t position() override;
276  void close() override ;
277 
278  std::shared_ptr<IBlockHeader> getCurrentBlockHeader() override ;
279  std::shared_ptr<EvioEvent> gotoEventNumber(size_t evNumber) override ;
280 
281  size_t getEventCount() override;
282  size_t getBlockCount() override;
283  };
284 
285 
286 }
287 
288 
289 #endif //EVIO_6_0_EVIOREADERV4_H
std::shared_ptr< EvioEvent > parseEvent(size_t index) override
This is a workhorse method.It retrieves the desired event from the file/buffer, and then parses it SA...
Definition: EvioReaderV4.cpp:975
std::shared_ptr< EvioEvent > getEvent(size_t index) override
Get the event in the file/buffer at a given index (starting at 1).As useful as this sounds...
Definition: EvioReaderV4.cpp:897
EvioReaderV4(std::string const &path, bool checkBlkNumSeq=false, bool synced=false)
Constructor for reading an event file.
Definition: EvioReaderV4.cpp:84
std::shared_ptr< EvioEvent > parseNextEvent() override
This is a workhorse method.It retrieves the next event from the file/buffer, and then parses it SAX-l...
Definition: EvioReaderV4.cpp:1130
std::shared_ptr< IBlockHeader > getCurrentBlockHeader() override
This returns the current (active) block (physical record) header.Since most users have no interest in...
Definition: EvioReaderV4.cpp:1287
std::string getPath() override
Get the path to the file.path to the file
Definition: EvioReaderV4.cpp:387
std::shared_ptr< EvioEvent > getFirstEvent() override
Get the &quot;first&quot; event if there is one.It&#39;s also called the Beginning-Of-Run event. This event is defined once but included in each of the related split files written out. the first event is it existed, else null.
Definition: EvioReaderV4.cpp:407
static const uint32_t MAGIC_OFFSET
Offset to get magic number from start of file.
Definition: EvioReaderV4.h:63
ReadWriteStatus
This enum denotes the status of a read/write.
Definition: IEvioReader.h:54
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
static const uint32_t VERSION_MASK
Mask to get version number from 6th int in block.
Definition: EvioReaderV4.h:72
std::shared_ptr< EvioEvent > nextEvent() override
Get the next event in the file/buffer.As useful as this sounds, most applications will probably call ...
Definition: EvioReaderV4.cpp:988
bool checkBlockNumberSequence() override
Is this reader checking the block number sequence and throwing an exception if it&#39;s not sequential an...
Definition: EvioReaderV4.cpp:375
size_t fileSize() override
Get the size of the file being read, in bytes.the file size in bytes
Definition: EvioReaderV4.cpp:438
This pure, virtual class is meant to encapsulate the operation of reading both 2 differently formatte...
Definition: IEvioReader.h:39
void parseFirstHeader(std::shared_ptr< ByteBuffer > &headerBuf)
Reads 8 words of the first block (physical record) header in order to determine the evio version # an...
Definition: EvioReaderV4.cpp:455
void setBuffer(std::shared_ptr< ByteBuffer > &buf) override
This method can be used to avoid creating additional EvioReader objects by reusing this one with anot...
Definition: EvioReaderV4.cpp:332
static const uint32_t BLOCK_SIZE_OFFSET
Offset to get block size from start of block.
Definition: EvioReaderV4.h:69
void close() override
This is closes the file, but for buffers it only sets the position to 0.
Definition: EvioReaderV4.cpp:1262
static const uint32_t DEFAULT_READ_BYTES
Default size for a single file read in bytes when reading evio format 1-3.
Definition: EvioReaderV4.h:77
ssize_t position() override
This is equivalent to obtaining the current position in the file.What it actually does is return the ...
Definition: EvioReaderV4.cpp:1243
ByteOrder & getByteOrder() override
Get the byte order of the file/buffer being read.byte order of the file/buffer being read...
Definition: EvioReaderV4.cpp:379
uint32_t getEventBuffer(size_t evNumber, ByteBuffer &buf) override
Get an evio bank or event in ByteBuffer form.number of event of interest buffer to contain bank&#39;s/eve...
Definition: EvioReaderV4.cpp:1166
std::shared_ptr< IBlockHeader > getFirstBlockHeader() override
This returns the FIRST block (record) header.the first block (record) header.
Definition: EvioReaderV4.cpp:442
std::string getDictionaryXML() override
Get the XML format dictionary if there is one.XML format dictionary, else null.
Definition: EvioReaderV4.cpp:399
static const uint32_t VERSION_OFFSET
Offset to get version number from start of file.
Definition: EvioReaderV4.h:66
IEvioReader::ReadWriteStatus processNextBlock()
Reads the block (physical record) header.
Definition: EvioReaderV4.cpp:671
bool isClosed() override
Has close() been called (without reopening by calling setBuffer(std::shared_ptr&lt;ByteBuffer&gt; &amp;))...
Definition: EvioReaderV4.cpp:371
std::shared_ptr< EventParser > & getParser() override
Get the file/buffer parser.file/buffer parser.
Definition: EvioReaderV4.cpp:391
size_t getEventCount() override
This is the number of events in the file/buffer.Any dictionary or first event are not included in the...
Definition: EvioReaderV4.cpp:1358
bool hasFirstEvent() override
Does this evio file have an associated first event? It&#39;s also called the Beginning-Of-Run event...
Definition: EvioReaderV4.cpp:416
void setParser(std::shared_ptr< EventParser > &evParser) override
Set the file/buffer parser.file/buffer parser.
Definition: EvioReaderV4.cpp:395
size_t getNumEventsRemaining() override
Get the number of events remaining in the file.Useful only if doing a sequential read.number of events remaining in the file if failed reading from file
Definition: EvioReaderV4.cpp:425
uint32_t getEventArray(size_t evNumber, std::vector< uint8_t > &vec) override
Get an evio bank or event in vector-of-bytes form.number of event of interest (starting at 1)...
Definition: EvioReaderV4.cpp:1152
std::shared_ptr< ByteBuffer > getByteBuffer() override
Get the byte buffer being read.Not useful when reading files. the byte buffer being read (in certain ...
Definition: EvioReaderV4.cpp:434
size_t getBlockCount() override
This is the number of blocks/records in the file/buffer including the empty block, record or trailer at the end.if object closed. the number of records in the file/buffer (estimate for version 3 files).
Definition: EvioReaderV4.cpp:1413
void rewind() override
The equivalent of rewinding the file.What it actually does is set the position of the file/buffer bac...
Definition: EvioReaderV4.cpp:1203
bool hasDictionaryXML() override
Does this evio file have an associated XML dictionary?true if this evio file has an associated XML di...
Definition: EvioReaderV4.cpp:403
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61
This is a class of interest to the user.
Definition: EvioReaderV4.h:58
uint32_t getEvioVersion() override
Get the evio version number.evio version number.
Definition: EvioReaderV4.cpp:383