HIPO  4.3.0
High Performance Output data format for experimental physics
node.h
Go to the documentation of this file.
1 //******************************************************************************
2 //* ██╗ ██╗██╗██████╗ ██████╗ ███████╗ ██████╗
3 //* ██║ ██║██║██╔══██╗██╔═══██╗ ██╔════╝ ██╔═████╗
4 //* ███████║██║██████╔╝██║ ██║ ███████╗ ██║██╔██║
5 //* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║
6 //* ██║ ██║██║██║ ╚██████╔╝ ███████║██╗╚██████╔╝
7 //* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝
8 //************************ Jefferson National Lab (2023) ***********************
9 /*
10  * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission
11  * to use, copy, modify, and distribute this software and its documentation
12  * for educational, research, and not-for-profit purposes, without fee and
13  * without a signed licensing agreement.
14  *
15  * IN NO EVENT SHALL JLAB BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL
16  * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
17  * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JLAB HAS
18  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  *
20  * JLAB SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE. THE HIPO DATA FORMAT SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
23  * ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". JLAB HAS NO OBLIGATION TO
24  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25  *
26  * This software was developed under the United States Government license.
27  * For more information contact author at gavalian@jlab.org
28  * Department of Experimental Nuclear Physics, Jefferson Lab.
29  */
35 
36 /*******************************************************************************
37  * File: bank.h
38  * Author: gavalian
39  *
40  * Created on April 12, 2017, 10:14 AM
41  */
42 
43 #ifndef HIPO_NODE_H
44 #define HIPO_NODE_H
45 #include <iostream>
46 #include <vector>
47 #include <cstring>
48 #include <cstdint>
49 #include <cstdio>
50 #include <cstdlib>
51 #include <map>
52 #include <tuple>
53 #include "constants.h"
54 
55 namespace hipo {
56 
68  class node {
69 
70  private:
71 
72  std::vector<char> nodeBuffer;
73  const char *nodePointer;
74 
75  protected:
76 
84  void create(int group, int item, int type, int size);
85 
91  void init(const char *b, int length){ allocate(length); memcpy(const_cast<char *>(nodePointer),b,length);}
92 
94  void initEmpty();
95 
96  public:
97 
99  node(){ allocate(8); nodePointer = &nodeBuffer[0];}
100 
105  node(std::tuple<int,int,int,int> params){
106  create(std::get<0>(params),std::get<1>(params), std::get<2>(params), std::get<3>(params));
107  }
108 
113  node(int size){ allocate(8+size); nodePointer = &nodeBuffer[0];}
114 
115  virtual ~node()= default;
116 
121  void assign(std::tuple<int,int,int,int> params );
122 
128  bool allocate(int size){
129  if(((int) nodeBuffer.size()) <size){
130  nodeBuffer.resize(size+8); nodePointer = &nodeBuffer[0];
131  } return true;
132  }
133 
135  virtual void reset(){ setDataLength(0);}
136 
138  int size() const noexcept{
139  int length = *reinterpret_cast<const uint32_t *>(nodePointer+4);
140  return length&STRUCT_SIZE_MASK;
141  //return getHeaderSize()+getDataSize();
142  }
143 
145  int capacity() const noexcept{
146  return (int) nodeBuffer.size();
147  //return getHeaderSize()+getDataSize();
148  }
149 
151  int formatLength() const noexcept {
152  int length = *reinterpret_cast<const uint32_t *>(nodePointer+4);
153  return (length>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
154  }
155 
160  void setFormatLength(int length){
161  if(length<128){
162  char *dest = const_cast<char *>(nodePointer);
163  *reinterpret_cast<uint32_t *>(dest+4) = ((length<<STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_MASK)|(length&STRUCT_SIZE_MASK);
164  } else { printf("node::setFormatLength: error >>> the format length can not exceed 128, you tried to set it to %d\n",length);}
165  }
166 
171  void setDataLength(int length){
172  int word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
173  int flength = (word>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
174  setNodeLength(flength+length);
175  /*int word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
176  int flength = (word>>24)&0x000000FF;
177  int totalLength = length + flength;
178  char *dest = const_cast<char *>(nodePointer);
179  *reinterpret_cast<uint32_t *>(dest+4) = (word&0xFF000000)|(totalLength&0x00FFFFFF);*/
180  }
181 
183  int dataLength() const noexcept {
184  int size = (*reinterpret_cast<const uint32_t *>(nodePointer+4))&STRUCT_SIZE_MASK;
185  int fsize = ((*reinterpret_cast<const uint32_t *>(nodePointer+4))>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
186  return size-fsize;
187  }
188 
190  int nodeLength(){
191  int size = (*reinterpret_cast<const uint32_t *>(nodePointer+4))&STRUCT_SIZE_MASK;
192  return size;
193  }
194 
199  void setNodeLength(int size){
200  if(size<16777215){
201  uint32_t word = *reinterpret_cast<const uint32_t *>(nodePointer+4);
202  uint32_t nodeSize = (word&STRUCT_FORMAT_MASK)|(size&STRUCT_SIZE_MASK);
203  char *dest = const_cast<char *>(nodePointer);
204  *reinterpret_cast<uint32_t *>(dest+4) = nodeSize;
205  } else { printf("node::setNodeLength:: error: the total size of the node exceeds 16777215\n");}
206  }
207 
209  int dataOffset() const noexcept;
210 
212  int group(){return (int) (*reinterpret_cast<const uint16_t *>(nodePointer));}
214  int item(){return (int) (*reinterpret_cast<const uint8_t *>(nodePointer+2));}
216  int type(){return (int) (*reinterpret_cast<const uint8_t *>(nodePointer+3));}
217 
219  const char *pointer(){ return nodePointer;};
220 
222  virtual void show();
223 
228  void setSize(int size);
229  //void setHeaderSize(int size);
230  //void setDataSize(int size);
231 
234  int getIntAt ( int index) const noexcept {
235  return *reinterpret_cast<const int32_t*>(nodePointer + index + dataOffset());
236  }
237 
240  int16_t getShortAt ( int index) const noexcept {
241  return *reinterpret_cast<const int16_t*>(nodePointer + index + dataOffset());
242  }
245  int8_t getByteAt ( int index) const noexcept {
246  return *reinterpret_cast<const int8_t*>(nodePointer + index + dataOffset());
247  }
250  float getFloatAt ( int index) const noexcept {
251  return *reinterpret_cast<const float*>(nodePointer + index + dataOffset());
252  }
255  double getDoubleAt( int index) const noexcept {
256  return *reinterpret_cast<const double*>(nodePointer + index + dataOffset());
257  }
260  long getLongAt ( int index) const noexcept {
261  return *reinterpret_cast<const int64_t*>(nodePointer + index + dataOffset());
262  }
263 
264  //std::string getStringAt(int index);
265 
269  void putIntAt(int index, int value){
270  char *a = const_cast<char*>(nodePointer);
271  *reinterpret_cast<int32_t*>(a + index + dataOffset()) = value;
272  }
273 
277  void putShortAt(int index, int16_t value){
278  char *a = const_cast<char*>(nodePointer);
279  *reinterpret_cast<int16_t*>(a + index + dataOffset()) = value;
280  }
281 
285  void putByteAt(int index, int8_t value){
286  char *a = const_cast<char*>(nodePointer);
287  *reinterpret_cast<int8_t*>(a + index + dataOffset()) = value;
288  }
289 
293  void putFloatAt(int index, float value){
294  char *a = const_cast<char*>(nodePointer);
295  *reinterpret_cast<float*>(a + index + dataOffset()) = value;
296  }
297 
301  void putDoubleAt(int index, double value){
302  char *a = const_cast<char*>(nodePointer);
303  *reinterpret_cast<double*>(a + index + dataOffset()) = value;
304  }
305 
309  void putLongAt(int index, int64_t value){
310  char *a = const_cast<char*>(nodePointer);
311  *reinterpret_cast<int64_t*>(a + index + dataOffset()) = value;
312  }
313 
314  //void putStringAt(int index, std::string &str);
315 
317  virtual void notify(){}
318 
319  friend class tuple;
320  friend class event;
321  };
322 
323  inline int node::dataOffset() const noexcept {
324  int fsize = ((*reinterpret_cast<const uint32_t *>(nodePointer+4))>>STRUCT_FORMAT_SHIFT)&STRUCT_FORMAT_BYTE;
325  return 8+fsize;
326  }
327 }
329 #endif /* NODE_H */
Represents a HIPO event, a container for multiple structures/banks.
Definition: event.h:77
Low-level node representing a tagged data element in a HIPO structure.
Definition: node.h:68
void setNodeLength(int size)
Set the total node length in the header word.
Definition: node.h:199
int formatLength() const noexcept
Definition: node.h:151
virtual void reset()
Reset the node by setting the data length to zero.
Definition: node.h:135
int group()
Definition: node.h:212
int item()
Definition: node.h:214
int size() const noexcept
Definition: node.h:138
virtual void show()
Print a summary of this node to stdout.
Definition: node.cpp:87
float getFloatAt(int index) const noexcept
Read a 32-bit float at the given byte offset within the data region.
Definition: node.h:250
void putFloatAt(int index, float value)
Write a 32-bit float at the given byte offset within the data region.
Definition: node.h:293
void assign(std::tuple< int, int, int, int > params)
Re-initialize this node from a parameter tuple.
Definition: node.cpp:65
int capacity() const noexcept
Definition: node.h:145
int8_t getByteAt(int index) const noexcept
Read an 8-bit integer at the given byte offset within the data region.
Definition: node.h:245
int dataOffset() const noexcept
Definition: node.h:323
node()
Default constructor; allocates an 8-byte header.
Definition: node.h:99
int dataLength() const noexcept
Definition: node.h:183
void putShortAt(int index, int16_t value)
Write a 16-bit integer at the given byte offset within the data region.
Definition: node.h:277
void initEmpty()
Initialize the node to an empty state.
Definition: node.cpp:79
void create(int group, int item, int type, int size)
Create a node with the given header fields and allocate storage.
Definition: node.cpp:51
node(int size)
Construct a node with a pre-allocated data region.
Definition: node.h:113
void putByteAt(int index, int8_t value)
Write an 8-bit integer at the given byte offset within the data region.
Definition: node.h:285
const char * pointer()
Definition: node.h:219
long getLongAt(int index) const noexcept
Read a 64-bit integer at the given byte offset within the data region.
Definition: node.h:260
int type()
Definition: node.h:216
int16_t getShortAt(int index) const noexcept
Read a 16-bit integer at the given byte offset within the data region.
Definition: node.h:240
void setSize(int size)
Set the total size of the node.
Definition: node.cpp:60
double getDoubleAt(int index) const noexcept
Read a 64-bit double at the given byte offset within the data region.
Definition: node.h:255
void init(const char *b, int length)
Initialize the node by copying raw bytes into the internal buffer.
Definition: node.h:91
void putIntAt(int index, int value)
Write a 32-bit integer at the given byte offset within the data region.
Definition: node.h:269
node(std::tuple< int, int, int, int > params)
Construct a node from a parameter tuple.
Definition: node.h:105
virtual ~node()=default
void putDoubleAt(int index, double value)
Write a 64-bit double at the given byte offset within the data region.
Definition: node.h:301
void putLongAt(int index, int64_t value)
Write a 64-bit integer at the given byte offset within the data region.
Definition: node.h:309
int getIntAt(int index) const noexcept
Read a 32-bit integer at the given byte offset within the data region.
Definition: node.h:234
virtual void notify()
Callback invoked when the node content is updated (e.g., after deserialization).
Definition: node.h:317
void setDataLength(int length)
Set the data payload length, preserving the format length.
Definition: node.h:171
void setFormatLength(int length)
Set the format descriptor length.
Definition: node.h:160
int nodeLength()
Definition: node.h:190
bool allocate(int size)
Ensure the internal buffer can hold at least size bytes.
Definition: node.h:128
N-tuple writer for storing columnar float data in HIPO files.
Definition: tuple.h:71
Definition: bank.cpp:47
constexpr uint32_t STRUCT_FORMAT_BYTE
Definition: constants.h:129
constexpr int STRUCT_FORMAT_SHIFT
Definition: constants.h:128
constexpr uint32_t STRUCT_FORMAT_MASK
Definition: constants.h:127
constexpr uint32_t STRUCT_SIZE_MASK
Definition: constants.h:126