#ifndef ALITOFRAWSTREAM_H
#define ALITOFRAWSTREAM_H
#include "TObject.h"
#include "AliTOFHitDataBuffer.h"
#include "AliTOFDecoder.h"
class AliTOFHitData;
class AliTOFDecoderV2;
#define FILLER 0x70000000
#define WORD_TYPE_MASK 0xf0000000
#define WORD_TYPE_POSITION 28
#define GLOBAL_HEADER 0x40000000
#define GLOBAL_TRAILER 0x50000000
#define HEADER_SLOT_ID_MASK 0x0000000f
#define HEADER_SLOT_ID_POSITION 0
#define GLOBAL_HEADER_TYPE 4
#define GLOBAL_TRAILER_TYPE 5
#define ERROR_TYPE 6
#define FILLER_TYPE 7
#define TRM_CHAIN0_HEADER_TYPE 0
#define TRM_CHAIN0_TRAILER_TYPE 1
#define TRM_CHAIN1_HEADER_TYPE 2
#define TRM_CHAIN1_TRAILER_TYPE 3
#define DRM_ID_NUMBER 1
#define LTM_ID_NUMBER 2
#define DRM_GLOBAL_HEADER 0x40000001
#define DRM_EVENT_WORDS_MASK 0x001ffff0
#define DRM_EVENT_WORDS_POSITION 4
#define DRM_DRM_ID_MASK 0x0fe00000
#define DRM_DRM_ID_POSITION 21
#define DRM_STATUS_HEADER_1 0x40000001
#define DRM_SLOT_ID_MASK 0x00007ff0
#define DRM_SLOT_ID_POSITION 4
#define DRM_C_BIT_MASK 0x00008000
#define DRM_C_BIT_POSITION 15
#define DRM_VERS_ID_MASK 0x001f0000
#define DRM_VERS_ID_POSITION 16
#define DRM_HEADER_SIZE_MASK 0x01e00000
#define DRM_HEADER_SIZE_POSITION 21
#define DRM_STATUS_HEADER_2 0x40000001
#define DRM_ENABLE_ID_MASK 0x00007ff0
#define DRM_ENABLE_ID_POSITION 4
#define DRM_ZERO_WORD2_MASK 0x00008000
#define DRM_ZERO_WORD2_POSITION 15
#define DRM_FAULT_ID_MASK 0x07ff0000
#define DRM_FAULT_ID_POSITION 16
#define DRM_RTO_BIT_MASK 0x08000000
#define DRM_RTO_BIT_POSITION 27
#define DRM_STATUS_HEADER_3 0x40000001
#define DRM_L0_BCID_MASK 0x0000fff0
#define DRM_L0_BCID_POSITION 4
#define DRM_RUNTIME_INFO_MASK 0x0fff0000
#define DRM_RUNTIME_INFO_POSITION 16
#define DRM_STATUS_HEADER_4 0x40000001
#define DRM_TEMPERATURE_MASK 0x00003ff0
#define DRM_TEMPERATURE_POSITION 4
#define DRM_ZERO_1_WORD4_MASK 0x00004000
#define DRM_ZERO_1_WORD4_POSITION 14
#define DRM_ACK_MASK 0x00008000
#define DRM_ACK_POSITION 15
#define DRM_SENS_AD_MASK 0x00070000
#define DRM_SENS_AD_POSITION 16
#define DRM_ZERO_2_WORD4_MASK 0x00080000
#define DRM_ZERO_2_WORD4_POSITION 19
#define DRM_EVENT_CRC_MASK 0x000ffff0
#define DRM_EVENT_CRC_POSITION 4
#define DRM_GLOBAL_TRAILER 0x50000001
#define DRM_LOCAL_EVENT_COUNTER_MASK 0x0000fff0
#define DRM_LOCAL_EVENT_COUNTER_POSITION 4
#define TRM_GLOBAL_HEADER 0x40000000
#define TRM_SLOT_ID_MASK 0x0000000f
#define TRM_SLOT_ID_POSITION 0
#define TRM_EVENT_WORDS_MASK 0x0001fff0
#define TRM_EVENT_WORDS_POSITION 4
#define TRM_ACQ_BITS_MASK 0x00060000
#define TRM_ACQ_BITS_POSITION 17
#define TRM_L_BIT_MASK 0x00080000
#define TRM_L_BIT_POSITION 19
#define TRM_CHAIN_0_HEADER 0x00000000
#define TRM_CHAIN_1_HEADER 0x20000000
#define TRM_BUNCH_ID_MASK 0x0000fff0
#define TRM_BUNCH_ID_POSITION 4
#define TRM_PB24_TEMP_MASK 0x00ff0000
#define TRM_PB24_TEMP_POSITION 16
#define TRM_PB24_ID_MASK 0x07000000
#define TRM_PB24_ID_POSITION 24
#define TRM_TS_BIT_MASK 0x08000000
#define TRM_TS_BIT_POSITION 27
#define TRM_CHAIN_0_TRAILER 0x10000000
#define TRM_CHAIN_1_TRAILER 0x30000000
#define TRM_STATUS_MASK 0x0000000f
#define TRM_STATUS_POSITION 0
#define TRM_TDC_DIGIT 0x8000000
#define TRM_DIGIT_TIME_MASK 0x00001fff
#define TRM_DIGIT_TIME_POSITION 0
#define TRM_LONG_DIGIT_TIME_MASK 0x001fffff
#define TRM_LONG_DIGIT_TIME_POSITION 0
#define TRM_TOT_WIDTH_MASK 0x001fe000
#define TRM_TOT_WIDTH_POSITION 13
#define TRM_CHAN_MASK 0x00e00000
#define TRM_CHAN_POSITION 21
#define TRM_TDC_ID_MASK 0x0f000000
#define TRM_TDC_ID_POSITION 24
#define TRM_E_BIT_MASK 0x10000000
#define TRM_E_BIT_POSITION 28
#define TRM_PS_BITS_MASK 0x60000000
#define TRM_PS_BITS_POSITION 29
#define TRM_FIRST_SLOT_ID 3
#define TIME_BIN_WIDTH 24.4e-3 //ns
#define TRM_TDC_ERROR 0x6000000
#define TRM_TDC_DIAGNOSTIC_ERROR 0x6f00000
#define TRM_TDC_ERROR_FLAGS_MASK 0x00007fff
#define TRM_TDC_ERROR_FLAGS_POSITION 0
#define TRM_TDC_ERROR_TDC_ID_MASK 0x0f00000
#define TRM_TDC_ERROR_TDC_ID_POSITION 24
#define TRM_TDC_ERROR_FAULT_CHIP_FLAG_ID_MASK 0x00007fff
#define TRM_TDC_ERROR_FAULT_CHIP_FLAG_ID_POSITION 0
#define TRM_TDC_ERROR_C_BIT_MASK 0x00008000
#define TRM_TDC_ERROR_C_BIT_POSITION 15
#define TRM_TDC_ERROR_JTAG_ERROR_CODE_MASK 0x000007ff
#define TRM_TDC_ERROR_JTAG_ERROR_CODE_POSITION 0
#define TRM_TDC_DIAGNOSTIC_ERROR_TDC_ID_MASK 0x00007800
#define TRM_TDC_DIAGNOSTIC_ERROR_TDC_ID_POSITION 11
#define TRM_GLOBAL_TRAILER 0x5000000f
#define TRM_EVENT_CRC_MASK 0x0000fff0
#define TRM_EVENT_CRC_POSITION 4
#define TRM_EVENT_COUNTER_MASK 0x0fff0000
#define TRM_EVENT_COUNTER_POSITION 16
#define LTM_GLOBAL_HEADER 0x40000002
#define LTM_EVENT_WORDS_MASK 0x0001fff0
#define LTM_EVENT_WORDS_POSITION 4
#define LTM_C_BIT_MASK 0x00020000
#define LTM_C_BIT_POSITION 17
#define LTM_FAULT_MASK 0x00fc0000
#define LTM_FAULT_POSITION 18
#define LTM_PDL_VALUE_1_MASK 0x000000ff
#define LTM_PDL_VALUE_1_POSITION 0
#define LTM_PDL_VALUE_2_MASK 0x0000ff00
#define LTM_PDL_VALUE_2_POSITION 8
#define LTM_PDL_VALUE_3_MASK 0x00ff0000
#define LTM_PDL_VALUE_3_POSITION 16
#define LTM_PDL_VALUE_4_MASK 0xff000000
#define LTM_PDL_VALUE_4_POSITION 24
#define LTM_ADC_VALUE_1_MASK 0x000003ff
#define LTM_ADC_VALUE_1_POSITION 0
#define LTM_ADC_VALUE_2_MASK 0x000ffc00
#define LTM_ADC_VALUE_2_POSITION 10
#define LTM_ADC_VALUE_3_MASK 0x3ff00000
#define LTM_ADC_VALUE_3_POSITION 20
#define LTM_GLOBAL_TRAILER 0x50000002
#define LTM_EVENT_CRC_MASK 0x0000fff0
#define LTM_EVENT_CRC_POSITION 4
#define LTM_EVENT_NUMBER_MASK 0x0fff0000
#define LTM_EVENT_NUMBER_POSITION 16
class TClonesArray;
class AliRawReader;
class AliTOFrawData;
class AliTOFRawStream: public TObject {
public:
AliTOFRawStream();
AliTOFRawStream(AliRawReader* rawReader);
virtual ~AliTOFRawStream();
AliTOFRawStream(const AliTOFRawStream& stream);
AliTOFRawStream& operator = (const AliTOFRawStream& stream);
virtual Bool_t Next();
virtual void LoadRawData(Int_t indexDDL);
Int_t GetDDL() const {return fDDL;};
Int_t GetTRM() const {return fTRM;};
Int_t GetTDC() const {return fTDC;};
Int_t GetTRMchain() const {return fTRMchain;};
Int_t GetTDCchannel() const {return fTDCchannel;};
Int_t GetSector() const {return fSector;};
Int_t GetPlate() const {return fPlate;};
Int_t GetStrip() const {return fStrip;};
Int_t GetPadZ() const {return fPadZ;};
Int_t GetPadX() const {return fPadX;};
Int_t GetTofBin() const {return fTime;};
Int_t GetToTbin() const {return fToT;};
Float_t GetLeadingEdge() const {return fLeadingEdge;};
Float_t GetTrailingEdge() const {return fTrailingEdge;};
Int_t GetPSbit() const {return fPSbit;};
Int_t GetACQ() const {return fACQ;};
Int_t GetErrorFlag() const {return fErrorFlag;};
Bool_t GetDecoderVersion() const {return fNewDecoderVersion;};
void SetDDL(Int_t nDDL) {fDDL = nDDL;};
void SetTRM(Int_t nTRM) {fTRM = nTRM;};
void SetTDC(Int_t nTDC) {fTDC = nTDC;};
void SetTRMchain(Int_t nChain) {fTRMchain = nChain;};
void SetTDCchannel(Int_t nChannel) {fTDCchannel = nChannel;};
void SetDecoderVersion(Bool_t version) {fNewDecoderVersion = version;};
TClonesArray *GetRawData() const {return fTOFrawData;};
void SetSector();
void SetPlate();
void SetStrip();
void SetPadZ();
void SetPadX();
Bool_t GetBCCorrections() const {return fgApplyBCCorrections;};
Int_t GetDDLBCshift(Int_t ddl) const {return fgkddlBCshift[ddl];};
Int_t GetLocalEventCounterDRM() const {return fLocalEventCounterDRM;};
Int_t GetLocalEventCounterLTM() const {return fLocalEventCounterLTM;};
Int_t GetLocalEventCounterTRM(Int_t trm) const {return fLocalEventCounterTRM[trm];};
Int_t GetLocalEventCounterChain(Int_t trm, Int_t chain) const {return fLocalEventCounterChain[trm][chain];};
Int_t GetChainBunchID(Int_t trm, Int_t chain) const {return fChainBunchID[trm][chain];};
void Raw2Digits(AliRawReader* rawReader, TClonesArray * const digitsArray);
void Raw2SDigits(AliRawReader* rawReader, TClonesArray * const sdigitsArray);
static void EquipmentId2VolumeId(Int_t nDDL, Int_t nTRM, Int_t iChain,
Int_t iTDC, Int_t iCH, Int_t *volume);
void EquipmentId2VolumeId(AliTOFHitData *hitData, Int_t *volume) const;
static Int_t Equip2VolNplate(Int_t iDDL, Int_t nTRM, Int_t nTDC);
static Int_t Equip2VolNstrip(Int_t iDDL, Int_t nTRM, Int_t nTDC);
static Int_t Equip2VolNpad(Int_t iDDL, Int_t iChain, Int_t nTDC, Int_t iCH);
static Int_t Equip2VolNpadX(Int_t iDDL, Int_t iChain, Int_t nTDC, Int_t iCH);
static Int_t Equip2VolNpadZ(Int_t iDDL, Int_t iChain, Int_t nTDC, Int_t iCH);
static Int_t GetDDLnumberPerSector(Int_t nDDL);
static Int_t GetSectorNumber(Int_t nDDL);
static Int_t Geant2DDL(Int_t vol[]);
static Int_t Geant2TRM(Int_t vol[]);
static Int_t Geant2TDC(Int_t vol[]);
static Int_t Geant2Chain(Int_t vol[]);
static Int_t Geant2Channel(Int_t vol[]);
static void Geant2EquipmentId(Int_t vol[], Int_t eqId[]);
Bool_t DecodeDDL(Int_t DDLMin, Int_t DDLMax, Int_t verbose);
Bool_t Decode(Int_t verbose);
Bool_t DecodeV2(Int_t verbose);
AliTOFDecoder *GetDecoder() const {return fDecoder;};
AliTOFDecoderV2 *GetDecoderV2() const {return fDecoderV2;};
void SetV2718Patch(Bool_t V2718Patch = kTRUE) {fDecoder->SetV2718Patch(V2718Patch);};
void SetRawReader(AliRawReader * const rawReader) {fRawReader=rawReader;};
AliTOFHitDataBuffer * GetDataBuffer(Int_t DDL) {return &fDataBuffer[DDL];};
AliTOFHitDataBuffer * GetPackedDataBuffer(Int_t DDL) {return &fPackedDataBuffer[DDL];};
void ResetDataBuffer(Int_t DDL) {fDataBuffer[DDL].Reset();};
void ResetPackedDataBuffer(Int_t DDL) {fPackedDataBuffer[DDL].Reset();};
void ResetBuffers();
Bool_t LoadRawDataBuffers(Int_t indexDDL, Int_t verbose = 0);
Bool_t LoadRawDataBuffersV2(Int_t indexDDL, Int_t verbose = 0);
static void ApplyBCCorrections(Bool_t Value = kTRUE) {fgApplyBCCorrections = Value;};
Int_t GetEventID() const {return fEventID;};
void LTM2VolumeID(Int_t iDDL,
Int_t iTRM,
Int_t iChain,
Int_t iTDC,
Int_t iChannel,
Int_t detind0[], Int_t detind1[]) const;
void VolumeID2LTM(Int_t detind[],
Int_t &iDDL,
Int_t &iTRM,
Int_t &iChain,
Int_t &iTDC,
Int_t &iChannel) const;
enum ETOFRawStreamError {
kPadXError = 0,
kPadAlongStripError = 1,
kPlateError = 2,
kStripError = 3,
kSectorError = 4,
kDDLMinError = 5,
kDDLMaxError = 6,
kDDLdataReading = 7,
kDDLDecoder = 8
};
private:
Int_t GetField(UInt_t word, Int_t fieldMask, Int_t fieldPosition) const;
AliRawReader* fRawReader;
TClonesArray *fTOFrawData;
AliTOFDecoder *fDecoder;
AliTOFDecoderV2 *fDecoderV2;
Int_t fDDL;
Int_t fTRM;
Int_t fTRMchain;
Int_t fTDC;
Int_t fTDCchannel;
Int_t fTime;
Int_t fToT;
Int_t fLeadingEdge;
Int_t fTrailingEdge;
Int_t fErrorFlag;
Int_t fSector;
Int_t fPlate;
Int_t fStrip;
Int_t fPadX;
Int_t fPadZ;
Int_t fPackedDigits;
Int_t fWordType;
Int_t fSlotID;
Int_t fACQ;
Int_t fPSbit;
Int_t fTDCerrorFlag;
Bool_t fInsideDRM;
Bool_t fInsideTRM;
Bool_t fInsideLTM;
Bool_t fInsideTRMchain0;
Bool_t fInsideTRMchain1;
AliTOFHitDataBuffer fDataBuffer[72];
AliTOFHitDataBuffer fPackedDataBuffer[72];
Int_t fLocalEventCounterDRM;
Int_t fLocalEventCounterLTM;
Int_t fLocalEventCounterTRM[13];
Int_t fLocalEventCounterChain[13][2];
Int_t fChainBunchID[13][2];
Int_t fEventID;
Bool_t fNewDecoderVersion;
static const Int_t fgkddlBCshift[72];
static Bool_t fgApplyBCCorrections;
static const Int_t fgkStrip0MapCrate0[];
static const Int_t fgkStrip1MapCrate0[];
static const Int_t fgkStrip0MapCrate1[];
static const Int_t fgkStrip1MapCrate1[];
static const Int_t fgkStrip0MapCrate2[];
static const Int_t fgkStrip1MapCrate2[];
static const Int_t fgkStrip0MapCrate3[];
static const Int_t fgkStrip1MapCrate3[];
static const Int_t fgkModule0MapCrate0[];
static const Int_t fgkModule1MapCrate0[];
static const Int_t fgkModule0MapCrate1[];
static const Int_t fgkModule1MapCrate1[];
static const Int_t fgkModule0MapCrate2[];
static const Int_t fgkModule1MapCrate2[];
static const Int_t fgkModule0MapCrate3[];
static const Int_t fgkModule1MapCrate3[];
static const Int_t fgkChannelMap0[5][19];
static const Int_t fgkChannelMap24[5][19];
static const Int_t fgkChainMap0[5][19];
static const Int_t fgkChainMap24[5][19];
ClassDef(AliTOFRawStream, 3)
};
#endif