from ROOT import *

gROOT.SetBatch(kTRUE)

pdg = TDatabasePDG.Instance()
gROOT.ProcessLine(".x mystyle.C")
#from ShipStyle import *
#lhcbstyleSetup()
#gROOT.SetBatch(kTRUE)


what = "nu"


if what == "anti":
    f = TFile("/afs/cern.ch/work/a/austyuzh/public/4barbara/newGen_15340000_anti-nuBg.root")
    t = f.Get("t")
else:
    #f = TFile("/afs/cern.ch/work/a/austyuzh/public/4barbara/newGen_15340000_nuBg.root")#newGen/ShipAna_Barbara_nu_th45MeV_LSSegments45MeV_FINALTEST.root")
    f = TFile("../../DATA/nuFromYandex/newGen_15340000_nuBg.root")
    t = f.Get("t")



#b = TBrowser()

flux_nu = 1.05e+11 * 2.e+20/ 5.e+13#1.61e+11 * 2.e+20/ 4.e+13
flux_anti = 7.38e+10 * 2.e+20 / 5.e+13#1.02e+11 * 2.e+20 / 4.e+13
entries_nu = 1.534e+7#200000-2
entries_anti =175*10e+3
if what=="anti":
    
    weight_nu = "(weight * %s / %s)"%(flux_anti,entries_anti)

else:
    weight_nu = "(weight * %s / %s)"%(flux_nu,entries_nu)
    
'''flux_nu = 1.61e+11 * 2.e+20/ 4.e+13
flux_anti = 1.02e+11 * 2.e+20 / 4.e+13
entries_nu = 1.345e+7#200000-2
entries_anti = 200000-2
weight_nu = "(weight * %s / %s)"%(flux_nu,entries_nu)
'''
n = t.Draw("event", "(nuE>10)*%s"%weight_nu)
print "nuE>10: entries=%.0f weighted=%.0f"%(n, t.GetHistogram().GetSum())
n = t.Draw("event", "(nuE>4 && nuE<10)*%s"%weight_nu)
print "4<nuE<10: entries=%.0f weighted=%.0f"%(n, t.GetHistogram().GetSum())
n = t.Draw("event>>h3", "(nuE<4)*%s"%weight_nu)
print "nuE<4: entries=%.0f weighted=%.0f"%(n, t.GetHistogram().GetSum())


assert(False)

def makeText(textes):
    pave = TPaveText(0.63, 0.72, 0.92, 0.84,"TNDC")
    for tx in textes:
        pave.AddText("%s"%tx)
    pave.SetTextAlign(11)
    return pave

def makeLegend(legTuple, pos=(0.66,0.78,0.90,0.90)):
    leg = TLegend(*pos);
    leg.SetLineColor(kWhite)
    for lt in legTuple:
        leg.AddEntry(lt[0],lt[1],"l");
        
    return leg


def makePlot(name,var,cut,logY=False,legend=True):
    c = TCanvas("c_%s"%name,"c_%s"%name)
    c.SetLogy(logY)
    t.Draw("%s"%var, "%s"%cut)
    h = t.GetHistogram()
    if legend:
        pave = makeLegend([[h,cut],],pos=(0.56,0.69,0.90,0.90))
        pave.Draw("same")
    c.Print(c.GetName()+".pdf")
    return c,h,pave

c0,h0,p0 = makePlot("recoed",weight_nu,"",True) 
c1,h1,p1 = makePlot("ndf_25",weight_nu,"NoB_MinDaughtersNdf>25",True) 
c2,h2,p2 = makePlot("acceptance",weight_nu,"NoB_MinDaughtersNdf>25 && Sum$( (NoB_vtxxSqr)/(245.*245.) + (NoB_vtxySqr)/(490.*490.) ) < 1. && Sum$(NoB_vtxz > -1958. && NoB_vtxz < 2568.)",True) 
c3,h3,p3 = makePlot("IP0<10m",weight_nu,"NoB_IP0<1000",True) 
c8, h8, p8 = makePlot("vtxSec",weight_nu,"TMath::Abs(Max$(NoB_vtxz)-Min$(NoB_vtxz))<30",True)

c4,h4,p4 = makePlot("ndf_weight","%s:NoB_MinDaughtersNdf"%weight_nu,"",False,False) 
c5,h5,p5 = makePlot("vtxz_weight","%s:vtxz"%weight_nu,"vtxz>-100",False,False) 
c6,h6,p6 = makePlot("IP_weight","%s:NoB_IP0"%weight_nu,"",False,False) 
c7,h7,p7 = makePlot("nrecoed_weight","%s:nRecoed"%weight_nu,"nRecoed<30",False,False) 
c9, h9, p9 = makePlot("vtxSec_weight","%s:TMath::Abs(Max$(NoB_vtxz)-Min$(NoB_vtxz))"%weight_nu,"TMath::Abs(Max$(NoB_vtxz)-Min$(NoB_vtxz))",False,False)

c10, h10, p10 = makePlot("notVetoed",weight_nu,"(nRecoed>0 && numberOfHitLSSegments == 0  && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1)",True)

c11, h11, p11 = makePlot("notVetoed_IP10",weight_nu,"(nRecoed>0 && numberOfHitLSSegments == 0  && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1 && NoB_IP0<1000)",True)

c12, h12, p12 = makePlot("notVetoed_acceptance_IP",weight_nu,"(nRecoed>0 && numberOfHitLSSegments == 0  && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1 && NoB_IP0<1000) && NoB_MinDaughtersNdf>25 && Sum$( (NoB_vtxxSqr)/(245.*245.) + (NoB_vtxySqr)/(490.*490.) ) < 1. && Sum$(NoB_vtxz > -1958. && NoB_vtxz < 2568.)",True)

c13, h13, p13 = makePlot("notVetoed_acceptance_IP",weight_nu,"(nRecoed>0 && numberOfHitLSSegments == 0  && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1 ) && NoB_MinDaughtersNdf>25 && Sum$( (NoB_vtxxSqr)/(245.*245.) + (NoB_vtxySqr)/(490.*490.) ) < 1. && Sum$(NoB_vtxz > -1958. && NoB_vtxz < 2568.)",True)

c13, h13, p13 = makePlot("notVetoed_acceptance",weight_nu,"(nRecoed>0 && numberOfHitLSSegments == 0  && strawVetoAny_eff==0 && upstreamVetoAny_eff==0 && RPCany_eff==0 && TrackSyst==1 && NoB_IP0<1000) && NoB_MinDaughtersNdf>25 && Sum$( (NoB_vtxxSqr)/(245.*245.) + (NoB_vtxySqr)/(490.*490.) ) < 1. && Sum$(NoB_vtxz > -1958. && NoB_vtxz < 2568.)",True)

assert(False)
