def set_mean_data(self):

		raw_path = self._path_to_simu + 'Results/'
		### we
		path = raw_path + 'list_We.npy'
		listWe = numpy.load(path)
		nbTot = listWe[0,0,:].size
		for i in numpy.arange(self._nb_wedges):
			self._mean_data._xiWe[:,i,1] = numpy.mean(listWe[:,i,:],axis=1)
			self._mean_data._xiWe[:,i,2] = numpy.sqrt( numpy.diag(numpy.cov( listWe[:,i,:] ))/nbTot)
		### mu
		path = raw_path + 'cov_Mu.npy'
		self._mean_data._xiMu[:,:,2] = myTools.convert1DTo2D(numpy.mean(numpy.load(raw_path + 'list_Mu.npy'),axis=1),self._nbBin1D,self._nbBinM)
		self._mean_data._xiMu[:,:,3] = myTools.convert1DTo2D(numpy.sqrt( numpy.diag(numpy.load(path))/nbTot ),self._nbBin1D,self._nbBinM)
		### 1D
		path = raw_path + 'cov_1D.npy'
		self._mean_data._xi1D[:,1]   = numpy.mean(numpy.load(raw_path + 'list_1D.npy'),axis=1)
		self._mean_data._xi1D[:,2]   = numpy.sqrt( numpy.diag(numpy.load(path))/nbTot )
		### 2D
		path = raw_path + 'cov_2D.npy'
		self._mean_data._xi2D[:,:,1] = myTools.convert1DTo2D(numpy.mean(numpy.load(raw_path + 'list_2D.npy'),axis=1),self._nbBinX2D, self._nbBinY2D)
		self._mean_data._xi2D[:,:,2] = myTools.convert1DTo2D(numpy.sqrt( numpy.diag(numpy.load(path))/nbTot ),self._nbBinX2D, self._nbBinY2D)
		### Multipol
		path = raw_path + 'list_Multipol.npy'
		listMultipol = numpy.load(path)
		for i in numpy.arange(5):
			self._mean_data._xiMul[:,i,1] = numpy.mean(listMultipol[:,i,:],axis=1)
			self._mean_data._xiMul[:,i,2] = numpy.sqrt( numpy.diag(numpy.cov( listMultipol[:,i,:] ))/nbTot)

		return
def plotCorrelationFunction():

    ####################################################################
    ### Simu

    ###
    for i in range(0, nbMocks__):
        plt.errorbar(numpy.arange(nbBin__), xi_dat__[:, i], fmt="o")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$\xi_{Simu}(s)$")
    myTools.deal_with_plot(False, False, True)
    plt.show()

    ### mean:
    meanXi2D = numpy.mean(xi_dat__, axis=1)
    plt.errorbar(numpy.arange(nbBin__), meanXi2D, fmt="o")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$<\xi_{Simu}(s)>$")
    myTools.deal_with_plot(False, False, True)
    plt.show()

    ### 2D:
    meanXi2D = myTools.convert1DTo2D(meanXi2D, nbBinY2D__, nbBinX2D__)
    myTools.plot2D(
        meanXi2D,
        [minX2D__, maxX2D__, minY2D__, maxY2D__],
        "s_{\perp} \, [h^{-1} Mpc]",
        "s_{\parallel} \, [h^{-1} Mpc]",
        "\\xi_{Simu}^{qf}",
    )

    ####################################################################
    ### Fit

    ###
    for i in range(0, nbMocks__):
        plt.errorbar(numpy.arange(nbBin__), xi_fit__[:, i], fmt="o")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$\xi_{Fit}(s)$")
    myTools.deal_with_plot(False, False, True)
    plt.show()

    ### mean:
    meanXi2D = numpy.mean(xi_fit__, axis=1)
    plt.errorbar(numpy.arange(nbBin__), meanXi2D, fmt="o")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$<\xi_{Fit}(s)>$")
    myTools.deal_with_plot(False, False, True)
    plt.show()

    ### 2D:
    meanXi2D = myTools.convert1DTo2D(meanXi2D, nbBinY2D__, nbBinX2D__)
    myTools.plot2D(
        meanXi2D,
        [minX2D__, maxX2D__, minY2D__, maxY2D__],
        "s_{\perp} \, [h^{-1} Mpc]",
        "s_{\parallel} \, [h^{-1} Mpc]",
        "\\xi_{Fit}^{qf}",
    )
def plotDataAndFit_2D():
    """
	"""

    ### but the xi from a flat array to a 2D array
    xxx = myTools.convert1DTo2D(xxx__, nbBinY2D__, nbBinX2D__)
    yyy_dat = myTools.convert1DTo2D(xi_dat__, nbBinY2D__, nbBinX2D__)
    yyy_fit = myTools.convert1DTo2D(xi_fit__, nbBinY2D__, nbBinX2D__)
    yyy_err = myTools.convert1DTo2D(xi_err__, nbBinY2D__, nbBinX2D__)
    yyy_res = myTools.convert1DTo2D(xi_res__, nbBinY2D__, nbBinX2D__)

    yyy_dat[(yyy_fit == 0.0)] = float("nan")
    yyy_err[(yyy_fit == 0.0)] = float("nan")
    yyy_res[(yyy_fit == 0.0)] = float("nan")
    yyy_fit[(yyy_fit == 0.0)] = float("nan")
    edge = [0.0, 200.0, -200.0, 200.0]

    ### Plot the arrays
    for i in numpy.arange(0, 3):

        coef = numpy.power(xxx, 1.0 * i)

        a = ""
        if i == 1:
            a += "|s|."
        elif i == 2:
            a += "|s|^{2}."

            ### data
        myTools.plot2D(
            coef * yyy_dat,
            edge,
            "s_{\\perp} \\, [h^{-1} Mpc]",
            "s_{\parallel} \\, [h^{-1} Mpc]",
            a + "\\xi_{Simu}(\\, \\overrightarrow{s} \\,)",
            "Simu",
        )
        ### Fit
        myTools.plot2D(
            coef * yyy_fit,
            edge,
            "s_{\\perp} \\, [h^{-1} Mpc]",
            "s_{\parallel} \\, [h^{-1} Mpc]",
            a + "\\xi_{Fit}(\\, \\overrightarrow{s} \\,)",
            "Fit",
        )
        ### residuals
        myTools.plot2D(
            coef * yyy_res,
            edge,
            "s_{\\perp} \\, [h^{-1} Mpc]",
            "s_{\parallel} \\, [h^{-1} Mpc]",
            a + "(\\xi_{Simu}-\\xi_{Fit})/\\xi_{error \, Simu}",
            "residuals",
        )

    return
def replaceValueByMean():
	'''
		
		Replace the values of the correlation by the one of the mean

	'''
	
	rawPath = '/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1547/Results_NicolasDistortionWithDistortion/xi_delta_QSO_result_'

	list1D = numpy.load(rawPath+'1D.npy')
	list2D = numpy.load(rawPath+'2D.npy')
	listMu = numpy.load(rawPath+'Mu.npy')
	listWe = numpy.load(rawPath+'We.npy')
	listMultipol = numpy.load(rawPath+'Multipol.npy')

	cov1D = numpy.load(rawPath+'cov_1D.npy')
	cov2D = numpy.load(rawPath+'cov_2D.npy')
	covMu = numpy.load(rawPath+'cov_Mu.npy')

	nbReal = list1D[0,:].size

	xi1D_[:,1] = numpy.mean(list1D,axis=1)
	xi1D_[:,2] = numpy.sqrt(numpy.diag(cov1D))/numpy.sqrt(nbReal)

	xi2D_[:,:,1] = myTools.convert1DTo2D( numpy.mean(list2D,axis=1), nbBinX2D__,nbBinY2D__)
	xi2D_[:,:,2] = myTools.convert1DTo2D( numpy.sqrt(numpy.diag(cov2D))/numpy.sqrt(nbReal), nbBinX2D__,nbBinY2D__)

	xiMu_[:,:,2] = myTools.convert1DTo2D( numpy.mean(listMu,axis=1), nbBin1D__,nbBinM__)
	xiMu_[:,:,3] = myTools.convert1DTo2D( numpy.sqrt(numpy.diag(covMu))/numpy.sqrt(nbReal), nbBin1D__,nbBinM__)

	result_Multipol_[:,:,1] = numpy.mean(listMultipol,axis=2)
	result_Multipol_[:,:,2] = numpy.var(listMultipol,axis=2)/numpy.sqrt(nbReal)

	xiWe_[:,:,1] = numpy.mean(listWe,axis=2)
	xiWe_[:,:,2] = numpy.var(listWe,axis=2)/numpy.sqrt(nbReal)


	return
xi1D_2 = numpy.dot(data,xi1D)

plt.errorbar(numpy.arange(nbPixel),xi1D,fmt='o',label='Before correction')
plt.errorbar(numpy.arange(nbPixel),xi1D_2,fmt='o',label='After correction')
myTools.deal_with_plot(False,False,True)
plt.show()

if (nbPixel==nbBin1D__):
	xi1D_[:,1] = xi1D_2
	plotXi(0)
	plotXi(1)
	plotXi(2)
	pathToCamb = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/CAMB_2_4/xi-z2.4.dat'
	fitCamb(xi1D_,pathToCamb,0)
else:
	xi2D_[:,:,1] = myTools.convert1DTo2D(xi1D_2,50,100)
	plotXi2D(0)
	plotXi2D(1)
	plotXi2D(2)






#path__ = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/Tests_test_PDFMocksJMC_meanLambda_testNoCap/'
#path = path__ + 'xi_delta_QSO_distortionMatrix_1D_LYA_QSO.txt'
#path = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/Tests_test_PDFMocksJMC_meanLambda_testNoCap//xi_delta_QSO_distortionMatrix_1D_LYA_QSO.txt'
#print path
#data = numpy.loadtxt(path)
#numpy.save('/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/Tests_test_PDFMocksJMC_meanLambda_testNoCap//xi_delta_QSO_distortionMatrix_1D_LYA_QSO',data)
def prepareForBAOFIT():

	### Constants
	param = numpy.asarray( [1.6,-0.336,0.9,0.,0.,3.25,0.962524,3.26,1.966,1.,1.,1.,1.,0.,0.,0.,0.,0.,0.] )
	doBootstraps = False
	nbRegions    = 80
	forest = 'LYA'
	QSO = 'QSO'

	'''
	### Create the .ini file (for simulation)
	i = box__
	j = simul__
	#param = numpy.load('/home/gpfs/manip/mnt0607/bao/hdumasde/Code/Python/Annalyse_BAOFIT/param.npy')
	#param = param[:,0,i*10+j]
	rowPath = '/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1547/noNoisenoCont/Box_00'+str(i)+'/Simu_00'+str(j)+'/Results/'
	path   = rowPath + '/xi_delta_QSO_'
	cov = numpy.load(rowPath + '/subSampling_LYA_QSO_cov_2D.npy')
	cor = numpy.load('/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1547/noNoisenoCont/Results/xi_delta_QSO_result_cor_meanFromFit_2D.npy')
	inputFile    = rowPath + 'xi_delta_QSO_2D_LYA_QSO.txt'
	pathToOutFit = rowPath + 'BaoFit_q_f/'
	pathToData   = rowPath + 'BaoFit_q_f/'
	pathToIni    = rowPath + 'BaoFit_q_f/'
	createIni2(inputFile, pathToOutFit, pathToData,pathToIni,'2D',param)
	'''
	
	### For data
	rowPath = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy_nicolasEstimator_method1/'
	path    = rowPath + '/xi_delta_QS0_'
	cov     = numpy.load('/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy/subSampling_'+forest+'_'+QSO+'_cov_2D.npy')
	cor     = numpy.load('/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1547/Results_RandomPosInCell/xi_delta_QSO_result_cor_2D_allSubSamplingFromFit.npy')
	inputFile    = rowPath + 'xi_delta_QSO_2D_'+forest+'_'+QSO+'.txt'
	pathToDistortionMatrix = rowPath + '/xi_delta_QSO_distortionMatrix_2D_'+forest+'_'+QSO+'.txt'
	pathToOutFit = rowPath + 'BaoFit_q_f__'+forest+'__'+QSO+'/'
	pathToData   = rowPath + 'BaoFit_q_f__'+forest+'__'+QSO+'/'
	pathToIni    = rowPath + 'BaoFit_q_f__'+forest+'__'+QSO+'/'

	if (doBootstraps):
		pathToOutFit += 'Bootstraps/boot_'+str(wickIdx__).zfill(4)+'/'
		pathToData   += 'Bootstraps/boot_'+str(wickIdx__).zfill(4)+'/'
		pathToIni    += 'Bootstraps/boot_'+str(wickIdx__).zfill(4)+'/'
		subprocess.call('mkdir ' + pathToIni, shell=True)
		#subprocess.call('cp '+rowPath+'/BaoFit_q_f/bao2D.grid ' + pathToIni, shell=True)
	
		### Set the seed
		numpy.random.seed(seed=42)
		array = numpy.random.choice(numpy.arange(1000000).astype(int), size=10000, replace=False)
		numpy.random.seed(seed=array[wickIdx__])
		randomList = numpy.random.randint( 0, high=nbRegions, size=nbRegions)
		print array[wickIdx__]

		subsampling = numpy.load(rowPath+'/subsampling_LYA_QSO_2D.npy')
		bootstrap   = numpy.zeros( shape=(nbBin2D__,nbRegions) )
	
		xi2D1D = numpy.zeros(nbBin2D__)
		for i in randomList:
			bootstrap[:,i] = subsampling[:,i]
		xi2D1D = numpy.mean( bootstrap, axis=1)
		cov = numpy.cov(bootstrap)/nbRegions
		xi2D_ = numpy.zeros(shape=(nbBinX2D__,nbBinY2D__,3))
		xi2D_[:,:,1] = myTools.convert1DTo2D(xi2D1D,nbBinX2D__,nbBinY2D__)
	else:
		### Get the correlation (data)
		xi1D_, xi2D_, xiMu_, xiWe_ = loadData('',inputFile,2)

	### Create the 'bao2D.grid' file
	createIni2(inputFile, pathToOutFit, pathToData,pathToIni,'2D',param)

	'''
	### Correlation
	correlation = numpy.zeros( shape=(nbBin2D__,2) )
	indexMatrix = numpy.arange(nbBin2D__)
	correlation[:,0] = (indexMatrix%nbBinY2D__)*nbBinX2D__ + indexMatrix/nbBinY2D__
	correlation[:,1] = xi2D_[:,:,1].flatten()
	cutCorrelation = (correlation[:,1]!=0.)
	numpy.savetxt( pathToData + '/bao2D.data',zip(correlation[:,0][cutCorrelation],correlation[:,1][cutCorrelation]),fmt='%u %1.20e')
	
	### Covariance matrix
	cov = myTools.getCovarianceMatrix(cor,numpy.diag(cov))
	covarianceMatrix = numpy.zeros( shape=(nbBin2D__*nbBin2D__,3) )
	indexMatrix1 = numpy.arange(nbBin2D__*nbBin2D__).reshape(nbBin2D__,nbBin2D__)/nbBin2D__
	indexMatrix2 = numpy.arange(nbBin2D__*nbBin2D__).reshape(nbBin2D__,nbBin2D__)%nbBin2D__
	indexMatrix1 = (indexMatrix1%nbBinY2D__)*nbBinX2D__ + indexMatrix1/nbBinY2D__
	indexMatrix2 = (indexMatrix2%nbBinY2D__)*nbBinX2D__ + indexMatrix2/nbBinY2D__
	covarianceMatrix[:,0] = numpy.triu(indexMatrix1,k=0).flatten()
	covarianceMatrix[:,1] = numpy.triu(indexMatrix2,k=0).flatten()
	covarianceMatrix[:,2] = numpy.triu(cov,k=0).flatten()
	cutCovarMatrix = (covarianceMatrix[:,2]!=0.)
	if (covarianceMatrix[:,2][cutCovarMatrix].size != int(nbBin2D__*(nbBin2D__+1.)/2.) ):
		print '  xi_delta_QSO.py::prepareForBAOFIT()  size of covariance matrix is incorrect'
		print '  size covariance matrix', cutCovarMatrix[:,2][cutCovarMatrix].size
		print '  size it should have', nbBin2D__*(nbBin2D__+1.)/2.
		return
	numpy.savetxt( pathToData + '/bao2D.cov',zip(covarianceMatrix[:,0][cutCovarMatrix],covarianceMatrix[:,1][cutCovarMatrix],covarianceMatrix[:,2][cutCovarMatrix]),fmt='%u %u %1.20e')
	
	
	### Distortion matrix
	dmatData = numpy.loadtxt(pathToDistortionMatrix)
	distortionMatrix = numpy.zeros( shape=(nbBin2D__*nbBin2D__,3) )
	distortionMatrix[:,0] = indexMatrix1.flatten()
	distortionMatrix[:,1] = indexMatrix2.flatten()
	distortionMatrix[:,2] = dmatData.flatten()
	cutDistortionMatrix = (distortionMatrix[:,2]!=0.)
	numpy.savetxt( pathToData + '/bao2D.dmat',zip(distortionMatrix[:,0][cutDistortionMatrix], distortionMatrix[:,1][cutDistortionMatrix], distortionMatrix[:,2][cutDistortionMatrix]),fmt='%u %u %1.20e')
	'''

	### Send the fit
	command = '/home/gpfs/manip/mnt0607/bao/hdumasde/Program/deepzot/bin/baofit -i ' + pathToIni + 'bao2D.ini' #--parameter-scan'   ### --toymc-samples 10000
	print command
	subprocess.call(command, shell=True)
	
	if (doBootstraps):
		subprocess.call('rm '+ pathToData + '/bao2D.ini', shell=True)
		subprocess.call('rm '+ pathToData + '/bao2D.grid', shell=True)
		subprocess.call('rm '+ pathToData + '/bao2D.data', shell=True)
		subprocess.call('rm '+ pathToData + '/bao2D.cov', shell=True)

	return