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;
301 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
303 // reads the next 4 bytes at the current position
304 // returns kFALSE if the data could not be read
306 while (fCount == 0) {
307 if (!ReadHeader()) return kFALSE;
309 if (fCount < (Int_t) sizeof(data)) {
311 "too few data left (%d bytes) to read an UInt_t!", fCount);
314 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
315 Error("ReadNextInt", "could not read data!");
321 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
323 // reads the next 2 bytes at the current position
324 // returns kFALSE if the data could not be read
326 while (fCount == 0) {
327 if (!ReadHeader()) return kFALSE;
329 if (fCount < (Int_t) sizeof(data)) {
330 Error("ReadNextShort",
331 "too few data left (%d bytes) to read an UShort_t!", fCount);
334 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
335 Error("ReadNextShort", "could not read data!");
341 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
343 // reads the next 1 byte at the current stream position
344 // returns kFALSE if the data could not be read
346 while (fCount == 0) {
347 if (!ReadHeader()) return kFALSE;
349 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
350 Error("ReadNextChar", "could not read data!");
357 Int_t AliRawReader::CheckData() const
359 // check the consistency of the data
360 // derived classes should overwrite the default method which returns 0 (no err)
366 void AliRawReader::DumpData(Int_t limit)
368 // print the raw data
369 // if limit is not negative, only the first and last "limit" lines of raw data
374 Error("DumpData", "no header");
378 " type = %d run = %d ", GetType(), GetRunNumber());
380 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
382 printf("event = -------- --------\n");
384 if (GetTriggerPattern()) {
385 printf(" trigger = %8.8x %8.8x ",
386 GetTriggerPattern()[1], GetTriggerPattern()[0]);
388 printf(" trigger = -------- -------- ");
390 if (GetDetectorPattern()) {
391 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
393 printf("detector = --------\n");
395 if (GetAttributes()) {
396 printf(" attributes = %8.8x %8.8x %8.8x ",
397 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
399 printf(" attributes = -------- -------- -------- ");
401 printf("GDC = %d\n", GetGDCId());
405 printf("-------------------------------------------------------------------------------\n");
406 printf("LDC = %d\n", GetLDCId());
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]);
415 printf(" attributes = -------- -------- -------- ");
417 printf("element size = %d\n", GetEquipmentElementSize());
419 printf("data header:\n"
420 " size = %d version = %d valid = %d compression = %d\n",
421 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
424 if (limit == 0) continue;
426 Int_t size = GetDataSize();
428 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
434 for (Int_t n = 0; n < size; n++) {
435 if (!ReadNextChar(byte)) {
436 Error("DumpData", "couldn't read byte number %d\n", n);
440 printf("%8.8x %s\n", n-pos, line);
441 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
444 if ((limit > 0) && (n/max == limit)) {
445 Int_t nContinue = ((size-1)/max+1-limit) * max;
447 printf(" [skipping %d bytes]\n", nContinue-n);
453 Int_t offset = pos/4;
454 if ((byte > 0x20) && (byte < 0x7f)) {
455 line[pos+offset] = byte;
457 line[pos+offset] = '.';
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];
466 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
469 } while (ReadHeader());
472 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
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!");
482 Int_t ddlId = GetDDLID();
484 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
488 Int_t prevEventNumber = -1;
489 Int_t prevDdlId = -1;
490 Int_t prevErrorCode = -1;
491 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
493 prevEventNumber = prevLog->GetEventNumber();
494 prevDdlId = prevLog->GetDdlID();
495 prevErrorCode = prevLog->GetErrorCode();
498 if ((prevEventNumber != fEventNumber) ||
499 (prevDdlId != ddlId) ||
500 (prevErrorCode != code)) {
501 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
502 AliRawDataErrorLog(fEventNumber,
509 if (prevLog) prevLog->AddCount();