#ifndef gltables_h
#define gltables_h

#include <TString.h>
#include <TObject.h>
#include <TSQLServer.h>
#include <TSQLRow.h>
#include <TSQLResult.h>
#include <GLTablesStruct.h>
#include <EventHeader.h>
#include <PscuHeader.h>
#include <RunHeaderEvent.h>
#include <RunTrailerEvent.h>

using namespace pamela;
// ==================================================================
/**
 * \brief Class to store GL_RUN data
 */
class GL_RUN : public TObject {
 private:
    
 public:
    //
    UInt_t ID; 
    UInt_t ID_RUN_FRAG;
    UInt_t ID_ROOT_L0;
    UInt_t ID_ROOT_L2;
    UInt_t RUNHEADER_TIME;
    UInt_t RUNTRAILER_TIME;
    UInt_t RUNHEADER_OBT;
    UInt_t RUNTRAILER_OBT;
    UInt_t RUNHEADER_PKT;
    UInt_t RUNTRAILER_PKT;
    UInt_t BOOT_NUMBER;
    UInt_t EV_FROM;
    UInt_t EV_TO;
    UInt_t NEVENTS;
    UInt_t PKT_COUNTER;
    UInt_t PKT_READY_COUNTER;
    UInt_t COMPILATIONTIMESTAMP;
    UInt_t FAV_WRK_SCHEDULE;
    UInt_t EFF_WRK_SCHEDULE;
    UInt_t PRH_VAR_TRG_MODE_A; 
    UInt_t PRH_VAR_TRG_MODE_B; 
    UInt_t ACQ_BUILD_INFO;
    UInt_t ACQ_VAR_INFO;
    UInt_t RM_ACQ_AFTER_CALIB;
    UInt_t RM_ACQ_SETTING_MODE;
    UInt_t TRK_CALIB_USED;
    UInt_t CAL_DSP_MASK;
    UInt_t LAST_TIMESYNC;
    UInt_t OBT_TIMESYNC;
    UInt_t VALIDATION;
    //
    //
    //
    GL_RUN();
    //
    // functions
    //
    Int_t Query_GL_RUN(UInt_t, TSQLServer*);
    Int_t Fill_GL_RUN(TSQLServer*);
    Int_t Query_GL_RUN_FRAGMENTS(TString, TSQLServer*);
    Int_t Fill_GL_RUN_FRAGMENTS(TSQLServer*);
    void Clear();
    //
    // SETTERS
    //
    void Set_GL_RUNT(RunTrailerEvent *runt, PscuHeader *pht);
    void Set_GL_RUNH(RunHeaderEvent *runt, PscuHeader *pht);
    void Set_GL_RUNT0();
    void Set_GL_RUNH0();
    void SetRUNHEADER_TIME(UInt_t abst);
    void SetRUNTRAILER_TIME(UInt_t abst);
    void SetRUNHEADER_PKT(UInt_t abst);
    void SetRUNTRAILER_PKT(UInt_t abst);
    void SetRUNHEADER_OBT(UInt_t abst);
    void SetRUNTRAILER_OBT(UInt_t abst);
    void SetBOOTNUMBER(UInt_t abst);
    void SetID(UInt_t);
    void SetID_ROOT_L2(UInt_t L2);
    void SetID_ROOT_L0(UInt_t L0);
    void SetID_RUN_FRAG(UInt_t RUN);
    void SetVALIDATION(UInt_t valid);
    void SetEV_FROM(UInt_t evfrom);
    void SetEV_TO(UInt_t evto);
    void SetNEVENTS(UInt_t nev);
    void SetLAST_TIMESYNC(UInt_t ts);
    void SetOBT_TIMESYNC(UInt_t ts);
    void SetPKT_COUNTER(UInt_t);
    void SetPKT_READY_COUNTER(UInt_t);
    void SetCOMPILATIONTIMESTAMP(UInt_t);
    void SetFAV_WRK_SCHEDULE(UInt_t);
    void SetEFF_WRK_SCHEDULE(UInt_t);
    void SetPRH_VAR_TRG_MODE_A(UInt_t);
    void SetPRH_VAR_TRG_MODE_B(UInt_t);
    void SetACQ_BUILD_INFO(UInt_t);
    void SetACQ_VAR_INFO(UInt_t);
    void SetRM_ACQ_AFTER_CALIB(UInt_t);
    void SetRM_ACQ_SETTING_MODE(UInt_t);
    void SetTRK_CALIB_USED(UInt_t);
    void SetCAL_DSP_MASK(UInt_t);
    //
    // GETTERS
    //
    void GetLevel2Struct(cGLRun *l2) const;
    UInt_t GetRUNHEADER_PKT(){return(RUNHEADER_PKT);};
    UInt_t GetRUNTRAILER_PKT(){return(RUNTRAILER_PKT);};
    UInt_t GetRUNHEADER_TIME(){return(RUNHEADER_TIME);};
    UInt_t GetRUNTRAILER_TIME(){return(RUNTRAILER_TIME);};
    UInt_t GetRUNHEADER_OBT(){return(RUNHEADER_OBT);};
    UInt_t GetRUNTRAILER_OBT(){return(RUNTRAILER_OBT);};
    UInt_t GetNEVENTS(){return(NEVENTS);};
    UInt_t GetBOOT_NUMBER(){return(BOOT_NUMBER);};
    UInt_t GetTRK_CALIB(){return(TRK_CALIB_USED);};
    UInt_t GetPKT_COUNTER(){return(PKT_COUNTER);};
    UInt_t GetLAST_TIMESYNC(){return(LAST_TIMESYNC);};
    UInt_t GetOBT_TIMESYNC(){return(OBT_TIMESYNC);};
    UInt_t GetPKT_READY_COUNTER(){return(PKT_READY_COUNTER);};
    UInt_t GetCOMPILATIONTIMESTAMP(){return(COMPILATIONTIMESTAMP);};
    UInt_t GetFAV_WRK_SCHEDULE(){return(FAV_WRK_SCHEDULE);};
    UInt_t GetEFF_WRK_SCHEDULE(){return(EFF_WRK_SCHEDULE);};
    UInt_t GetPRH_VAR_TRG_MODE_A(){return(PRH_VAR_TRG_MODE_A);};
    UInt_t GetPRH_VAR_TRG_MODE_B(){return(PRH_VAR_TRG_MODE_B);};
    UInt_t GetACQ_BUILD_INFO(){return(ACQ_BUILD_INFO);};
    UInt_t GetACQ_VAR_INFO(){return(ACQ_VAR_INFO);};
    UInt_t GetRM_ACQ_AFTER_CALIB(){return(RM_ACQ_AFTER_CALIB);};
    UInt_t GetRM_ACQ_SETTING_MODE(){return(RM_ACQ_SETTING_MODE);};
    UInt_t GetTRK_CALIB_USED(){return(TRK_CALIB_USED);};
    UInt_t GetCAL_DSP_MASK(){return(CAL_DSP_MASK);};

    //
    ClassDef(GL_RUN,1);
};

// ==================================================================
/**
 * \brief Class to store GL_ROOT 
 */
class GL_ROOT : public TObject{
 private:
    
 public:
    //
    UInt_t ID;
    UInt_t ID_RAW;
    UInt_t ID_TIMESYNC;
    TString   PATH;
    TString   NAME;

    GL_ROOT();

    Int_t Query_GL_ROOT(UInt_t id, TSQLServer *dbc);

    ClassDef(GL_ROOT,1);
};
// ==================================================================
/**
 * \brief Class to store GL_PARAM data
 */
class GL_PARAM : public TObject{
 private:
    
 public:
    //
    UInt_t ID;
    TString   PATH;
    TString   NAME;
    UInt_t FROM_TIME;
    UInt_t TO_TIME;
    UInt_t TYPE;
    TString   DESCR;

    GL_PARAM();

    Int_t Query_GL_PARAM(UInt_t time, UInt_t type, TSQLServer *dbc);
 
    ClassDef(GL_PARAM,1);
};
// ==================================================================
/**
 * \brief Class to store GL_TRK_CALIB data
 */
class GL_TRK_CALIB : public TObject{
 private:
    
 public:
    //
    UInt_t ID;
    UInt_t ID_ROOT_L0;
    UInt_t EV_ROOT_CALIBTRK1 ;
    UInt_t EV_ROOT_CALIBTRK2 ;
    UInt_t FROM_TIME;
    UInt_t TO_TIME;
    UInt_t OBT1;
    UInt_t OBT2;
    UInt_t PKT1;
    UInt_t PKT2;

    GL_TRK_CALIB();

    Int_t Query_GL_TRK_CALIB(UInt_t time, TSQLServer *dbc);

    ClassDef(GL_TRK_CALIB,1);
};

// ==================================================================
/**
 * \brief Class to store GL_CALO_CALIB data
 */
class GL_CALO_CALIB : public TObject{
 private:
    
 public:
    //
    UInt_t ID;
    UInt_t ID_ROOT_L0;
    UInt_t     EV_ROOT;
    UInt_t FROM_TIME;
    UInt_t TO_TIME;
    UInt_t     SECTION;

    GL_CALO_CALIB();

    Int_t Query_GL_CALO_CALIB(UInt_t time, UInt_t section, TSQLServer *dbc);

    ClassDef(GL_CALO_CALIB,1);
};
// ==================================================================
/**
 * \brief Class to store GL_S4_CALIB data
 */
class GL_S4_CALIB : public TObject{
 private:
    
 public:
    //
    UInt_t ID;
    UInt_t ID_ROOT_L0;
    UInt_t     EV_ROOT;
    UInt_t FROM_TIME;
    UInt_t TO_TIME;
    Float_t PARAM_FIT0;
    Float_t PARAM_FIT1;

    GL_S4_CALIB();

    Int_t Query_GL_S4_CALIB(UInt_t time, TSQLServer *dbc);

    ClassDef(GL_S4_CALIB,1);
};

// ==================================================================
/**
 * \brief Class to store TIMESYNC data
 */
class GL_TIMESYNC : public TObject{
 private:
    UInt_t obtfirst;
    UInt_t pktfirst;
    UInt_t toffset;
    //
    UInt_t ID;
    UInt_t ID_RAW;
    UInt_t OBT0;
    UInt_t TIMESYNC;
    UInt_t TYPE;
    
 public:
    //
    GL_TIMESYNC();
    GL_TIMESYNC(UInt_t ID, TString TYPE, TSQLServer *dbc);
    //
    UInt_t DBabsTime(UInt_t OBT);
    //
    UInt_t DBpkt(UInt_t PKT);
    ULong64_t DBobt(UInt_t OBT);
    //
    TString ConvertTime(TString tzone, UInt_t dbt);
    TString UnConvertTime(TString tzone, UInt_t dbt);


    ClassDef(GL_TIMESYNC,1);
};

#endif
