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>
42 #include <TInterpreter.h>
44 #include <Riostream.h>
45 #include "AliRawReader.h"
46 #include "AliRawReaderFile.h"
47 #include "AliRawReaderDate.h"
48 #include "AliRawReaderRoot.h"
49 #include "AliRawReaderChain.h"
53 ClassImp(AliRawReader)
56 AliRawReader::AliRawReader() :
57 fEquipmentIdsIn(NULL),
58 fEquipmentIdsOut(NULL),
59 fRequireHeader(kTRUE),
62 fSelectEquipmentType(-1),
63 fSelectMinEquipmentId(-1),
64 fSelectMaxEquipmentId(-1),
67 fSelectTriggerMask(0),
71 fErrorLogs("AliRawDataErrorLog",100),
75 // default constructor: initialize data members
76 // Allocate the swapped header in case of Mac
78 fHeaderSwapped=new AliRawDataHeader();
82 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
84 // Open the mapping file
85 // and load the mapping data
86 ifstream input(fileName);
87 if (input.is_open()) {
88 Warning("AliRawReader","Equipment ID mapping file is found !");
89 const Int_t kMaxDDL = 256;
90 fEquipmentIdsIn = new TArrayI(kMaxDDL);
91 fEquipmentIdsOut = new TArrayI(kMaxDDL);
92 Int_t equipIn, equipOut;
94 while (input >> equipIn >> equipOut) {
95 if (nIds >= kMaxDDL) {
96 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
99 fEquipmentIdsIn->AddAt(equipIn,nIds);
100 fEquipmentIdsOut->AddAt(equipOut,nIds);
103 fEquipmentIdsIn->Set(nIds);
104 fEquipmentIdsOut->Set(nIds);
109 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
114 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
116 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
117 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
118 fRequireHeader(rawReader.fRequireHeader),
119 fHeader(rawReader.fHeader),
120 fCount(rawReader.fCount),
121 fSelectEquipmentType(rawReader.fSelectEquipmentType),
122 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
123 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
124 fSkipInvalid(rawReader.fSkipInvalid),
125 fSelectEventType(rawReader.fSelectEventType),
126 fSelectTriggerMask(rawReader.fSelectTriggerMask),
127 fSelectTriggerExpr(rawReader.fSelectTriggerExpr),
130 fErrorLogs("AliRawDataErrorLog",100),
131 fHeaderSwapped(NULL),
132 fIsValid(rawReader.fIsValid)
135 // Allocate the swapped header in case of Mac
137 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
141 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
143 // assignment operator
144 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
145 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
147 fHeader = rawReader.fHeader;
148 fCount = rawReader.fCount;
150 fSelectEquipmentType = rawReader.fSelectEquipmentType;
151 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
152 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
153 fSkipInvalid = rawReader.fSkipInvalid;
154 fSelectEventType = rawReader.fSelectEventType;
155 fSelectTriggerMask = rawReader.fSelectTriggerMask;
156 fSelectTriggerExpr = rawReader.fSelectTriggerExpr;
158 fErrorCode = rawReader.fErrorCode;
160 fEventNumber = rawReader.fEventNumber;
161 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
163 fIsValid = rawReader.fIsValid;
168 AliRawReader::~AliRawReader()
171 // delete the mapping arrays if
173 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
174 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
176 if (fHeaderSwapped) delete fHeaderSwapped;
179 AliRawReader* AliRawReader::Create(const char *uri)
181 // RawReader's factory
182 // It instantiate corresponding raw-reader implementation class object
183 // depending on the URI provided
184 // Normal URIs point to files, while the URI starting with
185 // 'mem://:' or 'mem://<filename>' will create
186 // AliRawReaderDateOnline object which is supposed to be used
187 // in the online reconstruction
189 TString strURI = uri;
191 if (strURI.IsNull()) {
192 AliWarningClass("No raw-reader created");
196 TObjArray *fields = strURI.Tokenize("?");
197 TString &fileURI = ((TObjString*)fields->At(0))->String();
199 AliRawReader *rawReader = NULL;
200 if (fileURI.BeginsWith("mem://")) {
201 fileURI.ReplaceAll("mem://","");
202 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
204 TPluginManager* pluginManager = gROOT->GetPluginManager();
205 TString rawReaderName = "AliRawReaderDateOnline";
206 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
207 // if not, add a plugin for it
208 if (!pluginHandler) {
209 pluginManager->AddHandler("AliRawReader", "online",
210 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
211 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
213 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
214 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
221 else if (fileURI.BeginsWith("collection://")) {
222 fileURI.ReplaceAll("collection://","");
223 AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
224 rawReader = new AliRawReaderChain(fileURI);
227 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
228 if (fileURI.EndsWith("/")) {
229 rawReader = new AliRawReaderFile(fileURI);
230 } else if (fileURI.EndsWith(".root")) {
231 rawReader = new AliRawReaderRoot(fileURI);
233 rawReader = new AliRawReaderDate(fileURI);
237 if (!rawReader->IsRawReaderValid()) {
238 AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
245 // Now apply event selection criteria (if specified)
246 if (fields->GetEntries() > 1) {
247 Int_t eventType = -1;
248 ULong64_t triggerMask = 0;
250 for(Int_t i = 1; i < fields->GetEntries(); i++) {
251 if (!fields->At(i)) continue;
252 TString &option = ((TObjString*)fields->At(i))->String();
253 if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
254 option.ReplaceAll("EventType=","");
255 eventType = option.Atoi();
258 if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
259 option.ReplaceAll("Trigger=","");
260 if (option.IsDigit()) {
261 triggerMask = option.Atoll();
264 triggerExpr = option.Data();
268 AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
270 AliInfoClass(Form("Event selection criteria specified: eventype=%d trigger mask=%llx trigger expression=%s",
271 eventType,triggerMask,triggerExpr.Data()));
272 rawReader->SelectEvents(eventType,triggerMask,triggerExpr.Data());
281 Int_t AliRawReader::GetMappedEquipmentId() const
283 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
284 Error("AliRawReader","equipment Ids mapping is not initialized !");
285 return GetEquipmentId();
287 Int_t equipmentId = GetEquipmentId();
288 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
289 if (equipmentId == fEquipmentIdsIn->At(iId)) {
290 equipmentId = fEquipmentIdsOut->At(iId);
297 Int_t AliRawReader::GetDetectorID() const
299 // Get the detector ID
300 // The list of detector IDs
301 // can be found in AliDAQ.h
303 if (fEquipmentIdsIn && fEquipmentIdsIn)
304 equipmentId = GetMappedEquipmentId();
306 equipmentId = GetEquipmentId();
308 if (equipmentId >= 0) {
310 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
316 Int_t AliRawReader::GetDDLID() const
318 // Get the DDL ID (within one sub-detector)
319 // The list of detector IDs
320 // can be found in AliDAQ.h
322 if (fEquipmentIdsIn && fEquipmentIdsIn)
323 equipmentId = GetMappedEquipmentId();
325 equipmentId = GetEquipmentId();
327 if (equipmentId >= 0) {
329 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
336 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
338 // read only data of the detector with the given name and in the given
339 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
340 // no selection is applied if a value < 0 is used.
341 Int_t detectorID = AliDAQ::DetectorID(detectorName);
343 Select(detectorID,minDDLID,maxDDLID);
346 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
348 // read only data of the detector with the given ID and in the given
349 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
350 // no selection is applied if a value < 0 is used.
352 fSelectEquipmentType = -1;
355 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
357 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
360 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
362 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
365 void AliRawReader::SelectEquipment(Int_t equipmentType,
366 Int_t minEquipmentId, Int_t maxEquipmentId)
368 // read only data of the equipment with the given type and in the given
369 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
370 // no selection is applied if a value < 0 is used.
372 fSelectEquipmentType = equipmentType;
373 fSelectMinEquipmentId = minEquipmentId;
374 fSelectMaxEquipmentId = maxEquipmentId;
377 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask,
378 const char *triggerExpr)
380 // read only events with the given type and optionally
382 // no selection is applied if value = 0 is used.
383 // Trigger selection can be done via string (triggerExpr)
384 // which defines the trigger logic to be used. It works only
385 // after LoadTriggerClass() method is called for all involved
388 fSelectEventType = type;
389 fSelectTriggerMask = triggerMask;
390 if (triggerExpr) fSelectTriggerExpr = triggerExpr;
393 void AliRawReader::LoadTriggerClass(const char* name, Int_t index)
395 // Loads the list of trigger classes defined.
396 // Used in conjunction with IsEventSelected in the
397 // case when the trigger selection is given by
398 // fSelectedTriggerExpr
400 if (fSelectTriggerExpr.IsNull()) return;
402 fSelectTriggerExpr.ReplaceAll(name,Form("[%d]",index));
405 Bool_t AliRawReader::IsSelected() const
407 // apply the selection (if any)
409 if (fSkipInvalid && !IsValid()) return kFALSE;
411 if (fSelectEquipmentType >= 0)
412 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
415 if (fEquipmentIdsIn && fEquipmentIdsIn)
416 equipmentId = GetMappedEquipmentId();
418 equipmentId = GetEquipmentId();
420 if ((fSelectMinEquipmentId >= 0) &&
421 (equipmentId < fSelectMinEquipmentId))
423 if ((fSelectMaxEquipmentId >= 0) &&
424 (equipmentId > fSelectMaxEquipmentId))
430 Bool_t AliRawReader::IsEventSelected() const
432 // apply the event selection (if any)
434 // First check the event type
435 if (fSelectEventType >= 0) {
436 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
439 // Then check the trigger pattern and compared it
440 // to the required trigger mask
441 if (fSelectTriggerMask != 0) {
442 if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
445 if (!fSelectTriggerExpr.IsNull()) {
446 TString expr(fSelectTriggerExpr);
447 ULong64_t mask = GetClassMask();
448 for(Int_t itrigger = 0; itrigger < 50; itrigger++) {
449 if (mask & (1 << itrigger)) {
450 expr.ReplaceAll(Form("[%d]",itrigger),"1");
453 expr.ReplaceAll(Form("[%d]",itrigger),"0");
457 if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
458 (error == TInterpreter::kNoError)) {
466 UInt_t AliRawReader::SwapWord(UInt_t x) const
468 // Swap the endianess of the integer value 'x'
470 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
471 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
474 UShort_t AliRawReader::SwapShort(UShort_t x) const
476 // Swap the endianess of the short value 'x'
478 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
481 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
483 // reads the next 4 bytes at the current position
484 // returns kFALSE if the data could not be read
486 while (fCount == 0) {
487 if (!ReadHeader()) return kFALSE;
489 if (fCount < (Int_t) sizeof(data)) {
491 "too few data left (%d bytes) to read an UInt_t!", fCount);
494 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
495 Error("ReadNextInt", "could not read data!");
504 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
506 // reads the next 2 bytes at the current position
507 // returns kFALSE if the data could not be read
509 while (fCount == 0) {
510 if (!ReadHeader()) return kFALSE;
512 if (fCount < (Int_t) sizeof(data)) {
513 Error("ReadNextShort",
514 "too few data left (%d bytes) to read an UShort_t!", fCount);
517 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
518 Error("ReadNextShort", "could not read data!");
522 data=SwapShort(data);
527 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
529 // reads the next 1 byte at the current stream position
530 // returns kFALSE if the data could not be read
532 while (fCount == 0) {
533 if (!ReadHeader()) return kFALSE;
535 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
536 Error("ReadNextChar", "could not read data!");
542 Bool_t AliRawReader::GotoEvent(Int_t /*event*/)
544 Error("GotoEvent","Method not implemented! Nothing done");
548 Int_t AliRawReader::CheckData() const
550 // check the consistency of the data
551 // derived classes should overwrite the default method which returns 0 (no err)
557 void AliRawReader::DumpData(Int_t limit)
559 // print the raw data
560 // if limit is not negative, only the first and last "limit" lines of raw data
565 Error("DumpData", "no header");
569 " type = %d run = %d ", GetType(), GetRunNumber());
571 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
573 printf("event = -------- --------\n");
575 if (GetTriggerPattern()) {
576 printf(" trigger = %8.8x %8.8x ",
577 GetTriggerPattern()[1], GetTriggerPattern()[0]);
579 printf(" trigger = -------- -------- ");
581 if (GetDetectorPattern()) {
582 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
584 printf("detector = --------\n");
586 if (GetAttributes()) {
587 printf(" attributes = %8.8x %8.8x %8.8x ",
588 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
590 printf(" attributes = -------- -------- -------- ");
592 printf("GDC = %d\n", GetGDCId());
596 printf("-------------------------------------------------------------------------------\n");
597 printf("LDC = %d\n", GetLDCId());
599 printf("equipment:\n"
600 " size = %d type = %d id = %d\n",
601 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
602 if (GetEquipmentAttributes()) {
603 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
604 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
606 printf(" attributes = -------- -------- -------- ");
608 printf("element size = %d\n", GetEquipmentElementSize());
610 printf("data header:\n"
611 " size = %d version = %d valid = %d compression = %d\n",
612 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
615 if (limit == 0) continue;
617 Int_t size = GetDataSize();
619 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
625 for (Int_t n = 0; n < size; n++) {
626 if (!ReadNextChar(byte)) {
627 Error("DumpData", "couldn't read byte number %d\n", n);
631 printf("%8.8x %s\n", n-pos, line);
632 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
635 if ((limit > 0) && (n/max == limit)) {
636 Int_t nContinue = ((size-1)/max+1-limit) * max;
638 printf(" [skipping %d bytes]\n", nContinue-n);
644 Int_t offset = pos/4;
645 if ((byte > 0x20) && (byte < 0x7f)) {
646 line[pos+offset] = byte;
648 line[pos+offset] = '.';
651 sprintf(hex, "%2.2x", byte);
652 line[max+max/4+3+2*pos+offset] = hex[0];
653 line[max+max/4+4+2*pos+offset] = hex[1];
657 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
660 } while (ReadHeader());
663 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
667 // Add a raw data error message to the list
668 // of raw-data decoding errors
669 if (fEventNumber < 0) {
672 Int_t ddlId = GetEquipmentId();
674 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
678 Int_t prevEventNumber = -1;
679 Int_t prevDdlId = -1;
680 Int_t prevErrorCode = -1;
681 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
683 prevEventNumber = prevLog->GetEventNumber();
684 prevDdlId = prevLog->GetDdlID();
685 prevErrorCode = prevLog->GetErrorCode();
688 if ((prevEventNumber != fEventNumber) ||
689 (prevDdlId != ddlId) ||
690 (prevErrorCode != code)) {
691 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
692 AliRawDataErrorLog(fEventNumber,
699 if (prevLog) prevLog->AddCount();