Bug fix
[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
a6e7b125 300
a6e7b125 301Bool_t AliRawReader::ReadNextInt(UInt_t& data)
302{
8de97894 303// reads the next 4 bytes at the current position
304// returns kFALSE if the data could not be read
a6e7b125 305
306 while (fCount == 0) {
c946ab02 307 if (!ReadHeader()) return kFALSE;
a6e7b125 308 }
8de97894 309 if (fCount < (Int_t) sizeof(data)) {
310 Error("ReadNextInt",
311 "too few data left (%d bytes) to read an UInt_t!", fCount);
312 return kFALSE;
313 }
314 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 315 Error("ReadNextInt", "could not read data!");
316 return kFALSE;
317 }
a6e7b125 318 return kTRUE;
319}
320
321Bool_t AliRawReader::ReadNextShort(UShort_t& data)
322{
8de97894 323// reads the next 2 bytes at the current position
324// returns kFALSE if the data could not be read
a6e7b125 325
326 while (fCount == 0) {
c946ab02 327 if (!ReadHeader()) return kFALSE;
a6e7b125 328 }
8de97894 329 if (fCount < (Int_t) sizeof(data)) {
330 Error("ReadNextShort",
331 "too few data left (%d bytes) to read an UShort_t!", fCount);
332 return kFALSE;
333 }
334 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 335 Error("ReadNextShort", "could not read data!");
336 return kFALSE;
337 }
a6e7b125 338 return kTRUE;
339}
340
341Bool_t AliRawReader::ReadNextChar(UChar_t& data)
342{
343// reads the next 1 byte at the current stream position
8de97894 344// returns kFALSE if the data could not be read
a6e7b125 345
346 while (fCount == 0) {
c946ab02 347 if (!ReadHeader()) return kFALSE;
a6e7b125 348 }
8de97894 349 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
a6e7b125 350 Error("ReadNextChar", "could not read data!");
351 return kFALSE;
352 }
a6e7b125 353 return kTRUE;
354}
355
b4857df7 356
357Int_t AliRawReader::CheckData() const
358{
359// check the consistency of the data
360// derived classes should overwrite the default method which returns 0 (no err)
361
362 return 0;
363}
364
509a7696 365
366void AliRawReader::DumpData(Int_t limit)
367{
368// print the raw data
369// if limit is not negative, only the first and last "limit" lines of raw data
370// are printed
371
372 Reset();
373 if (!ReadHeader()) {
374 Error("DumpData", "no header");
375 return;
376 }
377 printf("header:\n"
378 " type = %d run = %d ", GetType(), GetRunNumber());
379 if (GetEventId()) {
380 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
381 } else {
382 printf("event = -------- --------\n");
383 }
384 if (GetTriggerPattern()) {
385 printf(" trigger = %8.8x %8.8x ",
386 GetTriggerPattern()[1], GetTriggerPattern()[0]);
387 } else {
388 printf(" trigger = -------- -------- ");
389 }
390 if (GetDetectorPattern()) {
391 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
392 } else {
393 printf("detector = --------\n");
394 }
395 if (GetAttributes()) {
396 printf(" attributes = %8.8x %8.8x %8.8x ",
397 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
398 } else {
399 printf(" attributes = -------- -------- -------- ");
400 }
401 printf("GDC = %d\n", GetGDCId());
402 printf("\n");
403
404 do {
405 printf("-------------------------------------------------------------------------------\n");
406 printf("LDC = %d\n", GetLDCId());
407
408 printf("equipment:\n"
409 " size = %d type = %d id = %d\n",
410 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
411 if (GetEquipmentAttributes()) {
412 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
413 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
414 } else {
415 printf(" attributes = -------- -------- -------- ");
416 }
417 printf("element size = %d\n", GetEquipmentElementSize());
418
39f9963f 419 printf("data header:\n"
420 " size = %d version = %d valid = %d compression = %d\n",
421 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
509a7696 422
423 printf("\n");
424 if (limit == 0) continue;
425
426 Int_t size = GetDataSize();
427 char line[70];
428 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
429 line[69] = '\0';
430 Int_t pos = 0;
431 Int_t max = 16;
432 UChar_t byte;
433
434 for (Int_t n = 0; n < size; n++) {
435 if (!ReadNextChar(byte)) {
436 Error("DumpData", "couldn't read byte number %d\n", n);
437 break;
438 }
439 if (pos >= max) {
440 printf("%8.8x %s\n", n-pos, line);
441 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
442 line[69] = '\0';
443 pos = 0;
444 if ((limit > 0) && (n/max == limit)) {
445 Int_t nContinue = ((size-1)/max+1-limit) * max;
446 if (nContinue > n) {
447 printf(" [skipping %d bytes]\n", nContinue-n);
448 n = nContinue-1;
449 continue;
450 }
451 }
452 }
453 Int_t offset = pos/4;
454 if ((byte > 0x20) && (byte < 0x7f)) {
455 line[pos+offset] = byte;
456 } else {
457 line[pos+offset] = '.';
458 }
459 char hex[3];
460 sprintf(hex, "%2.2x", byte);
461 line[max+max/4+3+2*pos+offset] = hex[0];
462 line[max+max/4+4+2*pos+offset] = hex[1];
463 pos++;
464 }
465
466 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
467 printf("\n");
468
469 } while (ReadHeader());
470}
38cf12f3 471
9fc249d9 472void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
473 Int_t code,
38cf12f3 474 const char *message)
475{
476 // Add a raw data error message to the list
477 // of raw-data decoding errors
478 if (fEventNumber < 0) {
479 AliError("No events have read so far! Impossible to add a raw data error log!");
480 return;
481 }
482 Int_t ddlId = GetDDLID();
483 if (ddlId < 0) {
484 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
485 return;
486 }
487
79dac785 488 Int_t prevEventNumber = -1;
489 Int_t prevDdlId = -1;
490 Int_t prevErrorCode = -1;
491 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
492 if (prevLog) {
493 prevEventNumber = prevLog->GetEventNumber();
494 prevDdlId = prevLog->GetDdlID();
495 prevErrorCode = prevLog->GetErrorCode();
496 }
497
498 if ((prevEventNumber != fEventNumber) ||
499 (prevDdlId != ddlId) ||
500 (prevErrorCode != code)) {
501 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
502 AliRawDataErrorLog(fEventNumber,
503 ddlId,
504 level,
505 code,
506 message);
507 }
508 else
509 if (prevLog) prevLog->AddCount();
510
38cf12f3 511}