]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReader.cxx
provide non-void return type for operator=() to make the
[u/mrichter/AliRoot.git] / RAW / AliRawReader.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 //
18 // This is the base class for reading raw data and providing
19 // information about digits
20 //
21 // The derived classes, which operate on concrete raw data formats,
22 // should implement
23 // - ReadHeader to read the next (mini or equipment) header
24 // - ReadNextData to read the next raw data block (=1 DDL)
25 // - ReadNext to read a given number of bytes
26 // - several getters like GetType
27 //
28 // Sequential access to the raw data is provided by the methods
29 // ReadHeader, ReadNextData, ReadNextInt, ReadNextShort, ReadNextChar
30 //
31 // If only data from a specific detector (and a given range of DDL numbers)
32 // should be read, this can be achieved by the Select method.
33 // Several getter provide information about the current event and the
34 // current type of raw data.
35 //
36 ///////////////////////////////////////////////////////////////////////////////
37
38 #include "AliRawReader.h"
39
40
41 ClassImp(AliRawReader)
42
43
44 AliRawReader::AliRawReader() :
45   fMiniHeader(NULL),
46   fCount(0),
47   fSelectDetectorID(-1),
48   fSelectMinDDLID(-1),
49   fSelectMaxDDLID(-1),
50   fSelectEquipmentType(-1),
51   fSelectMinEquipmentId(-1),
52   fSelectMaxEquipmentId(-1),
53   fErrorCode(0)
54 {
55 // default constructor: initialize data members
56
57 }
58
59 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
60   TObject(rawReader),
61   fMiniHeader(rawReader.fMiniHeader),
62   fCount(rawReader.fCount),
63   fSelectDetectorID(rawReader.fSelectDetectorID),
64   fSelectMinDDLID(rawReader.fSelectMinDDLID),
65   fSelectMaxDDLID(rawReader.fSelectMaxDDLID),
66   fSelectEquipmentType(rawReader.fSelectEquipmentType),
67   fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
68   fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
69   fErrorCode(0)
70 {
71 // copy constructor
72
73 }
74
75 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
76 {
77 // assignment operator
78
79   fMiniHeader = rawReader.fMiniHeader;
80   fCount = rawReader.fCount;
81
82   fSelectDetectorID = rawReader.fSelectDetectorID;
83   fSelectMinDDLID = rawReader.fSelectMinDDLID;
84   fSelectMaxDDLID = rawReader.fSelectMaxDDLID;
85
86   fErrorCode = rawReader.fErrorCode;
87
88   return *this;
89 }
90
91
92 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
93 {
94 // read only data of the detector with the given ID and in the given
95 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
96 // no selection is applied if a value < 0 is used.
97
98   fSelectDetectorID = detectorID;
99   fSelectMinDDLID = minDDLID;
100   fSelectMaxDDLID = maxDDLID;
101 }
102
103 void AliRawReader::SelectEquipment(Int_t equipmentType, 
104                                    Int_t minEquipmentId, Int_t maxEquipmentId)
105 {
106 // read only data of the equipment with the given type and in the given
107 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
108 // no selection is applied if a value < 0 is used.
109
110   fSelectEquipmentType = equipmentType;
111   fSelectMinEquipmentId = minEquipmentId;
112   fSelectMaxEquipmentId = maxEquipmentId;
113 }
114
115 Bool_t AliRawReader::IsSelected() const
116 {
117 // apply the selection (if any)
118
119   if (fSelectDetectorID >= 0) {
120     if (!fMiniHeader) return kFALSE;
121     if (fMiniHeader->fDetectorID != fSelectDetectorID) return kFALSE;
122     if ((fSelectMinDDLID >= 0) && (fMiniHeader->fDDLID < fSelectMinDDLID))
123       return kFALSE;
124     if ((fSelectMaxDDLID >= 0) && (fMiniHeader->fDDLID > fSelectMaxDDLID))
125       return kFALSE;
126   }
127
128   if (fSelectEquipmentType >= 0) {
129     if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
130     if ((fSelectMinEquipmentId >= 0) && 
131         (GetEquipmentId() < fSelectMinEquipmentId))
132       return kFALSE;
133     if ((fSelectMaxEquipmentId >= 0) && 
134         (GetEquipmentId() > fSelectMaxEquipmentId))
135       return kFALSE;
136   }
137
138   return kTRUE;
139 }
140
141
142 Bool_t AliRawReader::CheckMiniHeader(AliMiniHeader* miniHeader) const
143 {
144 // check the magic number of the mini header
145
146   if (!miniHeader) miniHeader = fMiniHeader;
147   if (!miniHeader) return kFALSE;
148   UInt_t magicWord = miniHeader->fMagicWord[2]*65536 +
149     miniHeader->fMagicWord[1]*256 + miniHeader->fMagicWord[0];
150   if (magicWord != AliMiniHeader::kMagicWord) {
151     return kFALSE;
152   }
153   return kTRUE;
154 }
155
156 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
157 {
158 // reads the next 4 bytes at the current position
159 // returns kFALSE if the data could not be read
160
161   while (fCount == 0) {
162     if (!ReadHeader()) return kFALSE;
163   }
164   if (fCount < (Int_t) sizeof(data)) {
165     Error("ReadNextInt", 
166           "too few data left (%d bytes) to read an UInt_t!", fCount);
167     return kFALSE;
168   }
169   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
170     Error("ReadNextInt", "could not read data!");
171     return kFALSE;
172   }
173   return kTRUE;
174 }
175
176 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
177 {
178 // reads the next 2 bytes at the current position
179 // returns kFALSE if the data could not be read
180
181   while (fCount == 0) {
182     if (!ReadHeader()) return kFALSE;
183   }
184   if (fCount < (Int_t) sizeof(data)) {
185     Error("ReadNextShort", 
186           "too few data left (%d bytes) to read an UShort_t!", fCount);
187     return kFALSE;
188   }
189   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
190     Error("ReadNextShort", "could not read data!");
191     return kFALSE;
192   }
193   return kTRUE;
194 }
195
196 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
197 {
198 // reads the next 1 byte at the current stream position
199 // returns kFALSE if the data could not be read
200
201   while (fCount == 0) {
202     if (!ReadHeader()) return kFALSE;
203   }
204   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
205     Error("ReadNextChar", "could not read data!");
206     return kFALSE;
207   }
208   return kTRUE;
209 }
210
211
212 Int_t AliRawReader::CheckData() const
213 {
214 // check the consistency of the data
215 // derived classes should overwrite the default method which returns 0 (no err)
216
217   return 0;
218 }
219
220
221 void AliRawReader::DumpData(Int_t limit)
222 {
223 // print the raw data
224 // if limit is not negative, only the first and last "limit" lines of raw data
225 // are printed
226
227   Reset();
228   if (!ReadHeader()) {
229     Error("DumpData", "no header");
230     return;
231   }
232   printf("header:\n"
233          " type = %d  run = %d  ", GetType(), GetRunNumber());
234   if (GetEventId()) {
235     printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
236   } else {
237     printf("event = -------- --------\n");
238   }
239   if (GetTriggerPattern()) {
240     printf(" trigger = %8.8x %8.8x  ",
241            GetTriggerPattern()[1], GetTriggerPattern()[0]);
242   } else {
243     printf(" trigger = -------- --------  ");
244   }
245   if (GetDetectorPattern()) {
246     printf("detector = %8.8x\n", GetDetectorPattern()[0]);
247   } else {
248     printf("detector = --------\n");
249   }
250   if (GetAttributes()) {
251     printf(" attributes = %8.8x %8.8x %8.8x  ",
252            GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
253   } else {
254     printf(" attributes = -------- -------- --------  ");
255   }
256   printf("GDC = %d\n", GetGDCId());
257   printf("\n");
258
259   do {
260     printf("-------------------------------------------------------------------------------\n");
261     printf("LDC = %d\n", GetLDCId());
262
263     printf("equipment:\n"
264            " size = %d  type = %d  id = %d\n",
265            GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
266     if (GetEquipmentAttributes()) {
267       printf(" attributes = %8.8x %8.8x %8.8x  ", GetEquipmentAttributes()[2],
268              GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
269     } else {
270       printf(" attributes = -------- -------- --------  ");
271     }
272     printf("element size = %d\n", GetEquipmentElementSize());
273
274     printf("mini header:\n"
275            " size = %d  detector = %d  DDL = %d\n"
276            " version = %d  compression = %d\n",
277            GetDataSize(), GetDetectorID(), GetDDLID(),
278            GetVersion(), IsCompressed());
279
280     printf("\n");
281     if (limit == 0) continue;
282
283     Int_t size = GetDataSize();
284     char line[70];
285     for (Int_t i = 0; i < 70; i++) line[i] = ' ';
286     line[69] = '\0';
287     Int_t pos = 0;
288     Int_t max = 16;
289     UChar_t byte;
290
291     for (Int_t n = 0; n < size; n++) {
292       if (!ReadNextChar(byte)) {
293         Error("DumpData", "couldn't read byte number %d\n", n);
294         break;
295       }
296       if (pos >= max) {
297         printf("%8.8x  %s\n", n-pos, line);
298         for (Int_t i = 0; i < 70; i++) line[i] = ' ';
299         line[69] = '\0';
300         pos = 0;
301         if ((limit > 0) && (n/max == limit)) {
302           Int_t nContinue = ((size-1)/max+1-limit) * max;
303           if (nContinue > n) {
304             printf(" [skipping %d bytes]\n", nContinue-n);
305             n = nContinue-1;
306             continue;
307           }
308         }
309       }
310       Int_t offset = pos/4;
311       if ((byte > 0x20) && (byte < 0x7f)) {
312         line[pos+offset] = byte;
313       } else {
314         line[pos+offset] = '.';
315       }
316       char hex[3];
317       sprintf(hex, "%2.2x", byte);
318       line[max+max/4+3+2*pos+offset] = hex[0];
319       line[max+max/4+4+2*pos+offset] = hex[1];
320       pos++;
321     }
322
323     if (pos > 0) printf("%8.8x  %s\n", size-pos, line);
324     printf("\n");
325            
326   } while (ReadHeader());
327 }