]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFRawStream.cxx
A small change in histogram names
[u/mrichter/AliRoot.git] / TOF / AliTOFRawStream.cxx
index 114fe5666a518c5a477adf557cecf18d24c4945a..3152e642f735582d7443132a1dc4362857a33648 100644 (file)
 
 /*
 $Log$
+Revision 1.19.1  2008/09/19  preghenella
+  Decode method updated:
+  it reads the CDH from the rawReader and sends it to the decoder;
+ LoadRawDataBuffers modified:
+     it corrects tof hit infos per ddlBC and deltaBC offsets
+     (in case of the static member fgApplyBCCorrections
+      has been setted to kTRUE);
+ Added static member fgApplyBCCorrections (kTRUE by default)
+ and the related static method ApplyBCCorrections;
+
 Revision 1.19  2007/05/18 13:07:53  decaro
 Error messages stored in the global raw-reader error log (Cvetan, Chiara)
 
@@ -92,6 +102,7 @@ Revision 0.01  2005/07/22 A. De Caro
 #include "Riostream.h"
 
 #include "TClonesArray.h"
+#include "TStopwatch.h"
 
 #include "AliDAQ.h"
 #include "AliLog.h"
@@ -101,18 +112,21 @@ Revision 0.01  2005/07/22 A. De Caro
 #include "AliTOFrawData.h"
 #include "AliTOFRawMap.h"
 #include "AliTOFRawStream.h"
+#include "AliTOFdigit.h"
+#include "AliTOFSDigit.h"
 //#include "AliTOFCableLengthMap.h"
 
 #include "AliTOFHitData.h"
 
 #include "AliRawEventHeaderBase.h"
+#include "AliRawDataHeader.h"
 
 ClassImp(AliTOFRawStream)
 
 const Int_t AliTOFRawStream::fgkddlBCshift[72] = 
 {
   2, 2, -1, -1,
-  2, 3,  0,  0,
+  2, 2,  0,  0,
   2, 2,  0,  0,
   2, 2,  0,  0,
   2, 2,  0,  0,
@@ -131,11 +145,11 @@ const Int_t AliTOFRawStream::fgkddlBCshift[72] =
   2, 2, -1, -1
 };
 
-
+Bool_t AliTOFRawStream::fgApplyBCCorrections = kTRUE;
 //_____________________________________________________________________________
 AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fRawReader(rawReader),
-  fTOFrawData(0x0),
+  fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
   fDecoder(new AliTOFDecoder()),
   fDDL(-1),
   fTRM(-1),
@@ -163,54 +177,48 @@ AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
   fInsideTRMchain1(kFALSE),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
   fLocalEventCounterDRM(-1),
   fLocalEventCounterLTM(-1),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fCableLengthMap(0x0),
-  fEventID(0)
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(new AliTOFCableLengthMap()),
+  fEventID(0),
+  fNewDecoderVersion(0)
 {
   //
   // create an object to read TOF raw digits
   //
 
   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
-    fDataBuffer[i]=new AliTOFHitDataBuffer();
-    fPackedDataBuffer[i]=new AliTOFHitDataBuffer();
+    ResetDataBuffer(i);
+    ResetPackedDataBuffer(i);
   }
 
-  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  //fTOFrawData = new TClonesArray("AliTOFrawData",1000);
   fTOFrawData->SetOwner();
 
   fRawReader->Reset();
   fRawReader->Select("TOF");
 
-  fLocalEventCounterTRM = new Int_t[13];//adc
-  fLocalEventCounterChain = new Int_t*[13];//adc
-  for (Int_t j=0;j<13;j++){//adc
-    fLocalEventCounterTRM[j] = -1;//adc
-    fLocalEventCounterChain[j] = new Int_t[2];//adc
-    for (Int_t k=0;k<2;k++){//adc
-      fLocalEventCounterChain[j][k] = -1;//adc
-    }//adc
-  }//adc
-
-  fCableLengthMap = new AliTOFCableLengthMap();
-
-  const AliRawEventHeaderBase * eventHeader = fRawReader->GetEventHeader();
-  //UInt_t run = rawReader->GetRunNumber();
-  const UInt_t *id = eventHeader->GetP("Id");
-  fEventID = ((id)[1]&0x00000fff); //bunch crossing
-  //UInt_t orbit=((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff)); //orbit number
-  //UInt_t period=(((id)[0]>>4)&0x0fffffff); //period number
-  //UInt_t type=eventHeader->Get("Type"); //this is 7 in physics events
+  for (Int_t jj=0;jj<13;jj++) {
+    fLocalEventCounterTRM[jj] = -1;
+    for (Int_t ii=0;ii<2;ii++) {
+      fLocalEventCounterChain[jj][ii] = -1;
+      fChainBunchID[jj][ii] = -1;
+    }
+  }
+
+  fEventID = (Int_t)fRawReader->GetBCID(); //bunch crossing
 
 }
 
 //_____________________________________________________________________________
 AliTOFRawStream::AliTOFRawStream():
-  fRawReader(0x0), 
-  fTOFrawData(0x0),
+  fRawReader(0x0),
+  fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
   fDecoder(new AliTOFDecoder()),
   fDDL(-1),
   fTRM(-1),
@@ -238,135 +246,99 @@ AliTOFRawStream::AliTOFRawStream():
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
   fInsideTRMchain1(kFALSE),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
   fLocalEventCounterDRM(-1),
   fLocalEventCounterLTM(-1),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fCableLengthMap(0x0),
-  fEventID(0)
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(new AliTOFCableLengthMap()),
+  fEventID(0),
+  fNewDecoderVersion(0)
 {
   //
   // default ctr
   //
   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
-    fDataBuffer[i]=new AliTOFHitDataBuffer();
-    fPackedDataBuffer[i]=new AliTOFHitDataBuffer();
+    ResetDataBuffer(i);
+    ResetPackedDataBuffer(i);
   }
 
-  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  //fTOFrawData = new TClonesArray("AliTOFrawData",1000);
   fTOFrawData->SetOwner();
 
-  fLocalEventCounterTRM = new Int_t[13];//adc
-  fLocalEventCounterChain = new Int_t*[13];//adc
-  for (Int_t j=0;j<13;j++){//adc
-    fLocalEventCounterTRM[j] = -1;//adc
-    fLocalEventCounterChain[j] = new Int_t[2];//adc
-    for (Int_t k=0;k<2;k++){//adc
-      fLocalEventCounterChain[j][k] = -1;//adc
-    }//adc
-  }//adc
-
-  fCableLengthMap = new AliTOFCableLengthMap();
+  for (Int_t j=0;j<13;j++){
+    fLocalEventCounterTRM[j] = -1;
+    for (Int_t k=0;k<2;k++){
+      fLocalEventCounterChain[j][k] = -1;
+      fChainBunchID[j][k] = -1;
+    }
+  }
 
 }
 
 //_____________________________________________________________________________
 AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   TObject(stream),
-  fRawReader(0x0),
-  fTOFrawData(0x0),
+  fRawReader(stream.fRawReader),
+  fTOFrawData(stream.fTOFrawData),
   fDecoder(new AliTOFDecoder()),
-  fDDL(-1),
-  fTRM(-1),
-  fTRMchain(-1),
-  fTDC(-1),
-  fTDCchannel(-1),
-  fTime(-1),
-  fToT(-1),
-  fLeadingEdge(-1),
-  fTrailingEdge(-1),
-  fErrorFlag(-1),
-  fSector(-1),
-  fPlate(-1),
-  fStrip(-1),
-  fPadX(-1),
-  fPadZ(-1),
-  fPackedDigits(0),
-  fWordType(-1),
-  fSlotID(-1),
-  fACQ(-1),
-  fPSbit(-1),
-  fTDCerrorFlag(-1),
-  fInsideDRM(kFALSE),
-  fInsideTRM(kFALSE),
-  fInsideLTM(kFALSE),
-  fInsideTRMchain0(kFALSE),
-  fInsideTRMchain1(kFALSE),
-  fLocalEventCounterDRM(-1),
-  fLocalEventCounterLTM(-1),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fCableLengthMap(0x0),
-  fEventID(0)
+  fDDL(stream.fDDL),
+  fTRM(stream.fTRM),
+  fTRMchain(stream.fTRMchain),
+  fTDC(stream.fTDC),
+  fTDCchannel(stream.fTDCchannel),
+  fTime(stream.fTime),
+  fToT(-stream.fToT),
+  fLeadingEdge(stream.fLeadingEdge),
+  fTrailingEdge(stream.fTrailingEdge),
+  fErrorFlag(stream.fErrorFlag),
+  fSector(stream.fSector),
+  fPlate(stream.fPlate),
+  fStrip(stream.fStrip),
+  fPadX(stream.fPadX),
+  fPadZ(stream.fPadZ),
+  fPackedDigits(stream.fPackedDigits),
+  fWordType(stream.fWordType),
+  fSlotID(stream.fSlotID),
+  fACQ(stream.fACQ),
+  fPSbit(stream.fPSbit),
+  fTDCerrorFlag(stream.fTDCerrorFlag),
+  fInsideDRM(stream.fInsideDRM),
+  fInsideTRM(stream.fInsideTRM),
+  fInsideLTM(stream.fInsideLTM),
+  fInsideTRMchain0(stream.fInsideTRMchain0),
+  fInsideTRMchain1(stream.fInsideTRMchain1),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
+  fLocalEventCounterDRM(stream.fLocalEventCounterDRM),
+  fLocalEventCounterLTM(stream.fLocalEventCounterLTM),
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(stream.fCableLengthMap),
+  fEventID(stream.fEventID),
+  fNewDecoderVersion(stream.fNewDecoderVersion)
 {
   //
   // copy constructor
   //
 
-  fRawReader = stream.fRawReader;
-
-  fTOFrawData = stream.fTOFrawData;
-
-  fDDL = stream.fDDL;
-  fTRM = stream.fTRM;
-  fTRMchain = stream.fTRMchain;
-  fTDC = stream.fTDC;
-  fTDCchannel = stream.fTDCchannel;
-  fTime = stream.fTime;
-  fToT = stream.fToT;
-  fLeadingEdge = stream.fLeadingEdge;
-  fTrailingEdge = stream.fTrailingEdge;
-
-  fErrorFlag = stream.fErrorFlag;
-
-  fSector = stream.fSector;
-  fPlate = stream.fPlate;
-  fStrip = stream.fStrip;
-  fPadX = stream.fPadX;
-  fPadZ = stream.fPadZ;
-
-  fPackedDigits = stream.fPackedDigits;
-
-  fWordType = stream.fWordType;
-  fSlotID = stream.fSlotID;
-  fACQ = stream.fACQ;
-  fPSbit = stream.fPSbit;
-  fTDCerrorFlag = stream.fTDCerrorFlag;
-  fInsideDRM = stream.fInsideDRM;
-  fInsideTRM = stream.fInsideTRM;
-  fInsideLTM = stream.fInsideLTM;
-  fInsideTRMchain0 = stream.fInsideTRMchain0;
-  fInsideTRMchain1 = stream.fInsideTRMchain1;
-
   for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
-    fDataBuffer[i]= new AliTOFHitDataBuffer(*stream.fDataBuffer[i]);
-    fPackedDataBuffer[i]= new AliTOFHitDataBuffer(*stream.fPackedDataBuffer[i]);
+    fDataBuffer[i] = stream.fDataBuffer[i];
+    fPackedDataBuffer[i] = stream.fPackedDataBuffer[i];
   }
 
   fTOFrawData = new TClonesArray(*stream.fTOFrawData);
 
-  fLocalEventCounterDRM = stream.fLocalEventCounterDRM;//adc
-  fLocalEventCounterLTM = stream.fLocalEventCounterLTM;//adc
-  for (Int_t j=0;j<13;j++){//adc
-    fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];//adc
-    for (Int_t k=0;k<2;k++){//adc
-      fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];//adc
-    }//adc
-  }//adc
-
-  fCableLengthMap = stream.fCableLengthMap;
-
-  fEventID = stream.fEventID;
+  for (Int_t j=0;j<13;j++){
+    fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];
+    for (Int_t k=0;k<2;k++){
+      fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];
+      fChainBunchID[j][k] = stream.fChainBunchID[j][k];
+    }
+  }
 
 }
 
@@ -377,6 +349,11 @@ AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
   // assignment operator
   //
 
+  if (this == &stream)
+    return *this;
+
+  TObject::operator=(stream);
+
   fRawReader = stream.fRawReader;
 
   fTOFrawData = stream.fTOFrawData;
@@ -418,18 +395,20 @@ AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
   
   fTOFrawData = stream.fTOFrawData;
 
-  fLocalEventCounterDRM = stream.fLocalEventCounterDRM;//adc
-  fLocalEventCounterLTM = stream.fLocalEventCounterLTM;//adc
-  for (Int_t j=0;j<13;j++){//adc
-    fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];//adc
-    for (Int_t k=0;k<2;k++){//adc
-      fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];//adc
-    }//adc
-  }//adc
+  fLocalEventCounterDRM = stream.fLocalEventCounterDRM;
+  fLocalEventCounterLTM = stream.fLocalEventCounterLTM;
+  for (Int_t j=0;j<13;j++){
+    fLocalEventCounterTRM[j] = stream.fLocalEventCounterTRM[j];
+    for (Int_t k=0;k<2;k++){
+      fLocalEventCounterChain[j][k] = stream.fLocalEventCounterChain[j][k];
+      fChainBunchID[j][k] = stream.fChainBunchID[j][k];
+    }
+  }
 
-  fCableLengthMap = stream.fCableLengthMap;
+  //fCableLengthMap = stream.fCableLengthMap;
 
   fEventID = stream.fEventID;
+  fNewDecoderVersion = stream.fNewDecoderVersion;
 
   return *this;
 
@@ -442,21 +421,12 @@ AliTOFRawStream::~AliTOFRawStream()
 
   fPackedDigits = 0;
 
-  for (Int_t i=0;i<72;i++){ 
-    delete fDataBuffer[i];
-    delete fPackedDataBuffer[i];
-  }
-
   delete fDecoder;
 
   fTOFrawData->Clear();
   delete fTOFrawData;
 
-  delete [] fLocalEventCounterTRM;
-  for (Int_t ii=0; ii<2; ii++) 
-    delete [] fLocalEventCounterChain[ii];
-
-  delete fCableLengthMap;
+  //delete fCableLengthMap;
 
 }
 
@@ -486,8 +456,10 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
   for (Int_t ii=0; ii<13; ii++)
     fLocalEventCounterTRM[ii] = -1;
   for (Int_t ii=0; ii<13; ii++)
-    for (Int_t jj=0; jj<2; jj++)
+    for (Int_t jj=0; jj<2; jj++) {
       fLocalEventCounterChain[ii][jj] = -1;
+      fChainBunchID[ii][jj] = -1;
+    }
 
   fRawReader->Reset();
   fRawReader->Select("TOF", indexDDL, indexDDL);
@@ -635,7 +607,7 @@ Bool_t AliTOFRawStream::Next()
       }
       dummy = 0x0000fff0;
       //AliInfo(Form("  DRM local event counter = %i", GetField(data,dummy,4)));
-      fLocalEventCounterDRM = GetField(data,dummy,4);//adc
+      fLocalEventCounterDRM = GetField(data,dummy,4);
       fInsideDRM = kFALSE; // DRM global trailer accepted
       fInsideTRM = kFALSE;
       fInsideLTM = kFALSE;
@@ -666,7 +638,7 @@ Bool_t AliTOFRawStream::Next()
       }
       dummy = 0x0fff0000;
       //AliInfo(Form("  LTM local event counter = %i", GetField(data,dummy,16)));
-      fLocalEventCounterLTM = GetField(data,dummy,16);//adc
+      fLocalEventCounterLTM = GetField(data,dummy,16);
       fInsideLTM = kFALSE; // LTM global trailer accepted
       break;
     case 15: //TRM global trailer
@@ -675,7 +647,7 @@ Bool_t AliTOFRawStream::Next()
       }
       dummy = 0x0fff0000;
       //AliInfo(Form("  TRM local event counter = %i", GetField(data,dummy,16)));
-      fLocalEventCounterTRM[fTRM] = GetField(data,dummy,16);//adc
+      fLocalEventCounterTRM[fTRM] = GetField(data,dummy,16);
       fInsideTRM = kFALSE; // TRM global trailer accepted
       break;
     default: // unexpected global trailer slot ID
@@ -707,6 +679,9 @@ Bool_t AliTOFRawStream::Next()
        }
        fInsideTRMchain0 = kTRUE;
        fTRMchain = 0;
+        dummy = 0x0000fff0;
+        //AliInfo(Form("  chain bunch ID = %i", GetField(data,dummy,4)));
+        fChainBunchID[fTRM][fTRMchain] = GetField(data,dummy,4);
        break;
       case TRM_CHAIN0_TRAILER_TYPE: // TRM chain0 trailer
        if (!fInsideTRMchain0) { // unexpected TRM chain0 trailer
@@ -714,7 +689,7 @@ Bool_t AliTOFRawStream::Next()
        }
         dummy = 0x0fff0000;
         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
-        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);//adc
+        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
        fInsideTRMchain0 = kFALSE;
        fTRMchain = -1;
        break;
@@ -724,6 +699,9 @@ Bool_t AliTOFRawStream::Next()
        }
        fInsideTRMchain1 = kTRUE;
        fTRMchain = 1;
+        dummy = 0x0000fff0;
+        //AliInfo(Form("  chain bunch ID = %i", GetField(data,dummy,4)));
+        fChainBunchID[fTRM][fTRMchain] = GetField(data,dummy,4);
        break;
       case TRM_CHAIN1_TRAILER_TYPE: // TRM chain1 trailer
        if (!fInsideTRMchain1) { // unexpected TRM chain1 trailer
@@ -731,7 +709,7 @@ Bool_t AliTOFRawStream::Next()
        }
         dummy = 0x0fff0000;
         //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
-        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);//adc
+        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
        fInsideTRMchain1 = kFALSE;
        fTRMchain = -1;
        break;
@@ -763,51 +741,71 @@ Bool_t AliTOFRawStream::Next()
       case 0: // packing ok, digit time and TOT
        fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
        fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
-         -
-         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)
-         /*+
-         (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
-         +
-         (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
+         /*-
+         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
          ;
+       if (fgApplyBCCorrections) {
+         AliDebug(2,"Apply nominal DDL BC time-shift correction");
+         AliDebug(2,"Apply deltaBC time-shift correction");
+         AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
+         fTime +=
+           (Int_t)(fgkddlBCshift[fDDL]*25./AliTOFGeometry::TdcBinWidth()*1.E+03)
+           +
+           (Int_t)((fChainBunchID[fTRM][fTRMchain]-fEventID)*25./AliTOFGeometry::TdcBinWidth()*1.E+03);
+       }
        break;
 
       case 1: // leading edge digit, long digit time, no TOT
        //fToT  = -1;
        //fTime  = -1;
        fLeadingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
-         -
-         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)
-         /*+
-         (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
-         +
-         (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
+         /*-
+         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
          ;
+       if (fgApplyBCCorrections) {
+         AliDebug(2,"Apply nominal DDL BC time-shift correction");
+         AliDebug(2,"Apply deltaBC time-shift correction");
+         AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
+         fLeadingEdge +=
+           (Int_t)(fgkddlBCshift[fDDL]*25./AliTOFGeometry::TdcBinWidth()*1.E+03)
+           +
+           (Int_t)((fChainBunchID[fTRM][fTRMchain]-fEventID)*25./AliTOFGeometry::TdcBinWidth()*1.E+03);
+       }
        break;
 
       case 2: // trailing edge digit, long digit time, no TOT
        //fToT  = -1;
        //fTime  = -1;
        fTrailingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
-         -
-         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)
-         /*+
-         (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
-         +
-         (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
+         /*-
+         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
          ;
+       if (fgApplyBCCorrections) {
+         AliDebug(2,"Apply nominal DDL BC time-shift correction");
+         AliDebug(2,"Apply deltaBC time-shift correction");
+         AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
+         fTrailingEdge +=
+           (Int_t)(fgkddlBCshift[fDDL]*25./AliTOFGeometry::TdcBinWidth()*1.E+03)
+           +
+           (Int_t)((fChainBunchID[fTRM][fTRMchain]-fEventID)*25./AliTOFGeometry::TdcBinWidth()*1.E+03);
+       }
        break;
 
       case 3: // TOT overflow
        fToT  = GetField(data,TRM_TOT_WIDTH_MASK, TRM_TOT_WIDTH_POSITION);
        fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
-         -
-         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)
-         /*+
-         (Int_t)(fgkddlBCshift[fDDL]*25.*1000./AliTOFGeometry::TdcBinWidth())
-         +
-         (Int_t)((fLocalEventCounterChain[fTRM][fTRMchain]-fEventID)*25.*1000./AliTOFGeometry::TdcBinWidth())*/
+         /*-
+         fCableLengthMap->GetCableTimeShiftBin(fDDL, fTRM, fTRMchain, fTDC)*/
          ;
+       if (fgApplyBCCorrections) {
+         AliDebug(2,"Apply nominal DDL BC time-shift correction");
+         AliDebug(2,"Apply deltaBC time-shift correction");
+         AliDebug(2,Form(" fChainBunchID[%d][%d] = %d ,fEventID = %d",fTRM,fTRMchain,fChainBunchID[fTRM][fTRMchain],fEventID));
+         fTime +=
+           (Int_t)(fgkddlBCshift[fDDL]*25./AliTOFGeometry::TdcBinWidth()*1.E+03)
+           +
+           (Int_t)((fChainBunchID[fTRM][fTRMchain]-fEventID)*25./AliTOFGeometry::TdcBinWidth()*1.E+03);
+       }
        break;
 
       } // end switch PS bits inside TRM chains
@@ -970,6 +968,7 @@ Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC)
   //
 
   Int_t iPlate = -1;
+
   if (iDDL==0) {
 
     if (nTRM>=4 && nTRM<7) {
@@ -1173,7 +1172,7 @@ Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
 
   if (((iDDL==1 || iDDL==2) && iPadAlongTheStrip< AliTOFGeometry::NpadX()) ||
       ((iDDL==0 || iDDL==3) && iPadAlongTheStrip>=AliTOFGeometry::NpadX())) {
-    std::cerr << "Problems with the padX number!" << endl;
+    std::cerr << "Warning -> AliTOFRawStream::Equip2VolNpad: Problems with the padX number!" << endl;
     //AliWarning("Problems with the padX number!");
   }
   return iPadAlongTheStrip;
@@ -1220,7 +1219,7 @@ Int_t AliTOFRawStream::Equip2VolNpadZ(Int_t iDDL, Int_t iChain, Int_t nTDC,
 }
 
 //----------------------------------------------------------------------------
-Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL) const
+Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL)
 {
   //
   // Returns the sector number [0;17]
@@ -1233,7 +1232,7 @@ Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL) const
 
 }
 //----------------------------------------------------------------------------
-Int_t AliTOFRawStream::GetDDLnumberPerSector(Int_t nDDL) const
+Int_t AliTOFRawStream::GetDDLnumberPerSector(Int_t nDDL)
 {
   //
   // Return the DRM/DDL number per sector [0;3]
@@ -1254,7 +1253,7 @@ void AliTOFRawStream::EquipmentId2VolumeId(AliTOFHitData *hitData, Int_t *volume
 //----------------------------------------------------------------------------
 void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
                                        Int_t nTDC, Int_t iCH,
-                                       Int_t *volume) const
+                                       Int_t *volume)
 {
   //
   // To convert:
@@ -1272,29 +1271,39 @@ void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
   //      padZ   number, i.e. volume[4] (variable in [0, 1])
   //
 
+  for (Int_t ii=0; ii<5; ii++) volume[ii] = -1;
+
   Int_t iDDL = GetDDLnumberPerSector(nDDL);
 
+  if (iDDL%2==1 && nTRM==3 && nTDC/3>0) { // Signal not coming from a TOF pad but -probably- from a TOF OR signal
+    //printf("Info -> AliTOFRawStream::EquipmentId2VolumeId: Signal not coming from a TOF pad but -probably- from a TOF OR signal (%2d %2d %2d)\n", nDDL, nTRM, nTDC);
+    return;
+  }
+
   Int_t iSector = GetSectorNumber(nDDL);
 
   Int_t iPlate = Equip2VolNplate(iDDL, nTRM, nTDC);
   if (iPlate==-1) {
-    if (fRawReader)
-      fRawReader->AddMajorErrorLog(kPlateError,"plate = -1");
-    AliWarning("Problems with the plate number!");
+    /*if (fRawReader)
+      fRawReader->AddMajorErrorLog(kPlateError,"plate = -1");*/
+    printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the plate number (%2d %2d %2d)!\n",
+          nDDL, nTRM, nTDC);
   }
 
   Int_t iStrip = Equip2VolNstrip(iDDL, nTRM, nTDC);
   if (iStrip==-1) {
-    if (fRawReader)
-      fRawReader->AddMajorErrorLog(kStripError,"strip = -1");
-    AliWarning("Problems with the strip number!");
+    /*if (fRawReader)
+      fRawReader->AddMajorErrorLog(kStripError,"strip = -1");*/
+    printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the strip number (%2d %2d %2d)!\n",
+          nDDL, nTRM, nTDC);
   }
 
   Int_t iPadAlongTheStrip  = Equip2VolNpad(iDDL, iChain, nTDC, iCH);
-  if (iPadAlongTheStrip==-1){
-    if (fRawReader)
-      fRawReader->AddMajorErrorLog(kPadAlongStripError,"pad = -1");
-    AliWarning("Problems with the pad number along the strip!");
+  if (iPadAlongTheStrip==-1) {
+    /*if (fRawReader)
+      fRawReader->AddMajorErrorLog(kPadAlongStripError,"pad = -1");*/
+    printf("Warning -> AliTOFRawStream::EquipmentId2VolumeId: Problems with the pad number along the strip (%2d %1d %2d %1d)!\n",
+          nDDL, iChain, nTDC, iCH);
   }
   
   Int_t iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
@@ -1347,6 +1356,7 @@ Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
 
   Int_t currentEquipment;
   Int_t currentDDL;
+  const AliRawDataHeader *currentCDH;
 
   //pointers
   UChar_t *data = 0x0;
@@ -1361,6 +1371,7 @@ Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
     //get equipment infos
     currentEquipment = fRawReader->GetEquipmentId();
     currentDDL = fRawReader->GetDDLID();
+    currentCDH = fRawReader->GetDataHeader();
     const Int_t kDataSize = fRawReader->GetDataSize();
     const Int_t kDataWords = kDataSize / 4;
     data = new UChar_t[kDataSize];
@@ -1388,11 +1399,11 @@ Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
     
     //set up the decoder
     fDecoder->SetVerbose(verbose);
-    fDecoder->SetDataBuffer(fDataBuffer[currentDDL]);
-    fDecoder->SetPackedDataBuffer(fPackedDataBuffer[currentDDL]);
+    fDecoder->SetDataBuffer(&fDataBuffer[currentDDL]);
+    fDecoder->SetPackedDataBuffer(&fPackedDataBuffer[currentDDL]);
     
     //start decoding
-    if (fDecoder->Decode((UInt_t *)data, kDataWords) == kTRUE) {
+    if (fDecoder->Decode((UInt_t *)data, kDataWords, currentCDH) == kTRUE) {
       fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",currentDDL));
       AliWarning(Form("Error while decoding DDL # %d: decoder returned with errors", currentDDL));
     }
@@ -1445,16 +1456,22 @@ AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
   }
   
   if (verbose > 0)
-    AliInfo(Form("Done. %d packed %s been found.", fPackedDataBuffer[indexDDL]->GetEntries(), fPackedDataBuffer[indexDDL]->GetEntries() > 1 ? "hits have" : "hit has"));
+    AliInfo(Form("Done. %d packed %s been found.", fPackedDataBuffer[indexDDL].GetEntries(), fPackedDataBuffer[indexDDL].GetEntries() > 1 ? "hits have" : "hit has"));
   
   AliTOFHitData *hitData; //hit data pointer
   
   if (verbose > 0)
     AliInfo("Filling TClonesArray ...");
 
+  if (verbose > 0)
+    if (fgApplyBCCorrections) {
+      AliInfo("Apply nominal DDL BC time-shift correction");
+      AliInfo("Apply deltaBC time-shift correction");
+    }
+
   //loop over DDL packed hits
-  for (Int_t iHit = 0; iHit < fPackedDataBuffer[indexDDL]->GetEntries(); iHit++){
-    hitData = fPackedDataBuffer[indexDDL]->GetHit(iHit); //get hit data
+  for (Int_t iHit = 0; iHit < fPackedDataBuffer[indexDDL].GetEntries(); iHit++){
+    hitData = fPackedDataBuffer[indexDDL].GetHit(iHit); //get hit data
     Int_t   hitACQ = hitData->GetACQ();
     Int_t   hitPS = hitData->GetPS();
     Int_t   hitSlotID = hitData->GetSlotID();
@@ -1463,10 +1480,15 @@ AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
     Int_t   hitChan = hitData->GetChan();
     Int_t   hitTimeBin = hitData->GetTimeBin();
     Int_t   hitTOTBin = hitData->GetTOTBin();
-    
-    Int_t hitLeading = hitData->GetTimeBin()
-      -
-      fCableLengthMap->GetCableTimeShiftBin(indexDDL, hitSlotID, hitChain, hitTDC);//-1; // adc
+
+    if (fgApplyBCCorrections) {
+      /* DDL BC shift time correction */
+      hitTimeBin += fgkddlBCshift[indexDDL];
+      /* deltaBC shift time correction */
+      hitTimeBin += hitData->GetDeltaBunchID();
+    }
+
+    Int_t hitLeading = hitData->GetTimeBin();
     Int_t hitTrailing = -1;
     Int_t hitError = -1;
     
@@ -1480,8 +1502,8 @@ AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
   if (verbose > 0)
     AliInfo("Resetting buffers ...");
 
-  fDataBuffer[indexDDL]->Reset();
-  fPackedDataBuffer[indexDDL]->Reset();
+  fDataBuffer[indexDDL].Reset();
+  fPackedDataBuffer[indexDDL].Reset();
 
   if (verbose > 0)
     AliInfo("Done.");
@@ -1489,3 +1511,739 @@ AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
   return kFALSE;
 }
 
+//---------------------------------------------------------------------------
+void AliTOFRawStream::Geant2EquipmentId(Int_t vol[], Int_t eqId[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- (variable in [0,47])
+  // in:
+  //      nDDL     -eqId[0]- (variable in [0;71]) -> number of the DDL
+  //      nTRM     -eqId[1]- (variable in [3;12]) -> number of the TRM
+  //      nTDC     -eqId[2]- (variable in [0;14]) -> number of the TDC
+  //      nChain   -eqId[3]- (variable in [0; 1]) -> number of the chain
+  //      nChannel -eqId[4]- (variable in [0; 8]) -> number of the channel
+  //
+
+  eqId[0] = Geant2DDL(vol);
+  eqId[1] = Geant2TRM(vol);
+  eqId[2] = Geant2TDC(vol);
+  eqId[3] = Geant2Chain(vol);
+  eqId[4] = Geant2Channel(vol);
+
+}
+
+//---------------------------------------------------------------------------
+Int_t AliTOFRawStream::Geant2DDL(Int_t vol[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- (variable in [0,47])
+  // in:
+  //      nDDL   (variable in [0;71]) -> number of the DDL
+  //
+
+
+  Int_t iDDL = -1;
+
+  if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
+    printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
+    return iDDL;
+  }
+  if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
+    printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
+    return iDDL;
+  }
+  if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
+    printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
+    return iDDL;
+  }
+  if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
+    printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
+  if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
+    printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
+  if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
+    printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
+    return iDDL;
+  }
+
+  Int_t nSector = vol[0];
+  Int_t nPlate  = vol[1];
+  Int_t nStrip  = vol[2];
+  Int_t nPadX   = vol[4];
+
+  if ( nPadX<24 && ( nPlate==0 || nPlate==1 || (nPlate==2 && nStrip<7) ) )
+    iDDL = 0;
+  else if ( nPadX>=24 && ( nPlate==0 || nPlate==1 || (nPlate==2 && nStrip<8) ) )
+    iDDL = 1;
+  else if ( nPadX>=24 && ( nPlate==3 || nPlate==4 || (nPlate==2 && nStrip>7) ) )
+    iDDL = 2;
+  else if ( nPadX<24 && ( nPlate==3 || nPlate==4 || (nPlate==2 && nStrip>6) ) )
+    iDDL = 3;
+
+  return 4*nSector+iDDL;
+
+}
+
+//---------------------------------------------------------------------------
+Int_t AliTOFRawStream::Geant2TRM(Int_t vol[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- (variable in [0,47])
+  // in:
+  //      nTRM   (variable in [3;12]) -> number of the TRM slot
+  //
+
+  Int_t nTRM = -1;
+
+  if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
+    printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
+    return nTRM;
+  }
+  if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
+    printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
+    return nTRM;
+  }
+  if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
+    printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
+    return nTRM;
+  }
+  if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ()) {
+    printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
+    return nTRM;
+  }
+  if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX()) {
+    printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
+    return nTRM;
+  }
+
+  if ( vol[3]>=AliTOFGeometry::NpadZ() )
+    {
+      printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
+      return nTRM;
+    }
+
+  Int_t nPlate  = vol[1];
+  Int_t nStrip  = vol[2];
+
+  Int_t iDDL = Geant2DDL(vol)%4;
+
+  switch (iDDL) {
+
+  case 0:
+
+    if (nPlate==0) {
+      if (nStrip<= 4) nTRM =  4;
+      else if (nStrip> 4 && nStrip<= 9) nTRM =  5;
+      else if (nStrip> 9 && nStrip<=14) nTRM =  6;
+      else if (nStrip>14) nTRM =  7;
+    }
+    else if (nPlate==1) {
+      if (nStrip== 0) nTRM =  7;
+      else if (nStrip> 0 && nStrip<= 5) nTRM =  8;
+      else if (nStrip> 5 && nStrip<=10) nTRM =  9;
+      else if (nStrip>10 && nStrip<=15) nTRM = 10;
+      else if (nStrip>15) nTRM = 11;
+    }
+    else if (nPlate==2) {
+      if (nStrip<= 1) nTRM = 11;
+      else if (nStrip> 1 && nStrip< 7) nTRM = 12;
+    }
+
+    break;
+  case 1:
+
+    if (nPlate==0) {
+      if (nStrip== 0) nTRM =  3;
+      else if (nStrip> 0 && nStrip<= 5) nTRM =  4;
+      else if (nStrip> 5 && nStrip<=10) nTRM =  5;
+      else if (nStrip>10 && nStrip<=15) nTRM =  6;
+      else if (nStrip>15) nTRM =  7;
+    }
+    else if (nPlate==1) {
+      if (nStrip<=1) nTRM = 7;
+      else if (nStrip> 1 && nStrip<= 6) nTRM =  8;
+      else if (nStrip> 6 && nStrip<=11) nTRM =  9;
+      else if (nStrip>11 && nStrip<=16) nTRM = 10;
+      else if (nStrip>16) nTRM = 11;
+    }
+    else if (nPlate==2) {
+      if (nStrip<= 2) nTRM = 11;
+      else if (nStrip> 2 && nStrip<= 7) nTRM = 12;
+    }
+
+    break;
+  case 2:
+
+    if (nPlate==4) {
+      if (nStrip>=14) nTRM =  4;
+      else if (nStrip<14 && nStrip>= 9) nTRM =  5;
+      else if (nStrip< 9 && nStrip>= 4) nTRM =  6;
+      else if (nStrip< 4) nTRM =  7;
+    }
+    else if (nPlate==3) {
+      if (nStrip==18) nTRM =  7;
+      else if (nStrip<18 && nStrip>=13) nTRM =  8;
+      else if (nStrip<13 && nStrip>= 8) nTRM =  9;
+      else if (nStrip< 8 && nStrip>= 3) nTRM = 10;
+      else if (nStrip< 3) nTRM = 11;
+    }
+    else if (nPlate==2) {
+      if (nStrip>=13) nTRM = 11;
+      else if (nStrip<13 && nStrip>= 8) nTRM = 12;
+    }
+
+    break;
+  case 3:
+
+    if (nPlate==4) {
+      if (nStrip==18) nTRM =  3;
+      else if (nStrip<18 && nStrip>=13) nTRM =  4;
+      else if (nStrip<13 && nStrip>= 8) nTRM =  5;
+      else if (nStrip< 8 && nStrip>= 3) nTRM =  6;
+      else if (nStrip< 3) nTRM =  7;
+    }
+    else if (nPlate==3) {
+      if (nStrip>=17) nTRM = 7;
+      else if (nStrip<17 && nStrip>=12) nTRM =  8;
+      else if (nStrip<12 && nStrip>= 7) nTRM =  9;
+      else if (nStrip< 7 && nStrip>= 2) nTRM = 10;
+      else if (nStrip< 2) nTRM = 11;
+    }
+    else if (nPlate==2) {
+      if (nStrip>=12) nTRM = 11;
+      else if (nStrip <12 && nStrip>= 7) nTRM = 12;
+    }
+
+    break;
+
+  }
+
+  return nTRM;
+
+}
+
+//---------------------------------------------------------------------------
+Int_t AliTOFRawStream::Geant2TDC(Int_t vol[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- (variable in [0,47])
+  // in:
+  //      nTDC   (variable in [0;14]) -> number of the TDC
+  //
+
+  Int_t nTDC = -1;
+
+  if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
+    printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
+    return nTDC;
+  }
+  if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
+    printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
+    return nTDC;
+  }
+  if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
+    printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
+    return nTDC;
+  }
+  if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
+    printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
+  if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
+    printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
+  if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
+    printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
+    return nTDC;
+  }
+
+  Int_t nPlate  = vol[1];
+  Int_t nStrip  = vol[2];
+  Int_t iPadX   = vol[4];
+
+  Int_t iDDL = Geant2DDL(vol)%4;
+
+  switch (iDDL) {
+
+  case 0:
+
+    if (nPlate==0) {
+      if (nStrip<= 4) nTDC = (3*(nStrip)+2-(iPadX/4)%3);
+      else if (nStrip> 4 && nStrip<= 9) nTDC = (3*(nStrip- 5)+2-(iPadX/4)%3);
+      else if (nStrip> 9 && nStrip<=14) nTDC = (3*(nStrip-10)+2-(iPadX/4)%3);
+      else if (nStrip>14) nTDC =  (3*(nStrip-15)+2-(iPadX/4)%3);
+    }
+    else if (nPlate==1) {
+      if (nStrip== 0) nTDC =  (3*(nStrip+ 4)+2-(iPadX/4)%3);
+      else if (nStrip> 0 && nStrip<= 5) nTDC = (3*(nStrip- 1)+2-(iPadX/4)%3);
+      else if (nStrip> 5 && nStrip<=10) nTDC = (3*(nStrip- 6)+2-(iPadX/4)%3);
+      else if (nStrip>10 && nStrip<=15) nTDC = (3*(nStrip-11)+2-(iPadX/4)%3);
+      else if (nStrip>15) nTDC = (3*(nStrip-16)+2-(iPadX/4)%3);
+    }
+    else if (nPlate==2) {
+      if (nStrip<= 1) nTDC = (3*(nStrip+ 3)+2-(iPadX/4)%3);
+      else if (nStrip> 1 && nStrip< 7) nTDC = (3*(nStrip- 2)+2-(iPadX/4)%3);
+    }
+
+    break;
+  case 1:
+
+    if (nPlate==0) {
+      if (nStrip== 0) nTDC = (3*(nStrip)+(iPadX/4)%3);
+      else if (nStrip> 0 && nStrip<= 5) nTDC = (3*( 5-nStrip)+(iPadX/4)%3);
+      else if (nStrip> 5 && nStrip<=10) nTDC = (3*(10-nStrip)+(iPadX/4)%3);
+      else if (nStrip>10 && nStrip<=15) nTDC = (3*(15-nStrip)+(iPadX/4)%3);
+      else if (nStrip>15) nTDC = (3*(20-nStrip)+(iPadX/4)%3);
+    }
+    else if (nPlate==1) {
+      if (nStrip<= 1) nTDC = (3*( 1-nStrip)+(iPadX/4)%3);
+      else if (nStrip> 1 && nStrip<= 6) nTDC = (3*( 6-nStrip)+(iPadX/4)%3);
+      else if (nStrip> 6 && nStrip<=11) nTDC = (3*(11-nStrip)+(iPadX/4)%3);
+      else if (nStrip>11 && nStrip<=16) nTDC = (3*(16-nStrip)+(iPadX/4)%3);
+      else if (nStrip>16) nTDC = (3*(21-nStrip)+(iPadX/4)%3);
+    }
+    else if (nPlate==2) {
+      if (nStrip<= 2) nTDC = (3*( 2-nStrip)+(iPadX/4)%3);
+      else if (nStrip> 2 && nStrip<= 7) nTDC = (3*( 7-nStrip)+(iPadX/4)%3);
+    }
+
+    break;
+  case 2:
+
+    if (nPlate==4) {
+      if (nStrip>=14) nTDC = (3*(18-nStrip)+((iPadX/4)%3));
+      else if (nStrip<14 && nStrip>= 9) nTDC = (3*(13-nStrip)+((iPadX/4)%3));
+      else if (nStrip< 9 && nStrip>= 4) nTDC = (3*( 8-nStrip)+((iPadX/4)%3));
+      else if (nStrip< 4) nTDC = (3*( 3-nStrip)+((iPadX/4)%3));
+    }
+    else if (nPlate==3) {
+      if (nStrip==18) nTDC = (3*(22-nStrip)+((iPadX/4)%3));
+      else if (nStrip<18 && nStrip>=13) nTDC = (3*(17-nStrip)+((iPadX/4)%3));
+      else if (nStrip<13 && nStrip>= 8) nTDC = (3*(12-nStrip)+((iPadX/4)%3));
+      else if (nStrip< 8 && nStrip>= 3) nTDC = (3*( 7-nStrip)+((iPadX/4)%3));
+      else if (nStrip< 3) nTDC = (3*( 2-nStrip)+((iPadX/4)%3));
+    }
+    else if (nPlate==2) {
+      if (nStrip>=13) nTDC = (3*(17-nStrip)+((iPadX/4)%3));
+      else if (nStrip<13 && nStrip>= 8) nTDC = (3*(12-nStrip)+((iPadX/4)%3));
+    }
+
+    break;
+  case 3:
+
+    if (nPlate==4) {
+      if (nStrip==18) nTDC = (3*(nStrip-18)+2-(iPadX/4)%3);
+      else if (nStrip<18 && nStrip>=13) nTDC = (3*(nStrip-13)+2-(iPadX/4)%3);
+      else if (nStrip<13 && nStrip>= 8) nTDC = (3*(nStrip- 8)+2-(iPadX/4)%3);
+      else if (nStrip< 8 && nStrip>= 3) nTDC = (3*(nStrip- 3)+2-(iPadX/4)%3);
+      else if (nStrip< 3) nTDC = (3*(nStrip+ 2)+2-(iPadX/4)%3);
+    }
+    else if (nPlate==3) {
+      if (nStrip>=17) nTDC = (3*(nStrip-17)+2-(iPadX/4)%3);
+      else if (nStrip<17 && nStrip>=12) nTDC = (3*(nStrip-12)+2-(iPadX/4)%3);
+      else if (nStrip<12 && nStrip>= 7) nTDC = (3*(nStrip- 7)+2-(iPadX/4)%3);
+      else if (nStrip< 7 && nStrip>= 2) nTDC = (3*(nStrip- 2)+2-(iPadX/4)%3);
+      else if (nStrip< 2) nTDC = (3*(nStrip+ 3)+2-(iPadX/4)%3);
+    }
+    else if (nPlate==2) {
+      if (nStrip>=12) nTDC = (3*(nStrip-12)+2-(iPadX/4)%3);
+      else if (nStrip <12 && nStrip>= 7) nTDC = (3*(nStrip- 7)+2-(iPadX/4)%3);
+    }
+
+    break;
+
+  }
+
+  return nTDC;
+
+}
+
+//---------------------------------------------------------------------------
+Int_t AliTOFRawStream::Geant2Chain(Int_t vol[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- variable in [0,47])
+  // in:
+  //      nChain (variable in [0; 1]) -> number of the TRM chain
+  //
+
+  Int_t nChain = -1;
+
+  if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
+    printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
+    return nChain;
+  }
+  if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
+    printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
+    return nChain;
+  }
+  if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
+    printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
+    return nChain;
+  }
+  if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
+    printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
+  if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
+    printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
+  if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
+    printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
+    return nChain;
+  }
+
+  Int_t iPadX = vol[4];
+
+  if (iPadX<12 || iPadX>=36) nChain = 0;
+  else nChain = 1;
+
+  return nChain;
+
+}
+
+//---------------------------------------------------------------------------
+Int_t AliTOFRawStream::Geant2Channel(Int_t vol[])
+{
+  //
+  // To convert:
+  //      nSector number -vol[0]- (variable in [0,17])
+  //      nPlate  number -vol[1]- (variable in [0, 5])
+  //      nStrip  number -vol[2]- (variable in [0,14/18])
+  //      nPadZ   number -vol[3]- (variable in [0, 1])
+  //      nPadX   number -vol[4]- (variable in [0,47])
+  // in:
+  //      nChannel (variable in [0; 7]) -> number of the TDC channel
+  //
+
+  Int_t nChannel = -1;
+
+  if (vol[0]<0 || vol[0]>=AliTOFGeometry::NSectors()) {
+    printf(" AliTOFRawStream - Error: the sector number (%i) is out of the range: [0,17]\n", vol[0]);
+    return nChannel;
+  }
+  if (vol[1]<0 || vol[1]>=AliTOFGeometry::NPlates()) {
+    printf(" AliTOFRawStream - Error: the module number (%i) is out of the range: [0,4]\n", vol[1]);
+    return nChannel;
+  }
+  if (vol[2]<0 || vol[2]>=AliTOFGeometry::NStrip(vol[1])) {
+    printf(" AliTOFRawStream - Error: the strip number (%i) is out of the range: [0,%i]\n", vol[2], AliTOFGeometry::NStrip(vol[1]));
+    return nChannel;
+  }
+  if (vol[3]<0 || vol[3]>=AliTOFGeometry::NpadZ())
+    printf(" AliTOFRawStream - Error: the padz number (%i) is out of the range: [0,1]\n", vol[3]);
+  if (vol[4]<0 || vol[4]>=AliTOFGeometry::NpadX())
+    printf(" AliTOFRawStream - Error: the padx number (%i) is out of the range: [0,47]\n", vol[4]);
+  if ( vol[3]>=AliTOFGeometry::NpadZ() ) {
+    printf("Maybe you have to invert the order between vol[3](=%i) and vol[4](=%i)\n", vol[3], vol[4]);
+    return nChannel;
+  }
+
+  Int_t iPadZ   = vol[3];
+  Int_t iPadX   = vol[4];
+
+  Int_t iDDL = Geant2DDL(vol)%4;
+
+  switch (iDDL) {
+
+  case 0:
+    nChannel = ((2*(23-iPadX) + (1-iPadZ)))%8;
+    break;
+  case 1:
+    nChannel = ((2*(iPadX-24) + (iPadZ)))%8;
+    break;
+  case 2:
+    nChannel = ((2*(iPadX-24) + (iPadZ)))%8;
+    break;
+  case 3:
+    nChannel = ((2*(23-iPadX) + (1-iPadZ)))%8;
+    break;
+  }
+
+  return nChannel;
+
+}
+
+//____________________________________________________________________________
+void AliTOFRawStream::Raw2Digits(AliRawReader* rawReader, TClonesArray* digitsArray)
+{
+  //
+  // Converts raw data to digits for TOF
+  //
+
+  TStopwatch stopwatch;
+  stopwatch.Start();
+
+  //TClonesArray *fDigits = new TClonesArray("AliTOFdigit", 4000);
+  //digitsTree->Branch("TOF", &fDigits);
+  TClonesArray &aDigits = *digitsArray;
+
+  Int_t inholes = 0;
+
+  Clear();
+  SetRawReader(rawReader);
+
+  //ofstream ftxt;
+  //if (fVerbose==2) ftxt.open("TOFdigitsRead.txt",ios::app);
+
+  TClonesArray staticRawData("AliTOFrawData",10000);
+  staticRawData.Clear();
+  TClonesArray * clonesRawData = &staticRawData;
+
+  Int_t dummy = -1;
+  Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
+  Int_t digit[4];
+
+  const Int_t kMaxNumberOfTracksPerDigit = 3;
+  Int_t tracks[kMaxNumberOfTracksPerDigit];
+  for (Int_t ii=0; ii<kMaxNumberOfTracksPerDigit; ii++)
+    tracks[ii] = -1;
+  Int_t last = -1;
+
+  Int_t indexDDL = 0;
+  Int_t iRawData = 0;
+  AliTOFrawData *tofRawDatum = 0;
+  for (indexDDL=0; indexDDL<AliDAQ::NumberOfDdls("TOF"); indexDDL++) {
+
+    rawReader->Reset();
+    if (fNewDecoderVersion) {
+      AliInfo("Using New Decoder \n"); 
+      LoadRawDataBuffers(indexDDL, 0);
+    }
+    else
+      LoadRawData(indexDDL);
+
+    clonesRawData = GetRawData();
+    if (clonesRawData->GetEntriesFast()!=0) AliInfo(Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
+    for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
+
+      tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
+
+      //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
+      if (tofRawDatum->GetTOF()==-1) continue;
+
+      EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
+                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
+
+      dummy = detectorIndex[3];
+      detectorIndex[3] = detectorIndex[4];//padz
+      detectorIndex[4] = dummy;//padx
+
+      digit[0] = tofRawDatum->GetTOF();
+      digit[1] = tofRawDatum->GetTOT();
+      digit[2] = tofRawDatum->GetTOT();
+      digit[3] = -1;//tofRawDatum->GetTOF(); //tofND
+
+      dummy = detectorIndex[3];
+      detectorIndex[3] = detectorIndex[4];//padx
+      detectorIndex[4] = dummy;//padz
+
+      // Do not reconstruct anything in the holes
+      if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
+       if (detectorIndex[1]==2) { // plate with holes
+         inholes++;
+         continue;
+       }
+      }
+
+      last = digitsArray->GetEntriesFast();
+      new (aDigits[last]) AliTOFdigit(tracks, detectorIndex, digit);
+      /*
+      if (fVerbose==2) {
+       if (indexDDL<10) ftxt << "  " << indexDDL;
+       else         ftxt << " " << indexDDL;
+       if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
+       else         ftxt << " " << tofRawDatum->GetTRM();
+       ftxt << "  " << tofRawDatum->GetTRMchain();
+       if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
+       else         ftxt << " " << tofRawDatum->GetTDC();
+       ftxt << "  " << tofRawDatum->GetTDCchannel();
+
+       if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
+       else              ftxt  << "  -> " << detectorIndex[0];
+       ftxt << "  " << detectorIndex[1];
+       if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
+       else              ftxt << " " << detectorIndex[2];
+       ftxt << "  " << detectorIndex[4];
+       if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
+       else              ftxt << " " << detectorIndex[3];
+
+       if (digit[1]<10)ftxt << "        " << digit[1];
+       else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
+       else ftxt << "      " << digit[1];
+       if (digit[0]<10) ftxt << "      " << digit[0] << endl;
+       else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
+       else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
+       else ftxt << "   " << digit[3] << endl;
+      }
+      */
+      AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
+                      last,
+                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
+                      digit[0], digit[1], digit[3]));
+
+      tofRawDatum = 0;
+    } // loop on tofRawData array
+
+    clonesRawData->Clear();
+
+  } // DDL Loop
+
+  //if (fVerbose==2) ftxt.close();
+
+
+  if (inholes) AliWarning(Form("Raw data in the TOF holes: %d",inholes));
+
+  Int_t nDigits = digitsArray->GetEntries();
+  AliDebug(1, Form("Got %d TOF digits", nDigits));
+  AliDebug(1, Form("Execution time to read TOF raw data and fill TOF digit tree : R:%.2fs C:%.2fs",
+                  stopwatch.RealTime(),stopwatch.CpuTime()));
+
+}
+
+//____________________________________________________________________________
+void AliTOFRawStream::Raw2SDigits(AliRawReader* rawReader, TClonesArray* sdigitsArray)
+{
+  //
+  // Converts raw data to sdigits for TOF
+  //
+
+  TStopwatch stopwatch;
+  stopwatch.Start();
+
+  Int_t inholes = 0;
+
+  //if(!GetLoader()->TreeS()) {MakeTree("S");  MakeBranch("S");}
+  TClonesArray &aSDigits = *sdigitsArray;
+
+  Clear();
+  SetRawReader(rawReader);
+
+  //ofstream ftxt;
+  //if (fVerbose==2) ftxt.open("TOFsdigitsRead.txt",ios::app);
+
+  TClonesArray staticRawData("AliTOFrawData",10000);
+  staticRawData.Clear();
+  TClonesArray * clonesRawData = &staticRawData;
+
+  Int_t dummy = -1;
+  Int_t detectorIndex[5] = {-1, -1, -1, -1, -1};
+  Int_t digit[2];
+  Int_t track = -1;
+  Int_t last = -1;
+
+  Int_t indexDDL = 0;
+  Int_t iRawData = 0;
+  AliTOFrawData *tofRawDatum = 0;
+  for (indexDDL=0; indexDDL<AliDAQ::NumberOfDdls("TOF"); indexDDL++) {
+
+    rawReader->Reset();
+    if (fNewDecoderVersion) {
+      AliInfo("Using New Decoder \n"); 
+      LoadRawDataBuffers(indexDDL, 0);
+    }
+    else
+      LoadRawData(indexDDL);
+
+    clonesRawData = GetRawData();
+    if (clonesRawData->GetEntriesFast()!=0) AliInfo(Form(" TOF raw data number = %3d", clonesRawData->GetEntriesFast()));
+    for (iRawData = 0; iRawData<clonesRawData->GetEntriesFast(); iRawData++) {
+
+      tofRawDatum = (AliTOFrawData*)clonesRawData->UncheckedAt(iRawData);
+
+      //if (tofRawDatum->GetTOT()==-1 || tofRawDatum->GetTOF()==-1) continue;
+      if (tofRawDatum->GetTOF()==-1) continue;
+
+      EquipmentId2VolumeId(indexDDL, tofRawDatum->GetTRM(), tofRawDatum->GetTRMchain(),
+                          tofRawDatum->GetTDC(), tofRawDatum->GetTDCchannel(), detectorIndex);
+
+      dummy = detectorIndex[3];
+      detectorIndex[3] = detectorIndex[4];//padz
+      detectorIndex[4] = dummy;//padx
+
+      digit[0] = tofRawDatum->GetTOF();
+      digit[1] = tofRawDatum->GetTOT();
+
+      dummy = detectorIndex[3];
+      detectorIndex[3] = detectorIndex[4];//padx
+      detectorIndex[4] = dummy;//padz
+
+      // Do not reconstruct anything in the holes
+      if (detectorIndex[0]==13 || detectorIndex[0]==14 || detectorIndex[0]==15 ) { // sectors with holes
+       if (detectorIndex[1]==2) { // plate with holes
+         inholes++;
+         continue;
+       }
+      }
+
+      last = sdigitsArray->GetEntriesFast();
+      new (aSDigits[last]) AliTOFSDigit(track, detectorIndex, digit);
+      /*
+      if (fVerbose==2) {
+       if (indexDDL<10) ftxt << "  " << indexDDL;
+       else         ftxt << " " << indexDDL;
+       if (tofRawDatum->GetTRM()<10) ftxt << "  " << tofRawDatum->GetTRM();
+       else         ftxt << " " << tofRawDatum->GetTRM();
+       ftxt << "  " << tofRawDatum->GetTRMchain();
+       if (tofRawDatum->GetTDC()<10) ftxt << "  " << tofRawDatum->GetTDC();
+       else         ftxt << " " << tofRawDatum->GetTDC();
+       ftxt << "  " << tofRawDatum->GetTDCchannel();
+
+       if (detectorIndex[0]<10) ftxt  << "  ->  " << detectorIndex[0];
+       else              ftxt  << "  -> " << detectorIndex[0];
+       ftxt << "  " << detectorIndex[1];
+       if (detectorIndex[2]<10) ftxt << "  " << detectorIndex[2];
+       else              ftxt << " " << detectorIndex[2];
+       ftxt << "  " << detectorIndex[4];
+       if (detectorIndex[4]<10) ftxt << "  " << detectorIndex[3];
+       else              ftxt << " " << detectorIndex[3];
+
+       if (digit[1]<10)ftxt << "        " << digit[1];
+       else if (digit[1]>=10 && digit[1]<100) ftxt << "      " << digit[1];
+       else ftxt << "      " << digit[1];
+       if (digit[0]<10) ftxt << "      " << digit[0] << endl;
+       else if (digit[0]>=10 && digit[0]<100)   ftxt << "    " << digit[0] << endl;
+       else if (digit[0]>=100 && digit[0]<1000) ftxt << "    " << digit[0] << endl;
+       else ftxt << "   " << digit[3] << endl;
+      }
+      */
+      AliDebug(2, Form(" Raw data reading %2d -> %2d %1d %2d %1d %2d (%d, %d, %d)",
+                      last,
+                      detectorIndex[0], detectorIndex[1], detectorIndex[2], detectorIndex[4], detectorIndex[3],
+                      digit[0], digit[1], digit[3]));
+
+      tofRawDatum = 0;
+    } // while loop
+
+    clonesRawData->Clear();
+
+  } // DDL Loop
+
+  //if (fVerbose==2) ftxt.close();
+
+  if (inholes) AliWarning(Form("Clusters in the TOF holes: %d",inholes));
+
+  Int_t nSDigits = sdigitsArray->GetEntries();
+  AliDebug(1, Form("Got %d TOF sdigits", nSDigits));
+  AliDebug(1, Form("Execution time to read TOF raw data and fill TOF sdigit tree : R:%.2fs C:%.2fs",
+                  stopwatch.RealTime(),stopwatch.CpuTime()));
+
+}