import numpy as np

import root_numpy as rtnp

import ROOT as rt

from rootpy.plotting import Hist, Hist2D, Canvas, Pad  # for histograms

from rootpy.io import root_open

from hsg3_style import *

"""
Explanation: Ratio Plots with root_numpy & rootpy
This is a check to see if I can run my stuff with root_numpy and rootpy :P

Start by importing my modules... 
Numpy, root_numpy, ROOT, rootpy.plotting
I don't know man.. just put stuff there..
Oh also put style :D
End of explanation
"""


infile = root_open('vbf125spin2pmin.root','read')

"""
Explanation: Open my file with root_open (rootpy.io)
End of explanation
"""


intree = infile.MVATree

intree

"""
Explanation: Let's make it simple for newbs... now get the tree...
End of explanation
"""


data = rtnp.tree2array(intree) # SO SIMPLE!

"""
Explanation: Now that's why I love root_numpy...
Dump the whole tree into a structured numpy array!!!
End of explanation
"""


data['Mll']

"""
Explanation: Now I can get one branch by simply doing....
End of explanation
"""


branches = rtnp.list_branches("vbf125spin2pmin.root")

branches

"""
Explanation: <b>REMEMBER:</b>
If you don't know the name of the tree or branches.. or even structures you can use
list_trees(), list_structures(), list_branches()
for example....
End of explanation
"""


hist_mll = Hist(10, 0,200, name="mll", title="mll;m(ll);Entries/20GeV")

hist_mll

"""
Explanation: Good eh? (''kalo e?'')

Anyways, now we can have the 'Mll' branch out of the tree...
Lets make a histogram to store the mll variable
End of explanation
"""


from rootpy.plotting.style import get_style, set_style

style = get_hgs3_style()#set_style(#'ATLAS')

#style.SetLabelOffset(0.4,'y')

set_style(style)

"""
Explanation: Let's use the default 'ATLAS' style of roottpy (I dont like it hmm, but to bored to make it myself)
End of explanation
"""


rtnp.fill_hist(hist_mll, data['Mll']/1000., weights=data['EventWeight'])

hist_mll

"""
Explanation: Anyways let's fill the histogram..
<b> Remember </b> Mll is in [MeV] so simply divide each index by 1000. to get GeV's
<b> WEIGHTS: </b> 
This is Monte Carlo sample. So the events are weighted! The weights are stored in the 'EventWeight' branch.. So simply use it as weights, like this:
End of explanation
"""


canvas = Canvas(800,600,name='c1')

canvas.cd()

"""
Explanation: Make a canvas...
class rootpy.plotting.Canvas(width=None, height=None, x=None, y=None, name=None, title=None, size_includes_decorations=False)
End of explanation
"""


ratio = hist_mll.Clone()

ratio.Divide(hist_mll)

pad_hist = Pad(0,0.3,1,1); set_pad_style(pad_hist); pad_hist.Draw()

pad_ratio = Pad(0,0,1,0.3); set_subpad_style(pad_ratio); pad_ratio.Draw()

pad_hist.cd()

hist_mll.Draw()

pad_ratio.cd()

ratio.draw('E4')

canvas

canvas

"""
Explanation: Let's make a fake ratio plot
End of explanation
"""


