]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliStream.cxx
Use default errors in case the vertexer didn't find any
[u/mrichter/AliRoot.git] / STEER / AliStream.cxx
index acd4d493d45758a2c0bbe3745d767c5e95316218..7658b123196efb466260cb0cd27b55c94c6df52f 100644 (file)
 
 /* $Id$ */
 
-//----------------------------------------------------------------------
+#include <TROOT.h>
+#include <TFile.h>
+#include <TObjString.h>
+
+#include "AliLog.h"
+#include "AliLoader.h"
+#include "AliRun.h"
+#include "AliStream.h"
+
+////////////////////////////////////////////////////////////////////////
+//
 // AliStream.cxx
+//
 // - store file names associated with a given stream
 // - open and close files
 // - return serial event number of the next event in the stream
 // and the TFile pointer for a proper file
-//----------------------------------------------------------------------
-
-#include <Riostream.h>
-
-#include "TFile.h"
-#include "TObjString.h"
-#include "TROOT.h"
-#include "TTree.h"
-
-#include "AliRun.h"
-#include "AliStream.h"
+//  Author: Jiri Chudoba (CERN), 2001
+//
+////////////////////////////////////////////////////////////////////////
 
 ClassImp(AliStream)
 
-//_______________________________________________________________________
 AliStream::AliStream():
-  fLastEventSerialNr(0),
+  fLastEventSerialNr(-1),
   fLastEventNr(0),
-  fCurrentFileIndex(0),
+  fCurrentFileIndex(-1),
   fEvents(0),
   fMode(0),
-  fCurrentFile(0),
-  fFileNames(0)
+  fFileNames(0x0),
+  fEventFolderName(0)
 {
-  //
   // root requires default ctor, where no new objects can be created
   // do not use this ctor, it is supplied only for root needs
-  //
 }
-
 //_______________________________________________________________________
-AliStream::AliStream(Option_t *option):
+
+AliStream::AliStream(const char* foldername,Option_t *option):
   fLastEventSerialNr(-1),
   fLastEventNr(0),
   fCurrentFileIndex(-1),
   fEvents(0),
   fMode(option),
-  fCurrentFile(0),
-  fFileNames(new TObjArray(1))
+  fFileNames(new TObjArray(1)),
+  fEventFolderName(foldername)
 {
-  //
-  // Default ctor
-  //
+// ctor
 }
-
 //_______________________________________________________________________
-AliStream::AliStream(const AliStream& str):
-  TNamed(str),
-  fLastEventSerialNr(0),
+
+AliStream::AliStream(const AliStream &as):
+  TNamed(as),
+  fLastEventSerialNr(-1),
   fLastEventNr(0),
-  fCurrentFileIndex(0),
+  fCurrentFileIndex(-1),
   fEvents(0),
   fMode(0),
-  fCurrentFile(0),
-  fFileNames(0)
+  fFileNames(0x0),
+  fEventFolderName(" ")
 {
   //
   // Copy ctor
   //
-  str.Copy(*this);
+  as.Copy(*this);
 }
-
 //_______________________________________________________________________
-void AliStream::Copy(AliStream& ) const
-{
-  Fatal("Copy","Not implemented\n");
-}
 
-//_______________________________________________________________________
 AliStream::~AliStream()
 {
 // default dtor
-  if (fFileNames) {
-    fFileNames->Delete();
-    delete fFileNames;
-  }
+  delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
+  if (fFileNames) delete fFileNames;
 }
+//_______________________________________________________________________
 
+void AliStream::Copy(TObject &) const
+{
+  //
+  // Copy function
+  //
+  AliFatal("Not implemented!");
+}
 //_______________________________________________________________________
+
 void AliStream::AddFile(const char *fileName)
 {
 // stores the name of the file
   TObjString *name = new TObjString(fileName);
   fFileNames->Add(name);
 }
-
 //_______________________________________________________________________
-Bool_t AliStream::NextEventInStream(Int_t &serialNr)
+
+Bool_t AliStream::NextEventInStream()
 {
 // returns kFALSE if no more events
 // returns kTRUE and the serial nr of the next event
@@ -116,100 +115,121 @@ Bool_t AliStream::NextEventInStream(Int_t &serialNr)
 
 // no files given:
   if (fFileNames->GetLast() < 0) return kFALSE;
-
-  if (!fCurrentFile) {
-    if (!OpenNextFile()) return kFALSE;
-  }
   
-  if (fLastEventSerialNr+1 >= fEvents) {
-    if (!OpenNextFile()) return kFALSE;
-  }
-
-  fLastEventSerialNr++;
-// in some cases the serial number does not start from 0, find the 
-// number of the next event
-  char name[20];
-  sprintf(name, "TreeS%d", ++fLastEventNr);
-  while (!fCurrentFile->Get(name) && fLastEventNr < fEvents)
-    sprintf(name, "TreeS%d", ++fLastEventNr);
-  serialNr = fLastEventNr;
+  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+  if (currentloader == 0x0) 
+   {
+    AliDebug(1, Form(
+         "Can not get RL from folder named %s. Attempting to open next file",
+         fEventFolderName.Data()));
+    Int_t res = OpenNextFile();
+    if ( res == 0) return kFALSE;
+    currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+   }
   
+  if (fLastEventSerialNr+1 >= fEvents) 
+   {
+    if (!OpenNextFile()) return kFALSE;
+   }
+  AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
+  currentloader->GetEvent(++fLastEventSerialNr);
   return kTRUE;
 }
-
 //_______________________________________________________________________
+
 void AliStream::ChangeMode(Option_t* option)
 {
   // set the mode to READ or UPDATE, reopen file with the new mode
   // only change from UPDATE to READ have sense in the current scheme,
   // other changes are possible but not usefull
+
   fMode = option;
-  if (fCurrentFile) {
-    fCurrentFile->Close();
+  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+  if (currentloader) {
+    delete currentloader;
     fCurrentFileIndex--;
     OpenNextFile();
   }
 }
-
 //_______________________________________________________________________
+
 Bool_t AliStream::OpenNextFile()
 {
   //
-  // Open the next file in the series
+  // Opens next file in the list
   //
   if (++fCurrentFileIndex > fFileNames->GetLast()) {
-    cerr<<"No more files in the stream"<<endl;
+    AliInfo("No more files in the stream") ;
     return kFALSE;
   }
 
-  const char * filename = 
-    static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
+  const char* filename =   static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
 
 // check if the file was already opened by some other code
-  TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
+  TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
   if (f) f->Close();
 
-  if (fCurrentFile) {
-    if (fCurrentFile->IsOpen()) {
-      fCurrentFile->Close();
-    }
-  }
+  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+  
+  if (currentloader) 
+   {
+     delete currentloader;
+   }
+  
+  currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
+  
 
-  fCurrentFile = TFile::Open(filename,fMode.Data());
-  if (!fCurrentFile) {
+  if (currentloader == 0x0) 
+   {
 // cannot open file specified on input. Do not skip it silently.
-    cerr<<"Cannot open file "<<filename<<endl;
+    AliError("Cannot open session ");
     return kFALSE;
-  }
+   }
+   
 // find nr of events in the given file  
-  TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
-  if (!te) {
-    Error("OpenNextFile", "input file does not contain TE");
-    return kFALSE;
-  }
-  fEvents = static_cast<Int_t>(te->GetEntries());
+  
+  if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
+   {
+    Int_t res = currentloader->LoadHeader();
+    if (res)
+     {
+       AliError("Problems with loading header");
+       return kFALSE;
+     }
+    fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
+   }
+  else
+    {
+     //if it is new, create or recreate there is no chance to find header in file
+      fEvents = 0;
+    }
+   
   fLastEventSerialNr = -1;
-  fLastEventNr = -1;
   return kTRUE;
 }
-
 //_______________________________________________________________________
+
 Bool_t AliStream::ImportgAlice()
 {
   //
-  // Import AliRun object pointed from gAlice
+  // Imports gAlice object from file
   //
   if (fFileNames->GetLast() < 0) return kFALSE;
-  if (!fCurrentFile) {
+  
+  AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+  if (!currentloader) 
+   {
     if (!OpenNextFile()) return kFALSE;
-  }
-  gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
+    currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
+   }
+  currentloader->LoadgAlice();
+  gAlice = currentloader->GetAliRun();
   if (!gAlice)  return kFALSE;
   return kTRUE;
 }
 
 //_______________________________________________________________________
-TString AliStream::GetFileName(const Int_t order) const
+TString AliStream::GetFileName(Int_t order) const
 {
   // returns name of the order-th file
   // returns empty string if such file does not exist