#ifndef TMHistogrammingEventDump_H
#define TMHistogrammingEventDump_H

#include "TMHistogramming.h"
#include "AliITSRawStreamSPD.h"
#include "event.h"
#include <TString.h>

const UInt_t gMAXLINES = 10000;

struct miniHeaderCaster {
  Int_t a[8];                                                                          // Data itself
  Int_t Get_blockLength_0_31()                 { return   a[0];                      } // See DAQ-TDR Page 131 for Mini Header
  Int_t Get_event_ID1_0_11()                   { return ( a[1] & 0x00000fff);        } // Bunch crossing
  Int_t Get_MBZ_12_13()                        { return ((a[1] & 0x00003000) >> 12); }
  Int_t Get_L1_trigger_type_14_23()            { return ((a[1] & 0x00ffc000) >> 14); }
  Int_t Get_format_version_24_31()             { return ((a[1] & 0xff000000) >> 24); }
  Int_t Get_eventID2_0_23()                    { return ( a[2] & 0x00ffffff);        } // Orbit number
  Int_t Get_MBZ2_24_31()                       { return ((a[2] & 0xff000000) >> 24); }
  Int_t Get_participating_sub_detectors_0_23() { return ( a[3] & 0x00ffffff);        }
  Int_t Get_block_att_24_31()                  { return ((a[3] & 0xff000000) >> 24); }
  Int_t Get_mini_event_ID_0_11()               { return ( a[4] & 0x00000fff);        }
  Int_t Get_status_error_bits_12_27()          { return ((a[4] & 0x0ffff000) >> 12); }
  Int_t Get_ResBZ_28_31()                      { return ((a[4] & 0xf0000000) >> 28); }
  Int_t Get_trigger_classes_low_0_31()         { return   a[5];                      }
  Int_t Get_trigger_classes_high_0_17()        { return ( a[6] & 0x0003ffff);        }
  Int_t Get_MBZ_18_27()                        { return ((a[6] & 0x0ffc0000) >> 18); }
  Int_t Get_ROI_Low_28_31()                    { return ((a[6] & 0xf0000000) >> 28); }
  Int_t Get_ROI_High_0_31()                    { return   a[7];                      }
  Int_t Get0() {return a[0];} // Used for debugging
  Int_t Get1() {return a[1];}
  Int_t Get2() {return a[2];}
  Int_t Get3() {return a[3];}
  Int_t Get4() {return a[4];}
  Int_t Get5() {return a[5];}
  Int_t Get6() {return a[6];}
  Int_t Get7() {return a[7];}
};

struct equipmentCaster {
  struct equipmentHeaderStruct eq;
  struct miniHeaderCaster mhc;
  Int_t justAfter[1];
};

struct headerCaster {
  struct eventHeaderStruct header;
  Int_t justAfter[1];
};

struct DATEstartCaster {
  struct eventHeaderStruct header0;
  struct eventHeaderStruct header1;
  void *ptr;
};

struct DATEraw {
  Int_t w[10000000]; // max event size 40 MBytes as an assument
};

class TMHistogrammingEventDump : public TMHistogramming {
 public:
  TMHistogrammingEventDump();
  virtual ~TMHistogrammingEventDump();
  
  virtual void StartEvent(eventHeaderStruct *event, UInt_t globalEventNumber);
  virtual void ProcessEvent(eventHeaderStruct *event);
  virtual void EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber);
  virtual void Reset();

  virtual void Dump(TGTextView *textview);

  virtual void   SetDumpToTerminal(Bool_t setv=kTRUE) {fDumpToTerminal=setv;}
  virtual Bool_t GetDumpToTerminal() {return fDumpToTerminal;}

 protected:

  Bool_t fTimeStampPresent;
  Bool_t fModeCollider;

  TString fTextLines[gMAXLINES];
  UInt_t  fNrLines;
  Bool_t  fDumpToTerminal;

  virtual void    DumpPayload(void *ptr);
  virtual void    DumpHeader(void *ptr);
  virtual void    DumpHeaderShort(void *ptr);
  virtual void    DumpHeaderLong(void *ptr);
  virtual void    DumpCalibHeader(void *ptr, UInt_t globalEventNumber);

  virtual Bool_t  DumpHeaderStruct(struct headerCaster *hc, Int_t status);
  virtual Bool_t  DumpEquipmentHeaderStruct(struct equipmentCaster *ec, Int_t status, Bool_t spdOnly = kFALSE);
  virtual Bool_t  DumpPayloadStruct(struct equipmentCaster *ec);
  virtual Bool_t  DumpCalibHeaderStruct(struct equipmentCaster *ec, UInt_t globalEventNumber);


  virtual Bool_t  DumpThis(const Char_t *msg);
  virtual Bool_t  DumpThis(const Char_t *msg, Int_t value, Char_t *status, Int_t binaryLength=32);
  virtual TString parseWord(Int_t word);
  virtual void    LoadBuffer(TGTextView *textview);
  virtual void    DumpBuffer();

  eventHeaderStruct *fEvent;

  enum calib_types{MINTH,MEANTH,DAC,UNIMA,NOISE,DELAY,FASTOR};

};

#endif
