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>
45 #include <Riostream.h>
46 #include "AliRawReader.h"
47 #include "AliRawReaderFile.h"
48 #include "AliRawReaderDate.h"
49 #include "AliRawReaderRoot.h"
50 #include "AliRawReaderChain.h"
54 ClassImp(AliRawReader)
57 AliRawReader::AliRawReader() :
58 fEquipmentIdsIn(NULL),
59 fEquipmentIdsOut(NULL),
60 fRequireHeader(kTRUE),
63 fSelectEquipmentType(-1),
64 fSelectMinEquipmentId(-1),
65 fSelectMaxEquipmentId(-1),
68 fSelectTriggerMask(0),
72 fErrorLogs("AliRawDataErrorLog",100),
76 // default constructor: initialize data members
77 // Allocate the swapped header in case of Mac
79 fHeaderSwapped=new AliRawDataHeader();
83 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
85 // Open the mapping file
86 // and load the mapping data
87 ifstream input(fileName);
88 if (input.is_open()) {
89 Warning("AliRawReader","Equipment ID mapping file is found !");
90 const Int_t kMaxDDL = 256;
91 fEquipmentIdsIn = new TArrayI(kMaxDDL);
92 fEquipmentIdsOut = new TArrayI(kMaxDDL);
93 Int_t equipIn, equipOut;
95 while (input >> equipIn >> equipOut) {
96 if (nIds >= kMaxDDL) {
97 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
100 fEquipmentIdsIn->AddAt(equipIn,nIds);
101 fEquipmentIdsOut->AddAt(equipOut,nIds);
104 fEquipmentIdsIn->Set(nIds);
105 fEquipmentIdsOut->Set(nIds);
110 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
115 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
117 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
118 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
119 fRequireHeader(rawReader.fRequireHeader),
120 fHeader(rawReader.fHeader),
121 fCount(rawReader.fCount),
122 fSelectEquipmentType(rawReader.fSelectEquipmentType),
123 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
124 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
125 fSkipInvalid(rawReader.fSkipInvalid),
126 fSelectEventType(rawReader.fSelectEventType),
127 fSelectTriggerMask(rawReader.fSelectTriggerMask),
128 fSelectTriggerExpr(rawReader.fSelectTriggerExpr),
131 fErrorLogs("AliRawDataErrorLog",100),
132 fHeaderSwapped(NULL),
133 fIsValid(rawReader.fIsValid)
136 // Allocate the swapped header in case of Mac
138 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
142 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
144 // assignment operator
145 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
146 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
148 fHeader = rawReader.fHeader;
149 fCount = rawReader.fCount;
151 fSelectEquipmentType = rawReader.fSelectEquipmentType;
152 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
153 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
154 fSkipInvalid = rawReader.fSkipInvalid;
155 fSelectEventType = rawReader.fSelectEventType;
156 fSelectTriggerMask = rawReader.fSelectTriggerMask;
157 fSelectTriggerExpr = rawReader.fSelectTriggerExpr;
159 fErrorCode = rawReader.fErrorCode;
161 fEventNumber = rawReader.fEventNumber;
162 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
164 fIsValid = rawReader.fIsValid;
169 AliRawReader::~AliRawReader()
172 // delete the mapping arrays if
174 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
175 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
177 if (fHeaderSwapped) delete fHeaderSwapped;
180 AliRawReader* AliRawReader::Create(const char *uri)
182 // RawReader's factory
183 // It instantiate corresponding raw-reader implementation class object
184 // depending on the URI provided
185 // Normal URIs point to files, while the URI starting with
186 // 'mem://:' or 'mem://<filename>' will create
187 // AliRawReaderDateOnline object which is supposed to be used
188 // in the online reconstruction
190 TString strURI = uri;
192 if (strURI.IsNull()) {
193 AliWarningClass("No raw-reader created");
197 TObjArray *fields = strURI.Tokenize("?");
198 TString &fileURI = ((TObjString*)fields->At(0))->String();
200 AliRawReader *rawReader = NULL;
201 if (fileURI.BeginsWith("mem://") || fileURI.BeginsWith("^")) {
202 if (fileURI.BeginsWith("mem://")) fileURI.ReplaceAll("mem://","");
203 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
205 TPluginManager* pluginManager = gROOT->GetPluginManager();
206 TString rawReaderName = "AliRawReaderDateOnline";
207 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
208 // if not, add a plugin for it
209 if (!pluginHandler) {
210 pluginManager->AddHandler("AliRawReader", "online",
211 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
212 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
214 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
215 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
222 else if (fileURI.BeginsWith("collection://")) {
223 fileURI.ReplaceAll("collection://","");
224 AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
225 rawReader = new AliRawReaderChain(fileURI);
228 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
229 TString filename(gSystem->ExpandPathName(fileURI.Data()));
230 if (filename.EndsWith("/")) {
231 rawReader = new AliRawReaderFile(filename);
232 } else if (filename.EndsWith(".root")) {
233 rawReader = new AliRawReaderRoot(filename);
235 rawReader = new AliRawReaderDate(filename);
239 if (!rawReader->IsRawReaderValid()) {
240 AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
247 // Now apply event selection criteria (if specified)
248 if (fields->GetEntries() > 1) {
249 Int_t eventType = -1;
250 ULong64_t triggerMask = 0;
252 for(Int_t i = 1; i < fields->GetEntries(); i++) {
253 if (!fields->At(i)) continue;
254 TString &option = ((TObjString*)fields->At(i))->String();
255 if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
256 option.ReplaceAll("EventType=","");
257 eventType = option.Atoi();
260 if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
261 option.ReplaceAll("Trigger=","");
262 if (option.IsDigit()) {
263 triggerMask = option.Atoll();
266 triggerExpr = option.Data();
270 AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
272 AliInfoClass(Form("Event selection criteria specified: eventype=%d trigger mask=%llx trigger expression=%s",
273 eventType,triggerMask,triggerExpr.Data()));
274 rawReader->SelectEvents(eventType,triggerMask,triggerExpr.Data());
283 Int_t AliRawReader::GetMappedEquipmentId() const
285 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
286 Error("AliRawReader","equipment Ids mapping is not initialized !");
287 return GetEquipmentId();
289 Int_t equipmentId = GetEquipmentId();
290 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
291 if (equipmentId == fEquipmentIdsIn->At(iId)) {
292 equipmentId = fEquipmentIdsOut->At(iId);
299 Int_t AliRawReader::GetDetectorID() const
301 // Get the detector ID
302 // The list of detector IDs
303 // can be found in AliDAQ.h
305 if (fEquipmentIdsIn && fEquipmentIdsIn)
306 equipmentId = GetMappedEquipmentId();
308 equipmentId = GetEquipmentId();
310 if (equipmentId >= 0) {
312 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
318 Int_t AliRawReader::GetDDLID() const
320 // Get the DDL ID (within one sub-detector)
321 // The list of detector IDs
322 // can be found in AliDAQ.h
324 if (fEquipmentIdsIn && fEquipmentIdsIn)
325 equipmentId = GetMappedEquipmentId();
327 equipmentId = GetEquipmentId();
329 if (equipmentId >= 0) {
331 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
338 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
340 // read only data of the detector with the given name and in the given
341 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
342 // no selection is applied if a value < 0 is used.
343 Int_t detectorID = AliDAQ::DetectorID(detectorName);
345 Select(detectorID,minDDLID,maxDDLID);
348 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
350 // read only data of the detector with the given ID and in the given
351 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
352 // no selection is applied if a value < 0 is used.
354 fSelectEquipmentType = -1;
357 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
359 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
362 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
364 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
367 void AliRawReader::SelectEquipment(Int_t equipmentType,
368 Int_t minEquipmentId, Int_t maxEquipmentId)
370 // read only data of the equipment with the given type and in the given
371 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
372 // no selection is applied if a value < 0 is used.
374 fSelectEquipmentType = equipmentType;
375 fSelectMinEquipmentId = minEquipmentId;
376 fSelectMaxEquipmentId = maxEquipmentId;
379 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask,
380 const char *triggerExpr)
382 // read only events with the given type and optionally
384 // no selection is applied if value = 0 is used.
385 // Trigger selection can be done via string (triggerExpr)
386 // which defines the trigger logic to be used. It works only
387 // after LoadTriggerClass() method is called for all involved
390 fSelectEventType = type;
391 fSelectTriggerMask = triggerMask;
392 if (triggerExpr) fSelectTriggerExpr = triggerExpr;
395 void AliRawReader::LoadTriggerClass(const char* name, Int_t index)
397 // Loads the list of trigger classes defined.
398 // Used in conjunction with IsEventSelected in the
399 // case when the trigger selection is given by
400 // fSelectedTriggerExpr
402 if (fSelectTriggerExpr.IsNull()) return;
404 fSelectTriggerExpr.ReplaceAll(name,Form("[%d]",index));
407 Bool_t AliRawReader::IsSelected() const
409 // apply the selection (if any)
411 if (fSkipInvalid && !IsValid()) return kFALSE;
413 if (fSelectEquipmentType >= 0)
414 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
417 if (fEquipmentIdsIn && fEquipmentIdsIn)
418 equipmentId = GetMappedEquipmentId();
420 equipmentId = GetEquipmentId();
422 if ((fSelectMinEquipmentId >= 0) &&
423 (equipmentId < fSelectMinEquipmentId))
425 if ((fSelectMaxEquipmentId >= 0) &&
426 (equipmentId > fSelectMaxEquipmentId))
432 Bool_t AliRawReader::IsEventSelected() const
434 // apply the event selection (if any)
436 // First check the event type
437 if (fSelectEventType >= 0) {
438 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
441 // Then check the trigger pattern and compared it
442 // to the required trigger mask
443 if (fSelectTriggerMask != 0) {
444 if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
447 if (!fSelectTriggerExpr.IsNull()) {
448 TString expr(fSelectTriggerExpr);
449 ULong64_t mask = GetClassMask();
450 for(Int_t itrigger = 0; itrigger < 50; itrigger++) {
451 if (mask & (1 << itrigger)) {
452 expr.ReplaceAll(Form("[%d]",itrigger),"1");
455 expr.ReplaceAll(Form("[%d]",itrigger),"0");
459 if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
460 (error == TInterpreter::kNoError)) {
468 UInt_t AliRawReader::SwapWord(UInt_t x) const
470 // Swap the endianess of the integer value 'x'
472 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
473 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
476 UShort_t AliRawReader::SwapShort(UShort_t x) const
478 // Swap the endianess of the short value 'x'
480 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
483 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
485 // reads the next 4 bytes at the current position
486 // returns kFALSE if the data could not be read
488 while (fCount == 0) {
489 if (!ReadHeader()) return kFALSE;
491 if (fCount < (Int_t) sizeof(data)) {
493 "too few data left (%d bytes) to read an UInt_t!", fCount);
496 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
497 Error("ReadNextInt", "could not read data!");
506 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
508 // reads the next 2 bytes at the current position
509 // returns kFALSE if the data could not be read
511 while (fCount == 0) {
512 if (!ReadHeader()) return kFALSE;
514 if (fCount < (Int_t) sizeof(data)) {
515 Error("ReadNextShort",
516 "too few data left (%d bytes) to read an UShort_t!", fCount);
519 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
520 Error("ReadNextShort", "could not read data!");
524 data=SwapShort(data);
529 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
531 // reads the next 1 byte at the current stream position
532 // returns kFALSE if the data could not be read
534 while (fCount == 0) {
535 if (!ReadHeader()) return kFALSE;
537 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
538 Error("ReadNextChar", "could not read data!");
544 Bool_t AliRawReader::GotoEvent(Int_t event)
546 // Random access to certain
547 // event index. Could be very slow
548 // for some non-root raw-readers.
549 // So it should be reimplemented there.
550 if (event < fEventNumber) RewindEvents();
552 while (fEventNumber < event) {
553 if (!NextEvent()) return kFALSE;
559 Int_t AliRawReader::CheckData() const
561 // check the consistency of the data
562 // derived classes should overwrite the default method which returns 0 (no err)
568 void AliRawReader::DumpData(Int_t limit)
570 // print the raw data
571 // if limit is not negative, only the first and last "limit" lines of raw data
576 Error("DumpData", "no header");
580 " type = %d run = %d ", GetType(), GetRunNumber());
582 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
584 printf("event = -------- --------\n");
586 if (GetTriggerPattern()) {
587 printf(" trigger = %8.8x %8.8x ",
588 GetTriggerPattern()[1], GetTriggerPattern()[0]);
590 printf(" trigger = -------- -------- ");
592 if (GetDetectorPattern()) {
593 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
595 printf("detector = --------\n");
597 if (GetAttributes()) {
598 printf(" attributes = %8.8x %8.8x %8.8x ",
599 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
601 printf(" attributes = -------- -------- -------- ");
603 printf("GDC = %d\n", GetGDCId());
607 printf("-------------------------------------------------------------------------------\n");
608 printf("LDC = %d\n", GetLDCId());
610 printf("equipment:\n"
611 " size = %d type = %d id = %d\n",
612 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
613 if (GetEquipmentAttributes()) {
614 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
615 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
617 printf(" attributes = -------- -------- -------- ");
619 printf("element size = %d\n", GetEquipmentElementSize());
621 printf("data header:\n"
622 " size = %d version = %d valid = %d compression = %d\n",
623 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
626 if (limit == 0) continue;
628 Int_t size = GetDataSize();
630 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
636 for (Int_t n = 0; n < size; n++) {
637 if (!ReadNextChar(byte)) {
638 Error("DumpData", "couldn't read byte number %d\n", n);
642 printf("%8.8x %s\n", n-pos, line);
643 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
646 if ((limit > 0) && (n/max == limit)) {
647 Int_t nContinue = ((size-1)/max+1-limit) * max;
649 printf(" [skipping %d bytes]\n", nContinue-n);
655 Int_t offset = pos/4;
656 if ((byte > 0x20) && (byte < 0x7f)) {
657 line[pos+offset] = byte;
659 line[pos+offset] = '.';
662 sprintf(hex, "%2.2x", byte);
663 line[max+max/4+3+2*pos+offset] = hex[0];
664 line[max+max/4+4+2*pos+offset] = hex[1];
668 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
671 } while (ReadHeader());
674 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
678 // Add a raw data error message to the list
679 // of raw-data decoding errors
680 if (fEventNumber < 0) {
683 Int_t ddlId = GetEquipmentId();
685 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
689 Int_t prevEventNumber = -1;
690 Int_t prevDdlId = -1;
691 Int_t prevErrorCode = -1;
692 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
694 prevEventNumber = prevLog->GetEventNumber();
695 prevDdlId = prevLog->GetDdlID();
696 prevErrorCode = prevLog->GetErrorCode();
699 if ((prevEventNumber != fEventNumber) ||
700 (prevDdlId != ddlId) ||
701 (prevErrorCode != code)) {
702 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
703 AliRawDataErrorLog(fEventNumber,
710 if (prevLog) prevLog->AddCount();
714 Bool_t AliRawReader::GotoEventWithID(Int_t event,
719 // Go to certain event number by
720 // checking the event ID.
721 // Useful in case event-selection
722 // is applied and the 'event' is
724 if (!GotoEvent(event)) return kFALSE;
726 while (GetBCID() != period ||
727 GetOrbitID() != orbitID ||
728 GetPeriod() != bcID) {
729 if (!NextEvent()) return kFALSE;