]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - STEER/AliStream.cxx
Adaption to new fluka common blocks (E. Futo)
[u/mrichter/AliRoot.git] / STEER / AliStream.cxx
... / ...
CommitLineData
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
16/* $Id$ */
17
18//----------------------------------------------------------------------
19// AliStream.cxx
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
24//----------------------------------------------------------------------
25
26#include <Riostream.h>
27
28#include "TFile.h"
29#include "TObjString.h"
30#include "TROOT.h"
31#include "TTree.h"
32
33#include "AliRun.h"
34#include "AliStream.h"
35
36ClassImp(AliStream)
37
38//_______________________________________________________________________
39AliStream::AliStream():
40 fLastEventSerialNr(0),
41 fLastEventNr(0),
42 fCurrentFileIndex(0),
43 fEvents(0),
44 fMode(0),
45 fCurrentFile(0),
46 fFileNames(0)
47{
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 //
52}
53
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))
63{
64 //
65 // Default ctor
66 //
67}
68
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//_______________________________________________________________________
93AliStream::~AliStream()
94{
95// default dtor
96 if (fFileNames) {
97 fFileNames->Delete();
98 delete fFileNames;
99 }
100}
101
102//_______________________________________________________________________
103void AliStream::AddFile(const char *fileName)
104{
105// stores the name of the file
106 TObjString *name = new TObjString(fileName);
107 fFileNames->Add(name);
108}
109
110//_______________________________________________________________________
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 }
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
137 return kTRUE;
138}
139
140//_______________________________________________________________________
141void AliStream::ChangeMode(Option_t* option)
142{
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
146 fMode = option;
147 if (fCurrentFile) {
148 fCurrentFile->Close();
149 fCurrentFileIndex--;
150 OpenNextFile();
151 }
152}
153
154//_______________________________________________________________________
155Bool_t AliStream::OpenNextFile()
156{
157 //
158 // Open the next file in the series
159 //
160 if (++fCurrentFileIndex > fFileNames->GetLast()) {
161 cerr<<"No more files in the stream"<<endl;
162 return kFALSE;
163 }
164
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
169 TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
170 if (f) f->Close();
171
172 if (fCurrentFile) {
173 if (fCurrentFile->IsOpen()) {
174 fCurrentFile->Close();
175 }
176 }
177
178 fCurrentFile = TFile::Open(filename,fMode.Data());
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
185 TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
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;
192 fLastEventNr = -1;
193 return kTRUE;
194}
195
196//_______________________________________________________________________
197Bool_t AliStream::ImportgAlice()
198{
199 //
200 // Import AliRun object pointed from gAlice
201 //
202 if (fFileNames->GetLast() < 0) return kFALSE;
203 if (!fCurrentFile) {
204 if (!OpenNextFile()) return kFALSE;
205 }
206 gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
207 if (!gAlice) return kFALSE;
208 return kTRUE;
209}
210
211//_______________________________________________________________________
212TString AliStream::GetFileName(const Int_t order) const
213{
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
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}
223