]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReader.cxx
Adding new URI for raw data - 'raw://run<#>', where <#> is the run number. The URI...
[u/mrichter/AliRoot.git] / RAW / AliRawReader.cxx
index 91195ceea9a8b435b80c6c947bc5a0c9c140671f..a336d4693f7dfc3bc900ac78dfbf663009cb29bf 100644 (file)
 #include <TClass.h>
 #include <TPluginManager.h>
 #include <TROOT.h>
+#include <TInterpreter.h>
+#include <TSystem.h>
 
 #include <Riostream.h>
 #include "AliRawReader.h"
 #include "AliRawReaderFile.h"
 #include "AliRawReaderDate.h"
 #include "AliRawReaderRoot.h"
+#include "AliRawReaderChain.h"
 #include "AliDAQ.h"
 #include "AliLog.h"
 
@@ -62,10 +65,13 @@ AliRawReader::AliRawReader() :
   fSelectMaxEquipmentId(-1),
   fSkipInvalid(kFALSE),
   fSelectEventType(-1),
+  fSelectTriggerMask(0),
+  fSelectTriggerExpr(),
   fErrorCode(0),
   fEventNumber(-1),
   fErrorLogs("AliRawDataErrorLog",100),
-  fHeaderSwapped(NULL)
+  fHeaderSwapped(NULL),
+  fIsValid(kTRUE)
 {
 // default constructor: initialize data members
 // Allocate the swapped header in case of Mac
@@ -118,10 +124,13 @@ AliRawReader::AliRawReader(const AliRawReader& rawReader) :
   fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
   fSkipInvalid(rawReader.fSkipInvalid),
   fSelectEventType(rawReader.fSelectEventType),
+  fSelectTriggerMask(rawReader.fSelectTriggerMask),
+  fSelectTriggerExpr(rawReader.fSelectTriggerExpr),
   fErrorCode(0),
   fEventNumber(-1),
   fErrorLogs("AliRawDataErrorLog",100),
-  fHeaderSwapped(NULL)
+  fHeaderSwapped(NULL),
+  fIsValid(rawReader.fIsValid)
 {
 // copy constructor
 // Allocate the swapped header in case of Mac
@@ -144,12 +153,16 @@ AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
   fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
   fSkipInvalid = rawReader.fSkipInvalid;
   fSelectEventType = rawReader.fSelectEventType;
+  fSelectTriggerMask = rawReader.fSelectTriggerMask;
+  fSelectTriggerExpr = rawReader.fSelectTriggerExpr;
 
   fErrorCode = rawReader.fErrorCode;
 
   fEventNumber = rawReader.fEventNumber;
   fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
 
+  fIsValid = rawReader.fIsValid;
+
   return *this;
 }
 
@@ -181,20 +194,13 @@ AliRawReader* AliRawReader::Create(const char *uri)
     return NULL;
   }
 
+  TObjArray *fields = strURI.Tokenize("?");
+  TString &fileURI = ((TObjString*)fields->At(0))->String();
+
   AliRawReader *rawReader = NULL;
-  if (!strURI.BeginsWith("mem://")) {
-    AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",strURI.Data()));
-    if (strURI.EndsWith("/")) {
-      rawReader = new AliRawReaderFile(strURI);
-    } else if (strURI.EndsWith(".root")) {
-      rawReader = new AliRawReaderRoot(strURI);
-    } else {
-      rawReader = new AliRawReaderDate(strURI);
-    }
-  }
-  else {
-    strURI.ReplaceAll("mem://","");
-    AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",strURI.Data()));
+  if (fileURI.BeginsWith("mem://") || fileURI.BeginsWith("^")) {
+    if (fileURI.BeginsWith("mem://")) fileURI.ReplaceAll("mem://","");
+    AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
 
     TPluginManager* pluginManager = gROOT->GetPluginManager();
     TString rawReaderName = "AliRawReaderDateOnline";
@@ -206,12 +212,89 @@ AliRawReader* AliRawReader::Create(const char *uri)
       pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
     }
     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
-      rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,strURI.Data());
+      rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
+    }
+    else {
+      delete fields;
+      return NULL;
+    }
+  }
+  else if (fileURI.BeginsWith("amore://")) {
+    // A special raw-data URL used in case
+    // the raw-data reading is steered from
+    // ouside, i.e. from AMORE
+    fileURI.ReplaceAll("amore://","");
+    AliInfoClass("Creating raw-reader in order to read events sent by AMORE");
+    rawReader = new AliRawReaderDate((void *)NULL);
+  }
+  else if (fileURI.BeginsWith("collection://")) {
+    fileURI.ReplaceAll("collection://","");
+    AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
+    rawReader = new AliRawReaderChain(fileURI);
+  }
+  else if (fileURI.BeginsWith("raw://run")) {
+    fileURI.ReplaceAll("raw://run","");
+    if (fileURI.IsDigit()) {
+      rawReader = new AliRawReaderChain(fileURI.Atoi());
     }
     else {
+      AliErrorClass(Form("Invalid syntax: %s",fileURI.Data()));
+      fields->Delete();
       return NULL;
     }
   }
+  else {
+    AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
+    TString filename(gSystem->ExpandPathName(fileURI.Data()));
+    if (filename.EndsWith("/")) {
+      rawReader = new AliRawReaderFile(filename);
+    } else if (filename.EndsWith(".root")) {
+      rawReader = new AliRawReaderRoot(filename);
+    } else {
+      rawReader = new AliRawReaderDate(filename);
+    }
+  }
+
+  if (!rawReader->IsRawReaderValid()) {
+    AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
+    delete rawReader;
+    fields->Delete();
+    delete fields;
+    return NULL;
+  }
+
+  // Now apply event selection criteria (if specified)
+  if (fields->GetEntries() > 1) {
+    Int_t eventType = -1;
+    ULong64_t triggerMask = 0;
+    TString triggerExpr;
+    for(Int_t i = 1; i < fields->GetEntries(); i++) {
+      if (!fields->At(i)) continue;
+      TString &option = ((TObjString*)fields->At(i))->String();
+      if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
+       option.ReplaceAll("EventType=","");
+       eventType = option.Atoi();
+       continue;
+      }
+      if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
+       option.ReplaceAll("Trigger=","");
+       if (option.IsDigit()) {
+         triggerMask = option.Atoll();
+       }
+       else {
+         triggerExpr = option.Data();
+       }
+       continue;
+      }
+      AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
+    }
+    AliInfoClass(Form("Event selection criteria specified:   eventype=%d   trigger mask=%llx   trigger expression=%s",
+                eventType,triggerMask,triggerExpr.Data()));
+    rawReader->SelectEvents(eventType,triggerMask,triggerExpr.Data());
+  }
+
+  fields->Delete();
+  delete fields;
 
   return rawReader;
 }
@@ -312,12 +395,32 @@ void AliRawReader::SelectEquipment(Int_t equipmentType,
   fSelectMaxEquipmentId = maxEquipmentId;
 }
 
-void AliRawReader::SelectEvents(Int_t type)
+void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask,
+                               const char *triggerExpr)
 {
-// read only events with the given type.
-// no selection is applied if a value < 0 is used.
+// read only events with the given type and optionally
+// trigger mask.
+// no selection is applied if value = 0 is used.
+// Trigger selection can be done via string (triggerExpr)
+// which defines the trigger logic to be used. It works only
+// after LoadTriggerClass() method is called for all involved
+// trigger classes.
 
   fSelectEventType = type;
+  fSelectTriggerMask = triggerMask;
+  if (triggerExpr) fSelectTriggerExpr = triggerExpr;
+}
+
+void AliRawReader::LoadTriggerClass(const char* name, Int_t index)
+{
+  // Loads the list of trigger classes defined.
+  // Used in conjunction with IsEventSelected in the
+  // case when the trigger selection is given by
+  // fSelectedTriggerExpr
+
+  if (fSelectTriggerExpr.IsNull()) return;
+
+  fSelectTriggerExpr.ReplaceAll(name,Form("[%d]",index));
 }
 
 Bool_t AliRawReader::IsSelected() const
@@ -347,12 +450,37 @@ Bool_t AliRawReader::IsSelected() const
 
 Bool_t AliRawReader::IsEventSelected() const
 {
-// apply the event selection (if any)
+  // apply the event selection (if any)
 
+  // First check the event type
   if (fSelectEventType >= 0) {
     if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
   }
 
+  // Then check the trigger pattern and compared it
+  // to the required trigger mask
+  if (fSelectTriggerMask != 0) {
+    if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
+  }
+
+  if (!fSelectTriggerExpr.IsNull()) {
+    TString expr(fSelectTriggerExpr);
+    ULong64_t mask = GetClassMask();
+    for(Int_t itrigger = 0; itrigger < 50; itrigger++) {
+      if (mask & (1 << itrigger)) {
+       expr.ReplaceAll(Form("[%d]",itrigger),"1");
+      }
+      else {
+       expr.ReplaceAll(Form("[%d]",itrigger),"0");
+      }
+    }
+    Int_t error;
+    if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
+       (error == TInterpreter::kNoError)) {
+      return kFALSE;
+    }
+  }
+
   return kTRUE;
 }
 
@@ -432,6 +560,20 @@ Bool_t AliRawReader::ReadNextChar(UChar_t& data)
   return kTRUE;
 }
 
+Bool_t  AliRawReader::GotoEvent(Int_t event)
+{
+  // Random access to certain
+  // event index. Could be very slow
+  // for some non-root raw-readers.
+  // So it should be reimplemented there.
+  if (event < fEventNumber) RewindEvents();
+
+  while (fEventNumber < event) {
+    if (!NextEvent()) return kFALSE;
+  }
+
+  return kTRUE;
+}
 
 Int_t AliRawReader::CheckData() const
 {
@@ -555,7 +697,6 @@ void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
   // Add a raw data error message to the list
   // of raw-data decoding errors
   if (fEventNumber < 0) {
-    AliError("No events have read so far! Impossible to add a raw data error log!");
     return;
   }
   Int_t ddlId = GetEquipmentId();
@@ -588,3 +729,25 @@ void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
     if (prevLog) prevLog->AddCount();
 
 }
+
+Bool_t AliRawReader::GotoEventWithID(Int_t event, 
+                                    UInt_t period,
+                                    UInt_t orbitID,
+                                    UShort_t bcID)
+{
+  // Go to certain event number by
+  // checking the event ID.
+  // Useful in case event-selection
+  // is applied and the 'event' is
+  // relative
+  if (!GotoEvent(event)) return kFALSE;
+
+  while (GetBCID()    != period  ||
+        GetOrbitID() != orbitID ||
+        GetPeriod()  != bcID) {
+    if (!NextEvent()) return kFALSE;
+  }
+
+  return kTRUE;
+}
+