]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStream.cxx
Execution time printout via AliInfo
[u/mrichter/AliRoot.git] / STEER / AliStream.cxx
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
22 #include "AliLog.h"
23 #include "AliLoader.h"
24 #include "AliRun.h"
25 #include "AliStream.h"
26
27 ////////////////////////////////////////////////////////////////////////
28 //
29 // AliStream.cxx
30 //
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
35 //  Author: Jiri Chudoba (CERN), 2001
36 //
37 ////////////////////////////////////////////////////////////////////////
38
39 ClassImp(AliStream)
40
41 AliStream::AliStream():
42   fLastEventSerialNr(-1),
43   fLastEventNr(0),
44   fCurrentFileIndex(-1),
45   fEvents(0),
46   fMode(0),
47   fFileNames(0x0),
48   fEventFolderName(0)
49 {
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
52 }
53 //_______________________________________________________________________
54
55 AliStream::AliStream(const char* foldername,Option_t *option):
56   fLastEventSerialNr(-1),
57   fLastEventNr(0),
58   fCurrentFileIndex(-1),
59   fEvents(0),
60   fMode(option),
61   fFileNames(new TObjArray(1)),
62   fEventFolderName(foldername)
63 {
64 // ctor
65 }
66 //_______________________________________________________________________
67
68 AliStream::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
85 AliStream::~AliStream()
86 {
87 // default dtor
88   delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
89   if (fFileNames) delete fFileNames;
90 }
91 //_______________________________________________________________________
92
93 void AliStream::Copy(TObject &) const
94 {
95   //
96   // Copy function
97   //
98   AliFatal("Not implemented!");
99 }
100 //_______________________________________________________________________
101
102 void AliStream::AddFile(const char *fileName)
103 {
104 // stores the name of the file
105   TObjString *name = new TObjString(fileName);
106   fFileNames->Add(name);
107 }
108 //_______________________________________________________________________
109
110 Bool_t AliStream::NextEventInStream()
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;
118   
119   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
120   if (currentloader == 0x0) 
121    {
122     AliDebug(1, Form(
123          "Can not get RL from folder named %s. Attempting to open next file",
124          fEventFolderName.Data()));
125     Int_t res = OpenNextFile();
126     if ( res == 0) return kFALSE;
127     currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
128    }
129   
130   if (fLastEventSerialNr+1 >= fEvents) 
131    {
132     if (!OpenNextFile()) return kFALSE;
133    }
134   AliDebug(1, Form("Trying to get event %d",fLastEventSerialNr+1));
135   currentloader->GetEvent(++fLastEventSerialNr);
136   return kTRUE;
137 }
138 //_______________________________________________________________________
139
140 void AliStream::ChangeMode(Option_t* option)
141 {
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
145
146   fMode = option;
147   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
148   if (currentloader) {
149     delete currentloader;
150     fCurrentFileIndex--;
151     OpenNextFile();
152   }
153 }
154 //_______________________________________________________________________
155
156 Bool_t AliStream::OpenNextFile()
157 {
158   //
159   // Opens next file in the list
160   //
161   if (++fCurrentFileIndex > fFileNames->GetLast()) {
162     AliInfo("No more files in the stream") ;
163     return kFALSE;
164   }
165
166   const char* filename =   static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
167
168 // check if the file was already opened by some other code
169   TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
170   if (f) f->Close();
171
172   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
173   
174   if (currentloader) 
175    {
176      delete currentloader;
177    }
178   
179   currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
180   
181
182   if (currentloader == 0x0) 
183    {
184 // cannot open file specified on input. Do not skip it silently.
185     AliError("Cannot open session ");
186     return kFALSE;
187    }
188    
189 // find nr of events in the given file  
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      {
196        AliError("Problems with loading header");
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    
207   fLastEventSerialNr = -1;
208   return kTRUE;
209 }
210 //_______________________________________________________________________
211
212 Bool_t AliStream::ImportgAlice()
213 {
214   //
215   // Imports gAlice object from file
216   //
217   if (fFileNames->GetLast() < 0) return kFALSE;
218   
219   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
220   if (!currentloader) 
221    {
222     if (!OpenNextFile()) return kFALSE;
223     currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
224    }
225   currentloader->LoadgAlice();
226   gAlice = currentloader->GetAliRun();
227   if (!gAlice)  return kFALSE;
228   return kTRUE;
229 }
230
231 //_______________________________________________________________________
232 TString AliStream::GetFileName(Int_t order) const
233 {
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
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 }
243