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