import math 
import numpy as np
import sympy as smp
import matplotlib.pyplot as plt
from numpy import *
import scipy as sp
from scipy.integrate import quad
from scipy.integrate import fixed_quad
from cmath import sqrt
from sympy import Eq,solve

x = smp.symbols('x', real=True)
y = smp.symbols('y', real=True)
z = smp.symbols('z', real=True)

s = smp.symbols('s', real=True)
t = smp.symbols('t', real=True)
u = smp.symbols('u', real=True)

d = np.loadtxt("kloe2016.txt")
X = d[:,2]
Y = d[:,3]
ampd = d[:,4]
#x, y dostanu z textaku KLOE2016, chci dopocitat s,t,u
#x, y a 4m**2 jsou prave strany, leve jsou avisle na s,t,u
m_eta = 0.5473 #MeV
m_pi0 = 0.1349768  #MeV
Q_eta = (m_eta - 3*m_pi0)

eq1 = Eq(sqrt(3)*(t-s)/(2*m_eta*Q_eta), X[0])
eq2 = Eq((3*((m_eta - m_pi0)**2 - u)/(2*m_eta*Q_eta))-1, Y[0])
eq3 = Eq(s+t+u, m_eta**2 + 3*m_pi0**2)

file = open('hodnoty.txt','w')
for i in range(len(X)):
    A = ([-sqrt(3)/(2*m_eta*Q_eta), sqrt(3)/(2*m_eta*Q_eta),0],[0,0,-3/(2*m_eta*Q_eta)],[1,1,1])
    b = [X[i],Y[i]+1-3*(m_eta -m_pi0)**2/(2*m_eta*Q_eta),(m_eta**2+3*m_pi0**2)]
    vysledek = np.real(np.linalg.solve(A, b))
    file.write(str(vysledek[0]) + ' '+ str(vysledek[1]) + ' ' + str(vysledek[2]) + ' ' + str(ampd[i]))
    file.write("\n")
file.close()

A = ([-sqrt(3)/(2*m_eta*Q_eta), sqrt(3)/(2*m_eta*Q_eta),0],[0,0,-3/(2*m_eta*Q_eta)],[1,1,1])
b = [-0.839,0.350+1-3*(m_eta -m_pi0)**2/(2*m_eta*Q_eta),(m_eta**2+3*m_pi0**2)]
vysledek = np.real(np.linalg.solve(A, b))

d = np.loadtxt("hodnoty.txt")
s = (d[:,2])
t = (d[:,0])
u = (d[:,1])
resultsTEST = [0] * (len(s))
chybaTEST = [0] * (len(s))

procento = 0.01*4*m_pi0**2
print("u")
for i in range(len(s)):
     a = abs(u[i] - 4*m_pi0**2)
     if a<procento:
         print(i)
     else:
         a=a

print("t")
for i in range(len(s)):
     a = abs(t[i] - 4*m_pi0**2)
     if a<procento:
         print(i)
     else:
         a=a

print("s")
for i in range(len(s)):
     a = abs(s[i] - 4*m_pi0**2)
     if a<procento:
         print(i)
     else:
         a=a










