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