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 is a class for reading raw data memory buffers.
22 ///////////////////////////////////////////////////////////////////////////////
24 #include "AliRawReaderMemory.h"
28 ClassImp(AliRawReaderMemory)
31 AliRawReaderMemory::AliRawReaderMemory() :
36 // create an object to read digits from
37 // the given memory location
40 AliRawReaderMemory::AliRawReaderMemory(UChar_t* memory, UInt_t size) :
45 // create an object to read digits from the given memory
46 fBuffers.push_back(AliRRMBuffer(memory, size, -1));
49 AliRawReaderMemory::~AliRawReaderMemory()
51 // close the input memory
54 void AliRawReaderMemory::RequireHeader(Bool_t required)
56 // Reading of raw data in case of missing
57 // raw data header is not implemented for
60 Fatal("AliRawReaderMemory","Reading of raw data without raw data header is not implemented !");
62 AliRawReader::RequireHeader(required);
65 Bool_t AliRawReaderMemory::ReadHeader()
67 // read a data header at the current buffer position
68 // returns kFALSE if the mini header could not be read
71 if (fCurrent>=fBuffers.size()) return kFALSE;
76 if (fBuffers[fCurrent].GetEquipmentId() == -1)
78 Warning("ReadHeader", "The equipment ID is not set for the DDL memory buffer.");
80 if (!fBuffers[fCurrent].GetBuffer()) break;
82 // Check if we would not read past the end of the buffer.
83 if ( fPosition+fCount >= fBuffers[fCurrent].GetBufferSize() ) break;
85 fHeader = reinterpret_cast<AliRawDataHeader*>(fBuffers[fCurrent].GetBuffer()+fPosition+fCount);
86 fHeaderV3 = reinterpret_cast<AliRawDataHeaderV3*>(fBuffers[fCurrent].GetBuffer()+fPosition+fCount);
88 //Access to version and size is uniform for V2 and V3
89 UChar_t version = fHeader->GetVersion();
90 UInt_t size = fHeader->fSize;
95 headerSize = sizeof(AliRawDataHeaderV3);
96 } else if(version == 2) {
98 headerSize = sizeof(AliRawDataHeader);
100 Error("ReadHeader", "Wrong raw data header version: %d. Expected: 2 or 3.", version);
104 // Check that the header is sane, that is the size does not go past the buffer.
105 // Otherwise try again at the next word location.
107 if ( ( (size == 0) ||
108 ((Int_t)fPosition + fCount + (Int_t)size > (Int_t)fBuffers[fCurrent].GetBufferSize() ) )
109 && size != 0xFFFFFFFF) {
111 if (fPosition + sizeof(UInt_t) <= fBuffers[fCurrent].GetBufferSize()) {
112 fPosition += sizeof(UInt_t);
115 Error("ReadHeader", "Could not find a valid DDL header!");
119 fPosition += fCount + headerSize;
124 if (size != 0xFFFFFFFF) {
125 fCount = (Int_t)size - headerSize;
127 fCount = fBuffers[fCurrent].GetBufferSize() - headerSize;
129 } while (!(result=IsSelected()) && OpenNextBuffer());
130 } while (!result && OpenNextBuffer());
135 Bool_t AliRawReaderMemory::OpenNextBuffer()
137 // increment to next buffer
140 if (fCurrent>=fBuffers.size()) return kFALSE;
141 if (++fCurrent>=fBuffers.size()) return kFALSE;
145 Bool_t AliRawReaderMemory::ReadNextData(UChar_t*& data)
147 // reads the next payload at the current buffer position
148 // returns kFALSE if the data could not be read
150 while (fCount == 0) {
151 if (!ReadHeader()) return kFALSE;
155 Error("ReadNextData","Cannot read data, payload is negative");
159 UInt_t currentPosition = fPosition;
163 if(fBuffers[fCurrent].GetBufferSize()<currentPosition){
164 Error("ReadNextData","Current position exceeds buffersize.");
167 data = fBuffers[fCurrent].GetBuffer()+currentPosition;
171 Bool_t AliRawReaderMemory::ReadNext(UChar_t* data, Int_t size)
173 // reads the next block of data at the current buffer position
174 // but does not shift to the next equipment. The next equipment
175 // must be activated by calling ReadHeader
176 // returns kFALSE if the data could not be read
179 if (fCurrent>=fBuffers.size()) return kFALSE;
180 if ( fBuffers[fCurrent].GetBufferSize()-fPosition < (UInt_t)size ) return kFALSE;
182 memcpy( data, fBuffers[fCurrent].GetBuffer()+fPosition, size );
189 Bool_t AliRawReaderMemory::Reset()
191 // reset the current position in the buffer to the beginning of the curevent
201 Bool_t AliRawReaderMemory::NextEvent()
203 // each memory buffer always contains only one event
204 if (fEventNumber < 0) {
212 Bool_t AliRawReaderMemory::RewindEvents()
214 // reset the event counter
220 Bool_t AliRawReaderMemory::SetMemory( UChar_t* memory, ULong_t size )
222 // SetMemory function kept for backward compatibility, only allowed
223 // if no blocks have been added so far
224 if (!memory || size<=0) return kFALSE;
225 if (fBuffers.size()>1 || (fBuffers.size()==1 && fPosition==0 && fCurrent==0)) {
226 Error("SetMemory","can not SetMemory for multiple buffers, use AddBuffer(...)");
229 if (fBuffers.size()==1) fBuffers.pop_back();
230 fBuffers.push_back(AliRRMBuffer(memory, size, -1));
239 void AliRawReaderMemory::SetEquipmentID(Int_t id)
241 // SetMemory function kept for backward compatibility, only allowed
242 // if no blocks have been added so far, set equipment id of the first
244 if (fBuffers.size()>1) {
245 Error("SetEquipmentID", "can not SetEquipmentID for multiple buffers, use AddBuffer(...)");
248 if (fBuffers.size()==0 || fCurrent>=fBuffers.size()) {
249 Error("SetEquipmentID", "no block available to set equipment id");
252 fBuffers[fCurrent].SetEquipmentId(id);
255 Int_t AliRawReaderMemory::GetEquipmentSize() const
257 // get the size of the equipment, that is payload + CDH
258 if (fCurrent>=fBuffers.size()) return 0;
259 return fBuffers[fCurrent].GetBufferSize();
262 Int_t AliRawReaderMemory::GetEquipmentId() const
264 // get the current equipment id
265 if (fCurrent>=fBuffers.size()) return -1;
266 return fBuffers[fCurrent].GetEquipmentId();
269 Bool_t AliRawReaderMemory::AddBuffer(UChar_t* memory, ULong_t size, Int_t equipmentId )
271 // Add a buffer to the list
272 if (!memory || size<=0 || equipmentId<0 ) return kFALSE;
273 fBuffers.push_back(AliRRMBuffer(memory, size, equipmentId));
277 void AliRawReaderMemory::ClearBuffers()
279 // Clear the buffer list
284 AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer()
293 AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer(UChar_t* pBuffer, UInt_t bufferSize, Int_t equipmentId)
296 fBufferSize(bufferSize),
297 fEquipmentId(equipmentId)
302 AliRawReaderMemory::AliRRMBuffer::~AliRRMBuffer()
307 AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer(const AliRRMBuffer& src)
309 fBuffer(src.fBuffer),
310 fBufferSize(src.fBufferSize),
311 fEquipmentId(src.fEquipmentId)
313 // copy ctor, there are no buffers allocated internally, pointers
317 AliRawReaderMemory::AliRRMBuffer& AliRawReaderMemory::AliRRMBuffer::operator=(const AliRRMBuffer& src)
320 if(&src == this) return *this;
322 fBufferSize=src.fBufferSize;
323 fEquipmentId=src.fEquipmentId;