]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitReaderUnpacked.cxx
code cleanup, removing unused functionality; fully implemented in the AliHLTTPCDataCo...
[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(-1),
61   fPrevRow(-1),
62   fNextChannelIsAlreadyConfirmed(kFALSE),
63   fMapping(NULL),
64   fDigitsVector(),
65   fBinRowPositionSorted(),
66   fPatch(0)
67 {
68   // see header file for class documentation
69   // or
70   // refer to README to build package
71   // or
72   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
73 }
74
75 AliHLTTPCDigitReaderUnpacked::~AliHLTTPCDigitReaderUnpacked(){
76   // see header file for class documentation
77   if(fMapping){
78     delete fMapping;
79   }
80   fMapping=NULL;
81 }
82
83 int AliHLTTPCDigitReaderUnpacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
84   // see header file for class documentation
85   int iResult=0;
86   AliHLTTPCUnpackedRawData *tmpptr=NULL;
87   fPtr = ptr;
88   fSize = size;
89
90   fPatch=patch;
91   fEndOfDataReached=kFALSE;
92   fEndOfChannelReached=kFALSE;
93   fPrevTime=0;
94   fEndTimeBinOfBunch=0;
95   fPrevSignal=0;
96   fPrevPad=-1;
97   fPrevRow=-1;
98   fNextChannelIsAlreadyConfirmed=kFALSE;
99   fDigitsVector.clear();
100
101   tmpptr = reinterpret_cast<AliHLTTPCUnpackedRawData*>(fPtr);
102   fDigitRowData = (AliHLTTPCDigitRowData*) tmpptr->fDigits;
103   fActRowData = fDigitRowData;
104
105   if(!fMapping){
106     fMapping = new AliHLTTPCMapping(patch);
107   }
108
109   while (fActRowData && ((iResult=GetNextRowData(fActRowData))>=0)) {/* empty body */};
110
111   if (iResult>=0) {
112   fActRowData = fDigitRowData;
113   fBin = -1;
114
115   int dummy=0;
116   AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetFirstRow(patch), dummy, fFirstRow);
117   AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetLastRow(patch), dummy, fLastRow);
118
119   fRow = fFirstRow; 
120
121   if ((Int_t)fActRowData->fRow != fRow){
122       HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
123   }
124   } else {
125     fActRowData=NULL;
126   }
127   return iResult;
128 }
129
130 int AliHLTTPCDigitReaderUnpacked::GetNextRowData(AliHLTTPCDigitRowData*& pRow) const
131 {
132   // get new row data from the current row data
133   int iResult=0;
134   AliHLTTPCDigitRowData* pCurrent=pRow;
135   assert(pCurrent);
136   pRow=NULL;
137   if (!pCurrent) return -EINVAL;
138   Byte_t *tmp = (Byte_t*) pCurrent;
139   Int_t size = sizeof(AliHLTTPCDigitRowData) + pCurrent->fNDigit*sizeof(AliHLTTPCDigitData);
140   tmp += size;
141   pRow = reinterpret_cast<AliHLTTPCDigitRowData*>(tmp);
142   
143   // check if the new pointer is within the range
144   if (((Byte_t*)fPtr) + fSize <= tmp){
145     if (((Byte_t*)fPtr) + fSize < tmp) {
146       // if the increment does not match exactly there is a format error
147       HLTError("input format not recognized: buffer %p %d, current row data %p, %d digits", fPtr, fSize, pCurrent, pCurrent->fNDigit);
148       iResult=-EBADF;
149     }
150     pRow=NULL;
151   } else {
152     // check if the current row structure has the right format
153     size = sizeof(AliHLTTPCDigitRowData) + pRow->fNDigit*sizeof(AliHLTTPCDigitData);
154     tmp += size;
155     if (((Byte_t*)fPtr) + fSize < tmp){
156       HLTError("Current row data not recognized %p (buffer %p %d) %d digits", pRow, fPtr, fSize, pRow->fNDigit);
157       pRow=NULL;
158       iResult=-EBADF;
159     }
160   }
161     
162   return iResult;
163 }
164 void AliHLTTPCDigitReaderUnpacked::SortBunchBinVector(){
165   fBinRowPositionSorted.clear();
166
167   fBinRowPositionSorted.push_back(0);
168   Int_t nAdded=0;
169   Int_t beginningOfPadIndex=0;
170   Int_t totalAdded=0;
171   for(Int_t i=1;i<(Int_t)fActRowData->fNDigit;i++){
172      if(fData[i-1].fPad == fData[i].fPad){// means that these sinals belong to the same pad
173       if(fData[i-1].fTime+1 == fData[i].fTime){ //means that the signal belong to the same bunch
174         nAdded++;
175         totalAdded++;
176         fBinRowPositionSorted.insert(fBinRowPositionSorted.begin()+beginningOfPadIndex+nAdded,i);
177       }
178       else{//we have a new bunch on this pad, put it in fornt of the previous bunch
179         totalAdded++;
180         nAdded=0;
181         fBinRowPositionSorted.insert(fBinRowPositionSorted.begin()+beginningOfPadIndex,i);
182       }
183     }
184     else{
185       totalAdded++;
186       beginningOfPadIndex=totalAdded;
187       fBinRowPositionSorted.push_back(i);
188       nAdded=0;
189     }
190   }
191 }
192
193 bool AliHLTTPCDigitReaderUnpacked::NextSignal(){
194   // see header file for class documentation
195   if (fActRowData==NULL) return false;
196
197   bool rreadvalue = true;
198
199   fBin++;
200
201   while ( fBin >= (Int_t)fActRowData->fNDigit ){
202     fRow++;
203     if ((fRow >= fFirstRow) && (fRow <= fLastRow)){
204       
205       //new row 
206       if (GetNextRowData(fActRowData)<0) {
207         rreadvalue = false;
208         return rreadvalue;
209       }
210       fBin = 0;
211     }
212     else {
213       rreadvalue = false;
214       return rreadvalue;
215     }
216     if(!fActRowData){
217       return false;
218     }
219     if ((Int_t)fActRowData->fRow != fRow){
220       HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
221     }
222   }
223   
224   fData = fActRowData->fDigitData;
225
226   if(fBin==0){
227     SortBunchBinVector(); 
228   }
229
230   if(fPrevPad == -1){
231     fPrevPad = GetSortedPad();
232   }
233  
234   return rreadvalue;
235 }
236
237 int AliHLTTPCDigitReaderUnpacked::GetRow(){
238   // see header file for class documentation
239   int rrow;
240
241   if(fUnsorted == kFALSE){
242     rrow = fRow;
243   }
244   else{
245    rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
246    if(fPatch>1){
247     rrow += AliHLTTPCTransform::GetFirstRow(2);
248    }
249   }
250
251   return rrow;
252 }
253
254 int AliHLTTPCDigitReaderUnpacked::GetPad(){
255   // see header file for class documentation
256   int rpad;
257   if(fUnsorted == kFALSE){
258     rpad = GetSortedPad();
259   }
260   else{
261     rpad = fPrevPad;
262   }
263   return rpad   ;
264 }
265
266 int AliHLTTPCDigitReaderUnpacked::GetSignal(){ 
267   // see header file for class documentation
268   int rsignal;
269   if(fUnsorted == kFALSE){
270     rsignal = GetSortedSignal();
271   }
272   else{
273     rsignal = fPrevSignal;
274   }
275   return rsignal;
276 }
277
278 int AliHLTTPCDigitReaderUnpacked::GetTime(){
279   // see header file for class documentation
280   int rtime;
281   if(fUnsorted==kFALSE){
282     rtime = GetSortedTime();
283   }
284   else{
285     rtime = fPrevTime+1-fDataBunch.size();
286   }
287   return rtime;
288 }
289
290 AliHLTUInt32_t AliHLTTPCDigitReaderUnpacked::GetAltroBlockHWaddr() const
291 {
292   // see header file for class documentation
293   return (AliHLTUInt32_t)(fMapping->GetHwAddress((UInt_t)GetSortedRow(),(UInt_t)GetSortedPad()));//fTPCRawStream->GetHWAddress();
294 }
295
296 AliHLTTPCDigitData AliHLTTPCDigitReaderUnpacked::GetSortedDigit(){
297   // see header file for class documentation
298   assert(fData);
299   if (!fData) return AliHLTTPCDigitData();
300   return fData[fBinRowPositionSorted.at(fBin)];
301 }
302
303 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedTime(){
304   // see header file for class documentation
305   assert(fData);
306   if (!fData) return -1;
307   int rtime;
308   rtime = (int)fData[fBinRowPositionSorted.at(fBin)].fTime;
309   if(fDataBunch.size()>1){
310     fEndTimeBinOfBunch=rtime;
311   }
312   return rtime;
313 }
314
315 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedSignal(){
316   // see header file for class documentation
317   assert(fData);
318   if (!fData) return -1;
319   int rsignal;
320   rsignal = (int)fData[fBinRowPositionSorted.at(fBin)].fCharge;
321   return rsignal;
322
323 }
324
325 Int_t AliHLTTPCDigitReaderUnpacked::GetSortedPad() const{
326   // see header file for class documentation
327   assert(fData);
328   if (!fData) return -1;
329   int rpad;
330   rpad = (int)fData[fBinRowPositionSorted.at(fBin)].fPad;
331   return rpad   ;
332 }
333
334 int AliHLTTPCDigitReaderUnpacked::GetSortedRow() const {
335   // see header file for class documentation
336   int rrow;
337   rrow = fRow-AliHLTTPCTransform::GetFirstRow(fPatch);
338   if(fPatch>1){
339     rrow += AliHLTTPCTransform::GetFirstRow(2);
340   }
341   return rrow;
342 }
343
344 bool AliHLTTPCDigitReaderUnpacked::NextChannel()
345 {
346   // see header file for class documentation
347
348   // If the next channel is already confirmed by the next bunch function
349   // or there are more signals (this will only be for the first signal)
350   
351   if(fEndOfDataReached == kTRUE){
352     return false;
353   }
354   if(fNextChannelIsAlreadyConfirmed){
355     fNextChannelIsAlreadyConfirmed = kFALSE;
356     fPrevTime=GetSortedTime();
357     fPrevSignal=GetSortedSignal();
358     fPrevPad = GetSortedPad();
359     fPrevRow = GetSortedRow();
360
361     if(GetAltroBlockHWaddr() == (UInt_t)-1){
362       fPrevPad = -1;
363       return NextChannel();
364     }
365
366     return true;
367   }
368   else if(NextSignal()) { // there is data
369
370     if(GetAltroBlockHWaddr() == (UInt_t)-1){
371       fPrevPad = -1;
372       return NextChannel();
373     }
374     return true;
375   }
376   return false;
377 }
378
379 int AliHLTTPCDigitReaderUnpacked::NextBunch()
380 {  
381   // see header file for class documentation
382
383   if(fEndOfDataReached == kTRUE || fEndOfChannelReached == kTRUE){
384     // sets fEndOfChannelReached back to false, for the next channel
385     // and returns 0 to tell stop the NextBunch calls for this channel.
386     fEndOfChannelReached=kFALSE;
387     return 0;
388   }
389
390
391   fDataBunch.clear();
392   fDigitsVector.clear();
393
394   //adding the first signal (will always be the leftover from either NextChannel call or previous bunch) 
395   fPrevTime=GetSortedTime();
396   fPrevSignal=GetSortedSignal();
397   fPrevPad = GetSortedPad();
398   fPrevRow = GetSortedRow();
399   fDataBunch.push_back(GetSortedSignal());
400   fDigitsVector.push_back(GetSortedDigit());
401
402   do{
403     if(NextSignal()){
404         if((fPrevPad == GetSortedPad()) && (fPrevRow == GetSortedRow())){//check if there is a change in channel(new pad or row)
405           if(fPrevTime+1 == GetSortedTime()){//if true means that we have consecutive signals
406             fPrevTime = GetSortedTime();
407             //fDataBunch.insert(fDataBunch.begin(), GetSortedSignal());// add the signal to the beginning of the buffer     
408             fDataBunch.push_back(GetSortedSignal());// add the signal to the beginning of the buffer
409             fDigitsVector.push_back(GetSortedDigit());
410           }
411           else{//end of bunch but not of channel
412             break;
413           }
414         }
415         else{
416           // end of channel, last bunch will be completed
417           fEndOfChannelReached = kTRUE;
418           // the next channel is already confirmed since the next channel returned true
419           fNextChannelIsAlreadyConfirmed = kTRUE;
420           break;
421         }
422     }
423     else{
424       // end of data, but there is one bunch to be completed.
425       fEndOfDataReached = kTRUE;
426       break;
427     }
428   }while(1);
429
430   return fDataBunch.size();
431 }
432
433 int AliHLTTPCDigitReaderUnpacked::GetBunchSize(){
434   // see header file for class documentation
435   return fDataBunch.size();
436 }
437
438 const UInt_t* AliHLTTPCDigitReaderUnpacked::GetSignals()
439 {
440   // see header file for class documentation
441   return &fDataBunch[0];
442 }
443
444 const AliHLTTPCDigitData* AliHLTTPCDigitReaderUnpacked::GetBunchDigits()
445 {
446   // see header file for class documentation
447   return &fDigitsVector[0];
448 }
449 int AliHLTTPCDigitReaderUnpacked::GetRowOffset() const
450 {
451   // see header file for class documentation
452   return AliHLTTPCTransform::GetFirstRow(fPatch);
453 }