evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileHeader.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_FILEHEADER_H
12 #define EVIO_6_0_FILEHEADER_H
13 
14 
15 #include <string>
16 #include <iostream>
17 #include <ios>
18 #include <iomanip>
19 #include <sstream>
20 
21 
22 #include "ByteOrder.h"
23 #include "HeaderType.h"
24 #include "ByteBuffer.h"
25 #include "Compressor.h"
26 #include "EvioException.h"
27 #include "Util.h"
28 
29 
30 namespace evio {
31 
32 
91  class FileHeader {
92 
93 
94  private:
95 
96  public:
97 
99  static const uint32_t HIPO_FILE_UNIQUE_WORD = 0x4F504948; // 0x4849504F = HIPO
101  static const uint32_t EVIO_FILE_UNIQUE_WORD = 0x4556494F; // = EVIO
103  static const uint32_t HEADER_SIZE_WORDS = 14;
105  static const uint32_t HEADER_SIZE_BYTES = 56;
107  static const uint32_t HEADER_MAGIC = 0xc0da0100;
108 
109  // Byte offset to header words
110 
112  static const uint32_t FILE_ID_OFFSET = 0;
114  static const uint32_t FILE_NUMBER_OFFSET = 4;
116  static const uint32_t HEADER_LENGTH_OFFSET = 8;
118  static const uint32_t RECORD_COUNT_OFFSET = 12;
120  static const uint32_t INDEX_ARRAY_OFFSET = 16;
122  static const uint32_t BIT_INFO_OFFSET = 20;
124  static const uint32_t USER_LENGTH_OFFSET = 24;
126  static const uint32_t MAGIC_OFFSET = 28;
128  static const uint32_t REGISTER1_OFFSET = 32;
130  static const uint32_t TRAILER_POSITION_OFFSET = 40;
132  static const uint32_t INT1_OFFSET = 48;
134  static const uint32_t INT2_OFFSET = 52;
135 
136  // Bits in bit info word
137 
139  static const uint32_t DICTIONARY_BIT = 0x100;
141  static const uint32_t FIRST_EVENT_BIT = 0x200;
143  static const uint32_t TRAILER_WITH_INDEX_BIT = 0x400;
144 
145  private:
146 
148  uint32_t fileId = HIPO_FILE_UNIQUE_WORD;
150  uint32_t fileNumber = 1;
152  uint64_t userRegister = 0L;
154  uint64_t trailerPosition = 0L;
156  uint32_t userIntFirst = 0;
158  uint32_t userIntSecond = 0;
160  size_t position = 0;
161 
163  uint32_t entries = 0;
165  uint32_t bitInfo = 0;
167  uint32_t headerLength = HEADER_SIZE_BYTES;
169  uint32_t headerLengthWords = HEADER_SIZE_WORDS;
171  uint32_t userHeaderLength = 0;
173  uint32_t userHeaderLengthWords = 0;
175  uint32_t indexLength = 0;
176 
179  uint32_t totalLength = HEADER_SIZE_BYTES;
180 
183  uint32_t headerVersion = 6;
185  uint32_t headerMagicWord = HEADER_MAGIC;
186 
190  uint32_t userHeaderLengthPadding = 0;
191 
194 
196  HeaderType headerType {HeaderType::HIPO_FILE};
197 
198  public:
199 
200  FileHeader();
201  FileHeader(const FileHeader & header);
202  explicit FileHeader(bool isEvio);
203  ~FileHeader() = default;
204 
205  void copy(const FileHeader & head);
206  void reset();
207 
208  private:
209 
210  void bitInfoInit();
211  void decodeBitInfoWord(uint32_t word);
212  void setUserHeaderLengthPadding(uint32_t padding);
213 
214  public:
215 
216  // Getters
217 
218  const ByteOrder & getByteOrder() const;
219  const HeaderType & getHeaderType() const;
220 
221  uint32_t getFileNumber() const;
222  uint32_t getFileId() const;
223  uint32_t getVersion() const;
224  uint32_t getEntries() const;
225 
226  uint32_t getLength() const;
227  uint32_t getIndexLength() const;
228  uint32_t getHeaderLength() const;
229 
230  uint32_t getUserHeaderLength() const;
231  uint32_t getUserHeaderLengthWords() const;
232  uint32_t getUserHeaderLengthPadding() const;
233 
234  uint32_t getUserIntFirst() const;
235  uint32_t getUserIntSecond() const;
236  uint64_t getUserRegister() const;
237 
238  size_t getPosition() const;
239  size_t getTrailerPosition() const;
240 
241  //--------------------
242  // Bit info methods
243  //--------------------
244 
245  uint32_t getBitInfoWord() const;
246  void setBitInfoWord(uint32_t word);
247  uint32_t setBitInfo(bool haveFirst, bool haveDictionary, bool haveTrailerWithIndex);
248  static uint32_t generateBitInfoWord(uint32_t version, bool hasDictionary,
249  bool hasFirst, bool trailerWithIndex,
250  uint32_t headerType = 1);
251 
252  uint32_t hasFirstEvent(bool hasFirst);
253  bool hasFirstEvent() const;
254 
255  uint32_t hasDictionary(bool hasDictionary);
256  bool hasDictionary() const;
257 
259  bool hasTrailerWithIndex() const;
260 
261  bool hasUserHeader() const;
262  bool hasIndex() const;
263 
264  static bool hasFirstEvent(uint32_t bitInfo);
265  static bool hasDictionary(uint32_t bitInfo);
266  static bool hasTrailerWithIndex(uint32_t bitInfo);
267 
268  // Setters
269 
270  FileHeader & setFileNumber(uint32_t num);
271  FileHeader & setUserRegister(uint64_t val);
272  FileHeader & setUserIntFirst(uint32_t val);
273  FileHeader & setUserIntSecond(uint32_t val);
274  FileHeader & setHeaderType(HeaderType & type);
275  FileHeader & setPosition(size_t pos);
276  FileHeader & setIndexLength(uint32_t length);
277  FileHeader & setEntries(uint32_t n);
278  FileHeader & setUserHeaderLength(uint32_t length);
279  FileHeader & setHeaderLength(uint32_t length);
280  FileHeader & setLength(uint32_t length);
281 
282 
283  // Writing, reading, printing
284  void writeHeader(ByteBuffer & buf, size_t off);
285  void writeHeader(std::shared_ptr<ByteBuffer> & buf, size_t off = 0);
286  void readHeader(ByteBuffer & buffer, size_t offset = 0);
287  void readHeader(std::shared_ptr<ByteBuffer> & buffer, size_t offset = 0);
288 
289  std::string toString() const;
290  };
291 
292 }
293 
294 
295 #endif //EVIO_6_0_FILEHEADER_H
static const HeaderType HIPO_FILE
Header for an hipo file.
Definition: HeaderType.h:41
bool hasIndex() const
Does this file have a valid index of record lengths immediately following header? There should be at ...
Definition: FileHeader.cpp:446
static const uint32_t INT2_OFFSET
Byte offset from beginning of header to the user integer #2.
Definition: FileHeader.h:134
uint32_t getHeaderLength() const
Get the length of this header data in bytes.
Definition: FileHeader.cpp:210
void writeHeader(ByteBuffer &buf, size_t off)
Writes the file (not record!) header into the given byte buffer.
Definition: FileHeader.cpp:591
FileHeader & setUserIntFirst(uint32_t val)
Set the first user integer.
Definition: FileHeader.cpp:475
bool hasFirstEvent() const
Does this header have a first event in the file header?
Definition: FileHeader.cpp:352
static const uint32_t REGISTER1_OFFSET
Byte offset from beginning of header to the user register #1.
Definition: FileHeader.h:128
static uint32_t generateBitInfoWord(uint32_t version, bool hasDictionary, bool hasFirst, bool trailerWithIndex, uint32_t headerType=1)
Calculates the bit info (6th) word of this header which has the version number in the lowest 8 bits (...
Definition: FileHeader.cpp:315
FileHeader & setPosition(size_t pos)
Set the position of this record in a file.
Definition: FileHeader.cpp:499
FileHeader & setUserRegister(uint64_t val)
Set the first user register.
Definition: FileHeader.cpp:467
static const uint32_t TRAILER_POSITION_OFFSET
Byte offset from beginning of header to write trailer position.
Definition: FileHeader.h:130
uint32_t getEntries() const
Get the number of events or entries in index.
Definition: FileHeader.cpp:175
static const uint32_t HEADER_LENGTH_OFFSET
Byte offset from beginning of header to the header length.
Definition: FileHeader.h:116
FileHeader & setUserIntSecond(uint32_t val)
Set the second user integer.
Definition: FileHeader.cpp:483
void copy(const FileHeader &head)
Copy the contents of the arg into this object.
Definition: FileHeader.cpp:51
void readHeader(ByteBuffer &buffer, size_t offset=0)
Reads the file header information from a byte buffer and validates it by checking the magic word (8th...
Definition: FileHeader.cpp:635
uint32_t getUserHeaderLengthWords() const
Get the length of the user-defined header in words.
Definition: FileHeader.cpp:189
static const uint32_t BIT_INFO_OFFSET
Byte offset from beginning of header to bit info word.
Definition: FileHeader.h:122
static const uint32_t TRAILER_WITH_INDEX_BIT
10th bit set in bitInfo word in file header means file trailer with index array exists.
Definition: FileHeader.h:143
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
static const uint32_t EVIO_FILE_UNIQUE_WORD
First word in every Evio file for identification purposes.
Definition: FileHeader.h:101
bool hasDictionary() const
Does this header have a dictionary in the file header?
Definition: FileHeader.cpp:386
uint32_t getBitInfoWord() const
Get the bit info word.
Definition: FileHeader.cpp:263
uint32_t getLength() const
Get the total length of header + index + user header (including padding) in bytes.
Definition: FileHeader.cpp:218
uint32_t getFileId() const
Get the file id.
Definition: FileHeader.cpp:133
static const uint32_t HEADER_SIZE_WORDS
Number of 32-bit words in a normal sized header.
Definition: FileHeader.h:103
static const uint32_t HEADER_MAGIC
Magic number used to track endianness.
Definition: FileHeader.h:107
FileHeader & setHeaderLength(uint32_t length)
Set the this header&#39;s length in bytes &amp; words.
Definition: FileHeader.cpp:560
bool hasTrailerWithIndex() const
Does this file have a trailer with a record length index?
Definition: FileHeader.cpp:420
FileHeader & setLength(uint32_t length)
Set the total length in bytes, header + index + user header.
Definition: FileHeader.cpp:576
static const uint32_t FILE_ID_OFFSET
Byte offset from beginning of header to the file id.
Definition: FileHeader.h:112
uint32_t getVersion() const
Get the Evio format version number.
Definition: FileHeader.cpp:196
std::string toString() const
Returns a string representation of the record.
Definition: FileHeader.cpp:715
const HeaderType & getHeaderType() const
Get the type of header this is.
Definition: FileHeader.cpp:119
static const uint32_t DICTIONARY_BIT
8th bit set in bitInfo word in record/file header means contains dictionary.
Definition: FileHeader.h:139
size_t getTrailerPosition() const
Get the trailer&#39;s (trailing header&#39;s) file position in bytes.
Definition: FileHeader.cpp:147
uint64_t getUserRegister() const
Get the user register value.
Definition: FileHeader.cpp:140
static const uint32_t USER_LENGTH_OFFSET
Byte offset from beginning of header to the user header length.
Definition: FileHeader.h:124
uint32_t getIndexLength() const
Get the length of the index array in bytes.
Definition: FileHeader.cpp:203
void setBitInfoWord(uint32_t word)
Set the bit info word and related values.
Definition: FileHeader.cpp:270
static const uint32_t RECORD_COUNT_OFFSET
Byte offset from beginning of header to the record count.
Definition: FileHeader.h:118
uint32_t getUserIntFirst() const
Get the first user integer value.
Definition: FileHeader.cpp:154
uint32_t getUserHeaderLengthPadding() const
Get the user header&#39;s padding - the number of bytes required to bring uncompressed user header to 4-b...
Definition: FileHeader.cpp:226
uint32_t getUserIntSecond() const
Get the second user integer value.
Definition: FileHeader.cpp:161
static const uint32_t HIPO_FILE_UNIQUE_WORD
First word in every HIPO file for identification purposes.
Definition: FileHeader.h:99
~FileHeader()=default
static const uint32_t MAGIC_OFFSET
Byte offset from beginning of header to the record length.
Definition: FileHeader.h:126
FileHeader & setEntries(uint32_t n)
Set the number of record entries.
Definition: FileHeader.cpp:522
uint32_t setBitInfo(bool haveFirst, bool haveDictionary, bool haveTrailerWithIndex)
Set the bit info word for a file header.
Definition: FileHeader.cpp:284
Definition: FileHeader.h:91
uint32_t getFileNumber() const
Get the file number or split number.
Definition: FileHeader.cpp:126
static const uint32_t FIRST_EVENT_BIT
9th bit set in bitInfo word in file header means every split file has same first event.
Definition: FileHeader.h:141
void reset()
Reset most internal variables (not file id &amp; header type).
Definition: FileHeader.cpp:78
static const uint32_t INT1_OFFSET
Byte offset from beginning of header to the user integer #1.
Definition: FileHeader.h:132
FileHeader()
Default, no-arg constructor.
Definition: FileHeader.cpp:18
static const uint32_t INDEX_ARRAY_OFFSET
Byte offset from beginning of header to the index array length.
Definition: FileHeader.h:120
size_t getPosition() const
Get the position of this record in a file.
Definition: FileHeader.cpp:168
FileHeader & setUserHeaderLength(uint32_t length)
Set the user-defined header&#39;s length in bytes &amp; words and the padding.
Definition: FileHeader.cpp:531
static const uint32_t FILE_NUMBER_OFFSET
Byte offset from beginning of header to the file number.
Definition: FileHeader.h:114
FileHeader & setHeaderType(HeaderType &type)
Set this header&#39;s type.
Definition: FileHeader.cpp:491
FileHeader & setFileNumber(uint32_t num)
Set the file number which is the split number starting at 1.
Definition: FileHeader.cpp:459
uint32_t getUserHeaderLength() const
Get the length of the user-defined header in bytes.
Definition: FileHeader.cpp:182
bool hasUserHeader() const
Is this header followed by a user header?
Definition: FileHeader.cpp:437
const ByteOrder & getByteOrder() const
Get the byte order of the file this header was read from.
Definition: FileHeader.cpp:112
static const uint32_t HEADER_SIZE_BYTES
Number of bytes in a normal sized header.
Definition: FileHeader.h:105
FileHeader & setIndexLength(uint32_t length)
Set the length of the index array in bytes.
Definition: FileHeader.cpp:509
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61
Numerical values associated with types of a file or record header.
Definition: HeaderType.h:32