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