1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This class provides access to Altro digits in raw data.
22 /// It loops over all Altro 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.
27 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliAltroRawStream.h"
30 #include "AliRawReader.h"
33 ClassImp(AliAltroRawStream)
36 //_____________________________________________________________________________
37 AliAltroRawStream::AliAltroRawStream(AliRawReader* rawReader) :
38 fNoAltroMapping(kTRUE),
39 fIsOldRCUFormat(kFALSE),
40 fIsShortDataHeader(kFALSE),
51 fRawReader(rawReader),
56 fRCUTrailerData(NULL),
59 // create an object to read Altro raw digits
60 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
63 //_____________________________________________________________________________
64 AliAltroRawStream::AliAltroRawStream(const AliAltroRawStream& stream) :
66 fNoAltroMapping(kTRUE),
67 fIsOldRCUFormat(kFALSE),
68 fIsShortDataHeader(kFALSE),
84 fRCUTrailerData(NULL),
87 Fatal("AliAltroRawStream", "copy constructor not implemented");
90 //_____________________________________________________________________________
91 AliAltroRawStream& AliAltroRawStream::operator = (const AliAltroRawStream&
94 Fatal("operator =", "assignment operator not implemented");
98 //_____________________________________________________________________________
99 AliAltroRawStream::~AliAltroRawStream()
105 //_____________________________________________________________________________
106 void AliAltroRawStream::Reset()
108 // reset altro raw stream params
110 fPosition = fCount = fBunchLength = 0;
112 fRCUTrailerData = NULL;
115 fDDLNumber = fPrevDDLNumber = fRCUId = fPrevRCUId = fHWAddress = fPrevHWAddress = fTime = fPrevTime = fSignal = fTimeBunch = -1;
117 if (fRawReader) fRawReader->Reset();
119 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
122 //_____________________________________________________________________________
123 Bool_t AliAltroRawStream::Next()
125 // read the next raw digit
126 // returns kFALSE if there is no digit left
128 fPrevDDLNumber = fDDLNumber;
130 fPrevHWAddress = fHWAddress;
133 while (fCount == 0) { // next trailer
134 if (fPosition <= 0) { // next payload
136 if (!fRawReader->ReadNextData(fData)) return kFALSE;
137 } while (fRawReader->GetDataSize() == 0);
139 fDDLNumber = fRawReader->GetDDLID();
141 fPosition = GetPosition();
145 AliFatal("Incorrect trailer information !");
150 if (fBunchLength == 0) ReadBunch();
158 //_____________________________________________________________________________
159 void AliAltroRawStream::SelectRawData(Int_t detId)
161 // Select the raw data for specific
163 AliDebug(1,Form("Selecting raw data for detector %d",detId));
164 fRawReader->Select(detId);
167 //_____________________________________________________________________________
168 void AliAltroRawStream::SelectRawData(const char *detName)
170 // Select the raw data for specific
172 AliDebug(1,Form("Selecting raw data for detector %s",detName));
173 fRawReader->Select(detName);
176 //_____________________________________________________________________________
177 UShort_t AliAltroRawStream::GetNextWord()
179 // Read the next 10 bit word in backward direction
180 // The input stream access is given by fData and fPosition
184 Int_t iBit = fPosition * 10;
185 Int_t iByte = iBit / 8;
186 Int_t shift = iBit % 8;
188 // the raw data is written as integers where the low bits are filled first
189 // -> little endian is assumed here !
190 Int_t iByteLow = iByte;
192 Int_t iByteHigh = iByte;
193 return ((fData[iByteHigh] * 256 + fData[iByteLow]) >> shift) & 0x03FF;
196 //_____________________________________________________________________________
197 Bool_t AliAltroRawStream::ReadTrailer()
199 //Read a trailer of 40 bits in the backward reading mode
200 //In case of no mapping is provided, read a dummy trailer
201 if (fNoAltroMapping) {
202 AliError("No ALTRO mapping information is loaded! Reading a dummy trailer!");
203 return ReadDummyTrailer();
206 //First reading filling words
208 Int_t nFillWords = 0;
209 while ((temp = GetNextWord()) == 0x2AA) nFillWords++;
210 if (nFillWords == 0) {
212 AliFatal("Incorrect trailer found ! Expected 0x2AA not found !");
215 //Then read the trailer
216 if (fPosition <= 4) {
218 AliFatal(Form("Incorrect raw data size ! Expected at lest 4 words but found %d !",fPosition));
221 fCount = (temp << 4) & 0x3FF;
222 if ((temp >> 6) != 0xA) {
224 AliFatal(Form("Incorrect trailer found ! Expecting 0xA but found %x !",temp >> 6));
227 temp = GetNextWord();
228 fHWAddress = (temp & 0x3) << 10;
229 if (((temp >> 2) & 0xF) != 0xA) {
231 AliFatal(Form("Incorrect trailer found ! Expecting second 0xA but found %x !",(temp >> 2) & 0xF));
233 fCount |= ((temp & 0x3FF) >> 6);
234 if (fCount == 0) return kFALSE;
236 temp = GetNextWord();
239 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
244 //_____________________________________________________________________________
245 Bool_t AliAltroRawStream::ReadDummyTrailer()
247 //Read a trailer of 40 bits in the backward reading mode
248 //In case of no mapping is provided, read a dummy trailer
250 while ((temp = GetNextWord()) == 0x2AA);
252 fSegmentation[0] = temp;
253 fSegmentation[1] = GetNextWord();
254 fSegmentation[2] = GetNextWord();
255 fCount = GetNextWord();
256 if (fCount == 0) return kFALSE;
259 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
264 //_____________________________________________________________________________
265 void AliAltroRawStream::ReadBunch()
267 // Read altro payload in
268 // backward direction
269 if (fPosition <= 0) {
271 AliFatal("Could not read bunch length !");
274 fBunchLength = GetNextWord() - 2;
275 fTimeBunch = fBunchLength;
278 if (fPosition <= 0) {
280 AliFatal("Could not read time bin !");
283 fTime = GetNextWord();
289 //_____________________________________________________________________________
290 void AliAltroRawStream::ReadAmplitude()
292 // Read next time bin amplitude
293 if (fPosition <= 0) {
295 AliFatal("Could not read sample amplitude !");
298 fSignal = GetNextWord();
305 //_____________________________________________________________________________
306 Int_t AliAltroRawStream::GetPosition()
308 // Sets the position in the
310 // Read the RCU trailer
311 // This includes the trailer size,
312 // RCU identifier and raw data payload.
313 // The RCU trailer format is described
314 // in details in the RCU manual.
316 if (!fIsOldRCUFormat) {
317 // First read 32-bit word with the
318 // trailer size (22 bits) and RCU ID (the rest)
319 Int_t index = fRawReader->GetDataSize();
320 UInt_t word = Get32bitWord(index);
321 fRCUId = (Int_t)(word >> 22);
322 Int_t trailerSize = (word & 0x3FFFFF);
324 // Now read the beginning of the trailer
325 // where the payload size is written
326 if (trailerSize < 2) {
328 AliFatal(Form("Invalid trailer size found (%d bytes) !",trailerSize*4));
330 fRCUTrailerSize = (trailerSize-2)*4;
331 index -= fRCUTrailerSize;
334 AliFatal(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
336 fRawReader->GetDataSize()));
338 fRCUTrailerData = fData + index;
339 Int_t position = Get32bitWord(index);
340 // The size is specified in a number of 40bits
341 // Therefore we need to transform it to number of bytes
344 // Check the consistency of the header and trailer
345 if ((fRawReader->GetDataSize() - trailerSize*4) != position) {
347 AliFatal(Form("Inconsistent raw data size ! Raw data size - %d bytes (from the header), RCU trailer - %d bytes, raw data paylod - %d bytes !",
348 fRawReader->GetDataSize(),
353 return position * 8 / 10;
356 // In case of the Old RCU trailer format
357 // we have to read just the size of altro payload
358 // in units of 40-bit words
359 Int_t index = fRawReader->GetDataSize();
360 Int_t position = Get32bitWord(index);
364 fRCUTrailerData = NULL;
366 // The size is specified in a number of 40bits
367 // Therefore we need to transform it to number of bytes
370 if (!fIsShortDataHeader) {
372 // Check the consistency of the header and trailer
373 if ((fRawReader->GetDataSize() - 4) != position) {
375 AliFatal(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
376 fRawReader->GetDataSize()-4,
381 // Check the consistency of the header and trailer
382 // In this case the header is shorter by 4 bytes
383 if (fRawReader->GetDataSize() != position) {
385 AliFatal(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
386 fRawReader->GetDataSize(),
390 // 7 32-bit words Common Data Header
391 // therefore we have to shift back by 4 bytes
392 // the pointer to the raw data payload
396 // Return the position in units of 10-bit words
397 return position*8/10;
401 //_____________________________________________________________________________
402 UInt_t AliAltroRawStream::Get32bitWord(Int_t &index)
404 // This method returns the 32 bit word at a given
405 // position inside the raw data payload.
406 // The 'index' points to the beginning of the next word.
407 // The method is supposed to be endian (platform)
411 AliFatal("Raw data paylod buffer is not yet initialized !");
416 AliFatal(Form("Invalid raw data payload index (%d) !",index));
420 word = fData[--index] << 24;
421 word |= fData[--index] << 16;
422 word |= fData[--index] << 8;
423 word |= fData[--index];
428 //_____________________________________________________________________________
429 Bool_t AliAltroRawStream::GetRCUTrailerData(UChar_t*& data) const
431 // Return a pointer to the RCU trailer
432 // data. Should be called always after
433 // the RCU trailer was already processed
434 // in the GetPosition() method
435 if (!fRCUTrailerSize || !fRCUTrailerData) {
436 AliError("No valid RCU trailer data is found !");
441 data = fRCUTrailerData;
446 //_____________________________________________________________________________
447 void AliAltroRawStream::PrintDebug() const
449 // The method prints all the available
450 // debug information.
451 // Its is used in case of decoding errors.
453 AliError("Start of debug printout\n--------------------");
456 if (fRawReader) fRawReader->Dump();
458 AliError("End of debug printout\n--------------------");