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 from a root file and providing |
19 | // information about digits |
20 | // |
21 | /////////////////////////////////////////////////////////////////////////////// |
22 | |
23 | #include "AliRawReaderRoot.h" |
42d20574 |
24 | #include "AliRawEvent.h" |
04fa961a |
25 | |
26 | |
27 | ClassImp(AliRawReaderRoot) |
28 | |
29 | |
30 | AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) |
31 | { |
32 | // create an object to read digits from the given input file for the |
33 | // event with the given number |
34 | |
be50fca2 |
35 | fEvent = NULL; |
03c6d9a3 |
36 | TDirectory* dir = gDirectory; |
04fa961a |
37 | fFile = TFile::Open(fileName); |
03c6d9a3 |
38 | dir->cd(); |
04fa961a |
39 | if (!fFile || !fFile->IsOpen()) { |
40 | Error("AliRawReaderRoot", "could not open file %s", fileName); |
41 | return; |
42 | } |
43 | TTree* tree = (TTree*) fFile->Get("RAW"); |
44 | if (!tree) { |
45 | Error("AliRawReaderRoot", "no raw data tree found"); |
46 | return; |
47 | } |
48 | TBranch* branch = tree->GetBranch("rawevent"); |
49 | if (!branch) { |
50 | Error("AliRawReaderRoot", "no raw data branch found"); |
51 | return; |
52 | } |
53 | |
54 | fEvent = new AliRawEvent; |
55 | branch->SetAddress(&fEvent); |
56 | if (branch->GetEntry(eventNumber) <= 0) { |
57 | Error("AliRawReaderRoot", "no event with number %d found", eventNumber); |
58 | return; |
59 | } |
60 | |
61 | fSubEventIndex = 0; |
62 | fSubEvent = NULL; |
63 | fRawData = NULL; |
64 | fMiniHeader = NULL; |
65 | |
66 | fCount = 0; |
67 | fPosition = fEnd = NULL; |
68 | } |
69 | |
70 | AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) |
71 | { |
72 | // create an object to read digits from the given raw event |
73 | |
74 | fFile = NULL; |
75 | fEvent = event; |
76 | |
77 | fSubEventIndex = 0; |
78 | fSubEvent = NULL; |
79 | fRawData = NULL; |
42d20574 |
80 | fMiniHeader = NULL; |
04fa961a |
81 | |
82 | fCount = 0; |
83 | fPosition = fEnd = NULL; |
84 | } |
85 | |
42d20574 |
86 | AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) : |
87 | AliRawReader(rawReader) |
88 | { |
89 | // copy constructor |
90 | |
91 | fFile = NULL; |
92 | fEvent = rawReader.fEvent; |
93 | |
94 | fSubEventIndex = rawReader.fSubEventIndex; |
95 | fSubEvent = rawReader.fSubEvent; |
96 | fRawData = rawReader.fRawData; |
97 | fMiniHeader = rawReader.fMiniHeader; |
98 | |
99 | fCount = rawReader.fCount; |
100 | fPosition = rawReader.fPosition; |
101 | fEnd = rawReader.fEnd; |
102 | } |
103 | |
104 | AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& |
105 | rawReader) |
106 | { |
107 | // assignment operator |
108 | |
109 | this->~AliRawReaderRoot(); |
110 | new(this) AliRawReaderRoot(rawReader); |
111 | return *this; |
112 | } |
113 | |
04fa961a |
114 | AliRawReaderRoot::~AliRawReaderRoot() |
115 | { |
116 | // delete objects and close root file |
117 | |
118 | if (fFile) { |
119 | if (fEvent) delete fEvent; |
120 | fFile->Close(); |
121 | delete fFile; |
122 | } |
123 | } |
124 | |
125 | |
42d20574 |
126 | UInt_t AliRawReaderRoot::GetType() const |
04fa961a |
127 | { |
128 | // get the type from the event header |
129 | |
130 | if (!fEvent) return 0; |
131 | return fEvent->GetHeader()->GetType(); |
132 | } |
133 | |
42d20574 |
134 | UInt_t AliRawReaderRoot::GetRunNumber() const |
04fa961a |
135 | { |
136 | // get the run number from the event header |
137 | |
138 | if (!fEvent) return 0; |
139 | return fEvent->GetHeader()->GetRunNumber(); |
140 | } |
141 | |
42d20574 |
142 | const UInt_t* AliRawReaderRoot::GetEventId() const |
04fa961a |
143 | { |
144 | // get the event id from the event header |
145 | |
146 | if (!fEvent) return NULL; |
147 | return fEvent->GetHeader()->GetId(); |
148 | } |
149 | |
42d20574 |
150 | const UInt_t* AliRawReaderRoot::GetTriggerPattern() const |
04fa961a |
151 | { |
152 | // get the trigger pattern from the event header |
153 | |
154 | if (!fEvent) return NULL; |
155 | return fEvent->GetHeader()->GetTriggerPattern(); |
156 | } |
157 | |
42d20574 |
158 | const UInt_t* AliRawReaderRoot::GetDetectorPattern() const |
04fa961a |
159 | { |
160 | // get the detector pattern from the event header |
161 | |
162 | if (!fEvent) return NULL; |
163 | return fEvent->GetHeader()->GetDetectorPattern(); |
164 | } |
165 | |
42d20574 |
166 | const UInt_t* AliRawReaderRoot::GetAttributes() const |
04fa961a |
167 | { |
168 | // get the type attributes from the event header |
169 | |
170 | if (!fEvent) return NULL; |
171 | return fEvent->GetHeader()->GetTypeAttribute(); |
172 | } |
173 | |
42d20574 |
174 | UInt_t AliRawReaderRoot::GetGDCId() const |
04fa961a |
175 | { |
176 | // get the GDC Id from the event header |
177 | |
178 | if (!fEvent) return 0; |
179 | return fEvent->GetHeader()->GetGDCId(); |
180 | } |
181 | |
182 | |
183 | Bool_t AliRawReaderRoot::ReadMiniHeader() |
184 | { |
185 | // read a mini header at the current position |
186 | // returns kFALSE if the mini header could not be read |
187 | |
b4857df7 |
188 | fErrorCode = 0; |
04fa961a |
189 | if (!fEvent) return kFALSE; |
b4857df7 |
190 | |
04fa961a |
191 | do { |
b4857df7 |
192 | // skip payload (if event was not selected) |
193 | if (fCount > 0) fPosition += fCount; |
194 | |
195 | // get the first or the next sub event if at the end of a sub event |
196 | if (!fSubEvent || (fPosition >= fEnd)) { |
197 | |
198 | // check for end of event data |
04fa961a |
199 | if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE; |
200 | fSubEvent = fEvent->GetSubEvent(fSubEventIndex++); |
b4857df7 |
201 | |
202 | // check the magic word of the sub event |
203 | if (!fSubEvent->GetHeader()->IsValid()) { |
204 | Error("ReadMiniHeader", "wrong magic number in sub event!"); |
205 | fSubEvent->GetHeader()->Dump(); |
206 | fErrorCode = kErrMagic; |
207 | return kFALSE; |
208 | } |
209 | |
04fa961a |
210 | fRawData = fSubEvent->GetRawData(); |
211 | fCount = 0; |
212 | fPosition = (UChar_t*) fRawData->GetBuffer(); |
213 | fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize(); |
214 | } |
b4857df7 |
215 | |
216 | // continue with the next sub event if no data left in the payload |
217 | if (fPosition >= fEnd) continue; |
218 | |
219 | // check that there are enough bytes left for the mini header |
04fa961a |
220 | if (fPosition + sizeof(AliMiniHeader) > fEnd) { |
b4857df7 |
221 | Error("ReadMiniHeader", "could not read mini header data!"); |
222 | Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition); |
223 | fSubEvent->GetHeader()->Dump(); |
224 | fCount = 0; |
225 | fPosition = fEnd; |
226 | fErrorCode = kErrNoMiniHeader; |
227 | continue; |
04fa961a |
228 | } |
b4857df7 |
229 | |
230 | // "read" and check the mini header |
04fa961a |
231 | fMiniHeader = (AliMiniHeader*) fPosition; |
232 | fPosition += sizeof(AliMiniHeader); |
72dd1d4f |
233 | if (!CheckMiniHeader()) { |
b4857df7 |
234 | Error("ReadMiniHeader", "wrong magic word in mini header!"); |
72dd1d4f |
235 | Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition); |
236 | fSubEvent->GetHeader()->Dump(); |
237 | fCount = 0; |
238 | fPosition = fEnd; |
b4857df7 |
239 | fErrorCode = kErrMiniMagic; |
72dd1d4f |
240 | continue; |
241 | } |
04fa961a |
242 | fCount = fMiniHeader->fSize; |
b4857df7 |
243 | |
244 | // check consistency of data size in the mini header and in the sub event |
245 | if (fPosition + fCount > fEnd) { |
04fa961a |
246 | Error("ReadMiniHeader", "size in mini header exceeds event size!"); |
03c6d9a3 |
247 | Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition); |
248 | fSubEvent->GetHeader()->Dump(); |
249 | fCount = 0; |
250 | fPosition = fEnd; |
b4857df7 |
251 | fErrorCode = kErrSize; |
03c6d9a3 |
252 | continue; |
04fa961a |
253 | } |
b4857df7 |
254 | |
04fa961a |
255 | } while (!IsSelected()); |
b4857df7 |
256 | |
04fa961a |
257 | return kTRUE; |
258 | } |
259 | |
260 | Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data) |
261 | { |
262 | // reads the next payload at the current position |
263 | // returns kFALSE if the data could not be read |
264 | |
b4857df7 |
265 | fErrorCode = 0; |
04fa961a |
266 | while (fCount == 0) { |
267 | if (!ReadMiniHeader()) return kFALSE; |
268 | } |
269 | data = fPosition; |
270 | fPosition += fCount; |
271 | fCount = 0; |
272 | return kTRUE; |
273 | } |
274 | |
275 | Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size) |
276 | { |
277 | // reads the next block of data at the current position |
278 | // returns kFALSE if the data could not be read |
279 | |
b4857df7 |
280 | fErrorCode = 0; |
04fa961a |
281 | if (fPosition + size > fEnd) { |
282 | Error("ReadNext", "could not read data!"); |
b4857df7 |
283 | fErrorCode = kErrOutOfBounds; |
04fa961a |
284 | return kFALSE; |
285 | } |
286 | memcpy(data, fPosition, size); |
287 | fPosition += size; |
288 | fCount -= size; |
289 | return kTRUE; |
290 | } |
291 | |
292 | |
293 | Bool_t AliRawReaderRoot::Reset() |
294 | { |
295 | // reset the current position to the beginning of the event |
296 | |
297 | fSubEventIndex = 0; |
298 | fSubEvent = NULL; |
299 | fRawData = NULL; |
300 | fMiniHeader = NULL; |
301 | |
302 | fCount = 0; |
303 | fPosition = fEnd = NULL; |
304 | return kTRUE; |
305 | } |
306 | |
b4857df7 |
307 | |
308 | Int_t AliRawReaderRoot::CheckData() const |
309 | { |
310 | // check the consistency of the data |
311 | |
312 | if (!fEvent) return 0; |
313 | |
314 | AliRawEvent* subEvent = NULL; |
315 | Int_t subEventIndex = 0; |
316 | UChar_t* position = 0; |
317 | UChar_t* end = 0; |
be50fca2 |
318 | Int_t result = 0; |
b4857df7 |
319 | |
320 | while (kTRUE) { |
321 | // get the first or the next sub event if at the end of a sub event |
322 | if (!subEvent || (position >= end)) { |
323 | |
324 | // check for end of event data |
be50fca2 |
325 | if (subEventIndex >= fEvent->GetNSubEvents()) return result; |
b4857df7 |
326 | subEvent = fEvent->GetSubEvent(subEventIndex++); |
327 | |
328 | // check the magic word of the sub event |
be50fca2 |
329 | if (!fSubEvent->GetHeader()->IsValid()) { |
330 | result |= kErrMagic; |
331 | return result; |
332 | } |
b4857df7 |
333 | |
334 | AliRawData* rawData = subEvent->GetRawData(); |
335 | position = (UChar_t*) rawData->GetBuffer(); |
336 | end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize(); |
337 | } |
338 | |
339 | // continue with the next sub event if no data left in the payload |
340 | if (position >= end) continue; |
341 | |
342 | // check that there are enough bytes left for the mini header |
be50fca2 |
343 | if (position + sizeof(AliMiniHeader) > end) { |
344 | result |= kErrNoMiniHeader; |
345 | position = end; |
346 | continue; |
347 | } |
b4857df7 |
348 | |
349 | // "read" and check the mini header |
350 | AliMiniHeader* miniHeader = (AliMiniHeader*) position; |
351 | position += sizeof(AliMiniHeader); |
be50fca2 |
352 | if (!CheckMiniHeader(miniHeader)) { |
353 | result |= kErrMiniMagic; |
354 | position = end; |
355 | continue; |
356 | } |
b4857df7 |
357 | |
358 | // check consistency of data size in the mini header and in the sub event |
be50fca2 |
359 | if (position + miniHeader->fSize > end) result |= kErrSize; |
b4857df7 |
360 | position += miniHeader->fSize; |
361 | }; |
fd0de2e2 |
362 | |
363 | return result; |
b4857df7 |
364 | } |