Coding conventions.
[u/mrichter/AliRoot.git] / RAW / AliRawReaderDate.cxx
CommitLineData
16048fcf 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
16048fcf 18///////////////////////////////////////////////////////////////////////////////
bea6b2a4 19///
20/// This is a class for reading raw data from a date file or event.
21///
22/// The AliRawReaderDate is constructed either with a pointer to a
23/// date event or with a file name and an event number.
24///
16048fcf 25///////////////////////////////////////////////////////////////////////////////
26
27#include "AliRawReaderDate.h"
28#ifdef ALI_DATE
29#include "event.h"
30#endif
31
32ClassImp(AliRawReaderDate)
33
34
35AliRawReaderDate::AliRawReaderDate(
36#ifdef ALI_DATE
37 void* event
38#else
39 void* /* event */
40#endif
c946ab02 41 ) :
39f9963f 42 fRequireHeader(kTRUE),
c946ab02 43 fEvent(NULL),
44 fSubEvent(NULL),
45 fEquipment(NULL),
46 fIsOwner(kFALSE),
47 fPosition(NULL),
48 fEnd(NULL)
16048fcf 49{
50// create an object to read digits from the given date event
51
52#ifdef ALI_DATE
53 fEvent = (eventHeaderStruct*) event;
c946ab02 54#else
55 Fatal("AliRawReaderDate", "this class was compiled without DATE");
56#endif
57}
58
59AliRawReaderDate::AliRawReaderDate(
60#ifdef ALI_DATE
61 const char* fileName, Int_t eventNumber
62#else
63 const char* /*fileName*/,
64 Int_t /*eventNumber*/
65#endif
66 ) :
39f9963f 67 fRequireHeader(kTRUE),
c946ab02 68 fEvent(NULL),
69 fSubEvent(NULL),
70 fEquipment(NULL),
71 fIsOwner(kFALSE),
72 fPosition(NULL),
73 fEnd(NULL)
74{
75// create an object to read digits from the given date event
76
77#ifdef ALI_DATE
78 FILE* file = fopen(fileName, "rb");
79 if (!file) {
80 Error("AliRawReaderDate", "could not open file %s", fileName);
81 return;
82 }
83 eventHeaderStruct header;
84 UInt_t headerSize = sizeof(eventHeaderStruct);
85 while (fread(&header, 1, headerSize, file) == headerSize) {
86 if (eventNumber == 0) {
87 UChar_t* buffer = new UChar_t[header.eventSize];
88 fseek(file, -headerSize, SEEK_CUR);
89 if (fread(buffer, 1, header.eventSize, file) != header.eventSize) break;
90 fEvent = (eventHeaderStruct*) buffer;
91 fIsOwner = kTRUE;
92 break;
93 }
94 fseek(file, header.eventSize-headerSize, SEEK_CUR);
95 eventNumber--;
96 }
97 fclose(file);
98
16048fcf 99#else
100 Fatal("AliRawReaderDate", "this class was compiled without DATE");
101#endif
102}
103
104AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
c946ab02 105 AliRawReader(rawReader),
39f9963f 106 fRequireHeader(rawReader.fRequireHeader),
c946ab02 107 fEvent(rawReader.fEvent),
108 fSubEvent(rawReader.fSubEvent),
109 fEquipment(rawReader.fEquipment),
110 fIsOwner(kFALSE),
111 fPosition(rawReader.fPosition),
112 fEnd(rawReader.fEnd)
113
16048fcf 114{
115// copy constructor
116
16048fcf 117}
118
119AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
120 rawReader)
121{
122// assignment operator
123
124 this->~AliRawReaderDate();
125 new(this) AliRawReaderDate(rawReader);
126 return *this;
127}
128
c946ab02 129AliRawReaderDate::~AliRawReaderDate()
130{
131// destructor
132
133#ifdef ALI_DATE
134 if (fIsOwner) delete[] fEvent;
135#endif
136}
137
16048fcf 138
139UInt_t AliRawReaderDate::GetType() const
140{
141// get the type from the event header
142
143#ifdef ALI_DATE
144 if (!fEvent) return 0;
145 return fEvent->eventType;
146#else
147 return 0;
148#endif
149}
150
151UInt_t AliRawReaderDate::GetRunNumber() const
152{
153// get the run number from the event header
154
155#ifdef ALI_DATE
156 if (!fEvent) return 0;
157 return fEvent->eventRunNb;
158#else
159 return 0;
160#endif
161}
162
163const UInt_t* AliRawReaderDate::GetEventId() const
164{
165// get the event id from the event header
166
167#ifdef ALI_DATE
168 if (!fEvent) return NULL;
169 return fEvent->eventId;
170#else
171 return NULL;
172#endif
173}
174
175const UInt_t* AliRawReaderDate::GetTriggerPattern() const
176{
177// get the trigger pattern from the event header
178
179#ifdef ALI_DATE
180 if (!fEvent) return NULL;
181 return fEvent->eventTriggerPattern;
182#else
183 return NULL;
184#endif
185}
186
187const UInt_t* AliRawReaderDate::GetDetectorPattern() const
188{
189// get the detector pattern from the event header
190
191#ifdef ALI_DATE
192 if (!fEvent) return NULL;
193 return fEvent->eventDetectorPattern;
194#else
195 return NULL;
196#endif
197}
198
199const UInt_t* AliRawReaderDate::GetAttributes() const
200{
201// get the type attributes from the event header
202
203#ifdef ALI_DATE
204 if (!fEvent) return NULL;
205 return fEvent->eventTypeAttribute;
206#else
207 return NULL;
208#endif
209}
210
c946ab02 211UInt_t AliRawReaderDate::GetLDCId() const
212{
213// get the LDC Id from the event header
214
215#ifdef ALI_DATE
216 if (!fSubEvent) return 0;
217 return fSubEvent->eventLdcId;
218#else
219 return 0;
220#endif
221}
222
16048fcf 223UInt_t AliRawReaderDate::GetGDCId() const
224{
225// get the GDC Id from the event header
226
227#ifdef ALI_DATE
228 if (!fEvent) return 0;
229 return fEvent->eventGdcId;
230#else
231 return 0;
232#endif
233}
234
235
c946ab02 236Int_t AliRawReaderDate::GetEquipmentSize() const
237{
238// get the size of the equipment
239
240#ifdef ALI_DATE
241 if (!fEquipment) return 0;
242 return fEquipment->equipmentSize;
243#else
244 return 0;
245#endif
246}
247
248Int_t AliRawReaderDate::GetEquipmentType() const
249{
250// get the type from the equipment header
251
252#ifdef ALI_DATE
253 if (!fEquipment) return -1;
254 return fEquipment->equipmentType;
255#else
256 return 0;
257#endif
258}
259
260Int_t AliRawReaderDate::GetEquipmentId() const
261{
262// get the ID from the equipment header
263
264#ifdef ALI_DATE
265 if (!fEquipment) return -1;
266 return fEquipment->equipmentId;
267#else
268 return 0;
269#endif
270}
271
272const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
273{
274// get the attributes from the equipment header
275
276#ifdef ALI_DATE
277 if (!fEquipment) return NULL;
278 return fEquipment->equipmentTypeAttribute;
279#else
280 return 0;
281#endif
282}
283
284Int_t AliRawReaderDate::GetEquipmentElementSize() const
285{
286// get the basic element size from the equipment header
287
288#ifdef ALI_DATE
289 if (!fEquipment) return 0;
290 return fEquipment->equipmentBasicElementSize;
291#else
292 return 0;
293#endif
294}
295
296
297Bool_t AliRawReaderDate::ReadHeader()
16048fcf 298{
39f9963f 299// read a data header at the current position
300// returns kFALSE if the data header could not be read
16048fcf 301
b4857df7 302 fErrorCode = 0;
303
16048fcf 304#ifdef ALI_DATE
921a8308 305 fHeader = NULL;
16048fcf 306 if (!fEvent) return kFALSE;
b4857df7 307 // check whether there are sub events
308 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
309
16048fcf 310 do {
b4857df7 311 // skip payload (if event was not selected)
312 if (fCount > 0) fPosition += fCount;
313
c946ab02 314 // get the first or the next equipment if at the end of an equipment
315 if (!fEquipment || (fPosition >= fEnd)) {
921a8308 316 fEquipment = NULL;
c946ab02 317
318 // get the first or the next sub event if at the end of a sub event
319 if (!fSubEvent ||
320 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
321
322 // check for end of event data
323 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
324 if (fSubEvent) {
325 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
326 fSubEvent->eventSize);
327 } else {
328 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
329 fEvent->eventHeadSize);
330 }
331
332 // check the magic word of the sub event
333 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
334 Error("ReadHeader", "wrong magic number in sub event!\n"
335 " run: %d event: %d %d LDC: %d GDC: %d\n",
336 fSubEvent->eventRunNb,
337 fSubEvent->eventId[0], fSubEvent->eventId[1],
338 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
339 fErrorCode = kErrMagic;
340 return kFALSE;
341 }
342
921a8308 343 // continue if no data in the subevent
344 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
345 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
346 fCount = 0;
347 continue;
348 }
349
c946ab02 350 fEquipment = (equipmentHeaderStruct*)
351 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
b4857df7 352
16048fcf 353 } else {
c946ab02 354 fEquipment = (equipmentHeaderStruct*) fEnd;
b4857df7 355 }
356
16048fcf 357 fCount = 0;
c946ab02 358 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
921a8308 359 if (fSubEvent->eventVersion <= 0x00030002) {
360 fEnd = fPosition + fEquipment->equipmentSize;
361 } else {
362 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
363 }
16048fcf 364 }
b4857df7 365
366 // continue with the next sub event if no data left in the payload
367 if (fPosition >= fEnd) continue;
368
39f9963f 369 if (fRequireHeader) {
370 // check that there are enough bytes left for the data header
371 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
372 Error("ReadHeader", "could not read data header data!");
c946ab02 373 Warning("ReadHeader", "skipping %d bytes\n"
374 " run: %d event: %d %d LDC: %d GDC: %d\n",
375 fEnd - fPosition, fSubEvent->eventRunNb,
376 fSubEvent->eventId[0], fSubEvent->eventId[1],
377 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
378 fCount = 0;
379 fPosition = fEnd;
39f9963f 380 fErrorCode = kErrNoDataHeader;
c946ab02 381 continue;
382 }
b4857df7 383
39f9963f 384 // "read" the data header
385 fHeader = (AliRawDataHeader*) fPosition;
386 fPosition += sizeof(AliRawDataHeader);
72dd1d4f 387 }
c946ab02 388
39f9963f 389 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
390 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
c946ab02 391
39f9963f 392 // check consistency of data size in the header and in the sub event
c946ab02 393 if (fPosition + fCount > fEnd) {
39f9963f 394 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 395 Warning("ReadHeader", "skipping %d bytes\n"
396 " run: %d event: %d %d LDC: %d GDC: %d\n",
397 fEnd - fPosition, fSubEvent->eventRunNb,
398 fSubEvent->eventId[0], fSubEvent->eventId[1],
399 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
400 fCount = 0;
401 fPosition = fEnd;
402 fErrorCode = kErrSize;
403 continue;
404 }
405
406 } else {
407 fCount = fEnd - fPosition;
16048fcf 408 }
b4857df7 409
921a8308 410 } while (!fEquipment || !IsSelected());
b4857df7 411
16048fcf 412 return kTRUE;
413#else
414 return kFALSE;
415#endif
416}
417
418Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
419{
420// reads the next payload at the current position
421// returns kFALSE if the data could not be read
422
b4857df7 423 fErrorCode = 0;
16048fcf 424 while (fCount == 0) {
c946ab02 425 if (!ReadHeader()) return kFALSE;
16048fcf 426 }
427 data = fPosition;
428 fPosition += fCount;
429 fCount = 0;
430 return kTRUE;
431}
432
433Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
434{
435// reads the next block of data at the current position
436// returns kFALSE if the data could not be read
437
b4857df7 438 fErrorCode = 0;
16048fcf 439 if (fPosition + size > fEnd) {
440 Error("ReadNext", "could not read data!");
b4857df7 441 fErrorCode = kErrOutOfBounds;
16048fcf 442 return kFALSE;
443 }
444 memcpy(data, fPosition, size);
445 fPosition += size;
446 fCount -= size;
447 return kTRUE;
448}
449
450
451Bool_t AliRawReaderDate::Reset()
452{
453// reset the current position to the beginning of the event
454
455#ifdef ALI_DATE
456 fSubEvent = NULL;
457#endif
458 fCount = 0;
459 fPosition = fEnd = NULL;
460 return kTRUE;
461}
b4857df7 462
463
464Int_t AliRawReaderDate::CheckData() const
465{
466// check the consistency of the data
467
468#ifdef ALI_DATE
469 if (!fEvent) return 0;
470 // check whether there are sub events
471 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
472
473 eventHeaderStruct* subEvent = NULL;
474 UChar_t* position = 0;
475 UChar_t* end = 0;
be50fca2 476 Int_t result = 0;
b4857df7 477
478 while (kTRUE) {
479 // get the first or the next sub event if at the end of a sub event
480 if (!subEvent || (position >= end)) {
481
482 // check for end of event data
be50fca2 483 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
b4857df7 484 if (subEvent) {
485 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
486 subEvent->eventSize);
487 } else {
488 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
489 fEvent->eventHeadSize);
490 }
491
492 // check the magic word of the sub event
be50fca2 493 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
494 result |= kErrMagic;
495 return result;
496 }
b4857df7 497
498 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
499 sizeof(equipmentHeaderStruct);
500 end = ((UChar_t*)subEvent) + subEvent->eventSize;
501 }
502
503 // continue with the next sub event if no data left in the payload
504 if (position >= end) continue;
505
39f9963f 506 // check that there are enough bytes left for the data header
507 if (position + sizeof(AliRawDataHeader) > end) {
508 result |= kErrNoDataHeader;
be50fca2 509 position = end;
510 continue;
511 }
b4857df7 512
39f9963f 513 // check consistency of data size in the data header and in the sub event
514 AliRawDataHeader* header = (AliRawDataHeader*) position;
086fa880 515 if (header->fSize != 0xFFFFFFFF) {
39f9963f 516 if (position + header->fSize > end) {
517 result |= kErrSize;
518 position = end;
519 } else {
520 position += header->fSize;
521 }
522 } else {
be50fca2 523 position = end;
be50fca2 524 }
b4857df7 525 };
526
527#endif
528 return 0;
529}