]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStream.cxx
Replacing Header with Id
[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 /* $Id$ */
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
29 #include <Riostream.h>
30
31 #include "TFile.h"
32 #include "TObjString.h"
33 #include "TROOT.h"
34 #include "TTree.h"
35
36 #include "AliRun.h"
37 #include "AliStream.h"
38
39 ClassImp(AliStream)
40
41 //_______________________________________________________________________
42 AliStream::AliStream():
43   fLastEventSerialNr(0),
44   fLastEventNr(0),
45   fCurrentFileIndex(0),
46   fEvents(0),
47   fMode(0),
48   fCurrentFile(0),
49   fFileNames(0)
50 {
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   //
55 }
56
57 //_______________________________________________________________________
58 AliStream::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))
66 {
67   //
68   // Default ctor
69   //
70 }
71
72 //_______________________________________________________________________
73 AliStream::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 //_______________________________________________________________________
90 void AliStream::Copy(AliStream& ) const
91 {
92   Fatal("Copy","Not implemented\n");
93 }
94
95 //_______________________________________________________________________
96 AliStream::~AliStream()
97 {
98 // default dtor
99   if (fFileNames) {
100     fFileNames->Delete();
101     delete fFileNames;
102   }
103 }
104
105 //_______________________________________________________________________
106 void AliStream::AddFile(const char *fileName)
107 {
108 // stores the name of the file
109   TObjString *name = new TObjString(fileName);
110   fFileNames->Add(name);
111 }
112
113 //_______________________________________________________________________
114 Bool_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
134 //_______________________________________________________________________
135 void AliStream::ChangeMode(Option_t* option)
136 {
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
140   fMode = option;
141   if (fCurrentFile) {
142     fCurrentFile->Close();
143     fCurrentFileIndex--;
144     OpenNextFile();
145   }
146 }
147
148 //_______________________________________________________________________
149 Bool_t AliStream::OpenNextFile()
150 {
151   //
152   // Open the next file in the series
153   //
154   if (++fCurrentFileIndex > fFileNames->GetLast()) {
155     cerr<<"No more files in the stream"<<endl;
156     return kFALSE;
157   }
158
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
163   TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename));
164   if (f) f->Close();
165
166   if (fCurrentFile) {
167     if (fCurrentFile->IsOpen()) {
168       fCurrentFile->Close();
169     }
170   }
171
172   fCurrentFile = TFile::Open(filename,fMode.Data());
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  
179   TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE"));
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
189 //_______________________________________________________________________
190 Bool_t AliStream::ImportgAlice()
191 {
192   //
193   // Import AliRun object pointed from gAlice
194   //
195   if (fFileNames->GetLast() < 0) return kFALSE;
196   if (!fCurrentFile) {
197     if (!OpenNextFile()) return kFALSE;
198   }
199   gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice"));
200   if (!gAlice)  return kFALSE;
201   return kTRUE;
202 }
203
204 //_______________________________________________________________________
205 TString AliStream::GetFileName(const Int_t order) const
206 {
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
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 }
216