]>
Commit | Line | Data |
---|---|---|
b3eef24e | 1 | #ifndef ALIHLTMUONDATABLOCKWRITER_H |
2 | #define ALIHLTMUONDATABLOCKWRITER_H | |
3 | /************************************************************************** | |
4 | * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. * | |
5 | * * | |
6 | * Author: The ALICE Off-line Project. * | |
7 | * Contributors are mentioned in the code where appropriate. * | |
8 | * * | |
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 | **************************************************************************/ | |
17 | ||
18 | /* $Id$ */ | |
19 | ||
20 | /** | |
21 | * @file AliHLTMUONDataBlockWriter.h | |
22 | * @author Artur Szostak <artursz@iafrica.com> | |
23 | * @date | |
24 | * @brief Definition of a writer class for internal dimuon HLT raw data blocks. | |
25 | */ | |
26 | ||
27 | #include "AliHLTMUONDataTypes.h" | |
28 | #include <cassert> | |
29 | ||
30 | #include "AliHLTMUONTriggerRecordsBlockStruct.h" | |
31 | #include "AliHLTMUONTrigRecsDebugBlockStruct.h" | |
32 | #include "AliHLTMUONTriggerChannelsBlockStruct.h" | |
33 | #include "AliHLTMUONRecHitsBlockStruct.h" | |
34 | #include "AliHLTMUONClustersBlockStruct.h" | |
35 | #include "AliHLTMUONChannelsBlockStruct.h" | |
90a74d7a | 36 | #include "AliHLTMUONMansoTracksBlockStruct.h" |
37 | #include "AliHLTMUONMansoCandidatesBlockStruct.h" | |
38 | #include "AliHLTMUONSinglesDecisionBlockStruct.h" | |
39 | #include "AliHLTMUONPairsDecisionBlockStruct.h" | |
b3eef24e | 40 | |
41 | /** | |
42 | * A light weight class for writing an internal dimuon HLT data block. | |
43 | * Suppose we are given a pointer 'buffer' to some empty memory buffer where we | |
44 | * can store our new data block and the size of the data block is given by the | |
45 | * variable 'size'. The data block is of type 'block_type', the data block entries | |
46 | * are of type 'entries_type' and the data block type code is 'type_code'. | |
47 | * The data block can be written in the following way: | |
48 | * | |
49 | * void* buffer = somebuffer; | |
50 | * AliHLTUInt32_t size = somebuffer_size; | |
51 | * | |
52 | * // Initialise the data block writer. | |
53 | * AliHLTMUONDataBlockWriter<block_type, entries_type, type_code> | |
54 | * block(buffer, size); | |
55 | * | |
56 | * // Initialise the block header | |
b12fe461 | 57 | * if (not block.InitCommonHeader()) |
b3eef24e | 58 | * { |
59 | * // handle error and exit... | |
60 | * } | |
61 | * | |
62 | * // Tell the writer how many entries we are going to use. | |
63 | * if (not block.SetNumberOfEntries(somevalue)) | |
64 | * { | |
65 | * // handle error and exit... | |
66 | * } | |
67 | * | |
68 | * // Add all the entries to the data block. | |
69 | * for (AliHLTUInt32_t i = 0; i < block.Nentries(); i++) | |
70 | * { | |
71 | * entries_type& entry = block[i]; | |
72 | * // fill the new entry... | |
73 | * entry.somefield = somevalue; | |
74 | * } | |
75 | * | |
76 | * The slightly slower but safer method is to do the following: | |
77 | * | |
78 | * AliHLTMUONDataBlockWriter<block_type, entries_type, type_code> | |
79 | * block(buffer, size); | |
b12fe461 | 80 | * if (not block.InitCommonHeader()) |
b3eef24e | 81 | * { |
82 | * // handle error and exit... | |
83 | * } | |
84 | * | |
85 | * // For each new entry add it to the data block. | |
86 | * while (HaveMoreEntries()) | |
87 | * { | |
88 | * entries_type* entry = block.AddEntry(); | |
b12fe461 | 89 | * if (entry == NULL) |
90 | * { | |
91 | * // handle buffer overflow and exit... | |
92 | * } | |
b3eef24e | 93 | * // fill the new entry... |
94 | * entry->somefield = somevalue; | |
95 | * } | |
96 | */ | |
97 | template < | |
98 | class DataBlockType, | |
99 | class DataElementType, | |
100 | AliHLTMUONDataBlockType blockTypeCode | |
101 | > | |
102 | class AliHLTMUONDataBlockWriter | |
103 | { | |
104 | public: | |
b12fe461 | 105 | typedef DataBlockType HeaderType; |
106 | typedef DataElementType ElementType; | |
b3eef24e | 107 | |
108 | /** | |
109 | * Constructor that sets the internal pointer to the start of the buffer | |
110 | * space to write to and the total size of the buffer in bytes. | |
111 | * @param buffer The pointer to the first byte of the memory buffer. | |
112 | * @param size The total size of the buffer in bytes. | |
113 | */ | |
114 | AliHLTMUONDataBlockWriter(void* buffer, AliHLTUInt32_t size) : | |
115 | fSize(size), | |
116 | fMaxArraySize(size - sizeof(DataBlockType)), | |
117 | fBlock(reinterpret_cast<DataBlockType*>(buffer)), | |
118 | fData(reinterpret_cast<DataElementType*>( | |
119 | reinterpret_cast<DataBlockType*>(buffer) + 1 | |
120 | )) | |
121 | { | |
122 | assert( buffer != NULL ); | |
123 | } | |
124 | ||
125 | /** | |
90a74d7a | 126 | * Initialises the common data block header by setting the type and record |
127 | * width fields. If the buffer size was to small to create the header then | |
128 | * this method returns false, otherwise true on success. | |
b3eef24e | 129 | */ |
90a74d7a | 130 | bool InitCommonHeader() const |
b3eef24e | 131 | { |
132 | // The block size must be at least sizeof(DataBlockType) bytes. | |
133 | if (fSize < sizeof(DataBlockType)) return false; | |
134 | ||
135 | // Now fill the fields in the header. | |
136 | fBlock->fHeader.fType = blockTypeCode; | |
137 | fBlock->fHeader.fRecordWidth = sizeof(DataElementType); | |
138 | fBlock->fHeader.fNrecords = 0; | |
139 | return true; | |
140 | } | |
141 | ||
142 | /** | |
90a74d7a | 143 | * Returns the common data block header. |
b3eef24e | 144 | */ |
90a74d7a | 145 | const AliHLTMUONDataBlockHeader& CommonBlockHeader() const |
b3eef24e | 146 | { |
147 | return fBlock->fHeader; | |
148 | } | |
149 | ||
90a74d7a | 150 | /** |
151 | * Returns the whole data block header. | |
152 | */ | |
153 | DataBlockType& BlockHeader() | |
154 | { | |
72836b63 | 155 | return *fBlock; |
90a74d7a | 156 | } |
157 | ||
158 | const DataBlockType& BlockHeader() const | |
159 | { | |
72836b63 | 160 | return *fBlock; |
90a74d7a | 161 | } |
162 | ||
b3eef24e | 163 | /** |
164 | * Returns a pointer to the next location where a data entry can be | |
165 | * written and increments the number of entries. | |
166 | * If the buffer is already full then NULL is returned and the number of | |
167 | * entries is not changed. | |
168 | */ | |
169 | DataElementType* AddEntry() const | |
170 | { | |
171 | if ( (Nentries() + 1) * sizeof(DataElementType) > fMaxArraySize ) | |
172 | return NULL; | |
173 | DataElementType* newentry = &fData[fBlock->fHeader.fNrecords]; | |
174 | fBlock->fHeader.fNrecords++; | |
175 | return newentry; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Sets the number of entries that will be filled into the buffer. | |
180 | * If the number of entries is to many to fit into the buffer then this | |
181 | * method returns false, otherwise true. | |
182 | */ | |
183 | bool SetNumberOfEntries(AliHLTUInt32_t n) const | |
184 | { | |
185 | if (n * sizeof(DataElementType) > fMaxArraySize) return false; | |
186 | fBlock->fHeader.fNrecords = n; | |
72836b63 | 187 | return true; |
b3eef24e | 188 | } |
189 | ||
190 | /** | |
191 | * Returns the total number of entries already added to the data block. | |
192 | */ | |
193 | AliHLTUInt32_t Nentries() const { return fBlock->fHeader.fNrecords; } | |
194 | ||
195 | /** | |
196 | * Returns a pointer to the i'th entry. | |
197 | * If the index 'i' is out of bounds then NULL is returned. | |
198 | * This is a safe access method because it does bounds checking but is | |
199 | * a little slower than the array operators. | |
200 | * @param i The index number of the entry to be returned. | |
201 | * @return A pointer to the entry or NULL. | |
202 | */ | |
203 | DataElementType* Entry(AliHLTUInt32_t i) | |
204 | { | |
205 | return (i < Nentries()) ? &fData[i] : NULL; | |
206 | } | |
207 | ||
208 | const DataElementType* Entry(AliHLTUInt32_t i) const | |
209 | { | |
210 | return (i < Nentries()) ? &fData[i] : NULL; | |
211 | } | |
212 | ||
213 | /** | |
214 | * Array operator for accessing the data entries directly. | |
215 | * The index variable 'i' is not checked (except in debug compilations) | |
216 | * so one should make sure they are within the valid range. | |
217 | */ | |
218 | DataElementType& operator [] (AliHLTUInt32_t i) | |
219 | { | |
220 | assert( i < Nentries() ); | |
221 | return fData[i]; | |
222 | } | |
223 | ||
224 | const DataElementType& operator [] (AliHLTUInt32_t i) const | |
225 | { | |
226 | assert( i < Nentries() ); | |
227 | return fData[i]; | |
228 | } | |
229 | ||
230 | /** | |
231 | * Returns a pointer to the array of elements in the data block. | |
232 | * Care must be taken not to read beyond the array limits given by | |
233 | * Nentries(). | |
234 | */ | |
235 | DataElementType* GetArray() { return fData; } | |
236 | const DataElementType* GetArray() const { return fData; } | |
237 | ||
238 | /** | |
239 | * Calculates the number of bytes used for the data block in the buffer. | |
b12fe461 | 240 | * This value will only make sense if a call to InitCommonHeader() was |
241 | * made and it returned true. | |
b3eef24e | 242 | */ |
243 | AliHLTUInt32_t BytesUsed() const | |
244 | { | |
72836b63 | 245 | assert( sizeof(DataElementType) == fBlock->fHeader.fRecordWidth); |
b3eef24e | 246 | return sizeof(DataBlockType) + Nentries() * sizeof(DataElementType); |
247 | } | |
248 | ||
249 | /** | |
250 | * Calculates the maximum number of entries that will fit into the | |
251 | * memory buffer. | |
252 | */ | |
253 | AliHLTUInt32_t MaxNumberOfEntries() const | |
254 | { | |
255 | return fMaxArraySize / sizeof(DataElementType); | |
256 | } | |
257 | ||
b12fe461 | 258 | AliHLTUInt32_t BufferSize() { return fSize; } |
259 | ||
b3eef24e | 260 | private: |
261 | ||
262 | AliHLTUInt32_t fSize; // Size of the buffer in bytes. | |
263 | AliHLTUInt32_t fMaxArraySize; // Maximum size of the fData array in bytes. | |
264 | DataBlockType* fBlock; // Pointer to the start of the data block. | |
265 | DataElementType* fData; // Pointer to the start of the data array. | |
266 | }; | |
267 | ||
268 | ||
269 | // We now define the writer classes for the various data block types from the | |
270 | // template class AliHLTMUONDataBlockWriter. | |
271 | ||
272 | typedef AliHLTMUONDataBlockWriter< | |
273 | AliHLTMUONTriggerRecordsBlockStruct, | |
274 | AliHLTMUONTriggerRecordStruct, | |
275 | kTriggerRecordsDataBlock | |
276 | > AliHLTMUONTriggerRecordsBlockWriter; | |
277 | ||
278 | typedef AliHLTMUONDataBlockWriter< | |
279 | AliHLTMUONTrigRecsDebugBlockStruct, | |
280 | AliHLTMUONTrigRecInfoStruct, | |
281 | kTrigRecsDebugDataBlock | |
282 | > AliHLTMUONTrigRecsDebugBlockWriter; | |
283 | ||
284 | typedef AliHLTMUONDataBlockWriter< | |
285 | AliHLTMUONTriggerChannelsBlockStruct, | |
286 | AliHLTMUONTriggerChannelStruct, | |
287 | kTriggerChannelsDataBlock | |
288 | > AliHLTMUONTriggerChannelsBlockWriter; | |
289 | ||
290 | typedef AliHLTMUONDataBlockWriter< | |
291 | AliHLTMUONRecHitsBlockStruct, | |
292 | AliHLTMUONRecHitStruct, | |
293 | kRecHitsDataBlock | |
294 | > AliHLTMUONRecHitsBlockWriter; | |
295 | ||
296 | typedef AliHLTMUONDataBlockWriter< | |
297 | AliHLTMUONClustersBlockStruct, | |
298 | AliHLTMUONClusterStruct, | |
299 | kClustersDataBlock | |
300 | > AliHLTMUONClustersBlockWriter; | |
301 | ||
302 | typedef AliHLTMUONDataBlockWriter< | |
303 | AliHLTMUONChannelsBlockStruct, | |
304 | AliHLTMUONChannelStruct, | |
305 | kChannelsDataBlock | |
306 | > AliHLTMUONChannelsBlockWriter; | |
307 | ||
90a74d7a | 308 | typedef AliHLTMUONDataBlockWriter< |
309 | AliHLTMUONMansoTracksBlockStruct, | |
310 | AliHLTMUONMansoTrackStruct, | |
311 | kMansoTracksDataBlock | |
312 | > AliHLTMUONMansoTracksBlockWriter; | |
313 | ||
314 | typedef AliHLTMUONDataBlockWriter< | |
315 | AliHLTMUONMansoCandidatesBlockStruct, | |
316 | AliHLTMUONMansoCandidateStruct, | |
317 | kMansoCandidatesDataBlock | |
318 | > AliHLTMUONMansoCandidatesBlockWriter; | |
319 | ||
320 | typedef AliHLTMUONDataBlockWriter< | |
321 | AliHLTMUONSinglesDecisionBlockStruct, | |
322 | AliHLTMUONTrackDecisionStruct, | |
323 | kSinglesDecisionDataBlock | |
324 | > AliHLTMUONSinglesDecisionBlockWriter; | |
325 | ||
326 | typedef AliHLTMUONDataBlockWriter< | |
327 | AliHLTMUONPairsDecisionBlockStruct, | |
b12fe461 | 328 | AliHLTMUONPairDecisionStruct, |
90a74d7a | 329 | kPairsDecisionDataBlock |
330 | > AliHLTMUONPairsDecisionBlockWriter; | |
331 | ||
b3eef24e | 332 | #endif // ALIHLTMUONDATABLOCKWRITER_H |