/**
 * \class BDEarliest
 *
 * \ingroup BeamDataUtil
 *
 * \brief Extract earliest time of spill.
 *
 * This is done by:
 *
 * 1) Find the earliest SWIC VME time that is w/in the set deviation
 * of the DAE time less the ACNET readout delay (encoded in the data).
 *
 * 2) If no such VME time can be found, use the earliest SWIC DAE time.
 *
 * 3) If there are no SWICS use earliest torroid 
 *
 * 4) If there are no torroids in the spill use the earliest DAE time
 * from a BPM.
 *
 * 5) If no BPMs, use zero.  You wouldn't want this spill anyways.
 *
 * \author (last to touch it) $Author: bv $
 *
 * \version $Revision: 1.4 $
 *
 * \date $Date: 2005/07/11 18:31:55 $
 *
 * Contact: bv@bnl.gov
 *
 * Created on: Fri Apr 15 09:56:12 2005
 *
 * $Id: BDEarliest.h,v 1.4 2005/07/11 18:31:55 bv Exp $
 *
 */

#ifndef BDEARLIEST_H
#define BDEARLIEST_H

#include <BeamDataUtil/BDProcessor.h>

#include <Conventions/Munits.h>

class BDEarliest : public BDProcessor {
public:
    BDEarliest();
    void SetSpill(const RawBeamMonHeaderBlock& rbmhb,
		  const RawBeamMonBlock& rbmb);


    /// Set the amount that the VME time is allowed to deviate from
    /// the DAE time after readout delay is removed.  The readout
    /// delay is taken from the value in the RawBeamMonBlock.
    void SetAllowedVmeDeviation(float before = -0.1*Munits::second,
				float after  = +0.4*Munits::second)
    { fBefore = before; fAfter = after; }

    /// Get the timestamps.  Note, that dae is corrected to have the
    /// ACNET readout delay removed
    void GetTimestamps(double& dae, double& vme)
    { dae = fDae; vme = fVme; }
    void GetLastTimestamps(double& dae, double& vme)
    { dae = fLastDae; vme = fLastVme; }

    static void CalculateEarliest(const RawBeamMonBlock& rbmb, double& dae, double& vme,
				  float before = -0.1*Munits::second,
				  float after  = +0.4*Munits::second);

private:
    double fDae, fVme, fLastDae, fLastVme;
    float fBefore, fAfter;
};

#endif  // BDEARLIEST_H
