]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStream.cxx
Add MCNSD flag to trigger mask
[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 #include <TTree.h>
22
23 #include "AliLog.h"
24 #include "AliLoader.h"
25 #include "AliRun.h"
26 #include "AliStream.h"
27
28 ////////////////////////////////////////////////////////////////////////
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 //
38 ////////////////////////////////////////////////////////////////////////
39
40 ClassImp(AliStream)
41
42 AliStream::AliStream():
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
69 AliStream::AliStream(const AliStream &as):
70   TNamed(as),
71   fLastEventSerialNr(-1),
72   fLastEventNr(0),
73   fCurrentFileIndex(-1),
74   fEvents(0),
75   fMode(0),
76   fFileNames(0x0),
77   fEventFolderName(" ")
78 {
79   //
80   // Copy ctor
81   //
82   as.Copy(*this);
83 }
84 //_______________________________________________________________________
85
86 AliStream::~AliStream()
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
111 Bool_t AliStream::NextEventInStream()
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   
120   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
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;
128     currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
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;
148   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
149   if (currentloader) {
150     delete currentloader;
151     fCurrentFileIndex--;
152     OpenNextFile();
153   }
154 }
155 //_______________________________________________________________________
156
157 Bool_t AliStream::OpenNextFile()
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
173   AliRunLoader* currentloader = AliRunLoader::GetRunLoader(fEventFolderName);
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
213 Bool_t AliStream::ImportgAlice()
214 {
215   //
216   // Imports gAlice object from file
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(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