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