#!/usr/bin/env python
import ROOT
from ROOT import RooFit as RF

ROOT.gSystem.Load("TestPdf_cxx.so")

# Create spectrum and fill it with constant values
spectrum = ROOT.TH1D("spectrum", "spectrum", 100, 0, 30)
for bin in range(1,spectrum.GetNbinsX()+1):
  spectrum.SetBinContent(bin, 1.0)

# Create workspace and variables
w = ROOT.RooWorkspace("w")
x = w.factory("x[0,15]")
y = w.factory("y[0,15]")
sigma_x = w.factory("sigma_x[0.1]")
sigma_y = w.factory("sigma_y[0.2]")
threshold = w.factory("threshold[2.0]")

# Create pdf and 2D histogram
pdf = ROOT.TestPdf("pdf", "pdf", x, y, sigma_x, sigma_y, threshold, spectrum)

# Create extended and nornamlized histogram
hist = pdf.createHistogram("hist", x, RF.Binning(1000), RF.YVar(y, RF.Binning(1000)), RF.Extended(True))
hist_norm = pdf.createHistogram("hist_norm", x, RF.Binning(1000), RF.YVar(y, RF.Binning(1000)))

# Compare histogram integrals with expectedEvents method
print "hist integral (extended) = ", hist.Integral("WIDTH")
print "hist integral (normalized) = ", hist_norm.Integral("WIDTH")
print "pdf expectedEvents() = ", pdf.expectedEvents( ROOT.RooArgSet(x, y) )

hist.Draw("COLZ")
