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