#include <iostream>
#include <fstream>
using namespace std;

#include "readinluminosity.h"
#include "starlightconstants.h"
#include "inputparameters.h"


Readinluminosity::Readinluminosity(Inputparameters& input)//:inputread(input)
{

	//storing inputparameters into protected variables for the object to use them
	ReadInputNPT=input.getNPT();
	ReadInputnumy=input.getnumy();
	ReadInputnumw=input.getnumw();
	ReadInputgg_or_gP=input.getgg_or_gP();
	ReadInputinterferencemode=input.getinterferencemode();

}

Readinluminosity::~Readinluminosity()
{
}

void Readinluminosity::read()
{
//  cout <<"ENTERHERE"<<endl;
        double dummy[18]; //14
//      double Yarray[1000],Warray[1000],Farray[1000][1000];
        double (*finterm)[StarlightLimits::MAXWBINS]=new double[StarlightLimits::MAXWBINS][StarlightLimits::MAXYBINS];  //decreased from 1000*1000; too big! causes fault!
        f_max=0.0;
        double fpart =0.;
        //double fptarray[500][500];  
        double fptsum=0.;
//      cout << "Aread" << endl;
        ifstream wylumfile;
        wylumfile.open("slight.txt");
        for(int i=0;i < 18;i++) // was 14; this is to account for sergei's additional parameters ie d-Au
        {
                wylumfile >> dummy[i];
                //cout << "Dummy[i]= "<<dummy[i]<<endl;
        }
        for(int i=0;i<ReadInputnumw;i++)
        {
                wylumfile >> Warray[i];
                //      cout << "Warray[i]= "<<Warray[i]<<endl;
        }
        for(int i=0;i<ReadInputnumy;i++)
        {
                wylumfile >> Yarray[i];
                //cout << "Yarray[i]= "<<Yarray[i]<<endl;
        }
//      cout << "Bread" <<endl;
        for(int i=0;i<ReadInputnumw;i++)
        {
                for(int j=0;j<ReadInputnumy;j++)
                {
                        wylumfile >> Farray[i][j];
                        //cout << "Farray[i,j]= "<<Farray[i][j]<<endl;
                        if( Farray[i][j] > f_max ) f_max=Farray[i][j];
                        //cout << "Farray[i,j],fmax= "<<Farray[i][j]<<" "<<f_max<<endl;

                }
        }
        //Normalize farray (JN 010402)
        for(int i=0;i<ReadInputnumw;i++)
        {
                for(int j=0;j<ReadInputnumy;j++)
                {
                        Farray[i][j]=Farray[i][j]/f_max;
                        //      cout <<"NormFarray[i][j]= "<<Farray[i][j]<<endl;
                }
        }
//              cout << "Cread"<<endl;
        //Is there a pt table ( for vector mesons with interference)?
        if(ReadInputgg_or_gP == 1) goto L1000;
        if(ReadInputinterferencemode == 0) goto L1000;
        // only numy/2 y bins here, from 0 (not -ymax) to ymax
        for (int i=0;i<ReadInputnumy/2;i++)
        {
                //fmax=0;
                //we want to convert fptarray to an integral array where fpt(i,j) is near 0, and fpt(j,NPT) ~1. This will facilitate a simple table loookup
                fptsum=0.;
                for(int j=0;j<ReadInputNPT;j++)
                {
                        wylumfile >> fpart;
                        finterm[i][j] = fpart;
                        fptarray[i][j]=0.;
                        fptsum=fptsum+fpart;
                        //cout << "fpart, fptsum= "<<fpart<<" "<<fptsum<<endl;
                }
                //convert array to integral
                fptarray[i][0]=finterm[i][0]/fptsum;
                //cout <<"finterm[i][0],fptarray[i][0]= "<<finterm[i][0]<<" "<<fptarray[i][0]<<endl;
                for(int j=1;j<ReadInputNPT;j++)
                {
                        for(int k=0;k<=j;k++)
                        {
                                fptarray[i][j]=fptarray[i][j]+finterm[i][k];
                                //cout <<"fptarray[i][j],finterm[i][k]= "<<fptarray[i][j]<<" "<<finterm[i][k]<<endl;
                        }
                fptarray[i][j]=fptarray[i][j]/fptsum;
//              cout <<"sigma fptarray[i][j]= "<<fptarray[i][j]<<endl;
                }
        }

L1000:
//      cout <<"Dread"<<endl;
        wylumfile >> bwnormsave;
        wylumfile.close();
	delete[] finterm;	
//         cout <<"Eread"<<endl;
        return;
}

