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