
from simplemc.likelihoods.BaseLikelihood import BaseLikelihood


class GaussBAODVLikelihood(BaseLikelihood):
    def __init__(self, name, z, DV, DVErr, fidtheory, maxchi2=1e30):
        """
        This is a BAO likelihood, where we give DV and its error and optionally a value
        at which chi2 is cut.
        Parameters
        ----------
        name
        z
        DV
        DVErr
        fidtheory
        maxchi2

        Returns
        -------

        """
        BaseLikelihood.__init__(self, name)
        self.z = z
        rd     = fidtheory.rd
        DV    /= rd
        DVErr /= rd
        self.maxchi2 = maxchi2
        print(name, "measurement in ", fidtheory.rd_approx, ":", DV, "+-", DVErr)
        self.setData(DV, DVErr)


    def setData(self, DV, DVErr):
        self.DV     = DV
        self.DVErr2 = DVErr**2


    def loglike(self):
        DVT  = self.theory_.DVOverrd(self.z)
        chi2 = min(self.maxchi2, (DVT-self.DV)**2/(self.DVErr2))
        return -chi2/2.0
