evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventHeaderParser.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_EVENTHEADERPARSER_H
12 #define EVIO_6_0_EVENTHEADERPARSER_H
13 
14 
15 #include <cstring>
16 #include <memory>
17 #include <cstring>
18 
19 
20 #include "ByteOrder.h"
21 #include "BankHeader.h"
22 #include "SegmentHeader.h"
23 #include "TagSegmentHeader.h"
24 #include "EvioNode.h"
25 
26 
27 namespace evio {
28 
29 
41 
42  public:
43 
53  static std::shared_ptr<BankHeader> createBankHeader(uint8_t * bytes, ByteOrder const & byteOrder) {
54 
55  std::shared_ptr<BankHeader> header = std::make_shared<BankHeader>();
56 
57  // Does the length make sense?
58  uint32_t len = 0;
59  Util::toIntArray(reinterpret_cast<char *>(bytes), 4, byteOrder, &len);
60 
61  header->setLength(len);
62  bytes += 4;
63 
64  // Read and parse second header word
65  uint32_t word = 0;
66  Util::toIntArray(reinterpret_cast<char *>(bytes), 4, byteOrder, &word);
67 
68  header->setTag(word >> 16);
69  int dt = (word >> 8) & 0xff;
70  int type = dt & 0x3f;
71  uint8_t padding = dt >> 6;
72  header->setDataType(type);
73  header->setPadding(padding);
74  header->setNumber(word);
75 
76  return header;
77  }
78 
79 
89  static std::shared_ptr<SegmentHeader> createSegmentHeader(uint8_t * bytes, ByteOrder const & byteOrder) {
90 
91  std::shared_ptr<SegmentHeader> header = std::make_shared<SegmentHeader>();
92 
93  // Read and parse header word
94  uint32_t word = 0;
95  Util::toIntArray(reinterpret_cast<char *>(bytes), 4, byteOrder, &word);
96 
97  uint32_t len = word & 0xffff;
98  header->setLength(len);
99 
100  int dt = (word >> 16) & 0xff;
101  int type = dt & 0x3f;
102  int padding = dt >> 6;
103  header->setDataType(type);
104  header->setPadding(padding);
105  header->setTag(word >> 24);
106 
107  return header;
108  }
109 
110 
120  static std::shared_ptr<TagSegmentHeader> createTagSegmentHeader(uint8_t * bytes, ByteOrder const & byteOrder) {
121 
122  std::shared_ptr<TagSegmentHeader> header = std::make_shared<TagSegmentHeader>();
123 
124  // Read and parse header word
125  uint32_t word = 0;
126  Util::toIntArray(reinterpret_cast<char *>(bytes), 4, byteOrder, &word);
127 
128  uint32_t len = word & 0xffff;
129  header->setLength(len);
130  header->setDataType((word >> 16) & 0xf);
131  header->setTag(word >> 20);
132 
133  return header;
134  }
135 
136 
154  static void swapBankHeader(std::shared_ptr<EvioNode> & node,
155  std::shared_ptr<ByteBuffer> & srcBuffer,
156  std::shared_ptr<ByteBuffer> & destBuffer,
157  uint32_t srcPos, uint32_t destPos) {
158  swapBankHeader(*(node.get()), *(srcBuffer.get()), *(destBuffer.get()), srcPos, destPos);
159  }
160 
161 
179  static void swapBankHeader(EvioNode & node, ByteBuffer & srcBuffer, ByteBuffer & destBuffer,
180  uint32_t srcPos, uint32_t destPos) {
181 
182  // Check endianness
183  if (srcBuffer.order() == destBuffer.order()) {
184  throw evio::EvioException("src & dest buffers need different byte order for swapping");
185  }
186 
187  // Read & swap first bank header word
188  uint32_t length = srcBuffer.getInt(srcPos);
189  destBuffer.putInt(destPos, length);
190  srcPos += 4;
191  destPos += 4;
192 
193  // Read & swap second bank header word
194  uint32_t word = srcBuffer.getInt(srcPos);
195  destBuffer.putInt(destPos, word);
196 
197  node.tag = (word >> 16) & 0xffff;
198  uint32_t dt = (word >> 8) & 0xff;
199 
200  node.dataType = dt & 0x3f;
201  node.pad = dt >> 6;
202  node.num = word & 0xff;
203  node.len = length;
204  node.pos = destPos - 4;
205  node.dataPos = destPos + 4;
206  node.dataLen = length - 1;
207  }
208 
209 
227  static void swapSegmentHeader(std::shared_ptr<EvioNode> & node,
228  std::shared_ptr<ByteBuffer> & srcBuffer,
229  std::shared_ptr<ByteBuffer> & destBuffer,
230  uint32_t srcPos, uint32_t destPos) {
231  swapSegmentHeader(*(node.get()), *(srcBuffer.get()), *(destBuffer.get()), srcPos, destPos);
232  }
233 
234 
252  static void swapSegmentHeader(EvioNode & node, ByteBuffer & srcBuffer, ByteBuffer & destBuffer,
253  uint32_t srcPos, uint32_t destPos) {
254 
255  if (srcBuffer.order() == destBuffer.order()) {
256  throw evio::EvioException("src & dest buffers need different byte order for swapping");
257  }
258 
259  // Read & swap segment header word
260  uint32_t word = srcBuffer.getInt(srcPos);
261  destBuffer.putInt(destPos, word);
262 
263  node.tag = (word >> 24) & 0xff;
264  uint32_t dt = (word >> 16) & 0xff;
265  node.dataType = dt & 0x3f;
266  node.pad = dt >> 6;
267  node.len = word & 0xffff;
268  node.num = 0;
269  node.pos = destPos;
270  node.dataPos = destPos + 4;
271  node.dataLen = node.len;
272  }
273 
274 
291  static void swapTagSegmentHeader(std::shared_ptr<EvioNode> & node,
292  std::shared_ptr<ByteBuffer> & srcBuffer,
293  std::shared_ptr<ByteBuffer> & destBuffer,
294  uint32_t srcPos, uint32_t destPos) {
295  swapTagSegmentHeader(*(node.get()), *(srcBuffer.get()), *(destBuffer.get()), srcPos, destPos);
296  }
297 
298 
315  static void swapTagSegmentHeader(EvioNode & node, ByteBuffer & srcBuffer, ByteBuffer & destBuffer,
316  uint32_t srcPos, uint32_t destPos) {
317 
318  if (srcBuffer.order() == destBuffer.order()) {
319  throw evio::EvioException("src & dest buffers need different byte order for swapping");
320  }
321 
322  // Read & swap tagsegment header word
323  uint32_t word = srcBuffer.getInt(srcPos);
324  destBuffer.putInt(destPos, word);
325 
326  node.tag = (word >> 20) & 0xfff;
327  node.dataType = (word >> 16) & 0xf;
328  node.len = word & 0xffff;
329  node.num = 0;
330  node.pad = 0;
331  node.pos = destPos;
332  node.dataPos = destPos + 4;
333  node.dataLen = node.len;
334  }
335 
336 
337  };
338 
339 
340 }
341 
342 #endif //EVIO_6_0_EVENTHEADERPARSER_H
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
const ByteOrder & order() const
Get the byte order of the data.
Definition: ByteBuffer.cpp:466
static void swapBankHeader(std::shared_ptr< EvioNode > &node, std::shared_ptr< ByteBuffer > &srcBuffer, std::shared_ptr< ByteBuffer > &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio bank header.
Definition: EventHeaderParser.h:154
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
The createXXX methods exist is in the EventParser class in the original Java, but must be moved in C+...
Definition: EventHeaderParser.h:40
ByteBuffer & putInt(uint32_t val)
Relative put method for writing an int value.
Definition: ByteBuffer.cpp:1570
Exception class for Evio software package.
Definition: EvioException.h:29
static void swapSegmentHeader(std::shared_ptr< EvioNode > &node, std::shared_ptr< ByteBuffer > &srcBuffer, std::shared_ptr< ByteBuffer > &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio segment header.
Definition: EventHeaderParser.h:227
static std::shared_ptr< BankHeader > createBankHeader(uint8_t *bytes, ByteOrder const &byteOrder)
Create a bank header from the first eight bytes of the data array.
Definition: EventHeaderParser.h:53
static void swapTagSegmentHeader(std::shared_ptr< EvioNode > &node, std::shared_ptr< ByteBuffer > &srcBuffer, std::shared_ptr< ByteBuffer > &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio tagsegment header.
Definition: EventHeaderParser.h:291
static void swapSegmentHeader(EvioNode &node, ByteBuffer &srcBuffer, ByteBuffer &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio segment header.
Definition: EventHeaderParser.h:252
This class is used to store relevant info about an evio container (bank, segment, or tag segment)...
Definition: EvioNode.h:41
static std::shared_ptr< TagSegmentHeader > createTagSegmentHeader(uint8_t *bytes, ByteOrder const &byteOrder)
Create a tag segment header from the first four bytes of the data array.
Definition: EventHeaderParser.h:120
int32_t getInt() const
Relative get method for reading an int value.
Definition: ByteBuffer.cpp:1074
static void toIntArray(char const *data, uint32_t dataLen, const ByteOrder &byteOrder, uint32_t *dest)
Turn byte array into an int array.
Definition: Util.h:133
static std::shared_ptr< SegmentHeader > createSegmentHeader(uint8_t *bytes, ByteOrder const &byteOrder)
Create a segment header from the first four bytes of the data array.
Definition: EventHeaderParser.h:89
static void swapBankHeader(EvioNode &node, ByteBuffer &srcBuffer, ByteBuffer &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio bank header.
Definition: EventHeaderParser.h:179
static void swapTagSegmentHeader(EvioNode &node, ByteBuffer &srcBuffer, ByteBuffer &destBuffer, uint32_t srcPos, uint32_t destPos)
This method reads and swaps an evio tagsegment header.
Definition: EventHeaderParser.h:315