from ROOT import *
# this module is a pythonification of
#/home/rpm1/Root/plotfiducial.C

#below are from my thesis (the intermediate Alex/Dick fiducial that is probably final)
acmin   = 0.54
acmax   = 0.96
ptotmax = 52.0
pzmin   = 14.0
ptmin   = 10.0
ptmax   = 38.0

def fiducial_lines():

    linecolor = 4 #4=blue
    linewidth = 5

    pzminpos = TFormula("pzminpos","[0]/x")
    pzminpos.SetParameter(0,pzmin)

    pzminneg = TFormula("pzminneg","-[0]/x")
    pzminneg.SetParameter(0,pzmin);

    ## (there's no pzmax limit)

    ## Define ptmin curves.
    ptminpos = TFormula("ptminpos","sqrt(1-([0]/x)^2)")
    ptminpos.SetParameter(0,ptmin)

    ptminneg = TFormula("ptminneg","-sqrt(1-([0]/x)^2)")
    ptminneg.SetParameter(0,ptmin)

    ## Define ptmax curves.
    ptmaxpos = TFormula("ptmaxpos","sqrt(1-([0]/x)^2)")
    ptmaxpos.SetParameter(0,ptmax)

    ptmaxneg =  TFormula("ptmaxneg","-sqrt(1-([0]/x)^2)")
    ptmaxneg.SetParameter(0,ptmax)

    ## Now figure out drawing range?
    ## (There's gotta be a better way to figure this out... -RPM)
    ## pzmin go from ptot=0.1...53.0


    ##  Double_t ptminstart, ptminend;
    dptot = 10.0
    while dptot<ptotmax:
        if ( ptminpos.Eval(dptot) > pzminpos.Eval(dptot) ):
            ptminstart = dptot
            break
        dptot+=0.1

    dptot = ptotmax
    while dptot>ptminstart:
        if (ptminpos.Eval(dptot) < acmax):
            ptminend=dptot
            break
        dptot-=0.1

#    dptot = 10.0
#    while dptot<ptotmax:
#        dptot = ptotmax
#        while dptot>ptminstart:
#            if ptminpos.Eval(dptot) < acmax:
#                ptminend = dptot
#                break
#            dptot-=0.1
#        dptot +- 0.1


    ptminposcurve = TF1("ptminposcurve", "ptminpos", ptminstart, ptminend)
    ptminnegcurve = TF1("ptminnegcurve", "ptminneg", ptminstart, ptminend)

    #  Double_t ptmaxstart;
    dptot = ptmax+0.001
    while dptot<53.0:
        if ( ptmaxpos.Eval(dptot) > acmin ):
            ptmaxstart = dptot
            break
        dptot+=0.1

    ptmaxposcurve = TF1("ptmaxposcurve","ptmaxpos",ptmaxstart,ptotmax)
    ptmaxnegcurve = TF1("ptmaxnegcurve","ptmaxneg",ptmaxstart,ptotmax)

    #Double_t pzminstart;
    #  Double_t pzminend;
    #  // The pzmin line meets the ptmin line.
    pzminstart = ptminstart
    dptot = 0.1
    while dptot<53.0:
        if pzminpos.Eval(dptot)<acmin:
            pzminend =  dptot
            break
        dptot += 0.1

    pzminposcurve = TF1("pzminposcurve","pzminpos",pzminstart,pzminend)
    pzminnegcurve = TF1("pzminnegcurve","pzminneg",pzminstart,pzminend)


    ##__________

    costhmaxposline = TLine(ptminend,acmax,ptotmax,acmax)
    costhminposline = TLine(pzminend,acmin,ptmaxstart,acmin)
    ptotmaxposline  = TLine(ptotmax,acmax,ptotmax,ptmaxpos.Eval(ptotmax))
    costhmaxnegline = TLine(ptminend,-acmax,ptotmax,-acmax)
    costhminnegline = TLine(pzminend,-acmin,ptmaxstart,-acmin)
    ptotmaxnegline  = TLine(ptotmax,-acmax,ptotmax,-ptmaxpos.Eval(ptotmax))

    pzminposcurve.SetLineWidth(linewidth)
    pzminnegcurve.SetLineWidth(linewidth)
    ptminposcurve.SetLineWidth(linewidth)
    ptminnegcurve.SetLineWidth(linewidth)
    ptmaxposcurve.SetLineWidth(linewidth)
    ptmaxnegcurve.SetLineWidth(linewidth)

    costhmaxposline.SetLineWidth(linewidth)
    costhminposline.SetLineWidth(linewidth)
    ptotmaxposline.SetLineWidth(linewidth)
    costhmaxnegline.SetLineWidth(linewidth)
    costhminnegline.SetLineWidth(linewidth)
    ptotmaxnegline.SetLineWidth(linewidth)

    pzminposcurve.SetLineColor(linecolor)
    pzminnegcurve.SetLineColor(linecolor)
    ptminposcurve.SetLineColor(linecolor)
    ptminnegcurve.SetLineColor(linecolor)
    ptmaxposcurve.SetLineColor(linecolor)
    ptmaxnegcurve.SetLineColor(linecolor)

    costhmaxposline.SetLineColor(linecolor)
    costhminposline.SetLineColor(linecolor)
    ptotmaxposline.SetLineColor(linecolor)
    costhmaxnegline.SetLineColor(linecolor)
    costhminnegline.SetLineColor(linecolor)
    ptotmaxnegline.SetLineColor(linecolor)

    return [pzminposcurve,pzminnegcurve,
            ptminposcurve,ptminnegcurve,ptmaxposcurve,ptmaxnegcurve,
            costhmaxposline, costhminposline,ptotmaxposline,
            costhmaxnegline,costhminnegline,ptotmaxnegline]