Crrected versions of AliAltroBuffer, AliAltroMapping and AliTPCRawStream
[u/mrichter/AliRoot.git] / RAW / AliTPCRawStream.cxx
CommitLineData
f8aae377 1/**************************************************************************
2 * Copyright(c) 1998-1999, 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
42d20574 16/* $Id$ */
17
f8aae377 18///////////////////////////////////////////////////////////////////////////////
bea6b2a4 19///
20/// This class provides access to TPC digits in raw data.
21///
22/// It loops over all TPC digits in the raw data given by the AliRawReader.
23/// The Next method goes to the next digit. If there are no digits left
24/// it returns kFALSE.
25/// Several getters provide information about the current digit.
26///
f8aae377 27///////////////////////////////////////////////////////////////////////////////
28
cc934096 29#include <TSystem.h>
30
f8aae377 31#include "AliTPCRawStream.h"
bea6b2a4 32#include "AliTPCHNode.h"
42d20574 33#include "AliRawReader.h"
cc934096 34#include "AliLog.h"
35#include "AliTPCAltroMapping.h"
f8aae377 36
37ClassImp(AliTPCRawStream)
38
39
40AliTPCHNode** AliTPCRawStream::fgRootNode = NULL;
95e50ecd 41UInt_t** AliTPCRawStream::fgLUTDimension = NULL;
42AliTPCHNode***AliTPCRawStream::fgLUTNode = NULL;
f8aae377 43
44
45AliTPCRawStream::AliTPCRawStream(AliRawReader* rawReader)
46{
47// create an object to read TPC raw digits
48
49 fRawReader = rawReader;
50 fRawReader->Select(0);
42d20574 51 fData = new UShort_t[fgkDataMax];
f8aae377 52 fDataSize = fPosition = 0;
53 fCount = fBunchLength = 0;
54
55 if (!fgRootNode) {
42d20574 56 fgRootNode = new AliTPCHNode*[fgkNumTables];
95e50ecd 57 fgLUTDimension = new UInt_t*[fgkNumTables];
58 fgLUTNode = new AliTPCHNode**[fgkNumTables];
42d20574 59 fCompression.CreateTreesFromFile(fgRootNode, fgkNumTables);
95e50ecd 60 fCompression.CreateLUTsFromTrees(fgRootNode, fgkNumTables, fgLUTDimension, fgLUTNode);
f8aae377 61 }
62
63 fSector = fPrevSector = fRow = fPrevRow = fPad = fPrevPad = fTime = fSignal = -1;
cc934096 64
65 TString path = gSystem->Getenv("ALICE_ROOT");
66 path += "/TPC/mapping/Patch";
67 TString path2;
68 for(Int_t i = 0; i < 6; i++) {
69 path2 = path;
70 path2 += i;
71 path2 += ".data";
72 fMapping[i] = new AliTPCAltroMapping(path2.Data());
73 }
f8aae377 74}
75
42d20574 76AliTPCRawStream::AliTPCRawStream(const AliTPCRawStream& stream) :
77 TObject(stream)
78{
79 Fatal("AliTPCRawStream", "copy constructor not implemented");
80}
81
82AliTPCRawStream& AliTPCRawStream::operator = (const AliTPCRawStream&
83 /* stream */)
84{
85 Fatal("operator =", "assignment operator not implemented");
86 return *this;
87}
88
f8aae377 89AliTPCRawStream::~AliTPCRawStream()
90{
91// clean up
92
93 delete[] fData;
cc934096 94
95 for(Int_t i = 0; i < 6; i++) delete fMapping[i];
f8aae377 96}
97
a8ffd46b 98void AliTPCRawStream::Reset()
99{
100// reset tpc raw stream params
101
102 fDataSize = fPosition = 0;
103 fCount = fBunchLength = 0;
104
105 fSector = fPrevSector = fRow = fPrevRow = fPad = fPrevPad = fTime = fSignal = -1;
106}
f8aae377 107
108Bool_t AliTPCRawStream::Next()
109{
110// read the next raw digit
111// returns kFALSE if there is no digit left
112
113 fPrevSector = fSector;
114 fPrevRow = fRow;
115 fPrevPad = fPad;
116
117 while (fCount == 0) { // next trailer
118 if (fPosition >= fDataSize) { // next payload
119 UChar_t* data;
120 do {
121 if (!fRawReader->ReadNextData(data)) return kFALSE;
122 } while (fRawReader->GetDataSize() == 0);
123
124 if (fRawReader->IsCompressed()) { // compressed data
0b3c7dfc 125 UInt_t size = 0;
95e50ecd 126 // fCompression.Decompress(fgRootNode, fgkNumTables,
127 // (char*) data, fRawReader->GetDataSize(),
128 // fData, size);
129 fCompression.DecompressWithLUTs(fgRootNode, fgLUTDimension, fgLUTNode, fgkNumTables,
130 (char*) data, fRawReader->GetDataSize(),
131 fData, size);
f8aae377 132 fDataSize = size;
133
134 } else { // uncompressed data
135 fDataSize = 0;
136 Int_t pos = (fRawReader->GetDataSize() * 8) / 10;
137 while (Get10BitWord(data, pos-1) == 0x2AA) pos--;
cc934096 138 pos++;
f8aae377 139 while (pos > 0) {
140 for (Int_t i = 0; i < 4; i++) { // copy trailer
141 fData[fDataSize++] = Get10BitWord(data, pos-4+i);
142 }
143 pos -= 4;
cc934096 144
145 Int_t count = (fData[fDataSize-2] << 4) & 0x3FF;
146 count |= ((fData[fDataSize-3] & 0x3FF) >> 6);
147 // Int_t count = fData[fDataSize-4];
f8aae377 148 pos -= (4 - (count % 4)) % 4; // skip fill words
149
150 while (count > 0) {
151 UShort_t bunchLength = Get10BitWord(data, pos-1);
152 fData[fDataSize++] = bunchLength;
153 fData[fDataSize++] = Get10BitWord(data, pos-2); // time bin
154
155 // copy signal amplitudes in increasing order on time
156 for (Int_t i = 0; i < bunchLength-2; i++) {
157 fData[fDataSize++] = Get10BitWord(data, pos - bunchLength + i);
158 }
159 pos -= bunchLength;
160 count -= bunchLength;
161 }
162 }
163 }
164
165 fPosition = 0;
166 }
167 if (fPosition + 4 >= fDataSize) {
168 Error("Next", "could not read trailer");
169 return kFALSE;
170 }
cc934096 171
172 Short_t temp = fData[fPosition++];
173 Short_t hwAdress = temp & 0x3FF;
174
175 temp = fData[fPosition++];
176 hwAdress |= (temp & 0x3) << 10;
177 if (((temp >> 2) & 0xF) != 0xA)
178 AliFatal(Form("Incorrect trailer found ! Expecting second 0xA but found %x !",(temp >> 2) & 0xF));
179 fCount = ((temp & 0x3FF) >> 6);
180
181 temp = fData[fPosition++];
182 fCount |= (temp << 4) & 0x3FF;
183 if ((temp >> 6) != 0xA)
184 AliFatal(Form("Incorrect trailer found ! Expecting 0xA but found %x !",temp >> 6));
185
186 temp = fData[fPosition++];
187 if (temp != 0x2AA)
188 AliFatal(Form("Incorrect trailer found ! Expecting 0x2AA but found %x !",temp));
189
190 Int_t ddlNumber = fRawReader->GetDDLID();
191 Int_t patchIndex;
192 if (ddlNumber < 72) {
193 fSector = ddlNumber / 2;
194 patchIndex = ddlNumber % 2;
195 }
196 else {
197 fSector = (ddlNumber - 72) / 4 + 36;
198 patchIndex = (ddlNumber - 72) % 4 + 2;
199 }
200 fPad = fMapping[patchIndex]->GetPad(hwAdress);
201 fRow = fMapping[patchIndex]->GetPadRow(hwAdress);
202
f8aae377 203 fBunchLength = 0;
204 }
205
206 if (fBunchLength == 0) {
207 if (fPosition >= fDataSize) {
208 Error("Next", "could not read bunch length");
209 return kFALSE;
210 }
211 fBunchLength = fData[fPosition++] - 2;
212 fCount--;
213
214 if (fPosition >= fDataSize) {
215 Error("Next", "could not read time bin");
216 return kFALSE;
217 }
218 fTime = fData[fPosition++] - fBunchLength;
219 fCount--;
220 }
221
222 fTime++;
223 if (fPosition >= fDataSize) {
224 Error("Next", "could not read sample amplitude");
225 return kFALSE;
226 }
42d20574 227 fSignal = fData[fPosition++] + fgkOffset;
f8aae377 228 fCount--;
229 fBunchLength--;
230
231 return kTRUE;
232}
233
234
42d20574 235UShort_t AliTPCRawStream::Get10BitWord(UChar_t* buffer, Int_t position) const
f8aae377 236{
42d20574 237// return a word in a 10 bit array as an UShort_t
238
f8aae377 239 Int_t iBit = position * 10;
240 Int_t iByte = iBit / 8;
241 Int_t shift = iBit % 8;
242// return ((buffer[iByte+1] * 256 + buffer[iByte]) >> shift) & 0x03FF;
243
244 // recalculate the byte numbers and the shift because
245 // the raw data is written as integers where the high bits are filled first
246 // -> little endian is assumed here !
247 Int_t iByteHigh = 4 * (iByte / 4) + 3 - (iByte % 4);
248 iByte++;
249 Int_t iByteLow = 4 * (iByte / 4) + 3 - (iByte % 4);
250 shift = 6 - shift;
251 return ((buffer[iByteHigh] * 256 + buffer[iByteLow]) >> shift) & 0x03FF;
252}