Removing warnings (icc)
[u/mrichter/AliRoot.git] / 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
18////////////////////////////////////////////////////////////////////////
19//
77843484 20// AliStream.cxx
88cb7938 21//
77843484 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
88cb7938 26//
27////////////////////////////////////////////////////////////////////////
77843484 28
b16a1b1e 29#include <Riostream.h>
77843484 30
e2afb3b6 31#include "TTree.h"
88cb7938 32#include "TROOT.h"
77843484 33
e2afb3b6 34#include "AliStream.h"
88cb7938 35#include "AliRun.h"
36
37#include "TObjString.h"
38#include "TArrayI.h"
39#include "TClonesArray.h"
40#include "TFile.h"
41#include "AliLoader.h"
77843484 42
43ClassImp(AliStream)
44
e2afb3b6 45AliStream::AliStream():
88cb7938 46 fLastEventSerialNr(-1),
e2afb3b6 47 fLastEventNr(0),
88cb7938 48 fCurrentFileIndex(-1),
e2afb3b6 49 fEvents(0),
50 fMode(0),
88cb7938 51 fFileNames(0x0),
52 fEventFolderName(0)
77843484 53{
88cb7938 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
6873edea 56}
e2afb3b6 57//_______________________________________________________________________
88cb7938 58
59AliStream::AliStream(const char* foldername,Option_t *option):
e2afb3b6 60 fLastEventSerialNr(-1),
61 fLastEventNr(0),
62 fCurrentFileIndex(-1),
63 fEvents(0),
64 fMode(option),
88cb7938 65 fFileNames(new TObjArray(1)),
66 fEventFolderName(foldername)
e2afb3b6 67{
88cb7938 68// ctor
e2afb3b6 69}
e2afb3b6 70//_______________________________________________________________________
e2afb3b6 71
77843484 72AliStream::~AliStream()
73{
74// default dtor
88cb7938 75 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
76 if (fFileNames) delete fFileNames;
77843484 77}
e2afb3b6 78//_______________________________________________________________________
88cb7938 79
3466e07f 80void AliStream::AddFile(const char *fileName)
77843484 81{
82// stores the name of the file
83 TObjString *name = new TObjString(fileName);
84 fFileNames->Add(name);
85}
e2afb3b6 86//_______________________________________________________________________
88cb7938 87
88Bool_t AliStream::NextEventInStream()
77843484 89{
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
93
94// no files given:
95 if (fFileNames->GetLast() < 0) return kFALSE;
77843484 96
88cb7938 97 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
98 if (currentloader == 0x0)
99 {
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);
106 }
033be4de 107
88cb7938 108 if (fLastEventSerialNr+1 >= fEvents)
109 {
110 if (!OpenNextFile()) return kFALSE;
111 }
8de97894 112 Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1);
88cb7938 113 currentloader->GetEvent(++fLastEventSerialNr);
77843484 114 return kTRUE;
115}
e2afb3b6 116//_______________________________________________________________________
88cb7938 117
6873edea 118void AliStream::ChangeMode(Option_t* option)
88cb7938 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
6873edea 122{
88cb7938 123
6873edea 124 fMode = option;
88cb7938 125 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
126 if (currentloader) {
127 delete currentloader;
6873edea 128 fCurrentFileIndex--;
129 OpenNextFile();
130 }
131}
e2afb3b6 132//_______________________________________________________________________
88cb7938 133
77843484 134Bool_t AliStream::OpenNextFile()
135{
fb0b99cb 136 if (++fCurrentFileIndex > fFileNames->GetLast()) {
137 cerr<<"No more files in the stream"<<endl;
138 return kFALSE;
139 }
140
88cb7938 141 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
3466e07f 142
143// check if the file was already opened by some other code
88cb7938 144 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
3466e07f 145 if (f) f->Close();
146
88cb7938 147 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
148
149 if (currentloader)
150 {
151 delete currentloader;
152 }
153
154 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
155
6873edea 156
88cb7938 157 if (currentloader == 0x0)
158 {
77843484 159// cannot open file specified on input. Do not skip it silently.
88cb7938 160 cerr<<"Cannot open session "<<filename<<endl;
77843484 161 return kFALSE;
88cb7938 162 }
163
77843484 164// find nr of events in the given file
88cb7938 165
166 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
167 {
168 Int_t res = currentloader->LoadHeader();
169 if (res)
170 {
171 Error("OpenNextFile","Problems with loading header");
172 return kFALSE;
173 }
174 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
175 }
176 else
177 {
178 //if it is new, create or recreate there is no chance to find header in file
179 fEvents = 0;
180 }
181
77843484 182 fLastEventSerialNr = -1;
183 return kTRUE;
184}
e2afb3b6 185//_______________________________________________________________________
88cb7938 186
77843484 187Bool_t AliStream::ImportgAlice()
188{
189 if (fFileNames->GetLast() < 0) return kFALSE;
88cb7938 190
191 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
192 if (!currentloader)
193 {
77843484 194 if (!OpenNextFile()) return kFALSE;
88cb7938 195 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
196 }
197 currentloader->LoadgAlice();
198 gAlice = currentloader->GetAliRun();
77843484 199 if (!gAlice) return kFALSE;
200 return kTRUE;
201}
e2afb3b6 202
203//_______________________________________________________________________
f7ae2b32 204TString AliStream::GetFileName(const Int_t order) const
88cb7938 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
f7ae2b32 208{
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();
213 return fileName;
214}
e2afb3b6 215