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" |
df3a33be |
35 | #include "AliDAQ.h" |
39f9963f |
36 | #include <TSystem.h> |
df3a33be |
37 | #include <TArrayC.h> |
04fa961a |
38 | |
39 | |
40 | ClassImp(AliRawReaderFile) |
41 | |
42 | |
39f9963f |
43 | AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) : |
dd9a70fe |
44 | fEventIndex(eventNumber), |
45 | fDirName("."), |
39f9963f |
46 | fDirectory(NULL), |
47 | fStream(NULL), |
48 | fEquipmentId(-1), |
49 | fBuffer(NULL), |
1c10cde6 |
50 | fBufferSize(0), |
df3a33be |
51 | fEquipmentSize(0), |
52 | fDDLIndex(NULL), |
be974aae |
53 | fDDLCurrent(-1), |
54 | fType(7), |
55 | fRunNb(0), |
56 | fDetectorPattern(0), |
57 | fTimestamp(0) |
04fa961a |
58 | { |
39f9963f |
59 | // create an object to read digits from the given event |
dd9a70fe |
60 | // in the current directory |
04fa961a |
61 | |
dd9a70fe |
62 | fDirectory = OpenDirectory(); |
a97af23d |
63 | if (!fDirectory) fIsValid = kFALSE; |
64 | if (!OpenNextFile()) fIsValid = kFALSE; |
39f9963f |
65 | fHeader = new AliRawDataHeader; |
be974aae |
66 | |
67 | fId[0] = fId[1] = 0; |
68 | fTriggerPattern[0] = fTriggerPattern[1] = 0; |
39f9963f |
69 | } |
70 | |
dd9a70fe |
71 | AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) : |
72 | fEventIndex(eventNumber), |
39f9963f |
73 | fDirName(dirName), |
74 | fDirectory(NULL), |
75 | fStream(NULL), |
76 | fEquipmentId(-1), |
77 | fBuffer(NULL), |
1c10cde6 |
78 | fBufferSize(0), |
df3a33be |
79 | fEquipmentSize(0), |
80 | fDDLIndex(NULL), |
be974aae |
81 | fDDLCurrent(-1), |
82 | fType(7), |
83 | fRunNb(0), |
84 | fDetectorPattern(0), |
85 | fTimestamp(0) |
39f9963f |
86 | { |
87 | // create an object to read digits from the given directory |
88 | |
dd9a70fe |
89 | fDirectory = OpenDirectory(); |
a97af23d |
90 | if (!fDirectory) fIsValid = kFALSE; |
36af91c9 |
91 | if (fEventIndex >= 0 && (!OpenNextFile())) fIsValid = kFALSE; |
39f9963f |
92 | fHeader = new AliRawDataHeader; |
be974aae |
93 | |
94 | fId[0] = fId[1] = 0; |
95 | fTriggerPattern[0] = fTriggerPattern[1] = 0; |
04fa961a |
96 | } |
97 | |
98 | AliRawReaderFile::~AliRawReaderFile() |
99 | { |
100 | // close the input file |
101 | |
39f9963f |
102 | if (fDirectory) gSystem->FreeDirectory(fDirectory); |
04fa961a |
103 | if (fStream) { |
104 | #if defined(__HP_aCC) || defined(__DECCXX) |
105 | if (fStream->rdbuf()->is_open()) fStream->close(); |
106 | #else |
107 | if (fStream->is_open()) fStream->close(); |
108 | #endif |
109 | delete fStream; |
110 | } |
1c10cde6 |
111 | if (fHeader) delete fHeader; |
04fa961a |
112 | if (fBuffer) delete[] fBuffer; |
df3a33be |
113 | if (fDDLIndex) delete fDDLIndex; fDDLIndex=NULL; |
04fa961a |
114 | } |
115 | |
299738b9 |
116 | void AliRawReaderFile::RequireHeader(Bool_t required) |
117 | { |
118 | // Reading of raw data in case of missing |
119 | // raw data header is not implemented for |
120 | // this class |
1c10cde6 |
121 | if (!required) { |
839d0155 |
122 | Warning("AliRawReaderFile","Reading of raw data without raw data header!"); |
1c10cde6 |
123 | if (fHeader) delete fHeader; |
124 | fHeader = NULL; |
125 | } |
126 | else { |
127 | if (!fHeader) fHeader = new AliRawDataHeader; |
128 | } |
299738b9 |
129 | |
130 | AliRawReader::RequireHeader(required); |
131 | } |
04fa961a |
132 | |
dd9a70fe |
133 | TString AliRawReaderFile::GetDirName() const |
134 | { |
135 | // return the current directory name |
136 | |
137 | TString dirName(fDirName); |
138 | if (fEventIndex >= 0) { |
139 | dirName += "/raw"; |
140 | dirName += fEventIndex; |
141 | } |
142 | return dirName; |
143 | } |
144 | |
145 | void* AliRawReaderFile::OpenDirectory() |
146 | { |
147 | // open and return the directory |
148 | |
149 | TString dirName = GetDirName(); |
150 | void* directory = gSystem->OpenDirectory(dirName); |
151 | if (!directory) { |
152 | Error("OpenDirectory", "could not open directory %s", dirName.Data()); |
153 | } |
154 | return directory; |
155 | } |
156 | |
df3a33be |
157 | Bool_t AliRawReaderFile::CreateFileIndex() |
158 | { |
159 | // scan the files of the directory and create index of all DDL files |
160 | // returns kFALSE if no DDL files available |
161 | Bool_t result=kFALSE; |
162 | fDDLCurrent=-1; |
163 | if (fDDLIndex) return fDDLIndex->GetSize()>0; |
164 | if (!fDirectory) return kFALSE; |
165 | fDDLIndex=new TArrayC(0); |
166 | if (!fDDLIndex) return kFALSE; |
167 | TString entry; |
168 | while (entry = gSystem->GetDirEntry(fDirectory)) { |
169 | const char* filename=entry.Data(); |
170 | if (!filename || entry.IsNull()) break; |
3fbd60fe |
171 | if (entry.BeginsWith("run")) { |
172 | entry.ReplaceAll("run",""); |
173 | fRunNb = entry.Atoi(); |
174 | continue; |
175 | } |
df3a33be |
176 | if (!entry.EndsWith(".ddl")) continue; |
177 | result=kTRUE; |
178 | entry.Remove(0, entry.Last('_')+1); |
179 | entry.Remove(entry.Length()-4); |
180 | Int_t equipmentId = atoi(entry.Data()); |
4ba28796 |
181 | Int_t ddlIndex = -1; |
182 | fDetectorPattern |= (1 << AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex)); |
df3a33be |
183 | if (fDDLIndex->GetSize()<=equipmentId) { |
184 | fDDLIndex->Set(equipmentId+1); |
185 | } |
186 | char* array=(char*)fDDLIndex->GetArray(); |
187 | array[equipmentId]=1; |
188 | } |
189 | |
190 | return result; |
191 | } |
192 | |
04fa961a |
193 | Bool_t AliRawReaderFile::OpenNextFile() |
194 | { |
42d20574 |
195 | // open the next file |
196 | // returns kFALSE if the current file is the last one |
197 | |
df3a33be |
198 | if (!fDDLIndex && !CreateFileIndex()) return kFALSE; |
199 | if (fSelectMinEquipmentId>=0 && fSelectMinEquipmentId>fEquipmentId) |
200 | fDDLCurrent=fSelectMinEquipmentId-1; |
201 | |
04fa961a |
202 | if (fStream) { |
203 | #if defined(__HP_aCC) || defined(__DECCXX) |
204 | if (fStream->rdbuf()->is_open()) fStream->close(); |
205 | #else |
206 | if (fStream->is_open()) fStream->close(); |
207 | #endif |
208 | delete fStream; |
209 | fStream = NULL; |
39f9963f |
210 | fEquipmentId = -1; |
1c10cde6 |
211 | fEquipmentSize = 0; |
04fa961a |
212 | } |
04fa961a |
213 | |
39f9963f |
214 | if (!fDirectory) return kFALSE; |
df3a33be |
215 | while (++fDDLCurrent<(fDDLIndex->GetSize()) && |
216 | (fDDLCurrent<=fSelectMaxEquipmentId || fSelectMaxEquipmentId<0)) { |
217 | if (fDDLIndex->At(fDDLCurrent)==0) continue; |
218 | Int_t dummy=0; |
219 | TString entry; |
220 | entry.Form("%s_%d.ddl", AliDAQ::DetectorNameFromDdlID(fDDLCurrent, dummy), fDDLCurrent); |
dd9a70fe |
221 | char* fileName = gSystem->ConcatFileName(GetDirName(), entry); |
df3a33be |
222 | if (!fileName) continue; |
be974aae |
223 | // read the timestamp |
224 | FileStat_t buf; |
225 | if (gSystem->GetPathInfo(fileName,buf) == 0) { |
226 | fTimestamp = buf.fMtime; |
227 | } |
04fa961a |
228 | #ifndef __DECCXX |
39f9963f |
229 | fStream = new fstream(fileName, ios::binary|ios::in); |
04fa961a |
230 | #else |
39f9963f |
231 | fStream = new fstream(fileName, ios::in); |
04fa961a |
232 | #endif |
df3a33be |
233 | delete [] fileName; |
39f9963f |
234 | break; |
235 | } |
236 | |
237 | if (!fStream) return kFALSE; |
df3a33be |
238 | fEquipmentId = fDDLCurrent; |
04fa961a |
239 | #if defined(__HP_aCC) || defined(__DECCXX) |
240 | return (fStream->rdbuf()->is_open()); |
241 | #else |
242 | return (fStream->is_open()); |
243 | #endif |
244 | } |
245 | |
246 | |
39f9963f |
247 | Bool_t AliRawReaderFile::ReadHeader() |
04fa961a |
248 | { |
39f9963f |
249 | // read a data header at the current stream position |
04fa961a |
250 | // returns kFALSE if the mini header could not be read |
251 | |
df3a33be |
252 | if (!fStream && !OpenNextFile()) return kFALSE; |
04fa961a |
253 | do { |
254 | if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount); |
1c10cde6 |
255 | if (fHeader) { |
256 | while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) { |
257 | if (!OpenNextFile()) return kFALSE; |
258 | } |
04fa961a |
259 | } |
cdd735d9 |
260 | else { |
261 | if (fStream->eof()) |
262 | if (!OpenNextFile()) return kFALSE; |
263 | } |
1c10cde6 |
264 | if (fHeader && fHeader->fSize != 0xFFFFFFFF) { |
39f9963f |
265 | fCount = fHeader->fSize - sizeof(AliRawDataHeader); |
266 | } else { |
267 | UInt_t currentPos = fStream->tellg(); |
268 | fStream->seekg(0, ios::end); |
269 | fCount = UInt_t(fStream->tellg()) - currentPos; |
270 | fStream->seekg(currentPos); |
271 | } |
1c10cde6 |
272 | fEquipmentSize = fCount; |
273 | if (fHeader) fEquipmentSize += sizeof(AliRawDataHeader); |
04fa961a |
274 | } while (!IsSelected()); |
275 | return kTRUE; |
276 | } |
277 | |
278 | Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data) |
279 | { |
280 | // reads the next payload at the current stream position |
281 | // returns kFALSE if the data could not be read |
282 | |
283 | while (fCount == 0) { |
39f9963f |
284 | if (!ReadHeader()) return kFALSE; |
04fa961a |
285 | } |
286 | if (fBufferSize < fCount) { |
287 | if (fBuffer) delete[] fBuffer; |
288 | fBufferSize = Int_t(fCount*1.2); |
289 | fBuffer = new UChar_t[fBufferSize]; |
290 | } |
291 | if (!fStream->read((char*) fBuffer, fCount)) { |
292 | Error("ReadNext", "could not read data!"); |
293 | return kFALSE; |
294 | } |
295 | fCount = 0; |
296 | |
297 | data = fBuffer; |
298 | return kTRUE; |
299 | } |
300 | |
301 | Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size) |
302 | { |
303 | // reads the next block of data at the current stream position |
304 | // returns kFALSE if the data could not be read |
305 | |
306 | if (!fStream->read((char*) data, size)) { |
307 | Error("ReadNext", "could not read data!"); |
308 | return kFALSE; |
309 | } |
310 | fCount -= size; |
311 | return kTRUE; |
312 | } |
313 | |
314 | |
315 | Bool_t AliRawReaderFile::Reset() |
316 | { |
dd9a70fe |
317 | // reset the current stream position to the first DDL file of the curevent |
04fa961a |
318 | |
dd9a70fe |
319 | void* directory = OpenDirectory(); |
320 | if (!directory) return kFALSE; |
39f9963f |
321 | |
322 | if (fStream) { |
04fa961a |
323 | #if defined(__HP_aCC) || defined(__DECCXX) |
324 | if (fStream->rdbuf()->is_open()) fStream->close(); |
325 | #else |
326 | if (fStream->is_open()) fStream->close(); |
327 | #endif |
328 | delete fStream; |
329 | fStream = NULL; |
04fa961a |
330 | } |
331 | |
39f9963f |
332 | if (fDirectory) gSystem->FreeDirectory(fDirectory); |
333 | fDirectory = directory; |
04fa961a |
334 | |
df3a33be |
335 | // Matthias 05.06.2008 |
336 | // do not open the next file. That might collide with a subsequent |
337 | // SelectEquipment call as the 'file pointer' is already set. |
338 | // This is important for the indexing of the DDL files. |
339 | // --------------------------------------------------------- |
340 | // All ReadNext functions first require the fCount member to be |
341 | // non zero or call ReadHeader. That allows to postpone the call |
342 | // to OpenNextFile to the next invocation of ReadHeader. |
343 | // ReadHeader has been mofified according to that. |
344 | /* |
39f9963f |
345 | OpenNextFile(); |
df3a33be |
346 | */ |
347 | fEquipmentId=-1; |
348 | fDDLCurrent=-1; |
04fa961a |
349 | fCount = 0; |
350 | return kTRUE; |
351 | } |
352 | |
dd9a70fe |
353 | Bool_t AliRawReaderFile::NextEvent() |
354 | { |
355 | // go to the next event directory |
356 | |
df3a33be |
357 | if (fDDLIndex) delete fDDLIndex; |
358 | fDDLIndex=NULL; |
4ba28796 |
359 | fDetectorPattern = 0; |
dd9a70fe |
360 | if (fEventIndex < -1) return kFALSE; |
361 | |
362 | do { |
363 | TString dirName = fDirName + "/raw"; |
364 | dirName += (fEventIndex + 1); |
365 | void* directory = gSystem->OpenDirectory(dirName); |
366 | if (!directory) return kFALSE; |
367 | gSystem->FreeDirectory(directory); |
368 | |
369 | fEventIndex++; |
370 | Reset(); |
371 | } while (!IsEventSelected()); |
372 | |
be974aae |
373 | // Read the header of the first payload |
374 | // in order to fill the 'fake' event header |
375 | if (ReadHeader() && fHeader) { |
376 | fId[0] = ((fHeader->GetEventID2() >> 20) & 0xf); |
377 | fId[1] = (fHeader->GetEventID1() & 0xfff) | ((fHeader->GetEventID2() & 0xfffff) << 12); |
378 | fTriggerPattern[0] = (fHeader->GetTriggerClasses() & 0xffffffff); |
379 | fTriggerPattern[1] = ((fHeader->GetTriggerClasses() >> 32) & 0x3ffff); |
380 | } |
381 | else { |
382 | Warning("AliRawReaderFile","Can not read CDH header! The event header fields will be empty!"); |
383 | } |
384 | Reset(); |
385 | |
38cf12f3 |
386 | fEventNumber++; |
387 | |
dd9a70fe |
388 | return kTRUE; |
389 | } |
390 | |
391 | Bool_t AliRawReaderFile::RewindEvents() |
392 | { |
393 | // reset the event counter |
394 | |
395 | if (fEventIndex >= 0) fEventIndex = -1; |
38cf12f3 |
396 | fEventNumber = -1; |
dd9a70fe |
397 | return Reset(); |
398 | } |