#ifndef ALIITSRAWSTREAMSPD_H
#define ALIITSRAWSTREAMSPD_H
#include "AliRawReader.h"
#include "AliITSRawStream.h"
#include "AliITSRawStreamSPDErrorLog.h"
class AliITSRawStreamSPD: public AliITSRawStream {
public :
AliITSRawStreamSPD(AliRawReader* rawReader);
AliITSRawStreamSPD(const AliITSRawStreamSPD& rstream);
AliITSRawStreamSPD& operator=(const AliITSRawStreamSPD& rstream);
virtual ~AliITSRawStreamSPD() {};
virtual Bool_t Next();
virtual Int_t ReadCalibHeader();
Int_t GetColumn() const {return fCoord1;};
Int_t GetRow() const {return fCoord2;};
UShort_t GetHalfStaveNr() const {return fHalfStaveNr;}
UShort_t GetChipAddr() const {return fChipAddr;}
Int_t GetChipCol() const {return fCol;};
Int_t GetChipRow() const {return fRow;};
static Int_t GetModuleNumber(UInt_t iDDL, UInt_t iModule);
static Int_t GetModuleNumber(UInt_t iDDL, UInt_t iHS, UInt_t iChip)
{return GetOfflineModuleFromOnline(iDDL,iHS,iChip);}
static Bool_t OfflineToOnline(UInt_t module, UInt_t colM, UInt_t RowM, UInt_t& eq, UInt_t& hs, UInt_t& chip, UInt_t& col, UInt_t& row);
static Bool_t OnlineToOffline(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row, UInt_t& module, UInt_t& colM, UInt_t& rowM);
static UInt_t GetOnlineEqIdFromOffline(UInt_t module);
static UInt_t GetOnlineHSFromOffline(UInt_t module);
static UInt_t GetOnlineChipFromOffline(UInt_t module, UInt_t colM);
static UInt_t GetOnlineColFromOffline(UInt_t module, UInt_t colM);
static UInt_t GetOnlineRowFromOffline(UInt_t module, UInt_t rowM);
static UInt_t GetOfflineModuleFromOnline(UInt_t eqId, UInt_t hs, UInt_t chip);
static UInt_t GetOfflineChipKeyFromOnline(UInt_t eqId, UInt_t hs, UInt_t chip);
static UInt_t GetOfflineColFromOnline(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t col);
static UInt_t GetOfflineRowFromOnline(UInt_t eqId, UInt_t hs, UInt_t chip, UInt_t row);
Bool_t GetFastOrSignal(UInt_t eq, UInt_t hs, UInt_t chip);
Int_t GetEventCounter() const {return fEventCounter;}
Short_t GetEventCounterFullEq(UInt_t eq) const;
Short_t GetEventCounterFullHS(UInt_t eq, UInt_t hs) const;
Short_t GetEventCounterFullChip(UInt_t eq, UInt_t hs, UInt_t chip) const;
Bool_t IsEventCounterFullConsistent() const;
Bool_t IsActiveEq(UInt_t eq) const;
Bool_t IsActiveHS(UInt_t eq, UInt_t hs) const;
Bool_t IsActiveChip(UInt_t eq, UInt_t hs, UInt_t chip) const;
Bool_t GetHalfStavePresent(UInt_t hs);
Int_t GetHword(UInt_t index);
UInt_t GetHrouterNr() const {return (fCalHeadWord[0] & 0x0000003f);}
Bool_t GetHhalfStaveScanned(UInt_t hs) const;
UInt_t GetHtype() const {return (Int_t)((fCalHeadWord[1]) & 0x000000ff);}
Bool_t GetHdataFormat() const {return (Bool_t)(((fCalHeadWord[1]) & 0x00000100)>>8);}
UInt_t GetHtriggers() const {return fCalHeadWord[2];}
Bool_t GetHchipPresent(UInt_t hs, UInt_t chip) const;
UInt_t GetHdacStart() const {return ((fCalHeadWord[5]>>24) & 0x000000ff);}
UInt_t GetHdacEnd() const {return ((fCalHeadWord[5]>>16) & 0x000000ff);}
UInt_t GetHdacStep() const {return ((fCalHeadWord[5]>>8) & 0x000000ff);}
UInt_t GetHdacId() const {return ((fCalHeadWord[5]) & 0x000000ff);}
UInt_t GetHrowStart() const {return (UInt_t) ((fCalHeadWord[6]>>24) & 0x000000ff);}
UInt_t GetHrowEnd() const {return (UInt_t) ((fCalHeadWord[6]>>16) & 0x000000ff);}
UInt_t GetHrowValue() const {return (UInt_t) ((fCalHeadWord[6]>> 8) & 0x000000ff);}
UInt_t GetHrowSpan() const {return (UInt_t) ((fCalHeadWord[6]) & 0x000000ff);}
UInt_t GetHdacValue() const {return (Int_t) ((fCalHeadWord[6]) & 0x000000ff);}
UInt_t GetHdacHigh(UInt_t hs) const;
UInt_t GetHdacLow(UInt_t hs) const;
UInt_t GetHTPAmp(UInt_t hs) const;
Bool_t GetHminTHchipPresent(UInt_t chip) const;
UInt_t GetHglobalDBversion() const {return fCalHeadWord[16];}
UInt_t GetFOHrouterNr() const {return GetHrouterNr();}
UInt_t GetFOHtype() const {return GetHtype();}
UInt_t GetFOHtriggers() const {return GetHtriggers();}
Bool_t GetFOHchipPresent(UInt_t hs, UInt_t chip) const {return GetHchipPresent(hs,chip);}
UInt_t GetFOHglobalDBversion() const {return fCalHeadWord[5];}
UInt_t GetFOHMatrixID() const {return fCalHeadWord[6] & 0x0000000f;}
UInt_t GetFOHpixelCol() const {return (fCalHeadWord[6] >> 20) & 0x0000001f;}
UInt_t GetFOHpixelRow() const {return (fCalHeadWord[6] >> 10) & 0x000000ff;}
UInt_t GetFOHnumDacs() const;
UInt_t GetFOHdacIndex(UInt_t index) const;
UInt_t GetFOHdacValue(UInt_t index) const;
UInt_t GetFOHchipCount(UInt_t hs, UInt_t chip) const;
void ActivateAdvancedErrorLog(Bool_t activate, AliITSRawStreamSPDErrorLog* advLogger = NULL);
AliITSRawStreamSPDErrorLog* GetAdvancedErrorLog() {return fAdvLogger;}
static const Char_t* GetErrorName(UInt_t errorCode);
enum {kDDLsNumber = 20};
enum {kModulesPerDDL = 12};
enum {kCalHeadLenMax = 64};
enum ESPDRawStreamError {
kTotal,
kHeaderMissingErr,
kTrailerMissingErr,
kTrailerWithoutHeaderErr,
kHeaderCountErr,
kTrailerCountErr,
kFillUnexpectErr,
kFillMissingErr,
kWrongFillWordErr,
kNumberHitsErr,
kEventCounterErr,
kDDLNumberErr,
kHSNumberErr,
kChipAddrErr,
kCalHeaderLengthErr,
kAdvEventCounterErr,
kAdvEventCounterOrderErr,
kTrailerErrorBitErr,
kLinkRxDetectorFatalErr,
kTSMtriggerErr,
kHighMultiplicityFlag
};
private :
static const Int_t fgkDDLModuleMap[kDDLsNumber][kModulesPerDDL];
Bool_t ReadNextShort();
Bool_t ReadNextInt();
void NewEvent();
void CheckHeaderAndTrailerCount(Int_t ddlID);
Int_t fEventCounter;
Short_t fEventCounterFull[20][6][10];
UShort_t fChipAddr;
UShort_t fHalfStaveNr;
UInt_t fCol;
UInt_t fRow;
UInt_t fCalHeadWord[kCalHeadLenMax];
UInt_t fCalHeadLen;
UShort_t fData;
UInt_t fOffset;
UInt_t fHitCount;
UChar_t fDataChar1, fDataChar2, fDataChar3, fDataChar4;
Bool_t fFirstWord;
Bool_t fCalHeadRead[20];
UInt_t fPrevEventId;
UInt_t fEqPLBytesRead;
UInt_t fEqPLChipHeadersRead;
UInt_t fEqPLChipTrailersRead;
Bool_t fHeaderOrTrailerReadLast;
UInt_t fExpectedHeaderTrailerCount;
Bool_t fFillOutOfSynch;
Int_t fDDLID;
Int_t fLastDDLID;
Bool_t fAdvancedErrorLog;
AliITSRawStreamSPDErrorLog *fAdvLogger;
Bool_t fFastOrSignal[20][6][10];
Bool_t fActiveEq[20];
Bool_t fActiveHS[20][6];
Bool_t fActiveChip[20][6][10];
ClassDef(AliITSRawStreamSPD, 0)
};
#endif