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),
49 fRawReader(rawReader),
54 fRCUTrailerData(NULL),
57 // create an object to read Altro raw digits
58 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
61 //_____________________________________________________________________________
62 AliAltroRawStream::AliAltroRawStream(const AliAltroRawStream& stream) :
64 fNoAltroMapping(kTRUE),
80 fRCUTrailerData(NULL),
83 Fatal("AliAltroRawStream", "copy constructor not implemented");
86 //_____________________________________________________________________________
87 AliAltroRawStream& AliAltroRawStream::operator = (const AliAltroRawStream&
90 Fatal("operator =", "assignment operator not implemented");
94 //_____________________________________________________________________________
95 AliAltroRawStream::~AliAltroRawStream()
101 //_____________________________________________________________________________
102 void AliAltroRawStream::Reset()
104 // reset altro raw stream params
106 fPosition = fCount = fBunchLength = 0;
108 fRCUTrailerData = NULL;
111 fDDLNumber = fPrevDDLNumber = fRCUId = fPrevRCUId = fHWAddress = fPrevHWAddress = fTime = fPrevTime = fSignal = fTimeBunch = -1;
113 if (fRawReader) fRawReader->Reset();
115 fSegmentation[0] = fSegmentation[1] = fSegmentation[2] = -1;
118 //_____________________________________________________________________________
119 Bool_t AliAltroRawStream::Next()
121 // read the next raw digit
122 // returns kFALSE if there is no digit left
124 fPrevDDLNumber = fDDLNumber;
126 fPrevHWAddress = fHWAddress;
129 while (fCount == 0) { // next trailer
130 if (fPosition <= 0) { // next payload
132 if (!fRawReader->ReadNextData(fData)) return kFALSE;
133 } while (fRawReader->GetDataSize() == 0);
135 fDDLNumber = fRawReader->GetDDLID();
137 fPosition = GetPosition();
141 AliFatal("Incorrect trailer information !");
146 if (fBunchLength == 0) ReadBunch();
154 //_____________________________________________________________________________
155 void AliAltroRawStream::SelectRawData(Int_t detId)
157 // Select the raw data for specific
159 AliDebug(1,Form("Selecting raw data for detector %d",detId));
160 fRawReader->Select(detId);
163 //_____________________________________________________________________________
164 UShort_t AliAltroRawStream::GetNextWord()
166 // Read the next 10 bit word in backward direction
167 // The input stream access is given by fData and fPosition
171 Int_t iBit = fPosition * 10;
172 Int_t iByte = iBit / 8;
173 Int_t shift = iBit % 8;
175 // the raw data is written as integers where the low bits are filled first
176 // -> little endian is assumed here !
177 Int_t iByteLow = iByte;
179 Int_t iByteHigh = iByte;
180 return ((fData[iByteHigh] * 256 + fData[iByteLow]) >> shift) & 0x03FF;
183 //_____________________________________________________________________________
184 Bool_t AliAltroRawStream::ReadTrailer()
186 //Read a trailer of 40 bits in the backward reading mode
187 //In case of no mapping is provided, read a dummy trailer
188 if (fNoAltroMapping) {
189 AliError("No ALTRO mapping information is loaded! Reading a dummy trailer!");
190 return ReadDummyTrailer();
193 //First reading filling words
195 Int_t nFillWords = 0;
196 while ((temp = GetNextWord()) == 0x2AA) nFillWords++;
198 AliFatal("Incorrect trailer found ! Expected 0x2AA not found !");
200 //Then read the trailer
202 AliFatal(Form("Incorrect raw data size ! Expected at lest 4 words but found %d !",fPosition));
204 fCount = (temp << 4) & 0x3FF;
205 if ((temp >> 6) != 0xA)
206 AliFatal(Form("Incorrect trailer found ! Expecting 0xA but found %x !",temp >> 6));
208 temp = GetNextWord();
209 fHWAddress = (temp & 0x3) << 10;
210 if (((temp >> 2) & 0xF) != 0xA)
211 AliFatal(Form("Incorrect trailer found ! Expecting second 0xA but found %x !",(temp >> 2) & 0xF));
212 fCount |= ((temp & 0x3FF) >> 6);
213 if (fCount == 0) return kFALSE;
215 temp = GetNextWord();
218 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
223 //_____________________________________________________________________________
224 Bool_t AliAltroRawStream::ReadDummyTrailer()
226 //Read a trailer of 40 bits in the backward reading mode
227 //In case of no mapping is provided, read a dummy trailer
229 while ((temp = GetNextWord()) == 0x2AA);
231 fSegmentation[0] = temp;
232 fSegmentation[1] = GetNextWord();
233 fSegmentation[2] = GetNextWord();
234 fCount = GetNextWord();
235 if (fCount == 0) return kFALSE;
238 fPosition -= (4 - (fCount % 4)) % 4; // skip fill words
243 //_____________________________________________________________________________
244 void AliAltroRawStream::ReadBunch()
246 // Read altro payload in
247 // backward direction
249 AliFatal("Could not read bunch length !");
251 fBunchLength = GetNextWord() - 2;
252 fTimeBunch = fBunchLength;
256 AliFatal("Could not read time bin !");
258 fTime = GetNextWord();
264 //_____________________________________________________________________________
265 void AliAltroRawStream::ReadAmplitude()
267 // Read next time bin amplitude
269 AliFatal("Could not read sample amplitude !");
271 fSignal = GetNextWord();
278 //_____________________________________________________________________________
279 Int_t AliAltroRawStream::GetPosition()
281 // Sets the position in the
283 // Read the RCU trailer
284 // This includes the trailer size,
285 // RCU identifier and raw data payload.
286 // The RCU trailer format is described
287 // in details in the RCU manual.
289 // First read 32-bit word with the
290 // trailer size (22 bits) and RCU ID (the rest)
291 Int_t index = fRawReader->GetDataSize();
292 UInt_t word = Get32bitWord(index);
293 fRCUId = (Int_t)(word >> 22);
294 Int_t trailerSize = (word & 0x3FFFFF);
296 // Now read the beginning of the trailer
297 // where the payload size is written
299 AliFatal(Form("Invalid trailer size found (%d bytes) !",trailerSize*4));
300 fRCUTrailerSize = (trailerSize-2)*4;
301 index -= fRCUTrailerSize;
303 AliFatal(Form("Invalid trailer size found (%d bytes) ! The size is bigger than the raw data size (%d bytes)!",
305 fRawReader->GetDataSize()));
306 fRCUTrailerData = fData + index;
307 Int_t position = Get32bitWord(index);
308 // The size is specified in a number of 40bits
309 // Therefore we need to transform it to number of bytes
312 // Check the consistency of the header and trailer
313 if ((fRawReader->GetDataSize() - trailerSize*4) != position)
314 AliFatal(Form("Inconsistent raw data size ! Raw data size - %d bytes (from the header), RCU trailer - %d bytes, raw data paylod - %d bytes !",
315 fRawReader->GetDataSize(),
319 return position * 8 / 10;
322 //_____________________________________________________________________________
323 UInt_t AliAltroRawStream::Get32bitWord(Int_t &index)
325 // This method returns the 32 bit word at a given
326 // position inside the raw data payload.
327 // The 'index' points to the beginning of the next word.
328 // The method is supposed to be endian (platform)
331 AliFatal("Raw data paylod buffer is not yet initialized !");
333 AliFatal(Form("Invalid raw data payload index (%d) !",index));
336 word = fData[--index] << 24;
337 word |= fData[--index] << 16;
338 word |= fData[--index] << 8;
339 word |= fData[--index];
344 //_____________________________________________________________________________
345 Bool_t AliAltroRawStream::GetRCUTrailerData(UChar_t*& data) const
347 // Return a pointer to the RCU trailer
348 // data. Should be called always after
349 // the RCU trailer was already processed
350 // in the GetPosition() method
351 if (!fRCUTrailerSize || !fRCUTrailerData) {
352 AliError("No valid RCU trailer data is found !");
357 data = fRCUTrailerData;