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 files.
22 /// The files of one event are expected to be in one directory. The name
23 /// of the directory is "raw" + the event number. Each file contains
24 /// the raw data (with data header) of one DDL. The convention for the
25 /// file names is "DET_#DDL.ddl". "DET" is the name of the detector and
26 /// "#DDL" is the unique equipment ID.
28 /// The constructor of AliRawReaderFile takes the event number or the
29 /// directory name as argument.
31 ///////////////////////////////////////////////////////////////////////////////
34 #include "AliRawReaderFile.h"
40 ClassImp(AliRawReaderFile)
43 AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) :
44 fEventIndex(eventNumber),
55 // create an object to read digits from the given event
56 // in the current directory
58 fDirectory = OpenDirectory();
60 fHeader = new AliRawDataHeader;
63 AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) :
64 fEventIndex(eventNumber),
75 // create an object to read digits from the given directory
77 fDirectory = OpenDirectory();
79 fHeader = new AliRawDataHeader;
82 AliRawReaderFile::~AliRawReaderFile()
84 // close the input file
86 if (fDirectory) gSystem->FreeDirectory(fDirectory);
88 #if defined(__HP_aCC) || defined(__DECCXX)
89 if (fStream->rdbuf()->is_open()) fStream->close();
91 if (fStream->is_open()) fStream->close();
95 if (fHeader) delete fHeader;
96 if (fBuffer) delete[] fBuffer;
97 if (fDDLIndex) delete fDDLIndex; fDDLIndex=NULL;
100 void AliRawReaderFile::RequireHeader(Bool_t required)
102 // Reading of raw data in case of missing
103 // raw data header is not implemented for
106 Warning("AliRawReaderFile","Reading of raw data without raw data header!");
107 if (fHeader) delete fHeader;
111 if (!fHeader) fHeader = new AliRawDataHeader;
114 AliRawReader::RequireHeader(required);
117 TString AliRawReaderFile::GetDirName() const
119 // return the current directory name
121 TString dirName(fDirName);
122 if (fEventIndex >= 0) {
124 dirName += fEventIndex;
129 void* AliRawReaderFile::OpenDirectory()
131 // open and return the directory
133 TString dirName = GetDirName();
134 void* directory = gSystem->OpenDirectory(dirName);
136 Error("OpenDirectory", "could not open directory %s", dirName.Data());
141 Bool_t AliRawReaderFile::CreateFileIndex()
143 // scan the files of the directory and create index of all DDL files
144 // returns kFALSE if no DDL files available
145 Bool_t result=kFALSE;
147 if (fDDLIndex) return fDDLIndex->GetSize()>0;
148 if (!fDirectory) return kFALSE;
149 fDDLIndex=new TArrayC(0);
150 if (!fDDLIndex) return kFALSE;
152 while (entry = gSystem->GetDirEntry(fDirectory)) {
153 const char* filename=entry.Data();
154 if (!filename || entry.IsNull()) break;
155 if (!entry.EndsWith(".ddl")) continue;
157 entry.Remove(0, entry.Last('_')+1);
158 entry.Remove(entry.Length()-4);
159 Int_t equipmentId = atoi(entry.Data());
160 if (fDDLIndex->GetSize()<=equipmentId) {
161 fDDLIndex->Set(equipmentId+1);
163 char* array=(char*)fDDLIndex->GetArray();
164 array[equipmentId]=1;
170 Bool_t AliRawReaderFile::OpenNextFile()
172 // open the next file
173 // returns kFALSE if the current file is the last one
175 if (!fDDLIndex && !CreateFileIndex()) return kFALSE;
176 if (fSelectMinEquipmentId>=0 && fSelectMinEquipmentId>fEquipmentId)
177 fDDLCurrent=fSelectMinEquipmentId-1;
180 #if defined(__HP_aCC) || defined(__DECCXX)
181 if (fStream->rdbuf()->is_open()) fStream->close();
183 if (fStream->is_open()) fStream->close();
191 if (!fDirectory) return kFALSE;
192 while (++fDDLCurrent<(fDDLIndex->GetSize()) &&
193 (fDDLCurrent<=fSelectMaxEquipmentId || fSelectMaxEquipmentId<0)) {
194 if (fDDLIndex->At(fDDLCurrent)==0) continue;
197 entry.Form("%s_%d.ddl", AliDAQ::DetectorNameFromDdlID(fDDLCurrent, dummy), fDDLCurrent);
198 char* fileName = gSystem->ConcatFileName(GetDirName(), entry);
199 if (!fileName) continue;
201 fStream = new fstream(fileName, ios::binary|ios::in);
203 fStream = new fstream(fileName, ios::in);
209 if (!fStream) return kFALSE;
210 fEquipmentId = fDDLCurrent;
211 #if defined(__HP_aCC) || defined(__DECCXX)
212 return (fStream->rdbuf()->is_open());
214 return (fStream->is_open());
219 Bool_t AliRawReaderFile::ReadHeader()
221 // read a data header at the current stream position
222 // returns kFALSE if the mini header could not be read
224 if (!fStream && !OpenNextFile()) return kFALSE;
226 if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
228 while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) {
229 if (!OpenNextFile()) return kFALSE;
234 if (!OpenNextFile()) return kFALSE;
236 if (fHeader && fHeader->fSize != 0xFFFFFFFF) {
237 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
239 UInt_t currentPos = fStream->tellg();
240 fStream->seekg(0, ios::end);
241 fCount = UInt_t(fStream->tellg()) - currentPos;
242 fStream->seekg(currentPos);
244 fEquipmentSize = fCount;
245 if (fHeader) fEquipmentSize += sizeof(AliRawDataHeader);
246 } while (!IsSelected());
250 Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data)
252 // reads the next payload at the current stream position
253 // returns kFALSE if the data could not be read
255 while (fCount == 0) {
256 if (!ReadHeader()) return kFALSE;
258 if (fBufferSize < fCount) {
259 if (fBuffer) delete[] fBuffer;
260 fBufferSize = Int_t(fCount*1.2);
261 fBuffer = new UChar_t[fBufferSize];
263 if (!fStream->read((char*) fBuffer, fCount)) {
264 Error("ReadNext", "could not read data!");
273 Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size)
275 // reads the next block of data at the current stream position
276 // returns kFALSE if the data could not be read
278 if (!fStream->read((char*) data, size)) {
279 Error("ReadNext", "could not read data!");
287 Bool_t AliRawReaderFile::Reset()
289 // reset the current stream position to the first DDL file of the curevent
291 void* directory = OpenDirectory();
292 if (!directory) return kFALSE;
295 #if defined(__HP_aCC) || defined(__DECCXX)
296 if (fStream->rdbuf()->is_open()) fStream->close();
298 if (fStream->is_open()) fStream->close();
304 if (fDirectory) gSystem->FreeDirectory(fDirectory);
305 fDirectory = directory;
307 // Matthias 05.06.2008
308 // do not open the next file. That might collide with a subsequent
309 // SelectEquipment call as the 'file pointer' is already set.
310 // This is important for the indexing of the DDL files.
311 // ---------------------------------------------------------
312 // All ReadNext functions first require the fCount member to be
313 // non zero or call ReadHeader. That allows to postpone the call
314 // to OpenNextFile to the next invocation of ReadHeader.
315 // ReadHeader has been mofified according to that.
325 Bool_t AliRawReaderFile::NextEvent()
327 // go to the next event directory
329 if (fDDLIndex) delete fDDLIndex;
331 if (fEventIndex < -1) return kFALSE;
334 TString dirName = fDirName + "/raw";
335 dirName += (fEventIndex + 1);
336 void* directory = gSystem->OpenDirectory(dirName);
337 if (!directory) return kFALSE;
338 gSystem->FreeDirectory(directory);
342 } while (!IsEventSelected());
349 Bool_t AliRawReaderFile::RewindEvents()
351 // reset the event counter
353 if (fEventIndex >= 0) fEventIndex = -1;