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