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 ////////////////////////////////////////////////////////////////////////
22 // - store file names associated with a given stream
23 // - open and close files
24 // - return serial event number of the next event in the stream
25 // and the TFile pointer for a proper file
27 ////////////////////////////////////////////////////////////////////////
29 #include <Riostream.h>
34 #include "AliStream.h"
37 #include "TObjString.h"
39 #include "TClonesArray.h"
41 #include "AliLoader.h"
45 AliStream::AliStream():
46 fLastEventSerialNr(-1),
48 fCurrentFileIndex(-1),
54 // root requires default ctor, where no new objects can be created
55 // do not use this ctor, it is supplied only for root needs
57 //_______________________________________________________________________
59 AliStream::AliStream(const char* foldername,Option_t *option):
60 fLastEventSerialNr(-1),
62 fCurrentFileIndex(-1),
65 fFileNames(new TObjArray(1)),
66 fEventFolderName(foldername)
70 //_______________________________________________________________________
72 AliStream::~AliStream()
75 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
76 if (fFileNames) delete fFileNames;
78 //_______________________________________________________________________
80 void AliStream::AddFile(const char *fileName)
82 // stores the name of the file
83 TObjString *name = new TObjString(fileName);
84 fFileNames->Add(name);
86 //_______________________________________________________________________
88 Bool_t AliStream::NextEventInStream()
90 // returns kFALSE if no more events
91 // returns kTRUE and the serial nr of the next event
92 // fCurrentFile points to the file containing offered event
95 if (fFileNames->GetLast() < 0) return kFALSE;
97 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
98 if (currentloader == 0x0)
100 Info("NextEventInStream",
101 "Can not get RL from folder named %s. Attempting to open next file",
102 fEventFolderName.Data());
103 Int_t res = OpenNextFile();
104 if ( res == 0) return kFALSE;
105 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
108 if (fLastEventSerialNr+1 >= fEvents)
110 if (!OpenNextFile()) return kFALSE;
112 Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1);
113 currentloader->GetEvent(++fLastEventSerialNr);
116 //_______________________________________________________________________
118 void AliStream::ChangeMode(Option_t* option)
119 // set the mode to READ or UPDATE, reopen file with the new mode
120 // only change from UPDATE to READ have sense in the current scheme,
121 // other changes are possible but not usefull
125 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
127 delete currentloader;
132 //_______________________________________________________________________
134 Bool_t AliStream::OpenNextFile()
136 if (++fCurrentFileIndex > fFileNames->GetLast()) {
137 cerr<<"No more files in the stream"<<endl;
141 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
143 // check if the file was already opened by some other code
144 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
147 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
151 delete currentloader;
154 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
157 if (currentloader == 0x0)
159 // cannot open file specified on input. Do not skip it silently.
160 cerr<<"Cannot open session "<<filename<<endl;
164 // find nr of events in the given file
166 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
168 Int_t res = currentloader->LoadHeader();
171 Error("OpenNextFile","Problems with loading header");
174 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
178 //if it is new, create or recreate there is no chance to find header in file
182 fLastEventSerialNr = -1;
185 //_______________________________________________________________________
187 Bool_t AliStream::ImportgAlice()
189 if (fFileNames->GetLast() < 0) return kFALSE;
191 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
194 if (!OpenNextFile()) return kFALSE;
195 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
197 currentloader->LoadgAlice();
198 gAlice = currentloader->GetAliRun();
199 if (!gAlice) return kFALSE;
203 //_______________________________________________________________________
204 TString AliStream::GetFileName(Int_t order) const
205 // returns name of the order-th file
206 // returns empty string if such file does not exist
207 // first file in the input stream is 0
209 TString fileName("");
210 if (order > fFileNames->GetLast()) return fileName;
211 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
212 if (fileNameStored) fileName = fileNameStored->GetString();