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