New raw-reader class which deals with events taken from shared memory via the DATE...
[u/mrichter/AliRoot.git] / RAW / AliRawReader.cxx
CommitLineData
a6e7b125 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
a6e7b125 18///////////////////////////////////////////////////////////////////////////////
bea6b2a4 19///
20/// This is the base class for reading raw data.
21///
22/// The derived classes, which operate on concrete raw data formats,
23/// should implement
24/// - ReadHeader to read the next (data/equipment) header
25/// - ReadNextData to read the next raw data block (=1 DDL)
26/// - ReadNext to read a given number of bytes
27/// - several getters like GetType
28///
29/// Sequential access to the raw data is provided by the methods
30/// ReadHeader, ReadNextData, ReadNextInt, ReadNextShort, ReadNextChar
31///
32/// If only data from a specific detector (and a given range of DDL numbers)
33/// should be read, this can be achieved by the Select method.
34/// Several getters provide information about the current event and the
35/// current type of raw data.
36///
a6e7b125 37///////////////////////////////////////////////////////////////////////////////
38
0cfe2438 39#include <Riostream.h>
a6e7b125 40#include "AliRawReader.h"
362c9d61 41#include "AliDAQ.h"
38cf12f3 42#include "AliLog.h"
8de97894 43
a6e7b125 44ClassImp(AliRawReader)
45
46
c946ab02 47AliRawReader::AliRawReader() :
0cfe2438 48 fEquipmentIdsIn(NULL),
49 fEquipmentIdsOut(NULL),
18882b3b 50 fRequireHeader(kTRUE),
39f9963f 51 fHeader(NULL),
c946ab02 52 fCount(0),
f224de6c 53 fSelectEquipmentType(-1),
54 fSelectMinEquipmentId(-1),
55 fSelectMaxEquipmentId(-1),
39f9963f 56 fSkipInvalid(kFALSE),
dd9a70fe 57 fSelectEventType(-1),
38cf12f3 58 fErrorCode(0),
59 fEventNumber(-1),
edd06192 60 fErrorLogs("AliRawDataErrorLog",100),
61 fHeaderSwapped(NULL)
a6e7b125 62{
42d20574 63// default constructor: initialize data members
edd06192 64// Allocate the swapped header in case of Mac
65#ifndef R__BYTESWAP
66 fHeaderSwapped=new AliRawDataHeader();
67#endif
0cfe2438 68}
42d20574 69
0cfe2438 70Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
71{
72 // Open the mapping file
73 // and load the mapping data
74 ifstream input(fileName);
75 if (input.is_open()) {
76 Warning("AliRawReader","Equipment ID mapping file is found !");
77 const Int_t kMaxDDL = 256;
78 fEquipmentIdsIn = new TArrayI(kMaxDDL);
79 fEquipmentIdsOut = new TArrayI(kMaxDDL);
80 Int_t equipIn, equipOut;
81 Int_t nIds = 0;
82 while (input >> equipIn >> equipOut) {
83 if (nIds >= kMaxDDL) {
84 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
85 break;
86 }
87 fEquipmentIdsIn->AddAt(equipIn,nIds);
88 fEquipmentIdsOut->AddAt(equipOut,nIds);
89 nIds++;
90 }
91 fEquipmentIdsIn->Set(nIds);
92 fEquipmentIdsOut->Set(nIds);
93 input.close();
94 return kTRUE;
95 }
96 else {
97 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
98 return kFALSE;
99 }
a6e7b125 100}
101
42d20574 102AliRawReader::AliRawReader(const AliRawReader& rawReader) :
c946ab02 103 TObject(rawReader),
0cfe2438 104 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
105 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
18882b3b 106 fRequireHeader(rawReader.fRequireHeader),
39f9963f 107 fHeader(rawReader.fHeader),
c946ab02 108 fCount(rawReader.fCount),
f224de6c 109 fSelectEquipmentType(rawReader.fSelectEquipmentType),
110 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
111 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
39f9963f 112 fSkipInvalid(rawReader.fSkipInvalid),
dd9a70fe 113 fSelectEventType(rawReader.fSelectEventType),
38cf12f3 114 fErrorCode(0),
115 fEventNumber(-1),
edd06192 116 fErrorLogs("AliRawDataErrorLog",100),
117 fHeaderSwapped(NULL)
42d20574 118{
119// copy constructor
edd06192 120// Allocate the swapped header in case of Mac
121#ifndef R__BYTESWAP
122 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
123#endif
42d20574 124}
125
126AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
127{
128// assignment operator
0cfe2438 129 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
130 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
42d20574 131
39f9963f 132 fHeader = rawReader.fHeader;
42d20574 133 fCount = rawReader.fCount;
134
39f9963f 135 fSelectEquipmentType = rawReader.fSelectEquipmentType;
136 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
137 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
138 fSkipInvalid = rawReader.fSkipInvalid;
dd9a70fe 139 fSelectEventType = rawReader.fSelectEventType;
42d20574 140
b4857df7 141 fErrorCode = rawReader.fErrorCode;
142
38cf12f3 143 fEventNumber = rawReader.fEventNumber;
144 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
145
42d20574 146 return *this;
147}
148
0cfe2438 149AliRawReader::~AliRawReader()
150{
151 // destructor
152 // delete the mapping arrays if
153 // initialized
154 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
155 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
79dac785 156 fErrorLogs.Delete();
edd06192 157 if (fHeaderSwapped) delete fHeaderSwapped;
0cfe2438 158}
159
160Int_t AliRawReader::GetMappedEquipmentId() const
161{
162 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
163 Error("AliRawReader","equipment Ids mapping is not initialized !");
164 return GetEquipmentId();
165 }
166 Int_t equipmentId = GetEquipmentId();
167 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
168 if (equipmentId == fEquipmentIdsIn->At(iId)) {
169 equipmentId = fEquipmentIdsOut->At(iId);
170 break;
171 }
172 }
173 return equipmentId;
174}
175
176Int_t AliRawReader::GetDetectorID() const
177{
178 // Get the detector ID
179 // The list of detector IDs
362c9d61 180 // can be found in AliDAQ.h
0cfe2438 181 Int_t equipmentId;
182 if (fEquipmentIdsIn && fEquipmentIdsIn)
183 equipmentId = GetMappedEquipmentId();
184 else
185 equipmentId = GetEquipmentId();
186
362c9d61 187 if (equipmentId >= 0) {
188 Int_t ddlIndex;
189 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
190 }
0cfe2438 191 else
192 return -1;
193}
194
195Int_t AliRawReader::GetDDLID() const
196{
197 // Get the DDL ID (within one sub-detector)
198 // The list of detector IDs
362c9d61 199 // can be found in AliDAQ.h
0cfe2438 200 Int_t equipmentId;
201 if (fEquipmentIdsIn && fEquipmentIdsIn)
202 equipmentId = GetMappedEquipmentId();
203 else
204 equipmentId = GetEquipmentId();
205
362c9d61 206 if (equipmentId >= 0) {
207 Int_t ddlIndex;
208 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
209 return ddlIndex;
210 }
0cfe2438 211 else
212 return -1;
213}
8de97894 214
362c9d61 215void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
216{
217// read only data of the detector with the given name and in the given
218// range of DDLs (minDDLID <= DDLID <= maxDDLID).
219// no selection is applied if a value < 0 is used.
220 Int_t detectorID = AliDAQ::DetectorID(detectorName);
221 if(detectorID >= 0)
222 Select(detectorID,minDDLID,maxDDLID);
223}
224
8de97894 225void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
a6e7b125 226{
8de97894 227// read only data of the detector with the given ID and in the given
f224de6c 228// range of DDLs (minDDLID <= DDLID <= maxDDLID).
8de97894 229// no selection is applied if a value < 0 is used.
a6e7b125 230
0cfe2438 231 fSelectEquipmentType = -1;
362c9d61 232
233 if (minDDLID < 0)
234 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
235 else
236 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
237
238 if (maxDDLID < 0)
239 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
240 else
241 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
8de97894 242}
a6e7b125 243
f224de6c 244void AliRawReader::SelectEquipment(Int_t equipmentType,
245 Int_t minEquipmentId, Int_t maxEquipmentId)
246{
247// read only data of the equipment with the given type and in the given
248// range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
249// no selection is applied if a value < 0 is used.
250
251 fSelectEquipmentType = equipmentType;
252 fSelectMinEquipmentId = minEquipmentId;
253 fSelectMaxEquipmentId = maxEquipmentId;
254}
255
dd9a70fe 256void AliRawReader::SelectEvents(Int_t type)
257{
258// read only events with the given type.
259// no selection is applied if a value < 0 is used.
260
261 fSelectEventType = type;
262}
263
42d20574 264Bool_t AliRawReader::IsSelected() const
a6e7b125 265{
8de97894 266// apply the selection (if any)
267
39f9963f 268 if (fSkipInvalid && !IsValid()) return kFALSE;
f224de6c 269
0cfe2438 270 if (fSelectEquipmentType >= 0)
f224de6c 271 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
0cfe2438 272
273 Int_t equipmentId;
274 if (fEquipmentIdsIn && fEquipmentIdsIn)
275 equipmentId = GetMappedEquipmentId();
276 else
277 equipmentId = GetEquipmentId();
278
279 if ((fSelectMinEquipmentId >= 0) &&
280 (equipmentId < fSelectMinEquipmentId))
281 return kFALSE;
282 if ((fSelectMaxEquipmentId >= 0) &&
283 (equipmentId > fSelectMaxEquipmentId))
284 return kFALSE;
f224de6c 285
8de97894 286 return kTRUE;
a6e7b125 287}
288
dd9a70fe 289Bool_t AliRawReader::IsEventSelected() const
290{
291// apply the event selection (if any)
292
293 if (fSelectEventType >= 0) {
294 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
295 }
296
297 return kTRUE;
298}
299
91f76e9b 300UInt_t AliRawReader::SwapWord(UInt_t x) const
301{
302 // Swap the endianess of the integer value 'x'
303
304 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
305 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
306}
307
308UShort_t AliRawReader::SwapShort(UShort_t x) const
309{
310 // Swap the endianess of the short value 'x'
311
312 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
313}
a6e7b125 314
a6e7b125 315Bool_t AliRawReader::ReadNextInt(UInt_t& data)
316{
8de97894 317// reads the next 4 bytes at the current position
318// returns kFALSE if the data could not be read
a6e7b125 319
320 while (fCount == 0) {
c946ab02 321 if (!ReadHeader()) return kFALSE;
a6e7b125 322 }
8de97894 323 if (fCount < (Int_t) sizeof(data)) {
324 Error("ReadNextInt",
325 "too few data left (%d bytes) to read an UInt_t!", fCount);
326 return kFALSE;
327 }
328 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 329 Error("ReadNextInt", "could not read data!");
330 return kFALSE;
331 }
91f76e9b 332#ifndef R__BYTESWAP
333 data=SwapWord(data);
334#endif
a6e7b125 335 return kTRUE;
336}
337
338Bool_t AliRawReader::ReadNextShort(UShort_t& data)
339{
8de97894 340// reads the next 2 bytes at the current position
341// returns kFALSE if the data could not be read
a6e7b125 342
343 while (fCount == 0) {
c946ab02 344 if (!ReadHeader()) return kFALSE;
a6e7b125 345 }
8de97894 346 if (fCount < (Int_t) sizeof(data)) {
347 Error("ReadNextShort",
348 "too few data left (%d bytes) to read an UShort_t!", fCount);
349 return kFALSE;
350 }
351 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 352 Error("ReadNextShort", "could not read data!");
353 return kFALSE;
354 }
91f76e9b 355#ifndef R__BYTESWAP
356 data=SwapShort(data);
357#endif
a6e7b125 358 return kTRUE;
359}
360
361Bool_t AliRawReader::ReadNextChar(UChar_t& data)
362{
363// reads the next 1 byte at the current stream position
8de97894 364// returns kFALSE if the data could not be read
a6e7b125 365
366 while (fCount == 0) {
c946ab02 367 if (!ReadHeader()) return kFALSE;
a6e7b125 368 }
8de97894 369 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 370 Error("ReadNextChar", "could not read data!");
371 return kFALSE;
372 }
a6e7b125 373 return kTRUE;
374}
375
b4857df7 376
377Int_t AliRawReader::CheckData() const
378{
379// check the consistency of the data
380// derived classes should overwrite the default method which returns 0 (no err)
381
382 return 0;
383}
384
509a7696 385
386void AliRawReader::DumpData(Int_t limit)
387{
388// print the raw data
389// if limit is not negative, only the first and last "limit" lines of raw data
390// are printed
391
392 Reset();
393 if (!ReadHeader()) {
394 Error("DumpData", "no header");
395 return;
396 }
397 printf("header:\n"
398 " type = %d run = %d ", GetType(), GetRunNumber());
399 if (GetEventId()) {
400 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
401 } else {
402 printf("event = -------- --------\n");
403 }
404 if (GetTriggerPattern()) {
405 printf(" trigger = %8.8x %8.8x ",
406 GetTriggerPattern()[1], GetTriggerPattern()[0]);
407 } else {
408 printf(" trigger = -------- -------- ");
409 }
410 if (GetDetectorPattern()) {
411 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
412 } else {
413 printf("detector = --------\n");
414 }
415 if (GetAttributes()) {
416 printf(" attributes = %8.8x %8.8x %8.8x ",
417 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
418 } else {
419 printf(" attributes = -------- -------- -------- ");
420 }
421 printf("GDC = %d\n", GetGDCId());
422 printf("\n");
423
424 do {
425 printf("-------------------------------------------------------------------------------\n");
426 printf("LDC = %d\n", GetLDCId());
427
428 printf("equipment:\n"
429 " size = %d type = %d id = %d\n",
430 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
431 if (GetEquipmentAttributes()) {
432 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
433 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
434 } else {
435 printf(" attributes = -------- -------- -------- ");
436 }
437 printf("element size = %d\n", GetEquipmentElementSize());
438
39f9963f 439 printf("data header:\n"
440 " size = %d version = %d valid = %d compression = %d\n",
441 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
509a7696 442
443 printf("\n");
444 if (limit == 0) continue;
445
446 Int_t size = GetDataSize();
447 char line[70];
448 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
449 line[69] = '\0';
450 Int_t pos = 0;
451 Int_t max = 16;
452 UChar_t byte;
453
454 for (Int_t n = 0; n < size; n++) {
455 if (!ReadNextChar(byte)) {
456 Error("DumpData", "couldn't read byte number %d\n", n);
457 break;
458 }
459 if (pos >= max) {
460 printf("%8.8x %s\n", n-pos, line);
461 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
462 line[69] = '\0';
463 pos = 0;
464 if ((limit > 0) && (n/max == limit)) {
465 Int_t nContinue = ((size-1)/max+1-limit) * max;
466 if (nContinue > n) {
467 printf(" [skipping %d bytes]\n", nContinue-n);
468 n = nContinue-1;
469 continue;
470 }
471 }
472 }
473 Int_t offset = pos/4;
474 if ((byte > 0x20) && (byte < 0x7f)) {
475 line[pos+offset] = byte;
476 } else {
477 line[pos+offset] = '.';
478 }
479 char hex[3];
480 sprintf(hex, "%2.2x", byte);
481 line[max+max/4+3+2*pos+offset] = hex[0];
482 line[max+max/4+4+2*pos+offset] = hex[1];
483 pos++;
484 }
485
486 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
487 printf("\n");
488
489 } while (ReadHeader());
490}
38cf12f3 491
9fc249d9 492void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
493 Int_t code,
38cf12f3 494 const char *message)
495{
496 // Add a raw data error message to the list
497 // of raw-data decoding errors
498 if (fEventNumber < 0) {
499 AliError("No events have read so far! Impossible to add a raw data error log!");
500 return;
501 }
3aea4e5e 502 Int_t ddlId = GetEquipmentId();
38cf12f3 503 if (ddlId < 0) {
504 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
505 return;
506 }
507
79dac785 508 Int_t prevEventNumber = -1;
509 Int_t prevDdlId = -1;
510 Int_t prevErrorCode = -1;
511 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
512 if (prevLog) {
513 prevEventNumber = prevLog->GetEventNumber();
514 prevDdlId = prevLog->GetDdlID();
515 prevErrorCode = prevLog->GetErrorCode();
516 }
517
518 if ((prevEventNumber != fEventNumber) ||
519 (prevDdlId != ddlId) ||
520 (prevErrorCode != code)) {
521 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
522 AliRawDataErrorLog(fEventNumber,
523 ddlId,
524 level,
525 code,
526 message);
527 }
528 else
529 if (prevLog) prevLog->AddCount();
530
38cf12f3 531}