#include "AliVMERawStream.h"
#include "AliRawReader.h"
ClassImp(AliVMERawStream)
AliVMERawStream::AliVMERawStream(AliRawReader* rawReader) :
fRawReader(rawReader),
fData(0),
fNChannels(-1),
fBlock(0),
fNumber(0),
fChannel(0),
fValue(0),
fTime(0),
fTimeMuSec(0)
{
fRawReader = rawReader;
ReadTDC();
ReadTime();
fRawReader->Reset();
fRawReader->SelectEquipment(551, 38, 38);
}
Bool_t AliVMERawStream::Next()
{
if (fNChannels == -1) {
if (!fRawReader->ReadNextInt(fData)) return kFALSE;
if (!CheckString("V551")) return kFALSE;
fNChannels = 0;
}
while (fNChannels == 0) {
if (!fRawReader->ReadNextInt(fData)) {
Error("Next", "incomplete equipment");
return kFALSE;
}
const char* v551 = "v551";
if (fData == *((UInt_t*) v551)) return kFALSE;
if (!CheckString("V550")) return kFALSE;
if (!fRawReader->ReadNextShort(fBlock)) {
Error("Next", "incomplete equipment");
return kFALSE;
}
if (!fRawReader->ReadNextShort(fNumber)) {
Error("Next", "incomplete equipment");
return kFALSE;
}
if (!fRawReader->ReadNextInt((UInt_t&) fNChannels)) {
Error("Next", "incomplete equipment");
return kFALSE;
}
}
if (!fRawReader->ReadNextInt(fData)) {
Error("Next", "incomplete equipment");
return kFALSE;
}
fChannel = (fData >> 12) & 0x03ff;
fValue = fData & 0x0fff;
fNChannels--;
return kTRUE;
}
Bool_t AliVMERawStream::CheckString(const char* str) const
{
if (fData != *((UInt_t*) str)) {
char strData[5];
memcpy(strData, &fData, 4);
strData[4] = 0;
Error("CheckString", "invalid %s string (%s)", str, strData);
return kFALSE;
}
return kTRUE;
}
Bool_t AliVMERawStream::ReadTDC()
{
fRawReader->Reset();
fRawReader->SelectEquipment(775, 72, 72);
if (!fRawReader->ReadNextInt(fData)) return kFALSE;
if (!CheckString("V775")) return kFALSE;
if (!fRawReader->ReadNextInt(fData)) {
Error("ReadTDC", "incomplete TDC equipment");
return kFALSE;
}
if ((fData & 0x02000000) == 0) {
Error("ReadTDC", "invalid header: 0x%x", fData);
return kFALSE;
}
Int_t nTDC = fRawReader->GetDataSize() / 4 - 4;
if ( nTDC != 3 ) {
Error("ReadTDC", "wrong number of TDC channels: %d", nTDC);
return kFALSE;
}
for (Int_t i = 0; i < fgkNTDC; i++) {
if (!fRawReader->ReadNextInt(fData)) {
Error("ReadTDC", "incomplete TDC equipment");
return kFALSE;
}
if (fData & 0x07000000) {
Warning("ReadTDC", "bad TDC data: %x", fData);
}
if ((fData & 0x00004000) == 0) {
Warning("ReadTDC", "TDC data not valid: %x", fData);
}
if (fData & 0x00002000) {
Warning("ReadTDC", "TDC data underflow: %x", fData);
}
if (fData & 0x00001000) {
Warning("ReadTDC", "TDC data overflow: %x", fData);
}
fTDCChannel[i] = (fData >> 16) & 0x1f;
fTDCValue[i] = fData & 0x0fff;
}
if (!fRawReader->ReadNextInt(fData)) {
Error("ReadTDC", "incomplete TDC equipment");
return kFALSE;
}
if ((fData & 0x04000000) == 0) {
Error("ReadTDC", "invalid counter: 0x%x", fData);
return kFALSE;
}
if (!fRawReader->ReadNextInt(fData)) {
Error("ReadTDC", "incomplete TDC equipment");
return kFALSE;
}
if (!CheckString("v775")) return kFALSE;
return kTRUE;
}
Bool_t AliVMERawStream::ReadTime()
{
fRawReader->Reset();
fRawReader->SelectEquipment(1970, 0x12345678, 0x12345678);
if (!fRawReader->ReadNextInt(fData)) return kFALSE;
if (!CheckString("TIME")) return kFALSE;
if (!fRawReader->ReadNextInt(fTime)) {
Error("ReadTime", "incomplete time equipment");
return kFALSE;
}
if (!fRawReader->ReadNextInt(fTimeMuSec)) {
Error("ReadTime", "incomplete time equipment");
return kFALSE;
}
if (!fRawReader->ReadNextInt(fData)) {
Error("ReadTime", "incomplete time equipment");
return kFALSE;
}
if (!CheckString("time")) return kFALSE;
return kTRUE;
}