]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderFile.cxx
Possibility to compile with AliAnalysisGoodies (Yves)
[u/mrichter/AliRoot.git] / RAW / AliRawReaderFile.cxx
index fb18b8f3b215077616177889191fcd7cefa56a25..fe33f776157d003f77ea0b4fab3ac75d1564f1d9 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 ///////////////////////////////////////////////////////////////////////////////
-//
-// This is a class for reading a raw data file and providing
-// information about digits
-//
+/// 
+/// This is a class for reading raw data files.
+///
+/// The files of one event are expected to be in one directory. The name 
+/// of the directory is "raw" + the event number. Each file contains
+/// the raw data (with data header) of one DDL. The convention for the
+/// file names is "DET_#DDL.ddl". "DET" is the name of the detector and
+/// "#DDL" is the unique equipment ID.
+///
+/// The constructor of AliRawReaderFile takes the event number or the
+/// directory name as argument.
+/// 
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "AliRawReaderFile.h"
+#include <TSystem.h>
 
 
 ClassImp(AliRawReaderFile)
 
 
-AliRawReaderFile::AliRawReaderFile(const char* fileName, Bool_t addNumber)
+AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) :
+  fEventIndex(eventNumber),
+  fDirName("."),
+  fDirectory(NULL),
+  fStream(NULL),
+  fEquipmentId(-1),
+  fBuffer(NULL),
+  fBufferSize(0)
 {
-// create an object to read digits from the given input file(s)
-// if addNumber is true, a number starting at 1 is appended to the file name
+// create an object to read digits from the given event
+// in the current directory
 
-  fFileName = fileName;
-  if (!addNumber) {
-    fFileNumber = -1;
-#ifndef __DECCXX
-    fStream = new fstream(fileName, ios::binary|ios::in);
-#else
-    fStream = new fstream(fileName, ios::in);
-#endif
-  } else {
-    fFileNumber = 0;
-    fStream = NULL;
-    OpenNextFile();
-  }
-  fMiniHeader = new AliMiniHeader;
-  fBuffer = NULL;
-  fBufferSize = 0;
+  fDirectory = OpenDirectory();
+  OpenNextFile();
+  fHeader = new AliRawDataHeader;
+}
+
+AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) :
+  fEventIndex(eventNumber),
+  fDirName(dirName),
+  fDirectory(NULL),
+  fStream(NULL),
+  fEquipmentId(-1),
+  fBuffer(NULL),
+  fBufferSize(0)
+{
+// create an object to read digits from the given directory
+
+  fDirectory = OpenDirectory();
+  OpenNextFile();
+  fHeader = new AliRawDataHeader;
 }
 
 AliRawReaderFile::~AliRawReaderFile()
 {
 // close the input file
 
+  if (fDirectory) gSystem->FreeDirectory(fDirectory);
   if (fStream) {
 #if defined(__HP_aCC) || defined(__DECCXX)
     if (fStream->rdbuf()->is_open()) fStream->close();
@@ -61,13 +83,50 @@ AliRawReaderFile::~AliRawReaderFile()
 #endif
     delete fStream;
   }
-  delete fMiniHeader;
+  delete fHeader;
   if (fBuffer) delete[] fBuffer;
 }
 
+void AliRawReaderFile::RequireHeader(Bool_t required)
+{
+  // Reading of raw data in case of missing
+  // raw data header is not implemented for
+  // this class
+  if (!required)
+    Fatal("AliRawReaderFile","Reading of raw data without raw data header is not implemented !");
+
+  AliRawReader::RequireHeader(required);
+}
+
+TString AliRawReaderFile::GetDirName() const
+{
+// return the current directory name
+
+  TString dirName(fDirName);
+  if (fEventIndex >= 0) {
+    dirName += "/raw";
+    dirName += fEventIndex;
+  }
+  return dirName;
+}
+
+void* AliRawReaderFile::OpenDirectory()
+{
+// open and return the directory
+
+  TString dirName = GetDirName();
+  void* directory = gSystem->OpenDirectory(dirName);
+  if (!directory) {
+    Error("OpenDirectory", "could not open directory %s", dirName.Data());
+  }
+  return directory;
+}
 
 Bool_t AliRawReaderFile::OpenNextFile()
 {
+// open the next file
+// returns kFALSE if the current file is the last one
+
   if (fStream) {
 #if defined(__HP_aCC) || defined(__DECCXX)
     if (fStream->rdbuf()->is_open()) fStream->close();
@@ -76,17 +135,27 @@ Bool_t AliRawReaderFile::OpenNextFile()
 #endif
     delete fStream;
     fStream = NULL;
+    fEquipmentId = -1;
   }
-  if (fFileNumber < 0) return kFALSE;
 
-  fFileNumber++;
-  char fileName[256];
-  sprintf(fileName, "%s%d", fFileName.Data(), fFileNumber);
+  if (!fDirectory) return kFALSE;
+  TString entry;
+  while (entry = gSystem->GetDirEntry(fDirectory)) {
+    if (entry.IsNull()) return kFALSE;
+    if (!entry.EndsWith(".ddl")) continue;
+    char* fileName = gSystem->ConcatFileName(GetDirName(), entry);
 #ifndef __DECCXX 
-  fStream = new fstream(fileName, ios::binary|ios::in);
+    fStream = new fstream(fileName, ios::binary|ios::in);
 #else
-  fStream = new fstream(fileName, ios::in);
+    fStream = new fstream(fileName, ios::in);
 #endif
+    break;
+  }
+
+  if (!fStream) return kFALSE;
+  entry.Remove(0, entry.Last('_')+1);
+  entry.Remove(entry.Length()-4);
+  fEquipmentId = atoi(entry.Data());
 #if defined(__HP_aCC) || defined(__DECCXX)
   return (fStream->rdbuf()->is_open());
 #else
@@ -95,19 +164,25 @@ Bool_t AliRawReaderFile::OpenNextFile()
 }
 
 
-Bool_t AliRawReaderFile::ReadMiniHeader()
+Bool_t AliRawReaderFile::ReadHeader()
 {
-// read a mini header at the current stream position
+// read a data header at the current stream position
 // returns kFALSE if the mini header could not be read
 
   if (!fStream) return kFALSE;
   do {
     if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
-    while (!fStream->read((char*) fMiniHeader, sizeof(AliMiniHeader))) {
+    while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) {
       if (!OpenNextFile()) return kFALSE;
     }
-    CheckMiniHeader();
-    fCount = fMiniHeader->fSize;
+    if (fHeader->fSize != 0xFFFFFFFF) {
+      fCount = fHeader->fSize - sizeof(AliRawDataHeader);
+    } else {
+      UInt_t currentPos = fStream->tellg();
+      fStream->seekg(0, ios::end);
+      fCount = UInt_t(fStream->tellg()) - currentPos;
+      fStream->seekg(currentPos);
+    }
   } while (!IsSelected());
   return kTRUE;
 }
@@ -118,7 +193,7 @@ Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data)
 // returns kFALSE if the data could not be read
 
   while (fCount == 0) {
-    if (!ReadMiniHeader()) return kFALSE;
+    if (!ReadHeader()) return kFALSE;
   }
   if (fBufferSize < fCount) {
     if (fBuffer) delete[] fBuffer;
@@ -151,9 +226,12 @@ Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size)
 
 Bool_t AliRawReaderFile::Reset()
 {
-// reset the current stream position to the beginning of the file
+// reset the current stream position to the first DDL file of the curevent
+
+  void* directory = OpenDirectory();
+  if (!directory) return kFALSE;
 
-  if ((fFileNumber > 0) && fStream) {
+  if (fStream) {
 #if defined(__HP_aCC) || defined(__DECCXX)
     if (fStream->rdbuf()->is_open()) fStream->close();
 #else
@@ -161,24 +239,43 @@ Bool_t AliRawReaderFile::Reset()
 #endif
     delete fStream;
     fStream = NULL;
-    fFileNumber = 0;
   }
 
-  if (!fStream) {
-    if (fFileNumber < 0) {
-#ifndef __DECCXX
-      fStream = new fstream(fFileName, ios::binary|ios::in);
-#else
-      fStream = new fstream(fFileName, ios::in);
-#endif
-    } else {
-      if (!OpenNextFile()) return kFALSE;
-    }
-  }
+  if (fDirectory) gSystem->FreeDirectory(fDirectory);
+  fDirectory = directory;
 
-  if (!fStream || !fStream->rdbuf()->is_open()) return kFALSE;
-  fStream->seekg(0);
+  OpenNextFile();
   fCount = 0;
   return kTRUE;
 }
 
+Bool_t AliRawReaderFile::NextEvent()
+{
+// go to the next event directory
+
+  if (fEventIndex < -1) return kFALSE;
+
+  do {
+    TString dirName = fDirName + "/raw";
+    dirName += (fEventIndex + 1);
+    void* directory = gSystem->OpenDirectory(dirName);
+    if (!directory) return kFALSE;
+    gSystem->FreeDirectory(directory);
+
+    fEventIndex++;
+    Reset();
+  } while (!IsEventSelected());
+
+  fEventNumber++;
+
+  return kTRUE;
+}
+
+Bool_t AliRawReaderFile::RewindEvents()
+{
+// reset the event counter
+
+  if (fEventIndex >= 0)  fEventIndex = -1;
+  fEventNumber = -1;
+  return Reset();
+}