]>
Commit | Line | Data |
---|---|---|
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 | ||
acd84897 | 16 | /* $Id$ */ |
77843484 | 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 | ||
b16a1b1e | 29 | #include <Riostream.h> |
77843484 | 30 | |
e2afb3b6 | 31 | #include "TFile.h" |
32 | #include "TObjString.h" | |
3466e07f | 33 | #include "TROOT.h" |
e2afb3b6 | 34 | #include "TTree.h" |
77843484 | 35 | |
36 | #include "AliRun.h" | |
e2afb3b6 | 37 | #include "AliStream.h" |
77843484 | 38 | |
39 | ClassImp(AliStream) | |
40 | ||
e2afb3b6 | 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) | |
77843484 | 50 | { |
e2afb3b6 | 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 | // | |
6873edea | 55 | } |
56 | ||
e2afb3b6 | 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)) | |
6873edea | 66 | { |
e2afb3b6 | 67 | // |
68 | // Default ctor | |
69 | // | |
77843484 | 70 | } |
71 | ||
e2afb3b6 | 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 | //_______________________________________________________________________ | |
77843484 | 96 | AliStream::~AliStream() |
97 | { | |
98 | // default dtor | |
f7ae2b32 | 99 | if (fFileNames) { |
100 | fFileNames->Delete(); | |
101 | delete fFileNames; | |
102 | } | |
77843484 | 103 | } |
104 | ||
e2afb3b6 | 105 | //_______________________________________________________________________ |
3466e07f | 106 | void AliStream::AddFile(const char *fileName) |
77843484 | 107 | { |
108 | // stores the name of the file | |
109 | TObjString *name = new TObjString(fileName); | |
110 | fFileNames->Add(name); | |
111 | } | |
112 | ||
e2afb3b6 | 113 | //_______________________________________________________________________ |
77843484 | 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 | } | |
033be4de | 130 | |
131 | fLastEventSerialNr++; | |
132 | // in some cases the serial number does not start from 0, find the | |
133 | // number of the next event | |
134 | char name[20]; | |
135 | sprintf(name, "TreeS%d", ++fLastEventNr); | |
136 | while (!fCurrentFile->Get(name) && fLastEventNr < fEvents) | |
137 | sprintf(name, "TreeS%d", ++fLastEventNr); | |
138 | serialNr = fLastEventNr; | |
139 | ||
77843484 | 140 | return kTRUE; |
141 | } | |
142 | ||
e2afb3b6 | 143 | //_______________________________________________________________________ |
6873edea | 144 | void AliStream::ChangeMode(Option_t* option) |
6873edea | 145 | { |
116cbefd | 146 | // set the mode to READ or UPDATE, reopen file with the new mode |
147 | // only change from UPDATE to READ have sense in the current scheme, | |
148 | // other changes are possible but not usefull | |
6873edea | 149 | fMode = option; |
150 | if (fCurrentFile) { | |
151 | fCurrentFile->Close(); | |
152 | fCurrentFileIndex--; | |
153 | OpenNextFile(); | |
154 | } | |
155 | } | |
156 | ||
e2afb3b6 | 157 | //_______________________________________________________________________ |
77843484 | 158 | Bool_t AliStream::OpenNextFile() |
159 | { | |
116cbefd | 160 | // |
161 | // Open the next file in the series | |
162 | // | |
fb0b99cb | 163 | if (++fCurrentFileIndex > fFileNames->GetLast()) { |
164 | cerr<<"No more files in the stream"<<endl; | |
165 | return kFALSE; | |
166 | } | |
167 | ||
3466e07f | 168 | const char * filename = |
169 | static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName(); | |
170 | ||
171 | // check if the file was already opened by some other code | |
e2afb3b6 | 172 | TFile *f = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(filename)); |
3466e07f | 173 | if (f) f->Close(); |
174 | ||
6873edea | 175 | if (fCurrentFile) { |
176 | if (fCurrentFile->IsOpen()) { | |
177 | fCurrentFile->Close(); | |
178 | } | |
179 | } | |
180 | ||
6873edea | 181 | fCurrentFile = TFile::Open(filename,fMode.Data()); |
77843484 | 182 | if (!fCurrentFile) { |
183 | // cannot open file specified on input. Do not skip it silently. | |
184 | cerr<<"Cannot open file "<<filename<<endl; | |
185 | return kFALSE; | |
186 | } | |
187 | // find nr of events in the given file | |
e2afb3b6 | 188 | TTree * te = dynamic_cast<TTree *>(fCurrentFile->Get("TE")); |
77843484 | 189 | if (!te) { |
190 | Error("OpenNextFile", "input file does not contain TE"); | |
191 | return kFALSE; | |
192 | } | |
193 | fEvents = static_cast<Int_t>(te->GetEntries()); | |
194 | fLastEventSerialNr = -1; | |
033be4de | 195 | fLastEventNr = -1; |
77843484 | 196 | return kTRUE; |
197 | } | |
198 | ||
e2afb3b6 | 199 | //_______________________________________________________________________ |
77843484 | 200 | Bool_t AliStream::ImportgAlice() |
201 | { | |
116cbefd | 202 | // |
203 | // Import AliRun object pointed from gAlice | |
204 | // | |
77843484 | 205 | if (fFileNames->GetLast() < 0) return kFALSE; |
206 | if (!fCurrentFile) { | |
207 | if (!OpenNextFile()) return kFALSE; | |
208 | } | |
e2afb3b6 | 209 | gAlice = dynamic_cast<AliRun*>(fCurrentFile->Get("gAlice")); |
77843484 | 210 | if (!gAlice) return kFALSE; |
211 | return kTRUE; | |
212 | } | |
e2afb3b6 | 213 | |
214 | //_______________________________________________________________________ | |
f7ae2b32 | 215 | TString AliStream::GetFileName(const Int_t order) const |
f7ae2b32 | 216 | { |
116cbefd | 217 | // returns name of the order-th file |
218 | // returns empty string if such file does not exist | |
219 | // first file in the input stream is 0 | |
f7ae2b32 | 220 | TString fileName(""); |
221 | if (order > fFileNames->GetLast()) return fileName; | |
222 | TObjString *fileNameStored = dynamic_cast<TObjString*>(fFileNames->At(order)); | |
223 | if (fileNameStored) fileName = fileNameStored->GetString(); | |
224 | return fileName; | |
225 | } | |
e2afb3b6 | 226 |