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 **************************************************************************/
19 #include <TObjString.h>
21 #include "AliLoader.h"
23 #include "AliStream.h"
25 ////////////////////////////////////////////////////////////////////////
29 // - store file names associated with a given stream
30 // - open and close files
31 // - return serial event number of the next event in the stream
32 // and the TFile pointer for a proper file
34 ////////////////////////////////////////////////////////////////////////
38 AliStream::AliStream():
39 fLastEventSerialNr(-1),
41 fCurrentFileIndex(-1),
47 // root requires default ctor, where no new objects can be created
48 // do not use this ctor, it is supplied only for root needs
50 //_______________________________________________________________________
52 AliStream::AliStream(const char* foldername,Option_t *option):
53 fLastEventSerialNr(-1),
55 fCurrentFileIndex(-1),
58 fFileNames(new TObjArray(1)),
59 fEventFolderName(foldername)
63 //_______________________________________________________________________
65 AliStream::AliStream(const AliStream &as):
67 fLastEventSerialNr(-1),
69 fCurrentFileIndex(-1),
80 //_______________________________________________________________________
82 AliStream::~AliStream()
85 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
86 if (fFileNames) delete fFileNames;
88 //_______________________________________________________________________
90 void AliStream::Copy(TObject &) const
95 Fatal("Copy","Not implemented!");
97 //_______________________________________________________________________
99 void AliStream::AddFile(const char *fileName)
101 // stores the name of the file
102 TObjString *name = new TObjString(fileName);
103 fFileNames->Add(name);
105 //_______________________________________________________________________
107 Bool_t AliStream::NextEventInStream()
109 // returns kFALSE if no more events
110 // returns kTRUE and the serial nr of the next event
111 // fCurrentFile points to the file containing offered event
114 if (fFileNames->GetLast() < 0) return kFALSE;
116 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
117 if (currentloader == 0x0)
119 Info("NextEventInStream",
120 "Can not get RL from folder named %s. Attempting to open next file",
121 fEventFolderName.Data());
122 Int_t res = OpenNextFile();
123 if ( res == 0) return kFALSE;
124 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
127 if (fLastEventSerialNr+1 >= fEvents)
129 if (!OpenNextFile()) return kFALSE;
131 Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1);
132 currentloader->GetEvent(++fLastEventSerialNr);
135 //_______________________________________________________________________
137 void AliStream::ChangeMode(Option_t* option)
139 // set the mode to READ or UPDATE, reopen file with the new mode
140 // only change from UPDATE to READ have sense in the current scheme,
141 // other changes are possible but not usefull
144 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
146 delete currentloader;
151 //_______________________________________________________________________
153 Bool_t AliStream::OpenNextFile()
156 // Opens next file in the list
158 if (++fCurrentFileIndex > fFileNames->GetLast()) {
159 Error("OpenNextFile", "No more files in the stream") ;
163 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
165 // check if the file was already opened by some other code
166 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
169 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
173 delete currentloader;
176 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
179 if (currentloader == 0x0)
181 // cannot open file specified on input. Do not skip it silently.
182 Error("OpenNextFile", "Cannot open session ");
186 // find nr of events in the given file
188 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
190 Int_t res = currentloader->LoadHeader();
193 Error("OpenNextFile","Problems with loading header");
196 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
200 //if it is new, create or recreate there is no chance to find header in file
204 fLastEventSerialNr = -1;
207 //_______________________________________________________________________
209 Bool_t AliStream::ImportgAlice()
212 // Imports gAlice object from file
214 if (fFileNames->GetLast() < 0) return kFALSE;
216 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
219 if (!OpenNextFile()) return kFALSE;
220 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
222 currentloader->LoadgAlice();
223 gAlice = currentloader->GetAliRun();
224 if (!gAlice) return kFALSE;
228 //_______________________________________________________________________
229 TString AliStream::GetFileName(Int_t order) const
231 // returns name of the order-th file
232 // returns empty string if such file does not exist
233 // first file in the input stream is 0
234 TString fileName("");
235 if (order > fFileNames->GetLast()) return fileName;
236 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
237 if (fileNameStored) fileName = fileNameStored->GetString();