HIPO  4.3.0
High Performance Output data format for experimental physics
recordbuilder.cpp
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 
11 #include "recordbuilder.h"
12 #include "constants.h"
13 //#include "hipoexceptions.h"
14 
15 #ifdef __LZ4__
16 #include <lz4.h>
17 #endif
18 
19 namespace hipo {
25  bufferIndex.resize(4*defaultNumberOfEvents);
26  bufferEvents.resize(defaultRecordSize);
27  bufferData.resize(defaultRecordSize+4*defaultNumberOfEvents);
28  bufferRecord.resize(defaultRecordSize+4*defaultNumberOfEvents+512*1024);
29  bufferIndexEntries = 0;
30  bufferEventsPosition = 0;
31  }
36  recordbuilder::recordbuilder(int maxEvents, int maxLength){
37  bufferIndex.resize(4*maxEvents);
38  bufferEvents.resize(maxLength);
39  bufferData.resize( maxLength+4*maxEvents + 1024);
40  bufferRecord.resize(maxLength+4*maxEvents+512*1024);
41  bufferIndexEntries = 0;
42  bufferEventsPosition = 0;
43  }
44 
49  return addEvent(evnt.getEventBuffer(),0,evnt.getSize());
50  }
56  bool recordbuilder::addEvent(std::vector<char> &vec, int start, int length){
57  if((bufferEventsPosition+length)>=bufferEvents.size()) return false;
58  if((bufferIndexEntries+1)*4>=bufferIndex.size()) return false;
59  *reinterpret_cast<int*>(&bufferIndex[bufferIndexEntries*4]) = length;
60  bufferIndexEntries++;
61  memcpy(&bufferEvents[bufferEventsPosition],&vec[start],length);
62  bufferEventsPosition += length;
63  return true;
64  }
71  bufferIndexEntries = 0;
72  bufferEventsPosition = 0;
73  }
78  int recordbuilder::getRecordLengthRounding(int bufferSize){
79  if(bufferSize%4==0) return 0;
80  int nwords = bufferSize/4;
81  int nbytes = 4*(nwords+1);
82  return (nbytes-bufferSize);
83  }
88  int nentries = *reinterpret_cast<int*>(&bufferRecord[RH_EVENT_COUNT_OFFSET]);
89  return nentries;
90  }
95  int size = *reinterpret_cast<int*>(&bufferRecord[RH_RECORD_LENGTH_OFFSET]);
96  return size*4;
97  }
98 
100  long wOne = *reinterpret_cast<long*>(&bufferRecord[RH_USER_WORD1_OFFSET]);
101  return wOne;
102  }
103 
105  long wTwo = *reinterpret_cast<long*>(&bufferRecord[RH_USER_WORD2_OFFSET]);
106  return wTwo;
107  }
108 
109  void recordbuilder::setUserWordOne(long userWordOne){
110  bufferUserWordOne = userWordOne;
111  }
112 
113  void recordbuilder::setUserWordTwo(long userWordTwo){
114  bufferUserWordTwo = userWordTwo;
115  }
116 
118  int indexSize = bufferIndexEntries*4;
119  int eventsSize = bufferEventsPosition;
120  memcpy(&bufferData[0],&bufferIndex[0],indexSize);
121  memcpy(&bufferData[indexSize],&bufferEvents[0],eventsSize);
122  int uncompressedSize = indexSize+eventsSize;
123  int compressedSize = compressRecord(uncompressedSize);
124  int rounding = getRecordLengthRounding(compressedSize);
125  int compressedSizeToWrite = compressedSize + rounding;
126  int compressedSizeToWriteWords = compressedSizeToWrite/4;
127  int recordLength = compressedSizeToWrite/4+RECORD_HEADER_WORDS;
128 
129  hipo::utils::writeInt(&bufferRecord[0], RH_RECORD_LENGTH_OFFSET, recordLength); // (1) - record length in words (includes header)
130  hipo::utils::writeInt(&bufferRecord[0], RH_RECORD_NUMBER_OFFSET, 0); // (2) - record #
131  hipo::utils::writeInt(&bufferRecord[0], RH_HEADER_LENGTH_OFFSET, RECORD_HEADER_WORDS); // (3) - record header lenght (in words)
132  hipo::utils::writeInt(&bufferRecord[0], RH_EVENT_COUNT_OFFSET, bufferIndexEntries); // (4) event count in the record
133  hipo::utils::writeInt(&bufferRecord[0], RH_INDEX_ARRAY_LEN_OFFSET, bufferIndexEntries*4); // (5) length of index array in bytes
134  int versionWord = (rounding<<BITINFO_PAD3_SHIFT)|(HIPO_VERSION);
135  hipo::utils::writeInt(&bufferRecord[0], RH_BIT_INFO_OFFSET, versionWord); // (6) record version number
136  hipo::utils::writeInt(&bufferRecord[0], RH_USER_HEADER_LEN_OFFSET, 0); // (7) user header length bytes
137  hipo::utils::writeInt(&bufferRecord[0], RH_MAGIC_NUMBER_OFFSET, HEADER_MAGIC); // (8) magic word
138  hipo::utils::writeInt(&bufferRecord[0], RH_DATA_LENGTH_OFFSET, eventsSize); // (9) magic word
139  int compressionWord = (1<<COMP_TYPE_SHIFT)|(COMP_LENGTH_MASK&compressedSizeToWriteWords);
140  hipo::utils::writeInt(&bufferRecord[0], RH_COMP_WORD_OFFSET, compressionWord);
141  hipo::utils::writeLong(&bufferRecord[0], RH_USER_WORD1_OFFSET, bufferUserWordOne);
142  hipo::utils::writeLong(&bufferRecord[0], RH_USER_WORD2_OFFSET, bufferUserWordTwo);
143 
144  //printf("record::build uncompressed size = %8d, compressed size = %8d, rounding = %4d , compressed FULL = %6d, record size = %6d, version = %X, size = %5X\n",
145  // uncompressedSize,compressedSize, rounding,compressedSizeToWrite, recordLength*4,versionWord,compressionWord);
146  }
151  int recordbuilder::compressRecord(int src_size){
152 
153  #ifdef __LZ4__
154  //(const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
155  int result = LZ4_compress_fast(&bufferData[0],&bufferRecord[RECORD_HEADER_SIZE],src_size,bufferRecord.size(),1);
156 
157  //int result = LZ4_decompress_safe(data,output,dataLength,dataLengthUncompressed);
158  //int result = LZ4_decompress_fast(data,output,dataLengthUncompressed);
159  return result;
160  //printf(" FIRST (%d) = %x %x %x %x\n",result);//,destUnCompressed[0],destUnCompressed[1],
161  //destUnCompressed[2],destUnCompressed[3]);
162  //LZ4_decompress_fast(buffer,destUnCompressed,decompressedLength);
163  //LZ4_uncompress(buffer,destUnCompressed,decompressedLength);
164  #endif
165 
166  #ifndef __LZ4__
167  printf("\n >>>>> LZ4 compression is not supported.");
168  printf("\n >>>>> check if libz4 is installed on your system.");
169  printf("\n >>>>> recompile the library with liblz4 installed.\n");
170  return 0;
171  #endif
172  }
173 }
Represents a HIPO event, a container for multiple structures/banks.
Definition: event.h:77
std::vector< char > & getEventBuffer()
Definition: event.cpp:329
int getSize()
Definition: event.cpp:315
void build()
Compresses accumulated events and builds the final record.
void setUserWordOne(long userWordOne)
Sets the first user-defined word in the record header.
int getRecordSize()
Returns the total size of the built record in bytes.
recordbuilder()
Default constructor with default capacity.
void reset()
Resets the builder, clearing all accumulated events.
long getUserWordTwo()
Returns the second user-defined word stored in the record header.
void setUserWordTwo(long userWordTwo)
Sets the second user-defined word in the record header.
long getUserWordOne()
Returns the first user-defined word stored in the record header.
bool addEvent(std::vector< char > &vec, int start, int length)
Adds an event from a raw byte vector.
int getEntries()
Returns the number of events currently in the record.
static void writeLong(char *buffer, int position, long value)
Writes a 64-bit long into a byte buffer at the given position.
Definition: utils.cpp:75
static void writeInt(char *buffer, int position, int value)
Writes a 32-bit integer into a byte buffer at the given position.
Definition: utils.cpp:70
Definition: bank.cpp:47
constexpr uint32_t COMP_LENGTH_MASK
Definition: constants.h:120
constexpr int BITINFO_PAD3_SHIFT
Definition: constants.h:111
constexpr int RECORD_HEADER_SIZE
Definition: constants.h:15
constexpr int RECORD_HEADER_WORDS
Definition: constants.h:13
constexpr int RH_INDEX_ARRAY_LEN_OFFSET
Definition: constants.h:38
constexpr int RH_EVENT_COUNT_OFFSET
Definition: constants.h:37
constexpr int RH_USER_WORD2_OFFSET
Definition: constants.h:45
constexpr int RH_BIT_INFO_OFFSET
Definition: constants.h:39
constexpr int RH_USER_HEADER_LEN_OFFSET
Definition: constants.h:40
constexpr int RH_RECORD_NUMBER_OFFSET
Definition: constants.h:35
constexpr int COMP_TYPE_SHIFT
Definition: constants.h:119
constexpr int RH_HEADER_LENGTH_OFFSET
Definition: constants.h:36
constexpr int RH_MAGIC_NUMBER_OFFSET
Definition: constants.h:41
constexpr int RH_RECORD_LENGTH_OFFSET
Definition: constants.h:34
constexpr uint32_t HEADER_MAGIC
Definition: constants.h:8
constexpr int RH_USER_WORD1_OFFSET
Definition: constants.h:44
constexpr int RH_COMP_WORD_OFFSET
Definition: constants.h:43
constexpr int HIPO_VERSION
Definition: constants.h:132
constexpr int RH_DATA_LENGTH_OFFSET
Definition: constants.h:42
HIPO record builder for accumulating and compressing events.