#!/usr/bin/env python
# this script just determines the avg backgorund of the atom and no-atom histogram for the 7ns photons and the integrated counts for the no-atom histogram = heralding eff
import matplotlib.pyplot as plt
import numpy as np                  


""" Useful parameters """
# all the times are expressed in nanoseconds. Frequencies in GHz.
l = 0.03
gamma_0 = 1 / 26.2348  # e-9
gamma_p = 1 / 7.08  # e-9
dt_tx = 1

"""
data import
"""
infile_tx_decaying = '2015_10_30-11_03_non_reversed_combined_histo_tx_1ns'
raw_tx_decaying = np.genfromtxt(infile_tx_decaying)
t_offset = 908.5

time = (raw_tx_decaying[:, 1])
atom = raw_tx_decaying[:, 4]
no_atom = raw_tx_decaying[:, 8]

avg_bg_atom = np.mean(atom[t_offset+150.5:t_offset+250.5])
avg_bg_no_atom = np.mean(no_atom[t_offset+150.5:t_offset+250.5])

#summed counts -tau : +6tau, no atom
eta = np.sum(no_atom[t_offset-7.5:t_offset+42.5]-avg_bg_no_atom)
print(avg_bg_atom)
print(avg_bg_no_atom)
print(eta)

delta=  ((no_atom-avg_bg_no_atom)-  (atom-avg_bg_atom) ) / (eta*dt_tx*1e-9)
print('acc corrected extinction:')
print(1-sum(delta[t_offset-7.5:t_offset+42.5])*dt_tx*1e-9)

#plt.plot(time-t_offset, atom)
plt.plot(time-t_offset, delta)
plt.show()
