////////////////////////////////////////////////////////////////////////
/// \class RAT::DQTriggerProc
///
/// \brief   Data quality trigger processor
///
/// \author Ashley R. Back <a.r.back@qmul.ac.uk>
///
/// REVISION HISTORY:\n
///
/// \details Carries out all the data quality checks relating to \n
/// triggers. Looks at the occupancy/rate of specific trigger channels \n
/// such as ESUM HI and NHIT 100. Also produces a trigger word summary \n
/// plot for the run.
///
////////////////////////////////////////////////////////////////////////

#ifndef __RAT_DQTriggerProc__
#define __RAT_DQTriggerProc__

#include <RAT/Processor.hh>
#include <RAT/DataQualityProc.hh>
#include <RAT/DB.hh>
#include <RAT/DBLink.hh>

#include <vector>

class TH1D;

namespace RAT
{
  namespace DS
  {
    class Run;
    class Entry;
    class EV;
  }
}

namespace RAT
{

  class DQTriggerProc : public DataQualityProc
  {
  public:

    DQTriggerProc();

    virtual ~DQTriggerProc();

    virtual void BeginOfRun( DS::Run& run );

    virtual Processor::Result DSEvent( DS::Run& run, DS::Entry& ds );

    virtual void EndOfRun( DS::Run& run );

  protected:
    virtual Processor::Result Event( DS::Entry& ds, DS::EV& ev );

    DBLinkPtr fDQChecks; ///<link to RATDB "DQCHECKS" table

    double fMinN100LRate; ///<minimum N100L rate from DQCHECKS
    double fMinEsumHRate; ///<minimum EsumH rate from DQCHECKS

    int fNOrphans; ///<Counter for the number of orphans

    int fNTrigTypeBits; ///<number of bits in trigType word

    int fMissingGTIDCount; ///<Number of missing GTIDs
    std::vector<int> fMissingGTIDs; ///<Missing GTIDs
    int fBitFlipGTIDCount; ///<Number of bitflip GTIDs
    std::vector<int> fBitFlipGTIDs; ///<GTID of events that have been bit flipped

    int fMissingGTIDLimit; ///<Limit for number of missing GTIDS
    int fBitFlipGTIDLimit; ///<Limit for number of missing GTIDS

    bool firstEventFlag;
    bool passedSecondEvent;
    int fPreviousGTID; ///< Previous GTID in
    int fPreviousPreviousGTID; ///< Previous GTID in

    TH1D* fTrigTypeRateHist; ///<histogram containing rates for each bit in trigType word

    std::vector<int> fTrigTypeCounts; ///<vector containing event counts for each bit in trigType word
    std::vector<double> fTrigTypeRates; ///<vector containing rates for each bit in trigType word

  };

}// namespace RAT

#endif // DQTriggerProc