Replacing Header with Id
[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
18////////////////////////////////////////////////////////////////////////
19//
20// AliStream.cxx
21//
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
26//
27////////////////////////////////////////////////////////////////////////
28
b16a1b1e 29#include <Riostream.h>
77843484 30
e2afb3b6 31#include "TFile.h"
32#include "TObjString.h"
3466e07f 33#include "TROOT.h"
e2afb3b6 34#include "TTree.h"
77843484 35
36#include "AliRun.h"
e2afb3b6 37#include "AliStream.h"
77843484 38
39ClassImp(AliStream)
40
e2afb3b6 41//_______________________________________________________________________
42AliStream::AliStream():
43 fLastEventSerialNr(0),
44 fLastEventNr(0),
45 fCurrentFileIndex(0),
46 fEvents(0),
47 fMode(0),
48 fCurrentFile(0),
49 fFileNames(0)
77843484 50{
e2afb3b6 51 //
52 // root requires default ctor, where no new objects can be created
53 // do not use this ctor, it is supplied only for root needs
54 //
6873edea 55}
56
e2afb3b6 57//_______________________________________________________________________
58AliStream::AliStream(Option_t *option):
59 fLastEventSerialNr(-1),
60 fLastEventNr(0),
61 fCurrentFileIndex(-1),
62 fEvents(0),
63 fMode(option),
64 fCurrentFile(0),
65 fFileNames(new TObjArray(1))
6873edea 66{
e2afb3b6 67 //
68 // Default ctor
69 //
77843484 70}
71
e2afb3b6 72//_______________________________________________________________________
73AliStream::AliStream(const AliStream& str):
74 TNamed(str),
75 fLastEventSerialNr(0),
76 fLastEventNr(0),
77 fCurrentFileIndex(0),
78 fEvents(0),
79 fMode(0),
80 fCurrentFile(0),
81 fFileNames(0)
82{
83 //
84 // Copy ctor
85 //
86 str.Copy(*this);
87}
88
89//_______________________________________________________________________
90void AliStream::Copy(AliStream& ) const
91{
92 Fatal("Copy","Not implemented\n");
93}
94
95//_______________________________________________________________________
77843484 96AliStream::~AliStream()
97{
98// default dtor
f7ae2b32 99 if (fFileNames) {
100 fFileNames->Delete();
101 delete fFileNames;
102 }
77843484 103}
104
e2afb3b6 105//_______________________________________________________________________
3466e07f 106void AliStream::AddFile(const char *fileName)
77843484 107{
108// stores the name of the file
109 TObjString *name = new TObjString(fileName);
110 fFileNames->Add(name);
111}
112
e2afb3b6 113//_______________________________________________________________________
77843484 114Bool_t AliStream::NextEventInStream(Int_t &serialNr)
115{
116// returns kFALSE if no more events
117// returns kTRUE and the serial nr of the next event
118// fCurrentFile points to the file containing offered event
119
120// no files given:
121 if (fFileNames->GetLast() < 0) return kFALSE;
122
123 if (!fCurrentFile) {
124 if (!OpenNextFile()) return kFALSE;
125 }
126
127 if (fLastEventSerialNr+1 >= fEvents) {
128 if (!OpenNextFile()) return kFALSE;
129 }
130 serialNr = ++fLastEventSerialNr;
131 return kTRUE;
132}
133
e2afb3b6 134//_______________________________________________________________________
6873edea 135void AliStream::ChangeMode(Option_t* option)
6873edea 136{
116cbefd 137 // set the mode to READ or UPDATE, reopen file with the new mode
138 // only change from UPDATE to READ have sense in the current scheme,
139 // other changes are possible but not usefull
6873edea 140 fMode = option;
141 if (fCurrentFile) {
142 fCurrentFile->Close();
143 fCurrentFileIndex--;
144 OpenNextFile();
145 }
146}
147
e2afb3b6 148//_______________________________________________________________________
77843484 149Bool_t AliStream::OpenNextFile()
150{
116cbefd 151 //
152 // Open the next file in the series
153 //
fb0b99cb 154 if (++fCurrentFileIndex > fFileNames->GetLast()) {
155 cerr<<"No more files in the stream"<<endl;
156 return kFALSE;
157 }
158
3466e07f 159 const char * filename =
160 static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
161
162// check if the file was already opened by some other code
e2afb3b6 163 TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
3466e07f 164 if (f) f->Close();
165
6873edea 166 if (fCurrentFile) {
167 if (fCurrentFile->IsOpen()) {
168 fCurrentFile->Close();
169 }
170 }
171
6873edea 172 fCurrentFile = TFile::Open(filename,fMode.Data());
77843484 173 if (!fCurrentFile) {
174// cannot open file specified on input. Do not skip it silently.
175 cerr<<"Cannot open file "<<filename<<endl;
176 return kFALSE;
177 }
178// find nr of events in the given file
e2afb3b6 179 TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
77843484 180 if (!te) {
181 Error("OpenNextFile", "input file does not contain TE");
182 return kFALSE;
183 }
184 fEvents = static_cast<Int_t>(te->GetEntries());
185 fLastEventSerialNr = -1;
186 return kTRUE;
187}
188
e2afb3b6 189//_______________________________________________________________________
77843484 190Bool_t AliStream::ImportgAlice()
191{
116cbefd 192 //
193 // Import AliRun object pointed from gAlice
194 //
77843484 195 if (fFileNames->GetLast() < 0) return kFALSE;
196 if (!fCurrentFile) {
197 if (!OpenNextFile()) return kFALSE;
198 }
e2afb3b6 199 gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
77843484 200 if (!gAlice) return kFALSE;
201 return kTRUE;
202}
e2afb3b6 203
204//_______________________________________________________________________
f7ae2b32 205TString AliStream::GetFileName(const Int_t order) const
f7ae2b32 206{
116cbefd 207 // returns name of the order-th file
208 // returns empty string if such file does not exist
209 // first file in the input stream is 0
f7ae2b32 210 TString fileName("");
211 if (order > fFileNames->GetLast()) return fileName;
212 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
213 if (fileNameStored) fileName = fileNameStored->GetString();
214 return fileName;
215}
e2afb3b6 216