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