]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDRawStream.cxx
Update by MinJung: Nug fixes and handling of new type of corruption
[u/mrichter/AliRoot.git] / TRD / AliTRDRawStream.cxx
index 902e4d596e2bb30614d5cf4cbb7e99477d3d64e0..5ea06d4cdb6f917b69966bb6ca29dabd5602c460 100644 (file)
 
 /* $Id$ */
 
-///////////////////////////////////////////////////////////////////////////////
-//                                                                           //
-// This class provides access to TRD digits in raw data.                     //
-//                                                                           //
-// It loops over all TRD digits in the raw data given by the AliRawReader.   //
-// The Next method goes to the next digit. If there are no digits left       //
-// it returns kFALSE.                                                        //
-// Several getters provide information about the current digit.              //
-//                                                                           //
-// Author: C. Lippmann (C.Lippmann@gsi.de)                                   //
-//                                                                           //
-///////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////
+//                                                                        //
+// This class provides access to TRD digits in raw data.                  //
+//                                                                        //
+// It loops over all TRD digits in the raw data given by the AliRawReader //
+// The Next method goes to the next digit. If there are no digits left    //
+// it returns kFALSE.                                                     //
+// Several getters provide information about the current digit.           //
+//                                                                        //
+// Author:                                                                //
+//   Christian Lippmann (C.Lippmann@gsi.de)                               //
+//                                                                        //
+////////////////////////////////////////////////////////////////////////////
 
 #include "AliLog.h"
 #include "AliRawReader.h"
-#include "AliTRDdigitsManager.h"
-#include "AliTRDdataArrayI.h"
 
 #include "AliTRDRawStream.h"
 #include "AliTRDgeometry.h"
-#include "AliTRDCommonParam.h"
 #include "AliTRDcalibDB.h"
+#include "AliTRDdigitsManager.h"
+#include "AliTRDdataArrayI.h"
+#include "AliTRDdataArrayS.h"
+#include "AliTRDSignalIndex.h"
+#include "AliTRDfeeParam.h"
 
 ClassImp(AliTRDRawStream)
 
 //_____________________________________________________________________________
 AliTRDRawStream::AliTRDRawStream() 
-  :TObject()
+  :AliTRDrawStreamBase()
+//  :TObject()
+  ,fGeo(NULL) 
   ,fSig()
   ,fADC(0)
   ,fTB(0)
@@ -57,6 +62,8 @@ AliTRDRawStream::AliTRDRawStream()
   ,fDCS(0)
   ,fROW(0)
   ,fCOL(0)
+  ,fDET(0)
+  ,fLastDET(-1)
   ,fBCctr(0)
   ,fPTctr(0)
   ,fPTphase(0)
@@ -67,41 +74,44 @@ AliTRDRawStream::AliTRDRawStream()
   ,fTCon(0)
   ,fPEDon(0)
   ,fGAINon(0)
-  ,fFiltered(0)
+  ,fXTon(0)
+  ,fNonLinOn(0)
+  ,fBypass(0)
+  ,fCommonAdditive(0)
+  ,fZeroSuppressed(0)
   ,fHCHctr1(0)
   ,fHCHctr2(0)
   ,fMCMHctr1(0)
   ,fMCMHctr2(0)
   ,fGTUctr1(0)
   ,fGTUctr2(0)
-  ,fTracklPID(0)
-  ,fTracklDefL(0)
-  ,fTracklPadPos(0)
+  ,fHCdataCtr(0)
+  ,fTracklPID(0.)
+  ,fTracklDefL(0.)
+  ,fTracklPadPos(0.)
   ,fTracklPadRow(0)
   ,fGTUlinkMask()
   ,fRawReader(NULL)
-  ,fCount(0)
-  ,fDetector(-1)
-  ,fPrevDetector(-1)
-  ,fNPads(-1)
-  ,fRow(-1)
-  ,fPrevRow(-1)
-  ,fColumn(-1)
-  ,fPrevColumn(-1)
-  ,fTime(-1)
-  ,fSignal(-1)
-  ,fRawVersion(1)
-  ,fDataWord(0)
-  ,fStatus(0)
+  ,fRawVersion(2)
+  ,fNextStatus(0)
+  ,fTbSwitch(0)
+  ,fTbSwitchCtr(0)
+  ,fTimeWords(0)
+  ,fWordCtr(0)
   ,fRowMax(0)
   ,fColMax(0)
+  ,fADCmask()
   ,fChamberDone()
-  ,fGeo(NULL)
-  ,fDigitsManager(NULL) 
-  ,fDigits(NULL) 
-  ,fTrack0(NULL) 
-  ,fTrack1(NULL) 
-  ,fTrack2(NULL) 
+  ,fRetVal(0)
+  ,fEqID(0)
+  ,fDataSize(0)
+  ,fSizeOK(kFALSE)
+  ,fCountBytes(0)
+  ,fBufSize(0)
+  ,fBufferSet(kFALSE)
+  ,fPos(NULL)
+  ,fDataWord(NULL)
+  ,fTimeBinsCalib(0)
 {
   //
   // Default constructor
@@ -113,89 +123,11 @@ AliTRDRawStream::AliTRDRawStream()
 
 }
 
-//_____________________________________________________________________________
-AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader
-                               , AliTRDdigitsManager *man
-                              , AliTRDdataArrayI *dig) 
-  :TObject()
-  ,fSig()
-  ,fADC(0)
-  ,fTB(0)
-  ,fEv(0)
-  ,fROB(0)
-  ,fMCM(0)
-  ,fSM(0)
-  ,fLAYER(0)
-  ,fSTACK(0)
-  ,fROC(0)
-  ,fSIDE(0)
-  ,fDCS(0)
-  ,fROW(0)
-  ,fCOL(0)
-  ,fBCctr(0)
-  ,fPTctr(0)
-  ,fPTphase(0)
-  ,fRVmajor(0)
-  ,fRVminor(0)
-  ,fHCHWords(0)
-  ,fTBins(0)
-  ,fTCon(0)
-  ,fPEDon(0)
-  ,fGAINon(0)
-  ,fFiltered(0)
-  ,fHCHctr1(0)
-  ,fHCHctr2(0)
-  ,fMCMHctr1(0)
-  ,fMCMHctr2(0)
-  ,fGTUctr1(0)
-  ,fGTUctr2(0)
-  ,fTracklPID(0)
-  ,fTracklDefL(0)
-  ,fTracklPadPos(0)
-  ,fTracklPadRow(0)
-  ,fGTUlinkMask()
-  ,fRawReader(rawReader)
-  ,fCount(0)
-  ,fDetector(-1)
-  ,fPrevDetector(-1)
-  ,fNPads(-1)
-  ,fRow(-1)
-  ,fPrevRow(-1)
-  ,fColumn(-1)
-  ,fPrevColumn(-1)
-  ,fTime(-1)
-  ,fSignal(-1)
-  ,fRawVersion(1)
-  ,fDataWord(0)
-  ,fStatus(0)
-  ,fRowMax(0)
-  ,fColMax(0)
-  ,fChamberDone()
-  ,fGeo(NULL) 
-  ,fDigitsManager(man) 
-  ,fDigits(dig) 
-  ,fTrack0(NULL) 
-  ,fTrack1(NULL) 
-  ,fTrack2(NULL) 
-
-{
-  //
-  // Create an object to read TRD raw digits
-  //
-
-  fGeo = new AliTRDgeometry();
-
-  fRawReader->Select("TRD");
-
-  for (Int_t i = 0; i < 540; i++) {
-    fChamberDone[i] = 0;
-  }
-
-}
-
 //_____________________________________________________________________________
 AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader) 
-  :TObject()
+  :AliTRDrawStreamBase(rawReader)
+//  :TObject()
+  ,fGeo(NULL) 
   ,fSig()
   ,fADC(0)
   ,fTB(0)
@@ -210,6 +142,8 @@ AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader)
   ,fDCS(0)
   ,fROW(0)
   ,fCOL(0)
+  ,fDET(0)
+  ,fLastDET(-1)
   ,fBCctr(0)
   ,fPTctr(0)
   ,fPTphase(0)
@@ -220,42 +154,44 @@ AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader)
   ,fTCon(0)
   ,fPEDon(0)
   ,fGAINon(0)
-  ,fFiltered(0)
+  ,fXTon(0)
+  ,fNonLinOn(0)
+  ,fBypass(0)
+  ,fCommonAdditive(0)
+  ,fZeroSuppressed(0)
   ,fHCHctr1(0)
   ,fHCHctr2(0)
   ,fMCMHctr1(0)
   ,fMCMHctr2(0)
   ,fGTUctr1(0)
   ,fGTUctr2(0)
-  ,fTracklPID(0)
-  ,fTracklDefL(0)
-  ,fTracklPadPos(0)
+  ,fHCdataCtr(0)
+  ,fTracklPID(0.)
+  ,fTracklDefL(0.)
+  ,fTracklPadPos(0.)
   ,fTracklPadRow(0)
   ,fGTUlinkMask()
   ,fRawReader(rawReader)
-  ,fCount(0)
-  ,fDetector(-1)
-  ,fPrevDetector(-1)
-  ,fNPads(-1)
-  ,fRow(-1)
-  ,fPrevRow(-1)
-  ,fColumn(-1)
-  ,fPrevColumn(-1)
-  ,fTime(-1)
-  ,fSignal(-1)
-  ,fRawVersion(1)
-  ,fDataWord(0)
-  ,fStatus(0)
+  ,fRawVersion(2)
+  ,fNextStatus(0)
+  ,fTbSwitch(0)
+  ,fTbSwitchCtr(0)
+  ,fTimeWords(0)
+  ,fWordCtr(0)
   ,fRowMax(0)
   ,fColMax(0)
+  ,fADCmask()
   ,fChamberDone()
-  ,fGeo(NULL) 
-  ,fDigitsManager(NULL) 
-  ,fDigits(NULL) 
-  ,fTrack0(NULL) 
-  ,fTrack1(NULL) 
-  ,fTrack2(NULL) 
-
+  ,fRetVal(0)
+  ,fEqID(0)
+  ,fDataSize(0)
+  ,fSizeOK(kFALSE)
+  ,fCountBytes(0)
+  ,fBufSize(0)
+  ,fBufferSet(kFALSE)
+  ,fPos(NULL)
+  ,fDataWord(NULL)
+  ,fTimeBinsCalib(0)
 {
   //
   // Create an object to read TRD raw digits
@@ -271,67 +207,73 @@ AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader)
 
 //_____________________________________________________________________________
 AliTRDRawStream::AliTRDRawStream(const AliTRDRawStream& stream)
-  :TObject(stream)
+  :AliTRDrawStreamBase(stream)
+//  :TObject()
+  ,fGeo(NULL)
   ,fSig()
-  ,fADC(0)
-  ,fTB(0)
-  ,fEv(0)
-  ,fROB(0)
-  ,fMCM(0)
-  ,fSM(0)
-  ,fLAYER(0)
-  ,fSTACK(0)
-  ,fROC(0)
-  ,fSIDE(0)
-  ,fDCS(0)
-  ,fROW(0)
-  ,fCOL(0)
-  ,fBCctr(0)
-  ,fPTctr(0)
-  ,fPTphase(0)
-  ,fRVmajor(0)
-  ,fRVminor(0)
-  ,fHCHWords(0)
-  ,fTBins(0)
+  ,fADC(-1)
+  ,fTB(-1)
+  ,fEv(-1)
+  ,fROB(-1)
+  ,fMCM(-1)
+  ,fSM(-1)
+  ,fLAYER(-1)
+  ,fSTACK(-1)
+  ,fROC(-1)
+  ,fSIDE(-1)
+  ,fDCS(-1)
+  ,fROW(-1)
+  ,fCOL(-1)
+  ,fDET(0)
+  ,fLastDET(-1)
+  ,fBCctr(-1)
+  ,fPTctr(-1)
+  ,fPTphase(-1)
+  ,fRVmajor(-1)
+  ,fRVminor(-1)
+  ,fHCHWords(-1)
+  ,fTBins(-1)
   ,fTCon(0)
   ,fPEDon(0)
   ,fGAINon(0)
-  ,fFiltered(0)
-  ,fHCHctr1(0)
-  ,fHCHctr2(0)
-  ,fMCMHctr1(0)
-  ,fMCMHctr2(0)
-  ,fGTUctr1(0)
-  ,fGTUctr2(0)
-  ,fTracklPID(0)
-  ,fTracklDefL(0)
-  ,fTracklPadPos(0)
-  ,fTracklPadRow(0)
+  ,fXTon(0)
+  ,fNonLinOn(-1)
+  ,fBypass(-1)
+  ,fCommonAdditive(-1)
+  ,fZeroSuppressed(0)
+  ,fHCHctr1(-1)
+  ,fHCHctr2(-1)
+  ,fMCMHctr1(-1)
+  ,fMCMHctr2(-1)
+  ,fGTUctr1(-1)
+  ,fGTUctr2(-1)
+  ,fHCdataCtr(-1)
+  ,fTracklPID(-1.)
+  ,fTracklDefL(-1.)
+  ,fTracklPadPos(-1.)
+  ,fTracklPadRow(-1)
   ,fGTUlinkMask()
   ,fRawReader(NULL)
-  ,fCount(0)
-  ,fDetector(-1)
-  ,fPrevDetector(-1)
-  ,fNPads(-1)
-  ,fRow(-1)
-  ,fPrevRow(-1)
-  ,fColumn(-1)
-  ,fPrevColumn(-1)
-  ,fTime(-1)
-  ,fSignal(-1)
-  ,fRawVersion(1)
-  ,fDataWord(0)
-  ,fStatus(0)
-  ,fRowMax(0)
-  ,fColMax(0)
+  ,fRawVersion(-1)
+  ,fNextStatus(0)
+  ,fTbSwitch(0)
+  ,fTbSwitchCtr(0)
+  ,fTimeWords(0)
+  ,fWordCtr(0)
+  ,fRowMax(-1)
+  ,fColMax(-1)
+  ,fADCmask()
   ,fChamberDone()
-  ,fGeo(NULL) 
-  ,fDigitsManager(NULL) 
-  ,fDigits(NULL) 
-  ,fTrack0(NULL) 
-  ,fTrack1(NULL) 
-  ,fTrack2(NULL) 
-
+  ,fRetVal(0)
+  ,fEqID(0)
+  ,fDataSize(0)
+  ,fSizeOK(kFALSE)
+  ,fCountBytes(0)
+  ,fBufSize(0)
+  ,fBufferSet(kFALSE)
+  ,fPos(NULL)
+  ,fDataWord(NULL)
+  ,fTimeBinsCalib(0)
 {
   //
   // Copy constructor
@@ -349,7 +291,7 @@ AliTRDRawStream& AliTRDRawStream::operator = (const AliTRDRawStream&
   // Assigment operator
   //
 
-  Fatal("operator =", "assignment operator not implemented");
+  AliFatal("Assignment operator not implemented");
   return *this;
 
 }
@@ -361,158 +303,36 @@ AliTRDRawStream::~AliTRDRawStream()
   // Destructor
   //
 
+  if (fGeo) {  
+    delete fGeo;
+  }
+
 }
 
 //_____________________________________________________________________________
-Bool_t AliTRDRawStream::SetRawVersion(Int_t rv)
+void AliTRDRawStream::SetRawReader(AliRawReader *rawReader) 
 {
   //
-  // Set the raw data version
+  // Sets the raw reader
   //
 
-  if ( rv >= 0 && rv <= 2 ) {
-    fRawVersion = rv;
-    return kTRUE;
-  }
-
-  return kFALSE;
+  if (rawReader)
+    {
+      fRawReader = rawReader;
+    }
 
 }
 
 //_____________________________________________________________________________
-Bool_t AliTRDRawStream::Next()
+Bool_t AliTRDRawStream::SetRawVersion(Int_t rv)
 {
   //
-  // This is Bogdans code for reading raw data (offline use only).
-  // It is used for fRawVersion == 0. This funcyion read the next raw digit.
-  // Returns kFALSE if there is no digit left
+  // Set the raw data version
   //
 
-  fPrevDetector = fDetector;
-  fPrevRow      = fRow;
-  fPrevColumn   = fColumn;
-  UChar_t data;
-
-  AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
-  if (!calibration) return kFALSE;
-  
-  Int_t timeBins = calibration->GetNumberOfTimeBins();
-  
-  while (fCount >= 0) {
-
-    while (fCount == 0) {  // next detector
-
-      // read the flag
-      if (!fRawReader->ReadNextChar(data)) {
-        return kFALSE;
-      }
-      if (data != 0xBB) {
-       AliError(Form("wrong flag: %x", data));
-       fCount = -1;
-       return kFALSE;
-      }
-
-      // read the detector number
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read detector number");
-       fCount = -1;
-       return kFALSE;
-      }
-      fDetector = data;
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read detector number");
-       fCount = -1;
-       return kFALSE;
-      }
-      fDetector += (UInt_t(data) << 8);
-
-      // read the number of byts
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read number of bytes");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount = data;
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read number of bytes");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount += (UInt_t(data) << 8);
-      if (!fRawReader->ReadNextChar(data)) {
-        AliError("Could not read number of bytes");
-        fCount = -1;
-        return kFALSE;
-      }
-      fCount += (UInt_t(data) << 16);
-
-      // read the number of active pads
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read number of active pads");
-       fCount = -1;
-       return kFALSE;
-      }
-      fNPads = data;
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read number of active pads");
-       fCount = -1;
-       return kFALSE;
-      }
-      fNPads += (UInt_t(data) << 8);
-
-      fTime = timeBins;
-
-    }
-
-    // read the pad row and column number
-    if ((fTime >= timeBins) && (fCount > 2)) {
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read row number");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount--;
-      fRow = data - 1;
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read column number");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount--;
-      fColumn = data - 1;
-      fTime = 0;
-    }
-
-    // read the next data byte
-    if (!fRawReader->ReadNextChar(data)) {
-      AliError("Could not read data");
-      fCount = -1;
-      return kFALSE;
-    }
-    fCount--;
-
-    if (data == 0) {  // zeros
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read time value");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount--;
-      fTime += data + 1;
-
-    } 
-    else {          // signal
-      fSignal = (UInt_t(data & 0x7F) << 8);
-      if (!fRawReader->ReadNextChar(data)) {
-       AliError("Could not read ADC value");
-       fCount = -1;
-       return kFALSE;
-      }
-      fCount--;
-      fSignal += data;
-      fTime++;
-      return kTRUE;
-    }
+  if ( rv >= 0 && rv <= 3 ) {
+    fRawVersion = rv;
+    return kTRUE;
   }
 
   return kFALSE;
@@ -520,464 +340,613 @@ Bool_t AliTRDRawStream::Next()
 }
 
 //____________________________________________________________________________
-Bool_t AliTRDRawStream::ReadAll()
+Bool_t AliTRDRawStream::Init()
 {
-
   //
-  // Read all TRD raw data word (32 bits). This is for all fRawVersion > 0.
-  // Return kFALSE if something is not cool
+  // Initialization
   //
-  // by C. Lippmann
-  //
-  AliTRDCommonParam *commonParam = AliTRDCommonParam::Instance();
-  if (!commonParam) {
-    AliError("Could not get common parameters");
-    return kFALSE;
-  }
 
-  AliTRDcalibDB     *calibration = AliTRDcalibDB::Instance();
-  if (!calibration) {
+  if (!AliTRDcalibDB::Instance()) {
     AliError("Could not get calibration object");
     return kFALSE;
   }
+
+  if (!fGeo) {
+    fGeo = new AliTRDgeometry();
+  }
   
-  UInt_t timeTotal = calibration->GetNumberOfTimeBins();
+  fTimeBinsCalib = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
+  AliDebug(2, Form("Number of Timebins read from CDB: %d", fTimeBinsCalib));
 
   // The number of data words needed for this number of time bins (there
   // are 3 time bins in one word)
-  UInt_t timeWords = ( timeTotal%3 == 0 ) ? timeTotal/3 :  timeTotal/3 + 1;
+  fTimeWords = (fTimeBinsCalib-1)/3 + 1;
 
-  AliDebug(2, Form("Number of Timebins read from CDB: %d", timeTotal));
-
-  UInt_t tBswitch    = 3;
-  UInt_t tBswitchCtr = 0;
-  Int_t  wordCtr     = 0;
-  Int_t  EqID        = 0;
-  Int_t  datasize    = 0;
-  Int_t  iDET        = 0;
+  fTbSwitch    = 3;
+  fTbSwitchCtr = 0;
 
   fHCHctr1 = fHCHctr2 =  0;
   fGTUctr1 = fGTUctr2 = -1;
 
-  AliInfo("Converting TRD raw data to digits ...");
-
-  while ( 1 ) { // loop over all supermodules
+  fHCdataCtr = 0;
+  fWordCtr   = 0;  
 
-    wordCtr   = 0;
-    fHCHctr1  = 0;
-    fMCMHctr1 = 0;
-
-    //
-    // 0) Find first GTU Link Mask and test if we can read data
-    //
-    do {
-
-      if ( !fRawReader->ReadNextInt( fDataWord ) ) {  // This is the standard exit point
-       // Compress also the digits from the last detector
-       if ( fChamberDone[iDET] == 2 ) {
-         //printf("Compressing data for det %d\n", iDET);
-         fDigits->Compress(1,0);
-         fTrack0->Compress(1,0);
-         fTrack1->Compress(1,0);
-         fTrack2->Compress(1,0);
-       }
-       AliInfo(Form("Finished processing TRD raw data: Found %d Half-Chambers", fHCHctr2));
-       //
-       /*
-       fDigits = fDigitsManager->GetDigits(iDET+1);
-       fTrack0 = fDigitsManager->GetDictionary(iDET+1,0);
-       fTrack1 = fDigitsManager->GetDictionary(iDET+1,1);
-       fTrack2 = fDigitsManager->GetDictionary(iDET+1,2);
-       fDigits->Allocate(fRowMax,fColMax,timeTotal);
-       fTrack0->Allocate(fRowMax,fColMax,timeTotal);
-       fTrack1->Allocate(fRowMax,fColMax,timeTotal);
-       fTrack2->Allocate(fRowMax,fColMax,timeTotal);
-       fDigits->SetDataUnchecked(0, 0, 0, 50);
-       fTrack0->SetDataUnchecked(0, 0, 0, 0);
-       fTrack1->SetDataUnchecked(0, 0, 0, 0);
-       fTrack2->SetDataUnchecked(0, 0, 0, 0);  
-       fDigits->Compress(1,0);
-       fTrack0->Compress(1,0);
-       fTrack1->Compress(1,0);
-       fTrack2->Compress(1,0);
-       */
-       //
-       return kTRUE;
-      }
-      wordCtr++;
-
-      // After reading the first word check for size of this data and get Eq. ID
-      if ( wordCtr == 1 ) {
-       datasize = fRawReader->GetDataSize()/4;  // Size of this payload is in 32bit words
-       EqID     = fRawReader->GetEquipmentId(); // Get Equipment ID
-      }
-
-      // GTU Link Mask?
-      if ( (fDataWord & 0xfffff000) ==  0xe0000000 ) {
-       fStatus = 1;      // GTU link mask found
-       DecodeGTUlinkMask();
-       break;
-      } 
-      else {
-       AliError(Form("Equipment %d: First data word is not GTU Link Mask!", EqID));
-       return kFALSE;
-      }
-
-    } 
-    while ( wordCtr < datasize );
-
-    //
-    // loop over all half chambers in one supermodule
-    //
-    while ( wordCtr < datasize ) {
-
-      //
-      // 1) Find end_of_tracklet_marker
-      //
-      while ( wordCtr < datasize ) {
-
-       if ( !fRawReader->ReadNextInt( fDataWord ) ) {
-         AliError("Could not read data");
-         return kFALSE;
-       }
-       wordCtr++;
-
-       // GTU Link Mask?
-       if ( (fDataWord & 0xfffff000) ==  0xe0000000 ) {
-         DecodeGTUlinkMask();
-         continue;
-       }
-
-       // endoftrackletmarker?
-       if ( fDataWord == endoftrackletmarker ) {
-         AliDebug(3, "end-of-tracklet-marker found");
-         fStatus = 1;
-         break;
-       } 
-        else {
-         // Tracklets found
-         AliDebug(3, "Tracklet found");
-         DecodeTracklet();
-       }
+  fDET     = 0;
+  fLastDET     = -1;
+  fRetVal = 0;
+  fEqID     = 0;
+  fDataSize = 0;
+  fSizeOK = kFALSE;
+  
+  fNextStatus = kStart;
 
-      }
+  fCountBytes = 0;
+  fBufSize = 0;
+  fDataWord = NULL;
+  fPos = NULL;
+  fWordCtr = 0;
+  fBufferSet = kFALSE;
 
-      if ( fStatus == 0 ) break;
-    
-      //
-      // 2) Look for non-end_of_tracklet_marker
-      //
-      fStatus = 0;
-      while ( wordCtr < datasize ) { 
-
-       if ( !fRawReader->ReadNextInt( fDataWord ) ) {
-         AliError("Could not read data");
-         return kFALSE;
-       }
-       wordCtr++;
-       //printf("Word %d: 0x%08x\n", wordCtr, fDataWord); 
+  return kTRUE;
 
-       if ( fDataWord != endoftrackletmarker ) {
-         fStatus = 1;
-         break;
-       }
+}
 
-      }
+//____________________________________________________________________________
+Int_t AliTRDRawStream::NextData()
+{
+  //
+  // Updates the next data word pointer
+  //
 
-      if ( fStatus == 0 ) break;
-    
-      //
-      // 3) This Word must be Half Chamber Header
-      //
-      fStatus = 0;
-      if ( (fDataWord & 0x00000003) == 1 ) { // HC header
-
-       // If both half chambers of chamber corresponding to previous header
-       // were already processed, we can compress these digits
-       iDET = fGeo->GetDetector(fLAYER, fSTACK, fSM); // !!this is still the previous HC!!!
-       if ( fChamberDone[iDET] == 2 ) {
-         //printf("Compressing data for det %d\n", iDET);
-         fDigits->Compress(1,0);
-         fTrack0->Compress(1,0);
-         fTrack1->Compress(1,0);
-         fTrack2->Compress(1,0);
+  if (fCountBytes + kSizeWord >= fBufSize)
+    {
+      fBufferSet = fRawReader->ReadNextData(fPos);
+      if (fBufferSet == kTRUE)
+       {
+         fBufSize = fRawReader->GetDataSize();
+         fCountBytes = 0;        
+         fDataWord = (UInt_t*)fPos;
+         fNextStatus = kNextSM;
+         fWordCtr = 0;
+         return kNextSM;
        }
-
-       // Read from new HC header the chamber position (fLAYER, fSTACK, fSM)
-       DecodeHCheader(timeTotal); // This is the new header!
-       wordCtr += fHCHWords;
-       iDET    = fGeo->GetDetector(fLAYER, fSTACK, fSM);
-       fRowMax = commonParam->GetRowMax(fLAYER,fSTACK,fSM);
-       fColMax = commonParam->GetColMax(fROC);
-
-       // The container for the digits of this detector
-       fDigits = fDigitsManager->GetDigits(iDET);
-       fTrack0 = fDigitsManager->GetDictionary(iDET,0);
-       fTrack1 = fDigitsManager->GetDictionary(iDET,1);
-       fTrack2 = fDigitsManager->GetDictionary(iDET,2);
-       
-       // Allocate memory if it was not already done
-       if (fDigits->GetNtime() == 0) {
-         //printf("Allocating digits memory for det %d\n", iDET);
-         fDigits->Allocate(fRowMax,fColMax,timeTotal);
-         fTrack0->Allocate(fRowMax,fColMax,timeTotal);
-         fTrack1->Allocate(fRowMax,fColMax,timeTotal);
-         fTrack2->Allocate(fRowMax,fColMax,timeTotal);
+      else
+       {
+         fNextStatus = kStop;
+         return kNoMoreData;
        }
+    }
+  else
+    {
+
+      fPos += kSizeWord;
+      fCountBytes += kSizeWord;          
+      fDataWord = (UInt_t*)fPos;
+      fWordCtr++;
+      return kWordOK;
+    }
+}
 
-       fMCMHctr2 = 0;
+//____________________________________________________________________________
+Bool_t AliTRDRawStream::Next()
+{
+  //
+  // Updates the next data word pointer
+  //
 
-       fChamberDone[iDET] = kTRUE;
+  if (fNextStatus == kStart)
+    {
+      Init();
+    }
 
-      }
-    
-      //
-      // 4) Scan MCM data
-      //
-      fStatus = 0;
-      while ( wordCtr < datasize ) {
-
-       if ( !fRawReader->ReadNextInt( fDataWord ) ) {
-         AliError("Could not read data");
-         return kFALSE;
-       }
-       wordCtr++;
-       //printf("Word %d: 0x%08x\n", wordCtr, fDataWord); 
-      
-       if( (fDataWord & 0x0000000f) == 0xC ) { // MCM Header
-         DecodeMCMheader();
-         if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 ) {
-           AliError("Wrong fMCM or fROB. Skip this data");
-           break;
-         }
-         tBswitch    = 3;  // For first adc channel we expect: (fDataWord & 3) = 3
-         tBswitchCtr = 0;  // 
-         fADC = fTB  = 0;  // Reset Counter
-         fStatus     = 1;  // Now 1 means MCM header is found
+  while (fNextStatus != kStop)
+    { // !kStop
+      NextData();
+      if (fNextStatus == kNextMCM || fNextStatus == kNextData)
+       {
+         fHCdataCtr += 4;
+         
+         if( ((*fDataWord & 0x80000000) == 0x0) && ((*fDataWord & 0x0000000f) == 0xC) )
+           { // MCM Header
+             DecodeMCMheader();
+             if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 ) 
+               {
+                 AliWarning("Wrong fMCM or fROB. Skip this data");
+                 fRawReader->AddMajorErrorLog(kWrongMCMorROB,Form("MCM=%d, ROB=%d",fMCM,fROB));
+                 fNextStatus = kNextHC;
+                 continue;
+               }
+             fTbSwitch    = 3;  // For first adc channel we expect: (*fDataWord & 3) = 3
+             fTbSwitchCtr = 0;  // 
+             fADC = fTB   = 0;  // Reset Counter
+             fNextStatus = kNextData;
+             continue;
+           }
+         
+         if ( *fDataWord == kEndofrawdatamarker ) 
+           {  // End of half-chamber data, finished
+             fGTUctr1 = -1;
+             fNextStatus = kNextHC;
+             continue;
+           }
+         
+         if (fNextStatus == kNextData )
+           {       // MCM header is set, ADC data is valid.
+             
+             // Found some data. Decode it now:
+             fRetVal = DecodeDataWord();
+             if ( fRetVal ==  0 ) continue;
+             if ( fRetVal == -1 ) 
+               {
+                 fNextStatus = kNextHC;
+                 continue;
+               }
+             if ( fRetVal == 1)
+               {
+                 {  // A real pad
+                   fTB += 3;           
+                   return kTRUE;
+                 }                     
+               }
+             // following ifs have been moved to DEcodeDatawordV1V2
+             //            if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) 
+             //              {       
+             //                // Write Digits
+             //                if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) 
+             //                  {  // A real pad
+             //                    fTB += 3;           
+             //                    return kTRUE;
+             //                  }            
+             //              }
+             //            else 
+             //              {
+             //                fCOL = -1;             
+             //              }
+           }// kNextData  
+         
          continue;
-       }
-    
-       // End of half-chamber data, finished:
-       if ( fDataWord == endofeventmarker ) {
-         fGTUctr1 = -1;
-         break;
-       }
+       } //next mcm
 
-       if ( fStatus == 1 ) {       // MCM header is set, ADC data is valid.
-    
+      if ( fNextStatus == kNextHC )
+       {
          //
-         // Found some data. Decode it now:
+         // 1) Find end_of_tracklet_marker
          //
-         if ( (fDataWord & 0x00000003) != 0x2 && (fDataWord & 0x00000003) != 0x3) {
-           AliError(Form("Data %08x : Data Word ends neither with 11 nor 10", (Int_t)fDataWord));
-           break;
-         }
-
-         if ( (fDataWord & 0x00000003) != tBswitch ) {    // Next ADC channel found
-           //if ( fTB+1 != timeBins ) AliError(Form("Time bins in data (%d) != DB (%d)", fTB+1, timeBins));
-           tBswitch = (tBswitch & 2) | !(tBswitch & 1);   // 0x3 <--> 0x2
-           tBswitchCtr = 0;
-           fADC++;
-           fTB=0;
-         }
-
-         tBswitchCtr++; // Just read one word
-       
-         // We have only timeTotal time bins
-         if ( tBswitchCtr > timeWords ) {
-           AliError(Form("Data is strange. Already found %d words for this ADC channel", (Int_t)tBswitchCtr));
-           continue;
-         }
-
-         // We have only 21 ADC channels.
-         if ( fADC > (Int_t)fGeo->ADCmax()-1 ) {
-           AliError(Form("Data %08x : Data is strange. fADC is already %d", (Int_t)fDataWord, (Int_t)fADC));
-           continue;
-         }
-
-         // There are 18 pads connected to each MCM ADC channels 2...19. The other channels cross to other
-         // MCMs and are good for online tracking in the MCM.
-         if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) {
-
-           // Get Pad column
-           fCOL = fGeo->GetPadCol(fROB, fMCM, fADC);
-
-           // We have only 144 Pad Columns
-           if ( fCOL > fColMax-1 || fCOL < 0 ) {
-             AliError(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
-                           fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
+         // GTU Link Mask?
+         if ( (*fDataWord & 0xfffff000) ==  0xe0000000 ) 
+           {
+             DecodeGTUlinkMask();
+             continue;
            }
-
-           // Decode 32 bit data words with information from 3 time bins and copy the data
-           fSig[0] = (fDataWord & 0x00000ffc) >> 2;
-           fSig[1] = (fDataWord & 0x003ff000) >> 12;
-           fSig[2] = (fDataWord & 0xffc00000) >> 22;
-
-           // Print data to screen:
-           AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
-                            fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));
-
-           // Write Digits
-           if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) {  // A real pad
-             for ( Int_t ctr = 0; ctr < 3; ctr++ ) {
-               if ( fTB+ctr < (Int_t)timeTotal ) {
-                 /*
-                 fDigits->SetDataUnchecked(fROW, fCOL, fTB+ctr, fSig[ctr]);
-                 fTrack0->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
-                 fTrack1->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
-                 fTrack2->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
-                 */
-                 fDigits->SetData(fROW, fCOL, fTB+ctr, fSig[ctr]);
-                 fTrack0->SetData(fROW, fCOL, fTB+ctr, 0);
-                 fTrack1->SetData(fROW, fCOL, fTB+ctr, 0);
-                 fTrack2->SetData(fROW, fCOL, fTB+ctr, 0);
-               }
-             }
+         
+         // endoftrackletmarker?
+         if ( *fDataWord == kEndoftrackletmarker ) 
+           {
+             AliDebug(3, "end-of-tracklet-marker found");
+             fNextStatus = kSeekNonEoTracklet;
+             continue;
+           } 
+         else 
+           {
+             // Tracklets found
+             AliDebug(3, "Tracklet found");
+             DecodeTracklet();
+             continue;
            }
+       } //if next HC
 
-           fTB += 3;
-
-         } 
-          else {
-
-            fCOL = -1;
-
-         }
+      if (fNextStatus == kSeekNonEoTracklet)
+       {
+         //
+         // 2) Look for non-end_of_tracklet_marker
+         //
+         //printf("Word %d: 0x%08x\n", fWordCtr, *fDataWord); 
 
+         if ( *fDataWord != kEndoftrackletmarker ) 
+           {
+             fNextStatus = kDecodeHC;
+             AliDebug(3, "NON end-of-tracklet-marker found");
+             //// no do not continue - this should be the hcheader
+           }
+         else
+           {
+             //just go on and find the non-end_of_tracklet_marker
+             continue;
+           }
        }
-       
-      }
-
-      AliDebug(2, Form("SM%d L%dS%d side %x: Processed %d MCMs.", fSM, fLAYER, fSTACK, fSIDE+10, fMCMHctr2));
-
-    } // End Half-Chamber loop
 
-    AliDebug(1, Form("SM%d (Eq %d): Processed %d HC (%d MCMs)", fSM, EqID, fHCHctr1, fMCMHctr1));
-
-  } // End Super Module loop
-
-  // Compress also the digits from the last detector
-  if ( fChamberDone[iDET] == 2 ) {
-    //printf("Compressing data for det %d\n", iDET);
-    fDigits->Compress(1,0);
-    fTrack0->Compress(1,0);
-    fTrack1->Compress(1,0);
-    fTrack2->Compress(1,0);
-  }
-
-  return kTRUE;
+      if ( fNextStatus == kDecodeHC )
+       {
+         AliDebug(3, "Decode HC");
 
+         //
+         // 3) This Word must be Half Chamber Header
+         //
+         if ( (*fDataWord & 0x00000003) == 1 ) 
+           { // HC header
+             DecodeHCheader(fTimeBinsCalib); // This is the new header!
+             fLastDET = fDET;
+             fDET    = fGeo->GetDetector(fLAYER, fSTACK, fSM);
+             fRowMax = fGeo->GetRowMax(fLAYER,fSTACK,fSM);
+             fColMax = fGeo->GetColMax(fROC);
+             
+             fMCMHctr2 = 0;
+             fHCdataCtr = 0;
+             
+             fChamberDone[fDET]++;
+             fNextStatus = kNextMCM;
+             AliDebug(3, "Decode HC OK");            
+             continue;
+           } //HC header
+         else
+           {
+             AliDebug(3, "Decode HC NOT OK");        
+             fNextStatus = kNextSM;
+             continue;
+           }
+       } // if decode HC
+
+      if (fNextStatus == kNextSM)
+       {
+         
+         fDET     = 0;
+         fRetVal = 0;
+         fEqID     = 0;
+         fDataSize = 0;
+         fSizeOK = kFALSE;
+         
+         // After reading the first word check for size of this data and get Eq. ID
+         if ( fWordCtr == 1 ) 
+           {
+             fDataSize = fRawReader->GetDataSize()/4;  // Size of this payload in 32bit words
+             fEqID     = fRawReader->GetEquipmentId(); // Get Equipment ID
+             if ( fDataSize > 0 ) fSizeOK = kTRUE;
+           }
+         
+         // GTU Link Mask?
+         if ( (*fDataWord & 0xfffff000) ==  0xe0000000 ) 
+           {
+             DecodeGTUlinkMask();
+             fNextStatus = kNextHC;
+             continue;
+           } 
+         else 
+           {
+             AliWarning(Form("Equipment %d: First data word is not GTU Link Mask!", fEqID));
+              fRawReader->AddMajorErrorLog(kGTULinkMaskMissing,Form("Equipment %d",fEqID));
+             fNextStatus = kStop;
+           }       
+       }// if nextSM
+
+    } // not kStop
+
+  AliDebug(1, Form("That's all folks! %d", fSM));
+  return kFALSE;
 }
 
-//============================================================================
-// Decoding functions
-//============================================================================
-
 //____________________________________________________________________________
-void AliTRDRawStream::DecodeHCheader(Int_t timeBins)
+Int_t AliTRDRawStream::NextChamber(AliTRDdigitsManager *man)
 {
   //
-  // Decode a half chamber header
+  // Updates the next data word pointer
   //
 
-  if ( (fDataWord >> 31) != 1 )  {
-
-    if ( fRawVersion == 1 ) {
-      DecodeHCheaderV1();
+  AliTRDdataArrayS *digits = 0;
+  AliTRDdataArrayI *track0 = 0;
+  AliTRDdataArrayI *track1 = 0;
+  AliTRDdataArrayI *track2 = 0; 
+  AliTRDSignalIndex *indexes = 0;
+         
+  if (fNextStatus == kStart)
+    {
+      Init();
     }
-    else {
-      AliError(Form("Mismatch between fRawVersion (%d) and HC header signature", fRawVersion));
-    }
-    return;
 
-  } 
-  else {
+  while (fNextStatus != kStop)
+    { // !kStop
+      NextData();
+      if (fNextStatus == kNextMCM || fNextStatus == kNextData)
+      //while (fNextStatus == kNextMCM || fNextStatus == kNextData)
+       {
+         fHCdataCtr += 4;
+         
+         if( ((*fDataWord & 0x80000000) == 0x0) && ((*fDataWord & 0x0000000f) == 0xC) )
+           { // MCM Header
+             DecodeMCMheader();
+             if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 ) 
+               {
+                 AliWarning("Wrong fMCM or fROB. Skip this data");
+                 fRawReader->AddMajorErrorLog(kWrongMCMorROB,Form("MCM=%d, ROB=%d",fMCM,fROB));
+                 fNextStatus = kNextHC;
+                 continue;
+               }
+             fTbSwitch    = 3;  // For first adc channel we expect: (*fDataWord & 3) = 3
+             fTbSwitchCtr = 0;  // 
+             fADC = fTB   = 0;  // Reset Counter
+             fNextStatus  = kNextData;
 
-    fRVmajor = (fDataWord >> 24) & 0x7f;
-    fRVminor = (fDataWord >> 17) & 0x7f;
-    if ( fRawVersion != fRVmajor ) {
-      AliError(Form("Mismatch between fRawVersion (%d) and fRVmajor from HC header(%d)"
-                   ,fRawVersion,fRVmajor));
-    }
-    if (fRawVersion == 2 ) {
-      DecodeHCheaderV2(timeBins);
-    }
-    return;
+//           NextData(); // if while loop!
+             continue; // if if
+           }
+         
+         if ( *fDataWord == kEndofrawdatamarker ) 
+           {  // End of half-chamber data, finished
+             fGTUctr1 = -1;
+             fNextStatus = kNextHC;
+             // full chamber processed ?
+             if (fChamberDone[fDET] == 2)
+               {
+                 return fDET;
+               }
+             else
+               {
+//               break; // if while loop
+                 continue; // if if
+               }
+           }
+         
+         if (fNextStatus == kNextData )
+           {       // MCM header is set, ADC data is valid.
+             
+             // Found some data. Decode it now:
+             fRetVal = DecodeDataWord();
+             if ( fRetVal ==  0 ) continue;
+             if ( fRetVal == -1 ) 
+               {
+                 fNextStatus = kNextHC;
+
+//               NextData(); // if while loop!
+//               break; //if while loop!
+                 continue;// if if
+               }
+             
+           if ( fRetVal == 1)
+             {
+               {  // A real pad
+                 // here fill the data arrays
+                 //return kTRUE;
+                 for (Int_t it = 0; it < 3; it++)
+                   {
+                     if ( GetTimeBin() + it < GetNumberOfTimeBins() )
+                       {
+                         if (GetSignals()[it] > 0)
+                           {
+                             digits->SetDataUnchecked(fROW, fCOL, fTB + it, fSig[it]);
+                             indexes->AddIndexTBin(fROW, fCOL, fTB + it);
+                             if (man->UsesDictionaries())
+                               {
+                                 track0->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
+                                 track1->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
+                                 track2->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
+                               }
+                           }
+                       } // check the tbins range
+                   } // for each tbin of current 3
+                 fTB += 3;             
+               }// real pad                    
+             } // if fRetVal == 1
+           
+           // following ifs have been moved to DEcodeDatawordV1V2
+//         if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) 
+//           {       
+//             // Write Digits
+//             if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) 
+//               {  // A real pad
+//                 fTB += 3;           
+//                 return kTRUE;
+//               }            
+//           }
+//         else 
+//           {
+//             fCOL = -1;             
+//           }
+           }// kNextData  
+         
+//       NextData(); // if while loop!
+         continue; //if if
+       } //next mcm
+
+      if ( fNextStatus == kNextHC )
+       {
+         //
+         // 1) Find end_of_tracklet_marker
+         //
+         // GTU Link Mask?
+         if ( (*fDataWord & 0xfffff000) ==  0xe0000000 ) 
+           {
+             DecodeGTUlinkMask();
+             continue;
+           }
+         
+         // endoftrackletmarker?
+         if ( *fDataWord == kEndoftrackletmarker ) 
+           {
+             AliDebug(3, "end-of-tracklet-marker found");
+             fNextStatus = kSeekNonEoTracklet;
+             continue;
+           } 
+         else 
+           {
+             // Tracklets found
+             AliDebug(3, "Tracklet found");
+             DecodeTracklet();
+             continue;
+           }
+       } //if next HC
 
-  }
+      if (fNextStatus == kSeekNonEoTracklet)
+       {
+         //
+         // 2) Look for non-end_of_tracklet_marker
+         //
+         //printf("Word %d: 0x%08x\n", fWordCtr, *fDataWord); 
 
-  AliError(Form(" Unsupported raw version: %d", fRawVersion));
-  return;
+         if ( *fDataWord != kEndoftrackletmarker ) 
+           {
+             fNextStatus = kDecodeHC;
+             AliDebug(3, "NON end-of-tracklet-marker found");
+             //// no do not continue - this should be the hcheader
+           }
+         else
+           {
+             //just go on and find the non-end_of_tracklet_marker
+             continue;
+           }
+       }
 
+      if ( fNextStatus == kDecodeHC )
+       {
+         AliDebug(3, "Decode HC");
+
+         //
+         // 3) This Word must be Half Chamber Header
+         //
+         if ( (*fDataWord & 0x00000003) == 1 ) 
+           { // HC header
+             DecodeHCheader(fTimeBinsCalib); // This is the new header!
+             fDET    = fGeo->GetDetector(fLAYER, fSTACK, fSM);
+             fRowMax = fGeo->GetRowMax(fLAYER,fSTACK,fSM);
+             fColMax = fGeo->GetColMax(fROC);
+
+             if (fLastDET != fDET)
+               {
+                 AliDebug(4, "New DET!");            
+                 // allocate stuff for the new det
+                 //man->ResetArrays();
+                 digits = (AliTRDdataArrayS *) man->GetDigits(fDET);
+                 track0 = (AliTRDdataArrayI *) man->GetDictionary(fDET,0);
+                 track1 = (AliTRDdataArrayI *) man->GetDictionary(fDET,1);
+                 track2 = (AliTRDdataArrayI *) man->GetDictionary(fDET,2);
+                 
+                 // Allocate memory space for the digits buffer
+                 if (digits->GetNtime() == 0) 
+                   {
+                     AliDebug(4, "Allocating digits");       
+                     //AliDebug(5, Form("Alloc digits for det %d", det));
+                     digits->Allocate(fRowMax, fColMax, fTBins);
+                     if (man->UsesDictionaries())
+                       {
+                         track0->Allocate(fRowMax, fColMax, fTBins);
+                         track1->Allocate(fRowMax, fColMax, fTBins);
+                         track2->Allocate(fRowMax, fColMax, fTBins);
+                       }
+                   }
+                 
+                 indexes = man->GetIndexes(fDET);
+                 indexes->SetSM(fSM);
+                 indexes->SetStack(fSTACK);
+                 indexes->SetLayer(fLAYER);
+                 indexes->SetDetNumber(fDET);
+                 
+                 if (indexes->IsAllocated() == kFALSE)
+                   {
+                     AliDebug(4, "Allocating indexes");              
+                     indexes->Allocate(fRowMax, fColMax, fTBins);
+                   }
+                 fLastDET = fDET;
+               }
+             
+             fMCMHctr2 = 0;
+             fHCdataCtr = 0;
+             
+             fChamberDone[fDET]++;
+             fNextStatus = kNextMCM;
+             AliDebug(3, "Decode HC OK");            
+             continue;
+           } //HC header
+         else
+           {
+             AliDebug(3, "Decode HC NOT OK");        
+             fNextStatus = kNextSM;
+             continue;
+           }
+       } // if decode HC
+
+      if (fNextStatus == kNextSM)
+       {
+         
+         fDET     = 0;
+         fRetVal = 0;
+         fEqID     = 0;
+         fDataSize = 0;
+         fSizeOK = kFALSE;
+         
+         // After reading the first word check for size of this data and get Eq. ID
+         if ( fWordCtr == 1 ) 
+           {
+             fDataSize = fRawReader->GetDataSize()/4;  // Size of this payload in 32bit words
+             fEqID     = fRawReader->GetEquipmentId(); // Get Equipment ID
+             if ( fDataSize > 0 ) fSizeOK = kTRUE;
+           }
+         
+         // GTU Link Mask?
+         if ( (*fDataWord & 0xfffff000) ==  0xe0000000 ) 
+           {
+             DecodeGTUlinkMask();
+             fNextStatus = kNextHC;
+             continue;
+           } 
+         else 
+           {
+             AliWarning(Form("Equipment %d: First data word is not GTU Link Mask!", fEqID));
+              fRawReader->AddMajorErrorLog(kGTULinkMaskMissing,Form("Equipment %d",fEqID));
+             fNextStatus = kStop;
+           }       
+       }// if nextSM
+
+    } // not kStop
+
+  AliDebug(1, Form("That's all folks! %d", fSM));
+  //return kFALSE;
+  return -1;
 }
 
+//============================================================================
+// Decoding functions
+//============================================================================
+
+
 //____________________________________________________________________________
-void AliTRDRawStream::DecodeHCheaderV1()
+void AliTRDRawStream::DecodeHCheader(Int_t timeBins)
 {
-
   //
-  // Decode the HC header (fRawVersion == 1, SM I Commissioning 06)
+  // Decode the HC header (fRawVersion == 2, 3, 4, ???)
   //
 
-  if ( (fDataWord & 0x3) == 1 ) {
-
-    fDCS   = (fDataWord >> 20);
-    fSM    = (fDataWord >> 15) & 0x1f;
-    fLAYER = (fDataWord >> 12) & 0x7;
-    fSTACK = (fDataWord >>  9) & 0x7;
-    fSIDE  = (fDataWord >>  8) & 0x1;
-
-    fROC   = fGeo->GetDetectorSec(fLAYER, fSTACK);
-
-    //AliDebug(3, Form("0x%08x: HC header: dcs=%d; sm=%d; roc=%d; side=%x", fDataWord, fDCS, fSM, fROC, fSIDE+10));
-    if ((fSM    <  0) || 
-        (fSM    > 17) || 
-        (fLAYER <  0) || 
-        (fLAYER >  5) || 
-        (fSTACK <  0) || 
-        (fSTACK >  4) || 
-        (fSIDE  <  0) || 
-        (fSIDE  >  1)) {
-      AliError(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
-                   fDataWord, fDCS, fSM, fLAYER, fSTACK));
-    } 
-    else {
-      fStatus = 1;
-      fHCHctr1++;
-      fHCHctr2++;
-    }
-    fHCHWords = 0;
-
-  } 
-  else { 
+  fRVmajor = (*fDataWord >> 24) & 0x7f;
+  fRVminor = (*fDataWord >> 17) & 0x7f;
 
-    AliError(Form("0x%08x: No HC header when it was expected.", fDataWord)); 
+  if (fRVmajor < 2 || fRVmajor > 4)
+    AliError(Form(" Unsupported raw version: %d", fRawVersion))
+  
+  if ( fRawVersion != fRVmajor ) {
+    
+    AliWarning("===============================================================================");
+    AliWarning(Form("Mismatch between fRawVersion (%d) and fRVmajor from HC header (%d)"
+                   ,fRawVersion,fRVmajor));
+    AliWarning(Form("Setting fRawVersion to %d", fRVmajor));
+    AliWarning("===============================================================================");
+    fRawVersion = fRVmajor;
 
   }
 
-}
-
-
-//____________________________________________________________________________
-void AliTRDRawStream::DecodeHCheaderV2(Int_t timeBins)
-{
   //
-  // Decode the HC header (fRawVersion == 2, Full raw production)
-  //
-
-  // 1st word
-  if ( (fDataWord & 0x3) == 1 ) {
+  // check for zero suppression
+  if ( fRawVersion >= 3 || fRawVersion <= 4 ) fZeroSuppressed = kTRUE;
+  else                                        fZeroSuppressed = kFALSE;
+  
+  // 1st word (h[0])
+  if ( (*fDataWord & 0x3) == 1 ) {
 
-    fHCHWords = (fDataWord >> 14) & 0x7;
-    fSM       = (fDataWord >>  9) & 0x1f;
-    fLAYER    = (fDataWord >>  6) & 0x7;
-    fSTACK    = (fDataWord >>  3) & 0x7;
-    fSIDE     = (fDataWord >>  2) & 0x1;
+    fHCHWords = (*fDataWord >> 14) & 0x7;
+    fSM       = (*fDataWord >>  9) & 0x1f;
+    fLAYER    = (*fDataWord >>  6) & 0x7;
+    fSTACK    = (*fDataWord >>  3) & 0x7;
+    fSIDE     = (*fDataWord >>  2) & 0x1;
 
     fROC      = fGeo->GetDetectorSec(fLAYER, fSTACK);
 
-    AliDebug(3, Form("0x%08x: HC header: sm=%d; roc=%d; side=%x", fDataWord, fSM, fROC, fSIDE+10));
+    AliDebug(3, Form("0x%08x: HC header: sm=%d; roc=%d; side=%x", *fDataWord, fSM, fROC, fSIDE+10));
 
     if ((fSM    <  0) || 
         (fSM    > 17) || 
@@ -987,140 +956,162 @@ void AliTRDRawStream::DecodeHCheaderV2(Int_t timeBins)
         (fSTACK >  4) || 
         (fSIDE  <  0) || 
         (fSIDE  >  1)) {
-      AliError(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
-                   fDataWord, fDCS, fSM, fLAYER, fSTACK));
+      AliWarning(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
+                    *fDataWord, fDCS, fSM, fLAYER, fSTACK));
+      fRawReader->AddMajorErrorLog(kHCHeaderCorrupt,Form("0x%08x:dcs=%d; sm=%d; layer=%d; stack=%d.",
+                                                        *fDataWord, fDCS, fSM, fLAYER, fSTACK));
     } 
     else {
-      fStatus = 1;
       fHCHctr1++;
       fHCHctr2++;
     }
   } 
   else { 
-    AliError(Form("0x%08x: No HC header when it was expected.", fDataWord)); 
+    AliWarning(Form("0x%08x: No HC header when it was expected.", *fDataWord)); 
+    fRawReader->AddMajorErrorLog(kHCHeaderMissing,Form("0x%08x", *fDataWord));
   }
 
-  // 2nd word
+  // 2nd word (h[1])
   if ( fHCHWords >= 1 ) {
     // read one more word
-    if ( !fRawReader->ReadNextInt( fDataWord ) ) {
-      AliError("Could not read data");
-      return;
-    }
-    if ( (fDataWord & 0x3) == 1 ) {
+    if (NextData() != kWordOK)
+      {
+       AliWarning("Next HC word missing");
+        fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC word missing"); 
+       fNextStatus = kNextHC;
+       return;
+      }
+    if ( (*fDataWord & 0x3) == 1 ) {
       
-      fBCctr   =  (fDataWord >> 16);
-      fPTctr   =  (fDataWord >> 12) & 0xf;
-      fPTphase =  (fDataWord >>  8) & 0xf;
-      fTBins   = ((fDataWord >>  2) & 0x3f) + 1;
+      fBCctr   =  (*fDataWord >> 16);
+      fPTctr   =  (*fDataWord >> 12) & 0xf;
+      fPTphase =  (*fDataWord >>  8) & 0xf;
+      fTBins   = ((*fDataWord >>  2) & 0x3f) + 1;
 
       AliDebug(3, Form("0x%08x: HC header 2: BCctr=%d PTctr=%d PTph=%d TB=%d"
-                      , fDataWord, fBCctr, fPTctr, fPTphase, fTBins));
+                      , *fDataWord, fBCctr, fPTctr, fPTphase, fTBins));
 
       if( fTBins != timeBins ) {
-       AliError(Form("Mismatch between Number of Time Bins from CDB (%d) and from HC header (%d)"
-                     , timeBins, fTBins));
+
+       AliWarning("===============================================================================");
+       AliError(Form("Mismatch between nNTB from CDB (%d) and from HC header (%d)"
+                     , timeBins, fTBins));
+       AliWarning(Form("We will use the value from the raw data (HC header): %d", fTBins));
+       AliWarning("===============================================================================");
+
+       fTimeWords = (fTBins - 1)/3 + 1;        
       }
 
     }
 
   }
 
-  // 3rd word
+  // 3nd word (h[2])
   if ( fHCHWords >= 2 ) {
     // read one more word
-    if ( !fRawReader->ReadNextInt( fDataWord ) ) {
-      AliError("Could not read data");
-      return;
-    }
-    if ( (fDataWord & 0x3) == 1 ) {
-      /*
-      Not finished. Next to come:
-      fTCon 
-      fPEDon
-      fGAINon
-      fFiltered
-      .....
-    */
+    if (NextData() != kWordOK)
+      {
+       AliWarning("Next HC word missing");
+        fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC word missing"); 
+       fNextStatus = kNextHC;
+       return;
+      }
+    if ( (*fDataWord & 0x3) == 1 ) {
+       
+      fTCon     = (*fDataWord >> 29) & 0x1;
+      fPEDon    = (*fDataWord >> 31) & 0x1;
+      fGAINon   = (*fDataWord >> 30) & 0x1;
+      fXTon     = (*fDataWord >> 28) & 0x1;
+      fNonLinOn = (*fDataWord >> 27) & 0x1;
+      fBypass   = (*fDataWord >> 26) & 0x1;
+
+      fCommonAdditive = (*fDataWord >> 20) & 0x3f;
+
+      AliDebug(3, Form("0x%08x: HC header 3: TC=%d, PED=%d, GAIN=%d, XT=%d, NonLin=%d, Bypass=%d, Add=%d"
+                     , fTCon, fPEDon, fGAINon, fXTon, fNonLinOn, fBypass, fCommonAdditive));
     }
-
   }
 
 }  
 
 //____________________________________________________________________________
 void AliTRDRawStream::DecodeMCMheader()
-{
-  //
-  //
-  //
-
-  if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
-    DecodeMCMheaderV1();
-    return;
-  }
-
-  AliError(Form(" Unsupported raw version: %d", fRawVersion));
-  return;
-
-}
-
-//____________________________________________________________________________
-void AliTRDRawStream::DecodeMCMheaderV1()
 {
 
   //
   // Decode the MCM header
   //
 
-  fMCM  = (fDataWord & 0xff000000) >> 24;
-  fEv   = (fDataWord & 0x00fffff0) >> 4;
+  if ( fRawVersion < 1 || fRawVersion > 3 ) 
+    {
+      AliError(Form(" Unsupported raw version: %d", fRawVersion));      
+    }
+
+  fMCM  = (*fDataWord & 0xff000000) >> 24;
+  fEv   = (*fDataWord & 0x00fffff0) >> 4;
 
   fROB  = fMCM / 16;
   fMCM  = fMCM % 16;
 
-  fROW  = fGeo->GetPadRow(fROB, fMCM);
+  fROW  = AliTRDfeeParam::Instance()->GetPadRowFromMCM(fROB, fMCM);
 
   AliDebug(4, Form("0x%08x: SM%d L%dS%d. MCM Header: fROB=%d fMCM=%02d fEv=%02d"
-                 , fDataWord, fSM, fLAYER, fSTACK, fROB, fMCM, fEv));
+                 , *fDataWord, fSM, fLAYER, fSTACK, fROB, fMCM, fEv));
 
   if ( fROB % 2 == 0 && fSIDE == 1 ) {
-    AliError(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
-                 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
+    AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
+                   , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
+    fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
+                                                      , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
   }
   if ( fROB % 2 != 0 && fSIDE == 0 ) {
-    AliError(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
-                 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
+    AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
+                   , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
+    fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
+                                                      , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
   }
-  if ( (fSTACK == 2 && fROW > 11) || (fSTACK != 2 && fROW > 15) || fROW < 0 ) {
-    AliError(Form("SM%d L%dS%d: Wrong Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
-                 , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
+  if ( (fSTACK == 2 && fROW >= fGeo->RowmaxC0()) ||
+       (fSTACK != 2 && fROW >= fGeo->RowmaxC1()) || fROW < 0 ) {
+    AliWarning(Form("SM%d L%dS%d: Wrong Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
+                   , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
+    fRawReader->AddMajorErrorLog(kWrongPadrow,Form("SM%d L%dS%d: Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
+                                                 , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
   }
   
   fMCMHctr1++;
   fMCMHctr2++;
 
-}
+  // AdcMask for Zero supressed data
+  if ( fRawVersion == 3 ) {
+    // read one more word
+    if (NextData() != kWordOK)
+      {
+       AliWarning("MCM ADC mask missing");
+        fRawReader->AddMajorErrorLog(kMCMADCMaskMissing,"Missing"); 
+       fNextStatus = kNextHC;
+       return;
+      }
+    if ( (*fDataWord & 0x000007ff) == 0xC ) {     // at the moment bits 4-10 are empty
+      
+      for ( Int_t ctr = 0; ctr < fGeo->ADCmax(); ctr++ ) {
+       if ( (*fDataWord >> (11+ctr)) == 0x1 ) fADCmask[ctr] = kTRUE;
+       else                                  fADCmask[ctr] = kFALSE;
+      }
 
-//____________________________________________________________________________
-void AliTRDRawStream::DecodeTracklet()
-{
-  //
-  //
-  //
+      AliDebug(4, Form("0x%08x: ADC mask", *fDataWord));
 
-  if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
-    DecodeTrackletV1();
-    return;
-  }
+    }
+    else {
+      AliWarning("Expected ADC mask but did not find one!");
+      fRawReader->AddMajorErrorLog(kMCMADCMaskMissing,"Decode error"); 
+    }
 
-  AliError(Form(" Unsupported raw version: %d", fRawVersion));
-  return;
+  }
 
 }
 
 //____________________________________________________________________________
-void AliTRDRawStream::DecodeTrackletV1()
+void AliTRDRawStream::DecodeTracklet()
 {
 
   //
@@ -1129,22 +1120,28 @@ void AliTRDRawStream::DecodeTrackletV1()
   // this function is not tested yet on real tracklets
   //
 
-  fTracklPID    = (fDataWord >> 24) & 0xff;
-  fTracklPadRow = (fDataWord >> 20) & 0xf;    // 0:15
-  fTracklDefL   = (fDataWord >> 13) & 0x7f;
-  fTracklPadPos = (fDataWord)       & 0x1fff;
+  if ( fRawVersion < 1 || fRawVersion > 3 ) 
+    {
+      AliError(Form(" Unsupported raw version: %d", fRawVersion));      
+    }
+
+  fTracklPID    = (*fDataWord >> 24) & 0xff;
+  fTracklPadRow = (*fDataWord >> 20) & 0xf;    // 0:15
+  fTracklDefL   = (*fDataWord >> 13) & 0x7f;
+  fTracklPadPos = (*fDataWord)       & 0x1fff;
 
   fTracklPID    /= (Float_t)((1<<8) - 1);                      // 0:1 (steps of 0.39%)
   fTracklDefL    = (fTracklDefL  - ((1<< 7)-1)/2.) * 140.e-4;  // -0.889:0.889cm 
   fTracklPadPos  = (fTracklPadPos - ((1<<13)-1)/2.) * 160.e-4; // -65.528:65.528 cm
 
-  AliDebug(4, Form("0x%08x: Tracklet found: SM%d L%dS%d side %x: PadRow=%d PadPos=%f DefL=%f PID=%f"
-                 , fDataWord, fSM, fLAYER, fSTACK, fSIDE+10
-                  , fTracklPadRow, fTracklPadPos, fTracklDefL, fTracklPID));
+  //AliDebug(4, Form("0x%08x: Tracklet found: SM%d L%dS%d side %x: PadRow=%d PadPos=%f DefL=%f PID=%f"
+  //             , *fDataWord, fSM, fLAYER, fSTACK, fSIDE+10
+  //                , fTracklPadRow, fTracklPadPos, fTracklDefL, fTracklPID));
 
   if( (fSTACK == 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC0()) ||
       (fSTACK != 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC1()) ) {
-    AliError(Form("Strange Row read from Tracklet Word: %d", fTracklPadRow));
+    AliWarning(Form("Strange Row read from Tracklet Word: %d", fTracklPadRow));
+    fRawReader->AddMajorErrorLog(kTrackletRowMismatch,Form("Word: %d", fTracklPadRow));
   }
 
 }
@@ -1152,45 +1149,218 @@ void AliTRDRawStream::DecodeTrackletV1()
 //____________________________________________________________________________
 void AliTRDRawStream::DecodeGTUlinkMask()
 {
+
   //
+  // Decode the link masks sent by the GTU. These marke the active optical links
+  // between GTU and Super Module. Up to now only fully active links are found
+  // (0xfff = 12 active links).
+  //
+
+  if ( fRawVersion < 1 || fRawVersion > 3 ) 
+    {
+      AliError(Form(" Unsupported raw version: %d", fRawVersion));      
+    }
+
+  if ( fGTUctr1 == -1 ) fGTUctr2++;
+  fGTUctr1++;
+
+  if ( (fGTUctr1 >= 0) && (fGTUctr1 < 5) && (fGTUctr2 >= 0) && (fGTUctr2 < 18) ) {
+    fGTUlinkMask[fGTUctr2][fGTUctr1] = (*fDataWord & 0xfff);
+  }
+
+}
+
+//____________________________________________________________________________
+Int_t  AliTRDRawStream::DecodeDataWord()
+{
+
   //
+  // Decode the Data
   //
 
-  if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
-    DecodeGTUlinkMaskV1();
-    return;
+  if      ( fRawVersion >= 1 && fRawVersion <= 2 ) {
+    return DecodeDataWordV1V2();
+  }
+  else if ( fRawVersion >= 3 && fRawVersion <= 3 ) {
+    return DecodeDataWordV3();
   }
 
   AliError(Form(" Unsupported raw version: %d", fRawVersion));
-  return;
+  return -1;
 
 }
 
 //____________________________________________________________________________
-void AliTRDRawStream::DecodeGTUlinkMaskV1()
+Int_t  AliTRDRawStream::DecodeDataWordV1V2()
 {
 
   //
-  // Decode the link masks sent by the GTU. These marke the active optical links
-  // between GTU and Super Module. Up to now only fully active links are found
-  // (0xfff = 12 active links).
+  // Decode the Data (full raw data. No zero suppression. 21 adc channels)
+  //
+  // return  0 means continue to next data word
+  // return -1 means break data loop
   //
 
-  if ( fGTUctr1 == -1 ) fGTUctr2++;
-  fGTUctr1++;
+//   //  check the content first! - something wrong with that...
+//   // Decode 32 bit data words with information from 3 time bins and copy the data
+//   fSig[0] = (*fDataWord & 0x00000ffc) >> 2;
+//   fSig[1] = (*fDataWord & 0x003ff000) >> 12;
+//   fSig[2] = (*fDataWord & 0xffc00000) >> 22;  
+//   if (fSig[0] <= 0 && fSig[1] <= 0 && fSig[2] <= 0)
+//     return 0;
+
+  if ( (*fDataWord & 0x00000003) != 0x2 && (*fDataWord & 0x00000003) != 0x3) {
+    //AliWarning(Form("Data %08x : Data Word ends neither with b11 nor b10", (Int_t)*fDataWord));
+    fRawReader->AddMinorErrorLog(kDataMaskError,Form("Data %08x", (Int_t)*fDataWord));
+    return -1;
+  }
 
-  //printf("fGTUctr=%d",fGTUctr);
-  //printf("fGTUctr1=%d, fGTUctr2=%d",fGTUctr1, fGTUctr2);
+  if ( (*fDataWord & 0x00000003) != fTbSwitch ) {    // Next ADC channel found
+    fTbSwitch = (fTbSwitch & 2) | !(fTbSwitch & 1);   // 0x3 <--> 0x2
+    fTbSwitchCtr = 0;
+    fADC++;
+    fTB=0;
+  }
 
-  if ( (fGTUctr1 >= 0) && (fGTUctr1 < 5) && (fGTUctr2 >= 0) && (fGTUctr2 < 18) ) {
-    fGTUlinkMask[fGTUctr2][fGTUctr1] = (fDataWord & 0xfff);
+  fTbSwitchCtr++; // Just read one word
+
+  // We have only timeTotal time bins
+  if ( fTbSwitchCtr > fTimeWords ) {
+    //AliWarning(Form("Data is strange. Already found %d words for this ADC channel", (Int_t)fTbSwitchCtr));
+    fRawReader->AddMinorErrorLog(kADCNumberOverflow,Form("%d words", (Int_t)fTbSwitchCtr));
+    return 0;
   }
 
-  for ( Int_t ctr = 0; ctr < 12; ctr++ ) {
-    if ( IsGTULinkActive(fGTUctr2, ctr/2, fGTUctr1, ctr%2) ) {
-      AliDebug(3, Form("SM %2d Stack %d: GTU Link %2d is active!", fGTUctr2, fGTUctr1, ctr)); 
-    }
+  // We have only 21 ADC channels.
+  if ( fADC > (Int_t)fGeo->ADCmax()-1 ) {
+    //AliWarning(Form("Data %08x : Data is strange. fADC is already %d", (Int_t)*fDataWord, (Int_t)fADC));
+    fRawReader->AddMinorErrorLog(kADCChannelOverflow,Form("Data %08x : fADC=%d", (Int_t)*fDataWord, (Int_t)fADC));
+    return 0;
+  }
+
+  // There are 18 pads connected to each MCM ADC channels 2...19. The other channels cross to other
+  // MCMs and are good for online tracking in the MCM.
+  if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) {
+
+    // Get Pad column
+    fCOL = AliTRDfeeParam::Instance()->GetPadColFromADC(fROB, fMCM, fADC);
+
+    // We have only 144 Pad Columns
+    //if ( fCOL > fColMax-1 || fCOL < 0 ) {
+    if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) 
+      {
+       // Decode 32 bit data words with information from 3 time bins and copy the data
+       fSig[0] = (*fDataWord & 0x00000ffc) >> 2;
+       fSig[1] = (*fDataWord & 0x003ff000) >> 12;
+       fSig[2] = (*fDataWord & 0xffc00000) >> 22;
+       
+       if (fSig[0] > 0 || fSig[1] > 0 || fSig[2] > 0)
+         return 1;
+       else
+         return 0;
+      }
+    else
+      {
+//     AliWarning(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
+//                     fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
+       fRawReader->AddMajorErrorLog(kWrongPadcolumn,Form("SM%d L%dS%d: column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
+                                                         fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
+       return 0;
+      }
+    // Print data to screen:
+    // Do NOT switch on for default production, it is VERY slow
+    //    AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
+    //              fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));
+    
   }
+  else {
+    
+    fCOL = -1;
+    return 0;
+  }
+
+  return 1;
 
 }
 
+//____________________________________________________________________________
+Int_t  AliTRDRawStream::DecodeDataWordV3()
+{
+
+  //
+  // Decode the data (Zero suppresses data. 21 adc channels)
+  //
+  // return  0 means continue to next data word
+  // return -1 means break data loop
+  //
+  // NOT TESTED YET!!!!!!!!
+  //
+
+  if ( (*fDataWord & 0x00000003) != 0x2 && (*fDataWord & 0x00000003) != 0x3) {
+    AliWarning(Form("Data %08x : Data Word ends neither with b11 nor b10", (Int_t)*fDataWord));
+    fRawReader->AddMinorErrorLog(kDataMaskError,Form("Data %08x", (Int_t)*fDataWord));
+    return -1;
+  }
+
+  if ( (*fDataWord & 0x00000003) != fTbSwitch ) {    // Next ADC channel found
+    fTbSwitch = (fTbSwitch & 2) | !(fTbSwitch & 1);   // 0x3 <--> 0x2
+    fTbSwitchCtr = 0;
+    //
+    // Jump to next ADC channel that is not masked
+    do {
+      fADC++;
+    } while ( ((fADC < fGeo->ADCmax()) && (fADCmask[fADC] == kFALSE)) || (fADC >= fGeo->ADCmax()) );
+    fTB=0;
+  }
+
+  fTbSwitchCtr++; // Just read one word
+
+  // We have only timeTotal time bins
+  if ( fTbSwitchCtr > fTimeWords ) {
+    AliWarning(Form("Data is strange. Already found %d words for this ADC channel", (Int_t)fTbSwitchCtr));
+    fRawReader->AddMinorErrorLog(kADCNumberOverflow,Form("%d words", (Int_t)fTbSwitchCtr));
+    return 0;
+  }
+
+  // We have only 21 ADC channels.
+  if ( fADC > (Int_t)fGeo->ADCmax()-1 ) {
+    AliWarning(Form("Data %08x : Data is strange. fADC is already %d", (Int_t)*fDataWord, (Int_t)fADC));
+    fRawReader->AddMinorErrorLog(kADCChannelOverflow,Form("Data %08x : fADC=%d", (Int_t)*fDataWord, (Int_t)fADC));
+    return 0;
+  }
+
+  // There are 18 pads connected to each MCM ADC channels 2...19. The other channels cross to other
+  // MCMs and are good for online tracking in the MCM.
+  if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) {
+
+    // Get Pad column
+    //fCOL = fGeo->GetPadColFromADC(fROB, fMCM, fADC);
+    fCOL = AliTRDfeeParam::Instance()->GetPadColFromADC(fROB, fMCM, fADC);
+
+    // We have only 144 Pad Columns
+    if ( fCOL > fColMax-1 || fCOL < 0 ) {
+      AliWarning(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
+                   fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
+      fRawReader->AddMajorErrorLog(kWrongPadcolumn,Form("SM%d L%dS%d: column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
+                   fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
+    }
+
+    // Decode 32 bit data words with information from 3 time bins and copy the data
+    fSig[0] = (*fDataWord & 0x00000ffc) >> 2;
+    fSig[1] = (*fDataWord & 0x003ff000) >> 12;
+    fSig[2] = (*fDataWord & 0xffc00000) >> 22;
+
+    // Print data to screen:
+    AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
+                    fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));
+    
+  }
+  else {
+    
+    fCOL = -1;
+    
+  }
+
+  return 1;
+
+}