]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReader.cxx
1. Adding error registers 3 & 4. They contain the information about an address and...
[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 <TClass.h>
40 #include <TPluginManager.h>
41 #include <TROOT.h>
42
43 #include <Riostream.h>
44 #include "AliRawReader.h"
45 #include "AliRawReaderFile.h"
46 #include "AliRawReaderDate.h"
47 #include "AliRawReaderRoot.h"
48 #include "AliDAQ.h"
49 #include "AliLog.h"
50
51 ClassImp(AliRawReader)
52
53
54 AliRawReader::AliRawReader() :
55   fEquipmentIdsIn(NULL),
56   fEquipmentIdsOut(NULL),
57   fRequireHeader(kTRUE),
58   fHeader(NULL),
59   fCount(0),
60   fSelectEquipmentType(-1),
61   fSelectMinEquipmentId(-1),
62   fSelectMaxEquipmentId(-1),
63   fSkipInvalid(kFALSE),
64   fSelectEventType(-1),
65   fErrorCode(0),
66   fEventNumber(-1),
67   fErrorLogs("AliRawDataErrorLog",100),
68   fHeaderSwapped(NULL)
69 {
70 // default constructor: initialize data members
71 // Allocate the swapped header in case of Mac
72 #ifndef R__BYTESWAP
73   fHeaderSwapped=new AliRawDataHeader();
74 #endif
75 }
76
77 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
78 {
79   // Open the mapping file
80   // and load the mapping data
81   ifstream input(fileName);
82   if (input.is_open()) {
83     Warning("AliRawReader","Equipment ID mapping file is found !");
84     const Int_t kMaxDDL = 256;
85     fEquipmentIdsIn = new TArrayI(kMaxDDL);
86     fEquipmentIdsOut = new TArrayI(kMaxDDL);
87     Int_t equipIn, equipOut;
88     Int_t nIds = 0;
89     while (input >> equipIn >> equipOut) {
90       if (nIds >= kMaxDDL) {
91         Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
92         break;
93       }
94       fEquipmentIdsIn->AddAt(equipIn,nIds); 
95       fEquipmentIdsOut->AddAt(equipOut,nIds);
96       nIds++;
97     }
98     fEquipmentIdsIn->Set(nIds);
99     fEquipmentIdsOut->Set(nIds);
100     input.close();
101     return kTRUE;
102   }
103   else {
104     Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
105     return kFALSE;
106   }
107 }
108
109 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
110   TObject(rawReader),
111   fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
112   fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
113   fRequireHeader(rawReader.fRequireHeader),
114   fHeader(rawReader.fHeader),
115   fCount(rawReader.fCount),
116   fSelectEquipmentType(rawReader.fSelectEquipmentType),
117   fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
118   fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
119   fSkipInvalid(rawReader.fSkipInvalid),
120   fSelectEventType(rawReader.fSelectEventType),
121   fErrorCode(0),
122   fEventNumber(-1),
123   fErrorLogs("AliRawDataErrorLog",100),
124   fHeaderSwapped(NULL)
125 {
126 // copy constructor
127 // Allocate the swapped header in case of Mac
128 #ifndef R__BYTESWAP
129   fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
130 #endif
131 }
132
133 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
134 {
135 // assignment operator
136   fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
137   fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
138
139   fHeader = rawReader.fHeader;
140   fCount = rawReader.fCount;
141
142   fSelectEquipmentType = rawReader.fSelectEquipmentType;
143   fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
144   fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
145   fSkipInvalid = rawReader.fSkipInvalid;
146   fSelectEventType = rawReader.fSelectEventType;
147
148   fErrorCode = rawReader.fErrorCode;
149
150   fEventNumber = rawReader.fEventNumber;
151   fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
152
153   return *this;
154 }
155
156 AliRawReader::~AliRawReader()
157 {
158   // destructor
159   // delete the mapping arrays if
160   // initialized
161   if (fEquipmentIdsIn) delete fEquipmentIdsIn;
162   if (fEquipmentIdsOut) delete fEquipmentIdsOut;
163   fErrorLogs.Delete();
164   if (fHeaderSwapped) delete fHeaderSwapped;
165 }
166
167 AliRawReader* AliRawReader::Create(const char *uri)
168 {
169   // RawReader's factory
170   // It instantiate corresponding raw-reader implementation class object
171   // depending on the URI provided
172   // Normal URIs point to files, while the URI starting with
173   // 'mem://:' or 'mem://<filename>' will create
174   // AliRawReaderDateOnline object which is supposed to be used
175   // in the online reconstruction
176
177   TString strURI = uri;
178
179   if (strURI.IsNull()) {
180     AliWarningClass("No raw-reader created");
181     return NULL;
182   }
183
184   AliRawReader *rawReader = NULL;
185   if (!strURI.BeginsWith("mem://")) {
186     AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",strURI.Data()));
187     if (strURI.EndsWith("/")) {
188       rawReader = new AliRawReaderFile(strURI);
189     } else if (strURI.EndsWith(".root")) {
190       rawReader = new AliRawReaderRoot(strURI);
191     } else {
192       rawReader = new AliRawReaderDate(strURI);
193     }
194   }
195   else {
196     strURI.ReplaceAll("mem://","");
197     AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",strURI.Data()));
198
199     TPluginManager* pluginManager = gROOT->GetPluginManager();
200     TString rawReaderName = "AliRawReaderDateOnline";
201     TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
202     // if not, add a plugin for it
203     if (!pluginHandler) {
204       pluginManager->AddHandler("AliRawReader", "online", 
205                                 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
206       pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
207     }
208     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
209       rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,strURI.Data());
210     }
211     else {
212       return NULL;
213     }
214   }
215
216   return rawReader;
217 }
218
219 Int_t AliRawReader::GetMappedEquipmentId() const
220 {
221   if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
222     Error("AliRawReader","equipment Ids mapping is not initialized !");
223     return GetEquipmentId();
224   }
225   Int_t equipmentId = GetEquipmentId();
226   for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
227     if (equipmentId == fEquipmentIdsIn->At(iId)) {
228       equipmentId = fEquipmentIdsOut->At(iId);
229       break;
230     }
231   }
232   return equipmentId;
233 }
234
235 Int_t AliRawReader::GetDetectorID() const
236 {
237   // Get the detector ID
238   // The list of detector IDs
239   // can be found in AliDAQ.h
240   Int_t equipmentId;
241   if (fEquipmentIdsIn && fEquipmentIdsIn)
242     equipmentId = GetMappedEquipmentId();
243   else
244     equipmentId = GetEquipmentId();
245
246   if (equipmentId >= 0) {
247     Int_t ddlIndex;
248     return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
249   }
250   else
251     return -1;
252 }
253
254 Int_t AliRawReader::GetDDLID() const
255 {
256   // Get the DDL ID (within one sub-detector)
257   // The list of detector IDs
258   // can be found in AliDAQ.h
259   Int_t equipmentId;
260   if (fEquipmentIdsIn && fEquipmentIdsIn)
261     equipmentId = GetMappedEquipmentId();
262   else
263     equipmentId = GetEquipmentId();
264
265   if (equipmentId >= 0) {
266     Int_t ddlIndex;
267     AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
268     return ddlIndex;
269   }
270   else
271     return -1;
272 }
273
274 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
275 {
276 // read only data of the detector with the given name and in the given
277 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
278 // no selection is applied if a value < 0 is used.
279   Int_t detectorID = AliDAQ::DetectorID(detectorName);
280   if(detectorID >= 0)
281     Select(detectorID,minDDLID,maxDDLID);
282 }
283
284 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
285 {
286 // read only data of the detector with the given ID and in the given
287 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
288 // no selection is applied if a value < 0 is used.
289
290   fSelectEquipmentType = -1;
291
292   if (minDDLID < 0)
293     fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
294   else
295     fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
296
297   if (maxDDLID < 0)
298     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
299   else
300     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
301 }
302
303 void AliRawReader::SelectEquipment(Int_t equipmentType, 
304                                    Int_t minEquipmentId, Int_t maxEquipmentId)
305 {
306 // read only data of the equipment with the given type and in the given
307 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
308 // no selection is applied if a value < 0 is used.
309
310   fSelectEquipmentType = equipmentType;
311   fSelectMinEquipmentId = minEquipmentId;
312   fSelectMaxEquipmentId = maxEquipmentId;
313 }
314
315 void AliRawReader::SelectEvents(Int_t type)
316 {
317 // read only events with the given type.
318 // no selection is applied if a value < 0 is used.
319
320   fSelectEventType = type;
321 }
322
323 Bool_t AliRawReader::IsSelected() const
324 {
325 // apply the selection (if any)
326
327   if (fSkipInvalid && !IsValid()) return kFALSE;
328
329   if (fSelectEquipmentType >= 0)
330     if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
331
332   Int_t equipmentId;
333   if (fEquipmentIdsIn && fEquipmentIdsIn)
334     equipmentId = GetMappedEquipmentId();
335   else
336     equipmentId = GetEquipmentId();
337
338   if ((fSelectMinEquipmentId >= 0) && 
339       (equipmentId < fSelectMinEquipmentId))
340     return kFALSE;
341   if ((fSelectMaxEquipmentId >= 0) && 
342       (equipmentId > fSelectMaxEquipmentId))
343     return kFALSE;
344
345   return kTRUE;
346 }
347
348 Bool_t AliRawReader::IsEventSelected() const
349 {
350 // apply the event selection (if any)
351
352   if (fSelectEventType >= 0) {
353     if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
354   }
355
356   return kTRUE;
357 }
358
359 UInt_t AliRawReader::SwapWord(UInt_t x) const
360 {
361    // Swap the endianess of the integer value 'x'
362
363    return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
364            ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
365 }
366
367 UShort_t AliRawReader::SwapShort(UShort_t x) const
368 {
369    // Swap the endianess of the short value 'x'
370
371    return (((x & 0x00ffU) <<  8) | ((x & 0xff00U) >>  8)) ;
372 }
373
374 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
375 {
376 // reads the next 4 bytes at the current position
377 // returns kFALSE if the data could not be read
378
379   while (fCount == 0) {
380     if (!ReadHeader()) return kFALSE;
381   }
382   if (fCount < (Int_t) sizeof(data)) {
383     Error("ReadNextInt", 
384           "too few data left (%d bytes) to read an UInt_t!", fCount);
385     return kFALSE;
386   }
387   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
388     Error("ReadNextInt", "could not read data!");
389     return kFALSE;
390   }
391 #ifndef R__BYTESWAP
392   data=SwapWord(data);
393 #endif
394   return kTRUE;
395 }
396
397 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
398 {
399 // reads the next 2 bytes at the current position
400 // returns kFALSE if the data could not be read
401
402   while (fCount == 0) {
403     if (!ReadHeader()) return kFALSE;
404   }
405   if (fCount < (Int_t) sizeof(data)) {
406     Error("ReadNextShort", 
407           "too few data left (%d bytes) to read an UShort_t!", fCount);
408     return kFALSE;
409   }
410   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
411     Error("ReadNextShort", "could not read data!");
412     return kFALSE;
413   }
414 #ifndef R__BYTESWAP
415   data=SwapShort(data);
416 #endif
417   return kTRUE;
418 }
419
420 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
421 {
422 // reads the next 1 byte at the current stream position
423 // returns kFALSE if the data could not be read
424
425   while (fCount == 0) {
426     if (!ReadHeader()) return kFALSE;
427   }
428   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
429     Error("ReadNextChar", "could not read data!");
430     return kFALSE;
431   }
432   return kTRUE;
433 }
434
435
436 Int_t AliRawReader::CheckData() const
437 {
438 // check the consistency of the data
439 // derived classes should overwrite the default method which returns 0 (no err)
440
441   return 0;
442 }
443
444
445 void AliRawReader::DumpData(Int_t limit)
446 {
447 // print the raw data
448 // if limit is not negative, only the first and last "limit" lines of raw data
449 // are printed
450
451   Reset();
452   if (!ReadHeader()) {
453     Error("DumpData", "no header");
454     return;
455   }
456   printf("header:\n"
457          " type = %d  run = %d  ", GetType(), GetRunNumber());
458   if (GetEventId()) {
459     printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
460   } else {
461     printf("event = -------- --------\n");
462   }
463   if (GetTriggerPattern()) {
464     printf(" trigger = %8.8x %8.8x  ",
465            GetTriggerPattern()[1], GetTriggerPattern()[0]);
466   } else {
467     printf(" trigger = -------- --------  ");
468   }
469   if (GetDetectorPattern()) {
470     printf("detector = %8.8x\n", GetDetectorPattern()[0]);
471   } else {
472     printf("detector = --------\n");
473   }
474   if (GetAttributes()) {
475     printf(" attributes = %8.8x %8.8x %8.8x  ",
476            GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
477   } else {
478     printf(" attributes = -------- -------- --------  ");
479   }
480   printf("GDC = %d\n", GetGDCId());
481   printf("\n");
482
483   do {
484     printf("-------------------------------------------------------------------------------\n");
485     printf("LDC = %d\n", GetLDCId());
486
487     printf("equipment:\n"
488            " size = %d  type = %d  id = %d\n",
489            GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
490     if (GetEquipmentAttributes()) {
491       printf(" attributes = %8.8x %8.8x %8.8x  ", GetEquipmentAttributes()[2],
492              GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
493     } else {
494       printf(" attributes = -------- -------- --------  ");
495     }
496     printf("element size = %d\n", GetEquipmentElementSize());
497
498     printf("data header:\n"
499            " size = %d  version = %d  valid = %d  compression = %d\n",
500            GetDataSize(), GetVersion(), IsValid(), IsCompressed());
501
502     printf("\n");
503     if (limit == 0) continue;
504
505     Int_t size = GetDataSize();
506     char line[70];
507     for (Int_t i = 0; i < 70; i++) line[i] = ' ';
508     line[69] = '\0';
509     Int_t pos = 0;
510     Int_t max = 16;
511     UChar_t byte;
512
513     for (Int_t n = 0; n < size; n++) {
514       if (!ReadNextChar(byte)) {
515         Error("DumpData", "couldn't read byte number %d\n", n);
516         break;
517       }
518       if (pos >= max) {
519         printf("%8.8x  %s\n", n-pos, line);
520         for (Int_t i = 0; i < 70; i++) line[i] = ' ';
521         line[69] = '\0';
522         pos = 0;
523         if ((limit > 0) && (n/max == limit)) {
524           Int_t nContinue = ((size-1)/max+1-limit) * max;
525           if (nContinue > n) {
526             printf(" [skipping %d bytes]\n", nContinue-n);
527             n = nContinue-1;
528             continue;
529           }
530         }
531       }
532       Int_t offset = pos/4;
533       if ((byte > 0x20) && (byte < 0x7f)) {
534         line[pos+offset] = byte;
535       } else {
536         line[pos+offset] = '.';
537       }
538       char hex[3];
539       sprintf(hex, "%2.2x", byte);
540       line[max+max/4+3+2*pos+offset] = hex[0];
541       line[max+max/4+4+2*pos+offset] = hex[1];
542       pos++;
543     }
544
545     if (pos > 0) printf("%8.8x  %s\n", size-pos, line);
546     printf("\n");
547            
548   } while (ReadHeader());
549 }
550
551 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
552                                Int_t code,
553                                const char *message)
554 {
555   // Add a raw data error message to the list
556   // of raw-data decoding errors
557   if (fEventNumber < 0) {
558     AliError("No events have read so far! Impossible to add a raw data error log!");
559     return;
560   }
561   Int_t ddlId = GetEquipmentId();
562   if (ddlId < 0) {
563     AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
564     return;
565   }
566
567   Int_t prevEventNumber = -1;
568   Int_t prevDdlId = -1;
569   Int_t prevErrorCode = -1;
570   AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
571   if (prevLog) {
572     prevEventNumber = prevLog->GetEventNumber();
573     prevDdlId       = prevLog->GetDdlID();
574     prevErrorCode   = prevLog->GetErrorCode();
575   }
576
577   if ((prevEventNumber != fEventNumber) ||
578       (prevDdlId != ddlId) ||
579       (prevErrorCode != code)) {
580     new (fErrorLogs[fErrorLogs.GetEntriesFast()])
581       AliRawDataErrorLog(fEventNumber,
582                          ddlId,
583                          level,
584                          code,
585                          message);
586   }
587   else
588     if (prevLog) prevLog->AddCount();
589
590 }