]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReader.cxx
Added preprocessor conditionals to support ROOT > 5.11.2.
[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
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 AliDAQConfig.h
161   Int_t equipmentId;
162   if (fEquipmentIdsIn && fEquipmentIdsIn)
163     equipmentId = GetMappedEquipmentId();
164   else
165     equipmentId = GetEquipmentId();
166
167   if (equipmentId >= 0)
168     return (equipmentId >> 8);
169   else
170     return -1;
171 }
172
173 Int_t AliRawReader::GetDDLID() const
174 {
175   // Get the DDL ID (within one sub-detector)
176   // The list of detector IDs
177   // can be found in AliDAQConfig.h
178   Int_t equipmentId;
179   if (fEquipmentIdsIn && fEquipmentIdsIn)
180     equipmentId = GetMappedEquipmentId();
181   else
182     equipmentId = GetEquipmentId();
183
184   if (equipmentId >= 0)
185     return (equipmentId & 0xFF);
186   else
187     return -1;
188 }
189
190 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
191 {
192 // read only data of the detector with the given ID and in the given
193 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
194 // no selection is applied if a value < 0 is used.
195
196   fSelectEquipmentType = -1;
197   if (minDDLID < 0) minDDLID = 0;
198   fSelectMinEquipmentId = (detectorID << 8) + minDDLID;
199   if (maxDDLID < 0) maxDDLID = 0xFF;
200   fSelectMaxEquipmentId = (detectorID << 8) + maxDDLID;
201 }
202
203 void AliRawReader::SelectEquipment(Int_t equipmentType, 
204                                    Int_t minEquipmentId, Int_t maxEquipmentId)
205 {
206 // read only data of the equipment with the given type and in the given
207 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
208 // no selection is applied if a value < 0 is used.
209
210   fSelectEquipmentType = equipmentType;
211   fSelectMinEquipmentId = minEquipmentId;
212   fSelectMaxEquipmentId = maxEquipmentId;
213 }
214
215 void AliRawReader::SelectEvents(Int_t type)
216 {
217 // read only events with the given type.
218 // no selection is applied if a value < 0 is used.
219
220   fSelectEventType = type;
221 }
222
223 Bool_t AliRawReader::IsSelected() const
224 {
225 // apply the selection (if any)
226
227   if (fSkipInvalid && !IsValid()) return kFALSE;
228
229   if (fSelectEquipmentType >= 0)
230     if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
231
232   Int_t equipmentId;
233   if (fEquipmentIdsIn && fEquipmentIdsIn)
234     equipmentId = GetMappedEquipmentId();
235   else
236     equipmentId = GetEquipmentId();
237
238   if ((fSelectMinEquipmentId >= 0) && 
239       (equipmentId < fSelectMinEquipmentId))
240     return kFALSE;
241   if ((fSelectMaxEquipmentId >= 0) && 
242       (equipmentId > fSelectMaxEquipmentId))
243     return kFALSE;
244
245   return kTRUE;
246 }
247
248 Bool_t AliRawReader::IsEventSelected() const
249 {
250 // apply the event selection (if any)
251
252   if (fSelectEventType >= 0) {
253     if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
254   }
255
256   return kTRUE;
257 }
258
259
260 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
261 {
262 // reads the next 4 bytes at the current position
263 // returns kFALSE if the data could not be read
264
265   while (fCount == 0) {
266     if (!ReadHeader()) return kFALSE;
267   }
268   if (fCount < (Int_t) sizeof(data)) {
269     Error("ReadNextInt", 
270           "too few data left (%d bytes) to read an UInt_t!", fCount);
271     return kFALSE;
272   }
273   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
274     Error("ReadNextInt", "could not read data!");
275     return kFALSE;
276   }
277   return kTRUE;
278 }
279
280 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
281 {
282 // reads the next 2 bytes at the current position
283 // returns kFALSE if the data could not be read
284
285   while (fCount == 0) {
286     if (!ReadHeader()) return kFALSE;
287   }
288   if (fCount < (Int_t) sizeof(data)) {
289     Error("ReadNextShort", 
290           "too few data left (%d bytes) to read an UShort_t!", fCount);
291     return kFALSE;
292   }
293   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
294     Error("ReadNextShort", "could not read data!");
295     return kFALSE;
296   }
297   return kTRUE;
298 }
299
300 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
301 {
302 // reads the next 1 byte at the current stream position
303 // returns kFALSE if the data could not be read
304
305   while (fCount == 0) {
306     if (!ReadHeader()) return kFALSE;
307   }
308   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
309     Error("ReadNextChar", "could not read data!");
310     return kFALSE;
311   }
312   return kTRUE;
313 }
314
315
316 Int_t AliRawReader::CheckData() const
317 {
318 // check the consistency of the data
319 // derived classes should overwrite the default method which returns 0 (no err)
320
321   return 0;
322 }
323
324
325 void AliRawReader::DumpData(Int_t limit)
326 {
327 // print the raw data
328 // if limit is not negative, only the first and last "limit" lines of raw data
329 // are printed
330
331   Reset();
332   if (!ReadHeader()) {
333     Error("DumpData", "no header");
334     return;
335   }
336   printf("header:\n"
337          " type = %d  run = %d  ", GetType(), GetRunNumber());
338   if (GetEventId()) {
339     printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
340   } else {
341     printf("event = -------- --------\n");
342   }
343   if (GetTriggerPattern()) {
344     printf(" trigger = %8.8x %8.8x  ",
345            GetTriggerPattern()[1], GetTriggerPattern()[0]);
346   } else {
347     printf(" trigger = -------- --------  ");
348   }
349   if (GetDetectorPattern()) {
350     printf("detector = %8.8x\n", GetDetectorPattern()[0]);
351   } else {
352     printf("detector = --------\n");
353   }
354   if (GetAttributes()) {
355     printf(" attributes = %8.8x %8.8x %8.8x  ",
356            GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
357   } else {
358     printf(" attributes = -------- -------- --------  ");
359   }
360   printf("GDC = %d\n", GetGDCId());
361   printf("\n");
362
363   do {
364     printf("-------------------------------------------------------------------------------\n");
365     printf("LDC = %d\n", GetLDCId());
366
367     printf("equipment:\n"
368            " size = %d  type = %d  id = %d\n",
369            GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
370     if (GetEquipmentAttributes()) {
371       printf(" attributes = %8.8x %8.8x %8.8x  ", GetEquipmentAttributes()[2],
372              GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
373     } else {
374       printf(" attributes = -------- -------- --------  ");
375     }
376     printf("element size = %d\n", GetEquipmentElementSize());
377
378     printf("data header:\n"
379            " size = %d  version = %d  valid = %d  compression = %d\n",
380            GetDataSize(), GetVersion(), IsValid(), IsCompressed());
381
382     printf("\n");
383     if (limit == 0) continue;
384
385     Int_t size = GetDataSize();
386     char line[70];
387     for (Int_t i = 0; i < 70; i++) line[i] = ' ';
388     line[69] = '\0';
389     Int_t pos = 0;
390     Int_t max = 16;
391     UChar_t byte;
392
393     for (Int_t n = 0; n < size; n++) {
394       if (!ReadNextChar(byte)) {
395         Error("DumpData", "couldn't read byte number %d\n", n);
396         break;
397       }
398       if (pos >= max) {
399         printf("%8.8x  %s\n", n-pos, line);
400         for (Int_t i = 0; i < 70; i++) line[i] = ' ';
401         line[69] = '\0';
402         pos = 0;
403         if ((limit > 0) && (n/max == limit)) {
404           Int_t nContinue = ((size-1)/max+1-limit) * max;
405           if (nContinue > n) {
406             printf(" [skipping %d bytes]\n", nContinue-n);
407             n = nContinue-1;
408             continue;
409           }
410         }
411       }
412       Int_t offset = pos/4;
413       if ((byte > 0x20) && (byte < 0x7f)) {
414         line[pos+offset] = byte;
415       } else {
416         line[pos+offset] = '.';
417       }
418       char hex[3];
419       sprintf(hex, "%2.2x", byte);
420       line[max+max/4+3+2*pos+offset] = hex[0];
421       line[max+max/4+4+2*pos+offset] = hex[1];
422       pos++;
423     }
424
425     if (pos > 0) printf("%8.8x  %s\n", size-pos, line);
426     printf("\n");
427            
428   } while (ReadHeader());
429 }