]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliStream.cxx
Rename lib file
[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
16/*
17$Log$
b9d0a01d 18Revision 1.4.6.2 2002/07/24 10:08:13 alibrary
19Updating VirtualMC
20
21Revision 1.6 2002/07/16 13:48:39 jchudoba
22Add methods to get access to names of files used in merging. Correct memory leak in dtor (thanks to Yves Schutz.)
23
f7ae2b32 24Revision 1.5 2002/04/09 13:38:47 jchudoba
25Add const to the filename argument
26
3466e07f 27Revision 1.4 2001/12/03 07:10:13 jchudoba
28Default ctor cannot create new objects, create dummy default ctor which leaves object in not well defined state - to be used only by root for I/O
29
314558d7 30Revision 1.3 2001/10/15 17:31:56 jchudoba
31Bug correction
32
fb0b99cb 33Revision 1.2 2001/10/04 15:58:52 jchudoba
34Option to open the stream in READ or UPDATE mode
35
6873edea 36Revision 1.1 2001/09/19 06:20:50 jchudoba
37Class to manage input filenames, used by AliRunDigitizer
38
77843484 39*/
40
41////////////////////////////////////////////////////////////////////////
42//
43// AliStream.cxx
44//
45// - store file names associated with a given stream
46// - open and close files
47// - return serial event number of the next event in the stream
48// and the TFile pointer for a proper file
49//
50////////////////////////////////////////////////////////////////////////
51
52#include <iostream.h>
53
54#include "TTree.h"
3466e07f 55#include "TROOT.h"
77843484 56
57#include "AliStream.h"
58
59#include "AliRun.h"
60
61ClassImp(AliStream)
62
63AliStream::AliStream()
64{
314558d7 65// root requires default ctor, where no new objects can be created
66// do not use this ctor, it is supplied only for root needs
77843484 67 fCurrentFile = 0;
68 fEvents = 0;
314558d7 69 fFileNames = 0;
6873edea 70}
71
72////////////////////////////////////////////////////////////////////////
73AliStream::AliStream(Option_t *option)
74{
75// ctor
76 fLastEventSerialNr = -1;
77 fLastEventNr = 0;
78 fCurrentFileIndex = -1;
79 fCurrentFile = 0;
80 fEvents = 0;
81 fFileNames = new TObjArray(1);
82 fMode = option;
77843484 83}
84
85////////////////////////////////////////////////////////////////////////
86AliStream::~AliStream()
87{
88// default dtor
f7ae2b32 89 if (fFileNames) {
90 fFileNames->Delete();
91 delete fFileNames;
92 }
77843484 93}
94
95////////////////////////////////////////////////////////////////////////
3466e07f 96void AliStream::AddFile(const char *fileName)
77843484 97{
98// stores the name of the file
99 TObjString *name = new TObjString(fileName);
100 fFileNames->Add(name);
101}
102
103////////////////////////////////////////////////////////////////////////
104Bool_t AliStream::NextEventInStream(Int_t &serialNr)
105{
106// returns kFALSE if no more events
107// returns kTRUE and the serial nr of the next event
108// fCurrentFile points to the file containing offered event
109
110// no files given:
111 if (fFileNames->GetLast() < 0) return kFALSE;
112
113 if (!fCurrentFile) {
114 if (!OpenNextFile()) return kFALSE;
115 }
116
117 if (fLastEventSerialNr+1 >= fEvents) {
118 if (!OpenNextFile()) return kFALSE;
119 }
120 serialNr = ++fLastEventSerialNr;
121 return kTRUE;
122}
123
6873edea 124////////////////////////////////////////////////////////////////////////
125void AliStream::ChangeMode(Option_t* option)
126// set the mode to READ or UPDATE, reopen file with the new mode
127// only change from UPDATE to READ have sense in the current scheme,
128// other changes are possible but not usefull
129{
130 fMode = option;
131 if (fCurrentFile) {
132 fCurrentFile->Close();
133 fCurrentFileIndex--;
134 OpenNextFile();
135 }
136}
137
77843484 138////////////////////////////////////////////////////////////////////////
139Bool_t AliStream::OpenNextFile()
140{
fb0b99cb 141 if (++fCurrentFileIndex > fFileNames->GetLast()) {
142 cerr<<"No more files in the stream"<<endl;
143 return kFALSE;
144 }
145
3466e07f 146 const char * filename =
147 static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
148
149// check if the file was already opened by some other code
150 TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
151 if (f) f->Close();
152
6873edea 153 if (fCurrentFile) {
154 if (fCurrentFile->IsOpen()) {
155 fCurrentFile->Close();
156 }
157 }
158
6873edea 159 fCurrentFile = TFile::Open(filename,fMode.Data());
77843484 160 if (!fCurrentFile) {
161// cannot open file specified on input. Do not skip it silently.
162 cerr<<"Cannot open file "<<filename<<endl;
163 return kFALSE;
164 }
165// find nr of events in the given file
166 TTree * te = (TTree *) fCurrentFile->Get("TE") ;
167 if (!te) {
168 Error("OpenNextFile", "input file does not contain TE");
169 return kFALSE;
170 }
171 fEvents = static_cast<Int_t>(te->GetEntries());
172 fLastEventSerialNr = -1;
173 return kTRUE;
174}
175
176////////////////////////////////////////////////////////////////////////
177Bool_t AliStream::ImportgAlice()
178{
179 if (fFileNames->GetLast() < 0) return kFALSE;
180 if (!fCurrentFile) {
181 if (!OpenNextFile()) return kFALSE;
182 }
183 gAlice = (AliRun*)fCurrentFile->Get("gAlice");
184 if (!gAlice) return kFALSE;
185 return kTRUE;
186}
f7ae2b32 187////////////////////////////////////////////////////////////////////////
188TString AliStream::GetFileName(const Int_t order) const
189// returns name of the order-th file
190// returns empty string if such file does not exist
191// first file in the input stream is 0
192{
193 TString fileName("");
194 if (order > fFileNames->GetLast()) return fileName;
195 TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order));
196 if (fileNameStored) fileName = fileNameStored->GetString();
197 return fileName;
198}
199////////////////////////////////////////////////////////////////////////