import os, sys, math, copy
# sys.path = ["../source"] + sys.path
# sys.path = ["../analysis"] + sys.path
# sys.path = ["../condor"] + sys.path

import ROOT
from ROOT import RooStats as RooStats
from ROOT.RooStats import HistFactory as HistFactory
# from extra import *
import cPickle as pickle
# from plotter import *


inputsys = "sysHist_ee.root"
inputSigFile = "inputSig_pureCI_LL.root"
sigName = "diff_CI_LL_const_12_TeV"


meas = HistFactory.Measurement("meas", "meas")
meas.SetOutputFilePrefix("./results/example_")

meas.SetPOI("mu")
meas.AddConstantParam("Lumi")
meas.SetLumi(1.0)
meas.SetLumiRelErr(0.5)
meas.SetExportOnly(False)

#setting data hist as bkg for ease 
chan = HistFactory.Channel("channel1")
chan.SetData("ee_dibosonNoOutliers_201516fakes_smoothDY_smoothTTbar", inputsys)
#chan.SetStatErrorConfig(0.05, "Poisson")

bkg1 = HistFactory.Sample("bkg", "ee_dibosonNoOutliers_201516fakes_smoothDY_smoothTTbar", inputsys)
chan.AddSample(bkg1)

sig = HistFactory.Sample(sigName, sigName, inputSigFile)
sig.AddNormFactor("mu", 1, 0, 100)
chan.AddSample(sig)

meas.AddChannel(chan)
meas.CollectHistograms()

# w = HistFactory.MakeModelAndMeasurementFast(meas) # Test make model and measurement, it works without any problems 

hist2workspace = ROOT.RooStats.HistFactory.HistoToWorkspaceFactoryFast(meas) # 
# w = hist2workspace.MakeCombinedModel(meas) # There's no issues with MakeCombinedModel as well 

# print meas, chan ## -> they exist and are consistant with what is required as inputs by MakeSingleChannelModel()
# print chan.GetSamples().empty() ## -> works fine 
# print meas.GetConstantParams().size() ## -> works fine
# print chan.GetSamples().size() ## -> works file, there's two histograms 
# print chan.GetSamples().at(0).GetHisto() ## -> looks like it fails here... Some issue with GetHisto() ?

##FAILS HERE
w = hist2workspace.MakeSingleChannelModel(meas,chan)