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