#PlotQuantumEfficiency.py			Lance Simms, LLNL 2010
#
#PURPOSE:
#  To plot the quantum efficiency of the CZT detector.  This is done by taking 
#  an input data file that has hit counts at energy intervals from 10-600 keV.
#
#KEYWORDS:
#  FileName: str
#    The relative path to the file that contains the data to be histogrammed
#    Assumed to live in /Users/lances/Desktop/Software/GEANT4/work/CZTCubeSat/Data/QuantumEfficiency/
#  FileName2: str
#    The relative path to the file that contains the data to be histogrammed
#    Assumed to live in /Users/lances/Desktop/Software/GEANT4/work/CZTCubeSat/Data/QuantumEfficiency/
#  PlotTwo: int
#    0 - Only plot data from FileName
#    1 - Plot data from FileName and FileName2
#
#EXAMPLES:
#  run PlotQuantumEfficiency.py --FileName=QuantumEfficiencyTest_5mm_NoWindow.dat --PlotTwo=0
#  run PlotQuantumEfficiency.py --FileName=QuantumEfficiencyTest_5mm_NoWindow.dat --FileName2=QuantumEfficiencyTest_1mm_NoWindow.dat --PlotTwo=0
#  run PlotQuantumEfficiency.py --FileName=QuantumEfficiencyTest_5mm_Window.dat --FileName2=QuantumEfficiencyTest_1mm_Window.dat --PlotTwo=0
##########
#
#Get the filename and options from command line
SaveFig=1
import os
UserDir = os.path.expanduser('~')
execfile(UserDir+'/Desktop/Software/Python/CZTCubeSat/CZTCubeSatSetup.py')
execfile(UserDir+'/Desktop/Software/Python/CZTCubeSat/BigFontPlotSettings.py')

from ReturnPlottableHistogram import *
from ReturnRebinnedHistogram import *
import getopt
import pdb

##PARSE KEYWORDS
keywords    = ['FileName=', 'FileName2=', 'PlotTwo=']

#DEFAULT VALUES FOR KEYWORDS
FileName           = 'QuantumEfficiencyTest_5mm_NoWindow.dat'
FileName2          = 'QuantumEfficiencyTest_1mm_NoWindow.dat'
PlotTwo            = 1

#NOW PARSE KEYWORDS
opts, extraparams = getopt.getopt(sys.argv[1:],'',keywords)
for o,p in opts:
  if o in ['--FileName']:
    FileName  = p
  if o in ['--FileName2']:
    FileName2 = p
  if o in ['--PlotTwo']:
    PlotTwo  = int(p)

#Read in the data from the file.  The references to structural parts are the number absorbed 
#in that particular area
E, Hits1, Hits2, UpperColCone, UpperColCyl, MiddleColCyl, LowerColCyl, \
   EntranceWindow, CZTDet, Escaped = \
   readcol(UserDir+'/Desktop/Software/GEANT4/work/CZTCubeSat/Data/QuantumEfficiency/'+FileName, twod=False)

#Make it into histogram values
EHist, Hits1Hist  = ReturnPlottableHistogram(E, Hits1)
EHist, CZTDetHist = ReturnPlottableHistogram(E, CZTDet)

#Read in the second file
if PlotTwo == 1:
  E_2, Hits1_2, Hits2_2, UpperColCone_2, UpperColCyl_2, MiddleColCyl_2, LowerColCyl_2, \
     EntranceWindow_2, CZTDet_2, Escaped_2 = \
     readcol(UserDir+'/Desktop/Software/GEANT4/work/CZTCubeSat/Data/QuantumEfficiency/'+FileName2, twod=False)

  #Make it into histogram values
  EHist_2, Hits1Hist_2  = ReturnPlottableHistogram(E_2, Hits1_2)
  EHist_2, CZTDetHist_2 = ReturnPlottableHistogram(E_2, CZTDet_2)


#Now histogram the values
mplot.figure(1)
mplot.clf()
mplot.plot(EHist, Hits1Hist,  label='Energy Deposit Recorded (5mm)')
mplot.plot(EHist, CZTDetHist, label='Energy Totally Absorbed (5mm)')
if PlotTwo == 1:
  mplot.plot(EHist_2, Hits1Hist_2,  label='Energy Deposit Recorded (1mm)')
  mplot.plot(EHist_2, CZTDetHist_2, label='Energy Totally Absorbed (1mm)')

#Set plot labels and limits
mplot.xlabel(r'Energy (keV)', fontsize=16, fontweight='bold')
mplot.ylabel('Count')
mplot.xlim([10,610])
mplot.ylim([0,11000])
mplot.legend()

#Allow for saving
SaveFig=1
if SaveFig == 1:
  mplot.savefig(FileName.replace('.dat','.png'))
