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