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),
73 // default constructor: initialize data members
74 // Allocate the swapped header in case of Mac
76 fHeaderSwapped=new AliRawDataHeader();
80 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
82 // Open the mapping file
83 // and load the mapping data
84 ifstream input(fileName);
85 if (input.is_open()) {
86 Warning("AliRawReader","Equipment ID mapping file is found !");
87 const Int_t kMaxDDL = 256;
88 fEquipmentIdsIn = new TArrayI(kMaxDDL);
89 fEquipmentIdsOut = new TArrayI(kMaxDDL);
90 Int_t equipIn, equipOut;
92 while (input >> equipIn >> equipOut) {
93 if (nIds >= kMaxDDL) {
94 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
97 fEquipmentIdsIn->AddAt(equipIn,nIds);
98 fEquipmentIdsOut->AddAt(equipOut,nIds);
101 fEquipmentIdsIn->Set(nIds);
102 fEquipmentIdsOut->Set(nIds);
107 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
112 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
114 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
115 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
116 fRequireHeader(rawReader.fRequireHeader),
117 fHeader(rawReader.fHeader),
118 fCount(rawReader.fCount),
119 fSelectEquipmentType(rawReader.fSelectEquipmentType),
120 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
121 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
122 fSkipInvalid(rawReader.fSkipInvalid),
123 fSelectEventType(rawReader.fSelectEventType),
124 fSelectTriggerMask(rawReader.fSelectTriggerMask),
127 fErrorLogs("AliRawDataErrorLog",100),
128 fHeaderSwapped(NULL),
129 fIsValid(rawReader.fIsValid)
132 // Allocate the swapped header in case of Mac
134 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
138 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
140 // assignment operator
141 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
142 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
144 fHeader = rawReader.fHeader;
145 fCount = rawReader.fCount;
147 fSelectEquipmentType = rawReader.fSelectEquipmentType;
148 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
149 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
150 fSkipInvalid = rawReader.fSkipInvalid;
151 fSelectEventType = rawReader.fSelectEventType;
152 fSelectTriggerMask = rawReader.fSelectTriggerMask;
154 fErrorCode = rawReader.fErrorCode;
156 fEventNumber = rawReader.fEventNumber;
157 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
159 fIsValid = rawReader.fIsValid;
164 AliRawReader::~AliRawReader()
167 // delete the mapping arrays if
169 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
170 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
172 if (fHeaderSwapped) delete fHeaderSwapped;
175 AliRawReader* AliRawReader::Create(const char *uri)
177 // RawReader's factory
178 // It instantiate corresponding raw-reader implementation class object
179 // depending on the URI provided
180 // Normal URIs point to files, while the URI starting with
181 // 'mem://:' or 'mem://<filename>' will create
182 // AliRawReaderDateOnline object which is supposed to be used
183 // in the online reconstruction
185 TString strURI = uri;
187 if (strURI.IsNull()) {
188 AliWarningClass("No raw-reader created");
192 TObjArray *fields = strURI.Tokenize("?");
193 TString &fileURI = ((TObjString*)fields->At(0))->String();
195 AliRawReader *rawReader = NULL;
196 if (fileURI.BeginsWith("mem://")) {
197 fileURI.ReplaceAll("mem://","");
198 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
200 TPluginManager* pluginManager = gROOT->GetPluginManager();
201 TString rawReaderName = "AliRawReaderDateOnline";
202 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
203 // if not, add a plugin for it
204 if (!pluginHandler) {
205 pluginManager->AddHandler("AliRawReader", "online",
206 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
207 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
209 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
210 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
217 else if (fileURI.BeginsWith("collection://")) {
218 fileURI.ReplaceAll("collection://","");
219 AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
220 rawReader = new AliRawReaderChain(fileURI);
223 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
224 if (fileURI.EndsWith("/")) {
225 rawReader = new AliRawReaderFile(fileURI);
226 } else if (fileURI.EndsWith(".root")) {
227 rawReader = new AliRawReaderRoot(fileURI);
229 rawReader = new AliRawReaderDate(fileURI);
233 if (!rawReader->IsRawReaderValid()) {
234 AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
241 // Now apply event selection criteria (if specified)
242 if (fields->GetEntries() > 1) {
243 Int_t eventType = -1;
244 ULong64_t triggerMask = 0;
245 for(Int_t i = 1; i < fields->GetEntries(); i++) {
246 if (!fields->At(i)) continue;
247 TString &option = ((TObjString*)fields->At(i))->String();
248 if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
249 option.ReplaceAll("EventType=","");
250 eventType = option.Atoi();
253 if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
254 option.ReplaceAll("Trigger=","");
255 triggerMask = option.Atoll();
258 AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
260 AliInfoClass(Form("Event selection criteria specified: eventype=%d trigger mask=%llx",
261 eventType,triggerMask));
262 rawReader->SelectEvents(eventType,triggerMask);
271 Int_t AliRawReader::GetMappedEquipmentId() const
273 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
274 Error("AliRawReader","equipment Ids mapping is not initialized !");
275 return GetEquipmentId();
277 Int_t equipmentId = GetEquipmentId();
278 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
279 if (equipmentId == fEquipmentIdsIn->At(iId)) {
280 equipmentId = fEquipmentIdsOut->At(iId);
287 Int_t AliRawReader::GetDetectorID() const
289 // Get the detector ID
290 // The list of detector IDs
291 // can be found in AliDAQ.h
293 if (fEquipmentIdsIn && fEquipmentIdsIn)
294 equipmentId = GetMappedEquipmentId();
296 equipmentId = GetEquipmentId();
298 if (equipmentId >= 0) {
300 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
306 Int_t AliRawReader::GetDDLID() const
308 // Get the DDL ID (within one sub-detector)
309 // The list of detector IDs
310 // can be found in AliDAQ.h
312 if (fEquipmentIdsIn && fEquipmentIdsIn)
313 equipmentId = GetMappedEquipmentId();
315 equipmentId = GetEquipmentId();
317 if (equipmentId >= 0) {
319 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
326 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
328 // read only data of the detector with the given name and in the given
329 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
330 // no selection is applied if a value < 0 is used.
331 Int_t detectorID = AliDAQ::DetectorID(detectorName);
333 Select(detectorID,minDDLID,maxDDLID);
336 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
338 // read only data of the detector with the given ID and in the given
339 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
340 // no selection is applied if a value < 0 is used.
342 fSelectEquipmentType = -1;
345 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
347 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
350 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
352 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
355 void AliRawReader::SelectEquipment(Int_t equipmentType,
356 Int_t minEquipmentId, Int_t maxEquipmentId)
358 // read only data of the equipment with the given type and in the given
359 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
360 // no selection is applied if a value < 0 is used.
362 fSelectEquipmentType = equipmentType;
363 fSelectMinEquipmentId = minEquipmentId;
364 fSelectMaxEquipmentId = maxEquipmentId;
367 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask)
369 // read only events with the given type and optionally
371 // no selection is applied if a value < 0 is used.
373 fSelectEventType = type;
374 fSelectTriggerMask = triggerMask;
377 Bool_t AliRawReader::IsSelected() const
379 // apply the selection (if any)
381 if (fSkipInvalid && !IsValid()) return kFALSE;
383 if (fSelectEquipmentType >= 0)
384 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
387 if (fEquipmentIdsIn && fEquipmentIdsIn)
388 equipmentId = GetMappedEquipmentId();
390 equipmentId = GetEquipmentId();
392 if ((fSelectMinEquipmentId >= 0) &&
393 (equipmentId < fSelectMinEquipmentId))
395 if ((fSelectMaxEquipmentId >= 0) &&
396 (equipmentId > fSelectMaxEquipmentId))
402 Bool_t AliRawReader::IsEventSelected() const
404 // apply the event selection (if any)
406 // First check the event type
407 if (fSelectEventType >= 0) {
408 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
411 // Then check the trigger pattern and compared it
412 // to the required trigger mask
413 if (fSelectTriggerMask != 0) {
414 if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
420 UInt_t AliRawReader::SwapWord(UInt_t x) const
422 // Swap the endianess of the integer value 'x'
424 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
425 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
428 UShort_t AliRawReader::SwapShort(UShort_t x) const
430 // Swap the endianess of the short value 'x'
432 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
435 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
437 // reads the next 4 bytes at the current position
438 // returns kFALSE if the data could not be read
440 while (fCount == 0) {
441 if (!ReadHeader()) return kFALSE;
443 if (fCount < (Int_t) sizeof(data)) {
445 "too few data left (%d bytes) to read an UInt_t!", fCount);
448 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
449 Error("ReadNextInt", "could not read data!");
458 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
460 // reads the next 2 bytes at the current position
461 // returns kFALSE if the data could not be read
463 while (fCount == 0) {
464 if (!ReadHeader()) return kFALSE;
466 if (fCount < (Int_t) sizeof(data)) {
467 Error("ReadNextShort",
468 "too few data left (%d bytes) to read an UShort_t!", fCount);
471 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
472 Error("ReadNextShort", "could not read data!");
476 data=SwapShort(data);
481 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
483 // reads the next 1 byte at the current stream position
484 // returns kFALSE if the data could not be read
486 while (fCount == 0) {
487 if (!ReadHeader()) return kFALSE;
489 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
490 Error("ReadNextChar", "could not read data!");
496 Bool_t AliRawReader::GotoEvent(Int_t /*event*/)
498 Error("GotoEvent","Method not implemented! Nothing done");
502 Int_t AliRawReader::CheckData() const
504 // check the consistency of the data
505 // derived classes should overwrite the default method which returns 0 (no err)
511 void AliRawReader::DumpData(Int_t limit)
513 // print the raw data
514 // if limit is not negative, only the first and last "limit" lines of raw data
519 Error("DumpData", "no header");
523 " type = %d run = %d ", GetType(), GetRunNumber());
525 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
527 printf("event = -------- --------\n");
529 if (GetTriggerPattern()) {
530 printf(" trigger = %8.8x %8.8x ",
531 GetTriggerPattern()[1], GetTriggerPattern()[0]);
533 printf(" trigger = -------- -------- ");
535 if (GetDetectorPattern()) {
536 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
538 printf("detector = --------\n");
540 if (GetAttributes()) {
541 printf(" attributes = %8.8x %8.8x %8.8x ",
542 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
544 printf(" attributes = -------- -------- -------- ");
546 printf("GDC = %d\n", GetGDCId());
550 printf("-------------------------------------------------------------------------------\n");
551 printf("LDC = %d\n", GetLDCId());
553 printf("equipment:\n"
554 " size = %d type = %d id = %d\n",
555 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
556 if (GetEquipmentAttributes()) {
557 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
558 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
560 printf(" attributes = -------- -------- -------- ");
562 printf("element size = %d\n", GetEquipmentElementSize());
564 printf("data header:\n"
565 " size = %d version = %d valid = %d compression = %d\n",
566 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
569 if (limit == 0) continue;
571 Int_t size = GetDataSize();
573 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
579 for (Int_t n = 0; n < size; n++) {
580 if (!ReadNextChar(byte)) {
581 Error("DumpData", "couldn't read byte number %d\n", n);
585 printf("%8.8x %s\n", n-pos, line);
586 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
589 if ((limit > 0) && (n/max == limit)) {
590 Int_t nContinue = ((size-1)/max+1-limit) * max;
592 printf(" [skipping %d bytes]\n", nContinue-n);
598 Int_t offset = pos/4;
599 if ((byte > 0x20) && (byte < 0x7f)) {
600 line[pos+offset] = byte;
602 line[pos+offset] = '.';
605 sprintf(hex, "%2.2x", byte);
606 line[max+max/4+3+2*pos+offset] = hex[0];
607 line[max+max/4+4+2*pos+offset] = hex[1];
611 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
614 } while (ReadHeader());
617 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
621 // Add a raw data error message to the list
622 // of raw-data decoding errors
623 if (fEventNumber < 0) {
626 Int_t ddlId = GetEquipmentId();
628 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
632 Int_t prevEventNumber = -1;
633 Int_t prevDdlId = -1;
634 Int_t prevErrorCode = -1;
635 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
637 prevEventNumber = prevLog->GetEventNumber();
638 prevDdlId = prevLog->GetDdlID();
639 prevErrorCode = prevLog->GetErrorCode();
642 if ((prevEventNumber != fEventNumber) ||
643 (prevDdlId != ddlId) ||
644 (prevErrorCode != code)) {
645 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
646 AliRawDataErrorLog(fEventNumber,
653 if (prevLog) prevLog->AddCount();