]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSRawStreamSDD.cxx
Increase speed of SDD raw data decoding (F.Prino)
[u/mrichter/AliRoot.git] / ITS / AliITSRawStreamSDD.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$*/
17
18 ///////////////////////////////////////////////////////////////////////////////
19 ///
20 /// This class provides access to ITS SDD digits in raw data.
21 ///
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "AliITSRawStreamSDD.h"
25 #include "AliRawReader.h"
26 #include "AliLog.h"
27
28 ClassImp(AliITSRawStreamSDD)
29   
30 const UInt_t AliITSRawStreamSDD::fgkCodeLength[8] =  {8, 18, 2, 3, 4, 5, 6, 7};
31
32 //______________________________________________________________________
33 AliITSRawStreamSDD::AliITSRawStreamSDD(AliRawReader* rawReader) :
34   AliITSRawStream(rawReader),
35 fDDLModuleMap(0),
36 fData(0),
37 fEventId(0),
38 fCarlosId(-1),
39 fChannel(0),
40 fJitter(0),
41 fResetSkip(0)
42 {
43 // create an object to read ITS SDD raw digits
44   fDDLModuleMap=new AliITSDDLModuleMapSDD();
45   fDDLModuleMap->SetDefaultMap();
46   Reset();
47   for(Int_t im=0;im<kSDDModules;im++){
48     fLowThresholdArray[im][0]=0;
49     fLowThresholdArray[im][1]=0;
50   }
51   for(Int_t i=0;i<kFifoWords;i++) fNfifo[i]=0;
52   for(Int_t i=0;i<kDDLsNumber;i++) fSkip[i]=0;
53   fRawReader->Reset();
54   fRawReader->Select("ITSSDD");
55
56   for(Short_t i=0; i<kCarlosWords; i++) fICarlosWord[i]=0x30000000 + i; // 805306368+i;
57   for(Short_t i=0; i<kFifoWords; i++) fIFifoWord[i]=0x30000010 + i;  // 805306384+i;
58 }
59
60 //______________________________________________________________________
61 AliITSRawStreamSDD::AliITSRawStreamSDD(const AliITSRawStreamSDD& rs) :
62 AliITSRawStream(rs.fRawReader),
63 fDDLModuleMap(rs.fDDLModuleMap),
64 fData(0),
65 fEventId(0),
66 fCarlosId(-1),
67 fChannel(0),
68 fJitter(0),
69 fResetSkip(0)
70 {
71   // copy constructor
72   AliError("Copy constructor should not be used.");
73 }
74 //__________________________________________________________________________
75 AliITSRawStreamSDD& AliITSRawStreamSDD::operator=(const AliITSRawStreamSDD& rs) {
76   // assignment operator
77   if (this!=&rs) {}
78   AliError("Assignment opertator should not be used.");
79   return *this;
80 }
81
82 //______________________________________________________________________
83 AliITSRawStreamSDD::~AliITSRawStreamSDD(){
84   if(fDDLModuleMap) delete fDDLModuleMap;
85 }
86 //______________________________________________________________________
87 UInt_t AliITSRawStreamSDD::ReadBits()
88 {
89 // read bits from the given channel
90   UInt_t result = (fChannelData[fCarlosId][fChannel] & ((1<<fReadBits[fCarlosId][fChannel]) - 1));
91   fChannelData[fCarlosId][fChannel] >>= fReadBits[fCarlosId][fChannel]; 
92   fLastBit[fCarlosId][fChannel] -= fReadBits[fCarlosId][fChannel];
93   return result;
94 }
95
96 //______________________________________________________________________
97 Int_t AliITSRawStreamSDD::DecompAmbra(Int_t value) const
98 {
99   // AMBRA decompression (from 8 to 10 bit)
100   
101   if ((value & 0x80) == 0) {
102     return value & 0x7f;
103   } else if ((value & 0x40) == 0) {
104     return 0x081 + ((value & 0x3f) << 1);
105   } else if ((value & 0x20) == 0) {
106     return 0x104 + ((value & 0x1f) << 3);
107   } else {
108     return 0x208 + ((value & 0x1f) << 4);
109   }
110   
111 }
112
113 //______________________________________________________________________
114 Bool_t AliITSRawStreamSDD::Next()
115 {
116 // read the next raw digit
117 // returns kFALSE if there is no digit left
118 // returns kTRUE and fCompletedModule=kFALSE when a digit is found
119 // returns kTRUE and fCompletedModule=kTRUE  when a module is completed (=3x3FFFFFFF footer words)
120
121   fPrevModuleID = fModuleID;
122   fCompletedModule=kFALSE;
123
124   while (kTRUE) {
125     if(fResetSkip==0){
126       Bool_t kSkip = SkipHeaderWord();
127       fResetSkip=1;
128       if(!kSkip) return kSkip;
129     }
130   
131     if ((fChannel < 0) || (fCarlosId < 0) || (fChannel >= 2) || (fCarlosId >= kModulesPerDDL) || (fLastBit[fCarlosId][fChannel] < fReadBits[fCarlosId][fChannel]) ) {
132       if (!fRawReader->ReadNextInt(fData)) return kFALSE;  // read next word
133
134
135       fChannel = -1;
136       if((fData >> 16) == 0x7F00){ // jitter word
137         fResetSkip=0;
138         Reset();
139         continue;
140       }
141
142       UInt_t nData28= fData >> 28;
143       UInt_t nData30= fData >> 30;
144
145
146       if (nData28== 0x02) {           // header
147         fEventId = (fData >> 3) & 0x07FF; 
148       } else if (nData28== 0x03) {    // Carlos and FIFO words or Footers
149         if(fData>=fICarlosWord[0]&&fData<=fICarlosWord[11]) { // Carlos Word
150           fCarlosId = fData-fICarlosWord[0];
151           Int_t iFifoIdx = fCarlosId/3;
152           fNfifo[iFifoIdx] = fCarlosId;
153         } else if (fData>=fIFifoWord[0]&&fData<=fIFifoWord[3]){ // FIFO word
154           fCarlosId = fNfifo[fData-fIFifoWord[0]];          
155         } else if(fData==0x3FFFFFFF){ // Carlos footer
156           fICountFoot[fCarlosId]++; // stop before the last word (last word=jitter)
157           if(fICountFoot[fCarlosId]==3){
158             fCompletedModule=kTRUE;
159             return kTRUE;
160           }
161         } else if(fData==0x3F1F1F1F){ // CarlosRX footer
162           // CARLOSRX footer -- do nothing
163         }else{
164           fRawReader->AddMajorErrorLog(kDataError,"Bad footer");
165           AliWarning(Form("Invalid data: bad footer %08X\n", fData));
166           return kFALSE;            
167         }
168       } else if (nData30 == 0x02 || nData30 == 0x03) {
169         fChannel = nData30-2;
170         fChannelData[fCarlosId][fChannel] += 
171           (ULong64_t(fData & 0x3FFFFFFF) << fLastBit[fCarlosId][fChannel]);
172         fLastBit[fCarlosId][fChannel] += 30;
173       } else if (nData28== 0x04) {
174         // JTAG word -- do nothing
175       } else {                               // unknown data format
176         fRawReader->AddMajorErrorLog(kDataFormatErr,Form("Invalid data %8.8x",fData));
177         AliWarning(Form("Invalid data: %8.8x\n", fData));
178         return kFALSE;
179       }
180       
181       if(fCarlosId>=0 && fCarlosId <kModulesPerDDL){
182         Int_t nDDL=fRawReader->GetDDLID();
183          fModuleID = GetModuleNumber(nDDL,fCarlosId);
184       }
185     } else {  // decode data
186       if (fReadCode[fCarlosId][fChannel]) {// read the next code word
187         fChannelCode[fCarlosId][fChannel] = ReadBits();
188         fReadCode[fCarlosId][fChannel] = kFALSE;
189         fReadBits[fCarlosId][fChannel] = fgkCodeLength[fChannelCode[fCarlosId][fChannel]];
190       } else {                      // read the next data word
191         UInt_t data = ReadBits();
192         fReadCode[fCarlosId][fChannel] = kTRUE;
193         fReadBits[fCarlosId][fChannel] = 3;
194         if (fChannelCode[fCarlosId][fChannel] == 0) {         // set the time bin         
195           fTimeBin[fCarlosId][fChannel] = data;
196         } else if (fChannelCode[fCarlosId][fChannel] == 1) {  // next anode
197           fTimeBin[fCarlosId][fChannel] = 0;
198           fAnode[fCarlosId][fChannel]++;
199         } else {                                   // ADC signal data
200           fSignal = DecompAmbra(data + (1 << fChannelCode[fCarlosId][fChannel]) + fLowThresholdArray[fModuleID-kSPDModules][fChannel]);
201           fCoord1 = fAnode[fCarlosId][fChannel];
202           fCoord2 = fTimeBin[fCarlosId][fChannel];
203           fTimeBin[fCarlosId][fChannel]++;
204           //printf("Data read, Module=%d , Anode=%d , Time=%d , Charge=%d\n",fModuleID,fCoord1,fCoord2,fSignal);
205           return kTRUE;
206         }
207       }
208     }
209   }
210   return kFALSE;
211 }
212
213 //______________________________________________________________________
214 void AliITSRawStreamSDD::Reset(){
215
216   //reset data member for a new ddl
217   for(Int_t i=0;i<2;i++){
218     for(Int_t ic=0;ic<kModulesPerDDL;ic++){
219       fChannelData[ic][i]=0;
220       fLastBit[ic][i]=0;
221       fChannelCode[ic][i]=0;
222       fReadCode[ic][i]=kTRUE;
223       fReadBits[ic][i]=3;
224       fTimeBin[ic][i]=0;
225       fAnode[ic][i]=0;     
226     }
227   }
228   for(Int_t i=0;i<kModulesPerDDL;i++) fICountFoot[i]=0;
229 }
230
231 //______________________________________________________________________
232 Bool_t AliITSRawStreamSDD::SkipHeaderWord(){
233   // skip the 1 DDL header word = 0xffffffff
234   while (kTRUE) {
235     if (!fRawReader->ReadNextInt(fData)) return kFALSE;    
236     if ((fData >> 30) == 0x01) continue;  // JTAG word
237     if(fData==0xFFFFFFFF) return kTRUE;
238   }
239 }
240