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