Moving the list classes to their proper place and fixing them up.
[u/mrichter/AliRoot.git] / HLT / MUON / AliHLTMUONDataBlockReader.h
CommitLineData
b3eef24e 1#ifndef ALIHLTMUONDATABLOCKREADER_H
2#define ALIHLTMUONDATABLOCKREADER_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 AliHLTMUONDataBlockReader.h
22 * @author Artur Szostak <artursz@iafrica.com>
23 * @date
24 * @brief Definition of a reader 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 reading the contents of an internal dimuon HLT
43 * data block.
44 * Suppose we are given a pointer 'buffer' to the buffer where a data block is
45 * stored in memory and the size of the data block is given by the variable 'size'.
46 * The data block is of type 'block_type' and the data block entries are of type
47 * 'entries_type'. The data block can be accessed in the following way:
48 *
49 * void* buffer = somebuffer;
50 * AliHLTUInt32_t size = somebuffer_size;
51 *
52 * // Initialise the data block reader.
53 * AliHLTMUONDataBlockReader<block_type, entries_type> block(buffer, size);
54 *
55 * // Check that the buffer has the expected size.
56 * if (not block.BufferSizeOk())
57 * {
58 * // handle error...
59 * }
60 *
61 * // Find the number of entries in the data block.
62 * AliHLTUInt32_t nentries = block.Nentries();
63 *
64 * // Loop over all entries in the data block.
65 * for (AliHLTUInt32_t i = 0; i < nentries; i++)
66 * {
67 * const entries_type& entry = block[i];
68 * // Do something with the entry...
69 * }
70 */
71template <class DataBlockType, class DataElementType>
72class AliHLTMUONDataBlockReader
73{
74public:
75
76 /**
77 * Constructor that sets the internal pointer to the start of the data
78 * block and the total size of the block in bytes.
79 * @param buffer The pointer to the first byte of the block in memory.
80 * @param size The total size of the data block in bytes.
81 */
82 AliHLTMUONDataBlockReader(const void* buffer, AliHLTUInt32_t size) :
83 fSize(size),
84 fBlock(reinterpret_cast<const DataBlockType*>(buffer)),
85 fData(reinterpret_cast<const DataElementType*>(
86 reinterpret_cast<const DataBlockType*>(buffer) + 1
87 ))
88 {
89 assert( buffer != NULL );
90 }
91
92 /**
93 * Checks that the size of the buffer storing the data block is correct.
94 * Basic sanity checks are performed such as seeing if the data block
95 * size corresponds to the number of reconstructed hits stored and that
96 * the size of the buffer is at least sizeof(DataBlockType) bytes big.
97 */
98 bool BufferSizeOk() const
99 {
100 // The block size must be at least sizeof(DataBlockType) bytes.
101 if (fSize < sizeof(DataBlockType)) return false;
102
103 // Now check if the size of the data block corresponds to the
104 // number of entries it claims to contain.
105 AliHLTUInt32_t arraysize = fSize - sizeof(DataBlockType);
106 return arraysize == Nentries() * sizeof(DataElementType);
107 }
108
109 /**
110 * Returns the common data block header.
111 */
90a74d7a 112 const AliHLTMUONDataBlockHeader& CommonBlockHeader() const
b3eef24e 113 {
114 return fBlock->fHeader;
115 }
90a74d7a 116
117 /**
118 * Returns the whole data block header.
119 */
120 const DataBlockType& BlockHeader() const
121 {
122 return fBlock;
123 }
b3eef24e 124
125 /**
126 * Returns the total number of entries in the data block.
127 */
128 AliHLTUInt32_t Nentries() const { return fBlock->fHeader.fNrecords; }
129
130 /**
131 * Returns a pointer to the i'th entry.
132 * If the index 'i' is out of bounds then NULL is returned.
133 * This is a safe access method because it does bounds checking but is
134 * a little slower than the array operator.
135 * @param i The index number of the entry to be returned.
136 * @return A pointer to the entry or NULL.
137 */
138 const DataElementType* Entry(AliHLTUInt32_t i) const
139 {
140 return (i < Nentries()) ? &fData[i] : NULL;
141 }
142
143 /**
144 * Array operator for accessing the data entries directly.
145 * The index variable 'i' is not checked (except in debug compilations)
146 * so one should make sure they are within the valid range.
147 */
148 const DataElementType& operator [] (AliHLTUInt32_t i) const
149 {
150 assert( i < Nentries() );
151 return fData[i];
152 }
153
154 /**
155 * Returns a pointer to the array of elements in the data block.
156 * Care must be taken not to read beyond the array limits given by
157 * Nentries().
158 */
159 const DataElementType* GetArray() const { return fData; }
160
161private:
162
163 AliHLTUInt32_t fSize; // Size of the data block in bytes.
164 const DataBlockType* fBlock; // Pointer to the data block buffer.
165 const DataElementType* fData; // Pointer to the data array.
166};
167
168
169// We now define the reader classes for the various data block types from the
170// template class AliHLTMUONDataBlockReader.
171
172typedef AliHLTMUONDataBlockReader<
173 AliHLTMUONTriggerRecordsBlockStruct,
174 AliHLTMUONTriggerRecordStruct
175 > AliHLTMUONTriggerRecordsBlockReader;
176
177typedef AliHLTMUONDataBlockReader<
178 AliHLTMUONTrigRecsDebugBlockStruct,
179 AliHLTMUONTrigRecInfoStruct
180 > AliHLTMUONTrigRecsDebugBlockReader;
181
182typedef AliHLTMUONDataBlockReader<
183 AliHLTMUONTriggerChannelsBlockStruct,
184 AliHLTMUONTriggerChannelStruct
185 > AliHLTMUONTriggerChannelsBlockReader;
186
187typedef AliHLTMUONDataBlockReader<
188 AliHLTMUONRecHitsBlockStruct,
189 AliHLTMUONRecHitStruct
190 > AliHLTMUONRecHitsBlockReader;
191
192typedef AliHLTMUONDataBlockReader<
193 AliHLTMUONClustersBlockStruct,
194 AliHLTMUONClusterStruct
195 > AliHLTMUONClustersBlockReader;
196
197typedef AliHLTMUONDataBlockReader<
198 AliHLTMUONChannelsBlockStruct,
199 AliHLTMUONChannelStruct
200 > AliHLTMUONChannelsBlockReader;
201
90a74d7a 202typedef AliHLTMUONDataBlockReader<
203 AliHLTMUONMansoTracksBlockStruct,
204 AliHLTMUONMansoTrackStruct
205 > AliHLTMUONMansoTracksBlockReader;
206
207typedef AliHLTMUONDataBlockReader<
208 AliHLTMUONMansoCandidatesBlockStruct,
209 AliHLTMUONMansoCandidateStruct
210 > AliHLTMUONMansoCandidatesBlockReader;
211
212typedef AliHLTMUONDataBlockReader<
213 AliHLTMUONSinglesDecisionBlockStruct,
214 AliHLTMUONTrackDecisionStruct
215 > AliHLTMUONSinglesDecisionBlockReader;
216
217typedef AliHLTMUONDataBlockReader<
218 AliHLTMUONPairsDecisionBlockStruct,
219 AliHLTMUONPairDecisionStruct
220 > AliHLTMUONPairsDecisionBlockReader;
221
b3eef24e 222#endif // ALIHLTMUONDATABLOCKREADER_H