]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFRawStream.cxx
corrected warnings with gcc 4.3
[u/mrichter/AliRoot.git] / TOF / AliTOFRawStream.cxx
index f66ac08ce8550be6057f361eeac694d772d58237..06d975bea13589a46f475c4e61d165d15747959a 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)
+
+Revision 1.18  2007/05/08 11:53:29  arcelli
+Improved class flexibility for further use (R.Preghenella)
+
+Revision 1.17  2007/05/03 08:53:50  decaro
+Coding convention: RS3 violation -> suppression
+
+Revision 1.16  2007/05/03 08:22:22  decaro
+Coding convention: RN17 violation -> suppression
+
+Revision 1.15  2007/04/30 15:22:06  arcelli
+Change TOF digit Time, Tot etc to int type
+
+Revision 1.14  2007/04/27 11:11:53  arcelli
+updates for the new decoder
+
+Revision 1.13  2007/03/16 11:46:35  decaro
+Coding convention: RN17 rule violation -> suppression
+
+Revision 1.12  2007/02/22 09:43:45  decaro
+Added AliTOFRawStream::GetIndex method for online calibration (C.Zampolli)
+
 Revision 1.11  2007/02/20 15:57:00  decaro
 Raw data update: to read the TOF raw data defined in UNPACKED mode
 
@@ -65,6 +99,8 @@ Revision 0.01  2005/07/22 A. De Caro
 ////////////////////////////////////////////////////////////////////////
 
 
+#include "Riostream.h"
+
 #include "TClonesArray.h"
 
 #include "AliDAQ.h"
@@ -72,320 +108,46 @@ Revision 0.01  2005/07/22 A. De Caro
 #include "AliRawReader.h"
 
 #include "AliTOFGeometry.h"
-#include "AliTOFGeometryV5.h"
 #include "AliTOFrawData.h"
 #include "AliTOFRawMap.h"
 #include "AliTOFRawStream.h"
+//#include "AliTOFCableLengthMap.h"
 
+#include "AliTOFHitData.h"
 
-/******************************************
-GENERAL DATA FORMAT                    
-******************************************/
-
-//filler
-#define FILLER 0x70000000
-
-//word type mask/position
-#define WORD_TYPE_MASK 0xf0000000
-#define WORD_TYPE_POSITION 28
-
-//global header word required bit pattern
-#define GLOBAL_HEADER 0x40000000
-
-//global trailer word required bit pattern
-#define GLOBAL_TRAILER 0x50000000
-
-//error word required bit pattern
-#define ERROR 0x30000000
-
-//header slot ID mask/position
-#define HEADER_SLOT_ID_MASK 0x0000000f
-#define HEADER_SLOT_ID_POSITION 0
-
-//word types
-#define GLOBAL_HEADER_TYPE 4
-#define GLOBAL_TRAILER_TYPE 5
-#define ERROR_TYPE 6
-#define FILLER_TYPE 7
-#define TRM_CHAIN0_HEADER_TYPE 0
-#define TRM_CHAIN0_TRAILER_TYPE 1
-#define TRM_CHAIN1_HEADER_TYPE 2
-#define TRM_CHAIN1_TRAILER_TYPE 3
-
-//slot types
-#define DRM_ID_NUMBER 1
-#define LTM_ID_NUMBER 2
-
-
-/******************************************
-DRM DATA FORMAT                        
-******************************************/
-
-//DRM global header word required bit pattern
-#define DRM_GLOBAL_HEADER 0x40000001
-
-//DRM event words mask/position
-#define DRM_EVENT_WORDS_MASK 0x001ffff0
-#define DRM_EVENT_WORDS_POSITION 4
-
-//DRM DRM ID mask/position
-#define DRM_DRM_ID_MASK 0x0fe00000
-#define DRM_DRM_ID_POSITION 21
-
-//DRM status header 1 word required bit pattern
-#define DRM_STATUS_HEADER_1 0x40000001
-
-//DRM slot ID mask/position
-#define DRM_SLOT_ID_MASK 0x00007ff0
-#define DRM_SLOT_ID_POSITION 4
-
-//DRM C-bit mask/position
-#define DRM_C_BIT_MASK 0x00008000
-#define DRM_C_BIT_POSITION 15
-
-//DRM status header 2 word required bit pattern
-#define DRM_STATUS_HEADER_2 0x40000001
-
-//DRM enable ID mask/position
-#define DRM_ENABLE_ID_MASK 0x00007ff0
-#define DRM_ENABLE_ID_POSITION 4
-
-//DRM fault ID mask/position
-#define DRM_FAULT_ID_MASK 0x07ff0000
-#define DRM_FAULT_ID_POSITION 16
-
-//DRM status header 3 word required bit pattern
-#define DRM_STATUS_HEADER_3 0x40000001
-
-//DRM TTC event counter mask/position
-#define DRM_TTC_EVENT_COUNTER_MASK 0x0ffffff0
-#define DRM_TTC_EVENT_COUNTER_POSITION 4
-
-//DRM event CRC mask/position
-//#define DRM_EVENT_CRC_MASK 0x001ffff0
-#define DRM_EVENT_CRC_MASK 0x000ffff0
-#define DRM_EVENT_CRC_POSITION 4
-
-//DRM global trailer word required bit pattern
-#define DRM_GLOBAL_TRAILER 0x50000001
-
-//DRM local event counter mask/position
-#define DRM_LOCAL_EVENT_COUNTER_MASK 0x0000fff0
-#define DRM_LOCAL_EVENT_COUNTER_POSITION 4
-
-
-/******************************************
-TRM DATA FORMAT                        
-******************************************/
-
-//TRM global header word required bit pattern
-#define TRM_GLOBAL_HEADER 0x40000000
-
-//TRM slot ID mask/position
-#define TRM_SLOT_ID_MASK 0x0000000f
-#define TRM_SLOT_ID_POSITION 0
-
-//TRM event words mask/position
-#define TRM_EVENT_WORDS_MASK 0x0001fff0
-#define TRM_EVENT_WORDS_POSITION 4
-
-//TRM ACQ-bits mask/position
-#define TRM_ACQ_BITS_MASK 0x00060000
-#define TRM_ACQ_BITS_POSITION 17
-
-//TRM L-bit mask/position
-#define TRM_L_BIT_MASK 0x00080000
-#define TRM_L_BIT_POSITION 19
-
-//TRM chain-0 header word required bit pattern
-#define TRM_CHAIN_0_HEADER 0x00000000
-
-//TRM chain-1 header word required bit pattern
-#define TRM_CHAIN_1_HEADER 0x20000000
-
-//TRM bunch ID mask/position
-#define TRM_BUNCH_ID_MASK 0x0000fff0
-#define TRM_BUNCH_ID_POSITION 4
-
-//TRM PB24 temp mask/position
-#define TRM_PB24_TEMP_MASK 0x00ff0000
-#define TRM_PB24_TEMP_POSITION 16
-
-//TRM PB24 ID mask/position
-#define TRM_PB24_ID_MASK 0x07000000
-#define TRM_PB24_ID_POSITION 24
-
-//TRM TS-bit mask/position
-#define TRM_TS_BIT_MASK 0x08000000
-#define TRM_TS_BIT_POSITION 27
-
-//TRM chain-0 trailer word required bit pattern
-#define TRM_CHAIN_0_TRAILER 0x10000000
-
-//TRM chain-1 trailer word required bit pattern
-#define TRM_CHAIN_1_TRAILER 0x30000000
-
-//TRM status mask/position
-#define TRM_STATUS_MASK 0x0000000f
-#define TRM_STATUS_POSITION 0
-
-
-//TDC digit
-
-//TRM TDC digit word required bit pattern
-#define TRM_TDC_DIGIT 0x8000000
-
-//TRM digit time mask/position
-#define TRM_DIGIT_TIME_MASK 0x00001fff
-#define TRM_DIGIT_TIME_POSITION 0
-
-//TRM long digit time mask/position
-#define TRM_LONG_DIGIT_TIME_MASK 0x001fffff
-#define TRM_LONG_DIGIT_TIME_POSITION 0
-
-//TRM TOT width mask/position
-#define TRM_TOT_WIDTH_MASK 0x001fe000
-#define TRM_TOT_WIDTH_POSITION 13
-
-//TRM chan mask/position
-#define TRM_CHAN_MASK 0x00e00000
-#define TRM_CHAN_POSITION 21
-
-//TRM TDC ID mask/position
-#define TRM_TDC_ID_MASK 0x0f000000
-#define TRM_TDC_ID_POSITION 24
-
-//TRM E-bit mask/position
-#define TRM_E_BIT_MASK 0x10000000
-#define TRM_E_BIT_POSITION 28
-
-//TRM PS-bits mask/position
-#define TRM_PS_BITS_MASK 0x60000000
-#define TRM_PS_BITS_POSITION 29
-
-#define TRM_FIRST_SLOT_ID 3
-
-//define hptdc time bin width
-#define TIME_BIN_WIDTH 24.4e-3 //ns
-
-//define hptdc tot bin width
-#define TOT_BIN_WIDTH 48.4e-3 //ns
-
-//TRM errors
-
-//TRM TDC error word required bit pattern
-#define TRM_TDC_ERROR 0x6000000
-
-//TRM TDC diagnostic error word required bit pattern
-#define TRM_TDC_DIAGNOSTIC_ERROR 0x6f00000
-
-//TRM TDC error flags mask/position
-#define TRM_TDC_ERROR_FLAGS_MASK 0x00007fff
-#define TRM_TDC_ERROR_FLAGS_POSITION 0
-
-//TRM TDC error TDC ID mask/position
-#define TRM_TDC_ERROR_TDC_ID_MASK 0x0f00000
-#define TRM_TDC_ERROR_TDC_ID_POSITION 24
-
-//TRM TDC fault chip flag ID mask/position
-#define TRM_TDC_ERROR_FAULT_CHIP_FLAG_ID_MASK 0x00007fff
-#define TRM_TDC_ERROR_FAULT_CHIP_FLAG_ID_POSITION 0
-
-//TRM TDC error C-bit mask/position
-#define TRM_TDC_ERROR_C_BIT_MASK 0x00008000
-#define TRM_TDC_ERROR_C_BIT_POSITION 15
-
-//TRM TDC JTAG error code mask/position
-#define TRM_TDC_ERROR_JTAG_ERROR_CODE_MASK 0x000007ff
-#define TRM_TDC_ERROR_JTAG_ERROR_CODE_POSITION 0
-
-//TRM TDC disgnostic error TDC ID mask/position
-#define TRM_TDC_DIAGNOSTIC_ERROR_TDC_ID_MASK 0x00007800
-#define TRM_TDC_DIAGNOSTIC_ERROR_TDC_ID_POSITION 11 
-
-//TRM global trailer word required bit pattern
-//#define TRM_GLOBAL_TRAILER 0x50000000
-#define TRM_GLOBAL_TRAILER 0x5000000f
-
-//TRM event CRC mask/position
-#define TRM_EVENT_CRC_MASK 0x0000fff0
-#define TRM_EVENT_CRC_POSITION 4
-
-//TRM event counter mask/position
-#define TRM_EVENT_COUNTER_MASK 0x0fff0000
-#define TRM_EVENT_COUNTER_POSITION 16
-
-
-/******************************************
-LTM DATA FORMAT                        
-******************************************/
-
-//LTM global header word required bit pattern
-#define LTM_GLOBAL_HEADER 0x40000002
-
-//LTM event words mask/position
-#define LTM_EVENT_WORDS_MASK 0x0001fff0
-#define LTM_EVENT_WORDS_POSITION 4
-
-//LTM C-bit mask/position
-#define LTM_C_BIT_MASK 0x00020000
-#define LTM_C_BIT_POSITION 17
-
-//LTM fault mask/position
-#define LTM_FAULT_MASK 0x00fc0000
-#define LTM_FAULT_POSITION 18
-
-//PDL data 
-
-//PDL value 1 mask/position
-#define LTM_PDL_VALUE_1_MASK 0x000000ff
-#define LTM_PDL_VALUE_1_POSITION 0
-
-//PDL value 2 mask/position
-#define LTM_PDL_VALUE_2_MASK 0x0000ff00
-#define LTM_PDL_VALUE_2_POSITION 8
-
-//PDL value 3 mask/position
-#define LTM_PDL_VALUE_3_MASK 0x00ff0000
-#define LTM_PDL_VALUE_3_POSITION 16
-
-//PDL value 4 mask/position
-#define LTM_PDL_VALUE_4_MASK 0xff000000
-#define LTM_PDL_VALUE_4_POSITION 24
-
-//ADC data 
-
-//ADC value 1 mask/position
-#define LTM_ADC_VALUE_1_MASK 0x000003ff
-#define LTM_ADC_VALUE_1_POSITION 0
-
-//ADC value 2 mask/position
-#define LTM_ADC_VALUE_2_MASK 0x000ffc00
-#define LTM_ADC_VALUE_2_POSITION 10
-
-//ADC value 3 mask/position
-#define LTM_ADC_VALUE_3_MASK 0x3ff00000
-#define LTM_ADC_VALUE_3_POSITION 20
-
-//LTM global trailer word required bit pattern
-#define LTM_GLOBAL_TRAILER 0x50000002
-
-//LTM event CRC mask/position
-#define LTM_EVENT_CRC_MASK 0x0000fff0
-#define LTM_EVENT_CRC_POSITION 4
-
-//LTM event number mask/position
-#define LTM_EVENT_NUMBER_MASK 0x0fff0000
-#define LTM_EVENT_NUMBER_POSITION 16
-
+#include "AliRawEventHeaderBase.h"
+#include "AliRawDataHeader.h"
 
 ClassImp(AliTOFRawStream)
 
-
+const Int_t AliTOFRawStream::fgkddlBCshift[72] = 
+{
+  2, 2, -1, -1,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2,  0,  0,
+  2, 2, -1, -1,
+  2, 2, -1, -1,
+  2, 2, -2, -2,
+  2, 2, -2, -2,
+  2, 2, -2, -2,
+  2, 2, -1, -1,
+  2, 2, -1, -1,
+  2, 2, -1, -1
+};
+
+Bool_t AliTOFRawStream::fgApplyBCCorrections = kTRUE;
 //_____________________________________________________________________________
 AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fRawReader(rawReader),
-  fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
+  fTOFrawData(0x0),
+  fDecoder(new AliTOFDecoder()),
   fDDL(-1),
   fTRM(-1),
   fTRMchain(-1),
@@ -401,7 +163,6 @@ AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fStrip(-1),
   fPadX(-1),
   fPadZ(-1),
-  fTOFGeometry(new AliTOFGeometryV5()),
   fPackedDigits(0),
   fWordType(-1),
   fSlotID(-1),
@@ -412,21 +173,54 @@ AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fInsideTRM(kFALSE),
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
-  fInsideTRMchain1(kFALSE)
+  fInsideTRMchain1(kFALSE),
+  fLocalEventCounterDRM(-1),
+  fLocalEventCounterLTM(-1),
+  fLocalEventCounterTRM(0x0),
+  fLocalEventCounterChain(0x0),
+  fChainBunchID(0x0),
+  fCableLengthMap(0x0),
+  fEventID(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();
+  }
+
+  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  fTOFrawData->SetOwner();
+
   fRawReader->Reset();
   fRawReader->Select("TOF");
 
+  fLocalEventCounterTRM = new Int_t[13];
+  fLocalEventCounterChain = new Int_t*[13];
+  fChainBunchID = new Int_t*[13];
+  for (Int_t j=0;j<13;j++){
+    fLocalEventCounterTRM[j] = -1;
+    fLocalEventCounterChain[j] = new Int_t[2];
+    fChainBunchID[j] = new Int_t[2];
+    for (Int_t k=0;k<2;k++){
+      fLocalEventCounterChain[j][k] = -1;
+      fChainBunchID[j][k] = -1;
+    }
+  }
+
+  fCableLengthMap = new AliTOFCableLengthMap();
+
+  fEventID = (Int_t)fRawReader->GetBCID(); //bunch crossing
+
 }
 
 //_____________________________________________________________________________
 AliTOFRawStream::AliTOFRawStream():
-  fRawReader(0x0),
-  fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
+  fRawReader(0x0), 
+  fTOFrawData(0x0),
+  fDecoder(new AliTOFDecoder()),
   fDDL(-1),
   fTRM(-1),
   fTRMchain(-1),
@@ -442,7 +236,6 @@ AliTOFRawStream::AliTOFRawStream():
   fStrip(-1),
   fPadX(-1),
   fPadZ(-1),
-  fTOFGeometry(new AliTOFGeometryV5()),
   fPackedDigits(0),
   fWordType(-1),
   fSlotID(-1),
@@ -453,11 +246,40 @@ AliTOFRawStream::AliTOFRawStream():
   fInsideTRM(kFALSE),
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
-  fInsideTRMchain1(kFALSE)
+  fInsideTRMchain1(kFALSE),
+  fLocalEventCounterDRM(-1),
+  fLocalEventCounterLTM(-1),
+  fLocalEventCounterTRM(0x0),
+  fLocalEventCounterChain(0x0),
+  fChainBunchID(0x0),
+  fCableLengthMap(0x0),
+  fEventID(0)
 {
   //
   // default ctr
   //
+  for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){
+    fDataBuffer[i]=new AliTOFHitDataBuffer();
+    fPackedDataBuffer[i]=new AliTOFHitDataBuffer();
+  }
+
+  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  fTOFrawData->SetOwner();
+
+  fLocalEventCounterTRM = new Int_t[13];
+  fLocalEventCounterChain = new Int_t*[13];
+  fChainBunchID = new Int_t*[13];
+  for (Int_t j=0;j<13;j++){
+    fLocalEventCounterTRM[j] = -1;
+    fLocalEventCounterChain[j] = new Int_t[2];
+    fChainBunchID[j] = new Int_t[2];
+    for (Int_t k=0;k<2;k++){
+      fLocalEventCounterChain[j][k] = -1;
+      fChainBunchID[j][k] = -1;
+    }
+  }
+
+  fCableLengthMap = new AliTOFCableLengthMap();
 
 }
 
@@ -465,7 +287,8 @@ AliTOFRawStream::AliTOFRawStream():
 AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   TObject(stream),
   fRawReader(0x0),
-  fTOFrawData(new TClonesArray("AliTOFrawData",1000)),
+  fTOFrawData(0x0),
+  fDecoder(new AliTOFDecoder()),
   fDDL(-1),
   fTRM(-1),
   fTRMchain(-1),
@@ -481,7 +304,6 @@ AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   fStrip(-1),
   fPadX(-1),
   fPadZ(-1),
-  fTOFGeometry(new AliTOFGeometryV5()),
   fPackedDigits(0),
   fWordType(-1),
   fSlotID(-1),
@@ -492,7 +314,14 @@ AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   fInsideTRM(kFALSE),
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
-  fInsideTRMchain1(kFALSE)
+  fInsideTRMchain1(kFALSE),
+  fLocalEventCounterDRM(-1),
+  fLocalEventCounterLTM(-1),
+  fLocalEventCounterTRM(0x0),
+  fLocalEventCounterChain(0x0),
+  fChainBunchID(0x0),
+  fCableLengthMap(0x0),
+  fEventID(0)
 {
   //
   // copy constructor
@@ -520,8 +349,6 @@ AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   fPadX = stream.fPadX;
   fPadZ = stream.fPadZ;
 
-  fTOFGeometry = stream.fTOFGeometry;
-
   fPackedDigits = stream.fPackedDigits;
 
   fWordType = stream.fWordType;
@@ -535,6 +362,27 @@ AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   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]);
+  }
+
+  fTOFrawData = new TClonesArray(*stream.fTOFrawData);
+
+  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;
+
+  fEventID = stream.fEventID;
+
 }
 
 //_____________________________________________________________________________
@@ -565,8 +413,6 @@ AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
   fPadX = stream.fPadX;
   fPadZ = stream.fPadZ;
 
-  fTOFGeometry = stream.fTOFGeometry;
-
   fPackedDigits = stream.fPackedDigits;
 
   fWordType = stream.fWordType;
@@ -579,6 +425,27 @@ AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
   fInsideLTM = stream.fInsideLTM;
   fInsideTRMchain0 = stream.fInsideTRMchain0;
   fInsideTRMchain1 = stream.fInsideTRMchain1;
+  
+  for (Int_t i=0;i<AliDAQ::NumberOfDdls("TOF");i++){ 
+    fDataBuffer[i] = stream.fDataBuffer[i];
+    fPackedDataBuffer[i] = stream.fPackedDataBuffer[i];
+  }
+  
+  fTOFrawData = stream.fTOFrawData;
+
+  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;
+
+  fEventID = stream.fEventID;
 
   return *this;
 
@@ -591,10 +458,23 @@ AliTOFRawStream::~AliTOFRawStream()
 
   fPackedDigits = 0;
 
-  delete fTOFGeometry;
+  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 [] fChainBunchID[ii];
+  }
+  delete fCableLengthMap;
+
 }
 
 
@@ -602,14 +482,14 @@ AliTOFRawStream::~AliTOFRawStream()
 
 void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 {
-
-  /*
-    for (Int_t indexDDL = 0;
-    indexDDL < AliDAQ::NumberOfDdls("TOF");
-    indexDDL++) {
-  */
+  //
+  // To load raw data
+  //
 
   fTOFrawData->Clear();
+
+  TClonesArray &arrayTofRawData =  *fTOFrawData;
+
   fPackedDigits = 0;
 
   // create raw data map
@@ -618,16 +498,26 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 
   Int_t slot[4] = {-1, -1, -1, -1};
 
+  fLocalEventCounterDRM = -1;
+  fLocalEventCounterLTM = -1;
+  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++)
+      fLocalEventCounterChain[ii][jj] = -1;
+
   fRawReader->Reset();
   fRawReader->Select("TOF", indexDDL, indexDDL);
     
   Bool_t signal = kFALSE;
 
+  AliTOFrawData *rawDigit = NULL;
+
   while(Next()) {
 
     signal = (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1);
     if (signal) {
-      //printf("  %2i  %1i  %2i  %1i  %2i  \n", fSector, fPlate, fStrip, fPadZ, fPadX);
+      AliDebug(2,Form("  %2i  %1i  %2i  %1i  %2i", fSector, fPlate, fStrip, fPadZ, fPadX));
 
       slot[0] = fTRM;
       slot[1] = fTRMchain;
@@ -636,7 +526,7 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 
       if (rawMap->TestHit(slot) != kEmpty) {
 
-       AliTOFrawData *rawDigit = static_cast<AliTOFrawData*>(rawMap->GetHit(slot));
+       rawDigit = static_cast<AliTOFrawData*>(rawMap->GetHit(slot));
 
        if (rawDigit->GetLeading()!=-1 && rawDigit->GetTrailing()==-1 &&
            fLeadingEdge==-1 && fTrailingEdge!=-1) {
@@ -646,24 +536,19 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
        else if ( ((rawDigit->GetTOF()!=-1 || rawDigit->GetLeading()!=-1 || rawDigit->GetTrailing()!=-1) &&
                   (fLeadingEdge!=-1 || fTrailingEdge!=-1 || fTime!=-1) )
 
-/*     else if ( ((rawDigit->GetTOF()!=-1 || rawDigit->GetLeading()!=-1 || rawDigit->GetTrailing()!=-1) &&
-                  (fTime!=-1 || fLeadingEdge!=-1 || fTrailingEdge!=-1))*/  /*||
-                 (rawDigit->GetLeading()==-1 && rawDigit->GetTrailing()!=-1 &&
-                 fLeadingEdge!=-1 && fTrailingEdge==-1) */)
+                 )
          {
 
-         TClonesArray &arrayTofRawData =  *fTOFrawData;
-         new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
+           new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
 
-       rawMap->SetHit(slot);
+           rawMap->SetHit(slot);
 
-       }
+         }
 
 
       }
       else {
 
-       TClonesArray &arrayTofRawData =  *fTOFrawData;
        new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
 
        rawMap->SetHit(slot);
@@ -674,32 +559,7 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 
   } // closed -> while (Next())
 
-    /*
-      fDDL  = fRawReader->GetDDLID();
-
-      for (Int_t ii=0; ii<fTOFrawData->GetEntriesFast(); ii++) {
-
-      AliTOFrawData *dummy = (AliTOFrawData*)fTOFrawData->UncheckedAt(ii);
-
-      fTRM = dummy->GetTRM();
-      fTRMchain = dummy->GetTRMchain();
-      fTDC = dummy->GetTDC();
-      fTDCchannel = dummy->GetTDCchannel();
-
-      SetSector();
-      SetPlate();
-      SetStrip();
-      SetPadZ();
-      SetPadX();
-
-      printf("  %2i, %2i %1i, %2i, %1i  -->  %2i, %1i, %2i, %1i, %2i  \n",
-            fDDL, fTRM, fTRMchain, fTDC, fTDCchannel,
-            fSector, fPlate, fStrip, fPadZ, fPadX);
-
-            } // closed loop on TOF raw data TClonesArray
-    */
-
-    //} // closed loop on indexDDL
+  rawMap->Delete();
 
 }
 
@@ -713,6 +573,8 @@ Bool_t AliTOFRawStream::Next()
 
   UInt_t data;
 
+  Int_t dummy = 0;
+
   if (!fRawReader->ReadNextInt(data)) return kFALSE;
 
   if (fSector!=-1 && fPlate!=-1 && fStrip!=-1 && fPadZ!=-1 && fPadX!=-1) {
@@ -781,7 +643,6 @@ Bool_t AliTOFRawStream::Next()
 
   case GLOBAL_TRAILER_TYPE: // global trailer
     fSlotID = GetField(data,HEADER_SLOT_ID_MASK,HEADER_SLOT_ID_POSITION);
-    
 
     switch (fSlotID) { // switch global trailer slot ID
 
@@ -789,6 +650,9 @@ Bool_t AliTOFRawStream::Next()
       if (!fInsideDRM) { // unexpected DRM global trailers -> exit
        break;
       }
+      dummy = 0x0000fff0;
+      //AliInfo(Form("  DRM local event counter = %i", GetField(data,dummy,4)));
+      fLocalEventCounterDRM = GetField(data,dummy,4);
       fInsideDRM = kFALSE; // DRM global trailer accepted
       fInsideTRM = kFALSE;
       fInsideLTM = kFALSE;
@@ -817,12 +681,18 @@ Bool_t AliTOFRawStream::Next()
       if (!fInsideLTM) { // unexpected LTM global trailer -> exit
        break;
       }
+      dummy = 0x0fff0000;
+      //AliInfo(Form("  LTM local event counter = %i", GetField(data,dummy,16)));
+      fLocalEventCounterLTM = GetField(data,dummy,16);
       fInsideLTM = kFALSE; // LTM global trailer accepted
       break;
     case 15: //TRM global trailer
       if (!fInsideTRM) { // unexpected TRM global trailers -> exit
        break;
       }
+      dummy = 0x0fff0000;
+      //AliInfo(Form("  TRM local event counter = %i", GetField(data,dummy,16)));
+      fLocalEventCounterTRM[fTRM] = GetField(data,dummy,16);
       fInsideTRM = kFALSE; // TRM global trailer accepted
       break;
     default: // unexpected global trailer slot ID
@@ -854,11 +724,17 @@ 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
          break;
        }
+        dummy = 0x0fff0000;
+        //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
+        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
        fInsideTRMchain0 = kFALSE;
        fTRMchain = -1;
        break;
@@ -868,11 +744,17 @@ 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
          break;
        }
+        dummy = 0x0fff0000;
+        //AliInfo(Form("  chain local event counter = %i", GetField(data,dummy,16)));
+        fLocalEventCounterChain[fTRM][fTRMchain] = GetField(data,dummy,16);
        fInsideTRMchain1 = kFALSE;
        fTRMchain = -1;
        break;
@@ -903,24 +785,72 @@ 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);
+       fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
+         /*-
+         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);
+       fLeadingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
+         /*-
+         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);
+       fTrailingEdge = GetField(data,TRM_LONG_DIGIT_TIME_MASK,TRM_LONG_DIGIT_TIME_POSITION)
+         /*-
+         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);
+       fTime = GetField(data,TRM_DIGIT_TIME_MASK,TRM_DIGIT_TIME_POSITION)
+         /*-
+         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
@@ -931,7 +861,7 @@ Bool_t AliTOFRawStream::Next()
 
 
   return kTRUE;
-
+  
 }
 //_____________________________________________________________________________
 
@@ -1024,6 +954,9 @@ void AliTOFRawStream::SetPadZ()
        iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
     }
 
+  //iPadZ = Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)%AliTOFGeometry::NpadZ();
+  //iPadZ = Equip2VolNpadZ(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
+
   fPadZ = iPadZ;
 
 }
@@ -1051,6 +984,9 @@ void AliTOFRawStream::SetPadX()
        iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
     }
 
+  //iPadX = (Int_t)(Equip2VolNpad(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel)/(Float_t(AliTOFGeometry::NpadZ())));
+  //iPadX = Equip2VolNpadX(GetDDLnumberPerSector(fDDL), fTRMchain, fTDC, fTDCchannel);
+
   fPadX = iPadX;
 
 }
@@ -1066,7 +1002,7 @@ Int_t AliTOFRawStream::GetField(UInt_t word, Int_t fieldMask, Int_t fieldPositio
 }
 
 //----------------------------------------------------------------------------
-Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC) const 
+Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC)
 {
   //
   // Returns the TOF plate number [0;4]
@@ -1152,7 +1088,7 @@ Int_t AliTOFRawStream::Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC) const
 }
 
 //----------------------------------------------------------------------------
-Int_t AliTOFRawStream::Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC) const 
+Int_t AliTOFRawStream::Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC)
 {
   //
   // Returns the TOF strip number per module:
@@ -1248,7 +1184,7 @@ Int_t AliTOFRawStream::Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC) const
 
 //----------------------------------------------------------------------------
 Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
-                               Int_t iCH) const 
+                                    Int_t iCH)
 {
   //
   // Returns the TOF pad number per strip [0;95]
@@ -1261,10 +1197,17 @@ Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
 
   Int_t iPadAlongTheStrip = -1;
 
-  Int_t iTDClocal = nTDC%3 + (1-iChain)*3;
+  // wrong
+  //Int_t iTDClocal = nTDC%3 + (1-iChain)*3;
+  //if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal;
+  //else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal);
 
-  if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal;
-  else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal);
+  // right
+  Int_t iTDClocal = -1;
+  Int_t iTDClocal03 = nTDC%3 + (1-iChain)*3;
+  Int_t iTDClocal12 = 2-nTDC%3 + iChain*3;
+  if (iDDL==0 || iDDL==3) iTDClocal = 5 - iTDClocal03;
+  else if (iDDL==1 || iDDL==2) iTDClocal = 6 + (5 - iTDClocal12);
 
   Int_t iCHlocal = iCH;
   if (iDDL==0 || iDDL==3) iCHlocal = 7 - iCH;
@@ -1272,13 +1215,53 @@ Int_t AliTOFRawStream::Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC,
   iPadAlongTheStrip = iTDClocal*AliTOFGeometry::NCh() + iCHlocal;
 
   if (((iDDL==1 || iDDL==2) && iPadAlongTheStrip< AliTOFGeometry::NpadX()) ||
-      ((iDDL==0 || iDDL==3) && iPadAlongTheStrip>=AliTOFGeometry::NpadX()))
-    AliError("Problems with the padX number!");
-
+      ((iDDL==0 || iDDL==3) && iPadAlongTheStrip>=AliTOFGeometry::NpadX())) {
+    std::cerr << "Problems with the padX number!" << endl;
+    //AliWarning("Problems with the padX number!");
+  }
   return iPadAlongTheStrip;
 
 }
 
+//----------------------------------------------------------------------------
+Int_t AliTOFRawStream::Equip2VolNpadX(Int_t iDDL, Int_t iChain, Int_t nTDC,
+                                     Int_t iCH)
+{
+  //
+  // Returns the TOF padX number [0;47]
+  // corresponding to the TOF equipment ID numbers:
+  //                          iDDL -> DDL number per sector [0;3]
+  //                        iChain -> TRM chain number [0;1]
+  //                          nTDC -> TDC number [0;14]
+  //                           iCH -> TDC channel number [0;7]
+  //
+
+  Int_t iPadX = (Int_t)(AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)/
+                       (Float_t(AliTOFGeometry::NpadZ())));
+
+  return iPadX;
+
+}
+
+//----------------------------------------------------------------------------
+Int_t AliTOFRawStream::Equip2VolNpadZ(Int_t iDDL, Int_t iChain, Int_t nTDC,
+                                     Int_t iCH)
+{
+  //
+  // Returns the TOF padZ number [0;1]
+  // corresponding to the TOF equipment ID numbers:
+  //                          iDDL -> DDL number per sector [0;3]
+  //                        iChain -> TRM chain number [0;1]
+  //                          nTDC -> TDC number [0;14]
+  //                           iCH -> TDC channel number [0;7]
+  //
+
+  Int_t iPadZ  = AliTOFRawStream::Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
+
+  return iPadZ;
+
+}
+
 //----------------------------------------------------------------------------
 Int_t AliTOFRawStream::GetSectorNumber(Int_t nDDL) const
 {
@@ -1306,6 +1289,11 @@ Int_t AliTOFRawStream::GetDDLnumberPerSector(Int_t nDDL) const
 
 }
 
+//----------------------------------------------------------------------------
+void AliTOFRawStream::EquipmentId2VolumeId(AliTOFHitData *hitData, Int_t *volume) const
+{
+  EquipmentId2VolumeId(hitData->GetDDLID(),hitData->GetSlotID(),hitData->GetChain(),hitData->GetTDC(),hitData->GetChan(),volume);
+}
 //----------------------------------------------------------------------------
 void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
                                        Int_t nTDC, Int_t iCH,
@@ -1332,18 +1320,35 @@ void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
   Int_t iSector = GetSectorNumber(nDDL);
 
   Int_t iPlate = Equip2VolNplate(iDDL, nTRM, nTDC);
-  if (iPlate==-1) AliError("Problems with the plate number!");
+  if (iPlate==-1) {
+    if (fRawReader)
+      fRawReader->AddMajorErrorLog(kPlateError,"plate = -1");
+    AliWarning("Problems with the plate number!");
+  }
 
   Int_t iStrip = Equip2VolNstrip(iDDL, nTRM, nTDC);
-  if (iStrip==-1) AliError("Problems with the strip number!");
+  if (iStrip==-1) {
+    if (fRawReader)
+      fRawReader->AddMajorErrorLog(kStripError,"strip = -1");
+    AliWarning("Problems with the strip number!");
+  }
 
   Int_t iPadAlongTheStrip  = Equip2VolNpad(iDDL, iChain, nTDC, iCH);
-  if (iPadAlongTheStrip==-1)
-    AliError("Problems with the pad number along the strip!");
-
+  if (iPadAlongTheStrip==-1){
+    if (fRawReader)
+      fRawReader->AddMajorErrorLog(kPadAlongStripError,"pad = -1");
+    AliWarning("Problems with the pad number along the strip!");
+  }
+  
   Int_t iPadX  = (Int_t)(iPadAlongTheStrip/(Float_t(AliTOFGeometry::NpadZ())));
   Int_t iPadZ  = iPadAlongTheStrip%AliTOFGeometry::NpadZ();
 
+  //Int_t iPadX  = (Int_t)(Equip2VolNpad(iDDL, iChain, nTDC, iCH)/(Float_t(AliTOFGeometry::NpadZ())));
+  //Int_t iPadZ  = Equip2VolNpad(iDDL, iChain, nTDC, iCH)%AliTOFGeometry::NpadZ();
+
+  //Int_t iPadX  = Equip2VolNpadX(iDDL, iChain, nTDC, iCH);
+  //Int_t iPadZ  = Equip2VolNpadZ(iDDL, iChain, nTDC, iCH);
+
   volume[0] = iSector;
   volume[1] = iPlate;
   volume[2] = iStrip;
@@ -1351,55 +1356,192 @@ void AliTOFRawStream::EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
   volume[4] = iPadZ;
 
 }
-//_____________________________________________________________________________
+//-----------------------------------------------------------------------------
+Bool_t AliTOFRawStream::DecodeDDL(Int_t nDDLMin, Int_t nDDLMax, Int_t verbose = 0) {
+  //
+  // To decode raw data for DDL number in [nDDLmin; nDDLmax]
+  //
+
+  //check and fix valid DDL range
+  if (nDDLMin < 0){
+    nDDLMin = 0;
+    fRawReader->AddMinorErrorLog(kDDLMinError);
+    AliWarning("Wrong DDL range: setting first DDL ID to 0");
+  }
+  if (nDDLMax > 71){
+    nDDLMax = 71;
+    fRawReader->AddMinorErrorLog(kDDLMaxError);
+    AliWarning("Wrong DDL range: setting last DDL ID to 71");
+  }  
+
+  //select required DDLs
+  fRawReader->Select("TOF", nDDLMin, nDDLMax);
+
+  if (verbose)
+    AliInfo(Form("Selected TOF DDL range: %d-%d", nDDLMin, nDDLMax));
+
+  return(Decode(verbose));
+}
+//-----------------------------------------------------------------------------
+Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
+  //
+  // New decoder method
+  //
+
+  Int_t currentEquipment;
+  Int_t currentDDL;
+  const AliRawDataHeader *currentCDH;
+
+  //pointers
+  UChar_t *data = 0x0;
+  
+  //loop and read DDL headers 
+  while(fRawReader->ReadHeader()){
+
+    //memory leak prevention (actually data should be always 0x0 here)
+    if (data != 0x0)
+      delete [] data;
+
+    //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];
+    
+    if (verbose)
+      AliInfo(Form("Found equipment # %d header (DDL # %d): %d bytes (%d words)", currentEquipment, currentDDL, kDataSize, kDataWords));
+    
+    if (verbose)
+      AliInfo(Form("Reading equipment #%d (DDL # %d) data...", currentEquipment, currentDDL));
+    
+    //read equipment payload
+    if (!fRawReader->ReadNext(data, kDataSize))
+      {
+       fRawReader->AddMajorErrorLog(kDDLdataReading);
+       if (verbose)
+         AliWarning("Error while reading DDL data. Go to next equipment");
+       delete [] data;
+       data = 0x0;
+       continue;
+      }
+    
+    if (verbose)
+      AliInfo(Form("Equipment # %d (DDL # %d) data has been readed", currentEquipment, currentDDL));
+    
+    
+    //set up the decoder
+    fDecoder->SetVerbose(verbose);
+    fDecoder->SetDataBuffer(fDataBuffer[currentDDL]);
+    fDecoder->SetPackedDataBuffer(fPackedDataBuffer[currentDDL]);
+    
+    //start decoding
+    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));
+    }
+    
+    delete [] data;
+    data = 0x0;
+  }
+  
+  //reset reader
+  fRawReader->Reset();
 
-Int_t AliTOFRawStream::GetIndex(Int_t *detId)
+  if (verbose)
+    AliInfo("All done");
+    
+  return kFALSE;
+  
+}
+//---------------------------------------------------------------------------
+void
+AliTOFRawStream::ResetBuffers()
 {
-  //Retrieve calibration channel index
-  const Int_t nSectors = fTOFGeometry->NSectors();
-  const Int_t nPlates = fTOFGeometry->NPlates();
-  const Int_t nStripA = fTOFGeometry->NStripA();
-  const Int_t nStripB = fTOFGeometry->NStripB();
-  const Int_t nStripC = fTOFGeometry->NStripC();
-  const Int_t nPadX = fTOFGeometry->NpadX();
-  const Int_t nPadZ = fTOFGeometry->NpadZ();
-
-
-  Int_t isector = detId[0];
-  if (isector >= nSectors)
-    AliError(Form("Wrong sector number in TOF (%d) !",isector));
-  Int_t iplate = detId[1];
-  if (iplate >= nPlates)
-    AliError(Form("Wrong plate number in TOF (%d) !",iplate));
-  Int_t istrip = detId[2];
-  Int_t ipadz = detId[3];
-  Int_t ipadx = detId[4];
-  Int_t stripOffset = 0;
-  switch (iplate) {
-  case 0:
-    stripOffset = 0;
-    break;
-  case 1:
-    stripOffset = nStripC;
-    break;
-  case 2:
-    stripOffset = nStripC+nStripB;
-    break;
-  case 3:
-    stripOffset = nStripC+nStripB+nStripA;
-    break;
-  case 4:
-    stripOffset = nStripC+nStripB+nStripA+nStripB;
-    break;
-  default:
-    AliError(Form("Wrong plate number in TOF (%d) !",iplate));
-    break;
-  };
-
-  Int_t idet = ((2*(nStripC+nStripB)+nStripA)*nPadZ*nPadX)*isector +
-               (stripOffset*nPadZ*nPadX)+
-               (nPadZ*nPadX)*istrip+
-              (nPadX)*ipadz+
-               ipadx;
-  return idet;
+  //
+  // To reset the buffers
+  //
+
+  for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls("TOF"); iDDL++){
+    ResetDataBuffer(iDDL);
+    ResetPackedDataBuffer(iDDL);
+  }
+}
+  
+//---------------------------------------------------------------------------
+Bool_t
+AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
+{
+  //
+  // To load the buffers
+  //
+
+  fTOFrawData->Clear();
+  fPackedDigits = 0;
+  
+  if (verbose > 0)
+    AliInfo(Form("Decoding raw data for DDL # %d ...", indexDDL));
+
+  if (DecodeDDL(indexDDL, indexDDL, verbose) != 0){ //decode DDL
+    fRawReader->AddMajorErrorLog(kDDLDecoder,Form("DDL # = %d",indexDDL));
+    AliWarning(Form("Error while decoding DDL # %d", indexDDL));
+    return kTRUE;
+  }
+  
+  if (verbose > 0)
+    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
+    Int_t   hitACQ = hitData->GetACQ();
+    Int_t   hitPS = hitData->GetPS();
+    Int_t   hitSlotID = hitData->GetSlotID();
+    Int_t   hitChain = hitData->GetChain();
+    Int_t   hitTDC = hitData->GetTDC();
+    Int_t   hitChan = hitData->GetChan();
+    Int_t   hitTimeBin = hitData->GetTimeBin();
+    Int_t   hitTOTBin = hitData->GetTOTBin();
+
+    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;
+    
+    TClonesArray &arrayTofRawData =  *fTOFrawData;
+    new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(hitSlotID, hitChain, hitTDC, hitChan, hitTimeBin, hitTOTBin, hitLeading, hitTrailing, hitPS, hitACQ, hitError);
+  }
+
+  if (verbose > 0)
+    AliInfo("Done.");
+
+  if (verbose > 0)
+    AliInfo("Resetting buffers ...");
+
+  fDataBuffer[indexDDL]->Reset();
+  fPackedDataBuffer[indexDDL]->Reset();
+
+  if (verbose > 0)
+    AliInfo("Done.");
+
+  return kFALSE;
 }
+