#!/usr/bin/env python

#------------------------------------------------------------------------------

import sys
if not sys.argv.count('-i') and not sys.argv.count('-b'):
    sys.argv.append('-b')
import ROOT
from array import array
#import rootlogon

#----------------------------------------------
# create random numerogram
#----------------------------------------------
rand = ROOT.TRandom()

numer = ROOT.TH1F('numer', 'numer', 40, 0., 200.)
numer.Sumw2()
for j in xrange(200):
    numer.Fill(rand.Gaus(80, 30))
    
denom = ROOT.TH1F('denom', 'denom', 40, 0., 200.)
denom.Sumw2()
for j in xrange(1400):
    denom.Fill(rand.Gaus(80, 170))

#----------------------------------------------
# draw
#----------------------------------------------
can = ROOT.TCanvas('can', '', 700, 500)

numer.SetMarkerStyle(8)
numer.Draw('P')
can.SaveAs('numer.png')

denom.SetMarkerStyle(8)
denom.Draw('P')
can.SaveAs('denom.png')

#----------------------------------------------
# rebin and draw
#----------------------------------------------
bins = array('d', [0.0, 40.0, 80.0, 90.0, 150.0, 200.0])

numer = numer.Rebin(len(bins)-1, '%s_rebin' % numer.GetName(), bins)
numer.Draw('P')
can.SaveAs('numer_rebin.png')

denom = denom.Rebin(len(bins)-1, '%s_rebin' % denom.GetName(), bins)
denom.Draw('P')
can.SaveAs('denom_rebin.png')

#----------------------------------------------
# divide
#----------------------------------------------
ratio = ROOT.TH1F('ratio', 'ratio', numer.GetXaxis().GetNbins(), numer.GetXaxis().GetXmin(), numer.GetXaxis().GetXmax() )
ratio.SetMarkerStyle(8)
ratio.Divide(numer, denom, 1.0, 1.0)
ratio.Draw('P')
can.SaveAs('ratio.png')

