Record changes.
[u/mrichter/AliRoot.git] / TPC / AliTPCMonitorDateFile.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.2  2007/10/12 13:36:27  cvetan
19 Coding convention fixes from Stefan
20
21 Revision 1.1  2007/09/17 10:23:31  cvetan
22 New TPC monitoring package from Stefan Kniege. The monitoring package can be started by running TPCMonitor.C macro located in macros folder.
23
24 */ 
25
26 ////////////////////////////////////////////////////////////////////////
27 ////
28 //// AliTPCMonitorDateFile class
29 //// 
30 //// Class for handling the data structure in a DATE file
31 //// Used to read DATE files for the TPC raw data Monitor 
32 ////
33 //// Author: Roland Bramm
34 ////         Stefan Kniege, IKF, Frankfurt
35 ////       
36 ////
37 /////////////////////////////////////////////////////////////////////////
38
39
40
41 #include "AliTPCMonitorDateFile.h"
42 #include "AliTPCMonitorDateFormat.h"
43 #include <Riostream.h>
44 ClassImp(AliTPCMonitorDateFile)
45
46 //____________________________________________________________________________
47 AliTPCMonitorDateFile::AliTPCMonitorDateFile() :
48   ffilePos(0),
49   fbigMem(0),
50   fbigMemsize(0),
51   fisBigMemAllocated(false),
52   ffileSize(0),
53   ffilename(""),
54   finitFile(false),
55   freadPosOverflow(false),
56   fin(new ifstream())
57 {
58   // Constructor
59 }
60
61 //____________________________________________________________________________
62 AliTPCMonitorDateFile::AliTPCMonitorDateFile(const AliTPCMonitorDateFile &datefile) :
63   TNamed(datefile.GetName(),datefile.GetTitle()),
64   ffilePos(datefile.ffilePos),
65   fbigMem(datefile.fbigMem),
66   fbigMemsize(datefile.fbigMemsize),
67   fisBigMemAllocated(datefile.fisBigMemAllocated),
68   ffileSize(datefile.ffileSize),
69   ffilename(datefile.ffilename),
70   finitFile(datefile.finitFile),
71   freadPosOverflow(datefile.freadPosOverflow),
72   fin(new ifstream())
73 {
74   // copy constructor 
75 }
76
77 //____________________________________________________________________________
78 AliTPCMonitorDateFile &AliTPCMonitorDateFile:: operator= (const AliTPCMonitorDateFile& datefile)
79 {
80
81   // assignment operator 
82   if(this!=&datefile)
83     {
84       ffilePos=datefile.ffilePos;
85       fbigMem=datefile.fbigMem;
86       fbigMemsize=datefile.fbigMemsize;
87       fisBigMemAllocated=datefile.fisBigMemAllocated;
88       ffileSize=datefile.ffileSize;
89       ffilename=datefile.ffilename;
90       finitFile=datefile.finitFile;
91       freadPosOverflow=datefile.freadPosOverflow;
92       fin = new ifstream();
93     }
94   return *this;
95 }
96  
97 //____________________________________________________________________________
98 AliTPCMonitorDateFile::~AliTPCMonitorDateFile() 
99 {
100   // Destructor
101   delete fin;
102   if(fisBigMemAllocated == true){
103     delete[] fbigMem;
104   }
105 }
106
107 //____________________________________________________________________________
108 void AliTPCMonitorDateFile::OpenDateFile(string name) 
109 {
110   // Open DATE file 
111   if( (ffilename != name) && (finitFile == true) ) {
112     CloseDateFile();
113     ResetFilePos();
114   }
115   fin->open(name.c_str());
116   ffilename = name;
117   SetFileSize();
118   finitFile = true;
119 }
120
121 //____________________________________________________________________________
122 Bool_t AliTPCMonitorDateFile::IsDateFileOpen() 
123 {
124   // Check if DATE file is open
125   return fin->is_open();
126 }
127
128 //____________________________________________________________________________
129 void AliTPCMonitorDateFile::CloseDateFile() 
130 {
131   // Close DATE file
132   fin->close();
133   freadPosOverflow = false;
134   delete fin;
135   fin = new ifstream();
136 }
137
138 //____________________________________________________________________________
139 void AliTPCMonitorDateFile::ResetFilePos() 
140 {
141   // Reset file position 
142   ffilePos = 0;
143 }
144
145 //____________________________________________________________________________
146 void AliTPCMonitorDateFile::SetFileSize() 
147 {
148   // Set size of DATE file
149   fin->seekg (0, ios::end);
150   ffileSize = fin->tellg();
151   fin->seekg (0, ios::beg);
152 }
153
154 //____________________________________________________________________________
155 Int_t AliTPCMonitorDateFile::GetFileSize() const
156 {
157   // Return size of DATE file
158   return ffileSize;
159 }
160
161 //____________________________________________________________________________
162 void AliTPCMonitorDateFile::ReadEvent() 
163 {
164   // Read in event from file
165   Int_t size;
166   Char_t         fmem[512];                      // array for event header
167   Char_t swapcarry[4];
168         Bool_t toSwapEndian = false;
169         //Fetch some bytes to get headers to know how much
170         fin->seekg(ffilePos);
171         //if( (getFilePosition() + sizeof(fmem)) <= (unsigned int)getFileSize()){
172         UInt_t sfmem = 272;
173         if( (GetFilePosition() + sfmem) <= (UInt_t)GetFileSize()){
174
175           
176           fin->read((Char_t*)fmem,sfmem);
177                 fin->seekg(ffilePos);
178                 AliTPCMonitorDateFormat *dateform;
179                 dateform = new AliTPCMonitorDateFormat((Char_t *)&fmem);
180                 
181
182           
183                 toSwapEndian = dateform->IsEventWrongEndian();
184                 if(toSwapEndian == true){
185                         delete dateform;
186                         for(Int_t i = 0; i < 68; i++) {
187                                 swapcarry[0] = fmem[(i*4)+0];
188                                 swapcarry[1] = fmem[(i*4)+1];
189                                 swapcarry[2] = fmem[(i*4)+2];
190                                 swapcarry[3] = fmem[(i*4)+3];
191                                 fmem[(i*4)+0] = swapcarry[3];
192                                 fmem[(i*4)+1] = swapcarry[2];
193                                 fmem[(i*4)+2] = swapcarry[1];
194                                 fmem[(i*4)+3] = swapcarry[0];
195                         }
196                         dateform = new AliTPCMonitorDateFormat((Char_t *)&fmem);
197                 }
198                 size = dateform->GetEventSize();
199                 if(size > GetAllocatedSizeofArray()) {
200                   AllocateArray((Int_t)(size*1.1));
201                 }
202                 if( (GetFilePosition() + size) <= GetFileSize()){
203                   fin->read((Char_t*)fbigMem,size);
204                   if(toSwapEndian == true){
205                                 for(Int_t i = 0; i < (size/4); i++) {
206                                         swapcarry[0] = fbigMem[(i*4)+0];
207                                         swapcarry[1] = fbigMem[(i*4)+1];
208                                         swapcarry[2] = fbigMem[(i*4)+2];
209                                         swapcarry[3] = fbigMem[(i*4)+3];
210                                         fbigMem[(i*4)+0] = swapcarry[3];
211                                         fbigMem[(i*4)+1] = swapcarry[2];
212                                         fbigMem[(i*4)+2] = swapcarry[1];
213                                         fbigMem[(i*4)+3] = swapcarry[0];
214                                 }
215                         }
216                         ffilePos += size;
217                 }else{
218                   freadPosOverflow = true;
219                 }
220                 delete dateform;
221         }else{
222           freadPosOverflow = true;
223         }
224 }
225
226 //____________________________________________________________________________
227 Bool_t AliTPCMonitorDateFile::IsLastEvent() const
228 {
229   // Check if event is last event in file
230   Bool_t retval;
231   if( (ffilePos < ffileSize) && (freadPosOverflow == false) )
232     retval = false;
233   else
234     retval = true;
235   return retval;
236 }
237
238 //____________________________________________________________________________
239 Bool_t AliTPCMonitorDateFile::IsEventValid() const
240 {
241   // Check Over flow flag 
242   Bool_t retval;
243   if(freadPosOverflow == false)
244     retval = true;
245   else
246     retval = false;
247   return retval;
248 }
249
250 //____________________________________________________________________________
251 Int_t AliTPCMonitorDateFile::GetFilePosition() const
252 {
253   // Return current position in file
254   return ffilePos;
255 }
256
257 //____________________________________________________________________________
258 Int_t AliTPCMonitorDateFile::GetAllocatedSizeofArray() const
259 {
260   // Return size of allocated data array
261   return fbigMemsize;
262 }
263
264 //____________________________________________________________________________
265 void AliTPCMonitorDateFile::AllocateArray(Int_t size) 
266 {
267   // allocate data array
268   if(fisBigMemAllocated == false) {
269     fbigMem = new Char_t[size];
270     fbigMemsize = size;
271     fisBigMemAllocated = true;
272   } else {
273     delete fbigMem;
274     fbigMem = new Char_t[size];
275     fbigMemsize = size;
276     fisBigMemAllocated = true;
277   }
278 }
279
280 //____________________________________________________________________________
281 Char_t *AliTPCMonitorDateFile::GetMemoryPointer() 
282 {
283   // Return pointer to data array
284   return fbigMem;
285 }
286