]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliL3DDLRawReaderFile.cxx
Compilation on Alpha
[u/mrichter/AliRoot.git] / HLT / misc / AliL3DDLRawReaderFile.cxx
1 // @(#) $Id$
2
3 // Author: Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #include "AliL3RootTypes.h"
7 #include "AliL3StandardIncludes.h"
8 #include "AliL3Logging.h"
9
10 #include "AliL3DDLRawReaderFile.h"
11
12 #if __GNUC__ == 3
13 using namespace std;
14 #endif
15
16 /**************************************************************************
17  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
18  *                                                                        *
19  * Author: The ALICE Off-line Project.                                    *
20  * Contributors are mentioned in the code where appropriate.              *
21  *                                                                        *
22  * Permission to use, copy, modify and distribute this software and its   *
23  * documentation strictly for non-commercial purposes is hereby granted   *
24  * without fee, provided that the above copyright notice appears in all   *
25  * copies and that both the copyright notice and this permission notice   *
26  * appear in the supporting documentation. The authors make no claims     *
27  * about the suitability of this software for any purpose. It is          *
28  * provided "as is" without express or implied warranty.                  *
29  **************************************************************************/
30
31 /** \class AliL3DDLRawReaderFile
32 <pre>
33 //_____________________________________________________________
34 // AliL3DDLRawReaderFile (taken from the offline AliROOT code,
35 // original authors: D.Favretto and A.K.Mohanty)
36 //
37 // This is the base class for reading ddl raw data 
38 // and providing information about digits
39 </pre>
40 */
41
42 ClassImp(AliL3DDLRawReaderFile)
43
44 AliL3DDLRawReaderFile::AliL3DDLRawReaderFile(const Char_t* name, Bool_t addnum)
45 {
46   // create an object to read digits from the given input file(s)
47   // if addNumber is true, a number starting at 1 is appended to the file name
48
49   fFileName = new Char_t[1024];
50   strcpy(fFileName,name);
51   if (!addnum) {
52     fFileNumber = -1;
53 #ifndef __DECCXX
54     fStream = new fstream(fFileName, ios::binary|ios::in);
55 #else
56     fStream = new fstream(fFileName, ios::in);
57 #endif
58   } else {
59     fFileNumber = 0;
60     fStream = NULL;
61     OpenNextFile();
62   }
63   fMiniHeader = new AliL3DDLMiniHeader;
64   fBuffer = NULL;
65   fBufferSize = 0;
66 }
67
68 AliL3DDLRawReaderFile::~AliL3DDLRawReaderFile()
69 {
70   // close the input file
71   if(fFileName) delete fFileName;
72
73   if (fStream) {
74 #if defined(__HP_aCC) || defined(__DECCXX)
75     if (fStream->rdbuf()->is_open()) fStream->close();
76 #else
77     if (fStream->is_open()) fStream->close();
78 #endif
79     delete fStream;
80   }
81   delete fMiniHeader;
82   if (fBuffer) delete[] fBuffer;
83 }
84
85 Bool_t AliL3DDLRawReaderFile::OpenNextFile()
86 {
87   if (fStream) {
88 #if defined(__HP_aCC) || defined(__DECCXX)
89     if (fStream->rdbuf()->is_open()) fStream->close();
90 #else
91     if (fStream->is_open()) fStream->close();
92 #endif
93     delete fStream;
94     fStream = NULL;
95   }
96   if (fFileNumber < 0) {
97     LOG(AliL3Log::kError,"AliL3DDLRawReaderFile::OpenNextFile","File")
98       <<"Could not open file, file number is negative."<<ENDLOG;
99     return kFALSE;
100   }
101
102   fFileNumber++;
103   Char_t fileName[1024];
104   sprintf(fileName, "%s%d", fFileName, fFileNumber);
105
106 #ifndef __DECCXX
107   fStream = new fstream(fileName, ios::binary|ios::in);
108 #else
109   fStream = new fstream(fileName, ios::in);
110 #endif
111 #if defined(__HP_aCC) || defined(__DECCXX)
112   return (fStream->rdbuf()->is_open());
113 #else
114   return (fStream->is_open());
115 #endif
116 }
117
118 Bool_t AliL3DDLRawReaderFile::ReadMiniHeader()
119 {
120   // read a mini header at the current stream position
121   // returns kFALSE if the mini header could not be read
122
123   if (!fStream) return kFALSE;
124   do {
125     if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
126     while (!fStream->read((Char_t*) fMiniHeader, sizeof(AliL3DDLMiniHeader))) {
127       if (!OpenNextFile()) return kFALSE;
128     }
129     //cout << fMiniHeader->fSize << " " << fMiniHeader->fDetectorID << " " << fMiniHeader->fVersion << " " << fMiniHeader->fCompressionFlag << " " << fMiniHeader->fDDLID << endl;
130     //cout << "loop here " << (Int_t)fMiniHeader->fDDLID<< endl;
131     CheckMiniHeader();
132     fCount = fMiniHeader->fSize;
133   } while (!IsSelected());
134   return kTRUE;
135 }
136
137 Bool_t AliL3DDLRawReaderFile::ReadNextData(UChar_t*& data)
138 {
139   // reads the next payload at the current stream position
140   // returns kFALSE if the data could not be read
141
142   while (fCount == 0) {
143     if (!ReadMiniHeader()) return kFALSE;
144   }
145   if (fBufferSize < fCount) {
146     if (fBuffer) delete[] fBuffer;
147     fBufferSize = Int_t(fCount*1.2);
148     fBuffer = new UChar_t[fBufferSize];
149   }
150   if (!fStream->read((Char_t*) fBuffer, fCount)) {
151     LOG(AliL3Log::kError,"AliL3DDLRawReaderFile::ReadNextData","Data")
152       <<"Could not read next data!"<<ENDLOG;
153     return kFALSE;
154   }
155   fCount = 0;
156
157   data = fBuffer;
158   return kTRUE;
159 }
160
161 Bool_t AliL3DDLRawReaderFile::ReadNext(UChar_t* data, Int_t size)
162 {
163   // reads the next block of data at the current stream position
164   // returns kFALSE if the data could not be read
165
166   if (!fStream->read((Char_t*) data, size)) {
167     LOG(AliL3Log::kError,"AliL3DDLRawReaderFile::ReadNext","Data")
168       <<"Could not read next data!"<<ENDLOG;
169     return kFALSE;
170   }
171   fCount -= size;
172   return kTRUE;
173 }
174
175 Bool_t AliL3DDLRawReaderFile::Reset()
176 {
177   // reset the current stream position to the beginning of the file
178
179   if ((fFileNumber > 0) && fStream) {
180 #if defined(__HP_aCC) || defined(__DECCXX)
181     if (fStream->rdbuf()->is_open()) fStream->close();
182 #else
183     if (fStream->is_open()) fStream->close();
184 #endif
185     delete fStream;
186     fStream = NULL;
187     fFileNumber = 0;
188   }
189
190   if (!fStream) {
191     if (fFileNumber < 0) {
192 #ifndef __DECCXX
193       fStream = new fstream(fFileName, ios::binary|ios::in);
194 #else
195       fStream = new fstream(fFileName, ios::in);
196 #endif
197     } else {
198       if (!OpenNextFile()){
199         LOG(AliL3Log::kError,"AliL3DDLRawReaderFile::Reset","Data")
200           <<"Could not reset data stream!"<<ENDLOG;
201         return kFALSE;
202       }
203     }
204   }
205
206   if (!fStream || !fStream->rdbuf()->is_open()) return kFALSE;
207   fStream->seekg(0);
208   fCount = 0;
209   return kTRUE;
210 }