1 #ifndef ALIHLTMUONDATABLOCKWRITER_H
2 #define ALIHLTMUONDATABLOCKWRITER_H
3 /**************************************************************************
4 * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. *
6 * Author: The ALICE Off-line Project. *
7 * Contributors are mentioned in the code where appropriate. *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
21 * @file AliHLTMUONDataBlockWriter.h
22 * @author Artur Szostak <artursz@iafrica.com>
24 * @brief Definition of a writer class for internal dimuon HLT raw data blocks.
27 #include "AliHLTMUONDataTypes.h"
30 #include "AliHLTMUONTriggerRecordsBlockStruct.h"
31 #include "AliHLTMUONTrigRecsDebugBlockStruct.h"
32 #include "AliHLTMUONTriggerChannelsBlockStruct.h"
33 #include "AliHLTMUONRecHitsBlockStruct.h"
34 #include "AliHLTMUONClustersBlockStruct.h"
35 #include "AliHLTMUONChannelsBlockStruct.h"
38 * A light weight class for writing an internal dimuon HLT data block.
39 * Suppose we are given a pointer 'buffer' to some empty memory buffer where we
40 * can store our new data block and the size of the data block is given by the
41 * variable 'size'. The data block is of type 'block_type', the data block entries
42 * are of type 'entries_type' and the data block type code is 'type_code'.
43 * The data block can be written in the following way:
45 * void* buffer = somebuffer;
46 * AliHLTUInt32_t size = somebuffer_size;
48 * // Initialise the data block writer.
49 * AliHLTMUONDataBlockWriter<block_type, entries_type, type_code>
50 * block(buffer, size);
52 * // Initialise the block header
53 * if (not block.InitHeader())
55 * // handle error and exit...
58 * // Tell the writer how many entries we are going to use.
59 * if (not block.SetNumberOfEntries(somevalue))
61 * // handle error and exit...
64 * // Add all the entries to the data block.
65 * for (AliHLTUInt32_t i = 0; i < block.Nentries(); i++)
67 * entries_type& entry = block[i];
68 * // fill the new entry...
69 * entry.somefield = somevalue;
72 * The slightly slower but safer method is to do the following:
74 * AliHLTMUONDataBlockWriter<block_type, entries_type, type_code>
75 * block(buffer, size);
76 * if (not block.InitHeader())
78 * // handle error and exit...
81 * // For each new entry add it to the data block.
82 * while (HaveMoreEntries())
84 * entries_type* entry = block.AddEntry();
85 * // fill the new entry...
86 * entry->somefield = somevalue;
91 class DataElementType,
92 AliHLTMUONDataBlockType blockTypeCode
94 class AliHLTMUONDataBlockWriter
99 * Constructor that sets the internal pointer to the start of the buffer
100 * space to write to and the total size of the buffer in bytes.
101 * @param buffer The pointer to the first byte of the memory buffer.
102 * @param size The total size of the buffer in bytes.
104 AliHLTMUONDataBlockWriter(void* buffer, AliHLTUInt32_t size) :
106 fMaxArraySize(size - sizeof(DataBlockType)),
107 fBlock(reinterpret_cast<DataBlockType*>(buffer)),
108 fData(reinterpret_cast<DataElementType*>(
109 reinterpret_cast<DataBlockType*>(buffer) + 1
112 assert( buffer != NULL );
116 * Initialises the data block header by setting the type and record width
117 * fields. If the buffer size was to small to create the header then this
118 * method returns false, otherwise true on success.
120 bool InitHeader() const
122 // The block size must be at least sizeof(DataBlockType) bytes.
123 if (fSize < sizeof(DataBlockType)) return false;
125 // Now fill the fields in the header.
126 fBlock->fHeader.fType = blockTypeCode;
127 fBlock->fHeader.fRecordWidth = sizeof(DataElementType);
128 fBlock->fHeader.fNrecords = 0;
133 * Returns the data block header.
135 const AliHLTMUONDataBlockHeader& BlockHeader() const
137 return fBlock->fHeader;
141 * Returns a pointer to the next location where a data entry can be
142 * written and increments the number of entries.
143 * If the buffer is already full then NULL is returned and the number of
144 * entries is not changed.
146 DataElementType* AddEntry() const
148 if ( (Nentries() + 1) * sizeof(DataElementType) > fMaxArraySize )
150 DataElementType* newentry = &fData[fBlock->fHeader.fNrecords];
151 fBlock->fHeader.fNrecords++;
156 * Sets the number of entries that will be filled into the buffer.
157 * If the number of entries is to many to fit into the buffer then this
158 * method returns false, otherwise true.
160 bool SetNumberOfEntries(AliHLTUInt32_t n) const
162 if (n * sizeof(DataElementType) > fMaxArraySize) return false;
163 fBlock->fHeader.fNrecords = n;
167 * Returns the total number of entries already added to the data block.
169 AliHLTUInt32_t Nentries() const { return fBlock->fHeader.fNrecords; }
172 * Returns a pointer to the i'th entry.
173 * If the index 'i' is out of bounds then NULL is returned.
174 * This is a safe access method because it does bounds checking but is
175 * a little slower than the array operators.
176 * @param i The index number of the entry to be returned.
177 * @return A pointer to the entry or NULL.
179 DataElementType* Entry(AliHLTUInt32_t i)
181 return (i < Nentries()) ? &fData[i] : NULL;
184 const DataElementType* Entry(AliHLTUInt32_t i) const
186 return (i < Nentries()) ? &fData[i] : NULL;
190 * Array operator for accessing the data entries directly.
191 * The index variable 'i' is not checked (except in debug compilations)
192 * so one should make sure they are within the valid range.
194 DataElementType& operator [] (AliHLTUInt32_t i)
196 assert( i < Nentries() );
200 const DataElementType& operator [] (AliHLTUInt32_t i) const
202 assert( i < Nentries() );
207 * Returns a pointer to the array of elements in the data block.
208 * Care must be taken not to read beyond the array limits given by
211 DataElementType* GetArray() { return fData; }
212 const DataElementType* GetArray() const { return fData; }
215 * Calculates the number of bytes used for the data block in the buffer.
216 * This value will only make sense if a call to InitHeader() was made
217 * and it returned true.
219 AliHLTUInt32_t BytesUsed() const
221 assert( Nentries() * sizeof(DataElementType)
222 == Nentries() * fBlock->fHeader.fRecordWidth
225 return sizeof(DataBlockType) + Nentries() * sizeof(DataElementType);
229 * Calculates the maximum number of entries that will fit into the
232 AliHLTUInt32_t MaxNumberOfEntries() const
234 return fMaxArraySize / sizeof(DataElementType);
239 AliHLTUInt32_t fSize; // Size of the buffer in bytes.
240 AliHLTUInt32_t fMaxArraySize; // Maximum size of the fData array in bytes.
241 DataBlockType* fBlock; // Pointer to the start of the data block.
242 DataElementType* fData; // Pointer to the start of the data array.
246 // We now define the writer classes for the various data block types from the
247 // template class AliHLTMUONDataBlockWriter.
249 typedef AliHLTMUONDataBlockWriter<
250 AliHLTMUONTriggerRecordsBlockStruct,
251 AliHLTMUONTriggerRecordStruct,
252 kTriggerRecordsDataBlock
253 > AliHLTMUONTriggerRecordsBlockWriter;
255 typedef AliHLTMUONDataBlockWriter<
256 AliHLTMUONTrigRecsDebugBlockStruct,
257 AliHLTMUONTrigRecInfoStruct,
258 kTrigRecsDebugDataBlock
259 > AliHLTMUONTrigRecsDebugBlockWriter;
261 typedef AliHLTMUONDataBlockWriter<
262 AliHLTMUONTriggerChannelsBlockStruct,
263 AliHLTMUONTriggerChannelStruct,
264 kTriggerChannelsDataBlock
265 > AliHLTMUONTriggerChannelsBlockWriter;
267 typedef AliHLTMUONDataBlockWriter<
268 AliHLTMUONRecHitsBlockStruct,
269 AliHLTMUONRecHitStruct,
271 > AliHLTMUONRecHitsBlockWriter;
273 typedef AliHLTMUONDataBlockWriter<
274 AliHLTMUONClustersBlockStruct,
275 AliHLTMUONClusterStruct,
277 > AliHLTMUONClustersBlockWriter;
279 typedef AliHLTMUONDataBlockWriter<
280 AliHLTMUONChannelsBlockStruct,
281 AliHLTMUONChannelStruct,
283 > AliHLTMUONChannelsBlockWriter;
285 #endif // ALIHLTMUONDATABLOCKWRITER_H