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