#!/usr/bin/python
from ROOT import TH1D, TCanvas, TGraphErrors
import ROOT 
import sys, math, re

def plot_results(args):
    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetOptFit(111)
    ROOT.gStyle.SetPalette(1)
    ROOT.gStyle.SetCanvasColor(10)
    ROOT.gStyle.SetLabelFont(132)
    ROOT.gStyle.SetLabelFont(132,'X')
    ROOT.gStyle.SetLabelFont(132,'Y')
    ROOT.gStyle.SetLabelSize(0.06,'X')
    ROOT.gStyle.SetLabelSize(0.06,'Y')
    ROOT.gStyle.SetLabelFont(132,'Z')
    ROOT.gStyle.SetTitleFont(132)
    ROOT.gStyle.SetTitleFont(132,'X')
    ROOT.gStyle.SetTitleFont(132,'Y')
    ROOT.gStyle.SetTitleSize(0.06,'X')
    ROOT.gStyle.SetTitleSize(0.06,'Y')
    ROOT.gStyle.SetTitleFont(132,'Z')
    ROOT.gStyle.SetTextFont(132)
    ROOT.gStyle.SetStatFont(132)
    ROOT.gStyle.SetTitleFillColor(10)
    ROOT.gStyle.SetStatColor(10)
    ROOT.gStyle.SetPadLeftMargin(0.15)
    ROOT.gStyle.SetPadRightMargin(0.15)
    ROOT.gStyle.SetPadTopMargin(0.05)
    ROOT.gStyle.SetPadBottomMargin(0.15)


    fr = open(args[0], 'r')
    hr = TGraphErrors(14)
    hr.SetTitle(';Data Set Number;(#rho - #frac{3}{4}) #times 10^{4}')
    hr.SetMarkerColor(2)
    hr.SetMarkerStyle(20)
    hr.SetLineColor(2)
    c = 0
    for line in fr:
        m = line.split()
        if m[0] == '#':
            continue
        else:
            #print m
            hr.SetPoint(c, float(m[0]), (float(m[9]) - 3./4.)*10000)
            hr.SetPointError(c, 0, math.sqrt(float(m[2])**2 + 
                                             float(m[3])**2))
            c+=1
    # hr.Fit('pol0')
    fd = open(args[1], 'r')
    hd = TGraphErrors(14)
    hd.SetTitle(';Data Set Number;(#delta - #frac{3}{4}) #times 10^{4}')
    hd.SetMarkerColor(2)
    hd.SetMarkerStyle(20)
    hd.SetLineColor(2)
    c = 0
    for line in fd:        
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hd.SetPoint(c, float(m[0]), (float(m[9]) - 3./4.) * 10000)
            hd.SetPointError(c, 0, math.sqrt(float(m[2])**2 
                                             + float(m[3])**2))
            c+=1
    # hd.Fit('pol0')
    fx = open(args[2], 'r')
    hx = TGraphErrors(9)
    hx.SetTitle(';Data Set Number;(#xi - 1) #times 10^{4}')
    hx.SetMarkerColor(2)
    hx.SetMarkerStyle(20)
    hx.SetLineColor(2)
    c = 0
    for line in fx:        
        m = line.split()
        if m[0]:
            if m[0] == '#':
                continue
            else:
                print m
                hx.SetPoint(c, float(m[0]), (float(m[10]) - 1.)*10000)
                hx.SetPointError(c, 0, float(m[11])*10000)
                c+=1
    # hx.Fit('pol0')
    c = TCanvas('c','c',800,800)
    c.Divide(1,3)
    c_1 = c.cd(1)
    c_1.SetGridx()
    c_1.SetGridy()
    hr.Draw('ap')

    c_2 = c.cd(2)
    c_2.SetGridx()
    c_2.SetGridy()
    hd.Draw('ap')

    c_3 = c.cd(3)
    c_3.SetGridx()
    c_3.SetGridy()
    hx.Draw('ap')

    c.Print('TWIST_final_results.eps')

def plot_EcalModeDiffs(args):
    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetOptFit(111)
    ROOT.gStyle.SetPalette(1)
    ROOT.gStyle.SetCanvasColor(10)
    ROOT.gStyle.SetLabelFont(132)
    ROOT.gStyle.SetLabelFont(132,'X')
    ROOT.gStyle.SetLabelFont(132,'Y')
    ROOT.gStyle.SetLabelFont(132,'Z')
    ROOT.gStyle.SetTitleFont(132)
    ROOT.gStyle.SetTitleFont(132,'X')
    ROOT.gStyle.SetTitleFont(132,'Y')
    ROOT.gStyle.SetLabelSize(0.06)
    ROOT.gStyle.SetLabelSize(0.06,'X')
    ROOT.gStyle.SetLabelSize(0.06,'Y')
    ROOT.gStyle.SetLabelSize(0.06,'Z')
    ROOT.gStyle.SetTitleSize(0.06)
    ROOT.gStyle.SetTitleSize(0.06,'X')
    ROOT.gStyle.SetTitleSize(0.06,'Y')
    ROOT.gStyle.SetTitleOffset(1.,'X')
    ROOT.gStyle.SetTitleOffset(1.,'Y')
    ROOT.gStyle.SetTitleFont(132,'Z')
    ROOT.gStyle.SetTextFont(132)
    ROOT.gStyle.SetStatFont(132)
    ROOT.gStyle.SetTitleFillColor(10)
    ROOT.gStyle.SetStatColor(10)
    ROOT.gStyle.SetPadLeftMargin(0.15)
    ROOT.gStyle.SetPadRightMargin(0.15)
    ROOT.gStyle.SetPadTopMargin(0.10)
    ROOT.gStyle.SetPadBottomMargin(0.20)

    fr = open(args[0], 'r')
    hr = TGraphErrors(14)
    hr.SetMarkerColor(2)
    hr.SetMarkerStyle(20)
    hr.SetLineColor(2)
    hr.SetTitle(';Data Set Number;#Delta#rho (Shifted - Scaled)/2 #times 10^{4}')
    c = 0
    for line in fr:
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hr.SetPoint(c, float(m[0]), (float(m[1]) + float(m[3]) \
                                             - float(m[4]) - float(m[6]))/2.)
            hr.SetPointError(c, 0, float(m[2])*0.03)
            c+=1
    hr.Fit('pol0')
    fd = open(args[1], 'r')
    hd = TGraphErrors(14)
    hd.SetTitle(';Data Set Number;#Delta#delta (Shifted - Scaled)/2  #times 10^{4}')
    hd.SetMarkerColor(2)
    hd.SetMarkerStyle(20)
    hd.SetLineColor(2)
    c = 0
    for line in fd:        
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hd.SetPoint(c, float(m[0]), (float(m[1]) - float(m[4]) + \
                                             float(m[3]) - float(m[6]))/2.)
            hd.SetPointError(c, 0, float(m[2])*0.03)
            c+=1
    hd.Fit('pol0')
    fx = open(args[2], 'r')
    hx = TGraphErrors(9)
    hx.SetTitle(';Data Set Number;#Delta#xi (Shifted - Scaled)/2 #times 10^{4}')
    hx.SetMarkerColor(2)
    hx.SetMarkerStyle(20)
    hx.SetLineColor(2)
    c = 0
    for line in fx:        
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hx.SetPoint(c, float(m[0]), (float(m[1]) + float(m[3]) - \
                                             float(m[4]) - float(m[6]))/2.)
            hx.SetPointError(c, 0, float(m[2])*0.03)
            c+=1
    hx.Fit('pol0')
    c = TCanvas('c','c',800,800)
    c.Divide(1,3)
    c_1 = c.cd(1)
    c_1.SetGridx()
    c_1.SetGridy()
    hr.Draw('ap')

    c_2 = c.cd(2)
    c_2.SetGridx()
    c_2.SetGridy()
    hd.Draw('ap')

    c_3 = c.cd(3)
    c_3.SetGridx()
    c_3.SetGridy()
    hx.Draw('ap')

    c.Print('TWIST_ecal_diffs.eps')

def plot_BlindAverages(args):
    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetOptFit(111)
    ROOT.gStyle.SetPalette(1)
    ROOT.gStyle.SetCanvasColor(10)
    ROOT.gStyle.SetLabelFont(132)
    ROOT.gStyle.SetLabelFont(132,'X')
    ROOT.gStyle.SetLabelFont(132,'Y')
    ROOT.gStyle.SetLabelFont(132,'Z')
    ROOT.gStyle.SetTitleFont(132)
    ROOT.gStyle.SetTitleFont(132,'X')
    ROOT.gStyle.SetTitleFont(132,'Y')
    ROOT.gStyle.SetLabelSize(0.06)
    ROOT.gStyle.SetLabelSize(0.06,'X')
    ROOT.gStyle.SetLabelSize(0.06,'Y')
    ROOT.gStyle.SetLabelSize(0.06,'Z')
    ROOT.gStyle.SetTitleSize(0.06)
    ROOT.gStyle.SetTitleSize(0.06,'X')
    ROOT.gStyle.SetTitleSize(0.06,'Y')
    ROOT.gStyle.SetTitleOffset(1.,'X')
    ROOT.gStyle.SetTitleOffset(1.,'Y')
    ROOT.gStyle.SetTitleFont(132,'Z')
    ROOT.gStyle.SetTextFont(132)
    ROOT.gStyle.SetStatFont(132)
    ROOT.gStyle.SetTitleFillColor(10)
    ROOT.gStyle.SetStatColor(10)
    ROOT.gStyle.SetPadLeftMargin(0.15)
    ROOT.gStyle.SetPadRightMargin(0.15)
    ROOT.gStyle.SetPadTopMargin(0.10)
    ROOT.gStyle.SetPadBottomMargin(0.20)

    fr = open(args[0], 'r')
    hr = TGraphErrors(14)
    hr.SetMarkerColor(2)
    hr.SetMarkerStyle(20)
    hr.SetLineColor(2)
    hr.SetTitle(';Data Set Number;#Delta#rho #times 10^{4}')
    c = 0
    for line in fr:
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hr.SetPoint(c, float(m[0]), (float(m[1]) + float(m[3]) \
                                             + float(m[4]) + float(m[6]))/2.)
            hr.SetPointError(c, 0, float(m[2]))
            c+=1
    hr.Fit('pol0')
    fd = open(args[1], 'r')
    hd = TGraphErrors(14)
    hd.SetTitle(';Data Set Number;#Delta#delta  #times 10^{4}')
    hd.SetMarkerColor(2)
    hd.SetMarkerStyle(20)
    hd.SetLineColor(2)
    c = 0
    for line in fd:        
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hd.SetPoint(c, float(m[0]), (float(m[1]) + float(m[4]) + \
                                             float(m[3]) + float(m[6]))/2.)
            hd.SetPointError(c, 0, float(m[2]))
            c+=1
    hd.Fit('pol0')
    fx = open(args[2], 'r')
    hx = TGraphErrors(9)
    hx.SetTitle(';Data Set Number;#Delta#xi #times 10^{4}')
    hx.SetMarkerColor(2)
    hx.SetMarkerStyle(20)
    hx.SetLineColor(2)
    c = 0
    for line in fx:        
        m = line.split()
        if m[0] == '#':
            continue
        else:
            # print m
            hx.SetPoint(c, float(m[0]), (float(m[1]) + float(m[3]) + \
                                             float(m[4]) + float(m[6]))/2.)
            hx.SetPointError(c, 0, float(m[2])*0.03)
            c+=1
    hx.Fit('pol0')
    c = TCanvas('c','c',800,200)
    c.Divide(1,3)
    c_1 = c.cd(1)
    c_1.SetGridx()
    c_1.SetGridy()
    hr.Draw('ap')

    #c_2 = c.cd(2)
    #c_2.SetGridx()
    #c_2.SetGridy()
    #hd.Draw('ap')

    #c_3 = c.cd(3)
    #c_3.SetGridx()
    #c_3.SetGridy()
    #hx.Draw('ap')

    c.Print('TWIST_rho_all.eps')

if __name__ == "__main__":
    print sys.argv
    if sys.argv[1] == '0':
        plot_results(sys.argv[2:])
    elif sys.argv[1] == '1':
        plot_EcalModeDiffs(sys.argv[2:])
    elif sys.argv[1] == '2':
        plot_BlindAverages(sys.argv[2:])