HIPO  4.3.0
High Performance Output data format for experimental physics
record.h
Go to the documentation of this file.
1 //******************************************************************************
2 //* ██╗ ██╗██╗██████╗ ██████╗ ██╗ ██╗ ██████╗ *
3 //* ██║ ██║██║██╔══██╗██╔═══██╗ ██║ ██║ ██╔═████╗ *
4 //* ███████║██║██████╔╝██║ ██║ ███████║ ██║██╔██║ *
5 //* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║ *
6 //* ██║ ██║██║██║ ╚██████╔╝ ██║██╗╚██████╔╝ *
7 //* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚═╝╚═╝ ╚═════╝ *
8 //************************ Jefferson National Lab (2017) ***********************
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  */
30 
31 /*
32  * File: record.h
33  * Author: gavalian
34  *
35  * Created on April 11, 2017, 4:47 PM
36  */
37 
44 
45 #ifndef HIPORECORD_H
46 #define HIPORECORD_H
47 
48 #include <iostream>
49 #include <vector>
50 #include <string>
51 #include <cstdint>
52 #include <cstdio>
53 #include <cstdlib>
54 #include <fstream>
55 
56 #include "event.h"
57 #include "utils.h"
58 
59 namespace hipo {
60 
62 
68  typedef struct recordHeader_t {
70  int recordLength{};
74  int headerLength{};
78  int bitInfo{};
83 
85 
91  class data {
92  private:
93  const char *data_ptr{};
94  int data_size{};
95  int data_endianness{};
96  int data_offset{};
97  int data_type;
98 
99  public:
101  data(){ data_ptr = nullptr; data_size = 0;}
103  ~data()= default;
104 
106  void setDataPtr(const char *__ptr){ data_ptr = __ptr;}
108  void setDataSize(int __size){ data_size = __size;}
110  void setDataOffset(int __offset) { data_offset = __offset;}
112  void setDataEndianness(int __endianness) { data_endianness = __endianness;}
114  void setDataType(int __type){ data_type = __type;}
116  const uint32_t *getEvioPtr(){ return reinterpret_cast<const uint32_t *>(data_ptr);}
118  int getEvioSize(){ return (int) data_size/4 ;}
120  const char *getDataPtr(){ return data_ptr;}
122  int getDataSize(){ return data_size;}
124  int getDataType(){ return data_type;}
126  int getDataEndianness(){ return data_endianness;}
128  int getDataOffset(){ return data_offset;}
129  };
130 
132 
139  class dataframe {
140  private:
141  std::vector<char> dataBuffer;
142  recordHeader_t recordHeader{};
143  int maxEvents = 50;
144  int maxSize = 512*1024;
145 
146  public:
154  dataframe(int max_events, int max_size);
157 
162  void init(const char *ptr);
168  bool addEvent(hipo::event &event);
175  int getEventAt(int pos, hipo::event &event);
177  int count();
179  int size();
181  void reset();
183  const char *buffer(){ return &dataBuffer[0];}
184 
186  void summary();
187  };
188 
190 
196  class record {
197 
198  private:
199 
200  //std::vector< std::vector<char> > eventBuffer;
201  std::vector<char> recordHeaderBuffer;
202  recordHeader_t recordHeader{};
203 
204  std::vector<char> recordBuffer;
205  std::vector<char> recordCompressedBuffer;
206 
207  hipo::benchmark readBenchmark;
208  hipo::benchmark unzipBenchmark;
209  hipo::benchmark indexBenchmark;
210 
211  char *getUncompressed(const char *data, int dataLength, int dataLengthUncompressed);
212  int getUncompressed(const char *data, char *dest, int dataLength, int dataLengthUncompressed);
213  void showBuffer(const char *data, int wrapping, int maxsize);
214 
215  public:
216 
221 
227  void read(hipo::bank &b, int event);
234  void readRecord(std::ifstream &stream, long position, int dataOffset);
241  void readRecord__(std::ifstream &stream, long position, long recordLength);
250  bool readRecord(std::ifstream &stream, long position, int dataOffset, long inputSize);
252  int getEventCount();
255 
263  void getColumn(hipo::data &data,int column, hipo::bank &bank, int event);
271  void getColumn(hipo::data &data,const char* column, hipo::bank &bank, int event);
272 
278  void readEvent( std::vector<char> &vec, int index);
284  void readHipoEvent(hipo::event &event, int index);
285 
291  void getData( hipo::data &data, int index);
292  //void getBank( hipo::bank &bank, int index);
293 
298  void getEventsMap(std::vector<std::pair<int,int>> &emap);
299 
301  hipo::benchmark &getReadBenchmark(){ return readBenchmark;}
303  hipo::benchmark &getUnzipBenchmark(){ return unzipBenchmark;}
305  hipo::benchmark &getIndexBenchmark(){ return indexBenchmark;}
306  };
307 }
308 #endif /* HIPORECORD_H */
Represents a HIPO bank, a tabular data structure with rows and typed columns.
Definition: bank.h:352
Simple timer for measuring code performance.
Definition: utils.h:157
Lightweight wrapper for a raw data pointer with type, size, and offset metadata.
Definition: record.h:91
void setDataSize(int __size)
Set the data size in bytes.
Definition: record.h:108
void setDataOffset(int __offset)
Set the byte offset into the source buffer.
Definition: record.h:110
void setDataPtr(const char *__ptr)
Set the raw data pointer.
Definition: record.h:106
int getDataEndianness()
Return the endianness indicator.
Definition: record.h:126
const char * getDataPtr()
Return the raw data pointer.
Definition: record.h:120
int getEvioSize()
Return the data size in 4-byte (uint32_t) units.
Definition: record.h:118
data()
Construct an empty data object with a null pointer and zero size.
Definition: record.h:101
int getDataOffset()
Return the byte offset into the source buffer.
Definition: record.h:128
const uint32_t * getEvioPtr()
Return the data pointer cast to a uint32_t pointer (EVIO format).
Definition: record.h:116
int getDataType()
Return the data type identifier.
Definition: record.h:124
void setDataType(int __type)
Set the data type identifier.
Definition: record.h:114
int getDataSize()
Return the data size in bytes.
Definition: record.h:122
~data()=default
Default destructor.
void setDataEndianness(int __endianness)
Set the endianness indicator for the data.
Definition: record.h:112
Container for multiple events packed into a single frame.
Definition: record.h:139
bool addEvent(hipo::event &event)
Append an event to the dataframe.
Definition: record.cpp:569
void reset()
Clear all events and reset the dataframe to an empty state.
Definition: record.cpp:556
int getEventAt(int pos, hipo::event &event)
Retrieve the event at the given position.
Definition: record.cpp:583
void init(const char *ptr)
Initialize the dataframe from an existing raw buffer.
Definition: record.cpp:589
int count()
Return the number of events currently stored in the frame.
Definition: record.cpp:595
dataframe()
Construct an empty dataframe with default limits.
Definition: record.h:148
void summary()
Print a summary of the dataframe contents to standard output.
Definition: record.cpp:598
const char * buffer()
Return a pointer to the underlying data buffer.
Definition: record.h:183
int size()
Return the total size of the internal buffer in bytes.
Definition: record.cpp:596
~dataframe()
Default destructor.
Definition: record.h:156
Represents a HIPO event, a container for multiple structures/banks.
Definition: event.h:77
Represents a single HIPO record containing multiple events.
Definition: record.h:196
hipo::benchmark & getUnzipBenchmark()
Return a reference to the decompression benchmark.
Definition: record.h:303
void readEvent(std::vector< char > &vec, int index)
Read raw event bytes into a vector.
Definition: record.cpp:401
hipo::benchmark & getIndexBenchmark()
Return a reference to the index-building benchmark.
Definition: record.h:305
record()
Construct an empty record.
int getEventCount()
Return the number of events stored in this record.
Definition: record.cpp:393
void getEventsMap(std::vector< std::pair< int, int >> &emap)
Build a map of event positions (offset, length pairs).
Definition: record.cpp:463
void read(hipo::bank &b, int event)
Read a specific bank from a given event in this record.
Definition: record.cpp:424
hipo::benchmark & getReadBenchmark()
Return a reference to the read-operation benchmark.
Definition: record.h:301
int getRecordSizeCompressed()
Return the compressed size of this record in bytes.
Definition: record.cpp:279
void readRecord(std::ifstream &stream, long position, int dataOffset)
Read a record from a file stream at the specified position.
Definition: record.cpp:61
void readHipoEvent(hipo::event &event, int index)
Read an event and populate a hipo::event object.
Definition: record.cpp:485
~record()
Destructor.
void getData(hipo::data &data, int index)
Get a lightweight data handle for a specific event.
Definition: record.cpp:411
void getColumn(hipo::data &data, int column, hipo::bank &bank, int event)
Extract a single column from a bank for a given event.
Definition: record.cpp:435
void readRecord__(std::ifstream &stream, long position, long recordLength)
Read a record using its total length (internal variant).
Definition: record.cpp:283
HIPO event container and manipulation interface.
struct hipo::recordHeader_t recordHeader_t
Header structure for a HIPO record.
Definition: bank.cpp:47
Header structure for a HIPO record.
Definition: record.h:68
int dataEndianness
Endianness of the stored data.
Definition: record.h:81
int bitInfo
Bit-packed information field.
Definition: record.h:78
int userHeaderLengthPadding
Padding added to the user header length.
Definition: record.h:77
int userHeaderLength
User header length in bytes.
Definition: record.h:76
int compressedLengthPadding
Padding added to the compressed data length.
Definition: record.h:80
int compressionType
Compression algorithm identifier.
Definition: record.h:79
int indexDataLength
Length of the index buffer in bytes.
Definition: record.h:75
int recordDataLength
Length of the uncompressed data buffer.
Definition: record.h:71
int headerLength
Length of the record header buffer.
Definition: record.h:74
int numberOfEvents
Number of events (data buckets) in the data buffer.
Definition: record.h:73
int signatureString
Identifier string "HREC" (int = 0x43455248).
Definition: record.h:69
int recordLength
Total length of the record, including the index array.
Definition: record.h:70
int recordDataLengthCompressed
Length of the compressed data buffer.
Definition: record.h:72
Utility functions and benchmark timer for HIPO library operations.