]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReader.cxx
Use new naming conventions from QuadSet.
[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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 ///
20 /// This is the base class for reading raw data.
21 ///
22 /// The derived classes, which operate on concrete raw data formats,
23 /// should implement
24 /// - ReadHeader to read the next (data/equipment) header
25 /// - ReadNextData to read the next raw data block (=1 DDL)
26 /// - ReadNext to read a given number of bytes
27 /// - several getters like GetType
28 ///
29 /// Sequential access to the raw data is provided by the methods
30 /// ReadHeader, ReadNextData, ReadNextInt, ReadNextShort, ReadNextChar
31 ///
32 /// If only data from a specific detector (and a given range of DDL numbers)
33 /// should be read, this can be achieved by the Select method.
34 /// Several getters provide information about the current event and the
35 /// current type of raw data.
36 ///
37 ///////////////////////////////////////////////////////////////////////////////
38
39 #include <Riostream.h>
40 #include "AliRawReader.h"
41 #include "AliDAQ.h"
42
43 ClassImp(AliRawReader)
44
45
46 AliRawReader::AliRawReader() :
47   fEquipmentIdsIn(NULL),
48   fEquipmentIdsOut(NULL),
49   fRequireHeader(kTRUE),
50   fHeader(NULL),
51   fCount(0),
52   fSelectEquipmentType(-1),
53   fSelectMinEquipmentId(-1),
54   fSelectMaxEquipmentId(-1),
55   fSkipInvalid(kFALSE),
56   fSelectEventType(-1),
57   fErrorCode(0)
58 {
59 // default constructor: initialize data members
60 }
61
62 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
63 {
64   // Open the mapping file
65   // and load the mapping data
66   ifstream input(fileName);
67   if (input.is_open()) {
68     Warning("AliRawReader","Equipment ID mapping file is found !");
69     const Int_t kMaxDDL = 256;
70     fEquipmentIdsIn = new TArrayI(kMaxDDL);
71     fEquipmentIdsOut = new TArrayI(kMaxDDL);
72     Int_t equipIn, equipOut;
73     Int_t nIds = 0;
74     while (input >> equipIn >> equipOut) {
75       if (nIds >= kMaxDDL) {
76         Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
77         break;
78       }
79       fEquipmentIdsIn->AddAt(equipIn,nIds); 
80       fEquipmentIdsOut->AddAt(equipOut,nIds);
81       nIds++;
82     }
83     fEquipmentIdsIn->Set(nIds);
84     fEquipmentIdsOut->Set(nIds);
85     input.close();
86     return kTRUE;
87   }
88   else {
89     Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
90     return kFALSE;
91   }
92 }
93
94 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
95   TObject(rawReader),
96   fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
97   fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
98   fRequireHeader(rawReader.fRequireHeader),
99   fHeader(rawReader.fHeader),
100   fCount(rawReader.fCount),
101   fSelectEquipmentType(rawReader.fSelectEquipmentType),
102   fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
103   fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
104   fSkipInvalid(rawReader.fSkipInvalid),
105   fSelectEventType(rawReader.fSelectEventType),
106   fErrorCode(0)
107 {
108 // copy constructor
109 }
110
111 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
112 {
113 // assignment operator
114   fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
115   fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
116
117   fHeader = rawReader.fHeader;
118   fCount = rawReader.fCount;
119
120   fSelectEquipmentType = rawReader.fSelectEquipmentType;
121   fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
122   fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
123   fSkipInvalid = rawReader.fSkipInvalid;
124   fSelectEventType = rawReader.fSelectEventType;
125
126   fErrorCode = rawReader.fErrorCode;
127
128   return *this;
129 }
130
131 AliRawReader::~AliRawReader()
132 {
133   // destructor
134   // delete the mapping arrays if
135   // initialized
136   if (fEquipmentIdsIn) delete fEquipmentIdsIn;
137   if (fEquipmentIdsOut) delete fEquipmentIdsOut;
138 }
139
140 Int_t AliRawReader::GetMappedEquipmentId() const
141 {
142   if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
143     Error("AliRawReader","equipment Ids mapping is not initialized !");
144     return GetEquipmentId();
145   }
146   Int_t equipmentId = GetEquipmentId();
147   for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
148     if (equipmentId == fEquipmentIdsIn->At(iId)) {
149       equipmentId = fEquipmentIdsOut->At(iId);
150       break;
151     }
152   }
153   return equipmentId;
154 }
155
156 Int_t AliRawReader::GetDetectorID() const
157 {
158   // Get the detector ID
159   // The list of detector IDs
160   // can be found in AliDAQ.h
161   Int_t equipmentId;
162   if (fEquipmentIdsIn && fEquipmentIdsIn)
163     equipmentId = GetMappedEquipmentId();
164   else
165     equipmentId = GetEquipmentId();
166
167   if (equipmentId >= 0) {
168     Int_t ddlIndex;
169     return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
170   }
171   else
172     return -1;
173 }
174
175 Int_t AliRawReader::GetDDLID() const
176 {
177   // Get the DDL ID (within one sub-detector)
178   // The list of detector IDs
179   // can be found in AliDAQ.h
180   Int_t equipmentId;
181   if (fEquipmentIdsIn && fEquipmentIdsIn)
182     equipmentId = GetMappedEquipmentId();
183   else
184     equipmentId = GetEquipmentId();
185
186   if (equipmentId >= 0) {
187     Int_t ddlIndex;
188     AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
189     return ddlIndex;
190   }
191   else
192     return -1;
193 }
194
195 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
196 {
197 // read only data of the detector with the given name and in the given
198 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
199 // no selection is applied if a value < 0 is used.
200   Int_t detectorID = AliDAQ::DetectorID(detectorName);
201   if(detectorID >= 0)
202     Select(detectorID,minDDLID,maxDDLID);
203 }
204
205 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
206 {
207 // read only data of the detector with the given ID and in the given
208 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
209 // no selection is applied if a value < 0 is used.
210
211   fSelectEquipmentType = -1;
212
213   if (minDDLID < 0)
214     fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
215   else
216     fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
217
218   if (maxDDLID < 0)
219     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
220   else
221     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
222 }
223
224 void AliRawReader::SelectEquipment(Int_t equipmentType, 
225                                    Int_t minEquipmentId, Int_t maxEquipmentId)
226 {
227 // read only data of the equipment with the given type and in the given
228 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
229 // no selection is applied if a value < 0 is used.
230
231   fSelectEquipmentType = equipmentType;
232   fSelectMinEquipmentId = minEquipmentId;
233   fSelectMaxEquipmentId = maxEquipmentId;
234 }
235
236 void AliRawReader::SelectEvents(Int_t type)
237 {
238 // read only events with the given type.
239 // no selection is applied if a value < 0 is used.
240
241   fSelectEventType = type;
242 }
243
244 Bool_t AliRawReader::IsSelected() const
245 {
246 // apply the selection (if any)
247
248   if (fSkipInvalid && !IsValid()) return kFALSE;
249
250   if (fSelectEquipmentType >= 0)
251     if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
252
253   Int_t equipmentId;
254   if (fEquipmentIdsIn && fEquipmentIdsIn)
255     equipmentId = GetMappedEquipmentId();
256   else
257     equipmentId = GetEquipmentId();
258
259   if ((fSelectMinEquipmentId >= 0) && 
260       (equipmentId < fSelectMinEquipmentId))
261     return kFALSE;
262   if ((fSelectMaxEquipmentId >= 0) && 
263       (equipmentId > fSelectMaxEquipmentId))
264     return kFALSE;
265
266   return kTRUE;
267 }
268
269 Bool_t AliRawReader::IsEventSelected() const
270 {
271 // apply the event selection (if any)
272
273   if (fSelectEventType >= 0) {
274     if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
275   }
276
277   return kTRUE;
278 }
279
280
281 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
282 {
283 // reads the next 4 bytes at the current position
284 // returns kFALSE if the data could not be read
285
286   while (fCount == 0) {
287     if (!ReadHeader()) return kFALSE;
288   }
289   if (fCount < (Int_t) sizeof(data)) {
290     Error("ReadNextInt", 
291           "too few data left (%d bytes) to read an UInt_t!", fCount);
292     return kFALSE;
293   }
294   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
295     Error("ReadNextInt", "could not read data!");
296     return kFALSE;
297   }
298   return kTRUE;
299 }
300
301 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
302 {
303 // reads the next 2 bytes at the current position
304 // returns kFALSE if the data could not be read
305
306   while (fCount == 0) {
307     if (!ReadHeader()) return kFALSE;
308   }
309   if (fCount < (Int_t) sizeof(data)) {
310     Error("ReadNextShort", 
311           "too few data left (%d bytes) to read an UShort_t!", fCount);
312     return kFALSE;
313   }
314   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
315     Error("ReadNextShort", "could not read data!");
316     return kFALSE;
317   }
318   return kTRUE;
319 }
320
321 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
322 {
323 // reads the next 1 byte at the current stream position
324 // returns kFALSE if the data could not be read
325
326   while (fCount == 0) {
327     if (!ReadHeader()) return kFALSE;
328   }
329   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
330     Error("ReadNextChar", "could not read data!");
331     return kFALSE;
332   }
333   return kTRUE;
334 }
335
336
337 Int_t AliRawReader::CheckData() const
338 {
339 // check the consistency of the data
340 // derived classes should overwrite the default method which returns 0 (no err)
341
342   return 0;
343 }
344
345
346 void AliRawReader::DumpData(Int_t limit)
347 {
348 // print the raw data
349 // if limit is not negative, only the first and last "limit" lines of raw data
350 // are printed
351
352   Reset();
353   if (!ReadHeader()) {
354     Error("DumpData", "no header");
355     return;
356   }
357   printf("header:\n"
358          " type = %d  run = %d  ", GetType(), GetRunNumber());
359   if (GetEventId()) {
360     printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
361   } else {
362     printf("event = -------- --------\n");
363   }
364   if (GetTriggerPattern()) {
365     printf(" trigger = %8.8x %8.8x  ",
366            GetTriggerPattern()[1], GetTriggerPattern()[0]);
367   } else {
368     printf(" trigger = -------- --------  ");
369   }
370   if (GetDetectorPattern()) {
371     printf("detector = %8.8x\n", GetDetectorPattern()[0]);
372   } else {
373     printf("detector = --------\n");
374   }
375   if (GetAttributes()) {
376     printf(" attributes = %8.8x %8.8x %8.8x  ",
377            GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
378   } else {
379     printf(" attributes = -------- -------- --------  ");
380   }
381   printf("GDC = %d\n", GetGDCId());
382   printf("\n");
383
384   do {
385     printf("-------------------------------------------------------------------------------\n");
386     printf("LDC = %d\n", GetLDCId());
387
388     printf("equipment:\n"
389            " size = %d  type = %d  id = %d\n",
390            GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
391     if (GetEquipmentAttributes()) {
392       printf(" attributes = %8.8x %8.8x %8.8x  ", GetEquipmentAttributes()[2],
393              GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
394     } else {
395       printf(" attributes = -------- -------- --------  ");
396     }
397     printf("element size = %d\n", GetEquipmentElementSize());
398
399     printf("data header:\n"
400            " size = %d  version = %d  valid = %d  compression = %d\n",
401            GetDataSize(), GetVersion(), IsValid(), IsCompressed());
402
403     printf("\n");
404     if (limit == 0) continue;
405
406     Int_t size = GetDataSize();
407     char line[70];
408     for (Int_t i = 0; i < 70; i++) line[i] = ' ';
409     line[69] = '\0';
410     Int_t pos = 0;
411     Int_t max = 16;
412     UChar_t byte;
413
414     for (Int_t n = 0; n < size; n++) {
415       if (!ReadNextChar(byte)) {
416         Error("DumpData", "couldn't read byte number %d\n", n);
417         break;
418       }
419       if (pos >= max) {
420         printf("%8.8x  %s\n", n-pos, line);
421         for (Int_t i = 0; i < 70; i++) line[i] = ' ';
422         line[69] = '\0';
423         pos = 0;
424         if ((limit > 0) && (n/max == limit)) {
425           Int_t nContinue = ((size-1)/max+1-limit) * max;
426           if (nContinue > n) {
427             printf(" [skipping %d bytes]\n", nContinue-n);
428             n = nContinue-1;
429             continue;
430           }
431         }
432       }
433       Int_t offset = pos/4;
434       if ((byte > 0x20) && (byte < 0x7f)) {
435         line[pos+offset] = byte;
436       } else {
437         line[pos+offset] = '.';
438       }
439       char hex[3];
440       sprintf(hex, "%2.2x", byte);
441       line[max+max/4+3+2*pos+offset] = hex[0];
442       line[max+max/4+4+2*pos+offset] = hex[1];
443       pos++;
444     }
445
446     if (pos > 0) printf("%8.8x  %s\n", size-pos, line);
447     printf("\n");
448            
449   } while (ReadHeader());
450 }