]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReader.cxx
update toy emcal trigger
[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 #include <TInterpreter.h>
43 #include <TSystem.h>
44 #include <TPRegexp.h>
45 #include <THashList.h>
46
47 #include <Riostream.h>
48 #include "AliRawReader.h"
49 #include "AliRawReaderFile.h"
50 #include "AliRawReaderDate.h"
51 #include "AliRawReaderRoot.h"
52 #include "AliRawReaderChain.h"
53 #include "AliDAQ.h"
54 #include "AliLog.h"
55
56 using std::ifstream;
57 ClassImp(AliRawReader)
58
59
60 AliRawReader::AliRawReader() :
61   fEquipmentIdsIn(NULL),
62   fEquipmentIdsOut(NULL),
63   fRequireHeader(kTRUE),
64   fHeader(NULL),
65   fHeaderV3(NULL),
66   fCount(0),
67   fSelectEquipmentType(-1),
68   fSelectMinEquipmentId(-1),
69   fSelectMaxEquipmentId(-1),
70   fSkipInvalid(kFALSE),
71   fSelectEventType(-1),
72   fSelectTriggerMask(0),
73   fSelectTriggerExpr(),
74   fErrorCode(0),
75   fEventNumber(-1),
76   fErrorLogs("AliRawDataErrorLog",100),
77   fHeaderSwapped(NULL),
78   fHeaderSwappedV3(NULL),
79   fIsValid(kTRUE),
80   fIsTriggerClassLoaded(kFALSE)
81 {
82 // default constructor: initialize data members
83 // Allocate the swapped header in case of Mac
84 #ifndef R__BYTESWAP
85   fHeaderSwapped=new AliRawDataHeader();
86   fHeaderSwappedV3=new AliRawDataHeaderV3();
87 #endif
88 }
89
90 Bool_t AliRawReader::LoadEquipmentIdsMap(const char *fileName)
91 {
92   // Open the mapping file
93   // and load the mapping data
94   ifstream input(fileName);
95   if (input.is_open()) {
96     Warning("AliRawReader","Equipment ID mapping file is found !");
97     const Int_t kMaxDDL = 256;
98     fEquipmentIdsIn = new TArrayI(kMaxDDL);
99     fEquipmentIdsOut = new TArrayI(kMaxDDL);
100     Int_t equipIn, equipOut;
101     Int_t nIds = 0;
102     while (input >> equipIn >> equipOut) {
103       if (nIds >= kMaxDDL) {
104         Error("AliRawReader","Too many equipment Id mappings found ! Truncating the list !");
105         break;
106       }
107       fEquipmentIdsIn->AddAt(equipIn,nIds); 
108       fEquipmentIdsOut->AddAt(equipOut,nIds);
109       nIds++;
110     }
111     fEquipmentIdsIn->Set(nIds);
112     fEquipmentIdsOut->Set(nIds);
113     input.close();
114     return kTRUE;
115   }
116   else {
117     Error("AliRawReader","equipment id map file is not found ! Skipping the mapping !");
118     return kFALSE;
119   }
120 }
121
122 AliRawReader::AliRawReader(const AliRawReader& rawReader) :
123   TObject(rawReader),
124   fEquipmentIdsIn(rawReader.fEquipmentIdsIn),
125   fEquipmentIdsOut(rawReader.fEquipmentIdsOut),
126   fRequireHeader(rawReader.fRequireHeader),
127   fHeader(rawReader.fHeader),
128   fHeaderV3(rawReader.fHeaderV3),
129   fCount(rawReader.fCount),
130   fSelectEquipmentType(rawReader.fSelectEquipmentType),
131   fSelectMinEquipmentId(rawReader.fSelectMinEquipmentId),
132   fSelectMaxEquipmentId(rawReader.fSelectMaxEquipmentId),
133   fSkipInvalid(rawReader.fSkipInvalid),
134   fSelectEventType(rawReader.fSelectEventType),
135   fSelectTriggerMask(rawReader.fSelectTriggerMask),
136   fSelectTriggerExpr(rawReader.fSelectTriggerExpr),
137   fErrorCode(0),
138   fEventNumber(-1),
139   fErrorLogs("AliRawDataErrorLog",100),
140   fHeaderSwapped(NULL),
141   fHeaderSwappedV3(NULL),
142   fIsValid(rawReader.fIsValid),
143   fIsTriggerClassLoaded(rawReader.fIsTriggerClassLoaded)
144 {
145 // copy constructor
146 // Allocate the swapped header in case of Mac
147 #ifndef R__BYTESWAP
148   fHeaderSwapped=new AliRawDataHeader(*rawReader.fHeaderSwapped);
149   fHeaderSwappedV3=new AliRawDataHeader(*rawReader.fHeaderSwappedV3);
150 #endif
151 }
152
153 AliRawReader& AliRawReader::operator = (const AliRawReader& rawReader)
154 {
155 // assignment operator
156   if(&rawReader == this) return *this;
157   fEquipmentIdsIn = rawReader.fEquipmentIdsIn;
158   fEquipmentIdsOut = rawReader.fEquipmentIdsOut;
159
160   fHeader = rawReader.fHeader;
161   fHeaderV3 = rawReader.fHeaderV3;
162   fCount = rawReader.fCount;
163
164   fSelectEquipmentType = rawReader.fSelectEquipmentType;
165   fSelectMinEquipmentId = rawReader.fSelectMinEquipmentId;
166   fSelectMaxEquipmentId = rawReader.fSelectMaxEquipmentId;
167   fSkipInvalid = rawReader.fSkipInvalid;
168   fSelectEventType = rawReader.fSelectEventType;
169   fSelectTriggerMask = rawReader.fSelectTriggerMask;
170   fSelectTriggerExpr = rawReader.fSelectTriggerExpr;
171
172   fErrorCode = rawReader.fErrorCode;
173
174   fEventNumber = rawReader.fEventNumber;
175   fErrorLogs = *((TClonesArray*)rawReader.fErrorLogs.Clone());
176
177   fIsValid = rawReader.fIsValid;
178   fIsTriggerClassLoaded = rawReader.fIsTriggerClassLoaded;
179
180   return *this;
181 }
182
183 AliRawReader::~AliRawReader()
184 {
185   // destructor
186   // delete the mapping arrays if
187   // initialized
188   if (fEquipmentIdsIn) delete fEquipmentIdsIn;
189   if (fEquipmentIdsOut) delete fEquipmentIdsOut;
190   fErrorLogs.Delete();
191   if (fHeaderSwapped) delete fHeaderSwapped;
192   if (fHeaderSwappedV3) delete fHeaderSwappedV3;
193 }
194
195 AliRawReader* AliRawReader::Create(const char *uri)
196 {
197   // RawReader's factory
198   // It instantiate corresponding raw-reader implementation class object
199   // depending on the URI provided
200   // Normal URIs point to files, while the URI starting with
201   // 'mem://:' or 'mem://<filename>' will create
202   // AliRawReaderDateOnline object which is supposed to be used
203   // in the online reconstruction
204
205   TString strURI = uri;
206
207   if (strURI.IsNull()) {
208     AliWarningClass("No raw-reader created");
209     return NULL;
210   }
211
212   TObjArray *fields = strURI.Tokenize("?");
213   TString &fileURI = ((TObjString*)fields->At(0))->String();
214
215   AliRawReader *rawReader = NULL;
216   if (fileURI.BeginsWith("mem://") || fileURI.BeginsWith("^")) {
217     if (fileURI.BeginsWith("mem://")) fileURI.ReplaceAll("mem://","");
218     AliInfoClass(Form("Creating raw-reader in order to read events in shared memory (option=%s)",fileURI.Data()));
219
220     TPluginManager* pluginManager = gROOT->GetPluginManager();
221     TString rawReaderName = "AliRawReaderDateOnline";
222     TPluginHandler* pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
223     // if not, add a plugin for it
224     if (!pluginHandler) {
225       pluginManager->AddHandler("AliRawReader", "online", 
226                                 "AliRawReaderDateOnline", "RAWDatarecOnline", "AliRawReaderDateOnline(const char*)");
227       pluginHandler = pluginManager->FindHandler("AliRawReader", "online");
228     }
229     if (pluginHandler && (pluginHandler->LoadPlugin() == 0)) {
230       rawReader = (AliRawReader*)pluginHandler->ExecPlugin(1,fileURI.Data());
231     }
232     else {
233       delete fields;
234       return NULL;
235     }
236   }
237   else if (fileURI.BeginsWith("amore://")) {
238     // A special raw-data URL used in case
239     // the raw-data reading is steered from
240     // ouside, i.e. from AMORE
241     fileURI.ReplaceAll("amore://","");
242     AliInfoClass("Creating raw-reader in order to read events sent by AMORE");
243     rawReader = new AliRawReaderDate((void *)NULL);
244   }
245   else if (fileURI.BeginsWith("collection://")) {
246     fileURI.ReplaceAll("collection://","");
247     AliInfoClass(Form("Creating raw-reader in order to read raw-data files collection defined in %s",fileURI.Data()));
248     rawReader = new AliRawReaderChain(fileURI);
249   }
250   else if (fileURI.BeginsWith("raw://run")) {
251     fileURI.ReplaceAll("raw://run","");
252     if (fileURI.IsDigit()) {
253       rawReader = new AliRawReaderChain(fileURI.Atoi());
254     }
255     else {
256       AliErrorClass(Form("Invalid syntax: %s",fileURI.Data()));
257       delete fields;
258       return NULL;
259     }
260   }
261   else {
262     AliInfoClass(Form("Creating raw-reader in order to read raw-data file: %s",fileURI.Data()));
263     TString filename(gSystem->ExpandPathName(fileURI.Data()));
264     if (filename.EndsWith("/")) {
265       rawReader = new AliRawReaderFile(filename);
266     } else if (filename.EndsWith(".root")) {
267       rawReader = new AliRawReaderRoot(filename);
268     } else {
269       rawReader = new AliRawReaderDate(filename);
270     }
271   }
272
273   if (!rawReader->IsRawReaderValid()) {
274     AliErrorClass(Form("Raw-reader is invalid - check the input URI (%s)",fileURI.Data()));
275     delete rawReader;
276     delete fields;
277     return NULL;
278   }
279
280   // Now apply event selection criteria (if specified)
281   if (fields->GetEntries() > 1) {
282     Int_t eventType = -1;
283     ULong64_t triggerMask = 0;
284     TString triggerExpr;
285     for(Int_t i = 1; i < fields->GetEntries(); i++) {
286       if (!fields->At(i)) continue;
287       TString &option = ((TObjString*)fields->At(i))->String();
288       if (option.BeginsWith("EventType=",TString::kIgnoreCase)) {
289         option.ReplaceAll("EventType=","");
290         eventType = option.Atoi();
291         continue;
292       }
293       if (option.BeginsWith("Trigger=",TString::kIgnoreCase)) {
294         option.ReplaceAll("Trigger=","");
295         if (option.IsDigit()) {
296           triggerMask = option.Atoll();
297         }
298         else {
299           triggerExpr = option.Data();
300         }
301         continue;
302       }
303       AliWarningClass(Form("Ignoring invalid event selection option: %s",option.Data()));
304     }
305     AliInfoClass(Form("Event selection criteria specified:   eventype=%d   trigger mask=%llx   trigger expression=%s",
306                  eventType,triggerMask,triggerExpr.Data()));
307     rawReader->SelectEvents(eventType,triggerMask,triggerExpr.Data());
308   }
309
310   delete fields;
311
312   return rawReader;
313 }
314
315 Int_t AliRawReader::GetMappedEquipmentId() const
316 {
317   if (!fEquipmentIdsIn || !fEquipmentIdsOut) {
318     Error("AliRawReader","equipment Ids mapping is not initialized !");
319     return GetEquipmentId();
320   }
321   Int_t equipmentId = GetEquipmentId();
322   for(Int_t iId = 0; iId < fEquipmentIdsIn->GetSize(); iId++) {
323     if (equipmentId == fEquipmentIdsIn->At(iId)) {
324       equipmentId = fEquipmentIdsOut->At(iId);
325       break;
326     }
327   }
328   return equipmentId;
329 }
330
331 Int_t AliRawReader::GetDetectorID() const
332 {
333   // Get the detector ID
334   // The list of detector IDs
335   // can be found in AliDAQ.h
336   Int_t equipmentId;
337   if (fEquipmentIdsIn && fEquipmentIdsIn)
338     equipmentId = GetMappedEquipmentId();
339   else
340     equipmentId = GetEquipmentId();
341
342   if (equipmentId >= 0) {
343     Int_t ddlIndex;
344     return AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
345   }
346   else
347     return -1;
348 }
349
350 Int_t AliRawReader::GetDDLID() const
351 {
352   // Get the DDL ID (within one sub-detector)
353   // The list of detector IDs
354   // can be found in AliDAQ.h
355   Int_t equipmentId;
356   if (fEquipmentIdsIn && fEquipmentIdsIn)
357     equipmentId = GetMappedEquipmentId();
358   else
359     equipmentId = GetEquipmentId();
360
361   if (equipmentId >= 0) {
362     Int_t ddlIndex;
363     AliDAQ::DetectorIDFromDdlID(equipmentId,ddlIndex);
364     return ddlIndex;
365   }
366   else
367     return -1;
368 }
369
370 void AliRawReader::Select(const char *detectorName, Int_t minDDLID, Int_t maxDDLID)
371 {
372 // read only data of the detector with the given name and in the given
373 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
374 // no selection is applied if a value < 0 is used.
375   Int_t detectorID = AliDAQ::DetectorID(detectorName);
376   if(detectorID >= 0)
377     Select(detectorID,minDDLID,maxDDLID);
378 }
379
380 void AliRawReader::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
381 {
382 // read only data of the detector with the given ID and in the given
383 // range of DDLs (minDDLID <= DDLID <= maxDDLID).
384 // no selection is applied if a value < 0 is used.
385
386   fSelectEquipmentType = -1;
387
388   if (minDDLID < 0)
389     fSelectMinEquipmentId = AliDAQ::DdlIDOffset(detectorID);
390   else
391     fSelectMinEquipmentId = AliDAQ::DdlID(detectorID,minDDLID);
392
393   if (maxDDLID < 0)
394     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,AliDAQ::NumberOfDdls(detectorID)-1);
395   else
396     fSelectMaxEquipmentId = AliDAQ::DdlID(detectorID,maxDDLID);
397 }
398
399 void AliRawReader::SelectEquipment(Int_t equipmentType, 
400                                    Int_t minEquipmentId, Int_t maxEquipmentId)
401 {
402 // read only data of the equipment with the given type and in the given
403 // range of IDs (minEquipmentId <= EquipmentId <= maxEquipmentId).
404 // no selection is applied if a value < 0 is used.
405
406   fSelectEquipmentType = equipmentType;
407   fSelectMinEquipmentId = minEquipmentId;
408   fSelectMaxEquipmentId = maxEquipmentId;
409 }
410
411 void AliRawReader::SelectEvents(Int_t type, ULong64_t triggerMask,
412                                 const char *triggerExpr)
413 {
414 // read only events with the given type and optionally
415 // trigger mask.
416 // no selection is applied if value = 0 is used.
417 // Trigger selection can be done via string (triggerExpr)
418 // which defines the trigger logic to be used. It works only
419 // after LoadTriggerClass() method is called for all involved
420 // trigger classes.
421
422   fSelectEventType = type;
423   fSelectTriggerMask = triggerMask;
424   if (triggerExpr) fSelectTriggerExpr = triggerExpr;
425 }
426
427 void AliRawReader::LoadTriggerClass(const char* name, Int_t index)
428 {
429   // Loads the list of trigger classes defined.
430   // Used in conjunction with IsEventSelected in the
431   // case when the trigger selection is given by
432   // fSelectedTriggerExpr
433
434   if (fSelectTriggerExpr.IsNull()) return;
435
436   fIsTriggerClassLoaded = kTRUE;
437
438   if (index >= 0)
439     fSelectTriggerExpr.ReplaceAll(name,Form("[%d]",index));
440   else
441     fSelectTriggerExpr.ReplaceAll(name,"0");
442 }
443
444 void AliRawReader::LoadTriggerAlias(const THashList *lst)
445 {
446   // Loads the list of trigger aliases defined.
447   // Replaces the alias by the OR of the triggers included in it.
448   // The subsiquent call to LoadTriggerClass is needed
449   // to obtain the final expression in
450   // fSelectedTriggerExpr
451
452   if (fSelectTriggerExpr.IsNull()) return;
453
454   // Make a THashList alias -> trigger classes
455
456   THashList alias2trig;
457   TIter iter(lst);
458   TNamed *nmd = 0;
459
460   // Loop on triggers
461
462   while((nmd = dynamic_cast<TNamed*>(iter.Next()))){
463
464     TString aliasList(nmd->GetTitle());
465     TObjArray* arrAliases = aliasList.Tokenize(',');
466     Int_t nAliases = arrAliases->GetEntries();
467
468     // Loop on aliases for the current trigger
469     for(Int_t i=0; i<nAliases; i++){
470
471       TObjString *alias = (TObjString*) arrAliases->At(i);
472
473       // Find the current alias in the hash list. If it is not there, add TNamed entry
474       TNamed * inlist = (TNamed*)alias2trig.FindObject((alias->GetString()).Data());
475       if (!inlist) {
476         inlist = new TNamed((alias->GetString()).Data(),nmd->GetName());
477         alias2trig.Add(inlist);
478       }
479       else {
480         TString tt(inlist->GetTitle());
481         tt += " || ";
482         tt += nmd->GetName();
483         inlist->SetTitle(tt.Data());
484       }
485     }
486     
487     delete arrAliases;
488   }
489   alias2trig.Sort(kSortDescending);
490
491   // Replace all the aliases by the OR of triggers
492   TIter iter1(&alias2trig);
493   while((nmd = dynamic_cast<TNamed*>(iter1.Next()))){
494     fSelectTriggerExpr.ReplaceAll(nmd->GetName(),nmd->GetTitle());
495   }
496 }
497
498 Bool_t AliRawReader::IsSelected() const
499 {
500 // apply the selection (if any)
501
502   if (fSkipInvalid && !IsValid()) return kFALSE;
503
504   if (fSelectEquipmentType >= 0)
505     if (GetEquipmentType() != fSelectEquipmentType) return kFALSE;
506
507   Int_t equipmentId;
508   if (fEquipmentIdsIn && fEquipmentIdsIn)
509     equipmentId = GetMappedEquipmentId();
510   else
511     equipmentId = GetEquipmentId();
512
513   if ((fSelectMinEquipmentId >= 0) && 
514       (equipmentId < fSelectMinEquipmentId))
515     return kFALSE;
516   if ((fSelectMaxEquipmentId >= 0) && 
517       (equipmentId > fSelectMaxEquipmentId))
518     return kFALSE;
519
520   return kTRUE;
521 }
522
523 Bool_t AliRawReader::IsEventSelected() const
524 {
525   // apply the event selection (if any)
526
527   // First check the event type
528   if (fSelectEventType >= 0) {
529     if (GetType() != (UInt_t) fSelectEventType) return kFALSE;
530   }
531
532   // Then check the trigger pattern and compared it
533   // to the required trigger mask
534   if (fSelectTriggerMask != 0) {
535     if ((GetClassMask() & fSelectTriggerMask) != fSelectTriggerMask) return kFALSE;
536   }
537
538   if (  fIsTriggerClassLoaded && !fSelectTriggerExpr.IsNull()) {
539     TString expr(fSelectTriggerExpr);
540     ULong64_t mask = GetClassMask();
541     for(Int_t itrigger = 0; itrigger < 50; itrigger++) {
542       if (mask & ((ULong64_t)1 << itrigger)) {
543         expr.ReplaceAll(Form("[%d]",itrigger),"1");
544       }
545       else {
546         expr.ReplaceAll(Form("[%d]",itrigger),"0");
547       }
548     }
549     // Possibility to introduce downscaling
550     TPRegexp("(%\\s*\\d+)").Substitute(expr,Form("&& !(%d$1)",GetEventIndex()),"g");
551     Int_t error;
552     Bool_t result = gROOT->ProcessLineFast(expr.Data(),&error);
553     if ( error == TInterpreter::kNoError)
554       return result;
555     else
556       return kFALSE;
557   }
558
559   return kTRUE;
560 }
561
562 UInt_t AliRawReader::SwapWord(UInt_t x) const
563 {
564    // Swap the endianess of the integer value 'x'
565
566    return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
567            ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
568 }
569
570 UShort_t AliRawReader::SwapShort(UShort_t x) const
571 {
572    // Swap the endianess of the short value 'x'
573
574    return (((x & 0x00ffU) <<  8) | ((x & 0xff00U) >>  8)) ;
575 }
576
577 Bool_t AliRawReader::ReadNextInt(UInt_t& data)
578 {
579 // reads the next 4 bytes at the current position
580 // returns kFALSE if the data could not be read
581
582   while (fCount == 0) {
583     if (!ReadHeader()) return kFALSE;
584   }
585   if (fCount < (Int_t) sizeof(data)) {
586     Error("ReadNextInt", 
587           "too few data left (%d bytes) to read an UInt_t!", fCount);
588     return kFALSE;
589   }
590   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
591     Error("ReadNextInt", "could not read data!");
592     return kFALSE;
593   }
594 #ifndef R__BYTESWAP
595   data=SwapWord(data);
596 #endif
597   return kTRUE;
598 }
599
600 Bool_t AliRawReader::ReadNextShort(UShort_t& data)
601 {
602 // reads the next 2 bytes at the current position
603 // returns kFALSE if the data could not be read
604
605   while (fCount == 0) {
606     if (!ReadHeader()) return kFALSE;
607   }
608   if (fCount < (Int_t) sizeof(data)) {
609     Error("ReadNextShort", 
610           "too few data left (%d bytes) to read an UShort_t!", fCount);
611     return kFALSE;
612   }
613   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
614     Error("ReadNextShort", "could not read data!");
615     return kFALSE;
616   }
617 #ifndef R__BYTESWAP
618   data=SwapShort(data);
619 #endif
620   return kTRUE;
621 }
622
623 Bool_t AliRawReader::ReadNextChar(UChar_t& data)
624 {
625 // reads the next 1 byte at the current stream position
626 // returns kFALSE if the data could not be read
627
628   while (fCount == 0) {
629     if (!ReadHeader()) return kFALSE;
630   }
631   if (!ReadNext((UChar_t*) &data, sizeof(data))) {
632     Error("ReadNextChar", "could not read data!");
633     return kFALSE;
634   }
635   return kTRUE;
636 }
637
638 Bool_t  AliRawReader::GotoEvent(Int_t event)
639 {
640   // Random access to certain
641   // event index. Could be very slow
642   // for some non-root raw-readers.
643   // So it should be reimplemented there.
644   if (event < fEventNumber) RewindEvents();
645
646   while (fEventNumber < event) {
647     if (!NextEvent()) return kFALSE;
648   }
649
650   return kTRUE;
651 }
652
653 Int_t AliRawReader::CheckData() const
654 {
655 // check the consistency of the data
656 // derived classes should overwrite the default method which returns 0 (no err)
657
658   return 0;
659 }
660
661
662 void AliRawReader::DumpData(Int_t limit)
663 {
664 // print the raw data
665 // if limit is not negative, only the first and last "limit" lines of raw data
666 // are printed
667
668   Reset();
669   if (!ReadHeader()) {
670     Error("DumpData", "no header");
671     return;
672   }
673   printf("header:\n"
674          " type = %d  run = %d  ", GetType(), GetRunNumber());
675   if (GetEventId()) {
676     printf("event = %8.8x %8.8x\n", GetEventId()[1], GetEventId()[0]);
677   } else {
678     printf("event = -------- --------\n");
679   }
680   if (GetTriggerPattern()) {
681     printf(" trigger = %8.8x %8.8x  ",
682            GetTriggerPattern()[1], GetTriggerPattern()[0]);
683   } else {
684     printf(" trigger = -------- --------  ");
685   }
686   if (GetDetectorPattern()) {
687     printf("detector = %8.8x\n", GetDetectorPattern()[0]);
688   } else {
689     printf("detector = --------\n");
690   }
691   if (GetAttributes()) {
692     printf(" attributes = %8.8x %8.8x %8.8x  ",
693            GetAttributes()[2], GetAttributes()[1], GetAttributes()[0]);
694   } else {
695     printf(" attributes = -------- -------- --------  ");
696   }
697   printf("GDC = %d\n", GetGDCId());
698   printf("\n");
699
700   do {
701     printf("-------------------------------------------------------------------------------\n");
702     printf("LDC = %d\n", GetLDCId());
703
704     printf("equipment:\n"
705            " size = %d  type = %d  id = %d\n",
706            GetEquipmentSize(), GetEquipmentType(), GetEquipmentId());
707     if (GetEquipmentAttributes()) {
708       printf(" attributes = %8.8x %8.8x %8.8x  ", GetEquipmentAttributes()[2],
709              GetEquipmentAttributes()[1], GetEquipmentAttributes()[0]);
710     } else {
711       printf(" attributes = -------- -------- --------  ");
712     }
713     printf("element size = %d\n", GetEquipmentElementSize());
714
715     printf("data header:\n"
716            " size = %d  version = %d  valid = %d  compression = %d\n",
717            GetDataSize(), GetVersion(), IsValid(), IsCompressed());
718
719     printf("\n");
720     if (limit == 0) continue;
721
722     Int_t size = GetDataSize();
723     char line[70];
724     for (Int_t i = 0; i < 70; i++) line[i] = ' ';
725     line[69] = '\0';
726     Int_t pos = 0;
727     Int_t max = 16;
728     UChar_t byte;
729
730     for (Int_t n = 0; n < size; n++) {
731       if (!ReadNextChar(byte)) {
732         Error("DumpData", "couldn't read byte number %d\n", n);
733         break;
734       }
735       if (pos >= max) {
736         printf("%8.8x  %s\n", n-pos, line);
737         for (Int_t i = 0; i < 70; i++) line[i] = ' ';
738         line[69] = '\0';
739         pos = 0;
740         if ((limit > 0) && (n/max == limit)) {
741           Int_t nContinue = ((size-1)/max+1-limit) * max;
742           if (nContinue > n) {
743             printf(" [skipping %d bytes]\n", nContinue-n);
744             n = nContinue-1;
745             continue;
746           }
747         }
748       }
749       Int_t offset = pos/4;
750       if ((byte > 0x20) && (byte < 0x7f)) {
751         line[pos+offset] = byte;
752       } else {
753         line[pos+offset] = '.';
754       }
755       char hex[3];
756       snprintf(hex, 3, "%2.2x", byte);
757       line[max+max/4+3+2*pos+offset] = hex[0];
758       line[max+max/4+4+2*pos+offset] = hex[1];
759       pos++;
760     }
761
762     if (pos > 0) printf("%8.8x  %s\n", size-pos, line);
763     printf("\n");
764            
765   } while (ReadHeader());
766 }
767
768 void AliRawReader::AddErrorLog(AliRawDataErrorLog::ERawDataErrorLevel level,
769                                Int_t code,
770                                const char *message)
771 {
772   // Add a raw data error message to the list
773   // of raw-data decoding errors
774   if (fEventNumber < 0) {
775     return;
776   }
777   Int_t ddlId = GetEquipmentId();
778   if (ddlId < 0) {
779     AliError("No ddl raw data have been read so far! Impossible to add a raw data error log!");
780     return;
781   }
782
783   Int_t prevEventNumber = -1;
784   Int_t prevDdlId = -1;
785   Int_t prevErrorCode = -1;
786   AliRawDataErrorLog *prevLog = (AliRawDataErrorLog *)fErrorLogs.Last();
787   if (prevLog) {
788     prevEventNumber = prevLog->GetEventNumber();
789     prevDdlId       = prevLog->GetDdlID();
790     prevErrorCode   = prevLog->GetErrorCode();
791   }
792
793   if ((prevEventNumber != fEventNumber) ||
794       (prevDdlId != ddlId) ||
795       (prevErrorCode != code)) {
796     new (fErrorLogs[fErrorLogs.GetEntriesFast()])
797       AliRawDataErrorLog(fEventNumber,
798                          ddlId,
799                          level,
800                          code,
801                          message);
802   }
803   else
804     if (prevLog) prevLog->AddCount();
805
806 }
807
808 Bool_t AliRawReader::GotoEventWithID(Int_t event, 
809                                      UInt_t period,
810                                      UInt_t orbitID,
811                                      UShort_t bcID)
812 {
813   // Go to certain event number by
814   // checking the event ID.
815   // Useful in case event-selection
816   // is applied and the 'event' is
817   // relative
818   if (!GotoEvent(event)) return kFALSE;
819
820   while (GetBCID()    != period  ||
821          GetOrbitID() != orbitID ||
822          GetPeriod()  != bcID) {
823     if (!NextEvent()) return kFALSE;
824   }
825
826   return kTRUE;
827 }
828