evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EvioCompactReaderV4.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_EVIOCOMPACTREADERV4_H
13 #define EVIO_6_0_EVIOCOMPACTREADERV4_H
14 
15 
16 #include <stdexcept>
17 #include <vector>
18 #include <memory>
19 #include <limits>
20 #include <climits>
21 #include <string>
22 #include <cstdio>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fstream>
27 #include <sys/mman.h>
28 #include <mutex>
29 #include <sys/mman.h>
30 
31 
32 #include "ByteBuffer.h"
33 #include "ByteOrder.h"
34 #include "BaseStructure.h"
35 #include "IEvioCompactReader.h"
36 #include "IEvioReader.h"
37 #include "EvioReaderV4.h"
38 #include "IBlockHeader.h"
39 #include "EvioNode.h"
40 #include "RecordNode.h"
41 
42 
43 namespace evio {
44 
45 
55 
56  public:
57 
59  static const int BLOCK_SIZE_OFFSET = 0;
60 
62  static const int BLOCK_NUMBER = 4;
63 
65  static const int BLOCK_HEADER_SIZE_OFFSET = 8;
66 
68  static const int BLOCK_EVENT_COUNT = 12;
69 
71  static const int BLOCK_RESERVED_1 = 16;
72 
74  static const int VERSION_MASK = 0xff;
75 
76  private:
77 
79  std::vector<std::shared_ptr<EvioNode>> eventNodes;
80 
82  std::unordered_map<uint32_t, std::shared_ptr<RecordNode>> blockNodes;
83 
88  int32_t eventCount = -1;
89 
91  uint32_t evioVersion = 4;
92 
95 
101  int32_t blockCount = -1;
102 
104  uint32_t firstBlockHeaderWords = 0;
105 
107  std::shared_ptr<BlockHeaderV4> blockHeader;
108 
110  bool hasDict = false;
111 
116  std::string dictionaryXML;
117 
119  std::shared_ptr<EvioXMLDictionary> dictionary = nullptr;
120 
122  std::shared_ptr<ByteBuffer> byteBuffer = nullptr;
123 
125  size_t initialPosition = 0;
126 
129  size_t validDataWords = 0;
130 
132  bool closed = false;
133 
134  //------------------------
135  // File specific members
136  //------------------------
137 
139  bool readingFile = false;
140 
142  std::ifstream file;
143 
145  std::string path = "";
146 
148  size_t fileBytes = 0;
149 
150  public:
151 
152  explicit EvioCompactReaderV4(std::string const & path);
153  explicit EvioCompactReaderV4(std::shared_ptr<ByteBuffer> & byteBuffer);
154 
155  void setBuffer(std::shared_ptr<ByteBuffer> & buf) override ;
156 
157  bool isFile() override ;
158  bool isCompressed() override ;
159  bool isClosed() override ;
160  ByteOrder getByteOrder() override ;
161  uint32_t getEvioVersion() override ;
162  std::string getPath() override ;
163  ByteOrder getFileByteOrder() override ;
164  std::string getDictionaryXML() override ;
165  std::shared_ptr<EvioXMLDictionary> getDictionary() override ;
166  bool hasDictionary() override ;
167 
168  private:
169 
170  void mapFile(std::string const & filename, size_t fileS);
171  void generateEventPositionTable();
172 
173  public:
174 
175  std::shared_ptr<ByteBuffer> getByteBuffer() override;
176  size_t fileSize() override ;
177 
178  std::shared_ptr<EvioNode> getEvent(size_t eventNumber) override ;
179  std::shared_ptr<EvioNode> getScannedEvent(size_t eventNumber) override ;
180 
181  std::shared_ptr<IBlockHeader> getFirstBlockHeader() override ;
182 
183  private:
184 
185  IEvioReader::ReadWriteStatus readFirstHeader();
186  void readDictionary();
187  std::shared_ptr<EvioNode> scanStructure(size_t eventNumber);
188 
189  public:
190 
191  void searchEvent(size_t eventNumber, uint16_t tag, uint8_t num,
192  std::vector<std::shared_ptr<EvioNode>> & vec) override ;
193  void searchEvent(size_t eventNumber, std::string const & dictName,
194  std::shared_ptr<EvioXMLDictionary> & dictionary,
195  std::vector<std::shared_ptr<EvioNode>> & vec) override ;
196 
197 
198  std::shared_ptr<ByteBuffer> removeEvent(size_t eventNumber) override ;
199 
200  std::shared_ptr<ByteBuffer> removeStructure(std::shared_ptr<EvioNode> & removeNode) override ;
201  std::shared_ptr<ByteBuffer> addStructure(size_t eventNumber, ByteBuffer & addBuffer) override ;
202 
203  std::shared_ptr<ByteBuffer> getData(std::shared_ptr<EvioNode> & node) override ;
204  std::shared_ptr<ByteBuffer> getData(std::shared_ptr<EvioNode> & node, bool copy) override ;
205 
206  std::shared_ptr<ByteBuffer> getData(std::shared_ptr<EvioNode> & node,
207  std::shared_ptr<ByteBuffer> & buf) override ;
208  std::shared_ptr<ByteBuffer> getData(std::shared_ptr<EvioNode> & node,
209  std::shared_ptr<ByteBuffer> & buf, bool copy) override ;
210 
211  std::shared_ptr<ByteBuffer> getEventBuffer(size_t eventNumber) override ;
212  std::shared_ptr<ByteBuffer> getEventBuffer(size_t eventNumber, bool copy) override ;
213 
214  std::shared_ptr<ByteBuffer> getStructureBuffer(std::shared_ptr<EvioNode> & node) override ;
215  std::shared_ptr<ByteBuffer> getStructureBuffer(std::shared_ptr<EvioNode> & node, bool copy) override ;
216 
217  void close() override ;
218 
219  uint32_t getEventCount() override ;
220  uint32_t getBlockCount() override ;
221 
222  void toFile(std::string const & fileName) override ;
223  };
224 
225 }
226 
227 #endif //EVIO_6_0_EVIOCOMPACTREADERV4_H
static const int BLOCK_SIZE_OFFSET
Offset to get block size from start of block.
Definition: EvioCompactReaderV4.h:59
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
uint32_t getEvioVersion() override
Get the evio version number.evio version number.
Definition: EvioCompactReaderV4.cpp:137
static const int BLOCK_EVENT_COUNT
Offset to get block event count from start of block.
Definition: EvioCompactReaderV4.h:68
uint32_t getBlockCount() override
This is the number of blocks in the file/buffer including the empty block at the end.the number of blocks in the file/buffer (estimate for version 3 files)
Definition: EvioCompactReaderV4.cpp:1191
std::shared_ptr< EvioNode > getScannedEvent(size_t eventNumber) override
Get the EvioNode object associated with a particular event number which has been scanned so all subst...
Definition: EvioCompactReaderV4.cpp:253
bool isClosed() override
Has close() been called (without reopening by calling setBuffer(std::shared_ptr&lt;ByteBuffer&gt; &amp;))...
Definition: EvioCompactReaderV4.cpp:129
ReadWriteStatus
This enum denotes the status of a read/write.
Definition: IEvioReader.h:54
std::shared_ptr< ByteBuffer > getData(std::shared_ptr< EvioNode > &node) override
Get the data associated with an evio structure in ByteBuffer form.The returned buffer is a view into ...
Definition: EvioCompactReaderV4.cpp:1108
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
static const int BLOCK_RESERVED_1
Offset to get block size from start of block.
Definition: EvioCompactReaderV4.h:71
bool hasDictionary() override
Does this evio file have an associated XML dictionary?true if this evio file has an associated XML di...
Definition: EvioCompactReaderV4.cpp:193
std::shared_ptr< EvioNode > getEvent(size_t eventNumber) override
Get the EvioNode object associated with a particular event number.number of event (place in file/buff...
Definition: EvioCompactReaderV4.cpp:243
std::shared_ptr< ByteBuffer > removeEvent(size_t eventNumber) override
This method removes the data of the given event from the buffer.It also marks any existing EvioNodes ...
Definition: EvioCompactReaderV4.cpp:655
static const int VERSION_MASK
Mask to get version number from 6th int in block.
Definition: EvioCompactReaderV4.h:74
std::shared_ptr< IBlockHeader > getFirstBlockHeader() override
This returns the FIRST block (or record) header.(Not the file header of evio version 6+ files)...
Definition: EvioCompactReaderV4.cpp:504
This class is used to read an evio format version 4 formatted file or buffer and extract specific evi...
Definition: EvioCompactReaderV4.h:54
std::shared_ptr< ByteBuffer > removeStructure(std::shared_ptr< EvioNode > &removeNode) override
This method removes the data, represented by the given node, from the buffer.It also marks the node a...
Definition: EvioCompactReaderV4.cpp:674
bool isFile() override
Is this reader reading a file?true if reading file, false if reading buffer
Definition: EvioCompactReaderV4.cpp:121
void setBuffer(std::shared_ptr< ByteBuffer > &buf) override
This method can be used to avoid creating additional EvioCompactReader objects by reusing this one wi...
Definition: EvioCompactReaderV4.cpp:97
EvioCompactReaderV4(std::string const &path)
Constructor for reading an event file.
Definition: EvioCompactReaderV4.cpp:26
std::shared_ptr< EvioXMLDictionary > getDictionary() override
Get the evio dictionary if is there is one.if object closed and dictionary still unread evio dictiona...
Definition: EvioCompactReaderV4.cpp:169
std::string getPath() override
Get the path to the file.path to the file
Definition: EvioCompactReaderV4.cpp:141
uint32_t getEventCount() override
This is the number of events in the file/buffer.Any dictionary event is not included in the count...
Definition: EvioCompactReaderV4.cpp:1187
std::string getDictionaryXML() override
Get the XML format dictionary is there is one.if object closed and dictionary still unread XML format...
Definition: EvioCompactReaderV4.cpp:149
std::shared_ptr< ByteBuffer > getByteBuffer() override
Get the byte buffer being read directly or corresponding to the event file.the byte buffer being read...
Definition: EvioCompactReaderV4.cpp:235
std::shared_ptr< ByteBuffer > addStructure(size_t eventNumber, ByteBuffer &addBuffer) override
This method adds an evio container (bank, segment, or tag segment) as the last structure contained in...
Definition: EvioCompactReaderV4.cpp:907
size_t fileSize() override
Get the size of the file being read, in bytes.For small files, obtain the file size using the memory ...
Definition: EvioCompactReaderV4.cpp:239
std::shared_ptr< ByteBuffer > getEventBuffer(size_t eventNumber) override
Get an evio bank or event in ByteBuffer form.The returned buffer is a view into the data of this read...
Definition: EvioCompactReaderV4.cpp:1137
This is an interface for a compact reader of evio format files and buffers.
Definition: IEvioCompactReader.h:41
ByteOrder getByteOrder() override
Get the byte order of the file/buffer being read.byte order of the file/buffer being read...
Definition: EvioCompactReaderV4.cpp:133
std::shared_ptr< ByteBuffer > getStructureBuffer(std::shared_ptr< EvioNode > &node) override
Get an evio structure (bank, seg, or tagseg) in ByteBuffer form.The returned buffer is a view into th...
Definition: EvioCompactReaderV4.cpp:1160
void toFile(std::string const &fileName) override
Save the internal byte buffer to the given file (overwrites existing file).name of file to write if e...
Definition: EvioCompactReaderV4.cpp:1195
static const int BLOCK_HEADER_SIZE_OFFSET
Offset to get block header size from start of block.
Definition: EvioCompactReaderV4.h:65
void searchEvent(size_t eventNumber, uint16_t tag, uint8_t num, std::vector< std::shared_ptr< EvioNode >> &vec) override
This method searches the specified event in a file/buffer and returns a vector of objects each of whi...
Definition: EvioCompactReaderV4.cpp:586
static const int BLOCK_NUMBER
Offset to get block number from start of block.
Definition: EvioCompactReaderV4.h:62
ByteOrder getFileByteOrder() override
When reading a file, this method&#39;s return value is the byte order of the evio data in the file...
Definition: EvioCompactReaderV4.cpp:145
void close() override
This only sets the position to its initial value.
Definition: EvioCompactReaderV4.cpp:1180
bool isCompressed() override
Is the data in the file/buffer compressed?true if data is compressed.
Definition: EvioCompactReaderV4.cpp:125
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61