import numpy as np
import math
import ROOT
import sys
import argparse

ROOT.gROOT.LoadMacro("/afs/cern.ch/user/z/zgao/eos/AtlasStyle/AtlasStyle.C")
from ROOT import SetAtlasStyle
SetAtlasStyle()



ROOT.gStyle.SetOptStat(0)


parser = argparse.ArgumentParser(description='')
parser.add_argument('-conpdf', '--conpdf', dest="conpdf_file", required=True, help='con pdf file')
#  parser.add_argument('-uncpdf', '--uncpdf', dest="uncpdf_file", required=True, help='unc pdf file')
parser.add_argument('-conroot', '--conroot', dest="conroot_file", required=True, help='con root file')
parser.add_argument('-onlydata', '--onlydata', dest="onlydata_file", required=True, help='onlydata_file file')
#  parser.add_argument('-uncroot', '--uncroot', dest="uncroot_file", required=True, help='unc root file')
args = parser.parse_args()


variables_name_string=["VBF_pT_j1","VBF_Dy_j_j","VBF_DRmin_y_j","VBF_m_jj","VBF_Zepp","VBF_Dphi_Zy_jj_FullRange","VBF_pTt_Zy","llg_angles_costheta_ginH","llg_angles_costheta_ginH_Zmassconstraint","llg_angles_costheta_linZ","llg_angles_costheta_linZ_Zmassconstraint","llg_angles_phi_linZ","llg_angles_phi_linZ_Zmassconstraint","llg_deta_Zy","llg_deta_Zy_Zmassconstraint","llg_dm_ll","llg_dm_ll_Zmassconstraint","llg_dphi_Zy","llg_dphi_Zy_Zmassconstraint","llg_eta","llg_eta_Zmassconstraint","llg_m","llg_m_Zmassconstraint","llg_pTt","llg_pTt_Zmassconstraint","llg_phi","llg_phi_Zmassconstraint","llg_pt","llg_pt_Zmassconstraint","VBF_BDTG","ph_pt"]
category=[" total "," VBF-enriched "," High relative pT "," High PTt ee "," Low  PTt ee "," High PTt uu "," Low  PTt uu "]

hists_name_ggH = [["hist "+str(i)+variables_name_string[i] + category[j] + " ggH"  for j in range(len(category))] for i in range(len(variables_name_string))]
hists_name_VBFH = [["hist "+str(i)+variables_name_string[i] + category[j] + " VBFH" for j in range(len(category))] for i in range(len(variables_name_string))]
hists_name_VH = [["hist "+str(i)+variables_name_string[i] + category[j] + " VH" for j in range(len(category))] for i in range(len(variables_name_string))]
hists_name_ttH = [["hist "+str(i)+variables_name_string[i] + category[j] + " ttH & bbH" for j in range(len(category))] for i in range(len(variables_name_string))]

hists_name_Zll_jet = [["hist "+str(i)+variables_name_string[i] + category[j] + " Zll jet" for j in range(len(category))] for i in range(len(variables_name_string))]
hists_name_ll_gamma = [["hist "+str(i)+variables_name_string[i] + category[j] + " ll gamma" for j in range(len(category))] for i in range(len(variables_name_string))]

hists_name_data = [["hist "+str(i)+variables_name_string[i] + category[j] + " data" for j in range(len(category))] for i in range(len(variables_name_string))]
new_hists_name_data = [["new hist "+str(i)+variables_name_string[i] + category[j] + " data" for j in range(len(category))] for i in range(len(variables_name_string))]


#  file_only_data_blind = ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/mc/comparison/only_data_blind/Run2_output_hist.root","READ")
#  file_only_data_blind = ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/iso_tight/Run2_data_blind_iso_tight.root","READ")
#  file_only_data_blind = ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/iso_tight/Run2_data_blind_iso_tight.root","READ")


file_iso_tight = ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/iso_tight/Run2_data_blind_iso_tight.root","READ")
file_iso_loose= ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/iso_loose/Run2_data_blind_iso_loose.root","READ")
file_noniso_tight = ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/non_iso_tight/Run2_data_blind_noniso_tight.root","READ")
file_noniso_loose= ROOT.TFile("/afs/cern.ch/user/z/zgao/eos/H_to_Zy/2_lep/pdf_root_file/non_iso_loose/Run2_data_blind_noniso_loose.root","READ")

file_only_data_blind = ROOT.TFile(args.onlydata_file,"READ")

file_data_mc = ROOT.TFile(args.conroot_file,"READ")
key_list = file_data_mc.GetListOfKeys()

canvas=ROOT.TCanvas("canvas","",800,600)
canvas.Print(args.conpdf_file+"[")


def final_errors(hist):
	list1 = []
	for i in range(hist.GetNbinsX()):
		list1.append(hist.GetBinError(i+1))
	return list1

def final_errors_2(hist):
	list1 = []
	for i in range(hist.GetNbinsX()):
		list1.append(hist.GetBinContent(i+1))
	return list1

def get_Z(hs, hb, fsc = 1):
	nbins = hs[0].GetNbinsX()
	Z = 0.
	#  print("Begin : ....")
	for i in range(nbins):
		s = 0
		b = 0
		#  print(" the ith bin is: ",i)
		for j in range(len(hs)):
			s += hs[j].GetBinContent(i+1)*fsc 
		#  print("s: ",s)
		for j in range(len(hb)):
			b += hb[j].GetBinContent(i+1)
		#  print("b: ",b)
		if b<=0:
			continue
		if s<=0:
			continue
		#  Z += 2*((s+b)*math.log(1+s/b)-s)
		Z += 2*((s+b)*ROOT.TMath.Log(1+s/b)-s)
		#  print("Z: ", Z)
		pass
	Z = math.sqrt(Z)
	return Z

def draw_hist(hist_ggH, hist_VBFH, hist_VH, hist_ttH, hist_Zll_jet, hist_ll_gamma, hist_data, h_ratio, h_ratio_background, new_iso_tight = None):

	list_ggH_content = [hist_ggH.GetBinContent(k+1) for k in range(hist_ggH.GetNbinsX())]
	#  for i in range(hist_ggH.GetNbinsX()):
		#  print(list_ggH_content[i])
	list_VBFH_content = [hist_VBFH.GetBinContent(k+1) for k in range(hist_VBFH.GetNbinsX())]
	list_VH_content = [hist_VH.GetBinContent(k+1) for k in range(hist_VH.GetNbinsX())]
	list_ttH_content = [hist_ttH.GetBinContent(k+1) for k in range(hist_ttH.GetNbinsX())]


	list_Zll_jet_content = [hist_Zll_jet.GetBinContent(k+1) for k in range(hist_Zll_jet.GetNbinsX())]
	list_ll_gamma_content = [hist_ll_gamma.GetBinContent(k+1) for k in range(hist_ll_gamma.GetNbinsX())]
	list_data_content = [hist_data.GetBinContent(k+1) for k in range(hist_data.GetNbinsX())]


	list_Zll_jet_err = [hist_Zll_jet.GetBinError(k+1) for k in range(hist_Zll_jet.GetNbinsX())]
	list_ll_gamma_err = [hist_ll_gamma.GetBinError(k+1) for k in range(hist_ll_gamma.GetNbinsX())]
	list_data_err = [hist_data.GetBinError(k+1) for k in range(hist_data.GetNbinsX())]


	signal_number = hist_ggH.Integral() + hist_VBFH.Integral() + hist_VH.Integral() + hist_ttH.Integral()
	background_number = hist_Zll_jet.Integral() + hist_ll_gamma.Integral() 
	total_mc_number = signal_number + background_number
	print("signal_number ",signal_number)
	print("background_number ",background_number)
	print("    ")
	data_number = hist_data.Integral()

	signal_hist = [hist_ggH ,hist_VBFH, hist_VH, hist_ttH]
	background_hist = [hist_Zll_jet, hist_ll_gamma]
	significance_Z = get_Z(signal_hist, background_hist)


	for k in range(hist_ggH.GetNbinsX()):
		
		h_ratio_background.SetBinContent(k+1,1)


		if (list_Zll_jet_content[k] + list_ll_gamma_content[k])==0 or list_data_content[k]==0:
			h_ratio.SetBinContent(k+1,0)
			h_ratio.SetBinError(k+1,0)

			
		else:
			h_ratio.SetBinContent(k+1, list_data_content[k] / (list_Zll_jet_content[k] + list_ll_gamma_content[k]))
			h_ratio.SetBinError(k+1, list_data_content[k]/(list_Zll_jet_content[k] + list_ll_gamma_content[k]) * math.sqrt((list_data_err[k]/list_data_content[k])**2 + ((list_Zll_jet_err[k]**2 + list_ll_gamma_err[k]**2)/ (list_Zll_jet_content[k] + list_ll_gamma_content[k])**2)))

		if (list_Zll_jet_content[k] + list_ll_gamma_content[k])==0:
			h_ratio_background.SetBinError(k+1,0)
		else:
			#  h_ratio_background.SetBinError(k+1, math.sqrt(2) * math.sqrt( list_Zll_jet_err[k] **2 + list_ll_gamma_err[k] **2) / ( list_Zll_jet_content[k] + list_ll_gamma_content[k] ))
			h_ratio_background.SetBinError(k+1,  math.sqrt( list_Zll_jet_err[k] **2 + list_ll_gamma_err[k] **2) / ( list_Zll_jet_content[k] + list_ll_gamma_content[k] ))


	pad_top = ROOT.TPad("pad_top", "", 0,0.32,1,1)
	pad_top.Draw()
	
	pad_bot = ROOT.TPad("pad_bot", "", 0, 0,1, 0.3)
	pad_bot.Draw()
	
	pad_top.cd()
	pad_top.SetTickx(1)
	pad_top.SetTicky(1)
	pad_top.SetLeftMargin(0.12)
	pad_top.SetRightMargin(0.05)
	pad_top.SetTopMargin(0.05)
	pad_top.SetRightMargin(0.03)
	pad_top.SetBottomMargin(0.02)
	pad_top.SetFrameBorderMode(0)
	pad_top.SetFrameBorderMode(0)
	pad_top.SetFillColor(0)
	pad_top.SetBorderMode(0)
	pad_top.SetBorderSize(2)


	stack_signal = ROOT.THStack("stack_signal","stack_signal mode")
	stack_background = ROOT.THStack("stack_background","stack_background mode")


	hist_ggH.Scale(20)
	hist_VBFH.Scale(200)
	hist_VH.Scale(200)
	hist_ttH.Scale(2000)

	if i ==25 or i ==19 or i==0 :
		hist_ggH.Rebin(2)
		hist_VBFH.Rebin(2)
		hist_VH.Rebin(2)
		hist_ttH.Rebin(2)
		hist_Zll_jet.Rebin(2)
		hist_ll_gamma.Rebin(2)
	#  if i==22:
	#	   hist_ggH.Rebin(5)
	#	   hist_VBFH.Rebin(5)
	#	   hist_VH.Rebin(5)
	#	   hist_ttH.Rebin(5)
	#	   hist_Zll_jet.Rebin(5)
	#	   hist_ll_gamma.Rebin(5)


	#  hist_ggH.Scale(200)
	#  hist_VBFH.Scale(2000)
	#  hist_VH.Scale(2000)
	#  hist_ttH.Scale(20000)

	hist_ggH.SetLineColor(ROOT.kRed)
	hist_VBFH.SetLineColor(ROOT.kBlue)
	hist_VH.SetLineColor(ROOT.kGreen)
	hist_ttH.SetLineColor(ROOT.kYellow)
	hist_Zll_jet.SetLineColor(ROOT.kMagenta)
	hist_ll_gamma.SetLineColor(ROOT.kOrange)

	hist_ggH.SetFillStyle(0)
	hist_VBFH.SetFillStyle(0)
	hist_VH.SetFillStyle(0)
	hist_ttH.SetFillStyle(0)
	#  hist_Zll_jet.SetFillStyle(0)
	#  hist_ll_gamma.SetFillStyle(0)

	hist_ggH.SetFillColor(ROOT.kRed)
	hist_VBFH.SetFillColor(ROOT.kBlue)
	hist_VH.SetFillColor(ROOT.kGreen)
	hist_ttH.SetFillColor(ROOT.kYellow)
	hist_Zll_jet.SetFillColor(ROOT.kMagenta)
	hist_ll_gamma.SetFillColor(ROOT.kOrange)

	hist_ggH.SetMarkerSize(0)
	hist_VBFH.SetMarkerSize(0)
	hist_VH.SetMarkerSize(0)
	hist_ttH.SetMarkerSize(0)
	#  hist_Zll_jet.SetMarkerSize(0)
	#  hist_ll_gamma.SetMarkerSize(0)

	#  hist_ggH.SetMarkerColor(ROOT.kRed)
	#  hist_VBFH.SetMarkerColor(ROOT.kBlue)
	#  hist_VH.SetMarkerColor(ROOT.kGreen)
	#  hist_ttH.SetMarkerColor(ROOT.kYellow)
	hist_Zll_jet.SetMarkerColor(ROOT.kMagenta)
	hist_ll_gamma.SetMarkerColor(ROOT.kOrange)


	#  hist_ggH.SetBinContent(50,1000)
	stack_signal.Add(hist_ggH)
	stack_signal.Add(hist_VBFH)
	stack_signal.Add(hist_VH)
	stack_signal.Add(hist_ttH)

	stack_background.Add(hist_Zll_jet)
	stack_background.Add(hist_ll_gamma)

	#  print(hist_ggH.GetName())

	stack_background.Draw()
	#  stack_background.SetMinimum(0)
	#  stack_background.SetMaximum(100)
	#  hist_Zll_jet.GetYaxis().SetRangeUser(0,100)
	#  stack_signal.Draw("hist,same,nostack")
	stack_signal.Draw("h,NOCLEAR,same")
	#  hist_data.Draw("PE same")
	if new_iso_tight is not None:
		new_iso_tight.Draw("P,same")
		new_iso_tight.SetMarkerColor(ROOT.kBlue)

	if i==0:
		stack_background.GetYaxis().SetTitle("Events / 4 GeV")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)
	if i==19:
		stack_background.GetYaxis().SetTitle("Events / 0.32 ")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)
	if i==21 or i==22:
		stack_background.GetYaxis().SetTitle("Events / 1.00 GeV")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)
	if i==25:
		stack_background.GetYaxis().SetTitle("Events / 0.16 ")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)
	if i==27 or i==28:
		stack_background.GetYaxis().SetTitle("Events / 4 GeV")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)
	if i==30:
		stack_background.GetYaxis().SetTitle("Events / 2 GeV")
		stack_background.GetYaxis().SetTitleOffset(0.7)
		stack_background.GetYaxis().SetTitleSize(0.07)

	stack_background.GetXaxis().SetLabelSize(0)
	stack_background.GetXaxis().SetTitleSize(0)


	sig_content1 = hist_ggH.GetBinContent(hist_ggH.GetMaximumBin())
	sig_content2 = hist_VBFH.GetBinContent(hist_VBFH.GetMaximumBin())
	sig_content3 = hist_VH.GetBinContent(hist_VH.GetMaximumBin())
	sig_content4 = hist_ttH.GetBinContent(hist_ttH.GetMaximumBin())
	sig_content = sig_content1 + sig_content2 + sig_content3 + sig_content4

	bkg_content1 = hist_Zll_jet.GetBinContent(hist_Zll_jet.GetMaximumBin())
	bkg_content2 = hist_ll_gamma.GetBinContent(hist_ll_gamma.GetMaximumBin())
	bkg_content = bkg_content1 + bkg_content2


    hist_data.Draw("PE same")
	#  print(hist_data.GetName())
	hist_data.GetYaxis().SetRangeUser(0,bkg_content * 1.2)
	if i ==25 or i ==19 or i==0 :
		hist_data.Rebin(2)
	#  if i==22:
		#  hist_data.Rebin(5)

	data_content = hist_data.GetBinContent(hist_data.GetMaximumBin())

	if sig_content >= bkg_content and sig_content >= data_content:
		stack_background.SetMaximum( sig_content *1.6)
		if i ==25 : 
			stack_background.SetMaximum( sig_content *1.9)
		if i ==0 or i==27 or i==30: 
			stack_background.SetMaximum( sig_content *1.8)
	if data_content >= bkg_content and data_content >= sig_content:
		stack_background.SetMaximum( data_content *1.6)
		if i==25:
			stack_background.SetMaximum( data_content *1.9)
		if i==0 or i==27 or i==30:
			stack_background.SetMaximum( data_content *1.8)
	if bkg_content >= data_content and bkg_content >= sig_content:
		stack_background.SetMaximum(bkg_content *1.6)
		if i==25:
			stack_background.SetMaximum(bkg_content *1.9)
		if i==0 or i==27 or i==30:
			stack_background.SetMaximum( data_content *1.8)
	pad_top.Modified()

	

	legend = ROOT.TLegend(0.6,0.6,0.9,0.9)

	legend.AddEntry(hist_ggH," ggH x20 ","F")
	legend.AddEntry(hist_VBFH," VBFH x200 ","F")
	legend.AddEntry(hist_VH," VH x200","F")
	legend.AddEntry(hist_ttH," ttH & bbH x2000","F")

	#  legend.AddEntry(hist_ggH," ggH x200 ","l")
	#  legend.AddEntry(hist_VBFH," VBFH x2000 ","l")
	#  legend.AddEntry(hist_VH," VH x2000","l")
	#  legend.AddEntry(hist_ttH," ttH & bbH x20000","l")

	legend.AddEntry(hist_ll_gamma," ll gamma ", "F")
	legend.AddEntry(hist_Zll_jet," Zll jet ","F")
	legend.AddEntry(hist_data," data ","ep")
	legend.SetFillStyle(0)
	legend.SetBorderSize(0)
	legend.SetTextSize(0.025);
	legend.Draw("same")
	#  text1 = ROOT.TPaveText(0.05,0.5,0.5,0.7, "NDC")
	#  text1.AddText(category[j])
	#  text1.AddText(f"Z = {significance_Z:.3f}")
	#  #  text1.AddText()
	#  text1.SetFillStyle(0)
	#  text1.SetBorderSize(0)
	#  text1.SetTextSize(0.07)
	#  text1.Draw("same")

	latex = ROOT.TLatex()
	latex.SetTextSize(0.07)
	latex.SetTextFont(42)
	latex.DrawLatexNDC(0.15,0.85, "#bf{ #it{ATLAS} Internal}")
	latex.DrawLatexNDC(0.15,0.75, "#sqrt{s} = 13.6 TeV")
	latex.DrawLatexNDC(0.15,0.65, f"{category[j]}")
	latex.DrawLatexNDC(0.15,0.55, f"Z = {significance_Z:.3f}")

	

	pad_bot.cd()
	pad_bot.SetTickx(1)
	pad_bot.SetTicky(1)
	pad_bot.SetLeftMargin(0.12)
	pad_bot.SetRightMargin(0.05)
	pad_bot.SetTopMargin(0.025)
	pad_bot.SetRightMargin(0.03)
	pad_bot.SetBottomMargin(0.25)
	pad_bot.SetFrameBorderMode(0)
	pad_bot.SetFrameBorderMode(0)
	pad_bot.SetFillColor(0)
	pad_bot.SetBorderMode(0)
	pad_bot.SetBorderSize(0)

	h_ratio_background.Draw("E4")
	#  h_ratio_background.SetFillColorAlpha(ROOT.kGreen,0.5)
	h_ratio_background.SetMarkerSize(0)
	#  h_ratio_background.SetFillColor(ROOT.kGreen)
	h_ratio_background.SetFillColor(ROOT.kOrange + 1 )

	if i==21 or  i==0 or  i==27 or	i==30 or i==22 or i ==28:
		h_ratio_background.GetXaxis().SetTitle(variables_name_string[i] + " [GeV] ")
	else:
		h_ratio_background.GetXaxis().SetTitle(variables_name_string[i])

	#  h_ratio_background.GetYaxis().SetTitle("Scale Factor")
	#  h_ratio_background.GetYaxis().CenterTitle()
	h_ratio_background.GetXaxis().SetTitleSize(0.15)
	h_ratio_background.GetXaxis().SetTitleOffset(0.7)
	h_ratio_background.GetXaxis().SetLabelSize(0.12)
	#  h_ratio_background.GetXaxis().SetMoreLogLabels()
	#  h_ratio_background.GetXaxis().SetNoExponent()
	#  h_ratio_background.GetYaxis().SetTitleSize(0.12)
	#  h_ratio_background.GetYaxis().SetTitleOffset(0.4)
	h_ratio_background.GetYaxis().SetLabelSize(0.12)
	h_ratio_background.GetYaxis().SetRangeUser(0,2)

	h_ratio.Draw("same") 
	h_ratio.GetYaxis().SetNdivisions(202)
	h_ratio.GetYaxis().SetRangeUser(0,2)
	#  #  h_con_ratio.GetXaxis().SetTitle("pt[Gev]")
#


	#  print(type(hist_VH))
	line=ROOT.TLine(hist_VH.GetXaxis().GetXmin(),1,hist_VH.GetXaxis().GetXmax(),1)
	line.SetLineWidth(1)
	line.SetLineStyle(2)
	line.SetLineColor(ROOT.kRed)
	line.Draw()
#  for i in range(len(variables_name_string)):
#  for i in range(25,26):
#  for i in range(0,1):
#  for i in range(21,22):
#  for i in range(22,23):
for i in range(21,23):
#  for i in range(19,20):
#  for i in range(27,28):
#  for i in range(30,31):
#
	for j in range(len(category)):

		print(hists_name_ggH[i][j])

		#  hist_ggH = file_only_data_blind.Get(hists_name_ggH[i][j])
		#  hist_VBFH = file_only_data_blind.Get(hists_name_VBFH[i][j])
		#  hist_VH = file_only_data_blind.Get(hists_name_VH[i][j])
		#  hist_ttH = file_only_data_blind.Get(hists_name_ttH[i][j])

		hist_iso_tight = file_iso_tight.Get(hists_name_data[i][j])
		hist_iso_tight_ggH = file_iso_tight.Get(hists_name_ggH[i][j])
		hist_iso_tight_VBFH = file_iso_tight.Get(hists_name_VBFH[i][j])
		hist_iso_tight_VH = file_iso_tight.Get(hists_name_VH[i][j])
		hist_iso_tight_ttH = file_iso_tight.Get(hists_name_ttH[i][j])
		hist_iso_tight_Zll_jet = file_iso_tight.Get(hists_name_Zll_jet[i][j])
		hist_iso_tight_ll_gamma = file_iso_tight.Get(hists_name_ll_gamma[i][j])

		hist_iso_loose = file_iso_loose.Get(hists_name_data[i][j])
		hist_iso_loose_ggH = file_iso_loose.Get(hists_name_ggH[i][j])
		hist_iso_loose_VBFH = file_iso_loose.Get(hists_name_VBFH[i][j])
		hist_iso_loose_VH = file_iso_loose.Get(hists_name_VH[i][j])
		hist_iso_loose_ttH = file_iso_loose.Get(hists_name_ttH[i][j])
		hist_iso_loose_Zll_jet = file_iso_loose.Get(hists_name_Zll_jet[i][j])
		hist_iso_loose_ll_gamma = file_iso_loose.Get(hists_name_ll_gamma[i][j])

		hist_noniso_tight = file_noniso_tight.Get(hists_name_data[i][j])
		hist_noniso_tight_ggH = file_noniso_tight.Get(hists_name_ggH[i][j])
		hist_noniso_tight_VBFH = file_noniso_tight.Get(hists_name_VBFH[i][j])
		hist_noniso_tight_VH = file_noniso_tight.Get(hists_name_VH[i][j])
		hist_noniso_tight_ttH = file_noniso_tight.Get(hists_name_ttH[i][j])
		hist_noniso_tight_Zll_jet = file_noniso_tight.Get(hists_name_Zll_jet[i][j])
		hist_noniso_tight_ll_gamma = file_noniso_tight.Get(hists_name_ll_gamma[i][j])

		hist_noniso_loose = file_noniso_loose.Get(hists_name_data[i][j])
		hist_noniso_loose_ggH = file_noniso_loose.Get(hists_name_ggH[i][j])
		hist_noniso_loose_VBFH = file_noniso_loose.Get(hists_name_VBFH[i][j])
		hist_noniso_loose_VH = file_noniso_loose.Get(hists_name_VH[i][j])
		hist_noniso_loose_ttH = file_noniso_loose.Get(hists_name_ttH[i][j])
		hist_noniso_loose_Zll_jet = file_noniso_loose.Get(hists_name_Zll_jet[i][j])
		hist_noniso_loose_ll_gamma = file_noniso_loose.Get(hists_name_ll_gamma[i][j])


		# try to get the fake factor

		new_iso_tight = hist_iso_tight.Clone("new_iso_tight")
		new_iso_tight.SetTitle(new_hists_name_data[i][j])

		new_hist_fake_factor = hist_iso_tight.Clone("fake_factor_hist")


		for k in range(hist_iso_tight.GetNbinsX()):
			 content_iso_tight = hist_iso_tight.GetBinContent(k+1) - hist_iso_tight_ggH.GetBinContent(k+1) -hist_iso_tight_VBFH.GetBinContent(k+1) - hist_iso_tight_VH.GetBinContent(k+1) - hist_iso_tight_ttH.GetBinContent(k+1)
			 content_iso_loose = hist_iso_loose.GetBinContent(k+1) - hist_iso_loose_ggH.GetBinContent(k+1) -hist_iso_loose_VBFH.GetBinContent(k+1) - hist_iso_loose_VH.GetBinContent(k+1) - hist_iso_loose_ttH.GetBinContent(k+1)
			 content_noniso_tight = hist_noniso_tight.GetBinContent(k+1) - hist_noniso_tight_ggH.GetBinContent(k+1) -hist_noniso_tight_VBFH.GetBinContent(k+1) - hist_noniso_tight_VH.GetBinContent(k+1) - hist_noniso_tight_ttH.GetBinContent(k+1)
			 content_noniso_loose = hist_noniso_loose.GetBinContent(k+1) - hist_noniso_loose_ggH.GetBinContent(k+1) -hist_noniso_loose_VBFH.GetBinContent(k+1) - hist_noniso_loose_VH.GetBinContent(k+1) - hist_noniso_loose_ttH.GetBinContent(k+1)

			 if content_noniso_loose >0:
				 fake_factor = content_iso_loose / content_noniso_loose
			 elif (content_noniso_loose==0 and content_iso_loose==0):
				 fake_factor = 1
			 else:
				 fake_factor = 0


			 new_hist_fake_factor.SetBinContent(k+1, fake_factor)

			 new_iso_tight_content = fake_factor * content_noniso_tight 
			 new_iso_tight.SetBinContent(k+1,new_iso_tight_content)
			#  print("content: ",new_iso_tight_content)



		#-- data / background mc ------
		h_ratio_iso_tight = hist_iso_tight_ggH.Clone("h_ratio_iso_tight")
		h_ratio_iso_loose = hist_iso_loose_ggH.Clone("h_ratio_iso_loose")
		h_ratio_noniso_tight = hist_noniso_tight_ggH.Clone("h_ratio_noniso_tight")
		h_ratio_noniso_loose = hist_noniso_loose_ggH.Clone("h_ratio_noniso_loose")
		h_ratio_background_iso_tight = hist_iso_tight_ggH.Clone("h_ratio_background_iso_tight")
		h_ratio_background_iso_loose = hist_iso_loose_ggH.Clone("h_ratio_background_iso_loose")
		h_ratio_background_noniso_tight = hist_noniso_tight_ggH.Clone("h_ratio_background_noniso_tight")
		h_ratio_background_noniso_loose = hist_noniso_loose_ggH.Clone("h_ratio_background_noniso_loose")


		canvas.Clear()
		canvas.Divide(2,2)

		canvas.cd(1)
		draw_hist(hist_iso_loose_ggH, hist_iso_loose_VBFH, hist_iso_loose_VH, hist_iso_loose_ttH, hist_iso_loose_Zll_jet, hist_iso_loose_ll_gamma, hist_iso_loose, h_ratio_iso_loose,  h_ratio_background_iso_loose, None)

		canvas.cd(2)
		draw_hist(hist_iso_tight_ggH, hist_iso_tight_VBFH, hist_iso_tight_VH, hist_iso_tight_ttH, hist_iso_tight_Zll_jet, hist_iso_tight_ll_gamma, hist_iso_tight, h_ratio_iso_tight,  h_ratio_background_iso_tight, new_iso_tight)

		canvas.cd(3)
		draw_hist(hist_noniso_loose_ggH, hist_noniso_loose_VBFH, hist_noniso_loose_VH, hist_noniso_loose_ttH, hist_noniso_loose_Zll_jet, hist_noniso_loose_ll_gamma, hist_noniso_loose, h_ratio_noniso_loose, h_ratio_background_noniso_loose,None)

		canvas.cd(4)
		draw_hist(hist_noniso_tight_ggH, hist_noniso_tight_VBFH, hist_noniso_tight_VH, hist_noniso_tight_ttH, hist_noniso_tight_Zll_jet, hist_noniso_tight_ll_gamma, hist_noniso_tight, h_ratio_noniso_tight,  h_ratio_background_noniso_tight,None)

		canvas.Update()
		canvas.Print(args.conpdf_file)
		print(" ** ", i*7 +j +1)


canvas.Print(args.conpdf_file+"]")



