]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/AliHLTTPCDigitReaderUnpacked.cxx
adding protection for invalid array index
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCDigitReaderUnpacked.cxx
CommitLineData
a38a7850 1// $Id$
2
67fada6b 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//*************************************************************************/
a38a7850 20
84645eb0 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*/
a38a7850 26
27#if __GNUC__== 3
28using namespace std;
29#endif
30
67fada6b 31#include <cassert>
a38a7850 32#include "AliHLTTPCDigitReaderUnpacked.h"
33#include "AliHLTTPCDigitData.h"
84645eb0 34#include "AliHLTTPCTransform.h"
35#include "AliHLTStdIncludes.h"
deba5d85 36#include "AliHLTTPCMapping.h"
a38a7850 37
84645eb0 38ClassImp(AliHLTTPCDigitReaderUnpacked)
a38a7850 39
84645eb0 40AliHLTTPCDigitReaderUnpacked::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),
deba5d85 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),
959cfda3 60 fPrevPad(-1),
deba5d85 61 fPrevRow(-1),
62 fNextChannelIsAlreadyConfirmed(kFALSE),
63 fMapping(NULL),
64 fDigitsVector(),
959cfda3 65 fBinRowPositionSorted(),
deba5d85 66 fPatch(0)
84645eb0 67{
5df0cbb9 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
84645eb0 73}
a38a7850 74
a38a7850 75AliHLTTPCDigitReaderUnpacked::~AliHLTTPCDigitReaderUnpacked(){
5df0cbb9 76 // see header file for class documentation
37c9ee0f 77 if(fMapping){
78 delete fMapping;
79 }
80 fMapping=NULL;
a38a7850 81}
82
84645eb0 83int AliHLTTPCDigitReaderUnpacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
5df0cbb9 84 // see header file for class documentation
67fada6b 85 int iResult=0;
5df0cbb9 86 AliHLTTPCUnpackedRawData *tmpptr=NULL;
a38a7850 87 fPtr = ptr;
88 fSize = size;
89
deba5d85 90 fPatch=patch;
37c9ee0f 91 fEndOfDataReached=kFALSE;
92 fEndOfChannelReached=kFALSE;
93 fPrevTime=0;
94 fEndTimeBinOfBunch=0;
95 fPrevSignal=0;
959cfda3 96 fPrevPad=-1;
37c9ee0f 97 fPrevRow=-1;
98 fNextChannelIsAlreadyConfirmed=kFALSE;
99 fDigitsVector.clear();
deba5d85 100
d3905cf0 101 tmpptr = reinterpret_cast<AliHLTTPCUnpackedRawData*>(fPtr);
a38a7850 102 fDigitRowData = (AliHLTTPCDigitRowData*) tmpptr->fDigits;
67fada6b 103 fActRowData = fDigitRowData;
a38a7850 104
deba5d85 105 if(!fMapping){
106 fMapping = new AliHLTTPCMapping(patch);
107 }
108
95e36f3d 109 while (fActRowData && ((iResult=GetNextRowData(fActRowData))>=0)) {/* empty body */};
67fada6b 110
111 if (iResult>=0) {
112 fActRowData = fDigitRowData;
a38a7850 113 fBin = -1;
114
055fed30 115 int dummy=0;
116 AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetFirstRow(patch), dummy, fFirstRow);
117 AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetLastRow(patch), dummy, fLastRow);
84645eb0 118
a38a7850 119 fRow = fFirstRow;
120
121 if ((Int_t)fActRowData->fRow != fRow){
84645eb0 122 HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
a38a7850 123 }
67fada6b 124 } else {
125 fActRowData=NULL;
126 }
127 return iResult;
128}
129
130int 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;
25f6555b 137 if (!pCurrent) return -EINVAL;
67fada6b 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;
a38a7850 163}
959cfda3 164void 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}
a38a7850 192
f44e97dc 193bool AliHLTTPCDigitReaderUnpacked::NextSignal(){
5df0cbb9 194 // see header file for class documentation
67fada6b 195 if (fActRowData==NULL) return false;
196
a38a7850 197 bool rreadvalue = true;
198
199 fBin++;
200
0191ac8f 201 while ( fBin >= (Int_t)fActRowData->fNDigit ){
a38a7850 202 fRow++;
a38a7850 203 if ((fRow >= fFirstRow) && (fRow <= fLastRow)){
0191ac8f 204
a38a7850 205 //new row
67fada6b 206 if (GetNextRowData(fActRowData)<0) {
a38a7850 207 rreadvalue = false;
208 return rreadvalue;
209 }
a38a7850 210 fBin = 0;
211 }
212 else {
213 rreadvalue = false;
214 return rreadvalue;
215 }
0191ac8f 216 if(!fActRowData){
217 return false;
218 }
a38a7850 219 if ((Int_t)fActRowData->fRow != fRow){
84645eb0 220 HLTWarning("Row number should match! fActRowData->fRow=%d fRow=%d", fActRowData->fRow, fRow);
a38a7850 221 }
222 }
0191ac8f 223
a38a7850 224 fData = fActRowData->fDigitData;
959cfda3 225
226 if(fBin==0){
227 SortBunchBinVector();
228 }
229
230 if(fPrevPad == -1){
231 fPrevPad = GetSortedPad();
232 }
a38a7850 233
234 return rreadvalue;
235}
236
237int AliHLTTPCDigitReaderUnpacked::GetRow(){
5df0cbb9 238 // see header file for class documentation
a38a7850 239 int rrow;
deba5d85 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
a38a7850 251 return rrow;
252}
253
254int AliHLTTPCDigitReaderUnpacked::GetPad(){
5df0cbb9 255 // see header file for class documentation
a38a7850 256 int rpad;
deba5d85 257 if(fUnsorted == kFALSE){
258 rpad = GetSortedPad();
259 }
260 else{
261 rpad = fPrevPad;
262 }
a38a7850 263 return rpad ;
264}
265
266int AliHLTTPCDigitReaderUnpacked::GetSignal(){
5df0cbb9 267 // see header file for class documentation
a38a7850 268 int rsignal;
deba5d85 269 if(fUnsorted == kFALSE){
270 rsignal = GetSortedSignal();
271 }
272 else{
273 rsignal = fPrevSignal;
274 }
a38a7850 275 return rsignal;
276}
277
278int AliHLTTPCDigitReaderUnpacked::GetTime(){
deba5d85 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
290AliHLTUInt32_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
deeb43e0 296AliHLTTPCDigitData 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
deba5d85 303Int_t AliHLTTPCDigitReaderUnpacked::GetSortedTime(){
5df0cbb9 304 // see header file for class documentation
67fada6b 305 assert(fData);
f44e97dc 306 if (!fData) return -1;
a38a7850 307 int rtime;
959cfda3 308 rtime = (int)fData[fBinRowPositionSorted.at(fBin)].fTime;
deba5d85 309 if(fDataBunch.size()>1){
310 fEndTimeBinOfBunch=rtime;
311 }
a38a7850 312 return rtime;
313}
deba5d85 314
315Int_t AliHLTTPCDigitReaderUnpacked::GetSortedSignal(){
316 // see header file for class documentation
317 assert(fData);
318 if (!fData) return -1;
319 int rsignal;
959cfda3 320 rsignal = (int)fData[fBinRowPositionSorted.at(fBin)].fCharge;
deba5d85 321 return rsignal;
322
323}
324
325Int_t AliHLTTPCDigitReaderUnpacked::GetSortedPad() const{
326 // see header file for class documentation
327 assert(fData);
328 if (!fData) return -1;
329 int rpad;
959cfda3 330 rpad = (int)fData[fBinRowPositionSorted.at(fBin)].fPad;
deba5d85 331 return rpad ;
332}
333
334int 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
344bool 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();
959cfda3 360
361 if(GetAltroBlockHWaddr() == (UInt_t)-1){
362 fPrevPad = -1;
363 return NextChannel();
364 }
365
deba5d85 366 return true;
367 }
368 else if(NextSignal()) { // there is data
959cfda3 369
370 if(GetAltroBlockHWaddr() == (UInt_t)-1){
371 fPrevPad = -1;
372 return NextChannel();
373 }
deba5d85 374 return true;
375 }
376 return false;
377}
378
379int 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());
deeb43e0 400 fDigitsVector.push_back(GetSortedDigit());
deba5d85 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
deeb43e0 409 fDigitsVector.push_back(GetSortedDigit());
deba5d85 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
433int AliHLTTPCDigitReaderUnpacked::GetBunchSize(){
434 // see header file for class documentation
435 return fDataBunch.size();
436}
437
438const UInt_t* AliHLTTPCDigitReaderUnpacked::GetSignals()
439{
440 // see header file for class documentation
441 return &fDataBunch[0];
442}
443
444const AliHLTTPCDigitData* AliHLTTPCDigitReaderUnpacked::GetBunchDigits()
445{
446 // see header file for class documentation
447 return &fDigitsVector[0];
448}
449int AliHLTTPCDigitReaderUnpacked::GetRowOffset() const
450{
451 // see header file for class documentation
452 return AliHLTTPCTransform::GetFirstRow(fPatch);
453}