##PlotFE55wSingleDoubleEventsIPC.py
##
##PURPOSE:
##  To plot FE55 histograms for the various number of channels being averaged.
##  These files are contained in 
##        /nfs/slac/g/ki/ki03/lances/H1RG-022/ASIC/08Jun29/
##
##SETUP:
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/ThesisPlotSettings.py')
from FE55_Extract_Database import *
import FE55_Extract_Database; reload(FE55_Extract_Database); from FE55_Extract_Database import *
PlotDir = '/nfs/slac/g/ki/ki03/lances/KPNO/Latex/Thesis/Fe55/Figures/'
SaveImg  = 0

x_k_alpha = 970  #K_alpha peak
x_k_beta  = 1060 #K_beta peak

QueryStr = ' WHERE ChAvg>=2 and ChAvg!=6 and X > 10 and Y > 10 and X < 90 and Y < 90 and ReadNum > 10 and NumPix=1 and NEGPIX=0 and MaxQ > 900'

ADU1_1s, ADU2_1s, ADU3_1s,ADU4_1s, ADU5_1s, ADU6_1s, ADU7_1s, ADU8_1s, ADU9_1s = FE55_Extract_Database('H1RG-022','ASIC','08Jun29','1000', \
QueryString=QueryStr ,Gain=4, NumADCs=2, WindowM=1, Mode=3, UseRef=0)
BinsIn  = arange(80)
Bins1, N1 = histOutline.histOutline([ADU4_1s,ADU5_1s,ADU6_1s,ADU7_1s], binsIn=BinsIn)
mplot.clf()
mplot.plot(Bins1, N1, 'r-', label='4 Ch')

QueryStr = ' WHERE ChAvg>=2 and ChAvg!=6 and X > 10 and Y > 10 and X < 90 and Y < 90 and ReadNum > 10 and NumPix=5 and NEGPIX=0 and MaxQ > 960 and ADU1 < 90 and ADU2 < 90 and ADU3 < 90 and ADU4<90'
ADU1_5s, ADU2_5s, ADU3_5s,ADU4_5s, ADU5_5s, ADU6_5s, ADU7_5s, ADU8_5s, ADU9_5s = FE55_Extract_Database('H1RG-022','ASIC','08Jun29','1000', \
QueryString=QueryStr ,Gain=4, NumADCs=2, WindowM=1, Mode=3, UseRef=0)
BinsIn  = arange(280)
Bins5, N5 = histOutline.histOutline([ADU1_5s,ADU2_5s,ADU3_5s,ADU4_5s], binsIn=BinsIn)
mplot.plot(Bins5, N5, 'b-', label='4 Ch')

QueryStr = ' WHERE ChAvg>=2 and ChAvg!=6 and X > 10 and Y > 10 and X < 90 and Y < 90 and ReadNum > 10 and NumPix=2 and NEGPIX=0 and MaxQ > 450 and ADU1 > 450 and ADU2 > 450 '

ADU1_2s, ADU2_2s, ADU3_2s,ADU4_2s, ADU5_2s, ADU6_2s, ADU7_2s, ADU8_2s, ADU9_2s = FE55_Extract_Database('H1RG-022','ASIC','08Jun29','1000', \
QueryString=QueryStr ,Gain=4, NumADCs=2, WindowM=1, Mode=3, UseRef=0)
BinsIn  = arange(80)
Bins2, N2 = histOutline.histOutline([ADU4_2s,ADU5_2s,ADU6_2s,ADU7_2s,ADU8_2s,ADU9_2s], binsIn=BinsIn)
mplot.plot(Bins2, N2, 'g-', label='4 Ch')

if SaveImg == 1:
  mplot.savefig(PlotDir+'Fe55Histogram_SingleDoublePixelEvents_Gain4.eps')
  mplot.savefig(PlotDir+'Fe55Histogram_SingleDoublePixelEvents_Gain4.png')
