1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This is the base class for reading raw data.
22 /// The derived classes, which operate on concrete raw data formats,
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
29 /// Sequential access to the raw data is provided by the methods
30 /// ReadHeader, ReadNextData, ReadNextInt, ReadNextShort, ReadNextChar
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.
37 ///////////////////////////////////////////////////////////////////////////////
39 #include <Riostream.h>
40 #include "AliRawReader.h"
44 ClassImp(AliRawReader)
47 AliRawReader::AliRawReader() :
48 fEquipmentIdsIn(NULL),
49 fEquipmentIdsOut(NULL),
50 fRequireHeader(kTRUE),
53 fSelectEquipmentType(-1),
54 fSelectMinEquipmentId(-1),
55 fSelectMaxEquipmentId(-1),
60 fErrorLogs("AliRawDataErrorLog",100),
63 // default constructor: initialize data members
64 // Allocate the swapped header in case of Mac
66 fHeaderSwapped=new AliRawDataHeader();
70 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
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;
82 while (input >> equipIn >> equipOut) {
83 if (nIds >= kMaxDDL) {
84 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
87 fEquipmentIdsIn->AddAt(equipIn,nIds);
88 fEquipmentIdsOut->AddAt(equipOut,nIds);
91 fEquipmentIdsIn->Set(nIds);
92 fEquipmentIdsOut->Set(nIds);
97 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
102 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
104 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
105 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
106 fRequireHeader(rawReader.fRequireHeader),
107 fHeader(rawReader.fHeader),
108 fCount(rawReader.fCount),
109 fSelectEquipmentType(rawReader.fSelectEquipmentType),
110 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
111 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
112 fSkipInvalid(rawReader.fSkipInvalid),
113 fSelectEventType(rawReader.fSelectEventType),
116 fErrorLogs("AliRawDataErrorLog",100),
120 // Allocate the swapped header in case of Mac
122 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
126 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
128 // assignment operator
129 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
130 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
132 fHeader = rawReader.fHeader;
133 fCount = rawReader.fCount;
135 fSelectEquipmentType = rawReader.fSelectEquipmentType;
136 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
137 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
138 fSkipInvalid = rawReader.fSkipInvalid;
139 fSelectEventType = rawReader.fSelectEventType;
141 fErrorCode = rawReader.fErrorCode;
143 fEventNumber = rawReader.fEventNumber;
144 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
149 AliRawReader::~AliRawReader()
152 // delete the mapping arrays if
154 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
155 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
157 if (fHeaderSwapped) delete fHeaderSwapped;
160 Int_t AliRawReader::GetMappedEquipmentId() const
162 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
163 Error("AliRawReader","equipment Ids mapping is not initialized !");
164 return GetEquipmentId();
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);
176 Int_t AliRawReader::GetDetectorID() const
178 // Get the detector ID
179 // The list of detector IDs
180 // can be found in AliDAQ.h
182 if (fEquipmentIdsIn && fEquipmentIdsIn)
183 equipmentId = GetMappedEquipmentId();
185 equipmentId = GetEquipmentId();
187 if (equipmentId >= 0) {
189 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
195 Int_t AliRawReader::GetDDLID() const
197 // Get the DDL ID (within one sub-detector)
198 // The list of detector IDs
199 // can be found in AliDAQ.h
201 if (fEquipmentIdsIn && fEquipmentIdsIn)
202 equipmentId = GetMappedEquipmentId();
204 equipmentId = GetEquipmentId();
206 if (equipmentId >= 0) {
208 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
215 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
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);
222 Select(detectorID,minDDLID,maxDDLID);
225 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
227 // read only data of the detector with the given ID and in the given
228 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
229 // no selection is applied if a value < 0 is used.
231 fSelectEquipmentType = -1;
234 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
236 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
239 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
241 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
244 void AliRawReader::SelectEquipment(Int_t equipmentType,
245 Int_t minEquipmentId, Int_t maxEquipmentId)
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.
251 fSelectEquipmentType = equipmentType;
252 fSelectMinEquipmentId = minEquipmentId;
253 fSelectMaxEquipmentId = maxEquipmentId;
256 void AliRawReader::SelectEvents(Int_t type)
258 // read only events with the given type.
259 // no selection is applied if a value < 0 is used.
261 fSelectEventType = type;
264 Bool_t AliRawReader::IsSelected() const
266 // apply the selection (if any)
268 if (fSkipInvalid && !IsValid()) return kFALSE;
270 if (fSelectEquipmentType >= 0)
271 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
274 if (fEquipmentIdsIn && fEquipmentIdsIn)
275 equipmentId = GetMappedEquipmentId();
277 equipmentId = GetEquipmentId();
279 if ((fSelectMinEquipmentId >= 0) &&
280 (equipmentId < fSelectMinEquipmentId))
282 if ((fSelectMaxEquipmentId >= 0) &&
283 (equipmentId > fSelectMaxEquipmentId))
289 Bool_t AliRawReader::IsEventSelected() const
291 // apply the event selection (if any)
293 if (fSelectEventType >= 0) {
294 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
300 UInt_t AliRawReader::SwapWord(UInt_t x) const
302 // Swap the endianess of the integer value 'x'
304 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
305 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
308 UShort_t AliRawReader::SwapShort(UShort_t x) const
310 // Swap the endianess of the short value 'x'
312 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
315 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
317 // reads the next 4 bytes at the current position
318 // returns kFALSE if the data could not be read
320 while (fCount == 0) {
321 if (!ReadHeader()) return kFALSE;
323 if (fCount < (Int_t) sizeof(data)) {
325 "too few data left (%d bytes) to read an UInt_t!", fCount);
328 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
329 Error("ReadNextInt", "could not read data!");
338 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
340 // reads the next 2 bytes at the current position
341 // returns kFALSE if the data could not be read
343 while (fCount == 0) {
344 if (!ReadHeader()) return kFALSE;
346 if (fCount < (Int_t) sizeof(data)) {
347 Error("ReadNextShort",
348 "too few data left (%d bytes) to read an UShort_t!", fCount);
351 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
352 Error("ReadNextShort", "could not read data!");
356 data=SwapShort(data);
361 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
363 // reads the next 1 byte at the current stream position
364 // returns kFALSE if the data could not be read
366 while (fCount == 0) {
367 if (!ReadHeader()) return kFALSE;
369 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
370 Error("ReadNextChar", "could not read data!");
377 Int_t AliRawReader::CheckData() const
379 // check the consistency of the data
380 // derived classes should overwrite the default method which returns 0 (no err)
386 void AliRawReader::DumpData(Int_t limit)
388 // print the raw data
389 // if limit is not negative, only the first and last "limit" lines of raw data
394 Error("DumpData", "no header");
398 " type = %d run = %d ", GetType(), GetRunNumber());
400 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
402 printf("event = -------- --------\n");
404 if (GetTriggerPattern()) {
405 printf(" trigger = %8.8x %8.8x ",
406 GetTriggerPattern()[1], GetTriggerPattern()[0]);
408 printf(" trigger = -------- -------- ");
410 if (GetDetectorPattern()) {
411 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
413 printf("detector = --------\n");
415 if (GetAttributes()) {
416 printf(" attributes = %8.8x %8.8x %8.8x ",
417 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
419 printf(" attributes = -------- -------- -------- ");
421 printf("GDC = %d\n", GetGDCId());
425 printf("-------------------------------------------------------------------------------\n");
426 printf("LDC = %d\n", GetLDCId());
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]);
435 printf(" attributes = -------- -------- -------- ");
437 printf("element size = %d\n", GetEquipmentElementSize());
439 printf("data header:\n"
440 " size = %d version = %d valid = %d compression = %d\n",
441 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
444 if (limit == 0) continue;
446 Int_t size = GetDataSize();
448 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
454 for (Int_t n = 0; n < size; n++) {
455 if (!ReadNextChar(byte)) {
456 Error("DumpData", "couldn't read byte number %d\n", n);
460 printf("%8.8x %s\n", n-pos, line);
461 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
464 if ((limit > 0) && (n/max == limit)) {
465 Int_t nContinue = ((size-1)/max+1-limit) * max;
467 printf(" [skipping %d bytes]\n", nContinue-n);
473 Int_t offset = pos/4;
474 if ((byte > 0x20) && (byte < 0x7f)) {
475 line[pos+offset] = byte;
477 line[pos+offset] = '.';
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];
486 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
489 } while (ReadHeader());
492 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
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!");
502 Int_t ddlId = GetEquipmentId();
504 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
508 Int_t prevEventNumber = -1;
509 Int_t prevDdlId = -1;
510 Int_t prevErrorCode = -1;
511 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
513 prevEventNumber = prevLog->GetEventNumber();
514 prevDdlId = prevLog->GetDdlID();
515 prevErrorCode = prevLog->GetErrorCode();
518 if ((prevEventNumber != fEventNumber) ||
519 (prevDdlId != ddlId) ||
520 (prevErrorCode != code)) {
521 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
522 AliRawDataErrorLog(fEventNumber,
529 if (prevLog) prevLog->AddCount();