]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RAW/AliRawReaderDate.cxx
Storing the trigger class mask into the raw data header. Should be ok for TPC,PHOS...
[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
247
c946ab02 248Int_t AliRawReaderDate::GetEquipmentSize() const
249{
87db01b0 250// get the size of the equipment (without header)
c946ab02 251
252#ifdef ALI_DATE
253 if (!fEquipment) return 0;
87db01b0 254 if (fSubEvent->eventVersion <= 0x00030001) {
255 return fEquipment->equipmentSize;
256 } else {
257 return fEquipment->equipmentSize - sizeof(equipmentHeaderStruct);
258 }
c946ab02 259#else
260 return 0;
261#endif
262}
263
264Int_t AliRawReaderDate::GetEquipmentType() const
265{
266// get the type from the equipment header
267
268#ifdef ALI_DATE
269 if (!fEquipment) return -1;
270 return fEquipment->equipmentType;
271#else
272 return 0;
273#endif
274}
275
276Int_t AliRawReaderDate::GetEquipmentId() const
277{
278// get the ID from the equipment header
279
280#ifdef ALI_DATE
281 if (!fEquipment) return -1;
282 return fEquipment->equipmentId;
283#else
284 return 0;
285#endif
286}
287
288const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
289{
290// get the attributes from the equipment header
291
292#ifdef ALI_DATE
293 if (!fEquipment) return NULL;
294 return fEquipment->equipmentTypeAttribute;
295#else
296 return 0;
297#endif
298}
299
300Int_t AliRawReaderDate::GetEquipmentElementSize() const
301{
302// get the basic element size from the equipment header
303
304#ifdef ALI_DATE
305 if (!fEquipment) return 0;
306 return fEquipment->equipmentBasicElementSize;
307#else
308 return 0;
309#endif
310}
311
312
313Bool_t AliRawReaderDate::ReadHeader()
16048fcf 314{
39f9963f 315// read a data header at the current position
316// returns kFALSE if the data header could not be read
16048fcf 317
b4857df7 318 fErrorCode = 0;
319
16048fcf 320#ifdef ALI_DATE
921a8308 321 fHeader = NULL;
16048fcf 322 if (!fEvent) return kFALSE;
b4857df7 323 // check whether there are sub events
324 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
325
16048fcf 326 do {
b4857df7 327 // skip payload (if event was not selected)
328 if (fCount > 0) fPosition += fCount;
329
c946ab02 330 // get the first or the next equipment if at the end of an equipment
331 if (!fEquipment || (fPosition >= fEnd)) {
921a8308 332 fEquipment = NULL;
c946ab02 333
334 // get the first or the next sub event if at the end of a sub event
335 if (!fSubEvent ||
336 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
337
338 // check for end of event data
339 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
5ba96ac1 340 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
341 ATTR_SUPER_EVENT)) {
342 fSubEvent = fEvent; // no super event
343 } else if (fSubEvent) {
c946ab02 344 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
345 fSubEvent->eventSize);
346 } else {
347 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
348 fEvent->eventHeadSize);
349 }
350
351 // check the magic word of the sub event
352 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
353 Error("ReadHeader", "wrong magic number in sub event!\n"
354 " run: %d event: %d %d LDC: %d GDC: %d\n",
355 fSubEvent->eventRunNb,
356 fSubEvent->eventId[0], fSubEvent->eventId[1],
357 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
358 fErrorCode = kErrMagic;
359 return kFALSE;
360 }
361
921a8308 362 // continue if no data in the subevent
363 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
364 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
365 fCount = 0;
366 continue;
367 }
368
c946ab02 369 fEquipment = (equipmentHeaderStruct*)
370 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
b4857df7 371
16048fcf 372 } else {
c946ab02 373 fEquipment = (equipmentHeaderStruct*) fEnd;
b4857df7 374 }
375
16048fcf 376 fCount = 0;
c946ab02 377 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
87db01b0 378 if (fSubEvent->eventVersion <= 0x00030001) {
921a8308 379 fEnd = fPosition + fEquipment->equipmentSize;
380 } else {
381 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
382 }
16048fcf 383 }
b4857df7 384
385 // continue with the next sub event if no data left in the payload
386 if (fPosition >= fEnd) continue;
387
39f9963f 388 if (fRequireHeader) {
389 // check that there are enough bytes left for the data header
390 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
391 Error("ReadHeader", "could not read data header data!");
c946ab02 392 Warning("ReadHeader", "skipping %d bytes\n"
393 " run: %d event: %d %d LDC: %d GDC: %d\n",
394 fEnd - fPosition, fSubEvent->eventRunNb,
395 fSubEvent->eventId[0], fSubEvent->eventId[1],
396 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
397 fCount = 0;
398 fPosition = fEnd;
39f9963f 399 fErrorCode = kErrNoDataHeader;
c946ab02 400 continue;
401 }
b4857df7 402
39f9963f 403 // "read" the data header
404 fHeader = (AliRawDataHeader*) fPosition;
405 fPosition += sizeof(AliRawDataHeader);
72dd1d4f 406 }
c946ab02 407
39f9963f 408 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
409 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
c946ab02 410
39f9963f 411 // check consistency of data size in the header and in the sub event
c946ab02 412 if (fPosition + fCount > fEnd) {
39f9963f 413 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 414 Warning("ReadHeader", "skipping %d bytes\n"
415 " run: %d event: %d %d LDC: %d GDC: %d\n",
416 fEnd - fPosition, fSubEvent->eventRunNb,
417 fSubEvent->eventId[0], fSubEvent->eventId[1],
418 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
419 fCount = 0;
420 fPosition = fEnd;
421 fErrorCode = kErrSize;
422 continue;
423 }
424
425 } else {
426 fCount = fEnd - fPosition;
16048fcf 427 }
b4857df7 428
921a8308 429 } while (!fEquipment || !IsSelected());
b4857df7 430
16048fcf 431 return kTRUE;
432#else
433 return kFALSE;
434#endif
435}
436
437Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
438{
439// reads the next payload at the current position
440// returns kFALSE if the data could not be read
441
b4857df7 442 fErrorCode = 0;
16048fcf 443 while (fCount == 0) {
c946ab02 444 if (!ReadHeader()) return kFALSE;
16048fcf 445 }
446 data = fPosition;
447 fPosition += fCount;
448 fCount = 0;
449 return kTRUE;
450}
451
452Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
453{
454// reads the next block of data at the current position
455// returns kFALSE if the data could not be read
456
b4857df7 457 fErrorCode = 0;
16048fcf 458 if (fPosition + size > fEnd) {
459 Error("ReadNext", "could not read data!");
b4857df7 460 fErrorCode = kErrOutOfBounds;
16048fcf 461 return kFALSE;
462 }
463 memcpy(data, fPosition, size);
464 fPosition += size;
465 fCount -= size;
466 return kTRUE;
467}
468
469
470Bool_t AliRawReaderDate::Reset()
471{
472// reset the current position to the beginning of the event
473
474#ifdef ALI_DATE
475 fSubEvent = NULL;
d7aa1c7c 476 fEquipment = NULL;
16048fcf 477#endif
478 fCount = 0;
479 fPosition = fEnd = NULL;
480 return kTRUE;
481}
b4857df7 482
483
dd9a70fe 484Bool_t AliRawReaderDate::NextEvent()
485{
486// go to the next event in the date file
487
5bec8712 488#ifdef ALI_DATE
dd9a70fe 489 if (!fFile) return kFALSE;
490
d7aa1c7c 491 Reset();
dd9a70fe 492 eventHeaderStruct header;
493 UInt_t headerSize = sizeof(eventHeaderStruct);
494 if (fEvent) delete[] fEvent;
495 fEvent = &header;
496
497 while (fread(&header, 1, headerSize, fFile) == headerSize) {
498 if (!IsEventSelected()) {
499 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
500 continue;
501 }
502 UChar_t* buffer = new UChar_t[header.eventSize];
503 fseek(fFile, -headerSize, SEEK_CUR);
504 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
505 Error("NextEvent", "could not read event from file");
506 delete[] buffer;
507 break;
508 }
509 fEvent = (eventHeaderStruct*) buffer;
510 return kTRUE;
511 };
512
513 fEvent = NULL;
bba5b21b 514#endif
5bec8712 515
dd9a70fe 516 return kFALSE;
517}
518
519Bool_t AliRawReaderDate::RewindEvents()
520{
521// go back to the beginning of the date file
522
523 if (!fFile) return kFALSE;
524
525 fseek(fFile, 0, SEEK_SET);
526 return Reset();
527}
528
529
b4857df7 530Int_t AliRawReaderDate::CheckData() const
531{
532// check the consistency of the data
533
534#ifdef ALI_DATE
535 if (!fEvent) return 0;
536 // check whether there are sub events
537 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
538
539 eventHeaderStruct* subEvent = NULL;
540 UChar_t* position = 0;
541 UChar_t* end = 0;
be50fca2 542 Int_t result = 0;
b4857df7 543
544 while (kTRUE) {
545 // get the first or the next sub event if at the end of a sub event
546 if (!subEvent || (position >= end)) {
547
548 // check for end of event data
be50fca2 549 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
5ba96ac1 550 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
551 ATTR_SUPER_EVENT)) {
552 subEvent = fEvent; // no super event
553 } else if (subEvent) {
b4857df7 554 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
555 subEvent->eventSize);
556 } else {
557 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
558 fEvent->eventHeadSize);
559 }
560
561 // check the magic word of the sub event
be50fca2 562 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
563 result |= kErrMagic;
564 return result;
565 }
b4857df7 566
567 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
568 sizeof(equipmentHeaderStruct);
569 end = ((UChar_t*)subEvent) + subEvent->eventSize;
570 }
571
572 // continue with the next sub event if no data left in the payload
573 if (position >= end) continue;
574
18882b3b 575 if (fRequireHeader) {
39f9963f 576 // check that there are enough bytes left for the data header
18882b3b 577 if (position + sizeof(AliRawDataHeader) > end) {
578 result |= kErrNoDataHeader;
39f9963f 579 position = end;
18882b3b 580 continue;
581 }
582
583 // check consistency of data size in the data header and in the sub event
584 AliRawDataHeader* header = (AliRawDataHeader*) position;
585 if (header->fSize != 0xFFFFFFFF) {
586 if (position + header->fSize > end) result |= kErrSize;
39f9963f 587 }
be50fca2 588 }
18882b3b 589 position = end;
b4857df7 590 };
591
592#endif
593 return 0;
594}