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 ///////////////////////////////////////////////////////////////////////////////
40 #include <TPluginManager.h>
43 #include <Riostream.h>
44 #include "AliRawReader.h"
45 #include "AliRawReaderFile.h"
46 #include "AliRawReaderDate.h"
47 #include "AliRawReaderRoot.h"
51 ClassImp(AliRawReader)
54 AliRawReader::AliRawReader() :
55 fEquipmentIdsIn(NULL),
56 fEquipmentIdsOut(NULL),
57 fRequireHeader(kTRUE),
60 fSelectEquipmentType(-1),
61 fSelectMinEquipmentId(-1),
62 fSelectMaxEquipmentId(-1),
67 fErrorLogs("AliRawDataErrorLog",100),
70 // default constructor: initialize data members
71 // Allocate the swapped header in case of Mac
73 fHeaderSwapped=new AliRawDataHeader();
77 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
79 // Open the mapping file
80 // and load the mapping data
81 ifstream input(fileName);
82 if (input.is_open()) {
83 Warning("AliRawReader","Equipment ID mapping file is found !");
84 const Int_t kMaxDDL = 256;
85 fEquipmentIdsIn = new TArrayI(kMaxDDL);
86 fEquipmentIdsOut = new TArrayI(kMaxDDL);
87 Int_t equipIn, equipOut;
89 while (input >> equipIn >> equipOut) {
90 if (nIds >= kMaxDDL) {
91 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
94 fEquipmentIdsIn->AddAt(equipIn,nIds);
95 fEquipmentIdsOut->AddAt(equipOut,nIds);
98 fEquipmentIdsIn->Set(nIds);
99 fEquipmentIdsOut->Set(nIds);
104 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
109 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
111 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
112 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
113 fRequireHeader(rawReader.fRequireHeader),
114 fHeader(rawReader.fHeader),
115 fCount(rawReader.fCount),
116 fSelectEquipmentType(rawReader.fSelectEquipmentType),
117 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
118 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
119 fSkipInvalid(rawReader.fSkipInvalid),
120 fSelectEventType(rawReader.fSelectEventType),
123 fErrorLogs("AliRawDataErrorLog",100),
127 // Allocate the swapped header in case of Mac
129 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
133 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
135 // assignment operator
136 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
137 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
139 fHeader = rawReader.fHeader;
140 fCount = rawReader.fCount;
142 fSelectEquipmentType = rawReader.fSelectEquipmentType;
143 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
144 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
145 fSkipInvalid = rawReader.fSkipInvalid;
146 fSelectEventType = rawReader.fSelectEventType;
148 fErrorCode = rawReader.fErrorCode;
150 fEventNumber = rawReader.fEventNumber;
151 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
156 AliRawReader::~AliRawReader()
159 // delete the mapping arrays if
161 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
162 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
164 if (fHeaderSwapped) delete fHeaderSwapped;
167 AliRawReader* AliRawReader::Create(const char *uri)
169 // RawReader's factory
170 // It instantiate corresponding raw-reader implementation class object
171 // depending on the URI provided
172 // Normal URIs point to files, while the URI starting with
173 // 'mem://:' or 'mem://<filename>' will create
174 // AliRawReaderDateOnline object which is supposed to be used
175 // in the online reconstruction
177 TString strURI = uri;
179 if (strURI.IsNull()) {
180 AliWarningClass("No raw-reader created");
184 AliRawReader *rawReader = NULL;
185 if (!strURI.BeginsWith("mem://")) {
186 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",strURI.Data()));
187 if (strURI.EndsWith("/")) {
188 rawReader = new AliRawReaderFile(strURI);
189 } else if (strURI.EndsWith(".root")) {
190 rawReader = new AliRawReaderRoot(strURI);
192 rawReader = new AliRawReaderDate(strURI);
196 strURI.ReplaceAll("mem://","");
197 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",strURI.Data()));
199 TPluginManager* pluginManager = gROOT->GetPluginManager();
200 TString rawReaderName = "AliRawReaderDateOnline";
201 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
202 // if not, add a plugin for it
203 if (!pluginHandler) {
204 pluginManager->AddHandler("AliRawReader", "online",
205 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
206 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
208 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
209 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,strURI.Data());
219 Int_t AliRawReader::GetMappedEquipmentId() const
221 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
222 Error("AliRawReader","equipment Ids mapping is not initialized !");
223 return GetEquipmentId();
225 Int_t equipmentId = GetEquipmentId();
226 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
227 if (equipmentId == fEquipmentIdsIn->At(iId)) {
228 equipmentId = fEquipmentIdsOut->At(iId);
235 Int_t AliRawReader::GetDetectorID() const
237 // Get the detector ID
238 // The list of detector IDs
239 // can be found in AliDAQ.h
241 if (fEquipmentIdsIn && fEquipmentIdsIn)
242 equipmentId = GetMappedEquipmentId();
244 equipmentId = GetEquipmentId();
246 if (equipmentId >= 0) {
248 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
254 Int_t AliRawReader::GetDDLID() const
256 // Get the DDL ID (within one sub-detector)
257 // The list of detector IDs
258 // can be found in AliDAQ.h
260 if (fEquipmentIdsIn && fEquipmentIdsIn)
261 equipmentId = GetMappedEquipmentId();
263 equipmentId = GetEquipmentId();
265 if (equipmentId >= 0) {
267 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
274 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
276 // read only data of the detector with the given name and in the given
277 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
278 // no selection is applied if a value < 0 is used.
279 Int_t detectorID = AliDAQ::DetectorID(detectorName);
281 Select(detectorID,minDDLID,maxDDLID);
284 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
286 // read only data of the detector with the given ID and in the given
287 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
288 // no selection is applied if a value < 0 is used.
290 fSelectEquipmentType = -1;
293 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
295 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
298 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
300 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
303 void AliRawReader::SelectEquipment(Int_t equipmentType,
304 Int_t minEquipmentId, Int_t maxEquipmentId)
306 // read only data of the equipment with the given type and in the given
307 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
308 // no selection is applied if a value < 0 is used.
310 fSelectEquipmentType = equipmentType;
311 fSelectMinEquipmentId = minEquipmentId;
312 fSelectMaxEquipmentId = maxEquipmentId;
315 void AliRawReader::SelectEvents(Int_t type)
317 // read only events with the given type.
318 // no selection is applied if a value < 0 is used.
320 fSelectEventType = type;
323 Bool_t AliRawReader::IsSelected() const
325 // apply the selection (if any)
327 if (fSkipInvalid && !IsValid()) return kFALSE;
329 if (fSelectEquipmentType >= 0)
330 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
333 if (fEquipmentIdsIn && fEquipmentIdsIn)
334 equipmentId = GetMappedEquipmentId();
336 equipmentId = GetEquipmentId();
338 if ((fSelectMinEquipmentId >= 0) &&
339 (equipmentId < fSelectMinEquipmentId))
341 if ((fSelectMaxEquipmentId >= 0) &&
342 (equipmentId > fSelectMaxEquipmentId))
348 Bool_t AliRawReader::IsEventSelected() const
350 // apply the event selection (if any)
352 if (fSelectEventType >= 0) {
353 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
359 UInt_t AliRawReader::SwapWord(UInt_t x) const
361 // Swap the endianess of the integer value 'x'
363 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
364 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
367 UShort_t AliRawReader::SwapShort(UShort_t x) const
369 // Swap the endianess of the short value 'x'
371 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
374 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
376 // reads the next 4 bytes at the current position
377 // returns kFALSE if the data could not be read
379 while (fCount == 0) {
380 if (!ReadHeader()) return kFALSE;
382 if (fCount < (Int_t) sizeof(data)) {
384 "too few data left (%d bytes) to read an UInt_t!", fCount);
387 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
388 Error("ReadNextInt", "could not read data!");
397 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
399 // reads the next 2 bytes at the current position
400 // returns kFALSE if the data could not be read
402 while (fCount == 0) {
403 if (!ReadHeader()) return kFALSE;
405 if (fCount < (Int_t) sizeof(data)) {
406 Error("ReadNextShort",
407 "too few data left (%d bytes) to read an UShort_t!", fCount);
410 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
411 Error("ReadNextShort", "could not read data!");
415 data=SwapShort(data);
420 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
422 // reads the next 1 byte at the current stream position
423 // returns kFALSE if the data could not be read
425 while (fCount == 0) {
426 if (!ReadHeader()) return kFALSE;
428 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
429 Error("ReadNextChar", "could not read data!");
436 Int_t AliRawReader::CheckData() const
438 // check the consistency of the data
439 // derived classes should overwrite the default method which returns 0 (no err)
445 void AliRawReader::DumpData(Int_t limit)
447 // print the raw data
448 // if limit is not negative, only the first and last "limit" lines of raw data
453 Error("DumpData", "no header");
457 " type = %d run = %d ", GetType(), GetRunNumber());
459 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
461 printf("event = -------- --------\n");
463 if (GetTriggerPattern()) {
464 printf(" trigger = %8.8x %8.8x ",
465 GetTriggerPattern()[1], GetTriggerPattern()[0]);
467 printf(" trigger = -------- -------- ");
469 if (GetDetectorPattern()) {
470 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
472 printf("detector = --------\n");
474 if (GetAttributes()) {
475 printf(" attributes = %8.8x %8.8x %8.8x ",
476 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
478 printf(" attributes = -------- -------- -------- ");
480 printf("GDC = %d\n", GetGDCId());
484 printf("-------------------------------------------------------------------------------\n");
485 printf("LDC = %d\n", GetLDCId());
487 printf("equipment:\n"
488 " size = %d type = %d id = %d\n",
489 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
490 if (GetEquipmentAttributes()) {
491 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
492 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
494 printf(" attributes = -------- -------- -------- ");
496 printf("element size = %d\n", GetEquipmentElementSize());
498 printf("data header:\n"
499 " size = %d version = %d valid = %d compression = %d\n",
500 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
503 if (limit == 0) continue;
505 Int_t size = GetDataSize();
507 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
513 for (Int_t n = 0; n < size; n++) {
514 if (!ReadNextChar(byte)) {
515 Error("DumpData", "couldn't read byte number %d\n", n);
519 printf("%8.8x %s\n", n-pos, line);
520 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
523 if ((limit > 0) && (n/max == limit)) {
524 Int_t nContinue = ((size-1)/max+1-limit) * max;
526 printf(" [skipping %d bytes]\n", nContinue-n);
532 Int_t offset = pos/4;
533 if ((byte > 0x20) && (byte < 0x7f)) {
534 line[pos+offset] = byte;
536 line[pos+offset] = '.';
539 sprintf(hex, "%2.2x", byte);
540 line[max+max/4+3+2*pos+offset] = hex[0];
541 line[max+max/4+4+2*pos+offset] = hex[1];
545 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
548 } while (ReadHeader());
551 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
555 // Add a raw data error message to the list
556 // of raw-data decoding errors
557 if (fEventNumber < 0) {
558 AliError("No events have read so far! Impossible to add a raw data error log!");
561 Int_t ddlId = GetEquipmentId();
563 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
567 Int_t prevEventNumber = -1;
568 Int_t prevDdlId = -1;
569 Int_t prevErrorCode = -1;
570 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
572 prevEventNumber = prevLog->GetEventNumber();
573 prevDdlId = prevLog->GetDdlID();
574 prevErrorCode = prevLog->GetErrorCode();
577 if ((prevEventNumber != fEventNumber) ||
578 (prevDdlId != ddlId) ||
579 (prevErrorCode != code)) {
580 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
581 AliRawDataErrorLog(fEventNumber,
588 if (prevLog) prevLog->AddCount();