]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderFile.cxx
access methods for block attributes and data header added
[u/mrichter/AliRoot.git] / RAW / AliRawReaderFile.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 /// 
20 /// This is a class for reading raw data files.
21 ///
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.
27 ///
28 /// The constructor of AliRawReaderFile takes the event number or the
29 /// directory name as argument.
30 /// 
31 ///////////////////////////////////////////////////////////////////////////////
32
33 #include "AliRawReaderFile.h"
34 #include <TSystem.h>
35
36
37 ClassImp(AliRawReaderFile)
38
39
40 AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) :
41   fEventIndex(eventNumber),
42   fDirName("."),
43   fDirectory(NULL),
44   fStream(NULL),
45   fEquipmentId(-1),
46   fBuffer(NULL),
47   fBufferSize(0)
48 {
49 // create an object to read digits from the given event
50 // in the current directory
51
52   fDirectory = OpenDirectory();
53   OpenNextFile();
54   fHeader = new AliRawDataHeader;
55 }
56
57 AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) :
58   fEventIndex(eventNumber),
59   fDirName(dirName),
60   fDirectory(NULL),
61   fStream(NULL),
62   fEquipmentId(-1),
63   fBuffer(NULL),
64   fBufferSize(0)
65 {
66 // create an object to read digits from the given directory
67
68   fDirectory = OpenDirectory();
69   OpenNextFile();
70   fHeader = new AliRawDataHeader;
71 }
72
73 AliRawReaderFile::AliRawReaderFile(const AliRawReaderFile& rawReader) :
74   AliRawReader(rawReader)
75 {
76   Fatal("AliRawReaderFile", "copy constructor not implemented");
77 }
78
79 AliRawReaderFile& AliRawReaderFile::operator = (const AliRawReaderFile& 
80                                               /* rawReader */)
81 {
82   Fatal("operator =", "assignment operator not implemented");
83   return *this;
84 }
85
86 AliRawReaderFile::~AliRawReaderFile()
87 {
88 // close the input file
89
90   if (fDirectory) gSystem->FreeDirectory(fDirectory);
91   if (fStream) {
92 #if defined(__HP_aCC) || defined(__DECCXX)
93     if (fStream->rdbuf()->is_open()) fStream->close();
94 #else
95     if (fStream->is_open()) fStream->close();
96 #endif
97     delete fStream;
98   }
99   delete fHeader;
100   if (fBuffer) delete[] fBuffer;
101 }
102
103
104 TString AliRawReaderFile::GetDirName() const
105 {
106 // return the current directory name
107
108   TString dirName(fDirName);
109   if (fEventIndex >= 0) {
110     dirName += "/raw";
111     dirName += fEventIndex;
112   }
113   return dirName;
114 }
115
116 void* AliRawReaderFile::OpenDirectory()
117 {
118 // open and return the directory
119
120   TString dirName = GetDirName();
121   void* directory = gSystem->OpenDirectory(dirName);
122   if (!directory) {
123     Error("OpenDirectory", "could not open directory %s", dirName.Data());
124   }
125   return directory;
126 }
127
128 Bool_t AliRawReaderFile::OpenNextFile()
129 {
130 // open the next file
131 // returns kFALSE if the current file is the last one
132
133   if (fStream) {
134 #if defined(__HP_aCC) || defined(__DECCXX)
135     if (fStream->rdbuf()->is_open()) fStream->close();
136 #else
137     if (fStream->is_open()) fStream->close();
138 #endif
139     delete fStream;
140     fStream = NULL;
141     fEquipmentId = -1;
142   }
143
144   if (!fDirectory) return kFALSE;
145   TString entry;
146   while (entry = gSystem->GetDirEntry(fDirectory)) {
147     if (entry.IsNull()) return kFALSE;
148     if (!entry.EndsWith(".ddl")) continue;
149     char* fileName = gSystem->ConcatFileName(GetDirName(), entry);
150 #ifndef __DECCXX 
151     fStream = new fstream(fileName, ios::binary|ios::in);
152 #else
153     fStream = new fstream(fileName, ios::in);
154 #endif
155     break;
156   }
157
158   if (!fStream) return kFALSE;
159   entry.Remove(0, entry.Last('_')+1);
160   entry.Remove(entry.Length()-4);
161   fEquipmentId = atoi(entry.Data());
162 #if defined(__HP_aCC) || defined(__DECCXX)
163   return (fStream->rdbuf()->is_open());
164 #else
165   return (fStream->is_open());
166 #endif
167 }
168
169
170 Bool_t AliRawReaderFile::ReadHeader()
171 {
172 // read a data header at the current stream position
173 // returns kFALSE if the mini header could not be read
174
175   if (!fStream) return kFALSE;
176   do {
177     if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
178     while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) {
179       if (!OpenNextFile()) return kFALSE;
180     }
181     if (fHeader->fSize != 0xFFFFFFFF) {
182       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
183     } else {
184       UInt_t currentPos = fStream->tellg();
185       fStream->seekg(0, ios::end);
186       fCount = UInt_t(fStream->tellg()) - currentPos;
187       fStream->seekg(currentPos);
188     }
189   } while (!IsSelected());
190   return kTRUE;
191 }
192
193 Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data)
194 {
195 // reads the next payload at the current stream position
196 // returns kFALSE if the data could not be read
197
198   while (fCount == 0) {
199     if (!ReadHeader()) return kFALSE;
200   }
201   if (fBufferSize < fCount) {
202     if (fBuffer) delete[] fBuffer;
203     fBufferSize = Int_t(fCount*1.2);
204     fBuffer = new UChar_t[fBufferSize];
205   }
206   if (!fStream->read((char*) fBuffer, fCount)) {
207     Error("ReadNext", "could not read data!");
208     return kFALSE;
209   }
210   fCount = 0;
211
212   data = fBuffer;
213   return kTRUE;
214 }
215
216 Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size)
217 {
218 // reads the next block of data at the current stream position
219 // returns kFALSE if the data could not be read
220
221   if (!fStream->read((char*) data, size)) {
222     Error("ReadNext", "could not read data!");
223     return kFALSE;
224   }
225   fCount -= size;
226   return kTRUE;
227 }
228
229
230 Bool_t AliRawReaderFile::Reset()
231 {
232 // reset the current stream position to the first DDL file of the curevent
233
234   void* directory = OpenDirectory();
235   if (!directory) return kFALSE;
236
237   if (fStream) {
238 #if defined(__HP_aCC) || defined(__DECCXX)
239     if (fStream->rdbuf()->is_open()) fStream->close();
240 #else
241     if (fStream->is_open()) fStream->close();
242 #endif
243     delete fStream;
244     fStream = NULL;
245   }
246
247   if (fDirectory) gSystem->FreeDirectory(fDirectory);
248   fDirectory = directory;
249
250   OpenNextFile();
251   fCount = 0;
252   return kTRUE;
253 }
254
255 Bool_t AliRawReaderFile::NextEvent()
256 {
257 // go to the next event directory
258
259   if (fEventIndex < -1) return kFALSE;
260
261   do {
262     TString dirName = fDirName + "/raw";
263     dirName += (fEventIndex + 1);
264     void* directory = gSystem->OpenDirectory(dirName);
265     if (!directory) return kFALSE;
266     gSystem->FreeDirectory(directory);
267
268     fEventIndex++;
269     Reset();
270   } while (!IsEventSelected());
271
272   return kTRUE;
273 }
274
275 Bool_t AliRawReaderFile::RewindEvents()
276 {
277 // reset the event counter
278
279   if (fEventIndex >= 0)  fEventIndex = -1;
280   return Reset();
281 }