#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os,sys
# import getopt
##maybe it's supposed to be sys.argv[:] ... maybe
tmpargv = sys.argv
sys.argv = [ '-n' ]
from ROOT import *
sys.argv = tmpargv

# import psyco
# psyco.full()

# from time import sleep
# from optparse import OptionParser

# from ROOT_Tools import RootHandler
# from CommandLineMod import CommandLine


if __name__ == "__main__":

	gStyle.SetTitleBorderSize	( 0			)   # duh
	gStyle.SetPadBorderMode		( 0			)
	gStyle.SetOptStat			( 111111	)	# Good standard stats display.
	gStyle.SetOptFit			( 1111		)	# Display fit info if available.
	gStyle.SetMarkerStyle		( 3			)	# double cross.
	gStyle.SetMarkerSize		( 1.0			)	# double cross.
	gStyle.SetOptTitle			( 1			)	# Display histogram titles.
	gStyle.SetCanvasColor		( 10		)	# Background color (white)
	gStyle.SetPadColor			( 10		)	# Background color (white)
	gStyle.SetFillColor			( 10		)	# Background color (white)
	gStyle.SetHistFillColor		( 10		)	# Background color (white)
	gStyle.SetFrameFillColor	( 10		)	# Background color (white)
#	gStyle.SetFuncColor			( 10		)	# Background color (white)
	gStyle.SetStatColor			( 10		)	# Background color (white)
	gStyle.SetTextFont			( 132			)

	gStyle.SetPadRightMargin	(0.12)
	gStyle.SetPadLeftMargin		(0.13)
	gStyle.SetPadBottomMargin	(0.18)
	gStyle.SetPadTopMargin	(0.10)

	gStyle.SetTitleFillColor	( 10		)	# Background color (white)
	gStyle.SetTitleFontSize		( 0.08		)	# Title Font Size
	gStyle.SetTitleX			( 0.10		)	# Title box width
	gStyle.SetTitleW			( 0.50		)	# Title box width
	gStyle.SetTitleH			( 0.09		)	# Title box width
	gStyle.SetTitleBorderSize	( 0			)

	gStyle.SetStatW				( 0.3		)	# Statistic box width
	gStyle.SetStatX				( 0.95		)	# Statistic box width
	gStyle.SetStatY				( 0.4		)	# Statistic box width
	gStyle.SetStatBorderSize	( 1			)
	gStyle.SetLegendBorderSize	( 1			)
	gStyle.SetPalette			( 1			)	# Nicer colour scale for 2D histograms.
	gStyle.SetPaperSize			( 20., 24.	)
	gStyle.SetHistLineWidth		( 1			)

	
	gStyle.SetLabelFont(132)
	gStyle.SetLabelFont(132,"X")
	gStyle.SetLabelFont(132,"Y")
	gStyle.SetLabelFont(132,"Z")
	gStyle.SetLabelFont(132,"T")
	gStyle.SetTitleFont(132)
	gStyle.SetTitleFont(132,"X")
	gStyle.SetTitleFont(132,"Y")
	gStyle.SetTitleFont(132,"Z")
	gStyle.SetTitleFont(132,"T")
	gStyle.SetLabelSize(0.06)
	gStyle.SetLabelSize(0.06,"X")
	gStyle.SetLabelSize(0.06,"Y")
	gStyle.SetLabelSize(0.06,"Z")
	gStyle.SetLabelSize(0.06,"T")
	gStyle.SetTitleSize(0.06   )
	gStyle.SetTitleSize(0.06,"X")
	gStyle.SetTitleSize(0.06,"Y")
	gStyle.SetTitleSize(0.06,"Z")
	gStyle.SetTitleSize(0.06,"T")
	gStyle.SetTextFont(132)
	gStyle.SetTextSize(0.1)
	# SetTextAttributes
	# SetTextAlign
	# SetTextAngle
	# SetTextColor
	# SetTextFont
	# SetTextSizePixels

	gStyle.SetStatFont(132)

	gROOT.SetBatch()
	gROOT.ForceStyle()

	# No Stats !
	gStyle.SetOptStat			( 0	)	# Good standard stats display.
	gStyle.SetOptFit			( 0	)	# Display fit info if available.

	gStyle.SetTitleW			( 0.70		)	# Title box width

	gStyle.SetPaperSize(10,15)
	#gStyle.SetCanvasDefH(600)  # Defaults
	# gStyle.SetCanvasDefW(700)  # Defaults
	# gStyle.SetCanvasDefW(1000)

	# We can have many files in input
	# FileList	= sys.argv[1].split(',')
	# FileList	= 'sum84-9-1_clk_raw.root'
	FileList	= ['sum74-4-1_clk_raw.root','sum474-1_2_21-1_clk_raw.root']
	# Fname	= 'sum74-4-1_clk_raw.root'

	# HistName 	= 
	# OutputFile	= sys.argv[3]

	Canv = TCanvas()
	# Leg = TLegend(0.7, 0.7,0.9,0.9)

	MinX = 0
	MaxX = 400
	MinZ = 0.7
	MaxZ = 270

	# pact[474][11]=[ 1.000, -4.857, -40.000, 811.068 ]

	# Title	= [';TDC hit time [ns]',';Drift time [#mu s];Residuals [#mu m]']
	Title	= ';PC 5 hit width;PC 6 hit width'
	# LegName = ['Before','After']

	# Canv.Divide(len(HistList))
	# Canv.Divide(len(FileList))

	Canv.SetLogz()

	# F = []
	F = TFile(FileList[0])

	# for i,f in enumerate(FileList):
	H	= F.Get('PACT/pc6vs5_11_before')

	H.SetTitle( Title)
 	H.SetAxisRange(MinX,MaxX,'X')
 	# H.SetAxisRange(MinZ,MaxZ,'Z')

	# H.SetTitleOffset( 0.9, 'Y')

	H.Draw("colz")

	# pact[474][11]=[ 1.000, -4.857, -40.000, 811.068 ]
	# pact[74][11]=[ 1.000, -6.000, -20.000, 758.523  ]

	Cut1 = TF1("Cut1","x -20.0",MinX,MaxX)
	Cut2 = TF1("Cut2","-6.000 * x + 758.523",MinX,MaxX)

	Cut1.Draw('same')
	Cut2.Draw('same')

	Canv.Print('../PsPACT_s74.eps')

	F.Close()

###################################
	F = TFile(FileList[1])

	Canv.Clear()

	H	= F.Get('PACT/pc6vs5_11_before')

	H.SetTitle( Title)
 	H.SetAxisRange(MinX,MaxX,'X')
 	# H.SetAxisRange(MinZ,MaxZ,'Z')

	# H.SetTitleOffset( 0.9, 'Y')

	H.Draw("colz")

	Cut1 = TF1("Cut1","x -40.0",MinX,MaxX)
	Cut2 = TF1("Cut2","-4.857 * x + 811.068",MinX,MaxX)

	Cut1.Draw('same')
	Cut2.Draw('same')

	Canv.Print('../PsPACT_s474.eps')

	Canv.Clear()