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 | |
803d1ab0 |
16 | /* $Id$ */ |
88cb7938 |
17 | |
024a7e64 |
18 | #include <TFile.h> |
19 | #include <TObjString.h> |
20 | |
21 | #include "AliLoader.h" |
22 | #include "AliRun.h" |
23 | #include "AliStream.h" |
24 | |
88cb7938 |
25 | //////////////////////////////////////////////////////////////////////// |
26 | // |
77843484 |
27 | // AliStream.cxx |
88cb7938 |
28 | // |
77843484 |
29 | // - store file names associated with a given stream |
30 | // - open and close files |
31 | // - return serial event number of the next event in the stream |
32 | // and the TFile pointer for a proper file |
88cb7938 |
33 | // |
34 | //////////////////////////////////////////////////////////////////////// |
77843484 |
35 | |
77843484 |
36 | ClassImp(AliStream) |
37 | |
e2afb3b6 |
38 | AliStream::AliStream(): |
88cb7938 |
39 | fLastEventSerialNr(-1), |
e2afb3b6 |
40 | fLastEventNr(0), |
88cb7938 |
41 | fCurrentFileIndex(-1), |
e2afb3b6 |
42 | fEvents(0), |
43 | fMode(0), |
88cb7938 |
44 | fFileNames(0x0), |
45 | fEventFolderName(0) |
77843484 |
46 | { |
024a7e64 |
47 | // root requires default ctor, where no new objects can be created |
48 | // do not use this ctor, it is supplied only for root needs |
6873edea |
49 | } |
e2afb3b6 |
50 | //_______________________________________________________________________ |
88cb7938 |
51 | |
52 | AliStream::AliStream(const char* foldername,Option_t *option): |
e2afb3b6 |
53 | fLastEventSerialNr(-1), |
54 | fLastEventNr(0), |
55 | fCurrentFileIndex(-1), |
56 | fEvents(0), |
57 | fMode(option), |
88cb7938 |
58 | fFileNames(new TObjArray(1)), |
59 | fEventFolderName(foldername) |
e2afb3b6 |
60 | { |
88cb7938 |
61 | // ctor |
e2afb3b6 |
62 | } |
e2afb3b6 |
63 | //_______________________________________________________________________ |
e2afb3b6 |
64 | |
024a7e64 |
65 | AliStream::AliStream(const AliStream &as): |
66 | TNamed(as), |
67 | fLastEventSerialNr(-1), |
68 | fLastEventNr(0), |
69 | fCurrentFileIndex(-1), |
70 | fEvents(0), |
71 | fMode(0), |
72 | fFileNames(0x0), |
73 | fEventFolderName(" ") |
74 | { |
75 | // |
76 | // Copy ctor |
77 | // |
78 | as.Copy(*this); |
79 | } |
80 | //_______________________________________________________________________ |
81 | |
77843484 |
82 | AliStream::~AliStream() |
83 | { |
84 | // default dtor |
88cb7938 |
85 | delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session |
86 | if (fFileNames) delete fFileNames; |
77843484 |
87 | } |
e2afb3b6 |
88 | //_______________________________________________________________________ |
88cb7938 |
89 | |
024a7e64 |
90 | void AliStream::Copy(TObject &) const |
91 | { |
92 | // |
93 | // Copy function |
94 | // |
95 | Fatal("Copy","Not implemented!"); |
96 | } |
97 | //_______________________________________________________________________ |
98 | |
3466e07f |
99 | void AliStream::AddFile(const char *fileName) |
77843484 |
100 | { |
101 | // stores the name of the file |
102 | TObjString *name = new TObjString(fileName); |
103 | fFileNames->Add(name); |
104 | } |
e2afb3b6 |
105 | //_______________________________________________________________________ |
88cb7938 |
106 | |
107 | Bool_t AliStream::NextEventInStream() |
77843484 |
108 | { |
109 | // returns kFALSE if no more events |
110 | // returns kTRUE and the serial nr of the next event |
111 | // fCurrentFile points to the file containing offered event |
112 | |
113 | // no files given: |
114 | if (fFileNames->GetLast() < 0) return kFALSE; |
77843484 |
115 | |
88cb7938 |
116 | AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
117 | if (currentloader == 0x0) |
118 | { |
119 | Info("NextEventInStream", |
120 | "Can not get RL from folder named %s. Attempting to open next file", |
121 | fEventFolderName.Data()); |
122 | Int_t res = OpenNextFile(); |
123 | if ( res == 0) return kFALSE; |
124 | currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
125 | } |
033be4de |
126 | |
88cb7938 |
127 | if (fLastEventSerialNr+1 >= fEvents) |
128 | { |
129 | if (!OpenNextFile()) return kFALSE; |
130 | } |
8de97894 |
131 | Info("NextEventInStream","Trying to get event %d",fLastEventSerialNr+1); |
88cb7938 |
132 | currentloader->GetEvent(++fLastEventSerialNr); |
77843484 |
133 | return kTRUE; |
134 | } |
e2afb3b6 |
135 | //_______________________________________________________________________ |
88cb7938 |
136 | |
6873edea |
137 | void AliStream::ChangeMode(Option_t* option) |
6873edea |
138 | { |
024a7e64 |
139 | // set the mode to READ or UPDATE, reopen file with the new mode |
140 | // only change from UPDATE to READ have sense in the current scheme, |
141 | // other changes are possible but not usefull |
88cb7938 |
142 | |
6873edea |
143 | fMode = option; |
88cb7938 |
144 | AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
145 | if (currentloader) { |
146 | delete currentloader; |
6873edea |
147 | fCurrentFileIndex--; |
148 | OpenNextFile(); |
149 | } |
150 | } |
e2afb3b6 |
151 | //_______________________________________________________________________ |
88cb7938 |
152 | |
77843484 |
153 | Bool_t AliStream::OpenNextFile() |
154 | { |
024a7e64 |
155 | // |
156 | // Opens next file in the list |
157 | // |
fb0b99cb |
158 | if (++fCurrentFileIndex > fFileNames->GetLast()) { |
17b9c50c |
159 | Error("OpenNextFile", "No more files in the stream") ; |
fb0b99cb |
160 | return kFALSE; |
161 | } |
162 | |
88cb7938 |
163 | const char* filename = static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName(); |
3466e07f |
164 | |
165 | // check if the file was already opened by some other code |
88cb7938 |
166 | TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename)); |
3466e07f |
167 | if (f) f->Close(); |
168 | |
88cb7938 |
169 | AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
170 | |
171 | if (currentloader) |
172 | { |
173 | delete currentloader; |
174 | } |
175 | |
176 | currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode); |
177 | |
6873edea |
178 | |
88cb7938 |
179 | if (currentloader == 0x0) |
180 | { |
77843484 |
181 | // cannot open file specified on input. Do not skip it silently. |
17b9c50c |
182 | Error("OpenNextFile", "Cannot open session "); |
77843484 |
183 | return kFALSE; |
88cb7938 |
184 | } |
185 | |
77843484 |
186 | // find nr of events in the given file |
88cb7938 |
187 | |
188 | if ( AliLoader::TestFileOption(fMode) )//tests if file is opened in read or update mode |
189 | { |
190 | Int_t res = currentloader->LoadHeader(); |
191 | if (res) |
192 | { |
193 | Error("OpenNextFile","Problems with loading header"); |
194 | return kFALSE; |
195 | } |
196 | fEvents = static_cast<Int_t>(currentloader->TreeE()->GetEntries()); |
197 | } |
198 | else |
199 | { |
200 | //if it is new, create or recreate there is no chance to find header in file |
201 | fEvents = 0; |
202 | } |
203 | |
77843484 |
204 | fLastEventSerialNr = -1; |
205 | return kTRUE; |
206 | } |
e2afb3b6 |
207 | //_______________________________________________________________________ |
88cb7938 |
208 | |
77843484 |
209 | Bool_t AliStream::ImportgAlice() |
210 | { |
024a7e64 |
211 | // |
212 | // Imports gAlice object from file |
213 | // |
77843484 |
214 | if (fFileNames->GetLast() < 0) return kFALSE; |
88cb7938 |
215 | |
216 | AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
217 | if (!currentloader) |
218 | { |
77843484 |
219 | if (!OpenNextFile()) return kFALSE; |
88cb7938 |
220 | currentloader = AliRunLoader::GetRunLoader(fEventFolderName); |
221 | } |
222 | currentloader->LoadgAlice(); |
223 | gAlice = currentloader->GetAliRun(); |
77843484 |
224 | if (!gAlice) return kFALSE; |
225 | return kTRUE; |
226 | } |
e2afb3b6 |
227 | |
228 | //_______________________________________________________________________ |
d0f1ee3b |
229 | TString AliStream::GetFileName(Int_t order) const |
f7ae2b32 |
230 | { |
024a7e64 |
231 | // returns name of the order-th file |
232 | // returns empty string if such file does not exist |
233 | // first file in the input stream is 0 |
f7ae2b32 |
234 | TString fileName(""); |
235 | if (order > fFileNames->GetLast()) return fileName; |
236 | TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order)); |
237 | if (fileNameStored) fileName = fileNameStored->GetString(); |
238 | return fileName; |
239 | } |
e2afb3b6 |
240 | |