]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliStream.cxx
Processing SPD Mean Vertex only in PHYSICS runs.
[u/mrichter/AliRoot.git] / STEER / AliStream.cxx
index 2f8937816235ad8646d5624a51e88b0ce306aaa8..7658b123196efb466260cb0cd27b55c94c6df52f 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.5  2002/04/09 13:38:47  jchudoba
-Add const to the filename argument
+/* $Id$ */
 
-Revision 1.4  2001/12/03 07:10:13  jchudoba
-Default ctor cannot create new objects, create dummy default ctor which leaves object in not well defined state - to be used only by root for I/O
+#include <TROOT.h>
+#include <TFile.h>
+#include <TObjString.h>
 
-Revision 1.3  2001/10/15 17:31:56  jchudoba
-Bug correction
-
-Revision 1.2  2001/10/04 15:58:52  jchudoba
-Option to open the stream in READ or UPDATE mode
-
-Revision 1.1  2001/09/19 06:20:50  jchudoba
-Class to manage input filenames, used by AliRunDigitizer
-
-*/
+#include "AliLog.h"
+#include "AliLoader.h"
+#include "AliRun.h"
+#include "AliStream.h"
 
 ////////////////////////////////////////////////////////////////////////
 //
@@ -40,62 +32,82 @@ Class to manage input filenames, used by AliRunDigitizer
 // - open and close files
 // - return serial event number of the next event in the stream
 // and the TFile pointer for a proper file
+//  Author: Jiri Chudoba (CERN), 2001
 //
 ////////////////////////////////////////////////////////////////////////
 
-#include <iostream.h>
-
-#include "TTree.h"
-#include "TROOT.h"
-
-#include "AliStream.h"
-
-#include "AliRun.h"
-
 ClassImp(AliStream)
 
-AliStream::AliStream()
+AliStream::AliStream():
+  fLastEventSerialNr(-1),
+  fLastEventNr(0),
+  fCurrentFileIndex(-1),
+  fEvents(0),
+  fMode(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
-  fCurrentFile = 0;
-  fEvents = 0;
-  fFileNames = 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),
+  fFileNames(new TObjArray(1)),
+  fEventFolderName(foldername)
 {
 // ctor
-  fLastEventSerialNr = -1;
-  fLastEventNr = 0;
-  fCurrentFileIndex = -1;
-  fCurrentFile = 0;
-  fEvents = 0;
-  fFileNames = new TObjArray(1);
-  fMode = option;
 }
+//_______________________________________________________________________
+
+AliStream::AliStream(const AliStream &as):
+  TNamed(as),
+  fLastEventSerialNr(-1),
+  fLastEventNr(0),
+  fCurrentFileIndex(-1),
+  fEvents(0),
+  fMode(0),
+  fFileNames(0x0),
+  fEventFolderName(" ")
+{
+  //
+  // Copy ctor
+  //
+  as.Copy(*this);
+}
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
 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
@@ -103,91 +115,129 @@ 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) {
+  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;
-  }
-  serialNr = ++fLastEventSerialNr;
+   }
+  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
 {
+  // 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()
 {
+  //
+  // 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 = (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 = (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;
   return kTRUE;
 }
+//_______________________________________________________________________
 
-////////////////////////////////////////////////////////////////////////
 Bool_t AliStream::ImportgAlice()
 {
+  //
+  // 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 = (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
-// returns name of the order-th file
-// returns empty string if such file does not exist
-// first file in the input stream is 0
+
+//_______________________________________________________________________
+TString AliStream::GetFileName(Int_t order) const
 {
+  // returns name of the order-th file
+  // returns empty string if such file does not exist
+  // first file in the input stream is 0
   TString fileName("");
   if (order > fFileNames->GetLast()) return fileName;
   TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
   if (fileNameStored) fileName = fileNameStored->GetString();
   return fileName;
 }
-////////////////////////////////////////////////////////////////////////
+