def correct_e_idiso_2012(pt, abseta):
    ''' Get DATA-MC correction factor electron ID and Iso

    Twiki: HiggsToTauTauWorkingHCP2012#Electron_ID_Isolation_EMu_Channe

    '''
    return ROOT.eleIDscale_MuEG_2012_53X(pt, abseta)
def correct_mueg_e_2012(pt, abseta):
    ''' Get DATA-MC correction factor electron leg of MuEG trigger '''
    return ROOT.eleTrigScale_MuEG_2012_53X(pt, abseta)
def correct_e_TIGHTidiso_2011(pt, abseta):
    return ROOT.eleTIGHTIDscale_2011(pt, abseta)
def correct_mueg_mu_2012(pt, abseta):
    ''' Get DATA-MC correction factor muon leg of MuEG trigger '''
    return ROOT.muTrigScale_MuEG_2012_53X(pt, abseta)
def correct_double_electron_trg_2011(ept1, eabseta1, ept2, eabseta2):
    return ROOT.Trg_DoubleEle_2011(ept1, eabseta1, ept2, eabseta2)
def correct_double_muon_trg_2012(mupt1, muabseta1, mupt2, muabseta2):
    return ROOT.Trg_DoubleMu_2012(mupt1, muabseta1, mupt2, muabseta2)
#    canvas = ROOT.TCanvas("asdf", "asdf", 800, 600)
#    projectionsy = bins_projectionsY(clone)
#    projectionsy[0].Draw()
#    for i in projectionsy[1:]:
#        i.Draw('same')
#    canvas.SaveAs('projectionsY.png')
#
#    projectionsx = bins_projectionsX(clone)
#    projectionsx[0].Draw()
#    for i in projectionsx[1:]:
#        i.Draw('same')
#    canvas.SaveAs('projectionsX.png')
#
#    raise Exception
#
    myeff = ROOT.TEfficiencyBugFixed(pass_histo, all_histo)
    myeff.SetStatisticOption(0)  # 0 means  ClopperPearson
    ROOT.SetOwnership(myeff, False)

    efficiency = parse_formula(args.efficiency, args.parameters)
    efficiency.SetName('efficiency')
    efficiency.SetTitle('efficiency')

    ROOT.SetOwnership(efficiency, False)
    myeff.Fit(efficiency, "LMI")  # fitta con l'esponenziale ma crasha

    if args.plot:
        canvas = ROOT.TCanvas("asdf", "asdf", 800, 600)
        canvas.SetLogz(True)
        myeff.Draw("LEGO")
        efficiency.Draw("SURFSAME")
def correct_mu_idiso_2012(pt, abseta):
    ''' Get DATA-MC correction for 53X data from inclusive E-Mu

    Twiki: HiggsToTauTauWorkingHCP2012#Muon_ID_Isolation_EMu_Channel
    '''
    return ROOT.muIDscale_MuEG_2012_53X(pt, abseta)
def smooth_graph(tgraph, width):
    if isinstance(tgraph, ROOT.TGraphAsymmErrors):
        return ROOT.smoothWithErrors(tgraph, width)
    else:
        return ROOT.smooth(tgraph, width)
def smooth_graph_bandutils(tgraph, order):
    if isinstance(tgraph, ROOT.TGraphAsymmErrors):
        return ROOT.smoothBandUtilsWithErrors(tgraph, order)
    else:
        return ROOT.smoothBandUtils(tgraph, order)
            iden = denominator.GetBinContent(ibinx, ibiny)
            ieff = inum / iden if iden <> 0 else 0
            statUp = ROOT.TEfficiency.ClopperPearson(int(iden), int(inum),
                                                     0.682689492137, True)
            statDown = ROOT.TEfficiency.ClopperPearson(int(iden), int(inum),
                                                       0.682689492137, False)
            worse_err = (abs(statUp - ieff) /
                         ieff if abs(statUp - ieff) > abs(statDown - ieff) else
                         abs(statDown - ieff) / ieff) if ieff else 0.
            eff_map.SetBinContent(ibinx, ibiny, ieff)
            eff_map.SetBinError(ibinx, ibiny, worse_err * ieff)
            eff_map_statUp.SetBinContent(ibinx, ibiny, statUp)
            eff_map_statDown.SetBinContent(ibinx, ibiny, statDown)
            worse_rel_err.SetBinContent(ibinx, ibiny, worse_err)

    canvas = ROOT.TCanvas("asdf", "asdf", 800, 600)
    ROOT.gStyle.SetPalette(53)
    eff_map.Draw('colz')
    canvas.Print(args.output.replace(".root", ".png"))
    outFile = ROOT.TFile(args.output, 'recreate')  #FIXME move to rootpy io
    outFile.cd()
    eff_map.Write()
    eff_map_statUp.Write()
    eff_map_statDown.Write()
    worse_rel_err.Write()
    base_dir = os.path.dirname(args.num)

    m = ROOT.RooRealVar('m', 'm', 55, 55, 200)
    os_trkMass = ROOT.RooDataHist(
        'higgs_data', 'higgs_data', ROOT.RooArgList(m),
        input_view.Get(os.path.join(base_dir, 'os_trkMass')))
    log.info("Getting histograms")
    pass_histo = input_view.Get(args.num)
    all_histo = input_view.Get(args.denom)
    if not all_histo.Integral():
        log.info("no entries in denominator!")
    else:
        log.info("pass/all = %0.0f/%0.0f = %0.2f%%", pass_histo.Integral(),
                 all_histo.Integral(),
                 pass_histo.Integral() / all_histo.Integral())
    # Fill the data.
    print type(pass_histo)
    ## for binp, bina in zipBins(pass_histo, all_histo):
    ##     bratio = binp.content/bina.content if bina.content <> 0 else 0
    ##     print "bin #%s: passed %s     all %s  ratio: %s" % (binp._binNum, binp.content, bina.content, bratio)
    graph = ROOT.TGraphAsymmErrors(pass_histo, all_histo)
    ## graph.SetMarkerColor(2)
    ## graph.GetYaxis().SetRangeUser(0,1)
    ## graph.SetMarkerStyle(20)
    ## graph.SetMarkerSize(1)
    ## canvas = ROOT.TCanvas("asdf", "asdf", 800, 600)
    ## graph.Draw('ap')
    ## canvas.Print( plot_name = args.output.replace('.root', 'TGraph.png') )
    ## canvas.Delete()
    log.info("Building x-y RooDataSet")
    x = ROOT.RooRealVar('x', 'x', 0)
    x.setMin(graph.GetX()[0] - graph.GetEXlow()[0])
    x.setMax(graph.GetX()[graph.GetN() - 1] +
             graph.GetEXhigh()[graph.GetN() - 1])
    y = ROOT.RooRealVar('y', 'y', 0)
    xy_data = ROOT.RooDataSet("xy_data", "xy_data", ROOT.RooArgSet(x, y),
from FinalStateAnalysis.Utilities.rootbindings import ROOT
import math
import logging
import sys

ROOT.gStyle.SetOptStat(0)
ROOT.gStyle.SetOptTitle(0)
ROOT.gROOT.SetBatch()

file_dataA = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012A.root')
file_dataB = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012B.root')
file_dataC = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012C.root')
file_dataD = ROOT.TFile('results/newNtuple_5Nov/LFVHETauAnalyzerMVA/ZetauEmbedded_Run2012D.root')

dataA=file_dataA.Get('os/gg/ept30/h_collmass_pfmet')
dataB=file_dataB.Get('os/gg/ept30/h_collmass_pfmet')
dataC=file_dataC.Get('os/gg/ept30/h_collmass_pfmet')
dataD=file_dataD.Get('os/gg/ept30/h_collmass_pfmet')

data=dataC.Clone()
data.Add(dataB)
data.Add(dataA)
data.Add(dataD)

c= ROOT.TCanvas("c","c", 800, 1000)
c.Draw()
c.SetGridx(1)
c.SetGridy(1)


njets=[0,1,2,3,4]
def smooth_graph_bandutils(tgraph, order):
    if isinstance(tgraph, ROOT.TGraphAsymmErrors):
        return ROOT.smoothBandUtilsWithErrors(tgraph, order)
    else:
        return ROOT.smoothBandUtils(tgraph, order)
def smooth_graph(tgraph, width):
    if isinstance(tgraph, ROOT.TGraphAsymmErrors):
        return ROOT.smoothWithErrors(tgraph, width)
    else:
        return ROOT.smooth(tgraph, width)