]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITSRawStreamSDD.cxx
Bug fix - Jira ticket ALIROOT-5665
[u/mrichter/AliRoot.git] / ITS / AliITSRawStreamSDD.cxx
index 3d1a0fdb62487d84c28f150f1d7b16d877440325..1cf27439a1cfd07fddab35f3860c67073309b644 100644 (file)
@@ -1,5 +1,5 @@
 /**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+ * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
  *                                                                        *
  * Author: The ALICE Off-line Project.                                    *
  * Contributors are mentioned in the code where appropriate.              *
@@ -13,7 +13,7 @@
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/* $Id*/
+/* $Id$*/
 
 ///////////////////////////////////////////////////////////////////////////////
 ///
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "AliITSRawStreamSDD.h"
+#include "AliITSRawStreamSDDCompressed.h"
 #include "AliRawReader.h"
 #include "AliLog.h"
 
 ClassImp(AliITSRawStreamSDD)
-
-const Int_t AliITSRawStreamSDD::fgkDDLModuleMap[kDDLsNumber][kModulesPerDDL] = {
-  {240,241,242,246,247,248,252,253,254,258,259,260},
-  {264,265,266,270,271,272,276,277,278,282,283,284},
-  {288,289,290,294,295,296,300,301,302,306,307,308},
-  {312,313,314,318,319,320,-1,-1,-1,-1,-1,-1},
-  {243,244,245,249,250,251,255,256,257,261,262,263},
-  {267,268,269,273,274,275,279,280,281,285,286,287},
-  {291,292,293,297,298,299,303,304,305,309,310,311},
-  {315,316,317,321,322,323,-1,-1,-1,-1,-1,-1},
-  {324,325,326,327,332,333,334,335,340,341,342,343},
-  {348,349,350,351,356,357,358,359,364,365,366,367},
-  {372,373,374,375,380,381,382,383,388,389,390,391},
-  {396,397,398,399,404,405,406,407,412,413,414,415},
-  {420,421,422,423,428,429,430,431,436,437,438,439},
-  {444,445,446,447,452,453,454,455,460,461,462,463},
-  {468,469,470,471,476,477,478,479,484,485,486,487},
-  {492,493,494,495,-1,-1,-1,-1,-1,-1,-1,-1},
-  {328,329,330,331,336,337,338,339,344,345,346,347},
-  {352,353,354,355,360,361,362,363,368,369,370,371},
-  {376,377,378,379,384,385,386,387,392,393,394,395},
-  {400,401,402,403,408,409,410,411,416,417,418,419},
-  {424,425,426,427,432,433,434,435,440,441,442,443},
-  {448,449,450,451,456,457,458,459,464,465,466,467},
-  {472,473,474,475,480,481,482,483,488,489,490,491},
-  {496,497,498,499,-1,-1,-1,-1,-1,-1,-1,-1}};
   
 const UInt_t AliITSRawStreamSDD::fgkCodeLength[8] =  {8, 18, 2, 3, 4, 5, 6, 7};
 
+//______________________________________________________________________
 AliITSRawStreamSDD::AliITSRawStreamSDD(AliRawReader* rawReader) :
   AliITSRawStream(rawReader),
+fDDLModuleMap(0),
 fData(0),
-fCarlosId(0),
+fResetSkip(kTRUE),
 fEventId(0),
+fCarlosId(-1),
 fChannel(0),
 fJitter(0),
-fNCarlos(kModulesPerDDL),
-fNfifo0(0),
-fNfifo1(0),
-fNfifo2(0),
-fNfifo3(0),
-fDDL(0){
+fEightBitSignal(0),
+fDecompressAmbra(kTRUE)
+{
 // create an object to read ITS SDD raw digits
-  
-  for(Int_t i=0;i<2;i++){
-    for(Int_t ic=0;ic<kModulesPerDDL;ic++){
-      fChannelData[ic][i]=0;
-      fLastBit[ic][i]=0;
-      fChannelCode[ic][i]=0;
-      fReadCode[ic][i]=kFALSE;
-      fReadBits[ic][i]=0;
-      fTimeBin[ic][i]=0;
-      fAnode[ic][i]=0;     
-    }
-    fLowThreshold[i]=0;
+  Reset();
+  for(Int_t im=0;im<kSDDModules;im++){
+    fLowThresholdArray[im][0]=0;
+    fLowThresholdArray[im][1]=0;
   }
-  for(Int_t i=0;i<kDDLsNumber;i++) fSkip[i]=0;
+  for(Int_t i=0;i<kFifoWords;i++) fNfifo[i]=0;
   fRawReader->Reset();
   fRawReader->Select("ITSSDD");
 
-  //  fRawReader->SelectEquipment(17, 101, 101);//select this for test data
-  //  fNCarlos = 8; //select this for test data
+  for(Short_t i=0; i<kCarlosWords; i++) fICarlosWord[i]=0x30000000 + i; // 805306368+i;
+  for(Short_t i=0; i<kFifoWords; i++) fIFifoWord[i]=0x30000010 + i;  // 805306384+i;
 }
 
+//______________________________________________________________________
+AliITSRawStreamSDD::AliITSRawStreamSDD(const AliITSRawStreamSDD& rs) :
+AliITSRawStream(rs.fRawReader),
+fDDLModuleMap(rs.fDDLModuleMap),
+fData(0),
+fResetSkip(kTRUE),
+fEventId(0),
+fCarlosId(-1),
+fChannel(0),
+fJitter(0),
+fEightBitSignal(0),
+fDecompressAmbra(kTRUE)
+{
+  // copy constructor
+  AliError("Copy constructor should not be used.");
+}
+//__________________________________________________________________________
+AliITSRawStreamSDD& AliITSRawStreamSDD::operator=(const AliITSRawStreamSDD& rs) {
+  // assignment operator
+  if (this!=&rs) {}
+  AliError("Assignment opertator should not be used.");
+  return *this;
+}
+
+//______________________________________________________________________
+AliITSRawStreamSDD::~AliITSRawStreamSDD(){
+  if(fDDLModuleMap) delete fDDLModuleMap;
+}
+//______________________________________________________________________
+UChar_t AliITSRawStreamSDD::ReadBlockAttributes(AliRawReader* rawReader){
+  // reads block attribuited from CDH
+  UChar_t *data;
+  rawReader->Reset();
+  rawReader->Select("ITSSDD");
+  do{
+    if(!rawReader->ReadNextData(data)) return 0x0;
+  }while(rawReader->GetDataSize()==0);
+  UChar_t attr=rawReader->GetBlockAttributes();
+  return attr;
+}
+//______________________________________________________________________
+AliITSRawStream* AliITSRawStreamSDD::CreateRawStreamSDD(AliRawReader* rawReader, UChar_t attributes){
+  // instantiates the proper raw stream from block attributes value
+
+  Int_t rawFormat=(attributes&0xE0)>>5;  
+  rawReader->Reset();
+  AliITSRawStream* inputSDD;
+  if(rawFormat==0){
+    inputSDD=new AliITSRawStreamSDDCompressed(rawReader);
+    inputSDD->SetADCEncoded(kTRUE);
+  }else{
+    inputSDD=new AliITSRawStreamSDD(rawReader);
+  }
+  return inputSDD;
+}
+//______________________________________________________________________
+AliITSRawStream* AliITSRawStreamSDD::CreateRawStreamSDD(AliRawReader* rawReader){
+  // reads the data format from CDH and instantiates the proper raw stream
+  UChar_t attr=ReadBlockAttributes(rawReader);
+  return CreateRawStreamSDD(rawReader,attr);
+}
+//______________________________________________________________________
 UInt_t AliITSRawStreamSDD::ReadBits()
 {
 // read bits from the given channel
@@ -100,6 +128,7 @@ UInt_t AliITSRawStreamSDD::ReadBits()
   return result;
 }
 
+//______________________________________________________________________
 Int_t AliITSRawStreamSDD::DecompAmbra(Int_t value) const
 {
   // AMBRA decompression (from 8 to 10 bit)
@@ -107,187 +136,102 @@ Int_t AliITSRawStreamSDD::DecompAmbra(Int_t value) const
   if ((value & 0x80) == 0) {
     return value & 0x7f;
   } else if ((value & 0x40) == 0) {
+    if(value&1) return 0x080 + ((value & 0x3f) << 1);
     return 0x081 + ((value & 0x3f) << 1);
   } else if ((value & 0x20) == 0) {
+    if(value&1) return 0x103 + ((value & 0x1f) << 3);
     return 0x104 + ((value & 0x1f) << 3);
   } else {
+    if(value&1) return 0x207 + ((value & 0x1f) << 4);
     return 0x208 + ((value & 0x1f) << 4);
   }
   
 }
 
+//______________________________________________________________________
 Bool_t AliITSRawStreamSDD::Next()
 {
 // read the next raw digit
 // returns kFALSE if there is no digit left
+// returns kTRUE and fCompletedModule=kFALSE and fCompletedDDL=kFALSE when a digit is found
+// returns kTRUE and fCompletedModule=kTRUE  and fCompletedDDL=kFALSE when a module is completed (=3x3FFFFFFF footer words)
+// returns kTRUE and fCompletedModule=kFALSE and fCompletedDDL=kTRUE  when a DDL is completed (=jitter word)
 
   fPrevModuleID = fModuleID;
-  fDDL=fRawReader->GetDDLID();
-  Int_t ddln = fRawReader->GetDDLID();
-  if(ddln <0) ddln=0;
-  while (fSkip[ddln] < 9) {
-    if (!fRawReader->ReadNextInt(fData))return kFALSE;
-    if ((fData >> 30) == 0x01) continue;  // JTAG word
-    fSkip[ddln]++;
-  }
-  
+  fCompletedModule=kFALSE;
 
-  Int_t countFoot[kModulesPerDDL];
-  for(Int_t i=0;i<kModulesPerDDL;i++) countFoot[i]=0;
-  UInt_t iCarlos0Word=805306368;
-  UInt_t iCarlos1Word=805306369;
-  UInt_t iCarlos2Word=805306370;
-  UInt_t iCarlos3Word=805306371;
-  UInt_t iCarlos4Word=805306372;
-  UInt_t iCarlos5Word=805306373;
-  UInt_t iCarlos6Word=805306374;
-  UInt_t iCarlos7Word=805306375;
-  UInt_t iCarlos8Word=805306376;
-  UInt_t iCarlos9Word=805306377;
-  UInt_t iCarlos10Word=805306378;
-  UInt_t iCarlos11Word=805306379;
-  UInt_t iFifo0Word=805306384;
-  UInt_t iFifo1Word=805306385;
-  UInt_t iFifo2Word=805306386;
-  UInt_t iFifo3Word=805306387;
   while (kTRUE) {
-    if ((fChannel < 0) || (fLastBit[fCarlosId][fChannel] < fReadBits[fCarlosId][fChannel])) {
+    if(fResetSkip){
+      Reset();
+      Bool_t kSkip = SkipHeaderWord();
+      if(!kSkip) return kSkip; 
+      fResetSkip=kFALSE;
+    }
+    if ((fChannel < 0) || (fCarlosId < 0) || (fChannel >= 2) || (fCarlosId >= kModulesPerDDL) || (fLastBit[fCarlosId][fChannel] < fReadBits[fCarlosId][fChannel]) ) {
+      if (!fRawReader->ReadNextInt(fData)) return kFALSE;  // read next word
 
-      if (!fRawReader->ReadNextInt(fData)){ 
-       return kFALSE;  // read next word 
-      }
-      ddln = fRawReader->GetDDLID();
-      if(ddln!=fDDL){
-       Reset();
-       for(Int_t icr=0;icr<kModulesPerDDL;icr++) countFoot[icr]=0;
-      }
-      if(ddln < 0 || ddln > (kDDLsNumber-1)) ddln  = 0;
-      while (fSkip[ddln] < 9) {
-       if (!fRawReader->ReadNextInt(fData)){ 
-         return kFALSE;
-       }
-       if ((fData >> 30) == 0x01) continue;  // JTAG word
-       fSkip[ddln]++;
-      }
 
-      fChannel = -1;
-      if((fData>=iCarlos0Word&&fData<=iCarlos11Word)||(fData>=iFifo0Word&&fData<=iFifo3Word)){
-       if(fData==iCarlos0Word){
-         fCarlosId = 0;
-         fNfifo0 = fCarlosId;
-       }
-       else if (fData==iCarlos1Word){
-         fCarlosId = 1;
-         fNfifo0 = fCarlosId;
-       }
-       else if (fData==iCarlos2Word){
-         fCarlosId = 2;
-         if(fNCarlos == 8) fNfifo1 = fCarlosId;
-         else fNfifo0 = fCarlosId;
-       }
-       else if (fData==iCarlos3Word){
-         fCarlosId = 3;
-         fNfifo1 = fCarlosId;
-       }
-       else if (fData==iCarlos4Word){
-         fCarlosId = 4;
-         if(fNCarlos == 8) fNfifo2 = fCarlosId;
-         else fNfifo1 = fCarlosId;
-       }
-       else if (fData==iCarlos5Word){
-         fCarlosId = 5;
-         if(fNCarlos == 8) fNfifo2 = fCarlosId;
-         else fNfifo1 = fCarlosId;
-       }
-       else if (fData==iCarlos6Word){
-         fCarlosId = 6;
-         if(fNCarlos == 8) fNfifo3 = fCarlosId;
-         else fNfifo2 = fCarlosId;
-       }
-       else if (fData==iCarlos7Word){
-         fCarlosId = 7;
-         if(fNCarlos == 8) fNfifo3 = fCarlosId;
-         else fNfifo2 = fCarlosId;
-       }
-       else if (fData==iCarlos8Word){
-         fCarlosId = 8;
-         fNfifo2 = fCarlosId;
-       }
-       else if (fData==iCarlos9Word){
-         fCarlosId = 9;
-         fNfifo3 = fCarlosId;
-       }
-       else if (fData==iCarlos10Word){
-         fCarlosId = 10;
-         fNfifo3 = fCarlosId;
-       }
-       else if (fData==iCarlos11Word){
-         fCarlosId = 11;
-         fNfifo3 = fCarlosId;
-       }
-       else if (fData==iFifo0Word){
-         fCarlosId = fNfifo0;
-       }
-       else if (fData==iFifo1Word){
-         fCarlosId = fNfifo1;
-       }
-       else if (fData==iFifo2Word){
-         fCarlosId = fNfifo2;
-       }
-       else if (fData==iFifo3Word){
-         fCarlosId = fNfifo3;
-       }
-      }
-      if(fData==1059004191) continue;
-      if (fNCarlos == 8 && (fCarlosId == 8 || fCarlosId == 9 || 
-                           fCarlosId ==10 || fCarlosId == 11))continue;
-      
-      fModuleID = fgkDDLModuleMap[fRawReader->GetDDLID()][fCarlosId];
-      
-      if ((fData >> 28) == 0x02) {           // header
-       fEventId = (fData >> 3) & 0x07FF;
-      } else if ((fData >> 28) == 0x03) {    // footer
-        countFoot[fCarlosId]++; // stop before the last word (last word=jitter)
-       Bool_t exit=kTRUE;
-       for(Int_t ic=0;ic<fNCarlos;ic++){
-         if(countFoot[ic]<3) exit=kFALSE;
-       }
-        if(exit){
-         return kFALSE;
-       }        
-      } else if ((fData >> 29) == 0x00) {    // error
+      if((fData >> 16) == 0x7F00){ // jitter word
+       fJitter = fData&0x000000ff;
+       fResetSkip=kTRUE;
+       fCompletedModule=kFALSE;
+       fCompletedDDL=kTRUE;
+       return kTRUE;
+       }
 
-       if ((fData & 0x00000163) != 0) {         
-         fRawReader->AddMajorErrorLog(kDataError,Form("Error code = %8.8x",fData));     
-         AliWarning(Form("error codes = %8.8x",fData));
-         return kFALSE;          
+      UInt_t nData28= fData >> 28;
+      UInt_t nData30= fData >> 30;
+
+
+      if (nData28== 0x02) {           // header
+       fEventId = (fData >> 3) & 0x07FF; 
+      } else if (nData28== 0x03) {    // Carlos and FIFO words or Footers
+       if(fData>=fICarlosWord[0]&&fData<=fICarlosWord[11]) { // Carlos Word
+         fCarlosId = fData-fICarlosWord[0];
+         Int_t iFifoIdx = fCarlosId/3;
+         fNfifo[iFifoIdx] = fCarlosId;
+       } else if (fData>=fIFifoWord[0]&&fData<=fIFifoWord[3]){ // FIFO word
+         fCarlosId = fNfifo[fData-fIFifoWord[0]];          
+       } else if(fData==0x3FFFFFFF && fCarlosId>=0 && fCarlosId<kModulesPerDDL){ // Carlos footer
+         fICountFoot[fCarlosId]++; // stop before the last word (last word=jitter)
+         if(fICountFoot[fCarlosId]==3){
+           fCompletedModule=kTRUE;
+           fCompletedDDL=kFALSE;
+           return kTRUE;
+         }
+       } else if(fData==0x3F1F1F1F){ // CarlosRX footer
+         // CARLOSRX footer -- do nothing
+       }else{
+         fRawReader->AddMajorErrorLog(kDataError,"Bad footer");
+         AliWarning(Form("Invalid data: bad footer %08X\n", fData));
+         return kFALSE;            
+       }
+      } else if (nData30 == 0x02 || nData30 == 0x03) {
+       fChannel = nData30-2;
+       if(fCarlosId>=0 && fCarlosId<kModulesPerDDL){
+         fChannelData[fCarlosId][fChannel] += 
+           (ULong64_t(fData & 0x3FFFFFFF) << fLastBit[fCarlosId][fChannel]);
+         fLastBit[fCarlosId][fChannel] += 30;
        }
-      } else if ((fData >> 30) == 0x01) {    // JTAG word
-       // ignored
-      } else if ((fData >> 30) == 0x02) {    // channel 0 data
-       
-       fChannel = 0;
-      } else if ((fData >> 30) == 0x03) {    // channel 1 data
-       fChannel = 1;
+      } else if (nData28== 0x04) {
+       // JTAG word -- do nothing
       } else {                               // unknown data format
        fRawReader->AddMajorErrorLog(kDataFormatErr,Form("Invalid data %8.8x",fData));
-       AliWarning(Form("invalid data: %8.8x\n", fData));
+       AliWarning(Form("Invalid data: %08X\n", fData));
        return kFALSE;
       }
       
-      if (fChannel >= 0) {   // add read word to the data
-       fChannelData[fCarlosId][fChannel] += 
-         (ULong64_t(fData & 0x3FFFFFFF) << fLastBit[fCarlosId][fChannel]);
-       fLastBit[fCarlosId][fChannel] += 30;
+      if(fCarlosId>=0 && fCarlosId <kModulesPerDDL){
+       Int_t nDDL=fRawReader->GetDDLID();
+       if(nDDL>=0){
+         fModuleID = GetModuleNumber(nDDL,fCarlosId);
+       }
       }
-
     } else {  // decode data
-
       if (fReadCode[fCarlosId][fChannel]) {// read the next code word
        fChannelCode[fCarlosId][fChannel] = ReadBits();
        fReadCode[fCarlosId][fChannel] = kFALSE;
        fReadBits[fCarlosId][fChannel] = fgkCodeLength[fChannelCode[fCarlosId][fChannel]];
-
       } else {                      // read the next data word
        UInt_t data = ReadBits();
        fReadCode[fCarlosId][fChannel] = kTRUE;
@@ -297,36 +241,47 @@ Bool_t AliITSRawStreamSDD::Next()
        } else if (fChannelCode[fCarlosId][fChannel] == 1) {  // next anode
          fTimeBin[fCarlosId][fChannel] = 0;
          fAnode[fCarlosId][fChannel]++;
-
-       }else {                                   // ADC signal data
-         fSignal = DecompAmbra(data + (1 << fChannelCode[fCarlosId][fChannel]) + fLowThreshold[fChannel]);
+       } else {                                   // ADC signal data
+         fEightBitSignal=data + (1 << fChannelCode[fCarlosId][fChannel]);
+         if(fDecompressAmbra) fSignal = DecompAmbra(fEightBitSignal + fLowThresholdArray[fModuleID-kSPDModules][fChannel]);
          fCoord1 = fAnode[fCarlosId][fChannel];
          fCoord2 = fTimeBin[fCarlosId][fChannel];
          fTimeBin[fCarlosId][fChannel]++;
+         fCompletedModule=kFALSE;
+         fCompletedDDL=kFALSE;
          return kTRUE;
        }
       }
     }
   }
-  return kFALSE;  
-
+  return kFALSE;
 }
 
+//______________________________________________________________________
 void AliITSRawStreamSDD::Reset(){
+
   //reset data member for a new ddl
-  
   for(Int_t i=0;i<2;i++){
     for(Int_t ic=0;ic<kModulesPerDDL;ic++){
       fChannelData[ic][i]=0;
       fLastBit[ic][i]=0;
       fChannelCode[ic][i]=0;
-      fReadCode[ic][i]=kFALSE;
-      fReadBits[ic][i]=0;
+      fReadCode[ic][i]=kTRUE;
+      fReadBits[ic][i]=3;
       fTimeBin[ic][i]=0;
       fAnode[ic][i]=0;     
+      fICountFoot[ic]=0;
     }
-    fLowThreshold[i]=0;
   }
-  fChannel=-1;
-  fDDL=fRawReader->GetDDLID();
 }
+
+//______________________________________________________________________
+Bool_t AliITSRawStreamSDD::SkipHeaderWord(){
+  // skip the 1 DDL header word = 0xffffffff
+  while (kTRUE) {
+    if (!fRawReader->ReadNextInt(fData)) return kFALSE;    
+    if ((fData >> 30) == 0x01) continue;  // JTAG word
+    if(fData==0xFFFFFFFF) return kTRUE;
+  }
+}
+