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