#include "AdvFuncConv.hh"

AdvFuncConv::AdvFuncConv()
{
}

// create Resolution test data(xbin)
std::vector <Double> AdvFuncConv::ResolTestXbin(Double initial,Double final,Double delta) {
    std::vector<Double> resX;
    resX.clear();
    UInt4 num_of_bin;
    num_of_bin = UInt4((final-initial)/delta);
    for (UInt4 i=0;i<num_of_bin+2;i++)
        {
        resX.push_back(initial+delta*i);
        }
    return resX;
}

// create Resolution test data(ybin)
std::vector <Double> AdvFuncConv::ResolTestYbin(Double initial,Double final,Double delta,Double a) {
    std::vector<Double> resY;
    resY.clear();
    UInt4 num_of_bin;
    num_of_bin = UInt4((final-initial)/delta);
    for (UInt4 i=0;i<num_of_bin+1;i++)
        {
        if(initial+delta*i < 0)
            {
            resY.push_back(0);
            }
        else
            {
            resY.push_back(exp(-i*a));
            }
        }
    return resY;
}

void AdvFuncConv::SetResolutionEC(ElementContainer ec)
    {
    std::vector<Double> ecX=ec.PutX();
    std::vector<Double> ecY=ec.PutY();
    ResY.clear();
    ResX.clear();

    if (ec.PutHeaderPointer()->CheckKey("CONVOLRANGE")==1)
        {
        std::vector<Double> xrange = ec.PutHeader().PutDoubleVector("CONVOLRANGE");
        UInt4 j=0;
        for (UInt4 i=0;i<ecY.size();i++)
            {
            if (xrange[0]<=(ecX[i]+ecX[i+1])*0.5 && (ecX[i]+ecX[i+1])*0.5<=xrange[1])
                {
                ResX.push_back(ecX[i]);
                ResY.push_back(ecY[i]);
                j=i;
                }
            }
        ResX.push_back(ecX[j+1]);
        }
    else
        {
        UInt4 j=0;
        for (UInt4 i=0;i<ecY.size();i++)
            {
            ResX.push_back(ecX[i]);
            ResY.push_back(ecY[i]);
            j=i;
            }
        ResX.push_back(ecX[j+1]);
        }
    }

void AdvFuncConv::ReadResolutionFile()
    {
//        std::ifstream fi("/home/dnaadmin/convo_fit_test/res.dat");
        std::ifstream fi("/usr/local/mlf/DNA/tmp/res.dat");
        std::string buf;
        size_t found;
        ResY.clear();
        ResX.clear();
        while(fi && getline(fi, buf)) {
              size_t count =0;
              UInt4 numm=0;
              UInt4 flug=0;
              while((found = buf.find(" ", count)) != std::string::npos){
                  std::string StringBuf = std::string(buf, count, found - count);
                  if (StringBuf != ""){
                        double rt;
                        std::stringstream ss;
                        ss << StringBuf;
                        ss >> rt;
                        if (flug == 0) ResX.push_back(rt);
                        if (flug == 1) ResY.push_back(rt);
                        flug = flug + 1;
                     }
                  count = found+1;
                  numm = numm+1;
                 }
                    if(buf.size() != count)
                          {
                        double rt;
                        std::stringstream ss;
                        std::string StringBuf = std::string(buf, count, buf.size() - count);
                        ss << StringBuf;
                        ss >> rt;
                        if (flug == 1) ResY.push_back(rt);
                          }
             }

    }
