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>
24 #include "AliLoader.h"
26 #include "AliStream.h"
28 ////////////////////////////////////////////////////////////////////////
32 // - store file names associated with a given stream
33 // - open and close files
34 // - return serial event number of the next event in the stream
35 // and the TFile pointer for a proper file
36 // Author: Jiri Chudoba (CERN), 2001
38 ////////////////////////////////////////////////////////////////////////
42 AliStream::AliStream():
43 fLastEventSerialNr(-1),
45 fCurrentFileIndex(-1),
51 // root requires default ctor, where no new objects can be created
52 // do not use this ctor, it is supplied only for root needs
54 //_______________________________________________________________________
56 AliStream::AliStream(const char* foldername,Option_t *option):
57 fLastEventSerialNr(-1),
59 fCurrentFileIndex(-1),
62 fFileNames(new TObjArray(1)),
63 fEventFolderName(foldername)
67 //_______________________________________________________________________
69 AliStream::AliStream(const AliStream &as):
71 fLastEventSerialNr(-1),
73 fCurrentFileIndex(-1),
84 //_______________________________________________________________________
86 AliStream::~AliStream()
89 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
90 if (fFileNames) delete fFileNames;
92 //_______________________________________________________________________
94 void AliStream::Copy(TObject &) const
99 AliFatal("Not implemented!");
101 //_______________________________________________________________________
103 void AliStream::AddFile(const char *fileName)
105 // stores the name of the file
106 TObjString *name = new TObjString(fileName);
107 fFileNames->Add(name);
109 //_______________________________________________________________________
111 Bool_t AliStream::NextEventInStream()
113 // returns kFALSE if no more events
114 // returns kTRUE and the serial nr of the next event
115 // fCurrentFile points to the file containing offered event
118 if (fFileNames->GetLast() < 0) return kFALSE;
120 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
121 if (currentloader == 0x0)
124 "Can not get RL from folder named %s. Attempting to open next file",
125 fEventFolderName.Data()));
126 Int_t res = OpenNextFile();
127 if ( res == 0) return kFALSE;
128 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
131 if (fLastEventSerialNr+1 >= fEvents)
133 if (!OpenNextFile()) return kFALSE;
135 AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
136 currentloader->GetEvent(++fLastEventSerialNr);
139 //_______________________________________________________________________
141 void AliStream::ChangeMode(Option_t* option)
143 // set the mode to READ or UPDATE, reopen file with the new mode
144 // only change from UPDATE to READ have sense in the current scheme,
145 // other changes are possible but not usefull
148 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
150 delete currentloader;
155 //_______________________________________________________________________
157 Bool_t AliStream::OpenNextFile()
160 // Opens next file in the list
162 if (++fCurrentFileIndex > fFileNames->GetLast()) {
163 AliInfo("No more files in the stream") ;
167 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
169 // check if the file was already opened by some other code
170 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
173 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
177 delete currentloader;
180 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
183 if (currentloader == 0x0)
185 // cannot open file specified on input. Do not skip it silently.
186 AliError("Cannot open session ");
190 // find nr of events in the given file
192 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
194 Int_t res = currentloader->LoadHeader();
197 AliError("Problems with loading header");
200 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
204 //if it is new, create or recreate there is no chance to find header in file
208 fLastEventSerialNr = -1;
211 //_______________________________________________________________________
213 Bool_t AliStream::ImportgAlice()
216 // Imports gAlice object from file
218 if (fFileNames->GetLast() < 0) return kFALSE;
220 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
223 if (!OpenNextFile()) return kFALSE;
224 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
226 currentloader->LoadgAlice();
227 gAlice = currentloader->GetAliRun();
228 if (!gAlice) return kFALSE;
232 //_______________________________________________________________________
233 TString AliStream::GetFileName(Int_t order) const
235 // returns name of the order-th file
236 // returns empty string if such file does not exist
237 // first file in the input stream is 0
238 TString fileName("");
239 if (order > fFileNames->GetLast()) return fileName;
240 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
241 if (fileNameStored) fileName = fileNameStored->GetString();