Always delete TObjArrays created by TString::Tokenize (Ruben)
[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
d87c6cb8 37 void* event, Bool_t owner
16048fcf 38#else
d87c6cb8 39 void* /* event */, Bool_t owner
16048fcf 40#endif
c946ab02 41 ) :
dd9a70fe 42 fFile(NULL),
c946ab02 43 fEvent(NULL),
44 fSubEvent(NULL),
45 fEquipment(NULL),
c946ab02 46 fPosition(NULL),
d87c6cb8 47 fEnd(NULL),
48 fOwner(owner)
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 ) :
dd9a70fe 67 fFile(NULL),
c946ab02 68 fEvent(NULL),
69 fSubEvent(NULL),
70 fEquipment(NULL),
c946ab02 71 fPosition(NULL),
d87c6cb8 72 fEnd(NULL),
73 fOwner(kTRUE)
c946ab02 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);
a97af23d 81 fIsValid = kFALSE;
c946ab02 82 return;
83 }
dd9a70fe 84 if (eventNumber < 0) return;
85
c946ab02 86 eventHeaderStruct header;
87 UInt_t headerSize = sizeof(eventHeaderStruct);
dd9a70fe 88 while (fread(&header, 1, headerSize, fFile) == headerSize) {
c946ab02 89 if (eventNumber == 0) {
90 UChar_t* buffer = new UChar_t[header.eventSize];
b91d160c 91 fseek(fFile, -(long)headerSize, SEEK_CUR);
dd9a70fe 92 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
c946ab02 93 fEvent = (eventHeaderStruct*) buffer;
c946ab02 94 break;
95 }
dd9a70fe 96 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
c946ab02 97 eventNumber--;
98 }
c946ab02 99
16048fcf 100#else
101 Fatal("AliRawReaderDate", "this class was compiled without DATE");
102#endif
103}
104
105AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
c946ab02 106 AliRawReader(rawReader),
dd9a70fe 107 fFile(rawReader.fFile),
c946ab02 108 fEvent(rawReader.fEvent),
109 fSubEvent(rawReader.fSubEvent),
110 fEquipment(rawReader.fEquipment),
c946ab02 111 fPosition(rawReader.fPosition),
d87c6cb8 112 fEnd(rawReader.fEnd),
113 fOwner(rawReader.fOwner)
c946ab02 114
16048fcf 115{
116// copy constructor
117
dd9a70fe 118 Fatal("AliRawReaderDate", "copy constructor not implemented");
16048fcf 119}
120
121AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
dd9a70fe 122 /*rawReader*/)
16048fcf 123{
124// assignment operator
125
dd9a70fe 126 Fatal("operator =", "assignment operator not implemented");
16048fcf 127 return *this;
128}
129
c946ab02 130AliRawReaderDate::~AliRawReaderDate()
131{
132// destructor
133
134#ifdef ALI_DATE
d87c6cb8 135 if (fEvent && fOwner) delete[] fEvent;
dd9a70fe 136 if (fFile) {
dd9a70fe 137 fclose(fFile);
138 }
c946ab02 139#endif
140}
141
16048fcf 142
143UInt_t AliRawReaderDate::GetType() const
144{
145// get the type from the event header
146
147#ifdef ALI_DATE
148 if (!fEvent) return 0;
149 return fEvent->eventType;
150#else
151 return 0;
152#endif
153}
154
155UInt_t AliRawReaderDate::GetRunNumber() const
156{
157// get the run number from the event header
158
159#ifdef ALI_DATE
160 if (!fEvent) return 0;
161 return fEvent->eventRunNb;
162#else
163 return 0;
164#endif
165}
166
167const UInt_t* AliRawReaderDate::GetEventId() const
168{
169// get the event id from the event header
170
171#ifdef ALI_DATE
172 if (!fEvent) return NULL;
173 return fEvent->eventId;
174#else
175 return NULL;
176#endif
177}
178
179const UInt_t* AliRawReaderDate::GetTriggerPattern() const
180{
181// get the trigger pattern from the event header
182
183#ifdef ALI_DATE
184 if (!fEvent) return NULL;
185 return fEvent->eventTriggerPattern;
186#else
187 return NULL;
188#endif
189}
190
191const UInt_t* AliRawReaderDate::GetDetectorPattern() const
192{
193// get the detector pattern from the event header
194
195#ifdef ALI_DATE
196 if (!fEvent) return NULL;
197 return fEvent->eventDetectorPattern;
198#else
199 return NULL;
200#endif
201}
202
203const UInt_t* AliRawReaderDate::GetAttributes() const
204{
205// get the type attributes from the event header
206
207#ifdef ALI_DATE
208 if (!fEvent) return NULL;
209 return fEvent->eventTypeAttribute;
210#else
211 return NULL;
212#endif
213}
214
e94ad92c 215const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
216{
217// get the type attributes from the sub event header
218
219#ifdef ALI_DATE
220 if (!fSubEvent) return NULL;
221 return fSubEvent->eventTypeAttribute;
222#else
223 return NULL;
224#endif
225}
226
c946ab02 227UInt_t AliRawReaderDate::GetLDCId() const
228{
229// get the LDC Id from the event header
230
231#ifdef ALI_DATE
232 if (!fSubEvent) return 0;
233 return fSubEvent->eventLdcId;
234#else
235 return 0;
236#endif
237}
238
16048fcf 239UInt_t AliRawReaderDate::GetGDCId() const
240{
241// get the GDC Id from the event header
242
243#ifdef ALI_DATE
244 if (!fEvent) return 0;
245 return fEvent->eventGdcId;
246#else
247 return 0;
248#endif
249}
250
741c154c 251UInt_t AliRawReaderDate::GetTimestamp() const
252{
253// get the timestamp from the event header
254
255#ifdef ALI_DATE
256 if (!fEvent) return 0;
257 return fEvent->eventTimestamp;
258#else
259 return 0;
260#endif
261}
16048fcf 262
c946ab02 263Int_t AliRawReaderDate::GetEquipmentSize() const
264{
299738b9 265// get the size of the equipment (including the header)
c946ab02 266
267#ifdef ALI_DATE
268 if (!fEquipment) return 0;
87db01b0 269 if (fSubEvent->eventVersion <= 0x00030001) {
299738b9 270 return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
87db01b0 271 } else {
299738b9 272 return fEquipment->equipmentSize;
87db01b0 273 }
c946ab02 274#else
275 return 0;
276#endif
277}
278
279Int_t AliRawReaderDate::GetEquipmentType() const
280{
281// get the type from the equipment header
282
283#ifdef ALI_DATE
284 if (!fEquipment) return -1;
285 return fEquipment->equipmentType;
286#else
287 return 0;
288#endif
289}
290
291Int_t AliRawReaderDate::GetEquipmentId() const
292{
293// get the ID from the equipment header
294
295#ifdef ALI_DATE
296 if (!fEquipment) return -1;
297 return fEquipment->equipmentId;
298#else
299 return 0;
300#endif
301}
302
303const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
304{
305// get the attributes from the equipment header
306
307#ifdef ALI_DATE
308 if (!fEquipment) return NULL;
309 return fEquipment->equipmentTypeAttribute;
310#else
311 return 0;
312#endif
313}
314
315Int_t AliRawReaderDate::GetEquipmentElementSize() const
316{
317// get the basic element size from the equipment header
318
319#ifdef ALI_DATE
320 if (!fEquipment) return 0;
321 return fEquipment->equipmentBasicElementSize;
322#else
323 return 0;
324#endif
325}
326
299738b9 327Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
328{
329 // Get the equipment header size
330 // 28 bytes by default
331#ifdef ALI_DATE
332 return sizeof(equipmentHeaderStruct);
333#else
334 return 0;
335#endif
336}
c946ab02 337
338Bool_t AliRawReaderDate::ReadHeader()
16048fcf 339{
39f9963f 340// read a data header at the current position
341// returns kFALSE if the data header could not be read
16048fcf 342
b4857df7 343 fErrorCode = 0;
344
16048fcf 345#ifdef ALI_DATE
921a8308 346 fHeader = NULL;
16048fcf 347 if (!fEvent) return kFALSE;
b4857df7 348 // check whether there are sub events
349 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
350
16048fcf 351 do {
b4857df7 352 // skip payload (if event was not selected)
353 if (fCount > 0) fPosition += fCount;
354
c946ab02 355 // get the first or the next equipment if at the end of an equipment
356 if (!fEquipment || (fPosition >= fEnd)) {
921a8308 357 fEquipment = NULL;
c946ab02 358
359 // get the first or the next sub event if at the end of a sub event
360 if (!fSubEvent ||
361 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
362
363 // check for end of event data
364 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
5ba96ac1 365 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
366 ATTR_SUPER_EVENT)) {
367 fSubEvent = fEvent; // no super event
368 } else if (fSubEvent) {
c946ab02 369 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
370 fSubEvent->eventSize);
371 } else {
372 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
373 fEvent->eventHeadSize);
374 }
375
376 // check the magic word of the sub event
377 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
378 Error("ReadHeader", "wrong magic number in sub event!\n"
379 " run: %d event: %d %d LDC: %d GDC: %d\n",
380 fSubEvent->eventRunNb,
381 fSubEvent->eventId[0], fSubEvent->eventId[1],
382 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
383 fErrorCode = kErrMagic;
384 return kFALSE;
385 }
386
921a8308 387 // continue if no data in the subevent
388 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
389 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
390 fCount = 0;
391 continue;
392 }
393
c946ab02 394 fEquipment = (equipmentHeaderStruct*)
395 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
b4857df7 396
16048fcf 397 } else {
c946ab02 398 fEquipment = (equipmentHeaderStruct*) fEnd;
b4857df7 399 }
400
16048fcf 401 fCount = 0;
c946ab02 402 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
87db01b0 403 if (fSubEvent->eventVersion <= 0x00030001) {
921a8308 404 fEnd = fPosition + fEquipment->equipmentSize;
405 } else {
406 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
407 }
16048fcf 408 }
b4857df7 409
410 // continue with the next sub event if no data left in the payload
411 if (fPosition >= fEnd) continue;
412
39f9963f 413 if (fRequireHeader) {
414 // check that there are enough bytes left for the data header
415 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
416 Error("ReadHeader", "could not read data header data!");
c946ab02 417 Warning("ReadHeader", "skipping %d bytes\n"
418 " run: %d event: %d %d LDC: %d GDC: %d\n",
419 fEnd - fPosition, fSubEvent->eventRunNb,
420 fSubEvent->eventId[0], fSubEvent->eventId[1],
421 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
422 fCount = 0;
423 fPosition = fEnd;
39f9963f 424 fErrorCode = kErrNoDataHeader;
c946ab02 425 continue;
426 }
b4857df7 427
39f9963f 428 // "read" the data header
429 fHeader = (AliRawDataHeader*) fPosition;
299738b9 430 if ((fPosition + fHeader->fSize) != fEnd) {
da1241fd 431 if ((fHeader->fSize != 0xFFFFFFFF) &&
432 (fEquipment->equipmentId != 4352))
806e573c 433 Warning("ReadHeader",
434 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
435 fHeader->fSize, fEnd - fPosition);
299738b9 436 fHeader->fSize = fEnd - fPosition;
437 }
39f9963f 438 fPosition += sizeof(AliRawDataHeader);
72dd1d4f 439 }
c946ab02 440
39f9963f 441 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
442 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
c946ab02 443
39f9963f 444 // check consistency of data size in the header and in the sub event
c946ab02 445 if (fPosition + fCount > fEnd) {
39f9963f 446 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 447 Warning("ReadHeader", "skipping %d bytes\n"
448 " run: %d event: %d %d LDC: %d GDC: %d\n",
449 fEnd - fPosition, fSubEvent->eventRunNb,
450 fSubEvent->eventId[0], fSubEvent->eventId[1],
451 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
452 fCount = 0;
453 fPosition = fEnd;
454 fErrorCode = kErrSize;
455 continue;
456 }
457
458 } else {
459 fCount = fEnd - fPosition;
16048fcf 460 }
b4857df7 461
921a8308 462 } while (!fEquipment || !IsSelected());
b4857df7 463
16048fcf 464 return kTRUE;
465#else
466 return kFALSE;
467#endif
468}
469
470Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
471{
472// reads the next payload at the current position
473// returns kFALSE if the data could not be read
474
b4857df7 475 fErrorCode = 0;
16048fcf 476 while (fCount == 0) {
c946ab02 477 if (!ReadHeader()) return kFALSE;
16048fcf 478 }
479 data = fPosition;
480 fPosition += fCount;
481 fCount = 0;
482 return kTRUE;
483}
484
485Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
486{
487// reads the next block of data at the current position
488// returns kFALSE if the data could not be read
489
b4857df7 490 fErrorCode = 0;
16048fcf 491 if (fPosition + size > fEnd) {
492 Error("ReadNext", "could not read data!");
b4857df7 493 fErrorCode = kErrOutOfBounds;
16048fcf 494 return kFALSE;
495 }
496 memcpy(data, fPosition, size);
497 fPosition += size;
498 fCount -= size;
499 return kTRUE;
500}
501
502
503Bool_t AliRawReaderDate::Reset()
504{
505// reset the current position to the beginning of the event
506
507#ifdef ALI_DATE
508 fSubEvent = NULL;
d7aa1c7c 509 fEquipment = NULL;
16048fcf 510#endif
511 fCount = 0;
512 fPosition = fEnd = NULL;
513 return kTRUE;
514}
b4857df7 515
516
dd9a70fe 517Bool_t AliRawReaderDate::NextEvent()
518{
519// go to the next event in the date file
520
5bec8712 521#ifdef ALI_DATE
b04576f7 522 if (!fFile) {
523 if (fEventNumber < 0 && fEvent) {
524 fEventNumber++;
525 return kTRUE;
526 }
527 else
528 return kFALSE;
529 }
dd9a70fe 530
d7aa1c7c 531 Reset();
dd9a70fe 532 eventHeaderStruct header;
533 UInt_t headerSize = sizeof(eventHeaderStruct);
534 if (fEvent) delete[] fEvent;
535 fEvent = &header;
536
537 while (fread(&header, 1, headerSize, fFile) == headerSize) {
538 if (!IsEventSelected()) {
539 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
540 continue;
541 }
542 UChar_t* buffer = new UChar_t[header.eventSize];
b91d160c 543 fseek(fFile, -(long)headerSize, SEEK_CUR);
dd9a70fe 544 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
545 Error("NextEvent", "could not read event from file");
546 delete[] buffer;
547 break;
548 }
549 fEvent = (eventHeaderStruct*) buffer;
38cf12f3 550 fEventNumber++;
dd9a70fe 551 return kTRUE;
552 };
553
554 fEvent = NULL;
bba5b21b 555#endif
5bec8712 556
dd9a70fe 557 return kFALSE;
558}
559
560Bool_t AliRawReaderDate::RewindEvents()
561{
562// go back to the beginning of the date file
563
b04576f7 564 if (fFile)
565 fseek(fFile, 0, SEEK_SET);
dd9a70fe 566
38cf12f3 567 fEventNumber = -1;
dd9a70fe 568 return Reset();
569}
570
571
b4857df7 572Int_t AliRawReaderDate::CheckData() const
573{
574// check the consistency of the data
575
576#ifdef ALI_DATE
577 if (!fEvent) return 0;
578 // check whether there are sub events
579 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
580
581 eventHeaderStruct* subEvent = NULL;
582 UChar_t* position = 0;
583 UChar_t* end = 0;
be50fca2 584 Int_t result = 0;
b4857df7 585
586 while (kTRUE) {
587 // get the first or the next sub event if at the end of a sub event
588 if (!subEvent || (position >= end)) {
589
590 // check for end of event data
be50fca2 591 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
5ba96ac1 592 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
593 ATTR_SUPER_EVENT)) {
594 subEvent = fEvent; // no super event
595 } else if (subEvent) {
b4857df7 596 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
597 subEvent->eventSize);
598 } else {
599 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
600 fEvent->eventHeadSize);
601 }
602
603 // check the magic word of the sub event
be50fca2 604 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
605 result |= kErrMagic;
606 return result;
607 }
b4857df7 608
609 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
610 sizeof(equipmentHeaderStruct);
611 end = ((UChar_t*)subEvent) + subEvent->eventSize;
612 }
613
614 // continue with the next sub event if no data left in the payload
615 if (position >= end) continue;
616
18882b3b 617 if (fRequireHeader) {
39f9963f 618 // check that there are enough bytes left for the data header
18882b3b 619 if (position + sizeof(AliRawDataHeader) > end) {
620 result |= kErrNoDataHeader;
39f9963f 621 position = end;
18882b3b 622 continue;
623 }
624
625 // check consistency of data size in the data header and in the sub event
626 AliRawDataHeader* header = (AliRawDataHeader*) position;
299738b9 627 if ((position + header->fSize) != end) {
806e573c 628 if (header->fSize != 0xFFFFFFFF)
629 Warning("ReadHeader",
630 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
631 header->fSize, end - position);
299738b9 632 header->fSize = end - position;
633 result |= kErrSize;
39f9963f 634 }
be50fca2 635 }
18882b3b 636 position = end;
b4857df7 637 };
638
639#endif
640 return 0;
641}
b900a426 642
643AliRawReader* AliRawReaderDate::CloneSingleEvent() const
644{
645 // Clones the current event and
646 // creates raw-reader for the cloned event
647 // Can be used in order to make asynchronious
648 // access to the current raw data within
649 // several threads (online event display/reco)
650
651#ifdef ALI_DATE
652 if (fEvent) {
653 UInt_t evSize = fEvent->eventSize;
654 if (evSize) {
655 UChar_t *newEvent = new UChar_t[evSize];
656 memcpy(newEvent,fEvent,evSize);
d87c6cb8 657 return new AliRawReaderDate((void *)newEvent,kTRUE);
b900a426 658 }
659 }
660#else
661 Fatal("AliRawReaderDate", "this class was compiled without DATE");
662#endif
663 return NULL;
664}