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++;
211 AliFatal("Incorrect trailer found ! Expected 0x2AA not found !");
213 //Then read the trailer
215 AliFatal(Form("Incorrect raw data size ! Expected at lest 4 words but found %d !",fPosition));
217 fCount = (temp << 4) & 0x3FF;
218 if ((temp >> 6) != 0xA)
219 AliFatal(Form("Incorrect trailer found ! Expecting 0xA but found %x !",temp >> 6));
221 temp = GetNextWord();
222 fHWAddress = (temp & 0x3) << 10;
223 if (((temp >> 2) & 0xF) != 0xA)
224 AliFatal(Form("Incorrect trailer found ! Expecting second 0xA but found %x !",(temp >> 2) & 0xF));
225 fCount |= ((temp & 0x3FF) >> 6);
226 if (fCount == 0) return kFALSE;
228 temp = GetNextWord();
231 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
236 //_____________________________________________________________________________
237 Bool_t AliAltroRawStream::ReadDummyTrailer()
239 //Read a trailer of 40 bits in the backward reading mode
240 //In case of no mapping is provided, read a dummy trailer
242 while ((temp = GetNextWord()) == 0x2AA);
244 fSegmentation[0] = temp;
245 fSegmentation[1] = GetNextWord();
246 fSegmentation[2] = GetNextWord();
247 fCount = GetNextWord();
248 if (fCount == 0) return kFALSE;
251 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
256 //_____________________________________________________________________________
257 void AliAltroRawStream::ReadBunch()
259 // Read altro payload in
260 // backward direction
262 AliFatal("Could not read bunch length !");
264 fBunchLength = GetNextWord() - 2;
265 fTimeBunch = fBunchLength;
269 AliFatal("Could not read time bin !");
271 fTime = GetNextWord();
277 //_____________________________________________________________________________
278 void AliAltroRawStream::ReadAmplitude()
280 // Read next time bin amplitude
282 AliFatal("Could not read sample amplitude !");
284 fSignal = GetNextWord();
291 //_____________________________________________________________________________
292 Int_t AliAltroRawStream::GetPosition()
294 // Sets the position in the
296 // Read the RCU trailer
297 // This includes the trailer size,
298 // RCU identifier and raw data payload.
299 // The RCU trailer format is described
300 // in details in the RCU manual.
302 if (!fIsOldRCUFormat) {
303 // First read 32-bit word with the
304 // trailer size (22 bits) and RCU ID (the rest)
305 Int_t index = fRawReader->GetDataSize();
306 UInt_t word = Get32bitWord(index);
307 fRCUId = (Int_t)(word >> 22);
308 Int_t trailerSize = (word & 0x3FFFFF);
310 // Now read the beginning of the trailer
311 // where the payload size is written
313 AliFatal(Form("Invalid trailer size found (%d bytes) !",trailerSize*4));
314 fRCUTrailerSize = (trailerSize-2)*4;
315 index -= fRCUTrailerSize;
317 AliFatal(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
319 fRawReader->GetDataSize()));
320 fRCUTrailerData = fData + index;
321 Int_t position = Get32bitWord(index);
322 // The size is specified in a number of 40bits
323 // Therefore we need to transform it to number of bytes
326 // Check the consistency of the header and trailer
327 if ((fRawReader->GetDataSize() - trailerSize*4) != position)
328 AliFatal(Form("Inconsistent raw data size ! Raw data size - %d bytes (from the header), RCU trailer - %d bytes, raw data paylod - %d bytes !",
329 fRawReader->GetDataSize(),
333 return position * 8 / 10;
336 // In case of the Old RCU trailer format
337 // we have to read just the size of altro payload
338 // in units of 40-bit words
339 Int_t index = fRawReader->GetDataSize();
340 Int_t position = Get32bitWord(index);
344 fRCUTrailerData = NULL;
346 // The size is specified in a number of 40bits
347 // Therefore we need to transform it to number of bytes
350 if (!fIsShortDataHeader) {
352 // Check the consistency of the header and trailer
353 if ((fRawReader->GetDataSize() - 4) != position)
354 AliFatal(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
355 fRawReader->GetDataSize()-4,
359 // Check the consistency of the header and trailer
360 // In this case the header is shorter by 4 bytes
361 if (fRawReader->GetDataSize() != position)
362 AliFatal(Form("Inconsistent raw data size ! Expected %d bytes (from the header), found %d bytes (in the RCU trailer)!",
363 fRawReader->GetDataSize(),
366 // 7 32-bit words Common Data Header
367 // therefore we have to shift back by 4 bytes
368 // the pointer to the raw data payload
372 // Return the position in units of 10-bit words
373 return position*8/10;
377 //_____________________________________________________________________________
378 UInt_t AliAltroRawStream::Get32bitWord(Int_t &index)
380 // This method returns the 32 bit word at a given
381 // position inside the raw data payload.
382 // The 'index' points to the beginning of the next word.
383 // The method is supposed to be endian (platform)
386 AliFatal("Raw data paylod buffer is not yet initialized !");
388 AliFatal(Form("Invalid raw data payload index (%d) !",index));
391 word = fData[--index] << 24;
392 word |= fData[--index] << 16;
393 word |= fData[--index] << 8;
394 word |= fData[--index];
399 //_____________________________________________________________________________
400 Bool_t AliAltroRawStream::GetRCUTrailerData(UChar_t*& data) const
402 // Return a pointer to the RCU trailer
403 // data. Should be called always after
404 // the RCU trailer was already processed
405 // in the GetPosition() method
406 if (!fRCUTrailerSize || !fRCUTrailerData) {
407 AliError("No valid RCU trailer data is found !");
412 data = fRCUTrailerData;