]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliStream.cxx
Coding Convention Violations
[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
134d2ab3 18#include <TROOT.h>
024a7e64 19#include <TFile.h>
20#include <TObjString.h>
21
22#include "AliLoader.h"
23#include "AliRun.h"
24#include "AliStream.h"
25
88cb7938 26////////////////////////////////////////////////////////////////////////
27//
77843484 28// AliStream.cxx
88cb7938 29//
77843484 30// - store file names associated with a given stream
31// - open and close files
32// - return serial event number of the next event in the stream
33// and the TFile pointer for a proper file
af7ba10c 34// Author: Jiri Chudoba (CERN), 2001
88cb7938 35//
36////////////////////////////////////////////////////////////////////////
77843484 37
77843484 38ClassImp(AliStream)
39
e2afb3b6 40AliStream::AliStream():
88cb7938 41 fLastEventSerialNr(-1),
e2afb3b6 42 fLastEventNr(0),
88cb7938 43 fCurrentFileIndex(-1),
e2afb3b6 44 fEvents(0),
45 fMode(0),
88cb7938 46 fFileNames(0x0),
47 fEventFolderName(0)
77843484 48{
024a7e64 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
6873edea 51}
e2afb3b6 52//_______________________________________________________________________
88cb7938 53
54AliStream::AliStream(const char* foldername,Option_t *option):
e2afb3b6 55 fLastEventSerialNr(-1),
56 fLastEventNr(0),
57 fCurrentFileIndex(-1),
58 fEvents(0),
59 fMode(option),
88cb7938 60 fFileNames(new TObjArray(1)),
61 fEventFolderName(foldername)
e2afb3b6 62{
88cb7938 63// ctor
e2afb3b6 64}
e2afb3b6 65//_______________________________________________________________________
e2afb3b6 66
024a7e64 67AliStream::AliStream(const AliStream &as):
68 TNamed(as),
69 fLastEventSerialNr(-1),
70 fLastEventNr(0),
71 fCurrentFileIndex(-1),
72 fEvents(0),
73 fMode(0),
74 fFileNames(0x0),
75 fEventFolderName(" ")
76{
77 //
78 // Copy ctor
79 //
80 as.Copy(*this);
81}
82//_______________________________________________________________________
83
77843484 84AliStream::~AliStream()
85{
86// default dtor
88cb7938 87 delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
88 if (fFileNames) delete fFileNames;
77843484 89}
e2afb3b6 90//_______________________________________________________________________
88cb7938 91
024a7e64 92void AliStream::Copy(TObject &) const
93{
94 //
95 // Copy function
96 //
97 Fatal("Copy","Not implemented!");
98}
99//_______________________________________________________________________
100
3466e07f 101void AliStream::AddFile(const char *fileName)
77843484 102{
103// stores the name of the file
104 TObjString *name = new TObjString(fileName);
105 fFileNames->Add(name);
106}
e2afb3b6 107//_______________________________________________________________________
88cb7938 108
109Bool_t AliStream::NextEventInStream()
77843484 110{
111// returns kFALSE if no more events
112// returns kTRUE and the serial nr of the next event
113// fCurrentFile points to the file containing offered event
114
115// no files given:
116 if (fFileNames->GetLast() < 0) return kFALSE;
77843484 117
88cb7938 118 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
119 if (currentloader == 0x0)
120 {
121 Info("NextEventInStream",
122 "Can not get RL from folder named %s. Attempting to open next file",
123 fEventFolderName.Data());
124 Int_t res = OpenNextFile();
125 if ( res == 0) return kFALSE;
126 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
127 }
033be4de 128
88cb7938 129 if (fLastEventSerialNr+1 >= fEvents)
130 {
131 if (!OpenNextFile()) return kFALSE;
132 }
8de97894 133 Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1);
88cb7938 134 currentloader->GetEvent(++fLastEventSerialNr);
77843484 135 return kTRUE;
136}
e2afb3b6 137//_______________________________________________________________________
88cb7938 138
6873edea 139void AliStream::ChangeMode(Option_t* option)
6873edea 140{
024a7e64 141 // set the mode to READ or UPDATE, reopen file with the new mode
142 // only change from UPDATE to READ have sense in the current scheme,
143 // other changes are possible but not usefull
88cb7938 144
6873edea 145 fMode = option;
88cb7938 146 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
147 if (currentloader) {
148 delete currentloader;
6873edea 149 fCurrentFileIndex--;
150 OpenNextFile();
151 }
152}
e2afb3b6 153//_______________________________________________________________________
88cb7938 154
77843484 155Bool_t AliStream::OpenNextFile()
156{
024a7e64 157 //
158 // Opens next file in the list
159 //
fb0b99cb 160 if (++fCurrentFileIndex > fFileNames->GetLast()) {
17b9c50c 161 Error("OpenNextFile", "No more files in the stream") ;
fb0b99cb 162 return kFALSE;
163 }
164
88cb7938 165 const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
3466e07f 166
167// check if the file was already opened by some other code
88cb7938 168 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
3466e07f 169 if (f) f->Close();
170
88cb7938 171 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
172
173 if (currentloader)
174 {
175 delete currentloader;
176 }
177
178 currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
179
6873edea 180
88cb7938 181 if (currentloader == 0x0)
182 {
77843484 183// cannot open file specified on input. Do not skip it silently.
17b9c50c 184 Error("OpenNextFile", "Cannot open session ");
77843484 185 return kFALSE;
88cb7938 186 }
187
77843484 188// find nr of events in the given file
88cb7938 189
190 if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode
191 {
192 Int_t res = currentloader->LoadHeader();
193 if (res)
194 {
195 Error("OpenNextFile","Problems with loading header");
196 return kFALSE;
197 }
198 fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries());
199 }
200 else
201 {
202 //if it is new, create or recreate there is no chance to find header in file
203 fEvents = 0;
204 }
205
77843484 206 fLastEventSerialNr = -1;
207 return kTRUE;
208}
e2afb3b6 209//_______________________________________________________________________
88cb7938 210
77843484 211Bool_t AliStream::ImportgAlice()
212{
024a7e64 213 //
214 // Imports gAlice object from file
215 //
77843484 216 if (fFileNames->GetLast() < 0) return kFALSE;
88cb7938 217
218 AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
219 if (!currentloader)
220 {
77843484 221 if (!OpenNextFile()) return kFALSE;
88cb7938 222 currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
223 }
224 currentloader->LoadgAlice();
225 gAlice = currentloader->GetAliRun();
77843484 226 if (!gAlice) return kFALSE;
227 return kTRUE;
228}
e2afb3b6 229
230//_______________________________________________________________________
d0f1ee3b 231TString AliStream::GetFileName(Int_t order) const
f7ae2b32 232{
024a7e64 233 // returns name of the order-th file
234 // returns empty string if such file does not exist
235 // first file in the input stream is 0
f7ae2b32 236 TString fileName("");
237 if (order > fFileNames->GetLast()) return fileName;
238 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
239 if (fileNameStored) fileName = fileNameStored->GetString();
240 return fileName;
241}
e2afb3b6 242