# Path: mva_tools/build_roc_simple.py
# def build_roc(h_sig, h_bkg, verbose=0):
# 
#     nbins_sig = h_sig.GetXaxis().GetNbins()
#     nbins_bkg = h_bkg.GetXaxis().GetNbins()
#     nbins = nbins_sig if nbins_sig == nbins_bkg else -1
#     #print nbins
#     if nbins < 0.0:
#         sys.exit("Error: nbins_sig != nbins_bkg")
# 
#     min_val = h_sig.GetXaxis().GetXmin()
#     max_val = h_sig.GetXaxis().GetXmax()
# 
#     step = float(max_val - min_val)/(nbins-1)
#     sig_eff = array('f', [])
#     bkg_rej = array('f', [])
# 
#     total_sig = h_sig.Integral()
#     total_bkg = h_bkg.Integral()
#     print total_sig
#     print total_bkg
# 
#     sig_rejected = 0.0
#     bkg_rejected = 0.0
#     for i in xrange_bins(nbins):
#         sig_rejected += h_sig.GetBinContent(i)
#         #print sig_rejected
#         bkg_rejected += h_bkg.GetBinContent(i)
#         #print bkg_rejected
# 
#         seff = float(total_sig-sig_rejected)/total_sig
#         brej = float(bkg_rejected)/total_bkg
#         #print seff, brej
#         sig_eff.append(seff)
#         bkg_rej.append(brej) 
# 
#         if verbose == 1:
#             bdt_score = min_val + i * step
#             print "bdt score =", bdt_score, "sig_eff =", seff 
#         
#         #bin_sig = h_sig.GetBinContent()
#     print "Overflow =", h_sig.GetBinContent(nbins_sig+1)
#     print "Underflow =", h_sig.GetBinContent(0)
# 
#     g = ROOT.TGraph(nbins, sig_eff, bkg_rej)
#     g.GetXaxis().SetRangeUser(0.0,1.0)
#     g.GetYaxis().SetRangeUser(0.0,1.0)
#     #g.Draw("AC")
#     #g.SetLineColor(ROOT.kRed)
#     g.SetTitle("ROC curve")
#     
# 
#     return g
#     
#     #print nbins_bkg
# 
# Path: mva_tools/build_roc_simple.py
# def roc_curve_sk(y_test, sk_y_predicted, step = 0.001):
# 
#     pos_label = 1 # prompt lepton
#     neg_label = 0 # non-prompt lepton
# 
#     y_test = column_or_1d(y_test)
# 
#     assert y_test.size == sk_y_predicted.size, "Error: len(y_test) != len(sk_y_predicted)"
#     assert np.unique(y_test).size == 2, "Error: number of classes is %i. Expected 2 classes only." % np.unique(y_test).size
# 
#     # calculate number of prompt and non-prompt leptons
#     num_prompts = np.sum(y_test == pos_label)
#     num_nonprompts = y_test.size - num_prompts
# 
#     # array of probability cut values 
#     # minimum probability is 0, maximum is one, 
#     # and step is defind above
#     cuts = np.arange(0, 1, step)
# 
#     tpr = []
#     fpr = []
# 
#     for cut in cuts:
#         prompts_passed = 0
#         nonprompts_passed = 0
# 
#         indxs = np.where(sk_y_predicted >= cut)
#         y_test_passed = np.take(y_test, indxs)
#         prompts_passed = np.sum(y_test_passed == pos_label)
#         nonprompts_passed = np.sum(y_test_passed == neg_label)
#         
#         tpr.append(float(prompts_passed)/num_prompts)
#         fpr.append(float(nonprompts_passed)/num_nonprompts)
# 
#     return np.array(fpr), np.array(tpr), 0


import os
import sys
import ROOT
import array
import numpy as np
import cPickle
from ROOT import TH1F
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import classification_report, roc_auc_score, roc_curve
from sklearn import tree
from mva_tools.build_roc_simple import build_roc 
from mva_tools.build_roc_simple import roc_curve_sk
    #    m_el_etcone20Dpt[0], m_el_ptcone20Dpt[0]]).item(0)

    _X_test.append([m_el_pt[0], m_el_eta[0], m_el_sigd0PV[0], m_el_z0SinTheta[0], m_el_etcone20Dpt[0], m_el_ptcone20Dpt[0]])
    _y_test.append([m_el_isprompt])
    

    # calculate the value of the classifier with TMVA/TskMVA
    bdtOutput = reader.EvaluateMVA("BDT")


    if m_el_isprompt == 0:
        histo_tmva_bkg.Fill(bdtOutput)
    elif m_el_isprompt == 1:
        histo_tmva_sig.Fill(bdtOutput)
    else:
        print "Warning: m_mu_isprompt is not 0 or 1!!!"

file.Close()

X_test = np.array(_X_test)
y_test = np.array(_y_test)

# sklearn tpr and tpr
sk_y_predicted = bdt.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, sk_y_predicted)

sig_eff = array.array('f', [rate for rate in tpr])
bkg_rej = array.array('f',[ (1-rate) for rate in fpr])

# roc_curve_sk() - skTMVA version of roc_curve

# Path: mva_tools/build_roc_simple.py
# def build_roc(h_sig, h_bkg, verbose=0):
# 
#     nbins_sig = h_sig.GetXaxis().GetNbins()
#     nbins_bkg = h_bkg.GetXaxis().GetNbins()
#     nbins = nbins_sig if nbins_sig == nbins_bkg else -1
#     #print nbins
#     if nbins < 0.0:
#         sys.exit("Error: nbins_sig != nbins_bkg")
# 
#     min_val = h_sig.GetXaxis().GetXmin()
#     max_val = h_sig.GetXaxis().GetXmax()
# 
#     step = float(max_val - min_val)/(nbins-1)
#     sig_eff = array('f', [])
#     bkg_rej = array('f', [])
# 
#     total_sig = h_sig.Integral()
#     total_bkg = h_bkg.Integral()
#     print total_sig
#     print total_bkg
# 
#     sig_rejected = 0.0
#     bkg_rejected = 0.0
#     for i in xrange_bins(nbins):
#         sig_rejected += h_sig.GetBinContent(i)
#         #print sig_rejected
#         bkg_rejected += h_bkg.GetBinContent(i)
#         #print bkg_rejected
# 
#         seff = float(total_sig-sig_rejected)/total_sig
#         brej = float(bkg_rejected)/total_bkg
#         #print seff, brej
#         sig_eff.append(seff)
#         bkg_rej.append(brej) 
# 
#         if verbose == 1:
#             bdt_score = min_val + i * step
#             print "bdt score =", bdt_score, "sig_eff =", seff 
#         
#         #bin_sig = h_sig.GetBinContent()
#     print "Overflow =", h_sig.GetBinContent(nbins_sig+1)
#     print "Underflow =", h_sig.GetBinContent(0)
# 
#     g = ROOT.TGraph(nbins, sig_eff, bkg_rej)
#     g.GetXaxis().SetRangeUser(0.0,1.0)
#     g.GetYaxis().SetRangeUser(0.0,1.0)
#     #g.Draw("AC")
#     #g.SetLineColor(ROOT.kRed)
#     g.SetTitle("ROC curve")
#     
# 
#     return g
#     
#     #print nbins_bkg
# 
# Path: mva_tools/build_roc_simple.py
# def roc_curve_sk(y_test, sk_y_predicted, step = 0.001):
# 
#     pos_label = 1 # prompt lepton
#     neg_label = 0 # non-prompt lepton
# 
#     y_test = column_or_1d(y_test)
# 
#     assert y_test.size == sk_y_predicted.size, "Error: len(y_test) != len(sk_y_predicted)"
#     assert np.unique(y_test).size == 2, "Error: number of classes is %i. Expected 2 classes only." % np.unique(y_test).size
# 
#     # calculate number of prompt and non-prompt leptons
#     num_prompts = np.sum(y_test == pos_label)
#     num_nonprompts = y_test.size - num_prompts
# 
#     # array of probability cut values 
#     # minimum probability is 0, maximum is one, 
#     # and step is defind above
#     cuts = np.arange(0, 1, step)
# 
#     tpr = []
#     fpr = []
# 
#     for cut in cuts:
#         prompts_passed = 0
#         nonprompts_passed = 0
# 
#         indxs = np.where(sk_y_predicted >= cut)
#         y_test_passed = np.take(y_test, indxs)
#         prompts_passed = np.sum(y_test_passed == pos_label)
#         nonprompts_passed = np.sum(y_test_passed == neg_label)
#         
#         tpr.append(float(prompts_passed)/num_prompts)
#         fpr.append(float(nonprompts_passed)/num_nonprompts)
# 
#     return np.array(fpr), np.array(tpr), 0


# Path: test/run1/apply_bdt_sklearn_tmva_electrons.py
import os
import sys
import ROOT
import array
import numpy as np
import cPickle
from ROOT import TH1F
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import classification_report, roc_auc_score, roc_curve
from sklearn import tree
from mva_tools.build_roc_simple import build_roc 
from mva_tools.build_roc_simple import roc_curve_sk

    #    m_el_etcone20Dpt[0], m_el_ptcone20Dpt[0]]).item(0)

    _X_test.append([m_el_pt[0], m_el_eta[0], m_el_sigd0PV[0], m_el_z0SinTheta[0], m_el_etcone20Dpt[0], m_el_ptcone20Dpt[0]])
    _y_test.append([m_el_isprompt])
    

    # calculate the value of the classifier with TMVA/TskMVA
    bdtOutput = reader.EvaluateMVA("BDT")


    if m_el_isprompt == 0:
        histo_tmva_bkg.Fill(bdtOutput)
    elif m_el_isprompt == 1:
        histo_tmva_sig.Fill(bdtOutput)
    else:
        print "Warning: m_mu_isprompt is not 0 or 1!!!"

file.Close()

X_test = np.array(_X_test)
y_test = np.array(_y_test)

# sklearn tpr and tpr
sk_y_predicted = bdt.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, sk_y_predicted)

sig_eff = array.array('f', [rate for rate in tpr])
bkg_rej = array.array('f',[ (1-rate) for rate in fpr])

# roc_curve_sk() - skTMVA version of roc_curve

fpr_comp, tpr_comp, _ = roc_curve_sk(y_test, sk_y_predicted)