]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWCFData.h
39e4c60e6440523c194ebec5458547979a65f77f
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCHWCFData.h
1 //-*- Mode: C++ -*-
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
15 #include "AliHLTTPCRootTypes.h"
16 #include "AliHLTDataTypes.h"
17 #include "AliHLTLogging.h"
18 #include "AliHLTErrorGuard.h"
19
20 class 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  */
53 class 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;
73
74   // check if index is within bounds
75   bool CheckBounds(int i) const {
76     if (fVersion<0) {
77       ALIHLTERRORGUARD(1, "");
78       return false;
79     }
80     int elementsize=GetElementSize(fVersion);
81     if (elementsize<0) return false;
82     return ((i+1)*elementsize+fRCUTrailerSize<=fBufferSize);
83   }
84
85   // get the size of one element
86   int GetElementSize(int version) const {
87     switch (version) {
88     case 0: return sizeof(AliHLTTPCHWClusterV0);
89     case 1: return sizeof(AliHLTTPCHWClusterV1);
90     default:
91       ALIHLTERRORGUARD(1, "invalid format version %d", fVersion);
92     }
93     return -1;
94   }
95
96   // pointer to RCU trailer
97   const AliHLTUInt8_t*  GetRCUTrailer() const
98   {
99     if (fRCUTrailerSize<=0 || fpBuffer==NULL || fBufferSize<fRCUTrailerSize) return NULL;
100     return fpBuffer+(fBufferSize-fRCUTrailerSize);
101   }
102
103   // size of RCU trailer
104   int GetRCUTrailerSize() const { return fRCUTrailerSize; }
105
106   // print info
107   void Print(const char* option);
108
109   // open a file and init
110   int Open(const char* filename);
111
112   enum {
113     kHWCFDataV0 = 0,
114     kHWCFDataV1 = 1,
115   };
116
117   struct AliHLTTPCHWClusterV0 {
118     AliHLTUInt32_t fHeader;
119     Float_t        fPad;
120     Float_t        fTime;
121     Float_t        fSigmaY2;
122     Float_t        fSigmaZ2;
123
124     Int_t    GetPadRow()  const;
125     Float_t  GetPad()     const {return fPad+0.5;}
126     Float_t  GetTime()    const {return fTime;}
127     Float_t  GetSigmaY2() const {
128       Float_t sy2 = fSigmaY2 - fPad*fPad;
129       return (sy2>0) ?sy2 :0.;
130     }
131     Float_t  GetSigmaZ2() const {
132       Float_t sz2 = fSigmaZ2 - fTime*fTime;
133       return (sz2>0) ?sz2 :0.;
134     }
135     Int_t    GetCharge()  const;
136     Int_t    GetQMax()    const {return -1;}
137   };
138
139   struct AliHLTTPCHWClusterV1 {
140     AliHLTUInt32_t fHeader;
141     AliHLTUInt32_t fCharge;
142     Float_t        fPad;
143     Float_t        fTime;
144     Float_t        fSigmaY2;
145     Float_t        fSigmaZ2;
146
147     Int_t    GetPadRow()  const;
148     Float_t  GetPad()     const {return fPad+0.5;}
149     Float_t  GetTime()    const {return fTime;}
150     Float_t  GetSigmaY2() const {
151       Float_t sy2 = fSigmaY2 - fPad*fPad;
152       return (sy2>0) ?sy2 :0.;
153     }
154     Float_t  GetSigmaZ2() const {
155       Float_t sz2 = fSigmaZ2 - fTime*fTime;
156       return (sz2>0) ?sz2 :0.;
157     }
158     Int_t    GetCharge()  const;
159     Int_t    GetQMax()    const;
160   };
161
162   template<typename T>
163   class AliHLTTPCHWClusterDecoder {
164   public:
165     AliHLTTPCHWClusterDecoder(const T* pClusterArray, int entries);
166     ~AliHLTTPCHWClusterDecoder();
167
168     // i'th element, no bounds check for performance reasons
169     const T& operator[](unsigned i) {
170       return fpClusterArray[i];
171     }
172
173     Int_t    GetPadRow(int i)  const {return fpClusterArray[i]->GetPadRow();}
174     Float_t  GetPad(int i)     const {return fpClusterArray[i]->GetPad();}
175     Float_t  GetTime(int i)    const {return fpClusterArray[i]->GetTime();}
176     Float_t  GetSigmaY2(int i) const {return fpClusterArray[i]->GetSigmaY2();}
177     Float_t  GetSigmaZ2(int i) const {return fpClusterArray[i]->GetSigmaZ2();}
178     Int_t    GetCharge(int i)  const {return fpClusterArray[i]->GetCharge();}
179     Int_t    GetQMax(int i)    const {return fpClusterArray[i]->GetQMax();}
180
181   private:
182     const T* fpClusterArray; //! array of clusters
183     int fEntries;            //! number of entries
184   };
185
186   class iterator {
187   public:
188     iterator()
189       : fData(NULL), fVersion(-1), fElementSize(0) {}
190     iterator(const AliHLTUInt8_t* pData, int version, int elementSize)
191       : fData(pData), fVersion(version), fElementSize(elementSize) {}
192     iterator(const iterator& i)
193       : fData(i.fData), fVersion(i.fVersion), fElementSize(i.fElementSize) {}
194     iterator& operator=(const iterator& i)
195       { fData=i.fData; fVersion=i.fVersion; fElementSize=i.fElementSize; return *this;}
196     ~iterator() {fData=NULL;}
197
198     bool operator==(const iterator& i) const  {return (fData!=NULL) && (fData==i.fData);}
199     bool operator!=(const iterator& i) const  {return (fData!=NULL) && (fData!=i.fData);}
200     // prefix operators
201     iterator& operator++() {fData+=fElementSize; return *this;}
202     iterator& operator--() {fData-=fElementSize; return *this;}
203     // postfix operators
204     iterator operator++(int) {iterator i(*this); fData+=fElementSize; return i;}
205     iterator operator--(int) {iterator i(*this); fData-=fElementSize; return i;}
206
207     iterator& operator+=(int step) {fData+=step*fElementSize; return *this;}
208
209     Int_t    GetPadRow()  const {
210       switch (fVersion) {
211       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetPadRow();
212       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetPadRow();
213       } return -1;
214     }
215     Float_t  GetPad()     const {
216       switch (fVersion) {
217       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetPad();
218       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetPad();
219       } return -10000.;
220     }
221     Float_t  GetTime()    const {
222       switch (fVersion) {
223       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetTime();
224       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetTime();
225       } return -10000.;
226     }
227     Float_t  GetSigmaY2() const {
228       switch (fVersion) {
229       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetSigmaY2();
230       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetSigmaY2();
231       } return -10000.;
232     }
233     Float_t  GetSigmaZ2() const {
234       switch (fVersion) {
235       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetSigmaZ2();
236       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetSigmaZ2();
237       } return -10000.;
238     }
239     Int_t    GetCharge()  const {
240       switch (fVersion) {
241       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetCharge();
242       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetCharge();
243       } return -1;
244     }
245     Int_t    GetQMax()    const {
246       switch (fVersion) {
247       case 0: return reinterpret_cast<const AliHLTTPCHWClusterV0*>(fData)->GetQMax();
248       case 1: return reinterpret_cast<const AliHLTTPCHWClusterV1*>(fData)->GetQMax();
249       } return -1;
250     }
251
252   protected:
253   private:
254     const AliHLTUInt8_t* fData; //! data
255     int fVersion; //! format version
256     int fElementSize; //! element size
257   };
258
259   // prepare iterator and end marker
260   iterator& begin() {
261     fIterator.~iterator();
262     new (&fIterator) iterator(fpBuffer, fVersion, GetElementSize(fVersion));
263     fIteratorEnd=fIterator;
264     fIteratorEnd+=GetNumberOfClusters();
265     return fIterator;
266   }
267
268   // get loop end marker
269   iterator& end() {
270     return fIteratorEnd;
271   }
272
273   // find one single element
274   iterator& find(int i) {
275     fIterator.~iterator();
276     fIteratorEnd.~iterator();
277     if (i>=GetNumberOfClusters()) return fIterator;
278     new (&fIterator) iterator(fpBuffer, fVersion, GetElementSize(fVersion));
279     fIterator+=i;
280     fIteratorEnd=fIterator;
281     fIteratorEnd+=1;
282     return fIterator;
283   }
284
285   static const unsigned  fgkAliHLTTPCHWClusterSize;
286  protected:
287
288  private:
289   AliHLTTPCHWCFData(const AliHLTTPCHWCFData&);
290   AliHLTTPCHWCFData& operator=(const AliHLTTPCHWCFData&);
291
292   // get pointer to i'th element
293   const AliHLTUInt8_t* Get(int i) const
294   {
295     if (!fpBuffer) return NULL;
296     int elementsize=GetElementSize(fVersion);
297     if (elementsize<0) return NULL;
298     return fpBuffer+(i*elementsize);
299   }
300
301
302   const AliHLTUInt8_t* fpBuffer; //! pointer to data buffer
303   int fBufferSize; //! size of data buffer
304
305   int fVersion; //! format version
306   int fForcedVersion; //! forced format version
307   int fRCUTrailerSize; //! size of the RCU trailer in Byte
308
309   TArrayC* fpFileBuffer; //! internal buffer for file content
310
311   iterator fIterator; //! iterator
312   iterator fIteratorEnd; //! end iterator
313
314   ClassDef(AliHLTTPCHWCFData, 0)
315 };
316 #endif