Updated SNM Glauber fit
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCHWCFData.h
CommitLineData
73cc961a 1//-*- Mode: C++ -*-
3aa4d287 2// $Id$
3#ifndef ALIHLTTPCHWCFDATA_H
4#define ALIHLTTPCHWCFDATA_H
5//* This file is property of and copyright by the ALICE HLT Project *
6//* ALICE Experiment at CERN, All rights reserved. *
7//* See cxx source for full Copyright notice *
8
9/// @file AliHLTTPCHWCFData.h
10/// @author Matthias Richter
11/// @date 2011-08-04
12/// @brief Decoder methods for the HWCF format
13///
14
7f85a87e 15#include "Rtypes.h"
3aa4d287 16#include "AliHLTDataTypes.h"
17#include "AliHLTLogging.h"
73cc961a 18#include "AliHLTErrorGuard.h"
3aa4d287 19
20class TArrayC;
21
22/**
23 * @class AliHLTTPCHWCFData
24 * The class provides decoding functionality for the output format of the
25 * TPC HW ClusterFinder
26 *
27 * Two formats have been defined in the past and can be detected:
28 * version 0: 5 32bit words 20 Byte
29 * <pre>
30 * word 0: header (big endian 32bit unsigned)
31 * bit 31-30: 0x11 indicates cluster
32 * bit 29-24: row number in partition
33 * bit 23-0: Qtot, fixed point number with 6 bits after the point
34 * word 1: pad (float)
35 * word 2: time (float)
36 * word 3: pad variance (float)
37 * word 4: time variance (float)
38 * </pre>
39 *
40 * version 1: 6 32bit words 24 Byte
41 * <pre>
42 * word 0: header (big endian 32bit unsigned)
43 * bit 31-30: 0x11 indicates cluster
44 * bit 29-24: row number in partition
45 * bit 23-0: Qmax, fixed point number with 6 bits after the point
46 * word 1: total charge 32bit big endian, fixed point number with 12 bits after the point
47 * word 2: pad (float)
48 * word 3: time (float)
49 * word 4: pad variance (float)
50 * word 5: time variance (float)
51 * </pre>
52 */
53class AliHLTTPCHWCFData : public AliHLTLogging {
54 public:
55 AliHLTTPCHWCFData(int forceVersion=-1);
56 virtual ~AliHLTTPCHWCFData();
57
58 int Init(const AliHLTUInt8_t* pBuffer, int bufferSize);
59 int Reset();
60
61 Int_t GetNumberOfClusters() const;
62
63 Int_t GetPadRow(int i) const;
64 Float_t GetPad(int i) const;
65 Float_t GetTime(int i) const;
66 Float_t GetSigmaY2(int i) const;
67 Float_t GetSigmaZ2(int i) const;
68 Int_t GetCharge(int i) const;
69 Int_t GetQMax(int i) const;
70
71 int CheckVersion();
72 bool CheckAssumption(int format, const AliHLTUInt8_t* pData, int size) const;
73cc961a 73
74 // check if index is within bounds
b6588d72 75
73cc961a 76 bool CheckBounds(int i) const {
77 if (fVersion<0) {
b6588d72 78 ErrorMsg("");
73cc961a 79 return false;
80 }
81 int elementsize=GetElementSize(fVersion);
82 if (elementsize<0) return false;
83 return ((i+1)*elementsize+fRCUTrailerSize<=fBufferSize);
84 }
85
86 // get the size of one element
87 int GetElementSize(int version) const {
88 switch (version) {
89 case 0: return sizeof(AliHLTTPCHWClusterV0);
90 case 1: return sizeof(AliHLTTPCHWClusterV1);
91 default:
b6588d72 92 ErrorMsg(Form("invalid format version %d", fVersion));
73cc961a 93 }
94 return -1;
95 }
3aa4d287 96
97 // pointer to RCU trailer
98 const AliHLTUInt8_t* GetRCUTrailer() const
99 {
100 if (fRCUTrailerSize<=0 || fpBuffer==NULL || fBufferSize<fRCUTrailerSize) return NULL;
73847ba6 101 return fpBuffer+(fBufferSize-fRCUTrailerSize);
3aa4d287 102 }
103
104 // size of RCU trailer
105 int GetRCUTrailerSize() const { return fRCUTrailerSize; }
106
107 // print info
108 void Print(const char* option);
109
b6588d72 110 // print error message
111 void ErrorMsg( const char *str ) const;
112
3aa4d287 113 // open a file and init
114 int Open(const char* filename);
115
116 enum {
117 kHWCFDataV0 = 0,
118 kHWCFDataV1 = 1,
119 };
120
121 struct AliHLTTPCHWClusterV0 {
122 AliHLTUInt32_t fHeader;
123 Float_t fPad;
124 Float_t fTime;
125 Float_t fSigmaY2;
126 Float_t fSigmaZ2;
127
128 Int_t GetPadRow() const;
1b9cc91a 129 Float_t GetPad() const {return fPad+0.5;}
3aa4d287 130 Float_t GetTime() const {return fTime;}
b83cf637 131 Float_t GetSigmaY2() const {
132 Float_t sy2 = fSigmaY2 - fPad*fPad;
133 return (sy2>0) ?sy2 :0.;
134 }
135 Float_t GetSigmaZ2() const {
136 Float_t sz2 = fSigmaZ2 - fTime*fTime;
137 return (sz2>0) ?sz2 :0.;
138 }
3aa4d287 139 Int_t GetCharge() const;
140 Int_t GetQMax() const {return -1;}
141 };
142
143 struct AliHLTTPCHWClusterV1 {
144 AliHLTUInt32_t fHeader;
145 AliHLTUInt32_t fCharge;
146 Float_t fPad;
147 Float_t fTime;
148 Float_t fSigmaY2;
149 Float_t fSigmaZ2;
150
151 Int_t GetPadRow() const;
1b9cc91a 152 Float_t GetPad() const {return fPad+0.5;}
3aa4d287 153 Float_t GetTime() const {return fTime;}
b83cf637 154 Float_t GetSigmaY2() const {
155 Float_t sy2 = fSigmaY2 - fPad*fPad;
156 return (sy2>0) ?sy2 :0.;
157 }
158 Float_t GetSigmaZ2() const {
159 Float_t sz2 = fSigmaZ2 - fTime*fTime;
160 return (sz2>0) ?sz2 :0.;
161 }
3aa4d287 162 Int_t GetCharge() const;
163 Int_t GetQMax() const;
164 };
165
166 template<typename T>
167 class AliHLTTPCHWClusterDecoder {
168 public:
169 AliHLTTPCHWClusterDecoder(const T* pClusterArray, int entries);
170 ~AliHLTTPCHWClusterDecoder();
171
172 // i'th element, no bounds check for performance reasons
173 const T& operator[](unsigned i) {
174 return fpClusterArray[i];
175 }
176
177 Int_t GetPadRow(int i) const {return fpClusterArray[i]->GetPadRow();}
178 Float_t GetPad(int i) const {return fpClusterArray[i]->GetPad();}
179 Float_t GetTime(int i) const {return fpClusterArray[i]->GetTime();}
180 Float_t GetSigmaY2(int i) const {return fpClusterArray[i]->GetSigmaY2();}
181 Float_t GetSigmaZ2(int i) const {return fpClusterArray[i]->GetSigmaZ2();}
182 Int_t GetCharge(int i) const {return fpClusterArray[i]->GetCharge();}
183 Int_t GetQMax(int i) const {return fpClusterArray[i]->GetQMax();}
184
185 private:
186 const T* fpClusterArray; //! array of clusters
187 int fEntries; //! number of entries
188 };
189
536e7d6d 190 class iterator {
191 public:
192 iterator()
193 : fData(NULL), fVersion(-1), fElementSize(0) {}
194 iterator(const AliHLTUInt8_t* pData, int version, int elementSize)
195 : fData(pData), fVersion(version), fElementSize(elementSize) {}
196 iterator(const iterator& i)
197 : fData(i.fData), fVersion(i.fVersion), fElementSize(i.fElementSize) {}
b22da46d 198 iterator& operator=(const iterator& i) {
199 if (this==&i) return *this;
200 fData=i.fData; fVersion=i.fVersion; fElementSize=i.fElementSize; return *this;
201 }
73cc961a 202 ~iterator() {fData=NULL;}
536e7d6d 203
204 bool operator==(const iterator& i) const {return (fData!=NULL) && (fData==i.fData);}
205 bool operator!=(const iterator& i) const {return (fData!=NULL) && (fData!=i.fData);}
206 // prefix operators
207 iterator& operator++() {fData+=fElementSize; return *this;}
208 iterator& operator--() {fData-=fElementSize; return *this;}
209 // postfix operators
210 iterator operator++(int) {iterator i(*this); fData+=fElementSize; return i;}
211 iterator operator--(int) {iterator i(*this); fData-=fElementSize; return i;}
212
213 iterator& operator+=(int step) {fData+=step*fElementSize; return *this;}
214
215 Int_t GetPadRow() const {
216 switch (fVersion) {
217 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetPadRow();
218 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetPadRow();
219 } return -1;
220 }
221 Float_t GetPad() const {
222 switch (fVersion) {
223 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetPad();
224 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetPad();
225 } return -10000.;
226 }
227 Float_t GetTime() const {
228 switch (fVersion) {
229 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetTime();
230 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetTime();
231 } return -10000.;
232 }
233 Float_t GetSigmaY2() const {
234 switch (fVersion) {
235 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetSigmaY2();
236 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetSigmaY2();
237 } return -10000.;
238 }
239 Float_t GetSigmaZ2() const {
240 switch (fVersion) {
241 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetSigmaZ2();
242 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetSigmaZ2();
243 } return -10000.;
244 }
245 Int_t GetCharge() const {
246 switch (fVersion) {
247 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetCharge();
248 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetCharge();
249 } return -1;
250 }
251 Int_t GetQMax() const {
252 switch (fVersion) {
253 case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetQMax();
254 case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetQMax();
255 } return -1;
256 }
257
258 protected:
259 private:
260 const AliHLTUInt8_t* fData; //! data
261 int fVersion; //! format version
262 int fElementSize; //! element size
263 };
264
265 // prepare iterator and end marker
266 iterator& begin() {
267 fIterator.~iterator();
268 new (&fIterator) iterator(fpBuffer, fVersion, GetElementSize(fVersion));
269 fIteratorEnd=fIterator;
270 fIteratorEnd+=GetNumberOfClusters();
271 return fIterator;
272 }
273
274 // get loop end marker
275 iterator& end() {
276 return fIteratorEnd;
277 }
278
73cc961a 279 // find one single element
280 iterator& find(int i) {
281 fIterator.~iterator();
282 fIteratorEnd.~iterator();
283 if (i>=GetNumberOfClusters()) return fIterator;
284 new (&fIterator) iterator(fpBuffer, fVersion, GetElementSize(fVersion));
285 fIterator+=i;
286 fIteratorEnd=fIterator;
287 fIteratorEnd+=1;
288 return fIterator;
289 }
290
3aa4d287 291 static const unsigned fgkAliHLTTPCHWClusterSize;
292 protected:
293
294 private:
295 AliHLTTPCHWCFData(const AliHLTTPCHWCFData&);
296 AliHLTTPCHWCFData& operator=(const AliHLTTPCHWCFData&);
297
298 // get pointer to i'th element
299 const AliHLTUInt8_t* Get(int i) const
300 {
301 if (!fpBuffer) return NULL;
302 int elementsize=GetElementSize(fVersion);
303 if (elementsize<0) return NULL;
304 return fpBuffer+(i*elementsize);
305 }
306
307
308 const AliHLTUInt8_t* fpBuffer; //! pointer to data buffer
309 int fBufferSize; //! size of data buffer
310
311 int fVersion; //! format version
312 int fForcedVersion; //! forced format version
313 int fRCUTrailerSize; //! size of the RCU trailer in Byte
314
315 TArrayC* fpFileBuffer; //! internal buffer for file content
316
536e7d6d 317 iterator fIterator; //! iterator
318 iterator fIteratorEnd; //! end iterator
319
3aa4d287 320 ClassDef(AliHLTTPCHWCFData, 0)
321};
322#endif