0a23816e8e7e1f2b30fd1e36cc7771537562144c
[u/mrichter/AliRoot.git] / HLT / MUON / src / AliRoot / ADCStreamSource.cxx
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 ////////////////////////////////////////////////////////////////////////////////
7
8 #include "AliRoot/ADCStreamSource.hpp"
9 #include "AliRoot/Base.hpp"
10 #include "TSystem.h"
11 #include <stdio.h>
12
13 // TODO: Change all the Error message statements to AliError at some stage.
14
15 ClassImp(AliHLTMUONADCStreamSource)
16 ClassImp(AliHLTMUONADCStreamSource::DataBlock)
17
18
19 AliHLTMUONADCStreamSource::AliHLTMUONADCStreamSource() : TObject()
20 {
21         fCurrentStream = -1;
22 }
23
24
25 AliHLTMUONADCStreamSource::~AliHLTMUONADCStreamSource()
26 {
27         // Everything is cleaned up implicitly.
28 }
29
30
31 void AliHLTMUONADCStreamSource::FillFromFile(const TString& filename, Int_t eventnumber)
32 {
33         DebugMsg(1, "Entering FillFromFile, file = " << filename.Data()
34                 << ", event number = " << eventnumber
35         );
36         FILE* file = fopen(filename, "r");
37         try
38         {
39                 Long_t id = -1;
40                 Long64_t size = -1;
41                 Long_t flags = -1;
42                 Long_t modtime = -1;
43                 if ( gSystem->GetPathInfo(filename, &id, &size, &flags, &modtime) == 0 )
44                 {
45                         DebugMsg(2, "Size of file: " << filename.Data() << " is " << size << " bytes");
46                         AliHLTMUONADCStream stream;
47                         stream.Size(size/sizeof(Int_t));
48                         size_t bytesread = fread(stream.Data(), 1, size, file);
49                         if (bytesread == (size_t)size)
50                                 AddStream(stream, eventnumber);
51                         else
52                                 Error("FillFromFile", "Could not read from file: %s", filename.Data());
53                 }
54                 else
55                         Error("FillFromFile", "Could not stat the file: %s", filename.Data());
56         }
57         finally
58         (
59                 fclose(file);
60         );
61         
62         DebugMsg(1, "Leaving FillFromFile");
63 }
64
65
66 void AliHLTMUONADCStreamSource::FillFrom(const TString& directory, Int_t eventnumber)
67 {
68         DebugMsg(1, "Entering FillFrom, directory = " << directory.Data()
69                 << ", event number = " << eventnumber
70         );
71         
72         void* dir = gSystem->OpenDirectory(directory);
73         try
74         {
75                 const char* entry = gSystem->GetDirEntry(dir);
76                 while (entry != NULL)
77                 {
78                         TString filename = entry;
79                         if (filename.BeginsWith("MUON"))
80                                 FillFromFile(filename, eventnumber);
81                         entry = gSystem->GetDirEntry(dir);
82                 };
83         }
84         finally
85         (
86                 gSystem->FreeDirectory(dir);
87         );
88         
89         
90         DebugMsg(1, "Leaving FillFrom");
91 }
92
93
94 void AliHLTMUONADCStreamSource::FillFrom(const TString& dirprefix, UInt_t firstevent, UInt_t lastevent)
95 {
96         DebugMsg(1, "Entering FillFrom");
97         
98         for (UInt_t i = firstevent; i <= lastevent; i++)
99         {
100                 TString dirname = dirprefix;
101                 dirname += i;
102                 FillFrom(dirname, i);
103         };
104         
105         DebugMsg(1, "Leaving FillFrom");
106 }
107
108
109 void AliHLTMUONADCStreamSource::Clear(Option_t* /*option*/)
110 {
111         fCurrentStream = -1;
112         fList.erase( fList.begin(), fList.end() );
113 }
114
115
116 Int_t AliHLTMUONADCStreamSource::NumberOfStreams() const
117 {
118         return fList.size();
119 }
120
121
122 Bool_t AliHLTMUONADCStreamSource::GetStream(Int_t index) const
123 {
124         if ( 0 <= index && index < NumberOfStreams() )
125         {
126                 fCurrentStream = index;
127                 return kTRUE;
128         }
129         else
130         {
131                 // The index is out of bounds so inform the user.
132                 if (NumberOfStreams() > 0)
133                         Error(  "GetStream",
134                                 "The ADC stream index (%d) is out of bounds. Valid range is [0, %d]",
135                                 index, NumberOfStreams() - 1
136                         );
137                 else
138                         Error(  "GetStream",
139                                 "The ADC stream index (%d) is out of bounds. No streams found.",
140                                 index
141                         );
142                 return kFALSE;
143         }
144 }
145
146
147 Bool_t AliHLTMUONADCStreamSource::FirstStream() const
148 {
149         if (NumberOfStreams() > 0)
150         {
151                 fCurrentStream = 0;
152                 return kTRUE;
153         }
154         else
155                 return kFALSE;
156 }
157
158
159 Bool_t AliHLTMUONADCStreamSource::NextStream() const
160 {
161         if ( 0 <= fCurrentStream && fCurrentStream < NumberOfStreams() - 1 )
162         {
163                 fCurrentStream++;
164                 return kTRUE;
165         }
166         else
167                 return kFALSE;
168 };
169
170
171 Int_t AliHLTMUONADCStreamSource::EventNumber() const
172 {
173         if (fCurrentStream >= 0)
174         {
175                 Assert( fCurrentStream < NumberOfStreams() );
176                 return fList[fCurrentStream].fEventNumber;
177         }
178         else
179         {
180                 Error("EventNumber", "No ADC stream selected.");
181                 return -1;
182         }
183 }
184
185
186 Bool_t AliHLTMUONADCStreamSource::FetchStream(AliHLTMUONADCStream& stream) const
187 {
188         if (fCurrentStream >= 0)
189         {
190                 Assert( fCurrentStream < NumberOfStreams() );
191                 stream = fList[fCurrentStream].fStream;
192                 return kTRUE;
193         }
194         else
195         {
196                 Error("FetchStream", "No ADC stream selected.");
197                 return kFALSE;
198         }
199 }
200
201
202 Bool_t AliHLTMUONADCStreamSource::FetchStream(Int_t index, AliHLTMUONADCStream& stream) const
203 {
204         if ( GetStream(index) )
205                 return FetchStream(stream);
206         else
207                 return kFALSE;
208 }
209
210
211 const AliHLTMUONADCStream* AliHLTMUONADCStreamSource::FetchStream() const
212 {
213         if (fCurrentStream >= 0)
214         {
215                 Assert( fCurrentStream < NumberOfStreams() );
216                 return &( fList[fCurrentStream].fStream );
217         }
218         else
219         {
220                 Error("FetchStream", "No ADC stream selected.");
221                 return NULL;
222         }
223 }
224
225
226 void AliHLTMUONADCStreamSource::AddStream(AliHLTMUONADCStream& stream, UInt_t eventnumber)
227 {
228         DebugMsg(1, "Entering AddStream");
229         
230         DataBlock newdata;
231         newdata.fEventNumber = eventnumber;
232         newdata.fStream = stream;
233         fList.push_back(newdata);
234         fCurrentStream = fList.size() - 1;
235         
236         DebugMsg(1, "Leaving AddStream");
237 }
238