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"
48 #include "AliRawReaderChain.h"
52 ClassImp(AliRawReader)
55 AliRawReader::AliRawReader() :
56 fEquipmentIdsIn(NULL),
57 fEquipmentIdsOut(NULL),
58 fRequireHeader(kTRUE),
61 fSelectEquipmentType(-1),
62 fSelectMinEquipmentId(-1),
63 fSelectMaxEquipmentId(-1),
66 fSelectTriggerMask(0),
69 fErrorLogs("AliRawDataErrorLog",100),
72 // default constructor: initialize data members
73 // Allocate the swapped header in case of Mac
75 fHeaderSwapped=new AliRawDataHeader();
79 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
81 // Open the mapping file
82 // and load the mapping data
83 ifstream input(fileName);
84 if (input.is_open()) {
85 Warning("AliRawReader","Equipment ID mapping file is found !");
86 const Int_t kMaxDDL = 256;
87 fEquipmentIdsIn = new TArrayI(kMaxDDL);
88 fEquipmentIdsOut = new TArrayI(kMaxDDL);
89 Int_t equipIn, equipOut;
91 while (input >> equipIn >> equipOut) {
92 if (nIds >= kMaxDDL) {
93 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
96 fEquipmentIdsIn->AddAt(equipIn,nIds);
97 fEquipmentIdsOut->AddAt(equipOut,nIds);
100 fEquipmentIdsIn->Set(nIds);
101 fEquipmentIdsOut->Set(nIds);
106 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
111 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
113 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
114 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
115 fRequireHeader(rawReader.fRequireHeader),
116 fHeader(rawReader.fHeader),
117 fCount(rawReader.fCount),
118 fSelectEquipmentType(rawReader.fSelectEquipmentType),
119 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
120 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
121 fSkipInvalid(rawReader.fSkipInvalid),
122 fSelectEventType(rawReader.fSelectEventType),
123 fSelectTriggerMask(rawReader.fSelectTriggerMask),
126 fErrorLogs("AliRawDataErrorLog",100),
130 // Allocate the swapped header in case of Mac
132 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
136 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
138 // assignment operator
139 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
140 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
142 fHeader = rawReader.fHeader;
143 fCount = rawReader.fCount;
145 fSelectEquipmentType = rawReader.fSelectEquipmentType;
146 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
147 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
148 fSkipInvalid = rawReader.fSkipInvalid;
149 fSelectEventType = rawReader.fSelectEventType;
150 fSelectTriggerMask = rawReader.fSelectTriggerMask;
152 fErrorCode = rawReader.fErrorCode;
154 fEventNumber = rawReader.fEventNumber;
155 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
160 AliRawReader::~AliRawReader()
163 // delete the mapping arrays if
165 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
166 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
168 if (fHeaderSwapped) delete fHeaderSwapped;
171 AliRawReader* AliRawReader::Create(const char *uri)
173 // RawReader's factory
174 // It instantiate corresponding raw-reader implementation class object
175 // depending on the URI provided
176 // Normal URIs point to files, while the URI starting with
177 // 'mem://:' or 'mem://<filename>' will create
178 // AliRawReaderDateOnline object which is supposed to be used
179 // in the online reconstruction
181 TString strURI = uri;
183 if (strURI.IsNull()) {
184 AliWarningClass("No raw-reader created");
188 TObjArray *fields = strURI.Tokenize("?");
189 TString &fileURI = ((TObjString*)fields->At(0))->String();
191 AliRawReader *rawReader = NULL;
192 if (fileURI.BeginsWith("mem://")) {
193 fileURI.ReplaceAll("mem://","");
194 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
196 TPluginManager* pluginManager = gROOT->GetPluginManager();
197 TString rawReaderName = "AliRawReaderDateOnline";
198 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
199 // if not, add a plugin for it
200 if (!pluginHandler) {
201 pluginManager->AddHandler("AliRawReader", "online",
202 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
203 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
205 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
206 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
213 else if (fileURI.BeginsWith("collection://")) {
214 fileURI.ReplaceAll("collection://","");
215 AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
216 rawReader = new AliRawReaderChain(fileURI);
219 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
220 if (fileURI.EndsWith("/")) {
221 rawReader = new AliRawReaderFile(fileURI);
222 } else if (fileURI.EndsWith(".root")) {
223 rawReader = new AliRawReaderRoot(fileURI);
225 rawReader = new AliRawReaderDate(fileURI);
229 // Now apply event selection criteria (if specified)
230 if (fields->GetEntries() > 1) {
231 Int_t eventType = -1;
232 ULong64_t triggerMask = 0;
233 for(Int_t i = 1; i < fields->GetEntries(); i++) {
234 if (!fields->At(i)) continue;
235 TString &option = ((TObjString*)fields->At(i))->String();
236 if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
237 option.ReplaceAll("EventType=","");
238 eventType = option.Atoi();
241 if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
242 option.ReplaceAll("Trigger=","");
243 triggerMask = option.Atoll();
246 AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
248 AliInfoClass(Form("Event selection criteria specified: eventype=%d trigger mask=%llx",
249 eventType,triggerMask));
250 rawReader->SelectEvents(eventType,triggerMask);
259 Int_t AliRawReader::GetMappedEquipmentId() const
261 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
262 Error("AliRawReader","equipment Ids mapping is not initialized !");
263 return GetEquipmentId();
265 Int_t equipmentId = GetEquipmentId();
266 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
267 if (equipmentId == fEquipmentIdsIn->At(iId)) {
268 equipmentId = fEquipmentIdsOut->At(iId);
275 Int_t AliRawReader::GetDetectorID() const
277 // Get the detector ID
278 // The list of detector IDs
279 // can be found in AliDAQ.h
281 if (fEquipmentIdsIn && fEquipmentIdsIn)
282 equipmentId = GetMappedEquipmentId();
284 equipmentId = GetEquipmentId();
286 if (equipmentId >= 0) {
288 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
294 Int_t AliRawReader::GetDDLID() const
296 // Get the DDL ID (within one sub-detector)
297 // The list of detector IDs
298 // can be found in AliDAQ.h
300 if (fEquipmentIdsIn && fEquipmentIdsIn)
301 equipmentId = GetMappedEquipmentId();
303 equipmentId = GetEquipmentId();
305 if (equipmentId >= 0) {
307 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
314 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
316 // read only data of the detector with the given name and in the given
317 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
318 // no selection is applied if a value < 0 is used.
319 Int_t detectorID = AliDAQ::DetectorID(detectorName);
321 Select(detectorID,minDDLID,maxDDLID);
324 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
326 // read only data of the detector with the given ID and in the given
327 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
328 // no selection is applied if a value < 0 is used.
330 fSelectEquipmentType = -1;
333 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
335 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
338 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
340 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
343 void AliRawReader::SelectEquipment(Int_t equipmentType,
344 Int_t minEquipmentId, Int_t maxEquipmentId)
346 // read only data of the equipment with the given type and in the given
347 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
348 // no selection is applied if a value < 0 is used.
350 fSelectEquipmentType = equipmentType;
351 fSelectMinEquipmentId = minEquipmentId;
352 fSelectMaxEquipmentId = maxEquipmentId;
355 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask)
357 // read only events with the given type and optionally
359 // no selection is applied if a value < 0 is used.
361 fSelectEventType = type;
362 fSelectTriggerMask = triggerMask;
365 Bool_t AliRawReader::IsSelected() const
367 // apply the selection (if any)
369 if (fSkipInvalid && !IsValid()) return kFALSE;
371 if (fSelectEquipmentType >= 0)
372 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
375 if (fEquipmentIdsIn && fEquipmentIdsIn)
376 equipmentId = GetMappedEquipmentId();
378 equipmentId = GetEquipmentId();
380 if ((fSelectMinEquipmentId >= 0) &&
381 (equipmentId < fSelectMinEquipmentId))
383 if ((fSelectMaxEquipmentId >= 0) &&
384 (equipmentId > fSelectMaxEquipmentId))
390 Bool_t AliRawReader::IsEventSelected() const
392 // apply the event selection (if any)
394 // First check the event type
395 if (fSelectEventType >= 0) {
396 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
399 // Then check the trigger pattern and compared it
400 // to the required trigger mask
401 if (fSelectTriggerMask != 0) {
402 if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
408 UInt_t AliRawReader::SwapWord(UInt_t x) const
410 // Swap the endianess of the integer value 'x'
412 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
413 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
416 UShort_t AliRawReader::SwapShort(UShort_t x) const
418 // Swap the endianess of the short value 'x'
420 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
423 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
425 // reads the next 4 bytes at the current position
426 // returns kFALSE if the data could not be read
428 while (fCount == 0) {
429 if (!ReadHeader()) return kFALSE;
431 if (fCount < (Int_t) sizeof(data)) {
433 "too few data left (%d bytes) to read an UInt_t!", fCount);
436 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
437 Error("ReadNextInt", "could not read data!");
446 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
448 // reads the next 2 bytes at the current position
449 // returns kFALSE if the data could not be read
451 while (fCount == 0) {
452 if (!ReadHeader()) return kFALSE;
454 if (fCount < (Int_t) sizeof(data)) {
455 Error("ReadNextShort",
456 "too few data left (%d bytes) to read an UShort_t!", fCount);
459 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
460 Error("ReadNextShort", "could not read data!");
464 data=SwapShort(data);
469 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
471 // reads the next 1 byte at the current stream position
472 // returns kFALSE if the data could not be read
474 while (fCount == 0) {
475 if (!ReadHeader()) return kFALSE;
477 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
478 Error("ReadNextChar", "could not read data!");
485 Int_t AliRawReader::CheckData() const
487 // check the consistency of the data
488 // derived classes should overwrite the default method which returns 0 (no err)
494 void AliRawReader::DumpData(Int_t limit)
496 // print the raw data
497 // if limit is not negative, only the first and last "limit" lines of raw data
502 Error("DumpData", "no header");
506 " type = %d run = %d ", GetType(), GetRunNumber());
508 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
510 printf("event = -------- --------\n");
512 if (GetTriggerPattern()) {
513 printf(" trigger = %8.8x %8.8x ",
514 GetTriggerPattern()[1], GetTriggerPattern()[0]);
516 printf(" trigger = -------- -------- ");
518 if (GetDetectorPattern()) {
519 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
521 printf("detector = --------\n");
523 if (GetAttributes()) {
524 printf(" attributes = %8.8x %8.8x %8.8x ",
525 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
527 printf(" attributes = -------- -------- -------- ");
529 printf("GDC = %d\n", GetGDCId());
533 printf("-------------------------------------------------------------------------------\n");
534 printf("LDC = %d\n", GetLDCId());
536 printf("equipment:\n"
537 " size = %d type = %d id = %d\n",
538 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
539 if (GetEquipmentAttributes()) {
540 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
541 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
543 printf(" attributes = -------- -------- -------- ");
545 printf("element size = %d\n", GetEquipmentElementSize());
547 printf("data header:\n"
548 " size = %d version = %d valid = %d compression = %d\n",
549 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
552 if (limit == 0) continue;
554 Int_t size = GetDataSize();
556 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
562 for (Int_t n = 0; n < size; n++) {
563 if (!ReadNextChar(byte)) {
564 Error("DumpData", "couldn't read byte number %d\n", n);
568 printf("%8.8x %s\n", n-pos, line);
569 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
572 if ((limit > 0) && (n/max == limit)) {
573 Int_t nContinue = ((size-1)/max+1-limit) * max;
575 printf(" [skipping %d bytes]\n", nContinue-n);
581 Int_t offset = pos/4;
582 if ((byte > 0x20) && (byte < 0x7f)) {
583 line[pos+offset] = byte;
585 line[pos+offset] = '.';
588 sprintf(hex, "%2.2x", byte);
589 line[max+max/4+3+2*pos+offset] = hex[0];
590 line[max+max/4+4+2*pos+offset] = hex[1];
594 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
597 } while (ReadHeader());
600 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
604 // Add a raw data error message to the list
605 // of raw-data decoding errors
606 if (fEventNumber < 0) {
609 Int_t ddlId = GetEquipmentId();
611 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
615 Int_t prevEventNumber = -1;
616 Int_t prevDdlId = -1;
617 Int_t prevErrorCode = -1;
618 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
620 prevEventNumber = prevLog->GetEventNumber();
621 prevDdlId = prevLog->GetDdlID();
622 prevErrorCode = prevLog->GetErrorCode();
625 if ((prevEventNumber != fEventNumber) ||
626 (prevDdlId != ddlId) ||
627 (prevErrorCode != code)) {
628 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
629 AliRawDataErrorLog(fEventNumber,
636 if (prevLog) prevLog->AddCount();