Adaption to new fluka common blocks (E. Futo)
[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
acd84897 16/* $Id$ */
77843484 17
1c0b669a 18//----------------------------------------------------------------------
77843484 19// AliStream.cxx
77843484 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
1c0b669a 24//----------------------------------------------------------------------
77843484 25
b16a1b1e 26#include <Riostream.h>
77843484 27
e2afb3b6 28#include "TFile.h"
29#include "TObjString.h"
3466e07f 30#include "TROOT.h"
e2afb3b6 31#include "TTree.h"
77843484 32
33#include "AliRun.h"
e2afb3b6 34#include "AliStream.h"
77843484 35
36ClassImp(AliStream)
37
e2afb3b6 38//_______________________________________________________________________
39AliStream::AliStream():
40 fLastEventSerialNr(0),
41 fLastEventNr(0),
42 fCurrentFileIndex(0),
43 fEvents(0),
44 fMode(0),
45 fCurrentFile(0),
46 fFileNames(0)
77843484 47{
e2afb3b6 48 //
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
51 //
6873edea 52}
53
e2afb3b6 54//_______________________________________________________________________
55AliStream::AliStream(Option_t *option):
56 fLastEventSerialNr(-1),
57 fLastEventNr(0),
58 fCurrentFileIndex(-1),
59 fEvents(0),
60 fMode(option),
61 fCurrentFile(0),
62 fFileNames(new TObjArray(1))
6873edea 63{
e2afb3b6 64 //
65 // Default ctor
66 //
77843484 67}
68
e2afb3b6 69//_______________________________________________________________________
70AliStream::AliStream(const AliStream& str):
71 TNamed(str),
72 fLastEventSerialNr(0),
73 fLastEventNr(0),
74 fCurrentFileIndex(0),
75 fEvents(0),
76 fMode(0),
77 fCurrentFile(0),
78 fFileNames(0)
79{
80 //
81 // Copy ctor
82 //
83 str.Copy(*this);
84}
85
86//_______________________________________________________________________
87void AliStream::Copy(AliStream& ) const
88{
89 Fatal("Copy","Not implemented\n");
90}
91
92//_______________________________________________________________________
77843484 93AliStream::~AliStream()
94{
95// default dtor
f7ae2b32 96 if (fFileNames) {
97 fFileNames->Delete();
98 delete fFileNames;
99 }
77843484 100}
101
e2afb3b6 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}
109
e2afb3b6 110//_______________________________________________________________________
77843484 111Bool_t AliStream::NextEventInStream(Int_t &serialNr)
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;
119
120 if (!fCurrentFile) {
121 if (!OpenNextFile()) return kFALSE;
122 }
123
124 if (fLastEventSerialNr+1 >= fEvents) {
125 if (!OpenNextFile()) return kFALSE;
126 }
033be4de 127
128 fLastEventSerialNr++;
129// in some cases the serial number does not start from 0, find the
130// number of the next event
131 char name[20];
132 sprintf(name, "TreeS%d", ++fLastEventNr);
133 while (!fCurrentFile->Get(name) && fLastEventNr < fEvents)
134 sprintf(name, "TreeS%d", ++fLastEventNr);
135 serialNr = fLastEventNr;
136
77843484 137 return kTRUE;
138}
139
e2afb3b6 140//_______________________________________________________________________
6873edea 141void AliStream::ChangeMode(Option_t* option)
6873edea 142{
116cbefd 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
6873edea 146 fMode = option;
147 if (fCurrentFile) {
148 fCurrentFile->Close();
149 fCurrentFileIndex--;
150 OpenNextFile();
151 }
152}
153
e2afb3b6 154//_______________________________________________________________________
77843484 155Bool_t AliStream::OpenNextFile()
156{
116cbefd 157 //
158 // Open the next file in the series
159 //
fb0b99cb 160 if (++fCurrentFileIndex > fFileNames->GetLast()) {
161 cerr<<"No more files in the stream"<<endl;
162 return kFALSE;
163 }
164
3466e07f 165 const char * filename =
166 static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
167
168// check if the file was already opened by some other code
e2afb3b6 169 TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
3466e07f 170 if (f) f->Close();
171
6873edea 172 if (fCurrentFile) {
173 if (fCurrentFile->IsOpen()) {
174 fCurrentFile->Close();
175 }
176 }
177
6873edea 178 fCurrentFile = TFile::Open(filename,fMode.Data());
77843484 179 if (!fCurrentFile) {
180// cannot open file specified on input. Do not skip it silently.
181 cerr<<"Cannot open file "<<filename<<endl;
182 return kFALSE;
183 }
184// find nr of events in the given file
e2afb3b6 185 TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
77843484 186 if (!te) {
187 Error("OpenNextFile", "input file does not contain TE");
188 return kFALSE;
189 }
190 fEvents = static_cast<Int_t>(te->GetEntries());
191 fLastEventSerialNr = -1;
033be4de 192 fLastEventNr = -1;
77843484 193 return kTRUE;
194}
195
e2afb3b6 196//_______________________________________________________________________
77843484 197Bool_t AliStream::ImportgAlice()
198{
116cbefd 199 //
200 // Import AliRun object pointed from gAlice
201 //
77843484 202 if (fFileNames->GetLast() < 0) return kFALSE;
203 if (!fCurrentFile) {
204 if (!OpenNextFile()) return kFALSE;
205 }
e2afb3b6 206 gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
77843484 207 if (!gAlice) return kFALSE;
208 return kTRUE;
209}
e2afb3b6 210
211//_______________________________________________________________________
f7ae2b32 212TString AliStream::GetFileName(const Int_t order) const
f7ae2b32 213{
116cbefd 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
f7ae2b32 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();
221 return fileName;
222}
e2afb3b6 223