////////////////////////////////////////////////////////////////////////
/// \class RAT::DQN16Proc
///
/// \brief   Data quality N16 processor
///
/// \author Mark Stringer <ms711@sussex.ac.uk>
///
///
/// \details Checks N16 specific calibration runs.
///
////////////////////////////////////////////////////////////////////////

#ifndef __RAT_DQN16Proc__
#define __RAT_DQN16Proc__

#include <RAT/Processor.hh>
#include <RAT/DataQualityProc.hh>
#include <RAT/DB.hh>
#include <RAT/DBLink.hh>
#include <RAT/DU/PMTInfo.hh>
#include <RAT/DS/UniversalTime.hh>
#include <sstream>
class TH1D;
class TH1I;
class TVector3;
class TVector2;

#include <vector>

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

namespace RAT
{
  class DQN16Proc : public DataQualityProc
  {
  public:

    DQN16Proc();

    virtual ~DQN16Proc();

    ///
    /// @param[in] run DS::Run location in data structure
    virtual void BeginOfRun( DS::Run& run );
    /// Access event in run
    ///
    /// @param[in] run DS::Run location in data structure
    /// @param[in] ds  DS::Entry location in data structure
    virtual Processor::Result DSEvent( DS::Run& run, DS::Entry& ds );
    ///
    /// @param[in] run DS::Run location in data structure
    virtual void EndOfRun( DS::Run& run );

  protected:

    /// @param[in] ds  DS::Entry location in data structure
    /// @param[in] ev  DS::EV  event location in data structure
    virtual Processor::Result Event( DS::Entry& ds, DS::EV& ev );

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

    int fTrigCheckBit; ///<N16 trigger bit

    int fN16Count; ///<counts N16 events
    int fN16Events; ///<number of pulses sent to N16

    double fN16MeanNHit; ///<Mean NHit in N16 events
    double fN16MeanNHitUpperLimit; ///< Upper limit of mean NHit of N16 events
    double fN16MeanNHitLowerLimit; ///< Lower limit of mean NHit of N16 events
    double fN16NHitSpread;  ///<Spread on the N16 NHit distribution
    double fN16NHitSpreadUpperLimit; ///<Upper limit on the spread of the NHit distribution

    TVector3 fN16ManipPosition; ///<Position of the source as obtained from the manip
    TVector3 fAverageFitPosition; ///<Mean Fit Position
    double fN16FitTolerance; ///<Tolerance on the difference between the fit vertex and the manip position of the laserball
    int fMaxNumN16FitFails; ///<Value for the maximum number of events failing fit tolerance check
    int fNumN16FitFails; ///<Counter for the number of events which have failed the fit tolerance check


    bool fFirstN16Event; ///<Check to see if first N16Event in run

    DS::UniversalTime fPrevEventTime; ///<Previous event time.


    TH1I* fTH1INHitsTagged; ///<histogram of NHits for events triggered by N16 source
    TH1I* fTH1INHitsUntagged; ///<histogram of NHits for events not triggered by N16 source
    TH1D* fTH1DTimeBetweenN16Events; ///<time between events
    TH1D* fTH1DQHS; ///<QHS histogram
  };

}// namespace RAT


#endif // DQN16Proc