/**
 * \class BMSpillFiller
 *
 * \ingroup BeamDataDbi
 *
 * \brief Fill BeamMonSpill table row object.
 *
 * This filler buffers multiple beam spills in order to write them all
 * to a single validity range.  This somewhat reduces the size of the
 * VLD table and speeds up later retrieval.
 *
 * The spill time is chosen to be the earliest SWIC (VME) timestamp
 * that is consistent (w/in 0.5 seconds) of the devices DAE timestamp
 * or, if none are found, the earliest DAE timestamp.
 *
 * \author (last to touch it) $Author: mdier $
 *
 * \version $Revision: 1.9 $
 *
 * \date $Date: 2005/10/31 22:21:40 $
 *
 * Contact: bv@bnl.gov
 *
 * Created on: Wed Apr 13 18:15:34 2005
 *
 * $Id: BMSpillFiller.h,v 1.9 2005/10/31 22:21:40 mdier Exp $
 *
 */


#ifndef BMSPILLFILLER_H
#define BMSPILLFILLER_H

#include <BeamDataDbi/BeamMonFiller.h>

#include <Conventions/Munits.h>

#include <deque>

class BeamMonSpill;

class BDEarliest;
class BDScalar;
class BDHornCurrent;
class BDTarget;
class BDHadMuMon;

class BMSpillFiller : public BeamMonFiller {
public:
    /// Create a BMSpillFiller with all the BDProcessors it needs
    BMSpillFiller(BDEarliest& bde,
		  BDScalar* bdpi[4],
		  BDHornCurrent& bdhc,
		  BDTarget& target,
		  BDHadMuMon* hadmu[4]);

    ~BMSpillFiller();


    ///  Set the number of spills to write to one validity range.
    void SetSpillsPerWrite(size_t spills_per_write=3000);

    /// Call once each spill, after the BDProcessors have visited the
    /// data.
    void Spill(const RawBeamMonHeaderBlock& rbmhb, const RawBeamMonBlock& rbmb);

private:
    // internal.  Will send spill rows to the BD when there are enough
    // or if all is true;
    void DBU(bool all=false);

    BDEarliest& fEarliest;
    BDScalar* fToroids[4];
    BDHornCurrent& fHorn;
    BDTarget &fTarget;

    // Buffer the spills.  This assumes time ordering!
    std::deque<BeamMonSpill*> fSpills;
    size_t fSpillsPerWrite;
    BDHadMuMon *fHadMu[4];
};


#endif  // BMSPILLFILLER_H
