from os.path import dirname, realpath, sep, pardir
import sys
sys.path.append(dirname(dirname(realpath(__file__))))
import os
os.chdir('../')
print os.getcwd()
from distribsForHNLandBG_byEvent import *

### NB: anche nei draw ci andrebbe NoB_!!!

tc = cutsWithDraw()
study = 'nu'
t = studies[study]['data']
cuts =     [ tc.recoed()[0], tc.notVetoed(1000.)[0], tc.ip(250.)[0] ]
tc.setNoB('NoB_')
tc.nu = True
print cuts
cuts =     [ tc.recoed()[0], tc.notVetoed(1000.)[0], tc.ip(250.)[0] ]
print cuts
cuts =     [ tc.recoed()[0] ]
t.Draw('IP0>>bgreco', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in cuts])+ ")", "norm")
cuts2 =     [ tc.recoed()[0], tc.notVetoed(1000.)[0] ]
t.Draw('IP0>>bgreconv', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in cuts2])+ ")", "normsame")
bgreconv = r.gDirectory.Get('bgreconv')
bgreconv.SetLineColor(r.kRed)
bgreconv.SetMarkerColor(r.kRed)
geo = studies[study]['geo']
ntot = studies[study]['ntot']
zmin = geo['Veto_5']['z']['pos']+geo['Veto_5']['z']['dim']
zmax = geo['Tr1_1']['z']['pos']-geo['Tr1_1']['z']['dim']
cuts3 = [ tc.recoed()[0], tc.notVetoed(1000.)[0], tc.redChi2(5.)[0], tc.ndf(25)[0], tc.fiducial(zmin, zmax, 250., 500.)[0], tc.goodtracks()[0], tc.ecal()[0], tc.muon1(1)[0], tc.muon2(1)[0], tc.doca(30.)[0] ]
t.Draw('IP0>>bgreconvcuts', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in cuts3])+ ")", "normsame")
bgreconvcuts = r.gDirectory.Get('bgreconvcuts')
bgreconvcuts.SetMarkerColor(r.kGreen)
bgreconvcuts.SetLineColor(r.kGreen)
bgreconvcuts.SetLineColor(r.kGreen+2)
bgreconvcuts.SetMarkerColor(r.kGreen+2)
c2 = r.TCanvas()
c2.cd()
cuts =     [ tc.recoed()[0]]
ntot = t.Draw('IP0>>bgtot', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in cuts])+ ")", "")
print ntot
w = t.GetHistogram().GetSumOfWeights()
print w
ccuts =     [ tc.recoed()[0], tc.ip(250.)[0] ]
nc = t.Draw('IP0>>bgtot', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in ccuts])+ ")", "")
print nc
wc = t.GetHistogram().GetSumOfWeights()
print wc
print wc/w
print w
print 1. - wc/w
nvcuts =     [ tc.recoed()[0], tc.notVetoed(1000.)[0] ]
nnv = t.Draw('IP0>>bgtot', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in nvcuts])+ ")", "")
print nnv
cnvcuts =     [ tc.recoed()[0], tc.notVetoed(1000.)[0], tc.ip(250.)[0] ]
nvw = t.GetHistogram().GetSumOfWeights()
print nvw
cnnv = t.Draw('IP0>>bgtot', "(weight*%s/%s)*("%(flux_nu, entries_nu)+"&&".join([c for c in cnvcuts])+ ")", "")
print cnnv
cnvw = t.GetHistogram().GetSumOfWeights()
print cnvw
print 1. - cnvw/nvw
print tc.noB
