from ROOT import *
gROOT.ProcessLine(".x mystyle.C")

'''f = TFile("ntupleStudy_tot_debug_nu_highFlux2.root")
t = f.Get("t")
#fanti = TFile("ntupleStudy_tot_debug_antinu_highFlux2.root")
#tanti = f.Get("t")
fanti = TFile("ntupleStudy_tot_debug_nu_highFlux2.root")
tanti = fanti.Get("t")
'''

#f = TFile("/afs/cern.ch/work/b/bstora/data/fromAnalysis/ntupleStudy_highFlux_nu_77_77.root ")
#t = f.Get("t")
fanti = TFile("/afs/cern.ch/work/b/bstora/data/fromAnalysis/ntupleStudy_highFlux_antinu_76_78.root")
tanti = fanti.Get("t")

f = TFile("/afs/cern.ch/work/b/bstora/ShipAna_Barbara_nu.root")
t = f.Get("t")

plotFolder = "/afs/cern.ch/work/b/bstora/plots/nuBackground/"

dictNodeNames = {'volIron':0, 'cave':1, 'LiSc':2, 'Startplate':3, 'TI':4, 'rockD':5, 'Endplate':6, 'Rib':7, 'volFeYoke':8, 'volHPT':9, 'TO':10, 'volRpc':11, 'volCoil':12, 'T1Lid':13, 'straw':14, 'strawVeto':15, 'volBase':16, 'Tr':17, 'gas':18, 'wire':19, 'others':20}

dictNodeNames2 = {0:'volIron', 1:'cave', 2:'LiSc', 3:'Startplate', 4:'TI', 5:'rockD', 6:'Endplate', 7:'Rib', 8:'volFeYoke', 9:'volHPT', 10:'TO', 11:'volRpc', 12:'volCoil', 13:'T1Lid', 14:'straw', 15:'strawVeto', 16:'volBase', 17:'Tr', 18:'gas', 19:'wire', 20:'others'}

def makePlotLabel(t, var, cut, cname, label=True):
    t.Draw(var, cut)
    h = t.GetHistogram()
    nbins = h.GetNbinsX()
    h_id = TH1F("h_id>>h_%s"%cname, "h_id", nbins, 0, nbins)
    if label:
        h_id.LabelsOption("U")
    for i in xrange(1,nbins+1):
        h_id.SetBinContent(i,h.GetBinContent(i))
        if label:
            h_id.GetXaxis().SetBinLabel(i, dictNodeNames2[int(h.GetBinCenter(i))])

    n = h_id.GetSum()
    c = None
    if not cname is None:
        c = TCanvas(cname,cname)
        h_id.Draw()
        c.Print(plotFolder+c.GetName()+".eps")
        c.Print(plotFolder+c.GetName()+".pdf")
    return c, h_id, n

c, h, nW = makePlotLabel(t,"event", "(isPrimary==1)*weight",None,False) 
c, h, nantiW = makePlotLabel(tanti,"event", "(isPrimary==1)*weight",None,False) 


c, h, nreco_nu = makePlotLabel(t,"nuIntNumSimpl", "nRecoed>0","AllRecoEd_nu") 
c, h, nreco_noLS_nu = makePlotLabel(t,"nuIntNumSimpl", "nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0)","RecoEd_noLS_nu") 

c, h, nreco_antinu =makePlotLabel(tanti,"nuIntNumSimpl", "nRecoed>0","AllRecoEd_antinu") 
c, h, nreco_noLS_antinu =makePlotLabel(tanti,"nuIntNumSimpl", "nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0)","RecoEd_noLS_antinu") 

c, h, nrecoW_nu = makePlotLabel(t,"nuIntNumSimpl", "(nRecoed>0)*weight","AllRecoEd_nu") 
c, h, nrecoW_noLS_nu = makePlotLabel(t,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight","RecoEd_noLS_nu") 

c, h, nrecoW_antinu =makePlotLabel(tanti,"nuIntNumSimpl", "(nRecoed>0)*weight","AllRecoEd_antinu") 
c, h, nrecoW_noLS_antinu =makePlotLabel(tanti,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight","RecoEd_noLS_antinu") 

colorNu = "blue"
colorAnti = "green"
print
print "*****************"
print 

t_tot = t.GetEntries()
tanti_tot = tanti.GetEntries()

print "Few numbers reweighed (entries): "
print " 	\\begin{itemize}"
print "     	\\item Total $\\nu$ entries: %0.f (%s)"%(nW,t_tot) 
print "      		\\begin{itemize} \\item \\textcolor{%s}{Reconstructed: %.0f (%s)} \\end{itemize}"%(colorNu, nrecoW_nu, nreco_nu)
print "         \\item Total $\\bar{\\nu}$ entries:  %.0f (%s)"%(nantiW,tanti_tot)
print "      		\\begin{itemize} \\item \\textcolor{%s}{Reconstructed: %.0f (%s)} \end{itemize}"%(colorAnti, nrecoW_antinu, nreco_antinu)
print "      \\end{itemize}"

print
print "*****************"
print 

print "Events not vetoed by the LS, no requirements on the other systems (entries in brackets): "
print " 	\\begin{itemize}"
print "     	\\item Total $\\nu$ entries: %0.f (%s)"%(nW,t.GetEntries()) 
print "      		\\begin{itemize} \\item \\textcolor{%s}{Reconstructed: %.0f (%s)} \\end{itemize}"%(colorNu,nrecoW_noLS_nu,nreco_noLS_nu)
print "         \\item Total $\\bar{\\nu}$ entries:  %0.f (%s)"%(nantiW,tanti.GetEntries())
print "      		\\begin{itemize} \\item \\textcolor{%s}{Reconstructed: %.0f (%s)} \end{itemize}"%(colorAnti, nrecoW_noLS_antinu, nreco_noLS_antinu)
print "      \\end{itemize}"

print
print "*****************"
print 

c, h, nreco_nu_allVeto = makePlotLabel(t,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)","reco_nu_allVeto") 
c, h, nreco_antinu_allVeto =makePlotLabel(tanti,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)","reco_antinu_allVeto") 
c, h, nrecoW_nu_allVeto = makePlotLabel(t,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight","recoW_nu_allVeto") 
c, h, nrecoW_antinu_allVeto =makePlotLabel(tanti,"nuIntNumSimpl", "(nRecoed>0 && (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight","recoW_antinu_allVeto") 


c, h, nnu_allVeto90 = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1)","nu_allVeto90") 
c, h, nantinu_allVeto90 =makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1)","antinu_allVeto90") 
c, h, nW_nu_allVeto90 = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1)*weight","W_nu_allVeto90") 
c, h, nW_antinu_allVeto90 =makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1)*weight","W_antinu_allVeto90") 

c, h, nnu_allVeto = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)","nu_allVeto") 
c, h, nantinu_allVeto =makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)","antinu_allVeto") 
c, h, nW_nu_allVeto = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight","W_nu_allVeto") 
c, h, nW_antinu_allVeto =makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight","W_antinu_allVeto") 

print
print "*****************"
print 

print "Reweighted events with number of entries in the ntuple in brackets"
print "\\begin{itemize}" 
print "\\item Number of interacting \\textcolor{%s}{$\\nu_\\mu$ %.0f (%s)} and \\textcolor{%s}{$\\bar{\\nu_\\mu}$ %.0f (%s)}"%(colorNu, nW, t.GetEntries(), colorAnti, nantiW, tanti.GetEntries()) 
print "\\item Applying all the veto systems (E-threshold for the LS at 15MeV): \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"%(colorNu, nW_nu_allVeto,nnu_allVeto, colorAnti, nW_antinu_allVeto,nantinu_allVeto)
print "		\\begin{itemize} \\item In case each system is with $0.9$ efficiency: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)} \\end{itemize}"%(colorNu, nW_nu_allVeto90,nnu_allVeto90, colorAnti, nW_antinu_allVeto90,nantinu_allVeto90)
print "	\item Applying reconstruction:\\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)} "%(colorNu, nrecoW_nu_allVeto,nreco_nu_allVeto, colorAnti, nrecoW_antinu_allVeto,nreco_antinu_allVeto)
#	\item Applying offline selection: ()
print "\\end{itemize}"

print
print "*****************"
print 

print
print "*****************"
print 

c, h, nnu_noOPERA = makePlotLabel(t,"event", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && TrackSyst==1)",None,False) 
c, h, nWnu_noOPERA = makePlotLabel(t,"event", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && TrackSyst==1)*weight",None,False) 

c, h, nnu_noUpstream = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWnu_noUpstream = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 

c, h, nnu_noStraw = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWnu_noStraw = makePlotLabel(t,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 

c, h, nantinu_noOPERA = makePlotLabel(tanti,"event", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && TrackSyst==1)",None,False) 
c, h, nWantinu_noOPERA = makePlotLabel(tanti,"event", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && upstreamVetoAny==0 && TrackSyst==1)*weight",None,False) 

c, h, nantinu_noUpstream = makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWantinu_noUpstream = makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && strawVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 

c, h, nantinu_noStraw = makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWantinu_noStraw = makePlotLabel(tanti,"nuIntNumSimpl", "( (scintVetoAny==0 || scintVetoAny_Ethr==0) && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 

c, h, nantinu_noLS = makePlotLabel(tanti,"nuIntNumSimpl", "(strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWantinu_noLS = makePlotLabel(tanti,"nuIntNumSimpl", "( strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 
c, h, nnu_noLS = makePlotLabel(t,"nuIntNumSimpl", "(strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)",None,False) 
c, h, nWnu_noLS = makePlotLabel(t,"nuIntNumSimpl", "( strawVetoAny==0 && upstreamVetoAny==0 && RPCany==0 && TrackSyst==1)*weight",None,False) 


c, h, nWantinu_tracking = makePlotLabel(tanti,"event", "(TrackSyst==1)*weight",None,False) 
c, h, nantinu_tracking = makePlotLabel(tanti,"event", " (TrackSyst==1)",None,False) 
c, h, nWnu_tracking = makePlotLabel(t,"event", "(TrackSyst==1)*weight",None,False) 
c, h, nnu_tracking = makePlotLabel(t,"event", "(TrackSyst==1)",None,False) 


print
print "*****************"
print 

print "\\begin{frame}[c]{Effect of each singular system}"
print "	\\begin{itemize}" 
print "\\item Requiring at least 1 hit in the tracking station: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"%(colorNu,nWnu_tracking, nnu_tracking,colorAnti, nWantinu_tracking, nantinu_tracking)
print "	\\item No OPERA-system: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"%(colorNu,nWnu_noOPERA,nnu_noOPERA,colorAnti,nWantinu_noOPERA,nantinu_noOPERA )
print "	\\item No upstream-veto: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"%(colorNu,nWnu_noUpstream,nnu_noUpstream,colorAnti,nWantinu_noUpstream,nantinu_noUpstream)
print "	\\item No straw-veto: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"	%(colorNu,nWnu_noStraw,nnu_noStraw,colorAnti,nWantinu_noStraw,nantinu_noStraw )
print "	\\item No LS: \\textcolor{%s}{ %.0f (%s)} \\textcolor{%s}{ %.0f (%s)}"	%(colorNu,nWnu_noLS,nnu_noLS,colorAnti,nWantinu_noLS,nantinu_noLS)

print " \\end{itemize}"
print
print "*****************"
print 

#
#### everything vetoed by the upstream is at very very high y
#### as long as the opera system is used as veto-system we can just build an upstream detector between 400<|y|<600
#c, h, n = makePlotLabel("upstreamVetoPoint_y", "(isPrimary==1 && strawVetoAny==0 && RPCany==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight",,False)
#                 t.Draw("upstreamVetoPoint_y", "(isPrimary==1  && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight")
#                 t.Draw("upstreamVetoPoint_y", "(isPrimary==1 && strawVetoAny==0  && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight")
# ### upstream veto serve principalmente, come atteso, a togliere il background provenienti dal ferro (yokes, ... ) di OPERA
#
## Somehow it does not work: 
#c, h, n = makePlotLabel(t,"upstreamVetoPoint_y", "(isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight", "upstreamVeto_noOthers_nu",False)
#c, h, n = makePlotLabel(tanti,"upstreamVetoPoint_y", "(isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight", "upstreamVeto_noOthers_antinu",False)
# DO:
# >>> t.Draw("upstreamVetoPoint_y", "(isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight")
# >>> tanti.Draw("upstreamVetoPoint_y", "(isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight")

c, h, n_reco_Upstream_noOthers_nu = makePlotLabel(t,"event", "(recoed>0 && isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))", None,False)
c, h, n_reco_Upstream_noOthers_antinu = makePlotLabel(tanti,"event", "(recoed>0 &&isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))", None,False)
if n_reco_Upstream_noOthers_nu>0:
    c, h, nW_reco_Upstream_noOthers_nu = makePlotLabel(t,"event", "(recoed>0 &&isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight", None,False)
if n_reco_Upstream_noOthers_antinu>0:
    c, h, nW_reco_Upstream_noOthers_antinu = makePlotLabel(tanti,"event", "(recoed>0 &&isPrimary==1  && upstreamVetoAny==1 && strawVetoAny_eff==0 && RPCany_eff==0 && (scintVetoAny==0 || scintVetoAny_Ethr==0))*weight", None,False)
print "Number of events veto-able by the upstream-veto that are reconstructed, but not vetoed by the strawVeto or the OPERA-system "
print "nu: %s "%(n_reco_Upstream_noOthers_nu)
print "anti-nu: %s"%(n_reco_Upstream_noOthers_antinu)

