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


In [1]:
import numpy as np

In [2]:
import root_numpy as rtnp

In [3]:
import ROOT as rt

In [4]:
from rootpy.plotting import Hist, Hist2D, Canvas, Pad  # for histograms

In [5]:
from rootpy.io import root_open

In [6]:
from hsg3_style import *

Open my file with root_open (rootpy.io)


In [7]:
infile = root_open('vbf125spin2pmin.root','read')

Let's make it simple for newbs... now get the tree...



In [8]:
intree = infile.MVATree

In [9]:
intree


Out[9]:
Tree('MVATree')

Now that's why I love root_numpy...

Dump the whole tree into a structured numpy array!!!



In [10]:
data = rtnp.tree2array(intree) # SO SIMPLE!

Now I can get one branch by simply doing....



In [11]:
data['Mll']


Out[11]:
array([  83006.0546875 ,   27705.55273438,   85706.3203125 ,
         51483.35546875,   34970.0703125 ,   18112.34375   ,
         36089.0078125 ,   69110.421875  ,   48663.67578125,
         36606.26171875,   82781.1171875 ,   82891.5390625 ,
         26933.07617188,   23514.26953125,   79589.1015625 ,
         73283.375     ,   57630.70703125,   56764.75      ,
         54881.94140625,   54761.48828125,   52599.47265625,
         56533.2265625 ,   46447.58203125,   51928.734375  ,
         38718.99609375,   29393.765625  ,   37856.1875    ,
         54143.703125  ,   61532.98046875,   31187.078125  ,
         46644.29296875,   69584.0078125 ,   24573.61914062,
         47993.171875  ,   73869.3515625 ,   33335.34765625,
         35476.8984375 ,   54044.08984375,   74347.8671875 ,
         48382.52734375,   35519.76953125,   58710.98828125,
         62842.71484375,   83496.0390625 ,   71203.859375  ,
         39566.19921875,   46217.01171875,   36541.71484375,
         32513.41601562,   58370.6953125 ,   41898.06640625,
         44025.9296875 ,  267736.3125    ,  284212.625     ,
         54507.52734375,   68162.9296875 ,   86082.84375   ,
         50101.48828125,   38836.75      ,   69285.1640625 ,
         26026.15234375,   50912.5546875 ,   19573.23828125,
         52268.10546875,   70843.1328125 ,   49819.70703125,
         47593.23828125,   40136.9921875 ,   78575.421875  ,
         55693.0078125 ,   58409.33984375,   57920.69140625,
         58200.22265625,   78646.6953125 ,   62420.6796875 ,
         79703.0703125 ,   34346.7109375 ,   45676.10546875,
         39041.67578125,   97795.6328125 ,   27773.35351562,
         69051.21875   ,   32619.91796875,   64427.76171875,
         47987.46484375,   38890.0234375 ,   26361.79882812,
         38482.1484375 ,  170485.609375  ,   40036.1953125 ,
         32932.46875   ,   45798.33203125,   44600.3203125 ,
         77168.0078125 ,   72681.796875  ,   39871.1328125 ,
         66895.9375    ,   65658.9140625 ,   61453.15625   ,
         49871.125     ,   63809.453125  ,   64109.1015625 ,
         54744.73828125,   24648.34570312,   72765.7109375 ,
         67248.765625  ,   55613.31640625,   61180.59375   ,
         53503.921875  ,   61650.59765625,   52874.46484375,
         60734.12890625,   49931.46484375,   70185.140625  ,
         56793.203125  ,   62968.125     ,   35079.09375   ,
         63776.234375  ,   56063.21484375,   19446.27148438,
         46073.734375  ,   48675.43359375,   23474.40429688,
         64825.7578125 ,   46571.9765625 ,   25820.72460938,
         56842.875     ,   57161.484375  ,   69037.8046875 ,
         30745.35546875,   47879.1328125 ,   75483.375     ,
         62909.3046875 ,   61927.10546875,   57615.94921875,
         80029.296875  ,   44735.53515625,   19521.07617188,
         23778.87890625,   34564.390625  ,   31704.70703125,
         46700.27734375,   86689.7421875 ,   63866.72265625,
         43806.8203125 ,   50820.02734375,   46196.8671875 ,
         53426.8203125 ,   64103.015625  ,   69754.78125   ,
         24118.60546875,   59707.00390625,   52014.80078125,
         70657.921875  ,   75093.2109375 ,   61718.55859375,
         58484.41796875,   64738.4765625 ,   25566.56054688,
        317379.15625   ,   53084.46484375,   26114.54296875,
         78068.59375   ,   70912.5859375 ,   39051.58203125,
         61779.62890625,  152340.6875    ,   78082.1484375 ,
         28503.61523438,   25308.28710938,   78455.671875  ,
         30398.2421875 ,   51965.703125  ,   47435.765625  ,
         71828.4140625 ,   38540.51953125,   51021.4375    ,
         64463.3984375 ,   45901.3046875 ,   42251.23828125,
         31539.16210938,   47105.97265625,   74439.1875    ,
         60042.03515625,   78088.28125   ,   68447.90625   ,
         50606.33984375,   38487.98046875,   39680.0546875 ,
         55440.4375    ,   43296.3984375 ,   51278.90234375,
         59936.59765625,   71456.7265625 ,   53679.78125   ,
         52118.5078125 ,   63518.25390625,   68335.546875  ,
         28338.38671875,   37635.69140625,   54119.01171875,
         97668.6328125 ,   50889.3359375 ,   95380.265625  ,
         69695.90625   ,   27722.60351562,   74868.0234375 ,
         35015.48828125,   33726.8203125 ,   43760.0390625 ,
         71673.7421875 ,   28039.06835938,   50552.5078125 ,
         26866.50390625,  109191.53125   ,   27862.53125   ,
         65211.3203125 ,   77692.40625   ,   51757.953125  ,
         67086.125     ,   73476.625     ,   74381.2421875 ,
         29221.31640625,   14183.19921875,   54045.03125   ,
         45054.01171875,   64350.046875  ,   60691.09765625,
         46926.7265625 ,   63347.55078125,   41135.05859375,
         50166.44140625,   26098.15429688,   40375.76953125,
         69225.9296875 ,   46286.87890625,   27838.28710938,
         12274.359375  ,   62850.734375  ,   34187.390625  ,
         60855.87109375,   44827.24609375,   33282.0703125 ,
         39130.37109375,  492576.78125   ,   20695.55078125,
         57949.0390625 ,   72851.8828125 ,  127828.1328125 ,
         48753.94140625,   43340.78125   ,   37422.796875  ,
         77094.4375    ,   51032.40234375,   72476.7890625 ,
         34078.16796875,   58676.50390625,   28133.43164062,
         29444.44335938,   50171.4375    ,   64874.37890625,
         31753.94335938,   52438.72265625,   35246.44921875,
         68853.9375    ,   43192.46875   ,   76736.984375  ,
         41921.8125    ,   37767.84375   ,   43773.88671875,
         59520.37109375,  292755.5       ,   50915.23828125,
         42883.4453125 ,   49052.609375  ,   48207.79296875,
         65415.0859375 ,   44108.3203125 ,   83896.015625  ,
         61931.91015625,   58029.83203125,   29884.08984375,
         57522.8046875 ,   75440.1640625 ,   61072.70703125,
        686533.625     ,  107494.4375    ,   52321.2734375 ,
         74657.296875  ,   33818.578125  ,   40808.83203125,   73277.171875  ], dtype=float32)

REMEMBER: 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....


In [12]:
branches = rtnp.list_branches("vbf125spin2pmin.root")

In [13]:
branches


Out[13]:
['EventWeight',
 'Mll',
 'MT',
 'MT_TrackHWW_Clj',
 'Mtt',
 'Mtt_TrackHWW_Clj',
 'Ptll',
 'DPhill',
 'METRel',
 'MET',
 'MET_phi',
 'METRel_TrackHWW_Clj',
 'MET_TrackHWW_Clj',
 'MET_phi_TrackHWW_Clj',
 'MW0_TrackHWW_Clj',
 'MW1_TrackHWW_Clj',
 'lepPt0',
 'lepPt1',
 'lepEta0',
 'lepEta1',
 'lepPhi0',
 'lepPhi1',
 'jetPt0',
 'jetEta0',
 'jetPhi0',
 'MinDPhi_TrackHWW_Clj',
 'DEtall',
 'm_el_n',
 'm_mu_n',
 'm_jet_n',
 'nbt',
 'RunNumber',
 'EventNumber',
 'mc_channel_number',
 'mva_weight',
 'spin2_mva_weight',
 'mcevt_pdf_id1',
 'mcevt_pdf_id2',
 'mcevt_pdf_x1',
 'mcevt_pdf_x2',
 'mcevt_pdf_scale',
 'MTtruth',
 'lepID0',
 'lepID1',
 'MVAEventWeight',
 'pileupEventWeight',
 'pileupEventWeight_090',
 'pileupEventWeight_080',
 'lepTrigSFEventWeight',
 'lepTrigSFEventWeightUp',
 'lepTrigSFEventWeightDown',
 'lepSF0Error',
 'lepSF1Error',
 'lepSF0ErrorIso',
 'lepSF1ErrorIso',
 'lepSF0EventWeight',
 'lepSF1EventWeight',
 'MV120_85_EventWeight',
 'MV120_85_CTJetWeight',
 'MV120_85_BJetWeight',
 'MV120_85_MisTagWeight',
 'MV120_85_CTJetWeightUp',
 'MV120_85_BJetWeightUp',
 'MV120_85_MisTagWeightUp',
 'MV120_85_CTJetWeightDown',
 'MV120_85_BJetWeightDown',
 'MV120_85_MisTagWeightDown',
 'isBlinded',
 'BDPhill',
 'BDPsill',
 'BPLeadLep',
 'BPSubLeadLep',
 'BELeadNeu',
 'BESubLeadNeu',
 'Esum',
 'DPt',
 'HPt',
 'Efun']

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


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

In [15]:
hist_mll


Out[15]:

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


In [16]:
from rootpy.plotting.style import get_style, set_style

In [17]:
style = get_hgs3_style()#set_style(#'ATLAS')

In [18]:
#style.SetLabelOffset(0.4,'y')

In [19]:
set_style(style)


INFO:rootpy.plotting.style:using ROOT style ''

Anyways let's fill the histogram..

Remember Mll is in [MeV] so simply divide each index by 1000. to get GeV's

WEIGHTS:

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:


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

In [21]:
hist_mll


Out[21]:

Make a canvas...

class rootpy.plotting.Canvas(width=None, height=None, x=None, y=None, name=None, title=None, size_includes_decorations=False)


In [22]:
canvas = Canvas(800,600,name='c1')

In [23]:
canvas.cd()


Out[23]:

Let's make a fake ratio plot


In [24]:
ratio = hist_mll.Clone()

In [25]:
ratio.Divide(hist_mll)


Out[25]:
True

In [26]:
pad_hist = Pad(0,0.3,1,1); set_pad_style(pad_hist); pad_hist.Draw()

In [27]:
pad_ratio = Pad(0,0,1,0.3); set_subpad_style(pad_ratio); pad_ratio.Draw()

In [28]:
pad_hist.cd()


Out[28]:
Pad('Pad_z8LrwSJGs3QhrUSK44p66C')

In [29]:
hist_mll.Draw()

In [30]:
pad_ratio.cd()


Out[30]:
Pad('Pad_i4gYQRgwJGWznh2q5qcELU')

In [31]:
ratio.draw('E4')

In [32]:
canvas


Out[32]:

In [ ]:


In [33]:
canvas


Out[33]:

In [ ]:


In [ ]: