////////////////////////////////////////////////////////////////////
/// \class RAT:TriggerClockJumpProc
///
/// \brief  Check for any trigger clock jumps
///
/// \author Tanner Kaptanoglu <tannerk@sas.upenn.edu>
///
/// \details This processor loops through the events and looks for
///          trigger clock jumps in the 50 and 10 MHz clocks. If it
///          find either, it pushes that information to a ratdb
///          table, to be corrected by the ReconstructClocks processor.
///
////////////////////////////////////////////////////////////////////

#ifndef __RAT__TriggerClockJumpProc__
#define __RAT__TriggerClockJumpProc__

#include <RAT/Processor.hh>
#include <RAT/DS/Entry.hh>
#include <RAT/DU/TrigBits.hh>
#include <RAT/ReconstructClocks.hh>

#include <stdint.h>

namespace RAT
{
  class TriggerClockJumpProc : public Processor
  {
    public:

      TriggerClockJumpProc();
      virtual ~TriggerClockJumpProc(){};

      virtual void BeginOfRun(DS::Run& run);
      virtual Processor::Result DSEvent(DS::Run&, DS::Entry& ds);
      void CorrectClock(DS::EV& ev);
      virtual void EndOfRun(DS::Run& run);


    protected:

      // Keep track of 50MHz clock jumps
      void Fix50MHzClock(std::vector<uint64_t> clock_10,
                         std::vector<uint64_t> clock_50,
                         int gtid);
      // Keep track of 10MHz clock jumps
      void Fix10MHzClock(std::vector<uint64_t> clock_10,
                         std::vector<uint64_t> clock_50,
                         int gtid);

      int gtidSync; // Keep track of the GTID we get a SYNC pulse
      int validGTID; // first valid GTID in the run
      int clockOnline; // whether the 10MHz clock is working

      ReconstructClocks fReconstructClocks; // Use to check 10MHz clock is working

      std::vector<uint64_t> clock50; // Keep track of 50MHz clock ticks
      std::vector<uint64_t> clock10; // Keep track of 10MHz clock ticks

      std::vector<int> gtidClock10; // GTIDs of the 10MHz clock jumps
      std::vector<int> gtidClock50; // GTIDs of the 50MHz clock jumps

      std::vector<double> delta10; // Size of the 10MHz clock jump corrections
      std::vector<double> delta50; // Size of the 50MHz clock jump corrections

      std::vector<double> jump50; // Size of 50MHz jump
      std::vector<double> jump10; // Size of 10MHz jump

      // Clock ticks that could not be corrected
      std::vector<int> gtidBadClock10; // Bad 50MHz clock ticks
      std::vector<int> gtidBadClock50; // Bad 10MHz clock ticks


  };
}//namespace RAT
#endif