]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFRawStream.cxx
software update to include time corrections during reconstruction.
[u/mrichter/AliRoot.git] / TOF / AliTOFRawStream.cxx
index 0a1c4121a20cfa68500222d69417ccec27917cca..19e0181f8fd8686460f86b3221bf1db05f694a17 100644 (file)
 
 /*
 $Log$
+
+Added lookup tables for
+         TRM number 3 in the left crates (TOF OR signals)
+         and detector elements (A.Silenzi)
+
 Revision 1.19.1  2008/09/19  preghenella
   Decode method updated:
   it reads the CDH from the rawReader and sends it to the decoder;
@@ -102,6 +107,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"
@@ -111,6 +117,8 @@ 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"
@@ -122,31 +130,98 @@ 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
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0,
+  0, 0, 0, 0
+};
+
+const Int_t AliTOFRawStream::fgkStrip0MapCrate0[]=
+  {3,5,1,9,11,7,15,17,13,2,-1,0,7,9,5,13,15,11,0,-1,17,-1,5,3};
+const Int_t AliTOFRawStream::fgkStrip1MapCrate0[]=
+  {4,0,2,10,6,8,16,12,14,3,18,1,8,4,6,14,10,12,1,16,18,2,6,4};
+const Int_t AliTOFRawStream::fgkStrip0MapCrate1[]=
+  {0,2,4,6,8,10,12,14,16,-1,0,2,4,6,8,10,12,14,-1,17,0,2,4,6};
+const Int_t AliTOFRawStream::fgkStrip1MapCrate1[]=
+  {1,3,5,7,9,11,13,15,17,18,1,3,5,7,9,11,13,15,16,18,1,3,5,7};
+const Int_t AliTOFRawStream::fgkStrip0MapCrate2[]=
+  {15,13,17,9,7,11,3,1,5,16,-1,18,11,9,13,5,3,7,14,-1,1,-1,9,11};
+const Int_t AliTOFRawStream::fgkStrip1MapCrate2[]=
+  {14,18,16,8,12,10,2,6,4,15,0,17,10,14,12,4,8,6,13,2,0,12,8,10};
+const Int_t AliTOFRawStream::fgkStrip0MapCrate3[]=
+  {18,16,14,12,10,8,6,4,2,-1,18,16,14,12,10,8,6,4,-1,1,14,12,10,8};
+const Int_t AliTOFRawStream::fgkStrip1MapCrate3[]=
+  {17,15,13,11,9,7,5,3,1,0,17,15,13,11,9,7,5,3,2,0,13,11,9,7};
+
+
+const Int_t AliTOFRawStream::fgkModule0MapCrate0[]=
+  {0,0,0,0,0,0,0,0,0,1,-1,1,1,1,1,1,1,1,2,-1,1,-1,2,2};
+const Int_t AliTOFRawStream::fgkModule1MapCrate0[]=
+  {0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,2,1,1,2,2,2};
+const Int_t AliTOFRawStream::fgkModule0MapCrate1[]=
+  {0,0,0,0,0,0,0,0,0,-1,1,1,1,1,1,1,1,1,-1,1,2,2,2,2,2};
+const Int_t AliTOFRawStream::fgkModule1MapCrate1[]=
+  {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2};
+const Int_t AliTOFRawStream::fgkModule0MapCrate2[]=
+  {4,4,4,4,4,4,4,4,4,3,-1,3,3,3,3,3,3,3,2,-1,3,-1,2,2};
+const Int_t AliTOFRawStream::fgkModule1MapCrate2[]=
+  {4,4,4,4,4,4,4,4,4,3,4,3,3,3,3,3,3,3,2,3,3,2,2,2};
+const Int_t AliTOFRawStream::fgkModule0MapCrate3[]=
+  {4,4,4,4,4,4,4,4,4,-1,3,3,3,3,3,3,3,3,-1,3,2,2,2,2,2};
+const Int_t AliTOFRawStream::fgkModule1MapCrate3[]=
+  {4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2};
+
+const Int_t AliTOFRawStream::fgkChannelMap0[5][19]=
+  {{1,2,2,0,0,1,4,5,5,3,3,4,7,8,8,6,6,7,10},
+   {11,11,9,9,13,14,14,12,12,13,16,17,17,15,15,16,19,20,20},
+   {18,18,21,23,23,22,22,23,23,22,22,21,21,20,20,-1,-1,-1,-1},
+   {19,19,18,17,17,16,16,15,15,14,14,13,13,12,12,11,11,10,10},
+   {9,8,8,7,7,6,6,5,5,4,4,3,3,2,2,1,1,0,0}
+  };
+
+const Int_t AliTOFRawStream::fgkChainMap0[5][19]=
+{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+{0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,-1,-1,-1,-1},
+{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
+{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
+};
+
+const Int_t AliTOFRawStream::fgkChannelMap24[5][19]=
+{{0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9},
+{10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,19,19},
+{20,20,21,21,22,22,23,23,22,22,23,23,21,18,18,-1,-1,-1,-1},
+{20,20,19,16,15,15,17,17,16,13,12,12,14,14,13,9,9,11,11},
+{10,7,6,6,8,8,7,4,3,3,5,5,4,1,0,0,2,2,1}
+};
+
+const Int_t AliTOFRawStream::fgkChainMap24[5][19]=
+{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
+{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
+{1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,-1,-1,-1,-1},
+{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
+{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
 };
 
 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),
@@ -174,15 +249,16 @@ AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
   fInsideTRMchain1(kFALSE),
-  fDataBuffer(),
-  fPackedDataBuffer(),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
   fLocalEventCounterDRM(-1),
   fLocalEventCounterLTM(-1),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fChainBunchID(0x0),
-  fCableLengthMap(0x0),
-  fEventID(0)
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(new AliTOFCableLengthMap()),
+  fEventID(0),
+  fNewDecoderVersion(0)
 {
   //
   // create an object to read TOF raw digits
@@ -193,35 +269,26 @@ AliTOFRawStream::AliTOFRawStream(AliRawReader* rawReader):
     ResetPackedDataBuffer(i);
   }
 
-  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  //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;
+  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;
     }
   }
 
-  fCableLengthMap = new AliTOFCableLengthMap();
-
-  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),
@@ -249,15 +316,16 @@ AliTOFRawStream::AliTOFRawStream():
   fInsideLTM(kFALSE),
   fInsideTRMchain0(kFALSE),
   fInsideTRMchain1(kFALSE),
-  fDataBuffer(),
-  fPackedDataBuffer(),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
   fLocalEventCounterDRM(-1),
   fLocalEventCounterLTM(-1),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fChainBunchID(0x0),
-  fCableLengthMap(0x0),
-  fEventID(0)
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(new AliTOFCableLengthMap()),
+  fEventID(0),
+  fNewDecoderVersion(0)
 {
   //
   // default ctr
@@ -267,24 +335,17 @@ AliTOFRawStream::AliTOFRawStream():
     ResetPackedDataBuffer(i);
   }
 
-  fTOFrawData = new TClonesArray("AliTOFrawData",1000);
+  //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();
-
 }
 
 //_____________________________________________________________________________
@@ -319,15 +380,16 @@ AliTOFRawStream::AliTOFRawStream(const AliTOFRawStream& stream) :
   fInsideLTM(stream.fInsideLTM),
   fInsideTRMchain0(stream.fInsideTRMchain0),
   fInsideTRMchain1(stream.fInsideTRMchain1),
-  fDataBuffer(),
-  fPackedDataBuffer(),
+  //fDataBuffer(),
+  //fPackedDataBuffer(),
   fLocalEventCounterDRM(stream.fLocalEventCounterDRM),
   fLocalEventCounterLTM(stream.fLocalEventCounterLTM),
-  fLocalEventCounterTRM(0x0),
-  fLocalEventCounterChain(0x0),
-  fChainBunchID(0x0),
-  fCableLengthMap(stream.fCableLengthMap),
-  fEventID(stream.fEventID)
+  //fLocalEventCounterTRM(),
+  //fLocalEventCounterChain(),
+  //fChainBunchID(),
+  //fCableLengthMap(stream.fCableLengthMap),
+  fEventID(stream.fEventID),
+  fNewDecoderVersion(stream.fNewDecoderVersion)
 {
   //
   // copy constructor
@@ -413,9 +475,10 @@ AliTOFRawStream& AliTOFRawStream::operator = (const AliTOFRawStream& stream)
     }
   }
 
-  fCableLengthMap = stream.fCableLengthMap;
+  //fCableLengthMap = stream.fCableLengthMap;
 
   fEventID = stream.fEventID;
+  fNewDecoderVersion = stream.fNewDecoderVersion;
 
   return *this;
 
@@ -433,12 +496,7 @@ AliTOFRawStream::~AliTOFRawStream()
   fTOFrawData->Clear();
   delete fTOFrawData;
 
-  delete [] fLocalEventCounterTRM;
-  for (Int_t ii=0; ii<2; ii++) {
-    delete [] fLocalEventCounterChain[ii];
-    delete [] fChainBunchID[ii];
-  }
-  delete fCableLengthMap;
+  //delete fCableLengthMap;
 
 }
 
@@ -451,6 +509,8 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
   // To load raw data
   //
 
+  fEventID = (Int_t)fRawReader->GetBCID(); //bunch crossing
+
   fTOFrawData->Clear();
 
   TClonesArray &arrayTofRawData =  *fTOFrawData;
@@ -458,8 +518,8 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
   fPackedDigits = 0;
 
   // create raw data map
-  AliTOFRawMap *rawMap = new AliTOFRawMap(fTOFrawData);
-  rawMap->Clear();
+  AliTOFRawMap rawMap(fTOFrawData);
+  rawMap.Clear();
 
   Int_t slot[4] = {-1, -1, -1, -1};
 
@@ -468,8 +528,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);
@@ -489,9 +551,9 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
       slot[2] = fTDC;
       slot[3] = fTDCchannel;
 
-      if (rawMap->TestHit(slot) != kEmpty) {
+      if (rawMap.TestHit(slot) != kEmpty) {
 
-       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) {
@@ -506,7 +568,7 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 
            new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
 
-           rawMap->SetHit(slot);
+           rawMap.SetHit(slot);
 
          }
 
@@ -516,16 +578,14 @@ void AliTOFRawStream::LoadRawData(Int_t indexDDL)
 
        new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(fTRM, fTRMchain, fTDC, fTDCchannel, fTime, fToT, fLeadingEdge, fTrailingEdge, fPSbit, fACQ, fErrorFlag);
 
-       rawMap->SetHit(slot);
+       rawMap.SetHit(slot);
 
-      } // else if (rawMap->TestHit(slot) == kEmpty)
+      } // else if (rawMap.TestHit(slot) == kEmpty)
 
     } // if (signal)
 
   } // closed -> while (Next())
 
-  rawMap->Delete();
-
 }
 
 //_____________________________________________________________________________
@@ -758,10 +818,7 @@ Bool_t AliTOFRawStream::Next()
          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);
+         fTime += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
        }
        break;
 
@@ -776,10 +833,7 @@ Bool_t AliTOFRawStream::Next()
          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);
+         fLeadingEdge += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
        }
        break;
 
@@ -794,10 +848,7 @@ Bool_t AliTOFRawStream::Next()
          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);
+         fTrailingEdge += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
        }
        break;
 
@@ -811,10 +862,7 @@ Bool_t AliTOFRawStream::Next()
          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);
+         fTime += fgkddlBCshift[fDDL] * 1024 + (fChainBunchID[fTRM][fTRMchain] - fEventID) * 1024;
        }
        break;
 
@@ -960,7 +1008,7 @@ void AliTOFRawStream::SetPadX()
 Int_t AliTOFRawStream::GetField(UInt_t word, Int_t fieldMask, Int_t fieldPosition) const
 {
   // 
-  // 
+  // Returns 'word' masked by 'fieldMask' and shifted by 'fieldPosition'
   // 
 
   return ((word & fieldMask) >> fieldPosition);
@@ -1182,7 +1230,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!\n";
     //AliWarning("Problems with the padX number!");
   }
   return iPadAlongTheStrip;
@@ -1262,8 +1310,8 @@ 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 nTDC, Int_t iCH,
+                                          Int_t *volume)
 {
   //
   // To convert:
@@ -1281,29 +1329,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())));
@@ -1406,6 +1464,8 @@ Bool_t AliTOFRawStream::Decode(Int_t verbose = 0) {
     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));
+      ResetDataBuffer(currentDDL);
+      ResetPackedDataBuffer(currentDDL);
     }
     
     delete [] data;
@@ -1480,20 +1540,15 @@ AliTOFRawStream::LoadRawDataBuffers(Int_t indexDDL, Int_t verbose)
     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   hitDeltaBC = hitData->GetDeltaBunchID();
+    Int_t   hitL0L1Latency = hitData->GetL0L1Latency();
 
     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);
+    new (arrayTofRawData[fPackedDigits++]) AliTOFrawData(hitSlotID, hitChain, hitTDC, hitChan, hitTimeBin, hitTOTBin, hitLeading, hitTrailing, hitPS, hitACQ, hitError, hitDeltaBC, hitL0L1Latency);
   }
 
   if (verbose > 0)
@@ -1522,11 +1577,11 @@ void AliTOFRawStream::Geant2EquipmentId(Int_t vol[], Int_t eqId[])
   //      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 file 
-  //      nTRM     -eqId[1]- (variable in [3;12]) -> number of the TRM file 
-  //      nTDC     -eqId[2]- (variable in [0;14]) -> number of the TDC file 
-  //      nChain   -eqId[3]- (variable in [0; 1]) -> number of the chain file 
-  //      nChannel -eqId[4]- (variable in [0; 8]) -> number of the channel file 
+  //      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);
@@ -1548,7 +1603,7 @@ Int_t AliTOFRawStream::Geant2DDL(Int_t vol[])
   //      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 file 
+  //      nDDL   (variable in [0;71]) -> number of the DDL
   //
 
 
@@ -1981,3 +2036,373 @@ Int_t AliTOFRawStream::Geant2Channel(Int_t vol[])
   return nChannel;
 
 }
+
+//____________________________________________________________________________
+void AliTOFRawStream::Raw2Digits(AliRawReader* rawReader, TClonesArray * const 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 * const 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()));
+
+}
+
+void AliTOFRawStream::VolumeID2LTM(Int_t detind[],
+                                  Int_t iDDL,
+                                  Int_t iTRM,
+                                  Int_t iChain,
+                                  Int_t iTDC,
+                                  Int_t iChannel) const {
+  //
+  // To convert the TOF trigger macropad ID (i.e. detind)
+  // into TOF OR signals equipment ID (i.e. iDDL, iTRM, iChain, iTDC, iChannel)
+  //
+
+  const Int_t kFirstTDCnumber = 12;
+
+  iDDL=-1, iTRM = 3 , iChain=-1, iTDC=-1, iChannel=-1;
+  if (detind[1]==0 || detind[1]==1 || (detind[1]==2 && detind[2]<=7)) {
+    if (detind[4]<24)
+      iDDL = detind[0]*4;
+    else
+      iDDL = detind[0]*4;
+  }
+  else {
+    if (detind[4]<24)
+      iDDL = detind[0]*4+2;
+    else
+      iDDL = detind[0]*4+2;
+  }
+
+  iChain=fgkChainMap24[detind[1]][detind[2]];
+  iTDC=(Int_t)(fgkChannelMap24[detind[1]][detind[2]]/8)+kFirstTDCnumber;
+  iChannel=fgkChannelMap24[detind[1]][detind[2]]%8;
+  
+}
+
+void AliTOFRawStream::LTM2VolumeID(Int_t iDDL,
+                                  Int_t iTRM,
+                                  Int_t iChain,
+                                  Int_t iTDC,
+                                  Int_t iChannel,
+                                  Int_t detind0[], Int_t detind1[]) const {
+  //
+  // To convert the TOF OR signals equipment ID (i.e. iDDL, iTRM, iChain, iTDC, iChannel)
+  // into TOF trigger macropad IDs (i.e. detind0 and detind1).
+  // In general, a couple of neighbouring TOF semi-strip represents a TOF trigger macropad.
+  //
+
+  const Int_t kFirstTDCnumber = 12;
+
+  Int_t iSector0=-1, iModule0=-1, iStrip0=-1, iPadX0=-1; // Le variabili del Volume ID
+  Int_t iSector1=-1, iModule1=-1, iStrip1=-1, iPadX1=-1; // Le variabili del Volume ID
+
+  if( iDDL%2==1 && iTRM==3 && iTDC-kFirstTDCnumber>=0 && iTDC-kFirstTDCnumber<3 ) {
+    iSector0 = (Int_t)(iDDL/4);
+    iSector1 = (Int_t)(iDDL/4);
+    Int_t iChan= iChannel+(iTDC-kFirstTDCnumber)*8;
+    if(iDDL%4 == 0 || iDDL%4 == 1 ){
+      if(iChain==0){      //CRATE 0
+        iPadX0=0;
+        iPadX1=0;
+        iStrip0=fgkStrip0MapCrate0[iChan];
+        iStrip1=fgkStrip1MapCrate0[iChan];
+        iModule0=fgkModule0MapCrate0[iChan];
+        iModule1=fgkModule1MapCrate0[iChan];
+      }
+      if(iChain==1){// CRATE 1
+        iPadX0=24;
+        iPadX1=24;
+        iStrip0=fgkStrip0MapCrate1[iChan];
+        iStrip1=fgkStrip1MapCrate1[iChan];
+        iModule0=fgkModule0MapCrate1[iChan];
+        iModule1=fgkModule1MapCrate1[iChan];
+      }
+
+    }
+    if(iDDL%4 == 2 || iDDL%4 == 3 ){
+      if(iChain==1){// CRATE 3
+        iPadX0=0;
+        iPadX1=0;
+        iStrip0=fgkStrip0MapCrate3[iChan];
+        iStrip1=fgkStrip1MapCrate3[iChan];
+        iModule0=fgkModule0MapCrate3[iChan];
+        iModule1=fgkModule1MapCrate3[iChan];
+      }
+      if(iChain==0){// CRATE 2
+        iPadX0=24;
+        iPadX1=24;
+        iStrip0=fgkStrip0MapCrate2[iChan];
+        iStrip1=fgkStrip1MapCrate2[iChan];
+        iModule0=fgkModule0MapCrate2[iChan];
+        iModule1=fgkModule1MapCrate2[iChan];
+      }
+    }
+  }
+  detind1[0]=iSector1;
+  detind1[1]=iModule1;
+  detind1[2]=iStrip1;
+  detind1[3]=iPadX1;
+  detind1[4]=0;
+  detind0[0]=iSector0;
+  detind0[1]=iModule0;
+  detind0[2]=iStrip0;
+  detind0[3]=iPadX0;
+  detind0[4]=0;
+}