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

#ROOT.RooAbsReal.defaultIntegratorConfig().setEpsAbs(1e-300)
#ROOT.RooAbsReal.defaultIntegratorConfig().setEpsRel(1e-300)
#ROOT.RooAbsReal.defaultIntegratorConfig().method2D().setLabel("RooMCIntegrator")
#ROOT.RooAbsReal.defaultIntegratorConfig().getConfigSection("RooMCIntegrator").setRealValue("nIntPerDim",5000)
ROOT.gSystem.Load("TestPdf_cxx.so")

# Create spectrum and fill it with constant values
spectrum = ROOT.TH1F("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[1.5]")

# Create pdf and 2D histogram
pdf = ROOT.TestPdf("pdf", "pdf", x, y, sigma_x, sigma_y, threshold, spectrum)
hist = pdf.createHistogram("hist", x, RF.Binning(1000), RF.YVar(y, RF.Binning(1000)))

# Compare integral of histogram with normalization integral
print "hist integral = ", hist.Integral("WIDTH")
print "pdf integral = ", pdf.expectedEvents( ROOT.RooArgSet(x, y) )

hist.Draw("COLZ")
