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