]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStream.cxx
8fa30c95e502520c71c70f1e262f88c70f8accf0
[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  2001/12/03 07:10:13  jchudoba
19 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
20
21 Revision 1.3  2001/10/15 17:31:56  jchudoba
22 Bug correction
23
24 Revision 1.2  2001/10/04 15:58:52  jchudoba
25 Option to open the stream in READ or UPDATE mode
26
27 Revision 1.1  2001/09/19 06:20:50  jchudoba
28 Class to manage input filenames, used by AliRunDigitizer
29
30 */
31
32 ////////////////////////////////////////////////////////////////////////
33 //
34 // AliStream.cxx
35 //
36 // - store file names associated with a given stream
37 // - open and close files
38 // - return serial event number of the next event in the stream
39 // and the TFile pointer for a proper file
40 //
41 ////////////////////////////////////////////////////////////////////////
42
43 #include <iostream.h>
44
45 #include "TTree.h"
46 #include "TROOT.h"
47
48 #include "AliStream.h"
49
50 #include "AliRun.h"
51
52 ClassImp(AliStream)
53
54 AliStream::AliStream()
55 {
56 // root requires default ctor, where no new objects can be created
57 // do not use this ctor, it is supplied only for root needs
58   fCurrentFile = 0;
59   fEvents = 0;
60   fFileNames = 0;
61 }
62
63 ////////////////////////////////////////////////////////////////////////
64 AliStream::AliStream(Option_t *option)
65 {
66 // ctor
67   fLastEventSerialNr = -1;
68   fLastEventNr = 0;
69   fCurrentFileIndex = -1;
70   fCurrentFile = 0;
71   fEvents = 0;
72   fFileNames = new TObjArray(1);
73   fMode = option;
74 }
75
76 ////////////////////////////////////////////////////////////////////////
77 AliStream::~AliStream()
78 {
79 // default dtor
80   if (fFileNames) delete fFileNames;
81 }
82
83 ////////////////////////////////////////////////////////////////////////
84 void AliStream::AddFile(const char *fileName)
85 {
86 // stores the name of the file
87   TObjString *name = new TObjString(fileName);
88   fFileNames->Add(name);
89 }
90
91 ////////////////////////////////////////////////////////////////////////
92 Bool_t AliStream::NextEventInStream(Int_t &serialNr)
93 {
94 // returns kFALSE if no more events
95 // returns kTRUE and the serial nr of the next event
96 // fCurrentFile points to the file containing offered event
97
98 // no files given:
99   if (fFileNames->GetLast() < 0) return kFALSE;
100
101   if (!fCurrentFile) {
102     if (!OpenNextFile()) return kFALSE;
103   }
104   
105   if (fLastEventSerialNr+1 >= fEvents) {
106     if (!OpenNextFile()) return kFALSE;
107   }
108   serialNr = ++fLastEventSerialNr;
109   return kTRUE;
110 }
111
112 ////////////////////////////////////////////////////////////////////////
113 void AliStream::ChangeMode(Option_t* option)
114 // set the mode to READ or UPDATE, reopen file with the new mode
115 // only change from UPDATE to READ have sense in the current scheme,
116 // other changes are possible but not usefull
117 {
118   fMode = option;
119   if (fCurrentFile) {
120     fCurrentFile->Close();
121     fCurrentFileIndex--;
122     OpenNextFile();
123   }
124 }
125
126 ////////////////////////////////////////////////////////////////////////
127 Bool_t AliStream::OpenNextFile()
128 {
129   if (++fCurrentFileIndex > fFileNames->GetLast()) {
130     cerr<<"No more files in the stream"<<endl;
131     return kFALSE;
132   }
133
134   const char * filename = 
135     static_cast<TObjString*>(fFileNames->At(fCurrentFileIndex))->GetName();
136
137 // check if the file was already opened by some other code
138   TFile *f = (TFile *)(gROOT->GetListOfFiles()->FindObject(filename));
139   if (f) f->Close();
140
141   if (fCurrentFile) {
142     if (fCurrentFile->IsOpen()) {
143       fCurrentFile->Close();
144     }
145   }
146
147   fCurrentFile = TFile::Open(filename,fMode.Data());
148   if (!fCurrentFile) {
149 // cannot open file specified on input. Do not skip it silently.
150     cerr<<"Cannot open file "<<filename<<endl;
151     return kFALSE;
152   }
153 // find nr of events in the given file  
154   TTree * te = (TTree *) fCurrentFile->Get("TE") ;
155   if (!te) {
156     Error("OpenNextFile", "input file does not contain TE");
157     return kFALSE;
158   }
159   fEvents = static_cast<Int_t>(te->GetEntries());
160   fLastEventSerialNr = -1;
161   return kTRUE;
162 }
163
164 ////////////////////////////////////////////////////////////////////////
165 Bool_t AliStream::ImportgAlice()
166 {
167   if (fFileNames->GetLast() < 0) return kFALSE;
168   if (!fCurrentFile) {
169     if (!OpenNextFile()) return kFALSE;
170   }
171   gAlice = (AliRun*)fCurrentFile->Get("gAlice");
172   if (!gAlice)  return kFALSE;
173   return kTRUE;
174 }