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>
23 #include "AliLoader.h"
25 #include "AliStream.h"
27 ////////////////////////////////////////////////////////////////////////
31 // - store file names associated with a given stream
32 // - open and close files
33 // - return serial event number of the next event in the stream
34 // and the TFile pointer for a proper file
35 // Author: Jiri Chudoba (CERN), 2001
37 ////////////////////////////////////////////////////////////////////////
41 AliStream::AliStream():
42 fLastEventSerialNr(-1),
44 fCurrentFileIndex(-1),
50 // root requires default ctor, where no new objects can be created
51 // do not use this ctor, it is supplied only for root needs
53 //_______________________________________________________________________
55 AliStream::AliStream(const char* foldername,Option_t *option):
56 fLastEventSerialNr(-1),
58 fCurrentFileIndex(-1),
61 fFileNames(new TObjArray(1)),
62 fEventFolderName(foldername)
66 //_______________________________________________________________________
68 AliStream::AliStream(const AliStream &as):
70 fLastEventSerialNr(-1),
72 fCurrentFileIndex(-1),
83 //_______________________________________________________________________
85 AliStream::~AliStream()
88 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
89 if (fFileNames) delete fFileNames;
91 //_______________________________________________________________________
93 void AliStream::Copy(TObject &) const
98 AliFatal("Not implemented!");
100 //_______________________________________________________________________
102 void AliStream::AddFile(const char *fileName)
104 // stores the name of the file
105 TObjString *name = new TObjString(fileName);
106 fFileNames->Add(name);
108 //_______________________________________________________________________
110 Bool_t AliStream::NextEventInStream()
112 // returns kFALSE if no more events
113 // returns kTRUE and the serial nr of the next event
114 // fCurrentFile points to the file containing offered event
117 if (fFileNames->GetLast() < 0) return kFALSE;
119 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
120 if (currentloader == 0x0)
123 "Can not get RL from folder named %s. Attempting to open next file",
124 fEventFolderName.Data()));
125 Int_t res = OpenNextFile();
126 if ( res == 0) return kFALSE;
127 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
130 if (fLastEventSerialNr+1 >= fEvents)
132 if (!OpenNextFile()) return kFALSE;
134 AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
135 currentloader->GetEvent(++fLastEventSerialNr);
138 //_______________________________________________________________________
140 void AliStream::ChangeMode(Option_t* option)
142 // set the mode to READ or UPDATE, reopen file with the new mode
143 // only change from UPDATE to READ have sense in the current scheme,
144 // other changes are possible but not usefull
147 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
149 delete currentloader;
154 //_______________________________________________________________________
156 Bool_t AliStream::OpenNextFile()
159 // Opens next file in the list
161 if (++fCurrentFileIndex > fFileNames->GetLast()) {
162 AliInfo("No more files in the stream") ;
166 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
168 // check if the file was already opened by some other code
169 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
172 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
176 delete currentloader;
179 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
182 if (currentloader == 0x0)
184 // cannot open file specified on input. Do not skip it silently.
185 AliError("Cannot open session ");
189 // find nr of events in the given file
191 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
193 Int_t res = currentloader->LoadHeader();
196 AliError("Problems with loading header");
199 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
203 //if it is new, create or recreate there is no chance to find header in file
207 fLastEventSerialNr = -1;
210 //_______________________________________________________________________
212 Bool_t AliStream::ImportgAlice()
215 // Imports gAlice object from file
217 if (fFileNames->GetLast() < 0) return kFALSE;
219 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
222 if (!OpenNextFile()) return kFALSE;
223 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
225 currentloader->LoadgAlice();
226 gAlice = currentloader->GetAliRun();
227 if (!gAlice) return kFALSE;
231 //_______________________________________________________________________
232 TString AliStream::GetFileName(Int_t order) const
234 // returns name of the order-th file
235 // returns empty string if such file does not exist
236 // first file in the input stream is 0
237 TString fileName("");
238 if (order > fFileNames->GetLast()) return fileName;
239 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
240 if (fileNameStored) fileName = fileNameStored->GetString();