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 **************************************************************************/
20 #include <TObjString.h>
22 #include "AliLoader.h"
24 #include "AliStream.h"
26 ////////////////////////////////////////////////////////////////////////
30 // - store file names associated with a given stream
31 // - open and close files
32 // - return serial event number of the next event in the stream
33 // and the TFile pointer for a proper file
34 // Author: Jiri Chudoba (CERN), 2001
36 ////////////////////////////////////////////////////////////////////////
40 AliStream::AliStream():
41 fLastEventSerialNr(-1),
43 fCurrentFileIndex(-1),
49 // root requires default ctor, where no new objects can be created
50 // do not use this ctor, it is supplied only for root needs
52 //_______________________________________________________________________
54 AliStream::AliStream(const char* foldername,Option_t *option):
55 fLastEventSerialNr(-1),
57 fCurrentFileIndex(-1),
60 fFileNames(new TObjArray(1)),
61 fEventFolderName(foldername)
65 //_______________________________________________________________________
67 AliStream::AliStream(const AliStream &as):
69 fLastEventSerialNr(-1),
71 fCurrentFileIndex(-1),
82 //_______________________________________________________________________
84 AliStream::~AliStream()
87 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
88 if (fFileNames) delete fFileNames;
90 //_______________________________________________________________________
92 void AliStream::Copy(TObject &) const
97 Fatal("Copy","Not implemented!");
99 //_______________________________________________________________________
101 void AliStream::AddFile(const char *fileName)
103 // stores the name of the file
104 TObjString *name = new TObjString(fileName);
105 fFileNames->Add(name);
107 //_______________________________________________________________________
109 Bool_t AliStream::NextEventInStream()
111 // returns kFALSE if no more events
112 // returns kTRUE and the serial nr of the next event
113 // fCurrentFile points to the file containing offered event
116 if (fFileNames->GetLast() < 0) return kFALSE;
118 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
119 if (currentloader == 0x0)
121 Info("NextEventInStream",
122 "Can not get RL from folder named %s. Attempting to open next file",
123 fEventFolderName.Data());
124 Int_t res = OpenNextFile();
125 if ( res == 0) return kFALSE;
126 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
129 if (fLastEventSerialNr+1 >= fEvents)
131 if (!OpenNextFile()) return kFALSE;
133 Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1);
134 currentloader->GetEvent(++fLastEventSerialNr);
137 //_______________________________________________________________________
139 void AliStream::ChangeMode(Option_t* option)
141 // set the mode to READ or UPDATE, reopen file with the new mode
142 // only change from UPDATE to READ have sense in the current scheme,
143 // other changes are possible but not usefull
146 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
148 delete currentloader;
153 //_______________________________________________________________________
155 Bool_t AliStream::OpenNextFile()
158 // Opens next file in the list
160 if (++fCurrentFileIndex > fFileNames->GetLast()) {
161 Error("OpenNextFile", "No more files in the stream") ;
165 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
167 // check if the file was already opened by some other code
168 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
171 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
175 delete currentloader;
178 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
181 if (currentloader == 0x0)
183 // cannot open file specified on input. Do not skip it silently.
184 Error("OpenNextFile", "Cannot open session ");
188 // find nr of events in the given file
190 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
192 Int_t res = currentloader->LoadHeader();
195 Error("OpenNextFile","Problems with loading header");
198 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
202 //if it is new, create or recreate there is no chance to find header in file
206 fLastEventSerialNr = -1;
209 //_______________________________________________________________________
211 Bool_t AliStream::ImportgAlice()
214 // Imports gAlice object from file
216 if (fFileNames->GetLast() < 0) return kFALSE;
218 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
221 if (!OpenNextFile()) return kFALSE;
222 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
224 currentloader->LoadgAlice();
225 gAlice = currentloader->GetAliRun();
226 if (!gAlice) return kFALSE;
230 //_______________________________________________________________________
231 TString AliStream::GetFileName(Int_t order) const
233 // returns name of the order-th file
234 // returns empty string if such file does not exist
235 // first file in the input stream is 0
236 TString fileName("");
237 if (order > fFileNames->GetLast()) return fileName;
238 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
239 if (fileNameStored) fileName = fileNameStored->GetString();