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 **************************************************************************/
18 //----------------------------------------------------------------------
20 // - store file names associated with a given stream
21 // - open and close files
22 // - return serial event number of the next event in the stream
23 // and the TFile pointer for a proper file
24 //----------------------------------------------------------------------
26 #include <Riostream.h>
29 #include "TObjString.h"
34 #include "AliStream.h"
38 //_______________________________________________________________________
39 AliStream::AliStream():
40 fLastEventSerialNr(0),
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
54 //_______________________________________________________________________
55 AliStream::AliStream(Option_t *option):
56 fLastEventSerialNr(-1),
58 fCurrentFileIndex(-1),
62 fFileNames(new TObjArray(1))
69 //_______________________________________________________________________
70 AliStream::AliStream(const AliStream& str):
72 fLastEventSerialNr(0),
86 //_______________________________________________________________________
87 void AliStream::Copy(AliStream& ) const
89 Fatal("Copy","Not implemented\n");
92 //_______________________________________________________________________
93 AliStream::~AliStream()
102 //_______________________________________________________________________
103 void AliStream::AddFile(const char *fileName)
105 // stores the name of the file
106 TObjString *name = new TObjString(fileName);
107 fFileNames->Add(name);
110 //_______________________________________________________________________
111 Bool_t AliStream::NextEventInStream(Int_t &serialNr)
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;
121 if (!OpenNextFile()) return kFALSE;
124 if (fLastEventSerialNr+1 >= fEvents) {
125 if (!OpenNextFile()) return kFALSE;
128 fLastEventSerialNr++;
129 // in some cases the serial number does not start from 0, find the
130 // number of the next event
132 sprintf(name, "TreeS%d", ++fLastEventNr);
133 while (!fCurrentFile->Get(name) && fLastEventNr < fEvents)
134 sprintf(name, "TreeS%d", ++fLastEventNr);
135 serialNr = fLastEventNr;
140 //_______________________________________________________________________
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 fCurrentFile->Close();
154 //_______________________________________________________________________
155 Bool_t AliStream::OpenNextFile()
158 // Open the next file in the series
160 if (++fCurrentFileIndex > fFileNames->GetLast()) {
161 cerr<<"No more files in the stream"<<endl;
165 const char * filename =
166 static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
168 // check if the file was already opened by some other code
169 TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
173 if (fCurrentFile->IsOpen()) {
174 fCurrentFile->Close();
178 fCurrentFile = TFile::Open(filename,fMode.Data());
180 // cannot open file specified on input. Do not skip it silently.
181 cerr<<"Cannot open file "<<filename<<endl;
184 // find nr of events in the given file
185 TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
187 Error("OpenNextFile", "input file does not contain TE");
190 fEvents = static_cast<Int_t>(te->GetEntries());
191 fLastEventSerialNr = -1;
196 //_______________________________________________________________________
197 Bool_t AliStream::ImportgAlice()
200 // Import AliRun object pointed from gAlice
202 if (fFileNames->GetLast() < 0) return kFALSE;
204 if (!OpenNextFile()) return kFALSE;
206 gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
207 if (!gAlice) return kFALSE;
211 //_______________________________________________________________________
212 TString AliStream::GetFileName(const Int_t order) const
214 // returns name of the order-th file
215 // returns empty string if such file does not exist
216 // first file in the input stream is 0
217 TString fileName("");
218 if (order > fFileNames->GetLast()) return fileName;
219 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
220 if (fileNameStored) fileName = fileNameStored->GetString();