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>
46 #include <Riostream.h>
47 #include "AliRawReader.h"
48 #include "AliRawReaderFile.h"
49 #include "AliRawReaderDate.h"
50 #include "AliRawReaderRoot.h"
51 #include "AliRawReaderChain.h"
55 ClassImp(AliRawReader)
58 AliRawReader::AliRawReader() :
59 fEquipmentIdsIn(NULL),
60 fEquipmentIdsOut(NULL),
61 fRequireHeader(kTRUE),
64 fSelectEquipmentType(-1),
65 fSelectMinEquipmentId(-1),
66 fSelectMaxEquipmentId(-1),
69 fSelectTriggerMask(0),
73 fErrorLogs("AliRawDataErrorLog",100),
77 // default constructor: initialize data members
78 // Allocate the swapped header in case of Mac
80 fHeaderSwapped=new AliRawDataHeader();
84 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
86 // Open the mapping file
87 // and load the mapping data
88 ifstream input(fileName);
89 if (input.is_open()) {
90 Warning("AliRawReader","Equipment ID mapping file is found !");
91 const Int_t kMaxDDL = 256;
92 fEquipmentIdsIn = new TArrayI(kMaxDDL);
93 fEquipmentIdsOut = new TArrayI(kMaxDDL);
94 Int_t equipIn, equipOut;
96 while (input >> equipIn >> equipOut) {
97 if (nIds >= kMaxDDL) {
98 Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
101 fEquipmentIdsIn->AddAt(equipIn,nIds);
102 fEquipmentIdsOut->AddAt(equipOut,nIds);
105 fEquipmentIdsIn->Set(nIds);
106 fEquipmentIdsOut->Set(nIds);
111 Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
116 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
118 fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
119 fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
120 fRequireHeader(rawReader.fRequireHeader),
121 fHeader(rawReader.fHeader),
122 fCount(rawReader.fCount),
123 fSelectEquipmentType(rawReader.fSelectEquipmentType),
124 fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
125 fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
126 fSkipInvalid(rawReader.fSkipInvalid),
127 fSelectEventType(rawReader.fSelectEventType),
128 fSelectTriggerMask(rawReader.fSelectTriggerMask),
129 fSelectTriggerExpr(rawReader.fSelectTriggerExpr),
132 fErrorLogs("AliRawDataErrorLog",100),
133 fHeaderSwapped(NULL),
134 fIsValid(rawReader.fIsValid)
137 // Allocate the swapped header in case of Mac
139 fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
143 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
145 // assignment operator
146 fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
147 fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
149 fHeader = rawReader.fHeader;
150 fCount = rawReader.fCount;
152 fSelectEquipmentType = rawReader.fSelectEquipmentType;
153 fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
154 fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
155 fSkipInvalid = rawReader.fSkipInvalid;
156 fSelectEventType = rawReader.fSelectEventType;
157 fSelectTriggerMask = rawReader.fSelectTriggerMask;
158 fSelectTriggerExpr = rawReader.fSelectTriggerExpr;
160 fErrorCode = rawReader.fErrorCode;
162 fEventNumber = rawReader.fEventNumber;
163 fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
165 fIsValid = rawReader.fIsValid;
170 AliRawReader::~AliRawReader()
173 // delete the mapping arrays if
175 if (fEquipmentIdsIn) delete fEquipmentIdsIn;
176 if (fEquipmentIdsOut) delete fEquipmentIdsOut;
178 if (fHeaderSwapped) delete fHeaderSwapped;
181 AliRawReader* AliRawReader::Create(const char *uri)
183 // RawReader's factory
184 // It instantiate corresponding raw-reader implementation class object
185 // depending on the URI provided
186 // Normal URIs point to files, while the URI starting with
187 // 'mem://:' or 'mem://<filename>' will create
188 // AliRawReaderDateOnline object which is supposed to be used
189 // in the online reconstruction
191 TString strURI = uri;
193 if (strURI.IsNull()) {
194 AliWarningClass("No raw-reader created");
198 TObjArray *fields = strURI.Tokenize("?");
199 TString &fileURI = ((TObjString*)fields->At(0))->String();
201 AliRawReader *rawReader = NULL;
202 if (fileURI.BeginsWith("mem://") || fileURI.BeginsWith("^")) {
203 if (fileURI.BeginsWith("mem://")) fileURI.ReplaceAll("mem://","");
204 AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
206 TPluginManager* pluginManager = gROOT->GetPluginManager();
207 TString rawReaderName = "AliRawReaderDateOnline";
208 TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
209 // if not, add a plugin for it
210 if (!pluginHandler) {
211 pluginManager->AddHandler("AliRawReader", "online",
212 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
213 pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
215 if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
216 rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
223 else if (fileURI.BeginsWith("amore://")) {
224 // A special raw-data URL used in case
225 // the raw-data reading is steered from
226 // ouside, i.e. from AMORE
227 fileURI.ReplaceAll("amore://","");
228 AliInfoClass("Creating raw-reader in order to read events sent by AMORE");
229 rawReader = new AliRawReaderDate((void *)NULL);
231 else if (fileURI.BeginsWith("collection://")) {
232 fileURI.ReplaceAll("collection://","");
233 AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
234 rawReader = new AliRawReaderChain(fileURI);
236 else if (fileURI.BeginsWith("raw://run")) {
237 fileURI.ReplaceAll("raw://run","");
238 if (fileURI.IsDigit()) {
239 rawReader = new AliRawReaderChain(fileURI.Atoi());
242 AliErrorClass(Form("Invalid syntax: %s",fileURI.Data()));
248 AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
249 TString filename(gSystem->ExpandPathName(fileURI.Data()));
250 if (filename.EndsWith("/")) {
251 rawReader = new AliRawReaderFile(filename);
252 } else if (filename.EndsWith(".root")) {
253 rawReader = new AliRawReaderRoot(filename);
255 rawReader = new AliRawReaderDate(filename);
259 if (!rawReader->IsRawReaderValid()) {
260 AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
267 // Now apply event selection criteria (if specified)
268 if (fields->GetEntries() > 1) {
269 Int_t eventType = -1;
270 ULong64_t triggerMask = 0;
272 for(Int_t i = 1; i < fields->GetEntries(); i++) {
273 if (!fields->At(i)) continue;
274 TString &option = ((TObjString*)fields->At(i))->String();
275 if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
276 option.ReplaceAll("EventType=","");
277 eventType = option.Atoi();
280 if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
281 option.ReplaceAll("Trigger=","");
282 if (option.IsDigit()) {
283 triggerMask = option.Atoll();
286 triggerExpr = option.Data();
290 AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
292 AliInfoClass(Form("Event selection criteria specified: eventype=%d trigger mask=%llx trigger expression=%s",
293 eventType,triggerMask,triggerExpr.Data()));
294 rawReader->SelectEvents(eventType,triggerMask,triggerExpr.Data());
303 Int_t AliRawReader::GetMappedEquipmentId() const
305 if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
306 Error("AliRawReader","equipment Ids mapping is not initialized !");
307 return GetEquipmentId();
309 Int_t equipmentId = GetEquipmentId();
310 for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
311 if (equipmentId == fEquipmentIdsIn->At(iId)) {
312 equipmentId = fEquipmentIdsOut->At(iId);
319 Int_t AliRawReader::GetDetectorID() const
321 // Get the detector ID
322 // The list of detector IDs
323 // can be found in AliDAQ.h
325 if (fEquipmentIdsIn && fEquipmentIdsIn)
326 equipmentId = GetMappedEquipmentId();
328 equipmentId = GetEquipmentId();
330 if (equipmentId >= 0) {
332 return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
338 Int_t AliRawReader::GetDDLID() const
340 // Get the DDL ID (within one sub-detector)
341 // The list of detector IDs
342 // can be found in AliDAQ.h
344 if (fEquipmentIdsIn && fEquipmentIdsIn)
345 equipmentId = GetMappedEquipmentId();
347 equipmentId = GetEquipmentId();
349 if (equipmentId >= 0) {
351 AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
358 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
360 // read only data of the detector with the given name and in the given
361 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
362 // no selection is applied if a value < 0 is used.
363 Int_t detectorID = AliDAQ::DetectorID(detectorName);
365 Select(detectorID,minDDLID,maxDDLID);
368 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
370 // read only data of the detector with the given ID and in the given
371 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
372 // no selection is applied if a value < 0 is used.
374 fSelectEquipmentType = -1;
377 fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
379 fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
382 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
384 fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
387 void AliRawReader::SelectEquipment(Int_t equipmentType,
388 Int_t minEquipmentId, Int_t maxEquipmentId)
390 // read only data of the equipment with the given type and in the given
391 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
392 // no selection is applied if a value < 0 is used.
394 fSelectEquipmentType = equipmentType;
395 fSelectMinEquipmentId = minEquipmentId;
396 fSelectMaxEquipmentId = maxEquipmentId;
399 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask,
400 const char *triggerExpr)
402 // read only events with the given type and optionally
404 // no selection is applied if value = 0 is used.
405 // Trigger selection can be done via string (triggerExpr)
406 // which defines the trigger logic to be used. It works only
407 // after LoadTriggerClass() method is called for all involved
410 fSelectEventType = type;
411 fSelectTriggerMask = triggerMask;
412 if (triggerExpr) fSelectTriggerExpr = triggerExpr;
415 void AliRawReader::LoadTriggerClass(const char* name, Int_t index)
417 // Loads the list of trigger classes defined.
418 // Used in conjunction with IsEventSelected in the
419 // case when the trigger selection is given by
420 // fSelectedTriggerExpr
422 if (fSelectTriggerExpr.IsNull()) return;
425 fSelectTriggerExpr.ReplaceAll(name,Form("[%d]",index));
427 fSelectTriggerExpr.ReplaceAll(name,"0");
430 Bool_t AliRawReader::IsSelected() const
432 // apply the selection (if any)
434 if (fSkipInvalid && !IsValid()) return kFALSE;
436 if (fSelectEquipmentType >= 0)
437 if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
440 if (fEquipmentIdsIn && fEquipmentIdsIn)
441 equipmentId = GetMappedEquipmentId();
443 equipmentId = GetEquipmentId();
445 if ((fSelectMinEquipmentId >= 0) &&
446 (equipmentId < fSelectMinEquipmentId))
448 if ((fSelectMaxEquipmentId >= 0) &&
449 (equipmentId > fSelectMaxEquipmentId))
455 Bool_t AliRawReader::IsEventSelected() const
457 // apply the event selection (if any)
459 // First check the event type
460 if (fSelectEventType >= 0) {
461 if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
464 // Then check the trigger pattern and compared it
465 // to the required trigger mask
466 if (fSelectTriggerMask != 0) {
467 if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
470 if (!fSelectTriggerExpr.IsNull()) {
471 TString expr(fSelectTriggerExpr);
472 ULong64_t mask = GetClassMask();
473 for(Int_t itrigger = 0; itrigger < 50; itrigger++) {
474 if (mask & (1 << itrigger)) {
475 expr.ReplaceAll(Form("[%d]",itrigger),"1");
478 expr.ReplaceAll(Form("[%d]",itrigger),"0");
481 // Possibility to introduce downscaling
482 TPRegexp("(%\\s*\\d+)").Substitute(expr,Form("&& !(%d$1)",GetEventIndex()),"g");
484 if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
485 (error == TInterpreter::kNoError)) {
493 UInt_t AliRawReader::SwapWord(UInt_t x) const
495 // Swap the endianess of the integer value 'x'
497 return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) << 8) |
498 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
501 UShort_t AliRawReader::SwapShort(UShort_t x) const
503 // Swap the endianess of the short value 'x'
505 return (((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8)) ;
508 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
510 // reads the next 4 bytes at the current position
511 // returns kFALSE if the data could not be read
513 while (fCount == 0) {
514 if (!ReadHeader()) return kFALSE;
516 if (fCount < (Int_t) sizeof(data)) {
518 "too few data left (%d bytes) to read an UInt_t!", fCount);
521 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
522 Error("ReadNextInt", "could not read data!");
531 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
533 // reads the next 2 bytes at the current position
534 // returns kFALSE if the data could not be read
536 while (fCount == 0) {
537 if (!ReadHeader()) return kFALSE;
539 if (fCount < (Int_t) sizeof(data)) {
540 Error("ReadNextShort",
541 "too few data left (%d bytes) to read an UShort_t!", fCount);
544 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
545 Error("ReadNextShort", "could not read data!");
549 data=SwapShort(data);
554 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
556 // reads the next 1 byte at the current stream position
557 // returns kFALSE if the data could not be read
559 while (fCount == 0) {
560 if (!ReadHeader()) return kFALSE;
562 if (!ReadNext((UChar_t*) &data, sizeof(data))) {
563 Error("ReadNextChar", "could not read data!");
569 Bool_t AliRawReader::GotoEvent(Int_t event)
571 // Random access to certain
572 // event index. Could be very slow
573 // for some non-root raw-readers.
574 // So it should be reimplemented there.
575 if (event < fEventNumber) RewindEvents();
577 while (fEventNumber < event) {
578 if (!NextEvent()) return kFALSE;
584 Int_t AliRawReader::CheckData() const
586 // check the consistency of the data
587 // derived classes should overwrite the default method which returns 0 (no err)
593 void AliRawReader::DumpData(Int_t limit)
595 // print the raw data
596 // if limit is not negative, only the first and last "limit" lines of raw data
601 Error("DumpData", "no header");
605 " type = %d run = %d ", GetType(), GetRunNumber());
607 printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
609 printf("event = -------- --------\n");
611 if (GetTriggerPattern()) {
612 printf(" trigger = %8.8x %8.8x ",
613 GetTriggerPattern()[1], GetTriggerPattern()[0]);
615 printf(" trigger = -------- -------- ");
617 if (GetDetectorPattern()) {
618 printf("detector = %8.8x\n", GetDetectorPattern()[0]);
620 printf("detector = --------\n");
622 if (GetAttributes()) {
623 printf(" attributes = %8.8x %8.8x %8.8x ",
624 GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
626 printf(" attributes = -------- -------- -------- ");
628 printf("GDC = %d\n", GetGDCId());
632 printf("-------------------------------------------------------------------------------\n");
633 printf("LDC = %d\n", GetLDCId());
635 printf("equipment:\n"
636 " size = %d type = %d id = %d\n",
637 GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
638 if (GetEquipmentAttributes()) {
639 printf(" attributes = %8.8x %8.8x %8.8x ", GetEquipmentAttributes()[2],
640 GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
642 printf(" attributes = -------- -------- -------- ");
644 printf("element size = %d\n", GetEquipmentElementSize());
646 printf("data header:\n"
647 " size = %d version = %d valid = %d compression = %d\n",
648 GetDataSize(), GetVersion(), IsValid(), IsCompressed());
651 if (limit == 0) continue;
653 Int_t size = GetDataSize();
655 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
661 for (Int_t n = 0; n < size; n++) {
662 if (!ReadNextChar(byte)) {
663 Error("DumpData", "couldn't read byte number %d\n", n);
667 printf("%8.8x %s\n", n-pos, line);
668 for (Int_t i = 0; i < 70; i++) line[i] = ' ';
671 if ((limit > 0) && (n/max == limit)) {
672 Int_t nContinue = ((size-1)/max+1-limit) * max;
674 printf(" [skipping %d bytes]\n", nContinue-n);
680 Int_t offset = pos/4;
681 if ((byte > 0x20) && (byte < 0x7f)) {
682 line[pos+offset] = byte;
684 line[pos+offset] = '.';
687 snprintf(hex, 3, "%2.2x", byte);
688 line[max+max/4+3+2*pos+offset] = hex[0];
689 line[max+max/4+4+2*pos+offset] = hex[1];
693 if (pos > 0) printf("%8.8x %s\n", size-pos, line);
696 } while (ReadHeader());
699 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
703 // Add a raw data error message to the list
704 // of raw-data decoding errors
705 if (fEventNumber < 0) {
708 Int_t ddlId = GetEquipmentId();
710 AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
714 Int_t prevEventNumber = -1;
715 Int_t prevDdlId = -1;
716 Int_t prevErrorCode = -1;
717 AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
719 prevEventNumber = prevLog->GetEventNumber();
720 prevDdlId = prevLog->GetDdlID();
721 prevErrorCode = prevLog->GetErrorCode();
724 if ((prevEventNumber != fEventNumber) ||
725 (prevDdlId != ddlId) ||
726 (prevErrorCode != code)) {
727 new (fErrorLogs[fErrorLogs.GetEntriesFast()])
728 AliRawDataErrorLog(fEventNumber,
735 if (prevLog) prevLog->AddCount();
739 Bool_t AliRawReader::GotoEventWithID(Int_t event,
744 // Go to certain event number by
745 // checking the event ID.
746 // Useful in case event-selection
747 // is applied and the 'event' is
749 if (!GotoEvent(event)) return kFALSE;
751 while (GetBCID() != period ||
752 GetOrbitID() != orbitID ||
753 GetPeriod() != bcID) {
754 if (!NextEvent()) return kFALSE;