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