]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStream.cxx
Transition to NewIO
[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 /*
17 $Log$
18 Revision 1.4.4.4  2002/12/11 10:00:34  hristov
19 Merging with v3-09-04 (P.Skowronski)
20
21 Revision 1.4.4.3  2002/11/22 14:19:50  hristov
22 Merging NewIO-01 with v3-09-04 (part one) (P.Skowronski)
23
24 Revision 1.4.4.2  2002/06/18 10:18:32  hristov
25 Important update (P.Skowronski)
26
27 Revision 1.4.4.1  2002/05/31 09:37:59  hristov
28 First set of changes done by Piotr
29
30 Revision 1.5  2002/04/09 13:38:47  jchudoba
31 Add const to the filename argument
32
33 Revision 1.4  2001/12/03 07:10:13  jchudoba
34 Default 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
36 Revision 1.3  2001/10/15 17:31:56  jchudoba
37 Bug correction
38
39 Revision 1.2  2001/10/04 15:58:52  jchudoba
40 Option to open the stream in READ or UPDATE mode
41
42 Revision 1.1  2001/09/19 06:20:50  jchudoba
43 Class to manage input filenames, used by AliRunDigitizer
44
45 */
46
47 ////////////////////////////////////////////////////////////////////////
48 //
49 // AliStream.cxx
50 //
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
55 //
56 ////////////////////////////////////////////////////////////////////////
57
58 #include <Riostream.h>
59
60 #include "TTree.h"
61 #include "TROOT.h"
62
63 #include "AliStream.h"
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"
71
72 ClassImp(AliStream)
73
74 AliStream::AliStream():
75   fLastEventSerialNr(-1),
76   fLastEventNr(0),
77   fCurrentFileIndex(-1),
78   fEvents(0),
79   fMode(0),
80   fFileNames(0x0),
81   fEventFolderName(0)
82 {
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
85 }
86 //_______________________________________________________________________
87
88 AliStream::AliStream(const char* foldername,Option_t *option):
89   fLastEventSerialNr(-1),
90   fLastEventNr(0),
91   fCurrentFileIndex(-1),
92   fEvents(0),
93   fMode(option),
94   fFileNames(new TObjArray(1)),
95   fEventFolderName(foldername)
96 {
97 // ctor
98 }
99 //_______________________________________________________________________
100
101 AliStream::~AliStream()
102 {
103 // default dtor
104   delete AliRunLoader::GetRunLoader(fEventFolderName); //clear the eventuall session
105   if (fFileNames) delete fFileNames;
106 }
107 //_______________________________________________________________________
108
109 void AliStream::AddFile(const char *fileName)
110 {
111 // stores the name of the file
112   TObjString *name = new TObjString(fileName);
113   fFileNames->Add(name);
114 }
115 //_______________________________________________________________________
116
117 Bool_t AliStream::NextEventInStream()
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;
125   
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    }
136   
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);
143   return kTRUE;
144 }
145 //_______________________________________________________________________
146
147 void AliStream::ChangeMode(Option_t* option)
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
151 {
152
153   fMode = option;
154   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
155   if (currentloader) {
156     delete currentloader;
157     fCurrentFileIndex--;
158     OpenNextFile();
159   }
160 }
161 //_______________________________________________________________________
162
163 Bool_t AliStream::OpenNextFile()
164 {
165   if (++fCurrentFileIndex > fFileNames->GetLast()) {
166     cerr<<"No more files in the stream"<<endl;
167     return kFALSE;
168   }
169
170   const char* filename =   static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
171
172 // check if the file was already opened by some other code
173   TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
174   if (f) f->Close();
175
176   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
177   
178   if (currentloader) 
179    {
180      delete currentloader;
181    }
182   
183   currentloader = AliRunLoader::Open(filename,fEventFolderName,fMode);
184   
185
186   if (currentloader == 0x0) 
187    {
188 // cannot open file specified on input. Do not skip it silently.
189     cerr<<"Cannot open session "<<filename<<endl;
190     return kFALSE;
191    }
192    
193 // find nr of events in the given file  
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    
211   fLastEventSerialNr = -1;
212   return kTRUE;
213 }
214 //_______________________________________________________________________
215
216 Bool_t AliStream::ImportgAlice()
217 {
218   if (fFileNames->GetLast() < 0) return kFALSE;
219   
220   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
221   if (!currentloader) 
222    {
223     if (!OpenNextFile()) return kFALSE;
224     currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
225    }
226   currentloader->LoadgAlice();
227   gAlice = currentloader->GetAliRun();
228   if (!gAlice)  return kFALSE;
229   return kTRUE;
230 }
231
232 //_______________________________________________________________________
233 TString AliStream::GetFileName(const Int_t order) const
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 {
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