Skip to content

Commit dfe359c

Browse files
committed
fix plotting
1 parent 43b38fa commit dfe359c

File tree

5 files changed

+249
-65
lines changed

5 files changed

+249
-65
lines changed

new_Hxx.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import ROOT
77
import uproot
88
from rhalphalib import AffineMorphTemplate
9-
#import matplotlib.pyplot as plt
10-
#import mplhep as hep
11-
#plt.style.use(hep.style.ROOT)
12-
#plt.switch_backend('agg')
9+
from util import make_dirs
1310

1411
rl.util.install_roofit_helpers()
1512

@@ -309,7 +306,6 @@ def dummy_rhalphabet(pseudo, throwPoisson, MCTF, justZ=False,
309306
# # Plot it
310307
from rhalphalib.plot.plot_TF import TF_smooth_plot, TF_params
311308
from rhalphalib.plot.plot_TF import plotTF as plotMCTF
312-
from utils import make_dirs
313309
_values = [par.value for par in tf_MCtempl.parameters.flatten()]
314310
_names = [par.name for par in tf_MCtempl.parameters.flatten()]
315311
np.save('{}/MCTF'.format(model_name), _values)

plot.py

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import matplotlib.ticker as mtick
1010
import numpy as np
1111

12-
from utils import make_dirs
12+
from util import make_dirs
1313

14-
from _plot_fractions import plot_fractions
15-
from _plot_cov import plot_cov
14+
from utils.plot_fractions import plot_fractions
15+
from rhalphalib.plot.plot_cov import plot_cov
1616

1717
import mplhep as hep
1818
plt.style.use([hep.cms.style.ROOT, {'font.size': 24}])
@@ -33,10 +33,6 @@ def str2bool(v):
3333
"--dir",
3434
default='',
3535
help="Model/Fit dir")
36-
parser.add_argument("--fd",
37-
action='store_true',
38-
dest='fitDiag',
39-
help="Plot from fitDiag")
4036
parser.add_argument("-i",
4137
"--input",
4238
default='fitDiagnostics.root',
@@ -142,7 +138,6 @@ def str2bool(v):
142138
def full_plot(cats, pseudo=True, fittype="", mask=False,
143139
toys=False,
144140
sqrtnerr=False,
145-
fromFD=False,
146141
filled=False,
147142
):
148143

@@ -219,17 +214,20 @@ def plot_data(x, y, yerr, xerr, ax=None, pseudo=pseudo, ugh=None, **kwargs):
219214
label=_d_label,
220215
**data_err_opts)
221216

222-
def th1_to_step(th1, restoreNorm=fromFD):
217+
def th1_to_step(th1, restoreNorm=True):
223218
_h, _bins = th1.numpy()
224219
if restoreNorm:
225220
_h = _h * np.diff(_bins)
226221
return _bins, np.r_[_h, _h[-1]]
227222

228-
def th1_to_err(th1):
223+
def th1_to_err(th1, restoreNorm=True):
229224
_h, _bins = th1.numpy()
230225
_x = _bins[:-1] + np.diff(_bins)/2
231226
_xerr = [abs(_bins[:-1] - _x), _bins[1:] - _x]
232227
_var = th1.variances
228+
if restoreNorm:
229+
_h = _h * np.diff(_bins)
230+
_var = _var * np.diff(_bins)
233231

234232
return _x, _h, _var, [_xerr[0], _xerr[1]]
235233

@@ -277,26 +275,23 @@ def from_cats(fcn, name):
277275
plt.subplots_adjust(hspace=0)
278276

279277
# Main
280-
if fromFD:
278+
279+
if hasattr(cats[0]['data'], "_fEXhigh"):
281280
res = np.array(list(map(tgasym_to_err, [cat['data'] for cat in cats])))
282281
else:
283-
res = np.array(list(map(th1_to_err, [cat['data_obs'] for cat in cats])))
282+
res = np.array(list(map(th1_to_err, [cat['data'] for cat in cats])))
284283
_x, _h = res[:, 0][0], np.sum(res[:, 1], axis=0)
285284
_xerr = res[:, -1][0]
286285
if sqrtnerr:
287286
_yerr = np.sqrt(_h)
288287
plot_data(_x, _h, yerr=[_yerr, _yerr], xerr=_xerr, ax=ax, ugh=ugh)
289288
else:
290-
if fromFD:
291-
# FIXME
292-
# _yerrlo = np.sqrt(np.sum(res[:, 2][0]**2, axis=0))
293-
# _yerrhi = np.sqrt(np.sum(res[:, 2][1]**2, axis=0))
294-
# print(_yerrlo, _yerrhi)
295-
#plot_data(_x, _h, yerr=[_yerrlo, _yerrhi], xerr=_xerr, ax=ax, ugh=ugh)
296-
plot_data(_x, _h, yerr=[np.sqrt(_h), np.sqrt(_h)], xerr=_xerr, ax=ax, ugh=ugh)
297-
else:
298-
_yerr = np.sqrt(np.sum(res[:, 2], axis=0))
299-
plot_data(_x, _h, yerr=[_yerr, _yerr], xerr=_xerr, ax=ax, ugh=ugh)
289+
# FIXME
290+
# _yerrlo = np.sqrt(np.sum(res[:, 2][0]**2, axis=0))
291+
# _yerrhi = np.sqrt(np.sum(res[:, 2][1]**2, axis=0))
292+
# print(_yerrlo, _yerrhi)
293+
#plot_data(_x, _h, yerr=[_yerrlo, _yerrhi], xerr=_xerr, ax=ax, ugh=ugh)
294+
plot_data(_x, _h, yerr=[np.sqrt(_h), np.sqrt(_h)], xerr=_xerr, ax=ax, ugh=ugh)
300295

301296
# Stack qcd/ttbar
302297
tot_h, bins = None, None
@@ -354,13 +349,13 @@ def from_cats(fcn, name):
354349
rax.axhline(0, c='gray', ls='--')
355350

356351
# Caculate diff
357-
if fromFD:
352+
if hasattr(cats[0]['data'], "_fEXhigh"):
358353
res = np.array(list(map(tgasym_to_err, [cat['data'] for cat in cats])))
359354
else:
360-
res = np.array(list(map(th1_to_err, [cat['data_obs'] for cat in cats])))
355+
res = np.array(list(map(th1_to_err, [cat['data'] for cat in cats])))
361356
_x, _y = res[:, 0][0], np.sum(res[:, 1], axis=0)
362357
_xerr = res[:, -1][0]
363-
if sqrtnerr or fromFD:
358+
if sqrtnerr:
364359
# FIXME fromFD should be separate
365360
_yerr = np.sqrt(_h)
366361
else:
@@ -566,15 +561,15 @@ def g(x, pos):
566561
"namespaces were found in the file: {}".format(
567562
args.fit, f.keys()))
568563

569-
fig = full_plot([cat], pseudo=args.pseudo, fittype=shape_type, mask=mask, toys=args.toys, fromFD=args.fitDiag, sqrtnerr=True)
564+
fig = full_plot([cat], pseudo=args.pseudo, fittype=shape_type, mask=mask, toys=args.toys, sqrtnerr=True)
570565

571566
if args.run2:
572567
cat_list = [f['shapes_{}/ptbin{}{}{};1'.format(shape_type, i, region, '2016')] for i in range(0, 6)]
573568
cat_list += [f['shapes_{}/ptbin{}{}{};1'.format(shape_type, i, region, '2017')] for i in range(0, 6)]
574569
cat_list += [f['shapes_{}/ptbin{}{}{};1'.format(shape_type, i, region, '2018')] for i in range(0, 6)]
575570
else:
576571
cat_list = [f['shapes_{}/ptbin{}{}{};1'.format(shape_type, i, region, args.year)] for i in range(0, 6)]
577-
full_plot(cat_list, pseudo=args.pseudo, fittype=shape_type, mask=mask, toys=args.toys, fromFD=args.fitDiag, sqrtnerr=True)
572+
full_plot(cat_list, pseudo=args.pseudo, fittype=shape_type, mask=mask, toys=args.toys, sqrtnerr=True)
578573

579574
# MuonCR if included
580575
# FIXME
@@ -590,39 +585,43 @@ def g(x, pos):
590585
# Take sqrt N err for data
591586
# Mock QCD while unavailable as template in rhalpha
592587
import os
593-
from input_shapes import input_dict_maker
594-
try:
595-
mockd = input_dict_maker(os.getcwd()+".pkl")
596-
597-
input_pseudo = True
598-
if args.toys or not args.pseudo:
599-
input_pseudo = False
600-
for shape_type in ["inputs"]:
601-
pbins = [450, 500, 550, 600, 675, 800, 1200]
602-
for region in regions:
603-
print("Plotting inputs", region)
604-
_mask = not input_pseudo
605-
mask = (_mask & (region == "pass")) | (_mask & (region == "pcc")) | (_mask & (region == "pbb"))
606-
full_plot([mockd['ptbin{}{}{}_{}'.format(i, region, args.year, shape_type)] for i in range(0, 6)],
607-
pseudo=input_pseudo, fittype=shape_type, mask=mask, sqrtnerr=True, toys=False)
608-
609-
# Per bin plots
610-
for i in range(0, 6):
611-
if not args.run_all: continue
612-
full_plot([mockd['ptbin{}{}{}_{}'.format(i, region, args.year, shape_type)]],
613-
pseudo=input_pseudo, fittype=shape_type, mask=mask, sqrtnerr=True, toys=False)
614-
615-
# MuonCR if included
616-
try:
617-
cat = mockd['muonCR{}_{}'.format(region, shape_type)]
618-
full_plot([cat], fittype=shape_type,
619-
pseudo=input_pseudo, mask=False, sqrtnerr=True, toys=False)
620-
print("Plotted input, muCR", region, shape_type)
621-
except Exception:
622-
print("Muon region not found")
623-
pass
624-
except:
625-
print("Input pkl file not found")
588+
from rhalphalib.plot.input_shapes import input_dict_maker
589+
590+
mockd = input_dict_maker(os.getcwd()+".pkl")
591+
print(mockd)
592+
593+
# try:
594+
mockd = input_dict_maker(os.getcwd()+".pkl")
595+
print(mockd)
596+
597+
input_pseudo = True
598+
if args.toys or not args.pseudo:
599+
input_pseudo = False
600+
for shape_type in ["inputs"]:
601+
pbins = [450, 500, 550, 600, 675, 800, 1200]
602+
for region in regions:
603+
print("Plotting inputs", region)
604+
_mask = not input_pseudo
605+
mask = (_mask & (region == "pass")) | (_mask & (region == "pcc")) | (_mask & (region == "pbb"))
606+
full_plot([mockd['ptbin{}{}{}_{}'.format(i, region, args.year, shape_type)] for i in range(0, 6)],
607+
pseudo=input_pseudo, fittype=shape_type, mask=mask, sqrtnerr=True, toys=False)
608+
# Per bin plots
609+
for i in range(0, 6):
610+
if not args.run_all: continue
611+
full_plot([mockd['ptbin{}{}{}_{}'.format(i, region, args.year, shape_type)]],
612+
pseudo=input_pseudo, fittype=shape_type, mask=mask, sqrtnerr=True, toys=False)
613+
614+
# MuonCR if included
615+
try:
616+
cat = mockd['muonCR{}_{}'.format(region, shape_type)]
617+
full_plot([cat], fittype=shape_type,
618+
pseudo=input_pseudo, mask=False, sqrtnerr=True, toys=False)
619+
print("Plotted input, muCR", region, shape_type)
620+
except Exception:
621+
print("Muon region not found")
622+
pass
623+
# except:
624+
# print("Input pkl file not found")
626625

627626
if args.three_regions:
628627
plot_fractions(os.path.join(args.dir, 'fitDiagnostics.root'),
File renamed without changes.

utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)