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 | |
16 | /////////////////////////////////////////////////////////////////////////////// |
17 | // |
18 | // This is a class for reading a raw data file and providing |
19 | // information about digits |
20 | // |
21 | /////////////////////////////////////////////////////////////////////////////// |
22 | |
23 | #include "AliRawReaderFile.h" |
39f9963f |
24 | #include <TSystem.h> |
04fa961a |
25 | |
26 | |
27 | ClassImp(AliRawReaderFile) |
28 | |
29 | |
39f9963f |
30 | AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) : |
31 | fDirName("raw"), |
32 | fDirectory(NULL), |
33 | fStream(NULL), |
34 | fEquipmentId(-1), |
35 | fBuffer(NULL), |
36 | fBufferSize(0) |
04fa961a |
37 | { |
39f9963f |
38 | // create an object to read digits from the given event |
04fa961a |
39 | |
39f9963f |
40 | fDirName += eventNumber; |
41 | fDirectory = gSystem->OpenDirectory(fDirName); |
42 | if (!fDirectory) { |
43 | Error("AliRawReaderFile", "could not open directory %s", fDirName.Data()); |
44 | } |
45 | OpenNextFile(); |
46 | fHeader = new AliRawDataHeader; |
47 | } |
48 | |
49 | AliRawReaderFile::AliRawReaderFile(const char* dirName) : |
50 | fDirName(dirName), |
51 | fDirectory(NULL), |
52 | fStream(NULL), |
53 | fEquipmentId(-1), |
54 | fBuffer(NULL), |
55 | fBufferSize(0) |
56 | { |
57 | // create an object to read digits from the given directory |
58 | |
59 | fDirectory = gSystem->OpenDirectory(fDirName); |
60 | if (!fDirectory) { |
61 | Error("AliRawReaderFile", "could not open directory %s", fDirName.Data()); |
04fa961a |
62 | } |
39f9963f |
63 | OpenNextFile(); |
64 | fHeader = new AliRawDataHeader; |
04fa961a |
65 | } |
66 | |
42d20574 |
67 | AliRawReaderFile::AliRawReaderFile(const AliRawReaderFile& rawReader) : |
68 | AliRawReader(rawReader) |
69 | { |
70 | Fatal("AliRawReaderFile", "copy constructor not implemented"); |
71 | } |
72 | |
73 | AliRawReaderFile& AliRawReaderFile::operator = (const AliRawReaderFile& |
74 | /* rawReader */) |
75 | { |
76 | Fatal("operator =", "assignment operator not implemented"); |
77 | return *this; |
78 | } |
79 | |
04fa961a |
80 | AliRawReaderFile::~AliRawReaderFile() |
81 | { |
82 | // close the input file |
83 | |
39f9963f |
84 | if (fDirectory) gSystem->FreeDirectory(fDirectory); |
04fa961a |
85 | if (fStream) { |
86 | #if defined(__HP_aCC) || defined(__DECCXX) |
87 | if (fStream->rdbuf()->is_open()) fStream->close(); |
88 | #else |
89 | if (fStream->is_open()) fStream->close(); |
90 | #endif |
91 | delete fStream; |
92 | } |
39f9963f |
93 | delete fHeader; |
04fa961a |
94 | if (fBuffer) delete[] fBuffer; |
95 | } |
96 | |
97 | |
98 | Bool_t AliRawReaderFile::OpenNextFile() |
99 | { |
42d20574 |
100 | // open the next file |
101 | // returns kFALSE if the current file is the last one |
102 | |
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 | fStream = NULL; |
39f9963f |
111 | fEquipmentId = -1; |
04fa961a |
112 | } |
04fa961a |
113 | |
39f9963f |
114 | if (!fDirectory) return kFALSE; |
115 | TString entry; |
116 | while (entry = gSystem->GetDirEntry(fDirectory)) { |
117 | if (entry.IsNull()) return kFALSE; |
118 | if (!entry.EndsWith(".ddl")) continue; |
119 | char* fileName = gSystem->ConcatFileName(fDirName, entry); |
04fa961a |
120 | #ifndef __DECCXX |
39f9963f |
121 | fStream = new fstream(fileName, ios::binary|ios::in); |
04fa961a |
122 | #else |
39f9963f |
123 | fStream = new fstream(fileName, ios::in); |
04fa961a |
124 | #endif |
39f9963f |
125 | break; |
126 | } |
127 | |
128 | if (!fStream) return kFALSE; |
129 | entry.Remove(0, entry.Last('_')+1); |
130 | entry.Remove(entry.Length()-4); |
131 | fEquipmentId = atoi(entry.Data()); |
04fa961a |
132 | #if defined(__HP_aCC) || defined(__DECCXX) |
133 | return (fStream->rdbuf()->is_open()); |
134 | #else |
135 | return (fStream->is_open()); |
136 | #endif |
137 | } |
138 | |
139 | |
39f9963f |
140 | Bool_t AliRawReaderFile::ReadHeader() |
04fa961a |
141 | { |
39f9963f |
142 | // read a data header at the current stream position |
04fa961a |
143 | // returns kFALSE if the mini header could not be read |
144 | |
145 | if (!fStream) return kFALSE; |
146 | do { |
147 | if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount); |
39f9963f |
148 | while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) { |
04fa961a |
149 | if (!OpenNextFile()) return kFALSE; |
150 | } |
39f9963f |
151 | if (fHeader->fSize != 0xFFFFFFFF) { |
152 | fCount = fHeader->fSize - sizeof(AliRawDataHeader); |
153 | } else { |
154 | UInt_t currentPos = fStream->tellg(); |
155 | fStream->seekg(0, ios::end); |
156 | fCount = UInt_t(fStream->tellg()) - currentPos; |
157 | fStream->seekg(currentPos); |
158 | } |
04fa961a |
159 | } while (!IsSelected()); |
160 | return kTRUE; |
161 | } |
162 | |
163 | Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data) |
164 | { |
165 | // reads the next payload at the current stream position |
166 | // returns kFALSE if the data could not be read |
167 | |
168 | while (fCount == 0) { |
39f9963f |
169 | if (!ReadHeader()) return kFALSE; |
04fa961a |
170 | } |
171 | if (fBufferSize < fCount) { |
172 | if (fBuffer) delete[] fBuffer; |
173 | fBufferSize = Int_t(fCount*1.2); |
174 | fBuffer = new UChar_t[fBufferSize]; |
175 | } |
176 | if (!fStream->read((char*) fBuffer, fCount)) { |
177 | Error("ReadNext", "could not read data!"); |
178 | return kFALSE; |
179 | } |
180 | fCount = 0; |
181 | |
182 | data = fBuffer; |
183 | return kTRUE; |
184 | } |
185 | |
186 | Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size) |
187 | { |
188 | // reads the next block of data at the current stream position |
189 | // returns kFALSE if the data could not be read |
190 | |
191 | if (!fStream->read((char*) data, size)) { |
192 | Error("ReadNext", "could not read data!"); |
193 | return kFALSE; |
194 | } |
195 | fCount -= size; |
196 | return kTRUE; |
197 | } |
198 | |
199 | |
200 | Bool_t AliRawReaderFile::Reset() |
201 | { |
202 | // reset the current stream position to the beginning of the file |
203 | |
39f9963f |
204 | void* directory = gSystem->OpenDirectory(fDirName); |
205 | if (!directory) { |
206 | Error("Reset", "could not open directory %s", fDirName.Data()); |
207 | return kFALSE; |
208 | } |
209 | |
210 | if (fStream) { |
04fa961a |
211 | #if defined(__HP_aCC) || defined(__DECCXX) |
212 | if (fStream->rdbuf()->is_open()) fStream->close(); |
213 | #else |
214 | if (fStream->is_open()) fStream->close(); |
215 | #endif |
216 | delete fStream; |
217 | fStream = NULL; |
04fa961a |
218 | } |
219 | |
39f9963f |
220 | if (fDirectory) gSystem->FreeDirectory(fDirectory); |
221 | fDirectory = directory; |
04fa961a |
222 | |
39f9963f |
223 | OpenNextFile(); |
04fa961a |
224 | fCount = 0; |
225 | return kTRUE; |
226 | } |
227 | |