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