Adding a method to access the event timestamp - GetTimestamp(). The method does not...
[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 ) :
dd9a70fe 42 fFile(NULL),
c946ab02 43 fEvent(NULL),
44 fSubEvent(NULL),
45 fEquipment(NULL),
c946ab02 46 fPosition(NULL),
47 fEnd(NULL)
16048fcf 48{
49// create an object to read digits from the given date event
50
51#ifdef ALI_DATE
52 fEvent = (eventHeaderStruct*) event;
c946ab02 53#else
54 Fatal("AliRawReaderDate", "this class was compiled without DATE");
55#endif
56}
57
58AliRawReaderDate::AliRawReaderDate(
59#ifdef ALI_DATE
60 const char* fileName, Int_t eventNumber
61#else
62 const char* /*fileName*/,
63 Int_t /*eventNumber*/
64#endif
65 ) :
dd9a70fe 66 fFile(NULL),
c946ab02 67 fEvent(NULL),
68 fSubEvent(NULL),
69 fEquipment(NULL),
c946ab02 70 fPosition(NULL),
71 fEnd(NULL)
72{
73// create an object to read digits from the given date event
74
75#ifdef ALI_DATE
dd9a70fe 76 fFile = fopen(fileName, "rb");
77 if (!fFile) {
c946ab02 78 Error("AliRawReaderDate", "could not open file %s", fileName);
79 return;
80 }
dd9a70fe 81 if (eventNumber < 0) return;
82
c946ab02 83 eventHeaderStruct header;
84 UInt_t headerSize = sizeof(eventHeaderStruct);
dd9a70fe 85 while (fread(&header, 1, headerSize, fFile) == headerSize) {
c946ab02 86 if (eventNumber == 0) {
87 UChar_t* buffer = new UChar_t[header.eventSize];
dd9a70fe 88 fseek(fFile, -headerSize, SEEK_CUR);
89 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
c946ab02 90 fEvent = (eventHeaderStruct*) buffer;
c946ab02 91 break;
92 }
dd9a70fe 93 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
c946ab02 94 eventNumber--;
95 }
c946ab02 96
16048fcf 97#else
98 Fatal("AliRawReaderDate", "this class was compiled without DATE");
99#endif
100}
101
102AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
c946ab02 103 AliRawReader(rawReader),
dd9a70fe 104 fFile(rawReader.fFile),
c946ab02 105 fEvent(rawReader.fEvent),
106 fSubEvent(rawReader.fSubEvent),
107 fEquipment(rawReader.fEquipment),
c946ab02 108 fPosition(rawReader.fPosition),
109 fEnd(rawReader.fEnd)
110
16048fcf 111{
112// copy constructor
113
dd9a70fe 114 Fatal("AliRawReaderDate", "copy constructor not implemented");
16048fcf 115}
116
117AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
dd9a70fe 118 /*rawReader*/)
16048fcf 119{
120// assignment operator
121
dd9a70fe 122 Fatal("operator =", "assignment operator not implemented");
16048fcf 123 return *this;
124}
125
c946ab02 126AliRawReaderDate::~AliRawReaderDate()
127{
128// destructor
129
130#ifdef ALI_DATE
dd9a70fe 131 if (fFile) {
132 delete[] fEvent;
133 fclose(fFile);
134 }
c946ab02 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
e94ad92c 211const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
212{
213// get the type attributes from the sub event header
214
215#ifdef ALI_DATE
216 if (!fSubEvent) return NULL;
217 return fSubEvent->eventTypeAttribute;
218#else
219 return NULL;
220#endif
221}
222
c946ab02 223UInt_t AliRawReaderDate::GetLDCId() const
224{
225// get the LDC Id from the event header
226
227#ifdef ALI_DATE
228 if (!fSubEvent) return 0;
229 return fSubEvent->eventLdcId;
230#else
231 return 0;
232#endif
233}
234
16048fcf 235UInt_t AliRawReaderDate::GetGDCId() const
236{
237// get the GDC Id from the event header
238
239#ifdef ALI_DATE
240 if (!fEvent) return 0;
241 return fEvent->eventGdcId;
242#else
243 return 0;
244#endif
245}
246
741c154c 247UInt_t AliRawReaderDate::GetTimestamp() const
248{
249// get the timestamp from the event header
250
251#ifdef ALI_DATE
252 if (!fEvent) return 0;
253 return fEvent->eventTimestamp;
254#else
255 return 0;
256#endif
257}
16048fcf 258
c946ab02 259Int_t AliRawReaderDate::GetEquipmentSize() const
260{
299738b9 261// get the size of the equipment (including the header)
c946ab02 262
263#ifdef ALI_DATE
264 if (!fEquipment) return 0;
87db01b0 265 if (fSubEvent->eventVersion <= 0x00030001) {
299738b9 266 return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
87db01b0 267 } else {
299738b9 268 return fEquipment->equipmentSize;
87db01b0 269 }
c946ab02 270#else
271 return 0;
272#endif
273}
274
275Int_t AliRawReaderDate::GetEquipmentType() const
276{
277// get the type from the equipment header
278
279#ifdef ALI_DATE
280 if (!fEquipment) return -1;
281 return fEquipment->equipmentType;
282#else
283 return 0;
284#endif
285}
286
287Int_t AliRawReaderDate::GetEquipmentId() const
288{
289// get the ID from the equipment header
290
291#ifdef ALI_DATE
292 if (!fEquipment) return -1;
293 return fEquipment->equipmentId;
294#else
295 return 0;
296#endif
297}
298
299const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
300{
301// get the attributes from the equipment header
302
303#ifdef ALI_DATE
304 if (!fEquipment) return NULL;
305 return fEquipment->equipmentTypeAttribute;
306#else
307 return 0;
308#endif
309}
310
311Int_t AliRawReaderDate::GetEquipmentElementSize() const
312{
313// get the basic element size from the equipment header
314
315#ifdef ALI_DATE
316 if (!fEquipment) return 0;
317 return fEquipment->equipmentBasicElementSize;
318#else
319 return 0;
320#endif
321}
322
299738b9 323Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
324{
325 // Get the equipment header size
326 // 28 bytes by default
327#ifdef ALI_DATE
328 return sizeof(equipmentHeaderStruct);
329#else
330 return 0;
331#endif
332}
c946ab02 333
334Bool_t AliRawReaderDate::ReadHeader()
16048fcf 335{
39f9963f 336// read a data header at the current position
337// returns kFALSE if the data header could not be read
16048fcf 338
b4857df7 339 fErrorCode = 0;
340
16048fcf 341#ifdef ALI_DATE
921a8308 342 fHeader = NULL;
16048fcf 343 if (!fEvent) return kFALSE;
b4857df7 344 // check whether there are sub events
345 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
346
16048fcf 347 do {
b4857df7 348 // skip payload (if event was not selected)
349 if (fCount > 0) fPosition += fCount;
350
c946ab02 351 // get the first or the next equipment if at the end of an equipment
352 if (!fEquipment || (fPosition >= fEnd)) {
921a8308 353 fEquipment = NULL;
c946ab02 354
355 // get the first or the next sub event if at the end of a sub event
356 if (!fSubEvent ||
357 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
358
359 // check for end of event data
360 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
5ba96ac1 361 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
362 ATTR_SUPER_EVENT)) {
363 fSubEvent = fEvent; // no super event
364 } else if (fSubEvent) {
c946ab02 365 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
366 fSubEvent->eventSize);
367 } else {
368 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
369 fEvent->eventHeadSize);
370 }
371
372 // check the magic word of the sub event
373 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
374 Error("ReadHeader", "wrong magic number in sub event!\n"
375 " run: %d event: %d %d LDC: %d GDC: %d\n",
376 fSubEvent->eventRunNb,
377 fSubEvent->eventId[0], fSubEvent->eventId[1],
378 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
379 fErrorCode = kErrMagic;
380 return kFALSE;
381 }
382
921a8308 383 // continue if no data in the subevent
384 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
385 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
386 fCount = 0;
387 continue;
388 }
389
c946ab02 390 fEquipment = (equipmentHeaderStruct*)
391 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
b4857df7 392
16048fcf 393 } else {
c946ab02 394 fEquipment = (equipmentHeaderStruct*) fEnd;
b4857df7 395 }
396
16048fcf 397 fCount = 0;
c946ab02 398 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
87db01b0 399 if (fSubEvent->eventVersion <= 0x00030001) {
921a8308 400 fEnd = fPosition + fEquipment->equipmentSize;
401 } else {
402 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
403 }
16048fcf 404 }
b4857df7 405
406 // continue with the next sub event if no data left in the payload
407 if (fPosition >= fEnd) continue;
408
39f9963f 409 if (fRequireHeader) {
410 // check that there are enough bytes left for the data header
411 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
412 Error("ReadHeader", "could not read data header data!");
c946ab02 413 Warning("ReadHeader", "skipping %d bytes\n"
414 " run: %d event: %d %d LDC: %d GDC: %d\n",
415 fEnd - fPosition, fSubEvent->eventRunNb,
416 fSubEvent->eventId[0], fSubEvent->eventId[1],
417 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
418 fCount = 0;
419 fPosition = fEnd;
39f9963f 420 fErrorCode = kErrNoDataHeader;
c946ab02 421 continue;
422 }
b4857df7 423
39f9963f 424 // "read" the data header
425 fHeader = (AliRawDataHeader*) fPosition;
299738b9 426 if ((fPosition + fHeader->fSize) != fEnd) {
427 Warning("ReadHeader",
428 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
429 fHeader->fSize, fEnd - fPosition);
430 fHeader->fSize = fEnd - fPosition;
431 }
39f9963f 432 fPosition += sizeof(AliRawDataHeader);
72dd1d4f 433 }
c946ab02 434
39f9963f 435 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
436 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
c946ab02 437
39f9963f 438 // check consistency of data size in the header and in the sub event
c946ab02 439 if (fPosition + fCount > fEnd) {
39f9963f 440 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 441 Warning("ReadHeader", "skipping %d bytes\n"
442 " run: %d event: %d %d LDC: %d GDC: %d\n",
443 fEnd - fPosition, fSubEvent->eventRunNb,
444 fSubEvent->eventId[0], fSubEvent->eventId[1],
445 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
446 fCount = 0;
447 fPosition = fEnd;
448 fErrorCode = kErrSize;
449 continue;
450 }
451
452 } else {
453 fCount = fEnd - fPosition;
16048fcf 454 }
b4857df7 455
921a8308 456 } while (!fEquipment || !IsSelected());
b4857df7 457
16048fcf 458 return kTRUE;
459#else
460 return kFALSE;
461#endif
462}
463
464Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
465{
466// reads the next payload at the current position
467// returns kFALSE if the data could not be read
468
b4857df7 469 fErrorCode = 0;
16048fcf 470 while (fCount == 0) {
c946ab02 471 if (!ReadHeader()) return kFALSE;
16048fcf 472 }
473 data = fPosition;
474 fPosition += fCount;
475 fCount = 0;
476 return kTRUE;
477}
478
479Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
480{
481// reads the next block of data at the current position
482// returns kFALSE if the data could not be read
483
b4857df7 484 fErrorCode = 0;
16048fcf 485 if (fPosition + size > fEnd) {
486 Error("ReadNext", "could not read data!");
b4857df7 487 fErrorCode = kErrOutOfBounds;
16048fcf 488 return kFALSE;
489 }
490 memcpy(data, fPosition, size);
491 fPosition += size;
492 fCount -= size;
493 return kTRUE;
494}
495
496
497Bool_t AliRawReaderDate::Reset()
498{
499// reset the current position to the beginning of the event
500
501#ifdef ALI_DATE
502 fSubEvent = NULL;
d7aa1c7c 503 fEquipment = NULL;
16048fcf 504#endif
505 fCount = 0;
506 fPosition = fEnd = NULL;
507 return kTRUE;
508}
b4857df7 509
510
dd9a70fe 511Bool_t AliRawReaderDate::NextEvent()
512{
513// go to the next event in the date file
514
5bec8712 515#ifdef ALI_DATE
dd9a70fe 516 if (!fFile) return kFALSE;
517
d7aa1c7c 518 Reset();
dd9a70fe 519 eventHeaderStruct header;
520 UInt_t headerSize = sizeof(eventHeaderStruct);
521 if (fEvent) delete[] fEvent;
522 fEvent = &header;
523
524 while (fread(&header, 1, headerSize, fFile) == headerSize) {
525 if (!IsEventSelected()) {
526 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
527 continue;
528 }
529 UChar_t* buffer = new UChar_t[header.eventSize];
530 fseek(fFile, -headerSize, SEEK_CUR);
531 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
532 Error("NextEvent", "could not read event from file");
533 delete[] buffer;
534 break;
535 }
536 fEvent = (eventHeaderStruct*) buffer;
38cf12f3 537 fEventNumber++;
dd9a70fe 538 return kTRUE;
539 };
540
541 fEvent = NULL;
bba5b21b 542#endif
5bec8712 543
dd9a70fe 544 return kFALSE;
545}
546
547Bool_t AliRawReaderDate::RewindEvents()
548{
549// go back to the beginning of the date file
550
551 if (!fFile) return kFALSE;
552
553 fseek(fFile, 0, SEEK_SET);
38cf12f3 554 fEventNumber = -1;
dd9a70fe 555 return Reset();
556}
557
558
b4857df7 559Int_t AliRawReaderDate::CheckData() const
560{
561// check the consistency of the data
562
563#ifdef ALI_DATE
564 if (!fEvent) return 0;
565 // check whether there are sub events
566 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
567
568 eventHeaderStruct* subEvent = NULL;
569 UChar_t* position = 0;
570 UChar_t* end = 0;
be50fca2 571 Int_t result = 0;
b4857df7 572
573 while (kTRUE) {
574 // get the first or the next sub event if at the end of a sub event
575 if (!subEvent || (position >= end)) {
576
577 // check for end of event data
be50fca2 578 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
5ba96ac1 579 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
580 ATTR_SUPER_EVENT)) {
581 subEvent = fEvent; // no super event
582 } else if (subEvent) {
b4857df7 583 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
584 subEvent->eventSize);
585 } else {
586 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
587 fEvent->eventHeadSize);
588 }
589
590 // check the magic word of the sub event
be50fca2 591 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
592 result |= kErrMagic;
593 return result;
594 }
b4857df7 595
596 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
597 sizeof(equipmentHeaderStruct);
598 end = ((UChar_t*)subEvent) + subEvent->eventSize;
599 }
600
601 // continue with the next sub event if no data left in the payload
602 if (position >= end) continue;
603
18882b3b 604 if (fRequireHeader) {
39f9963f 605 // check that there are enough bytes left for the data header
18882b3b 606 if (position + sizeof(AliRawDataHeader) > end) {
607 result |= kErrNoDataHeader;
39f9963f 608 position = end;
18882b3b 609 continue;
610 }
611
612 // check consistency of data size in the data header and in the sub event
613 AliRawDataHeader* header = (AliRawDataHeader*) position;
299738b9 614 if ((position + header->fSize) != end) {
615 Warning("ReadHeader",
616 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
617 header->fSize, end - position);
618 header->fSize = end - position;
619 result |= kErrSize;
39f9963f 620 }
be50fca2 621 }
18882b3b 622 position = end;
b4857df7 623 };
624
625#endif
626 return 0;
627}