3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
8 * Jochen Thaeder <thaeder@kip.uni-heidelberg.de> *
9 * for The ALICE Off-line Project. *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
20 /** @file AliHLTTPCDigitReaderRaw.cxx
21 @author Timm Steinbeck
23 @brief A digit reader implementation for the RAW data coming from the RCU.
30 #if defined(HAVE_TPC_MAPPING)
32 #include "AliHLTTPCDigitReaderRaw.h"
33 #include "AliHLTTPCTransform.h"
34 #include "AliHLTTPCRootTypes.h"
35 #include "AliHLTStdIncludes.h"
36 #include "AliHLTTPCLogging.h"
38 ClassImp(AliHLTTPCDigitReaderRaw)
40 AliHLTTPCDigitReaderRaw::AliHLTTPCDigitReaderRaw( unsigned formatVersion )
46 fDataFormatVersion(formatVersion),
60 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
63 // get max number of rows
64 for (Int_t ii=0; ii < 6; ii++)
65 if (AliHLTTPCTransform::GetNRows(ii) > fNMaxRows)
66 fNMaxRows = AliHLTTPCTransform::GetNRows(ii);
68 // get max number of pads
69 for (Int_t ii=0; ii < AliHLTTPCTransform::GetNRows();ii++ )
70 if (AliHLTTPCTransform::GetNPads(ii) > fNMaxPads)
71 fNMaxPads = AliHLTTPCTransform::GetNPads(ii);
73 // get max number of bins
74 fNTimeBins = AliHLTTPCTransform::GetNTimeBins();
76 HLTDebug("Array Borders ||| MAXPAD=%d ||| MAXROW=%d ||| MAXBIN=%d ||| MAXMUL=%d",
77 fNMaxPads, fNMaxRows, fNTimeBins, fNTimeBins*fNMaxRows*fNMaxPads);
80 fData = new Int_t[ fNMaxRows*fNMaxPads*fNTimeBins ];
84 AliHLTTPCDigitReaderRaw::AliHLTTPCDigitReaderRaw(const AliHLTTPCDigitReaderRaw& src)
90 fDataFormatVersion(src.fDataFormatVersion),
104 HLTFatal("copy constructor not for use");
107 AliHLTTPCDigitReaderRaw& AliHLTTPCDigitReaderRaw::operator=(const AliHLTTPCDigitReaderRaw& src)
113 fDataFormatVersion=src.fDataFormatVersion;
126 HLTFatal("assignment operator not for use");
130 AliHLTTPCDigitReaderRaw::~AliHLTTPCDigitReaderRaw(){
131 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
139 int AliHLTTPCDigitReaderRaw::InitBlock(void* ptr,unsigned long size,Int_t firstrow,Int_t lastrow, Int_t patch, Int_t slice) {
140 return AliHLTTPCDigitReader::InitBlock(ptr, size, firstrow, lastrow, patch, slice);
143 int AliHLTTPCDigitReaderRaw::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
145 fBuffer = (AliHLTUInt8_t*) ptr;
147 HLTError("invalid data buffer");
151 if (fBufferSize<=0) HLTWarning("no data available: zero length buffer");
157 fAltroBlockPositionBytes = 0;
158 fAltroBlockLengthBytes = 0;
159 fAltroBlock10BitWordCnt = 0xFFFFU;
160 fAltroBlockHWAddress = 0xFFFFU;
161 fBunchPosition = 0xFFFFU;
162 fBunchTimebinStart = ~0U;
164 fWordInBunch = (unsigned)-1;
166 Int_t firstrow=AliHLTTPCTransform::GetFirstRow(patch);
167 Int_t lastrow=AliHLTTPCTransform::GetLastRow(patch);
169 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
175 fNRows = lastrow - firstrow + 1;
178 if (patch > 1) offset = AliHLTTPCTransform::GetFirstRow( 2 );
180 fRowOffset = firstrow - offset;
184 // Init array with -1
185 memset( fData, 0xFF, sizeof(Int_t)*(fNMaxRows*fNMaxPads*fNTimeBins) );
187 const Int_t maxErrorPrintout=20;
190 // read data and fill in array
195 Int_t row = GetRealRow();
196 Int_t pad = GetRealPad();
197 Int_t bin = GetRealTime();
199 // HLTFatal("Index out of array range: PAD=%d ||| ROW=%d ||| BIN=%d ||| OFFSET=%d ||| ROWOFFSET=%d", pad, row, bin, offset, fRowOffset);
201 if ( row < firstrow || row > lastrow || pad > AliHLTTPCTransform::GetNPads(row + offset) || bin > fNTimeBins || pad<0 || bin<0){
202 // if ( row < firstrow || row > lastrow || pad > AliHLTTPCTransform::GetNPads(row + offset) || bin > fNTimeBins){
203 if (errorCount++<maxErrorPrintout) {
204 HLTFatal("Index out of range. Probably wrong patch! slice %d - patch %d", slice, patch);
205 HLTFatal("PAD=%d out of %d ||| ROW=%d (%d to %d) ||| BIN=%d out of %d ||| OFFSET=%d ||| ROWOFFSET=%d",
206 pad, AliHLTTPCTransform::GetNPads(row + offset), row, firstrow, lastrow, bin, fNTimeBins,
209 if ( row < firstrow || row > lastrow )
210 HLTFatal("Row out of range: %d ( %d to %d)", row, firstrow, lastrow);
211 if ( pad > AliHLTTPCTransform::GetNPads(row + offset) )
212 HLTFatal("Pad out of range: %d (pad count %d)", pad, AliHLTTPCTransform::GetNPads(row + offset));
213 if ( bin > fNTimeBins )
214 HLTFatal("Time bin out of range: %d (bin count %d)", bin, fNTimeBins);
216 // stop at the fist error message in order to avoid endless messages and
217 // to handle corrupted events
220 } else if ((row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin >= fNMaxRows*fNMaxPads*fNTimeBins ) {
221 if (errorCount++<maxErrorPrintout) {
222 HLTFatal("index out of range: PAD=%d ||| ROW=%d ||| BIN=%d ||| OFFSET=%d ||| ROWOFFSET=%d", pad, row, bin, offset, fRowOffset);
224 // stop at the fist error message in order to avoid endless messages and
225 // to handle corrupted events
229 fData[ (row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin ] = GetRealSignal() ;
233 HLTFatal("%d of %d entries out of range", errorCount, entryCount);
240 bool AliHLTTPCDigitReaderRaw::Next(){
241 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
243 Bool_t readvalue = kTRUE;
246 if (fCurrentBin >= fNTimeBins){
250 if (fCurrentPad >=fNMaxPads){
254 if (fCurrentRow >= fNMaxRows){
261 if (fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin >= fNMaxRows*fNMaxPads*fNTimeBins ) {
262 HLTFatal("Overflow: fCurrentRow=%d fCurrentPad=%d fCurrentBin=%d", fCurrentRow, fCurrentPad, fCurrentBin);
267 if (fData[ fCurrentRow*fNMaxPads*fNTimeBins + fCurrentPad*fNTimeBins + fCurrentBin ] != -1) break;
275 int AliHLTTPCDigitReaderRaw::GetRow(){
276 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
278 return (fCurrentRow + fRowOffset);
283 int AliHLTTPCDigitReaderRaw::GetPad(){
284 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
291 int AliHLTTPCDigitReaderRaw::GetSignal(){
292 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
294 return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
297 return GetRealSignal();
299 int AliHLTTPCDigitReaderRaw::GetTime(){
300 if ( fDataFormatVersion==0 || fDataFormatVersion==2 )
305 return GetRealTime();
309 bool AliHLTTPCDigitReaderRaw::RealNext(){
310 // printf( "%u %u %u %u %u\n", fBunchPosition, fBunchLength, fBunchTimebinStart, fWordInBunch, (unsigned)fAltroBlock10BitWordCnt );
311 fWordInBunch++; // use next word in bunch
312 if ( fWordInBunch==fBunchLength ) { // we have a bunch at all but have reached its end (or do not have an altro block yet)
313 if ( fBunchPosition+fBunchLength==fAltroBlock10BitWordCnt ) { // We were at the last bunch of this altro block (or do not have an altro block yet)
314 if ( !NextAltroBlock() )
319 fBunchPosition += fBunchLength;
321 fBunchLength = GetAltroBlock10BitWord( fBunchPosition );
322 fBunchTimebinStart = GetAltroBlock10BitWord( fBunchPosition+1 );
325 //HLTDebug( "%u %u %u %u %u\n", fBunchPosition, fBunchLength, fBunchTimebinStart, fWordInBunch, (unsigned)fAltroBlock10BitWordCnt );
328 int AliHLTTPCDigitReaderRaw::GetRealRow(){
331 int AliHLTTPCDigitReaderRaw::GetRealPad(){
334 int AliHLTTPCDigitReaderRaw::GetRealSignal(){
335 return GetAltroBlock10BitWord( fBunchPosition+fWordInBunch );
337 int AliHLTTPCDigitReaderRaw::GetRealTime(){
338 //HLTDebug( "GetRealTime: %u - %u\n", fBunchTimebinStart, fWordInBunch );
339 return fBunchTimebinStart-(fWordInBunch-2);
342 AliHLTUInt32_t AliHLTTPCDigitReaderRaw::GetRCUTrailer(){
343 if (fBufferSize<=0) return 0;
344 unsigned rcuDataBlockLen = GetRCUDataBlockLength();
345 return *((AliHLTUInt32_t*)(fBuffer+fBufferSize-rcuDataBlockLen));
348 bool AliHLTTPCDigitReaderRaw::NextAltroBlock()
350 if (fBufferSize<=0) return 0;
351 if ( !fAltroBlockLengthBytes )
353 // First block in back linked list (last block in memory)
354 fAltroBlockPositionBytes = fBufferSize-GetRCUDataBlockLength();
358 if ( fAltroBlockPositionBytes<fAltroBlockLengthBytes+GetCommonDataHeaderSize() )
360 HLTFatal("Inconsistent Data: fAltroBlockPositionBytes=%d fAltroBlockLengthBytes=%d", fAltroBlockPositionBytes, fAltroBlockLengthBytes);
362 if ( fAltroBlockPositionBytes<=fAltroBlockLengthBytes+GetCommonDataHeaderSize() )
363 return false; // We have reached the end of the back linked list
364 fAltroBlockPositionBytes -= fAltroBlockLengthBytes;
367 AliHLTUInt64_t altroTrailerWord = GetAltroBlock40BitWord( 0 );
369 if ( fVerify && ((altroTrailerWord & 0xFFFC000000ULL)!=0xAAA8000000ULL) )
371 HLTFatal("Data inconsistency in Altro Block at byte position %#x (%d): Expected 0x2AAA in high 14 bits of altro trailer word; Found %#llx (%#llx)",
372 fAltroBlockPositionBytes, fAltroBlockPositionBytes,
373 ((altroTrailerWord & 0xFFFC000000ULL) >> 26), altroTrailerWord);
379 if ( fVerify && ((altroTrailerWord & 0x000000F000ULL)!=0x000000A000ULL) )
381 HLTFatal("Data inconsistency in Altro Block at byte position %#x (%d): Expected 0xA in bits 12-15 of altro trailer word; Found %#llx .",
382 fAltroBlockPositionBytes, fAltroBlockPositionBytes, ((altroTrailerWord & 0x000000F000ULL) >> 12));
387 fAltroBlock10BitWordCnt = (altroTrailerWord >> 16) & 0x3FF;
388 fAltroBlockHWAddress = altroTrailerWord & 0xFFF;
393 HLTFatal("Mapping failed Patch %d HWA %#x (%d) - maxHWA %#x (%d)",
394 fPatch, fAltroBlockHWAddress, fAltroBlockHWAddress, fMaxHWA[fPatch], fMaxHWA[fPatch]);
398 unsigned words40Bit = fAltroBlock10BitWordCnt/4;
399 if ( fAltroBlock10BitWordCnt % 4 )
402 fAltroBlockLengthBytes = words40Bit*5;
403 if ( fAltroBlock10BitWordCnt % 4 )
404 fAltroBlock10BitFillWordCnt = 4-(fAltroBlock10BitWordCnt % 4);
406 fAltroBlock10BitFillWordCnt=0;
409 for ( unsigned b = 0; b < fAltroBlock10BitFillWordCnt; b++ )
411 if ( GetAltroBlockReal10BitWord(b)!=0x2AA )
413 HLTFatal("Data inconsistency in trailing 10 bit fill word of Altro Block at byte position %#x (%d): Expected 0x2AA; Found %#x",
414 fAltroBlockPositionBytes, fAltroBlockPositionBytes, GetAltroBlockReal10BitWord(b));
423 AliHLTUInt32_t AliHLTTPCDigitReaderRaw::GetAltroBlockHWaddr(){
424 return fAltroBlockHWAddress;
426 unsigned AliHLTTPCDigitReaderRaw::GetAltroBlock10BitWordCnt(){
427 return fAltroBlock10BitWordCnt;
429 AliHLTUInt64_t AliHLTTPCDigitReaderRaw::GetAltroBlock40BitWord( unsigned long ndx ){
430 AliHLTUInt64_t val=0;
431 unsigned wordOffset32Bit = (ndx / 4)*5;
432 switch ( ndx % 4 ) // 40 bit word index in a 4*40 bit=5*32 bit group
435 val = (*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+1)*sizeof(AliHLTUInt32_t)));
437 val |= (*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+2)*sizeof(AliHLTUInt32_t))) >> 24;
440 val = ((*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+2)*sizeof(AliHLTUInt32_t))) & 0x00FFFFFF);
442 val |= ((*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+3)*sizeof(AliHLTUInt32_t))) >> 16) & 0xFFFF;
445 val = ((*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+3)*sizeof(AliHLTUInt32_t))) & 0xFFFF);
447 val |= ((*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+4)*sizeof(AliHLTUInt32_t))) >> 8);
450 val = ((*(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+4)*sizeof(AliHLTUInt32_t))) & 0xFF);
452 val |= *(AliHLTUInt32_t*)(fBuffer+fAltroBlockPositionBytes-(wordOffset32Bit+5)*sizeof(AliHLTUInt32_t));
457 AliHLTUInt16_t AliHLTTPCDigitReaderRaw::GetAltroBlock10BitWord( unsigned long ndx ){
458 unsigned long realNdx = ndx+fAltroBlock10BitFillWordCnt;
459 unsigned long word40BitNdx = (realNdx / 4)+1;
460 AliHLTUInt64_t word40Bit = GetAltroBlock40BitWord( word40BitNdx );
461 switch ( realNdx % 4 )
464 return word40Bit & 0x3FF;
466 return (word40Bit>>10) & 0x3FF;
468 return (word40Bit>>20) & 0x3FF;
470 return (word40Bit>>30) & 0x3FF;
476 AliHLTUInt16_t AliHLTTPCDigitReaderRaw::GetAltroBlockReal10BitWord( unsigned long ndx ){
477 unsigned long word40BitNdx = (ndx / 4)+1;
478 AliHLTUInt64_t word40Bit = GetAltroBlock40BitWord( word40BitNdx );
482 return word40Bit & 0x3FF;
484 return (word40Bit>>10) & 0x3FF;
486 return (word40Bit>>20) & 0x3FF;
488 return (word40Bit>>30) & 0x3FF;
494 // Return length of trailing RCU data block in bytes
495 unsigned AliHLTTPCDigitReaderRaw::GetRCUDataBlockLength() const
497 switch ( fDataFormatVersion )
512 unsigned AliHLTTPCDigitReaderRaw::GetCommonDataHeaderSize() const
518 Bool_t AliHLTTPCDigitReaderRaw::ApplyMapping(){
520 if ( (unsigned)fAltroBlockHWAddress > fMaxHWA[fPatch]){
528 fRow = fMapping_0[(unsigned)fAltroBlockHWAddress][0];
529 fPad = fMapping_0[(unsigned)fAltroBlockHWAddress][1];
532 fRow = AliHLTTPCDigitReaderRaw::fMapping_1[(unsigned)fAltroBlockHWAddress][0];
533 fPad = AliHLTTPCDigitReaderRaw::fMapping_1[(unsigned)fAltroBlockHWAddress][1];
535 printf ("pad %d # row %d (hwa: %u / 0x%08X\n", fMapping_1[(unsigned)fAltroBlockHWAddress][0],fMapping_1[(unsigned)fAltroBlockHWAddress][1], (unsigned)fAltroBlockHWAddress, (unsigned)fAltroBlockHWAddress);
536 printf ("pad %d # row %d (hwa: %u / 0x%08X\n", fMapping_1[(unsigned)fAltroBlockHWAddress-1][0],fMapping_1[(unsigned)fAltroBlockHWAddress-1][1], (unsigned)fAltroBlockHWAddress-1, (unsigned)fAltroBlockHWAddress-1);
537 printf ("pad %d # row %d (hwa: %u / 0x%08X\n", fMapping_1[(unsigned)fAltroBlockHWAddress+1][0],fMapping_1[(unsigned)fAltroBlockHWAddress+1][1], (unsigned)fAltroBlockHWAddress+1, (unsigned)fAltroBlockHWAddress+1);
541 fRow = fMapping_2[(unsigned)fAltroBlockHWAddress][0];
542 fPad = fMapping_2[(unsigned)fAltroBlockHWAddress][1];
545 fRow = fMapping_3[(unsigned)fAltroBlockHWAddress][0];
546 fPad = fMapping_3[(unsigned)fAltroBlockHWAddress][1];
549 fRow = fMapping_4[(unsigned)fAltroBlockHWAddress][0];
550 fPad = fMapping_4[(unsigned)fAltroBlockHWAddress][1];
553 fRow = fMapping_5[(unsigned)fAltroBlockHWAddress][0];
554 fPad = fMapping_5[(unsigned)fAltroBlockHWAddress][1];
565 Int_t AliHLTTPCDigitReaderRaw::GetRow( unsigned patch, unsigned hw_addr )
567 if ( (unsigned)hw_addr > fMaxHWA[fPatch]){
573 return fMapping_0[hw_addr][0];
575 return fMapping_1[hw_addr][0];
577 return fMapping_2[hw_addr][0];
579 return fMapping_3[hw_addr][0];
581 return fMapping_4[hw_addr][0];
583 return fMapping_5[hw_addr][0];
588 Int_t AliHLTTPCDigitReaderRaw::GetPad( unsigned patch, unsigned hw_addr )
590 if ( (unsigned)hw_addr > fMaxHWA[fPatch]){
596 return fMapping_0[hw_addr][1];
598 return fMapping_1[hw_addr][1];
600 return fMapping_2[hw_addr][1];
602 return fMapping_3[hw_addr][1];
604 return fMapping_4[hw_addr][1];
606 return fMapping_5[hw_addr][1];
612 unsigned AliHLTTPCDigitReaderRaw::GetMaxHWA( unsigned patch )
616 return fMaxHWA[patch];
620 // ----- MAPPING ARRAYS
621 #include "mapping_array_out.inc"
623 #endif //#if defined(HAVE_TPC_MAPPING)