Bug fix - Jira ticket ALIROOT-5665
[u/mrichter/AliRoot.git] / ITS / AliITSRawStreamSDD.cxx
CommitLineData
2906f4c2 1/**************************************************************************
27639c72 2 * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
2906f4c2 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
ceb607d0 16/* $Id$*/
2906f4c2 17
18///////////////////////////////////////////////////////////////////////////////
19///
20/// This class provides access to ITS SDD digits in raw data.
21///
22///////////////////////////////////////////////////////////////////////////////
23
24#include "AliITSRawStreamSDD.h"
d9ed1779 25#include "AliITSRawStreamSDDCompressed.h"
2906f4c2 26#include "AliRawReader.h"
39a7c5cc 27#include "AliLog.h"
2906f4c2 28
29ClassImp(AliITSRawStreamSDD)
1ba0280f 30
31const UInt_t AliITSRawStreamSDD::fgkCodeLength[8] = {8, 18, 2, 3, 4, 5, 6, 7};
2906f4c2 32
979b5a5f 33//______________________________________________________________________
2906f4c2 34AliITSRawStreamSDD::AliITSRawStreamSDD(AliRawReader* rawReader) :
e56160b8 35 AliITSRawStream(rawReader),
979b5a5f 36fDDLModuleMap(0),
e56160b8 37fData(0),
0cc008b8 38fResetSkip(kTRUE),
a97b3678 39fEventId(0),
14dceddf 40fCarlosId(-1),
e56160b8 41fChannel(0),
a97b3678 42fJitter(0),
6297cc01 43fEightBitSignal(0),
44fDecompressAmbra(kTRUE)
9c8e7d50 45{
2906f4c2 46// create an object to read ITS SDD raw digits
27639c72 47 Reset();
83ec5e27 48 for(Int_t im=0;im<kSDDModules;im++){
49 fLowThresholdArray[im][0]=0;
50 fLowThresholdArray[im][1]=0;
51 }
765a9f95 52 for(Int_t i=0;i<kFifoWords;i++) fNfifo[i]=0;
a97b3678 53 fRawReader->Reset();
362c9d61 54 fRawReader->Select("ITSSDD");
1ba0280f 55
27639c72 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;
1ba0280f 58}
59
979b5a5f 60//______________________________________________________________________
61AliITSRawStreamSDD::AliITSRawStreamSDD(const AliITSRawStreamSDD& rs) :
62AliITSRawStream(rs.fRawReader),
63fDDLModuleMap(rs.fDDLModuleMap),
64fData(0),
0cc008b8 65fResetSkip(kTRUE),
979b5a5f 66fEventId(0),
67fCarlosId(-1),
68fChannel(0),
69fJitter(0),
6297cc01 70fEightBitSignal(0),
71fDecompressAmbra(kTRUE)
979b5a5f 72{
73 // copy constructor
74 AliError("Copy constructor should not be used.");
75}
76//__________________________________________________________________________
77AliITSRawStreamSDD& AliITSRawStreamSDD::operator=(const AliITSRawStreamSDD& rs) {
78 // assignment operator
79 if (this!=&rs) {}
80 AliError("Assignment opertator should not be used.");
81 return *this;
82}
83
84//______________________________________________________________________
85AliITSRawStreamSDD::~AliITSRawStreamSDD(){
86 if(fDDLModuleMap) delete fDDLModuleMap;
87}
88//______________________________________________________________________
d9ed1779 89UChar_t AliITSRawStreamSDD::ReadBlockAttributes(AliRawReader* rawReader){
90 // reads block attribuited from CDH
91 UChar_t *data;
92 rawReader->Reset();
93 rawReader->Select("ITSSDD");
94 do{
95 if(!rawReader->ReadNextData(data)) return 0x0;
96 }while(rawReader->GetDataSize()==0);
97 UChar_t attr=rawReader->GetBlockAttributes();
98 return attr;
99}
100//______________________________________________________________________
101AliITSRawStream* AliITSRawStreamSDD::CreateRawStreamSDD(AliRawReader* rawReader, UChar_t attributes){
102 // instantiates the proper raw stream from block attributes value
103
104 Int_t rawFormat=(attributes&0xE0)>>5;
105 rawReader->Reset();
106 AliITSRawStream* inputSDD;
107 if(rawFormat==0){
108 inputSDD=new AliITSRawStreamSDDCompressed(rawReader);
109 inputSDD->SetADCEncoded(kTRUE);
110 }else{
111 inputSDD=new AliITSRawStreamSDD(rawReader);
112 }
113 return inputSDD;
114}
115//______________________________________________________________________
116AliITSRawStream* AliITSRawStreamSDD::CreateRawStreamSDD(AliRawReader* rawReader){
117 // reads the data format from CDH and instantiates the proper raw stream
118 UChar_t attr=ReadBlockAttributes(rawReader);
119 return CreateRawStreamSDD(rawReader,attr);
120}
121//______________________________________________________________________
1ba0280f 122UInt_t AliITSRawStreamSDD::ReadBits()
123{
124// read bits from the given channel
a97b3678 125 UInt_t result = (fChannelData[fCarlosId][fChannel] & ((1<<fReadBits[fCarlosId][fChannel]) - 1));
126 fChannelData[fCarlosId][fChannel] >>= fReadBits[fCarlosId][fChannel];
127 fLastBit[fCarlosId][fChannel] -= fReadBits[fCarlosId][fChannel];
1ba0280f 128 return result;
2906f4c2 129}
130
979b5a5f 131//______________________________________________________________________
1ba0280f 132Int_t AliITSRawStreamSDD::DecompAmbra(Int_t value) const
133{
a97b3678 134 // AMBRA decompression (from 8 to 10 bit)
135
1ba0280f 136 if ((value & 0x80) == 0) {
137 return value & 0x7f;
138 } else if ((value & 0x40) == 0) {
0b4ad9b0 139 if(value&1) return 0x080 + ((value & 0x3f) << 1);
1ba0280f 140 return 0x081 + ((value & 0x3f) << 1);
141 } else if ((value & 0x20) == 0) {
0b4ad9b0 142 if(value&1) return 0x103 + ((value & 0x1f) << 3);
1ba0280f 143 return 0x104 + ((value & 0x1f) << 3);
144 } else {
0b4ad9b0 145 if(value&1) return 0x207 + ((value & 0x1f) << 4);
1ba0280f 146 return 0x208 + ((value & 0x1f) << 4);
147 }
a97b3678 148
1ba0280f 149}
2906f4c2 150
979b5a5f 151//______________________________________________________________________
2906f4c2 152Bool_t AliITSRawStreamSDD::Next()
153{
154// read the next raw digit
155// returns kFALSE if there is no digit left
de075dae 156// returns kTRUE and fCompletedModule=kFALSE and fCompletedDDL=kFALSE when a digit is found
157// returns kTRUE and fCompletedModule=kTRUE and fCompletedDDL=kFALSE when a module is completed (=3x3FFFFFFF footer words)
158// returns kTRUE and fCompletedModule=kFALSE and fCompletedDDL=kTRUE when a DDL is completed (=jitter word)
5dfa68c5 159
2906f4c2 160 fPrevModuleID = fModuleID;
5dfa68c5 161 fCompletedModule=kFALSE;
a97b3678 162
a97b3678 163 while (kTRUE) {
0cc008b8 164 if(fResetSkip){
165 Reset();
166 Bool_t kSkip = SkipHeaderWord();
167 if(!kSkip) return kSkip;
168 fResetSkip=kFALSE;
169 }
8345a1cf 170 if ((fChannel < 0) || (fCarlosId < 0) || (fChannel >= 2) || (fCarlosId >= kModulesPerDDL) || (fLastBit[fCarlosId][fChannel] < fReadBits[fCarlosId][fChannel]) ) {
14dceddf 171 if (!fRawReader->ReadNextInt(fData)) return kFALSE; // read next word
70881050 172
5dfa68c5 173
c3ad350c 174 if((fData >> 16) == 0x7F00){ // jitter word
de075dae 175 fJitter = fData&0x000000ff;
0cc008b8 176 fResetSkip=kTRUE;
de075dae 177 fCompletedModule=kFALSE;
178 fCompletedDDL=kTRUE;
179 return kTRUE;
180 }
5dfa68c5 181
14dceddf 182 UInt_t nData28= fData >> 28;
183 UInt_t nData30= fData >> 30;
184
185
186 if (nData28== 0x02) { // header
187 fEventId = (fData >> 3) & 0x07FF;
188 } else if (nData28== 0x03) { // Carlos and FIFO words or Footers
189 if(fData>=fICarlosWord[0]&&fData<=fICarlosWord[11]) { // Carlos Word
14dceddf 190 fCarlosId = fData-fICarlosWord[0];
191 Int_t iFifoIdx = fCarlosId/3;
14dceddf 192 fNfifo[iFifoIdx] = fCarlosId;
193 } else if (fData>=fIFifoWord[0]&&fData<=fIFifoWord[3]){ // FIFO word
14dceddf 194 fCarlosId = fNfifo[fData-fIFifoWord[0]];
4d350ec2 195 } else if(fData==0x3FFFFFFF && fCarlosId>=0 && fCarlosId<kModulesPerDDL){ // Carlos footer
fffa954f 196 fICountFoot[fCarlosId]++; // stop before the last word (last word=jitter)
197 if(fICountFoot[fCarlosId]==3){
198 fCompletedModule=kTRUE;
de075dae 199 fCompletedDDL=kFALSE;
fffa954f 200 return kTRUE;
9c8e7d50 201 }
14dceddf 202 } else if(fData==0x3F1F1F1F){ // CarlosRX footer
70881050 203 // CARLOSRX footer -- do nothing
14dceddf 204 }else{
70881050 205 fRawReader->AddMajorErrorLog(kDataError,"Bad footer");
fd2d1992 206 AliWarning(Form("Invalid data: bad footer %08X\n", fData));
14dceddf 207 return kFALSE;
9c8e7d50 208 }
14dceddf 209 } else if (nData30 == 0x02 || nData30 == 0x03) {
210 fChannel = nData30-2;
7a8c5708 211 if(fCarlosId>=0 && fCarlosId<kModulesPerDDL){
212 fChannelData[fCarlosId][fChannel] +=
213 (ULong64_t(fData & 0x3FFFFFFF) << fLastBit[fCarlosId][fChannel]);
214 fLastBit[fCarlosId][fChannel] += 30;
215 }
70881050 216 } else if (nData28== 0x04) {
217 // JTAG word -- do nothing
a97b3678 218 } else { // unknown data format
9c8e7d50 219 fRawReader->AddMajorErrorLog(kDataFormatErr,Form("Invalid data %8.8x",fData));
b24ea968 220 AliWarning(Form("Invalid data: %08X\n", fData));
9c8e7d50 221 return kFALSE;
a97b3678 222 }
223
8345a1cf 224 if(fCarlosId>=0 && fCarlosId <kModulesPerDDL){
e2fc1bc8 225 Int_t nDDL=fRawReader->GetDDLID();
4d350ec2 226 if(nDDL>=0){
227 fModuleID = GetModuleNumber(nDDL,fCarlosId);
228 }
8345a1cf 229 }
a97b3678 230 } else { // decode data
a97b3678 231 if (fReadCode[fCarlosId][fChannel]) {// read the next code word
9c8e7d50 232 fChannelCode[fCarlosId][fChannel] = ReadBits();
233 fReadCode[fCarlosId][fChannel] = kFALSE;
234 fReadBits[fCarlosId][fChannel] = fgkCodeLength[fChannelCode[fCarlosId][fChannel]];
a97b3678 235 } else { // read the next data word
9c8e7d50 236 UInt_t data = ReadBits();
237 fReadCode[fCarlosId][fChannel] = kTRUE;
238 fReadBits[fCarlosId][fChannel] = 3;
239 if (fChannelCode[fCarlosId][fChannel] == 0) { // set the time bin
240 fTimeBin[fCarlosId][fChannel] = data;
241 } else if (fChannelCode[fCarlosId][fChannel] == 1) { // next anode
242 fTimeBin[fCarlosId][fChannel] = 0;
243 fAnode[fCarlosId][fChannel]++;
244 } else { // ADC signal data
6297cc01 245 fEightBitSignal=data + (1 << fChannelCode[fCarlosId][fChannel]);
246 if(fDecompressAmbra) fSignal = DecompAmbra(fEightBitSignal + fLowThresholdArray[fModuleID-kSPDModules][fChannel]);
9c8e7d50 247 fCoord1 = fAnode[fCarlosId][fChannel];
248 fCoord2 = fTimeBin[fCarlosId][fChannel];
249 fTimeBin[fCarlosId][fChannel]++;
de075dae 250 fCompletedModule=kFALSE;
251 fCompletedDDL=kFALSE;
9c8e7d50 252 return kTRUE;
253 }
a97b3678 254 }
255 }
256 }
14dceddf 257 return kFALSE;
2906f4c2 258}
1ba0280f 259
979b5a5f 260//______________________________________________________________________
a97b3678 261void AliITSRawStreamSDD::Reset(){
765a9f95 262
a97b3678 263 //reset data member for a new ddl
a97b3678 264 for(Int_t i=0;i<2;i++){
265 for(Int_t ic=0;ic<kModulesPerDDL;ic++){
266 fChannelData[ic][i]=0;
267 fLastBit[ic][i]=0;
268 fChannelCode[ic][i]=0;
14dceddf 269 fReadCode[ic][i]=kTRUE;
270 fReadBits[ic][i]=3;
a97b3678 271 fTimeBin[ic][i]=0;
272 fAnode[ic][i]=0;
b24ea968 273 fICountFoot[ic]=0;
a97b3678 274 }
a97b3678 275 }
a97b3678 276}
765a9f95 277
979b5a5f 278//______________________________________________________________________
83ec5e27 279Bool_t AliITSRawStreamSDD::SkipHeaderWord(){
8345a1cf 280 // skip the 1 DDL header word = 0xffffffff
83ec5e27 281 while (kTRUE) {
282 if (!fRawReader->ReadNextInt(fData)) return kFALSE;
765a9f95 283 if ((fData >> 30) == 0x01) continue; // JTAG word
83ec5e27 284 if(fData==0xFFFFFFFF) return kTRUE;
765a9f95 285 }
765a9f95 286}
287