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