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