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