Fixes for coverity.
[u/mrichter/AliRoot.git] / STEER / STEER / AliStream.cxx
CommitLineData
77843484 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
803d1ab0 16/* $Id$ */
88cb7938 17
134d2ab3 18#include <TROOT.h>
024a7e64 19#include <TFile.h>
20#include <TObjString.h>
c93255fe 21#include <TTree.h>
024a7e64 22
594d8990 23#include "AliLog.h"
024a7e64 24#include "AliLoader.h"
25#include "AliRun.h"
26#include "AliStream.h"
27
88cb7938 28////////////////////////////////////////////////////////////////////////
29//
77843484 30// AliStream.cxx
88cb7938 31//
77843484 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
af7ba10c 36// Author: Jiri Chudoba (CERN), 2001
88cb7938 37//
38////////////////////////////////////////////////////////////////////////
77843484 39
77843484 40ClassImp(AliStream)
41
e2afb3b6 42AliStream::AliStream():
88cb7938 43 fLastEventSerialNr(-1),
e2afb3b6 44 fLastEventNr(0),
88cb7938 45 fCurrentFileIndex(-1),
e2afb3b6 46 fEvents(0),
47 fMode(0),
88cb7938 48 fFileNames(0x0),
49 fEventFolderName(0)
77843484 50{
024a7e64 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
6873edea 53}
e2afb3b6 54//_______________________________________________________________________
88cb7938 55
56AliStream::AliStream(const char* foldername,Option_t *option):
e2afb3b6 57 fLastEventSerialNr(-1),
58 fLastEventNr(0),
59 fCurrentFileIndex(-1),
60 fEvents(0),
61 fMode(option),
88cb7938 62 fFileNames(new TObjArray(1)),
63 fEventFolderName(foldername)
e2afb3b6 64{
88cb7938 65// ctor
e2afb3b6 66}
e2afb3b6 67//_______________________________________________________________________
e2afb3b6 68
024a7e64 69AliStream::AliStream(const AliStream &as):
70 TNamed(as),
71 fLastEventSerialNr(-1),
72 fLastEventNr(0),
73 fCurrentFileIndex(-1),
74 fEvents(0),
75 fMode(0),
76 fFileNames(0x0),
77 fEventFolderName(" ")
78{
79 //
80 // Copy ctor
81 //
82 as.Copy(*this);
83}
84//_______________________________________________________________________
85
77843484 86AliStream::~AliStream()
87{
88// default dtor
88cb7938 89 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
90 if (fFileNames) delete fFileNames;
77843484 91}
e2afb3b6 92//_______________________________________________________________________
88cb7938 93
024a7e64 94void AliStream::Copy(TObject &) const
95{
96 //
97 // Copy function
98 //
594d8990 99 AliFatal("Not implemented!");
024a7e64 100}
101//_______________________________________________________________________
102
3466e07f 103void AliStream::AddFile(const char *fileName)
77843484 104{
105// stores the name of the file
106 TObjString *name = new TObjString(fileName);
107 fFileNames->Add(name);
108}
e2afb3b6 109//_______________________________________________________________________
88cb7938 110
111Bool_t AliStream::NextEventInStream()
77843484 112{
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
116
117// no files given:
118 if (fFileNames->GetLast() < 0) return kFALSE;
77843484 119
88cb7938 120 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
121 if (currentloader == 0x0)
122 {
594d8990 123 AliDebug(1, Form(
88cb7938 124 "Can not get RL from folder named %s. Attempting to open next file",
594d8990 125 fEventFolderName.Data()));
88cb7938 126 Int_t res = OpenNextFile();
127 if ( res == 0) return kFALSE;
128 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
129 }
033be4de 130
88cb7938 131 if (fLastEventSerialNr+1 >= fEvents)
132 {
133 if (!OpenNextFile()) return kFALSE;
134 }
594d8990 135 AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
88cb7938 136 currentloader->GetEvent(++fLastEventSerialNr);
77843484 137 return kTRUE;
138}
e2afb3b6 139//_______________________________________________________________________
88cb7938 140
6873edea 141void AliStream::ChangeMode(Option_t* option)
6873edea 142{
024a7e64 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
88cb7938 146
6873edea 147 fMode = option;
88cb7938 148 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
149 if (currentloader) {
150 delete currentloader;
6873edea 151 fCurrentFileIndex--;
152 OpenNextFile();
153 }
154}
e2afb3b6 155//_______________________________________________________________________
88cb7938 156
77843484 157Bool_t AliStream::OpenNextFile()
158{
024a7e64 159 //
160 // Opens next file in the list
161 //
fb0b99cb 162 if (++fCurrentFileIndex > fFileNames->GetLast()) {
594d8990 163 AliInfo("No more files in the stream") ;
fb0b99cb 164 return kFALSE;
165 }
166
88cb7938 167 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
3466e07f 168
169// check if the file was already opened by some other code
88cb7938 170 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
3466e07f 171 if (f) f->Close();
172
88cb7938 173 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
174
175 if (currentloader)
176 {
177 delete currentloader;
178 }
179
180 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
181
6873edea 182
88cb7938 183 if (currentloader == 0x0)
184 {
77843484 185// cannot open file specified on input. Do not skip it silently.
594d8990 186 AliError("Cannot open session ");
77843484 187 return kFALSE;
88cb7938 188 }
189
77843484 190// find nr of events in the given file
88cb7938 191
192 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
193 {
194 Int_t res = currentloader->LoadHeader();
195 if (res)
196 {
594d8990 197 AliError("Problems with loading header");
88cb7938 198 return kFALSE;
199 }
200 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
201 }
202 else
203 {
204 //if it is new, create or recreate there is no chance to find header in file
205 fEvents = 0;
206 }
207
77843484 208 fLastEventSerialNr = -1;
209 return kTRUE;
210}
e2afb3b6 211//_______________________________________________________________________
88cb7938 212
77843484 213Bool_t AliStream::ImportgAlice()
214{
024a7e64 215 //
216 // Imports gAlice object from file
217 //
77843484 218 if (fFileNames->GetLast() < 0) return kFALSE;
88cb7938 219
220 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
221 if (!currentloader)
222 {
77843484 223 if (!OpenNextFile()) return kFALSE;
88cb7938 224 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
225 }
226 currentloader->LoadgAlice();
227 gAlice = currentloader->GetAliRun();
77843484 228 if (!gAlice) return kFALSE;
229 return kTRUE;
230}
e2afb3b6 231
232//_______________________________________________________________________
d0f1ee3b 233TString AliStream::GetFileName(Int_t order) const
f7ae2b32 234{
024a7e64 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
f7ae2b32 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();
242 return fileName;
243}
e2afb3b6 244