]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitReaderUnpacked.cxx
Coding violations
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCDigitReaderUnpacked.cxx
1 // $Id$
2
3 //*************************************************************************
4 // This file is property of and copyright by the ALICE HLT Project        * 
5 // ALICE Experiment at CERN, All rights reserved.                         *
6 //                                                                        *
7 // Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
9 //                  Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
10 //                  for The ALICE HLT Project.                            *
11 //                                                                        *
12 // Permission to use, copy, modify and distribute this software and its   *
13 // documentation strictly for non-commercial purposes is hereby granted   *
14 // without fee, provided that the above copyright notice appears in all   *
15 // copies and that both the copyright notice and this permission notice   *
16 // appear in the supporting documentation. The authors make no claims     *
17 // about the suitability of this software for any purpose. It is          *
18 // provided "as is" without express or implied warranty.                  *
19 //*************************************************************************/
20
21 /** @file   AliHLTTPCDigitReaderUnpacked.cxx
22     @author Timm Steinbeck, Jochen Thaeder, Matthias Richter
23     @date   
24     @brief  A digit reader implementation for unpacked TPC data.
25 */
26
27 #if __GNUC__== 3
28 using namespace std;
29 #endif
30
31 #include <cassert>
32 #include "AliHLTTPCDigitReaderUnpacked.h"
33 #include "AliHLTTPCDigitData.h"
34 #include "AliHLTTPCTransform.h"
35 #include "AliHLTStdIncludes.h"
36 #include "AliHLTTPCMapping.h"
37
38 ClassImp(AliHLTTPCDigitReaderUnpacked)
39
40 AliHLTTPCDigitReaderUnpacked::AliHLTTPCDigitReaderUnpacked()
41   :
42   fDigitRowData(NULL),
43   fActRowData(NULL),
44   fData(NULL),
45   fPtr(NULL),
46   fSize(0),
47   fBin(0),
48   fRow(0),
49   fFirstRow(0),
50   fLastRow(0),
51   fUnsorted(kFALSE),
52   fDataBunch(),
53   fTrackIDs(),
54   fTrackIDCounts(),
55   fEndOfDataReached(kFALSE),
56   fEndOfChannelReached(kFALSE),
57   fPrevTime(0),
58   fEndTimeBinOfBunch(0),
59   fPrevSignal(0),
60   fPrevPad(0),
61   fPrevRow(-1),
62   fNextChannelIsAlreadyConfirmed(kFALSE),
63   fMapping(NULL),
64   fDigitsVector(),
65   fPatch(0)
66 {
67   // see header file for class documentation
68   // or
69   // refer to README to build package
70   // or
71   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
72 }
73
74 AliHLTTPCDigitReaderUnpacked::~AliHLTTPCDigitReaderUnpacked(){
75   // see header file for class documentation
76   if(fMapping){
77     delete fMapping;
78   }
79   fMapping=NULL;
80 }
81
82 int AliHLTTPCDigitReaderUnpacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
83   // see header file for class documentation
84   int iResult=0;
85   AliHLTTPCUnpackedRawData *tmpptr=NULL;
86   fPtr = ptr;
87   fSize = size;
88
89   fPatch=patch;
90   fEndOfDataReached=kFALSE;
91   fEndOfChannelReached=kFALSE;
92   fPrevTime=0;
93   fEndTimeBinOfBunch=0;
94   fPrevSignal=0;
95   fPrevPad=0;
96   fPrevRow=-1;
97   fNextChannelIsAlreadyConfirmed=kFALSE;
98   fDigitsVector.clear();
99
100   tmpptr = reinterpret_cast<AliHLTTPCUnpackedRawData*>(fPtr);
101   fDigitRowData = (AliHLTTPCDigitRowData*) tmpptr->fDigits;
102   fActRowData = fDigitRowData;
103
104   if(!fMapping){
105     fMapping = new AliHLTTPCMapping(patch);
106   }
107
108   while (fActRowData && ((iResult=GetNextRowData(fActRowData))>=0)) {/* empty body */};
109
110   if (iResult>=0) {
111   fActRowData = fDigitRowData;
112   fBin = -1;
113
114   int dummy=0;
115   AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetFirstRow(patch), dummy, fFirstRow);
116   AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetLastRow(patch), dummy, fLastRow);
117
118   fRow = fFirstRow; 
119
120   if ((Int_t)fActRowData->fRow != fRow){
121       HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
122   }
123   } else {
124     fActRowData=NULL;
125   }
126   return iResult;
127 }
128
129 int AliHLTTPCDigitReaderUnpacked::GetNextRowData(AliHLTTPCDigitRowData*& pRow) const
130 {
131   // get new row data from the current row data
132   int iResult=0;
133   AliHLTTPCDigitRowData* pCurrent=pRow;
134   assert(pCurrent);
135   pRow=NULL;
136   Byte_t *tmp = (Byte_t*) pCurrent;
137   Int_t size = sizeof(AliHLTTPCDigitRowData) + pCurrent->fNDigit*sizeof(AliHLTTPCDigitData);
138   tmp += size;
139   pRow = reinterpret_cast<AliHLTTPCDigitRowData*>(tmp);
140   
141   // check if the new pointer is within the range
142   if (((Byte_t*)fPtr) + fSize <= tmp){
143     if (((Byte_t*)fPtr) + fSize < tmp) {
144       // if the increment does not match exactly there is a format error
145       HLTError("input format not recognized: buffer %p %d, current row data %p, %d digits", fPtr, fSize, pCurrent, pCurrent->fNDigit);
146       iResult=-EBADF;
147     }
148     pRow=NULL;
149   } else {
150     // check if the current row structure has the right format
151     size = sizeof(AliHLTTPCDigitRowData) + pRow->fNDigit*sizeof(AliHLTTPCDigitData);
152     tmp += size;
153     if (((Byte_t*)fPtr) + fSize < tmp){
154       HLTError("Current row data not recognized %p (buffer %p %d) %d digits", pRow, fPtr, fSize, pRow->fNDigit);
155       pRow=NULL;
156       iResult=-EBADF;
157     }
158   }
159     
160   return iResult;
161 }
162
163 bool AliHLTTPCDigitReaderUnpacked::NextSignal(){
164   // see header file for class documentation
165   if (fActRowData==NULL) return false;
166
167   bool rreadvalue = true;
168
169   fBin++;
170
171   if ( fBin >= (Int_t)fActRowData->fNDigit ){
172     fRow++;
173     if ((fRow >= fFirstRow) && (fRow <= fLastRow)){
174
175       //new row 
176       if (GetNextRowData(fActRowData)<0) {
177         rreadvalue = false;
178         return rreadvalue;
179       }
180
181       fBin = 0;
182     }
183     else {
184       rreadvalue = false;
185       return rreadvalue;
186     }
187   if(!fActRowData){
188     return false;
189   }
190     if ((Int_t)fActRowData->fRow != fRow){
191       HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
192     }
193   }
194
195   fData = fActRowData->fDigitData;
196  
197   return rreadvalue;
198 }
199
200 int AliHLTTPCDigitReaderUnpacked::GetRow(){
201   // see header file for class documentation
202   int rrow;
203
204   if(fUnsorted == kFALSE){
205     rrow = fRow;
206   }
207   else{
208    rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
209    if(fPatch>1){
210     rrow += AliHLTTPCTransform::GetFirstRow(2);
211    }
212   }
213
214   return rrow;
215 }
216
217 int AliHLTTPCDigitReaderUnpacked::GetPad(){
218   // see header file for class documentation
219   int rpad;
220   if(fUnsorted == kFALSE){
221     rpad = GetSortedPad();
222   }
223   else{
224     rpad = fPrevPad;
225   }
226   return rpad   ;
227 }
228
229 int AliHLTTPCDigitReaderUnpacked::GetSignal(){ 
230   // see header file for class documentation
231   int rsignal;
232   if(fUnsorted == kFALSE){
233     rsignal = GetSortedSignal();
234   }
235   else{
236     rsignal = fPrevSignal;
237   }
238   return rsignal;
239 }
240
241 int AliHLTTPCDigitReaderUnpacked::GetTime(){
242   // see header file for class documentation
243   int rtime;
244   if(fUnsorted==kFALSE){
245     rtime = GetSortedTime();
246   }
247   else{
248     rtime = fPrevTime+1-fDataBunch.size();
249   }
250   return rtime;
251 }
252
253 AliHLTUInt32_t AliHLTTPCDigitReaderUnpacked::GetAltroBlockHWaddr() const
254 {
255   // see header file for class documentation
256   return (AliHLTUInt32_t)(fMapping->GetHwAddress((UInt_t)GetSortedRow(),(UInt_t)GetSortedPad()));//fTPCRawStream->GetHWAddress();
257 }
258
259 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedTime(){
260   // see header file for class documentation
261   assert(fData);
262   if (!fData) return -1;
263   int rtime;
264   rtime = (int)fData[fBin].fTime;
265   if(fDataBunch.size()>1){
266     fEndTimeBinOfBunch=rtime;
267   }
268   return rtime;
269 }
270
271 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedSignal(){
272   // see header file for class documentation
273   assert(fData);
274   if (!fData) return -1;
275   int rsignal;
276   rsignal = (int)fData[fBin].fCharge;
277   return rsignal;
278
279 }
280
281 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedPad() const{
282   // see header file for class documentation
283   assert(fData);
284   if (!fData) return -1;
285   int rpad;
286   rpad = (int)fData[fBin].fPad;
287   return rpad   ;
288 }
289
290 int AliHLTTPCDigitReaderUnpacked::GetSortedRow() const {
291   // see header file for class documentation
292   int rrow;
293   rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
294   if(fPatch>1){
295     rrow += AliHLTTPCTransform::GetFirstRow(2);
296   }
297   return rrow;
298 }
299
300 bool AliHLTTPCDigitReaderUnpacked::NextChannel()
301 {
302   // see header file for class documentation
303
304   // If the next channel is already confirmed by the next bunch function
305   // or there are more signals (this will only be for the first signal)
306   
307   if(fEndOfDataReached == kTRUE){
308     return false;
309   }
310   if(fNextChannelIsAlreadyConfirmed){
311     fNextChannelIsAlreadyConfirmed = kFALSE;
312     fPrevTime=GetSortedTime();
313     fPrevSignal=GetSortedSignal();
314     fPrevPad = GetSortedPad();
315     fPrevRow = GetSortedRow();
316     return true;
317   }
318   else if(NextSignal()) { // there is data
319     return true;
320   }
321   return false;
322 }
323
324 int AliHLTTPCDigitReaderUnpacked::NextBunch()
325 {  
326   // see header file for class documentation
327
328   if(fEndOfDataReached == kTRUE || fEndOfChannelReached == kTRUE){
329     // sets fEndOfChannelReached back to false, for the next channel
330     // and returns 0 to tell stop the NextBunch calls for this channel.
331     fEndOfChannelReached=kFALSE;
332     return 0;
333   }
334
335
336   fDataBunch.clear();
337   fDigitsVector.clear();
338
339   //adding the first signal (will always be the leftover from either NextChannel call or previous bunch) 
340   fPrevTime=GetSortedTime();
341   fPrevSignal=GetSortedSignal();
342   fPrevPad = GetSortedPad();
343   fPrevRow = GetSortedRow();
344   fDataBunch.push_back(GetSortedSignal());
345   fDigitsVector.push_back(fData[fBin]);
346
347   do{
348     if(NextSignal()){
349         if((fPrevPad == GetSortedPad()) && (fPrevRow == GetSortedRow())){//check if there is a change in channel(new pad or row)
350           if(fPrevTime+1 == GetSortedTime()){//if true means that we have consecutive signals
351             fPrevTime = GetSortedTime();
352             //fDataBunch.insert(fDataBunch.begin(), GetSortedSignal());// add the signal to the beginning of the buffer     
353             fDataBunch.push_back(GetSortedSignal());// add the signal to the beginning of the buffer
354             fDigitsVector.push_back(fData[fBin]);
355           }
356           else{//end of bunch but not of channel
357             break;
358           }
359         }
360         else{
361           // end of channel, last bunch will be completed
362           fEndOfChannelReached = kTRUE;
363           // the next channel is already confirmed since the next channel returned true
364           fNextChannelIsAlreadyConfirmed = kTRUE;
365           break;
366         }
367     }
368     else{
369       // end of data, but there is one bunch to be completed.
370       fEndOfDataReached = kTRUE;
371       break;
372     }
373   }while(1);
374
375   return fDataBunch.size();
376 }
377
378 int AliHLTTPCDigitReaderUnpacked::GetBunchSize(){
379   // see header file for class documentation
380   return fDataBunch.size();
381 }
382
383 const UInt_t* AliHLTTPCDigitReaderUnpacked::GetSignals()
384 {
385   // see header file for class documentation
386   return &fDataBunch[0];
387 }
388
389 const AliHLTTPCDigitData* AliHLTTPCDigitReaderUnpacked::GetBunchDigits()
390 {
391   // see header file for class documentation
392   return &fDigitsVector[0];
393 }
394 int AliHLTTPCDigitReaderUnpacked::GetRowOffset() const
395 {
396   // see header file for class documentation
397   return AliHLTTPCTransform::GetFirstRow(fPatch);
398 }