/**
 * \class BDSwicPedAccessor
 *
 * \ingroup BeamDataUtil
 *
 * \brief Access the BeamMonSwicPed database tables.
 *
 * Currently this constructs pedestal values using measurements from
 * the vld range containing the time given in SetSpillTime().  If that
 * doesn't provide enough to satisify the minium_sample_size given in
 * the constructor it will go back in time adding additional vld
 * ranges until the minimum is satisfied.
 *
 * \author (last to touch it) $Author: bv $
 *
 * \version $Revision: 1.6 $
 *
 * \date $Date: 2005/05/12 18:48:31 $
 *
 * Contact: bv@bnl.gov
 *
 * Created on: Wed Apr 13 18:14:17 2005
 *
 * $Id: BDSwicPedAccessor.h,v 1.6 2005/05/12 18:48:31 bv Exp $
 *
 */

#ifndef BDSWICPEDACCESSOR_H
#define BDSWICPEDACCESSOR_H

#include <Validity/VldContext.h>
#include <Validity/VldRange.h>

#include <BeamDataUtil/BeamMonSwicPeds.h>
#include <DatabaseInterface/DbiResultPtr.h>

#include <vector>
#include <map>

class UtilBlockedRunningAverageVector;

class BDSwicPedAccessor {

public:

    BDSwicPedAccessor();
    ~BDSwicPedAccessor();

    /// Add a device of the given name to the list of ones that should
    /// be maintained.  The optional minimum sample specifies the
    /// minimum number of pedestals desired.
    void AddDevice(const char* device_name,
		   int minimum_sample_size=10);

    /// Update pedestals.  Should be called once per spill.  Return
    /// true if needed to update peds.
    bool SetSpillTime(VldContext vc);

    /// Get the current pedestal statistics for given device.  
    /// Return 0 and zero out vectors if the device name isn't
    /// known.
    int GetPeds(const char* device_name,
		std::vector<double> &mean,
		std::vector<double> &sigma) const;
    int GetPeds(const char* device_name,
		std::vector<double> &mean) const;
private:
        
    struct Device {
	UtilBlockedRunningAverageVector* channels;
	VldRange range;
	bool is_profile;
	Device() : channels(0), range(), is_profile(false) {}
    };
    typedef std::map<std::string,Device> SwicMap;
    SwicMap fSwicMap;

    DbiResultPtr<BeamMonSwicPeds>* fDRP;
};

#endif  // BDSWICPEDACCESSOR_H
