likelihoods.py 42.6 KB
Newer Older
Marcel Rieger's avatar
Marcel Rieger committed
1
# coding: utf-8
2
3
4
5
6
7

"""
Likelihood plots using ROOT.
"""

import math
8
from collections import OrderedDict
9
10

import numpy as np
11
12
13
import scipy.interpolate
import scipy.optimize
from scinum import Number
14

15
16
17
from dhi.config import (
    poi_data, br_hh_names, campaign_labels, chi2_levels, colors, color_sequence, marker_sequence,
)
18
from dhi.util import import_ROOT, to_root_latex, create_tgraph, DotDict, minimize_1d, multi_match
19
20
from dhi.plots.util import (
    use_style, draw_model_parameters, fill_hist_from_points, create_random_name, get_contours,
21
)
22
23


24
25
26
colors = colors.root


27
@use_style("dhi_default")
28
29
30
def plot_likelihood_scan_1d(
    path,
    poi,
31
    values,
32
    theory_value=None,
33
34
35
    poi_min=None,
    x_min=None,
    x_max=None,
36
    y_min=None,
37
    y_max=None,
38
    y_log=False,
39
40
    model_parameters=None,
    campaign=None,
41
42
):
    """
43
44
45
46
47
48
    Creates a likelihood plot of the 1D scan of a *poi* and saves it at *path*. *values* should be a
    mapping to lists of values or a record array with keys "<poi_name>" and "dnll2". *theory_value*
    can be a 3-tuple denoting the nominal theory prediction of the POI and its up and down
    uncertainties which is drawn as a vertical bar. When *poi_min* is set, it should be the value of
    the poi that leads to the best likelihood. Otherwise, it is estimated from the interpolated
    curve.
49

50
51
    *x_min* and *x_max* define the x-axis range of POI, and *y_min* and *y_max* control the range of
    the y-axis. When *y_log* is *True*, the y-axis is plotted with a logarithmic scale.
Marcel Rieger's avatar
Marcel Rieger committed
52
53
    *model_parameters* can be a dictionary of key-value pairs of model parameters. *campaign* should
    refer to the name of a campaign label defined in *dhi.config.campaign_labels*.
54

55
    Example: https://cms-hh.web.cern.ch/tools/inference/tasks/likelihood.html#1d
56
57
58
59
60
    """
    import plotlib.root as r
    ROOT = import_ROOT()

    # get valid poi and delta nll values
61
62
    poi_values = np.array(values[poi], dtype=np.float32)
    dnll2_values = np.array(values["dnll2"], dtype=np.float32)
63

64
    # set x range
65
66
67
68
69
70
71
72
73
74
    if x_min is None:
        x_min = min(poi_values)
    if x_max is None:
        x_max = max(poi_values)

    # select valid points
    mask = ~np.isnan(dnll2_values)
    poi_values = poi_values[mask]
    dnll2_values = dnll2_values[mask]

75
    # set y range
Marcel Rieger's avatar
Marcel Rieger committed
76
    y_max_value = max(dnll2_values[(poi_values >= x_min) & (poi_values <= x_max)])
77
    if y_log:
78
        if y_min is None:
79
            y_min = 1e-3
80
81
        if y_max is None:
            y_max = y_min * 10**(math.log10(y_max_value / y_min) * 1.35)
Marcel Rieger's avatar
Marcel Rieger committed
82
        y_max_line = y_min * 10**(math.log10(y_max / y_min) / 1.4)
83
    else:
84
        if y_min is None:
85
            y_min = 0.
86
87
        if y_max is None:
            y_max = 1.35 * (y_max_value - y_min)
Marcel Rieger's avatar
Marcel Rieger committed
88
        y_max_line = y_max / 1.4 + y_min
89
90
91
92
93
94
95
96
97

    # evaluate the scan, run interpolation and error estimation
    scan = evaluate_likelihood_scan_1d(poi_values, dnll2_values, poi_min=poi_min)

    # start plotting
    r.setup_style()
    canvas, (pad,) = r.routines.create_canvas(pad_props={"Logy": y_log})
    pad.cd()
    draw_objs = []
98
    legend_entries = []
99
100
101
102
103
104
105
106
107
108
109
110

    # dummy histogram to control axes
    x_title = to_root_latex(poi_data[poi].label)
    y_title = "-2 #Delta log(L)"
    h_dummy = ROOT.TH1F("dummy", ";{};{}".format(x_title, y_title), 1, x_min, x_max)
    r.setup_hist(h_dummy, pad=pad, props={"LineWidth": 0, "Minimum": y_min, "Maximum": y_max})
    draw_objs.append((h_dummy, "HIST"))

    # 1 and 2 sigma indicators
    for value in [scan.poi_p1, scan.poi_m1, scan.poi_p2, scan.poi_m2]:
        if value is not None:
            line = ROOT.TLine(value, y_min, value, scan.interp(value))
Marcel Rieger's avatar
Marcel Rieger committed
111
            r.setup_line(line, props={"LineColor": colors.black, "LineStyle": 2, "NDC": False})
112
113
114
115
            draw_objs.append(line)

    # lines at chi2_1 intervals
    for n in [chi2_levels[1][1], chi2_levels[1][2]]:
116
        if n < y_max_line:
117
            line = ROOT.TLine(x_min, n, x_max, n)
Marcel Rieger's avatar
Marcel Rieger committed
118
            r.setup_line(line, props={"LineColor": colors.black, "LineStyle": 2, "NDC": False})
119
120
            draw_objs.append(line)

121
122
    # theory prediction with uncertainties
    if theory_value:
Marcel Rieger's avatar
Marcel Rieger committed
123
124
125
126
127
        has_thy_err = len(theory_value) == 3
        if has_thy_err:
            # theory graph
            g_thy = create_tgraph(1, theory_value[0], y_min, theory_value[2], theory_value[1],
                0, y_max_line)
Marcel Rieger's avatar
Marcel Rieger committed
128
129
            r.setup_graph(g_thy, props={"LineColor": colors.red, "FillStyle": 1001,
                "FillColor": colors.red_trans_50})
Marcel Rieger's avatar
Marcel Rieger committed
130
131
132
            draw_objs.append((g_thy, "SAME,02"))
            legend_entries.append((g_thy, "Theory prediction", "LF"))
        # theory line
Marcel Rieger's avatar
Marcel Rieger committed
133
        line_thy = ROOT.TLine(theory_value[0], y_min, theory_value[0], y_max_line)
134
        r.setup_line(line_thy, props={"NDC": False}, color=colors.red)
135
        draw_objs.append(line_thy)
Marcel Rieger's avatar
Marcel Rieger committed
136
137
        if not has_thy_err:
            legend_entries.append((line_thy, "Theory prediction", "L"))
138
139

    # line for best fit value
140
    line_fit = ROOT.TLine(scan.poi_min, y_min, scan.poi_min, y_max_line)
141
    r.setup_line(line_fit, props={"LineWidth": 2, "NDC": False}, color=colors.black)
142
143
    fit_label = "{} = {}".format(to_root_latex(poi_data[poi].label),
        scan.num_min.str(format="%.2f", style="root"))
144
    draw_objs.append(line_fit)
Marcel Rieger's avatar
Marcel Rieger committed
145
    legend_entries.insert(0, (line_fit, fit_label, "L"))
146
147

    # nll curve
148
    g_nll = create_tgraph(len(poi_values), poi_values, dnll2_values)
149
150
151
    r.setup_graph(g_nll, props={"LineWidth": 2, "MarkerStyle": 20, "MarkerSize": 0.75})
    draw_objs.append((g_nll, "SAME,CP"))

152
    # legend
153
    legend = r.routines.create_legend(pad=pad, width=230, n=len(legend_entries))
154
155
156
157
158
    r.setup_legend(legend)
    for tpl in legend_entries:
        legend.AddEntry(*tpl)
    draw_objs.append(legend)

Marcel Rieger's avatar
Marcel Rieger committed
159
160
161
162
    # model parameter labels
    if model_parameters:
        draw_objs.extend(draw_model_parameters(model_parameters, pad))

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    # cms label
    cms_labels = r.routines.create_cms_labels(pad=pad)
    draw_objs.extend(cms_labels)

    # campaign label
    if campaign:
        campaign_label = to_root_latex(campaign_labels.get(campaign, campaign))
        campaign_label = r.routines.create_top_right_label(campaign_label, pad=pad)
        draw_objs.append(campaign_label)

    # draw all objects
    r.routines.draw_objects(draw_objs)

    # save
    r.update_canvas(canvas)
    canvas.SaveAs(path)


181
182
183
184
185
186
187
188
189
@use_style("dhi_default")
def plot_likelihood_scans_1d(
    path,
    poi,
    data,
    theory_value=None,
    x_min=None,
    x_max=None,
    y_min=None,
190
    y_max=None,
191
192
193
194
195
196
197
198
199
    y_log=False,
    model_parameters=None,
    campaign=None,
):
    """
    Plots multiple curves of 1D likelihood scans of a POI *poi1* and *poi2*, and saves it at *path*.
    All information should be passed as a list *data*. Entries must be dictionaries with the
    following content:

200
        - "values": A mapping to lists of values or a record array with keys "<poi1_name>",
201
202
203
204
205
206
207
208
          "<poi2_name>" and "dnll2".
        - "poi_min": A float describing the best fit value of the POI. When not set, the minimum is
          estimated from the interpolated curve.
        - "name": A name of the data to be shown in the legend.

    *theory_value* can be a 3-tuple denoting the nominal theory prediction of the POI and its up and
    down uncertainties which is drawn as a vertical bar.

209
    *x_min* and *x_max* define the x-axis range of POI, and *y_min* and *y_max* control the range of
210
    the y-axis. When *y_log* is *True*, the y-axis is plotted with a logarithmic scale. When
Marcel Rieger's avatar
Marcel Rieger committed
211
212
    *model_parameters* can be a dictionary of key-value pairs of model parameters. *campaign* should
    refer to the name of a campaign label defined in *dhi.config.campaign_labels*.
213

214
    Example: https://cms-hh.web.cern.ch/tools/inference/tasks/likelihood.html#1d_1
215
216
217
218
219
220
    """
    import plotlib.root as r
    ROOT = import_ROOT()

    # validate data entries
    for i, d in enumerate(data):
221
222
223
224
225
226
227
        # convert likelihood values to arrays
        assert("values" in d)
        values = d["values"]
        if isinstance(values, np.ndarray):
            values = {k: values[k] for k in values.dtype.names}
        assert(poi in values)
        assert("dnll2" in values)
228
        # keep only valid points
229
230
231
232
233
        values = {k: np.array(v, dtype=np.float32) for k, v in values.items()}
        mask = ~np.isnan(values["dnll2"])
        values[poi] = values[poi][mask]
        values["dnll2"] = values["dnll2"][mask]
        d["values"] = values
234
235
236
237
238
239
240
        # check poi minimum
        d.setdefault("poi_min", None)
        # default name
        d.setdefault("name", str(i + 1))

    # set x range
    if x_min is None:
241
        x_min = min([min(d["values"][poi]) for d in data])
242
    if x_max is None:
243
        x_max = max([max(d["values"][poi]) for d in data])
244
245
246

    # set y range
    y_max_value = max([
Marcel Rieger's avatar
Marcel Rieger committed
247
        d["values"]["dnll2"][(d["values"][poi] >= x_min) & (d["values"][poi] <= x_max)].max()
248
249
250
251
        for d in data
    ])
    if y_log:
        if y_min is None:
252
            y_min = 1e-3
253
254
        if y_max is None:
            y_max = y_min * 10**(math.log10(y_max_value / y_min) * 1.35)
Marcel Rieger's avatar
Marcel Rieger committed
255
        y_max_line = y_min * 10**(math.log10(y_max / y_min) / 1.4)
256
257
258
    else:
        if y_min is None:
            y_min = 0.
259
260
        if y_max is None:
            y_max = 1.35 * (y_max_value - y_min)
Marcel Rieger's avatar
Marcel Rieger committed
261
        y_max_line = y_max / 1.4 + y_min
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

    # start plotting
    r.setup_style()
    canvas, (pad,) = r.routines.create_canvas(pad_props={"Logy": y_log})
    pad.cd()
    draw_objs = []
    legend_entries = []

    # dummy histogram to control axes
    x_title = to_root_latex(poi_data[poi].label)
    y_title = "-2 #Delta log(L)"
    h_dummy = ROOT.TH1F("dummy", ";{};{}".format(x_title, y_title), 1, x_min, x_max)
    r.setup_hist(h_dummy, pad=pad, props={"LineWidth": 0, "Minimum": y_min, "Maximum": y_max})
    draw_objs.append((h_dummy, "HIST"))

    # lines at chi2_1 intervals
    for n in [chi2_levels[1][1], chi2_levels[1][2]]:
279
        if n < y_max:
280
281
282
283
284
285
            line = ROOT.TLine(x_min, n, x_max, n)
            r.setup_line(line, props={"LineColor": colors.black, "LineStyle": 2, "NDC": False})
            draw_objs.append(line)

    # theory prediction with uncertainties
    if theory_value:
Marcel Rieger's avatar
Marcel Rieger committed
286
287
288
289
290
        has_thy_err = len(theory_value) == 3
        if has_thy_err:
            # theory graph
            g_thy = create_tgraph(1, theory_value[0], y_min, theory_value[2], theory_value[1],
                0, y_max_line)
Marcel Rieger's avatar
Marcel Rieger committed
291
292
            r.setup_graph(g_thy, props={"LineColor": colors.red, "FillStyle": 1001,
                "FillColor": colors.red_trans_50})
Marcel Rieger's avatar
Marcel Rieger committed
293
294
295
296
            draw_objs.append((g_thy, "SAME,02"))
            legend_entries.append((g_thy, "Theory prediction", "LF"))
        # theory line
        line_thy = ROOT.TLine(theory_value[0], y_min, theory_value[0], y_max_line)
297
298
        r.setup_line(line_thy, props={"NDC": False}, color=colors.red)
        draw_objs.append(line_thy)
Marcel Rieger's avatar
Marcel Rieger committed
299
300
        if not has_thy_err:
            legend_entries.append((line_thy, "Theory prediction", "L"))
301
302

    # perform scans and draw nll curves
303
    for d, col, ms in zip(data, color_sequence[:len(data)], marker_sequence[:len(data)]):
304
        # evaluate the scan, run interpolation and error estimation
305
        scan = evaluate_likelihood_scan_1d(d["values"][poi], d["values"]["dnll2"],
306
307
308
            poi_min=d["poi_min"])

        # draw the curve
309
310
        g_nll = create_tgraph(len(d["values"][poi]), d["values"][poi],
            d["values"]["dnll2"])
311
        r.setup_graph(g_nll, props={"LineWidth": 2, "MarkerStyle": ms, "MarkerSize": 1.2},
312
            color=colors[col])
313
        draw_objs.append((g_nll, "SAME,CP"))
Marcel Rieger's avatar
Marcel Rieger committed
314
        legend_entries.append((g_nll, to_root_latex(br_hh_names.get(d["name"], d["name"])), "LP"))
315
316

        # line for best fit value
317
        line_fit = ROOT.TLine(scan.poi_min, y_min, scan.poi_min, y_max_line)
318
        r.setup_line(line_fit, props={"LineWidth": 2, "NDC": False}, color=colors[col])
319
320
321
322
323
324
        draw_objs.append(line_fit)

    # legend
    legend_cols = min(int(math.ceil(len(legend_entries) / 4.)), 3)
    legend_rows = int(math.ceil(len(legend_entries) / float(legend_cols)))
    legend = r.routines.create_legend(pad=pad, width=legend_cols * 210, n=legend_rows,
325
        props={"NColumns": legend_cols, "TextSize": 18})
326
327
    r.fill_legend(legend, legend_entries)
    draw_objs.append(legend)
Marcel Rieger's avatar
Marcel Rieger committed
328
    legend_box = r.routines.create_legend_box(legend, pad, "trl",
Marcel Rieger's avatar
Marcel Rieger committed
329
330
        props={"LineWidth": 0, "FillColor": colors.white_trans_70})
    draw_objs.insert(-1, legend_box)
331

Marcel Rieger's avatar
Marcel Rieger committed
332
333
334
335
    # model parameter labels
    if model_parameters:
        draw_objs.extend(draw_model_parameters(model_parameters, pad))

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    # cms label
    cms_labels = r.routines.create_cms_labels(pad=pad)
    draw_objs.extend(cms_labels)

    # campaign label
    if campaign:
        campaign_label = to_root_latex(campaign_labels.get(campaign, campaign))
        campaign_label = r.routines.create_top_right_label(campaign_label, pad=pad)
        draw_objs.append(campaign_label)

    # draw all objects
    r.routines.draw_objects(draw_objs)

    # save
    r.update_canvas(canvas)
    canvas.SaveAs(path)


@use_style("dhi_default")
355
356
357
358
def plot_likelihood_scan_2d(
    path,
    poi1,
    poi2,
359
    values,
360
361
    poi1_min=None,
    poi2_min=None,
Marcel Rieger's avatar
Marcel Rieger committed
362
    draw_sm_point=True,
363
364
365
366
367
368
369
370
    x_min=None,
    x_max=None,
    y_min=None,
    y_max=None,
    z_min=None,
    z_max=None,
    model_parameters=None,
    campaign=None,
371
372
):
    """
373
    Creates a likelihood plot of the 2D scan of two POIs *poi1* and *poi2*, and saves it at *path*.
374
375
376
    *values* should be a mapping to lists of values or a record array with keys "<poi1_name>",
    "<poi2_name>" and "dnll2". When *poi1_min* and *poi2_min* are set, they should be the values of
    the POIs that lead to the best likelihood. Otherwise, they are  estimated from the interpolated
Marcel Rieger's avatar
Marcel Rieger committed
377
    curve. The standard model point at (1, 1) as drawn as well unless *draw_sm_point* is *False*.
378

379
380
    *x_min*, *x_max*, *y_min* and *y_max* define the axis range of *poi1* and *poi2*, respectively,
    and default to the ranges of the poi values. *z_min* and *z_max* limit the range of the z-axis.
381
382
    *model_parameters* can be a dictionary of key-value pairs of model parameters. *campaign* should
    refer to the name of a campaign label defined in *dhi.config.campaign_labels*.
383
384

    Example: https://cms-hh.web.cern.ch/tools/inference/tasks/likelihood.html#2d
385
386
387
388
389
    """
    import plotlib.root as r
    ROOT = import_ROOT()

    # get poi and delta nll values
390
391
392
    poi1_values = np.array(values[poi1], dtype=np.float32)
    poi2_values = np.array(values[poi2], dtype=np.float32)
    dnll2_values = np.array(values["dnll2"], dtype=np.float32)
393
394
395

    # evaluate the scan, run interpolation and error estimation
    scan = evaluate_likelihood_scan_2d(
396
        poi1_values, poi2_values, dnll2_values, poi1_min=poi1_min, poi2_min=poi2_min,
397
398
    )

399
400
401
402
403
404
405
406
    # determine contours independent of plotting
    contours = get_contours(
        poi1_values,
        poi2_values,
        dnll2_values,
        levels=[chi2_levels[2][1], chi2_levels[2][2]],
        frame_kwargs=[{"mode": "edge"}],
    )
407

408
409
    # start plotting
    r.setup_style()
410
    canvas, (pad,) = r.routines.create_canvas(pad_props={"RightMargin": 0.17, "Logz": True})
411
412
413
    pad.cd()
    draw_objs = []

414
415
416
417
418
419
420
    # create the 2D histogram from values
    h_nll = create_dnll2_hist(poi1_values, poi2_values, dnll2_values, x_min=x_min, x_max=x_max,
        y_min=y_min, y_max=y_max, z_min=z_min, z_max=z_max)
    x_min = h_nll.GetXaxis().GetXmin()
    x_max = h_nll.GetXaxis().GetXmax()
    y_min = h_nll.GetYaxis().GetXmin()
    y_max = h_nll.GetYaxis().GetXmax()
Marcel Rieger's avatar
Marcel Rieger committed
421
    z_min = h_nll.GetMinimum() or 1e-3
422
423
424
    z_max = h_nll.GetMaximum()

    # dummy histogram to control axes
425
426
427
    x_title = to_root_latex(poi_data[poi1].label)
    y_title = to_root_latex(poi_data[poi2].label)
    z_title = "-2 #Delta log(L)"
428
429
430
    h_dummy = ROOT.TH2F("h_nll", ";{};{};{}".format(x_title, y_title, z_title),
        1, x_min, x_max, 1, y_min, y_max)
    r.setup_hist(h_dummy, pad=pad, props={"Contour": 100, "Minimum": z_min, "Maximum": z_max})
Marcel Rieger's avatar
Marcel Rieger committed
431
    draw_objs.append((h_dummy, ""))
432
433
434

    # setup the nll hist
    r.setup_hist(h_nll, props={"ContourXX": 100, "Minimum": z_min, "Maximum": z_max})
Marcel Rieger's avatar
Marcel Rieger committed
435
    r.setup_z_axis(h_nll.GetZaxis(), pad=pad, props={"Title": z_title, "TitleOffset": 1.3})
436
437
438
439
440
441
442
443
444
    draw_objs.append((h_nll, "SAME,COLZ"))

    # 1 and 2 sigma contours
    for g in contours[0]:
        r.setup_graph(g, props={"LineWidth": 2, "LineColor": colors.green})
        draw_objs.append((g, "SAME,C"))
    for g in contours[1]:
        r.setup_graph(g, props={"LineWidth": 2, "LineColor": colors.yellow})
        draw_objs.append((g, "SAME,C"))
445

Marcel Rieger's avatar
Marcel Rieger committed
446
447
448
449
450
451
    # SM point
    if draw_sm_point:
        g_sm = create_tgraph(1, 1, 1)
        r.setup_graph(g_sm, props={"MarkerStyle": 33, "MarkerSize": 2.5}, color=colors.red)
        draw_objs.insert(-1, (g_sm, "P"))

452
453
454
    # best fit point
    g_fit = ROOT.TGraphAsymmErrors(1)
    g_fit.SetPoint(0, scan.num1_min(), scan.num2_min())
455
456
457
458
459
460
    if scan.num1_min.uncertainties:
        g_fit.SetPointEXhigh(0, scan.num1_min.u(direction="up"))
        g_fit.SetPointEXlow(0, scan.num1_min.u(direction="down"))
    if scan.num2_min.uncertainties:
        g_fit.SetPointEYhigh(0, scan.num2_min.u(direction="up"))
        g_fit.SetPointEYlow(0, scan.num2_min.u(direction="down"))
Marcel Rieger's avatar
Marcel Rieger committed
461
    r.setup_graph(g_fit, color=colors.black)
462
463
    draw_objs.append((g_fit, "PEZ"))

464
    # measurement and best fit value labels
465
466
467
468
    fit_label1 = "{} = {}".format(to_root_latex(poi_data[poi1].label),
        scan.num1_min.str(format="%.2f", style="root"))
    fit_label2 = "{} = {}".format(to_root_latex(poi_data[poi2].label),
        scan.num2_min.str(format="%.2f", style="root"))
469
470
    labels = [fit_label1, fit_label2]
    for i, l in enumerate(labels):
Marcel Rieger's avatar
Marcel Rieger committed
471
        l = r.routines.create_top_right_label(l, pad=pad, x_offset=160, y_offset=30 + i * 34,
472
473
            props={"TextAlign": 13})
        draw_objs.append(l)
474

475
    # model parameter labels
476
    if model_parameters:
Marcel Rieger's avatar
Marcel Rieger committed
477
        draw_objs.extend(draw_model_parameters(model_parameters, pad))
478

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
    # cms label
    cms_labels = r.routines.create_cms_labels(pad=pad)
    draw_objs.extend(cms_labels)

    # campaign label
    if campaign:
        campaign_label = to_root_latex(campaign_labels.get(campaign, campaign))
        campaign_label = r.routines.create_top_right_label(campaign_label, pad=pad)
        draw_objs.append(campaign_label)

    # draw all objects
    r.routines.draw_objects(draw_objs)

    # save
    r.update_canvas(canvas)
    canvas.SaveAs(path)
495
496


497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
@use_style("dhi_default")
def plot_likelihood_scans_2d(
    path,
    poi1,
    poi2,
    data,
    x_min=None,
    x_max=None,
    y_min=None,
    y_max=None,
    fill_nans=True,
    model_parameters=None,
    campaign=None,
):
    """
    Creates the likelihood contour plots of multiple 2D scans of two POIs *poi1* and *poi2*, and
    saves it at *path*. All information should be passed as a list *data*. Entries must be
    dictionaries with the following content:

516
        - "values": A mapping to lists of values or a record array with keys "<poi1_name>",
517
518
519
520
521
522
523
          "<poi2_name>" and "dnll2".
        - "poi_mins": A list of two floats describing the best fit value of the two POIs. When not
          set, the minima are estimated from the interpolated curve.
        - "name": A name of the data to be shown in the legend.

    *x_min*, *x_max*, *y_min* and *y_max* define the axis range of *poi1* and *poi2*, respectively,
    and default to the ranges of the poi values. When *fill_nans* is *True*, points with failed
524
    fits, denoted by nan values, are filled with the averages of neighboring fits. When
Marcel Rieger's avatar
Marcel Rieger committed
525
526
    *model_parameters* can be a dictionary of key-value pairs of model parameters. *campaign* should
    refer to the name of a campaign label defined in *dhi.config.campaign_labels*.
527

528
    Example: Example: https://cms-hh.web.cern.ch/tools/inference/tasks/likelihood.html#2d_1
529
530
531
532
533
534
    """
    import plotlib.root as r
    ROOT = import_ROOT()

    # validate data entries
    for i, d in enumerate(data):
535
536
537
538
539
540
541
542
543
544
        # convert likelihood values to arrays
        assert("values" in d)
        values = d["values"]
        if isinstance(values, np.ndarray):
            values = {k: values[k] for k in values.dtype.names}
        assert(poi1 in values)
        assert(poi2 in values)
        assert("dnll2" in values)
        values = {k: np.array(v, dtype=np.float32) for k, v in values.items()}
        d["values"] = values
545
546
547
548
549
550
        # check poi minima
        d["poi_mins"] = d.get("poi_mins") or [None, None]
        assert(len(d["poi_mins"]) == 2)
        # default name
        d.setdefault("name", str(i + 1))

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
    # determine contours independent of plotting
    contours = [
        get_contours(
            d["values"][poi1],
            d["values"][poi2],
            d["values"]["dnll2"],
            levels=[chi2_levels[2][1], chi2_levels[2][2]],
            frame_kwargs=[{"mode": "edge"}],
        )
        for d in data
    ]

    # start plotting
    r.setup_style()
    canvas, (pad,) = r.routines.create_canvas(pad_props={"Logz": True})
    pad.cd()
    legend_entries = []
    draw_objs = []

570
571
    # set ranges
    if x_min is None:
572
        x_min = min([min(d["values"][poi1]) for d in data])
573
    if x_max is None:
574
        x_max = max([max(d["values"][poi1]) for d in data])
575
    if y_min is None:
576
        y_min = min([min(d["values"][poi2]) for d in data])
577
    if y_max is None:
578
        y_max = max([max(d["values"][poi2]) for d in data])
579
580
581
582
583

    # dummy histogram to control axes
    x_title = to_root_latex(poi_data[poi1].label)
    y_title = to_root_latex(poi_data[poi2].label)
    h_dummy = ROOT.TH2F("h", ";{};{};".format(x_title, y_title), 1, x_min, x_max, 1, y_min, y_max)
Marcel Rieger's avatar
Marcel Rieger committed
584
    r.setup_hist(h_dummy, pad=pad, props={"LineWidth": 0})
585
586
587
    draw_objs.append((h_dummy, "HIST"))

    # loop through data entries
588
    for d, (cont1, cont2), col in zip(data, contours, color_sequence[:len(data)]):
589
590
        # evaluate the scan
        scan = evaluate_likelihood_scan_2d(
591
            d["values"][poi1], d["values"][poi2], d["values"]["dnll2"],
592
593
594
            poi1_min=d["poi_mins"][0], poi2_min=d["poi_mins"][1],
        )

595
596
597
598
599
600
601
602
603
        # plot 1 and 2 sigma contours
        for g1 in cont1:
            r.setup_graph(g1, props={"LineWidth": 2, "LineStyle": 1, "LineColor": colors[col]})
            draw_objs.append((g1, "SAME,C"))
        for g2 in cont2:
            r.setup_graph(g2, props={"LineWidth": 2, "LineStyle": 2, "LineColor": colors[col]})
            draw_objs.append((g2, "SAME,C"))
        name = to_root_latex(br_hh_names.get(d["name"], d["name"]))
        legend_entries.append((g1, name, "L"))
604
605
606
607

        # best fit point
        g_fit = create_tgraph(1, scan.num1_min(), scan.num2_min())
        r.setup_graph(g_fit, props={"MarkerStyle": 33, "MarkerSize": 2}, color=colors[col])
608
        draw_objs.append((g_fit, "SAME,PEZ"))
Marcel Rieger's avatar
Marcel Rieger committed
609
610
611

    # append legend entries to show styles
    g_fit_style = g_fit.Clone()
612
613
    g1_style = g1.Clone()
    g2_style = g2.Clone()
Marcel Rieger's avatar
Marcel Rieger committed
614
    r.apply_properties(g_fit_style, {"MarkerColor": colors.black})
615
616
    r.apply_properties(g1_style, {"LineColor": colors.black})
    r.apply_properties(g2_style, {"LineColor": colors.black})
Marcel Rieger's avatar
Marcel Rieger committed
617
618
    legend_entries.extend([
        (g_fit_style, "Best fit value", "P"),
619
620
        (g1_style, "#pm 1 #sigma", "L"),
        (g2_style, "#pm 2 #sigma", "L"),
Marcel Rieger's avatar
Marcel Rieger committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    ])

    # prepend empty values
    n_empty = 3 - (len(legend_entries) % 3)
    if n_empty not in (0, 3):
        for _ in range(n_empty):
            legend_entries.insert(3 - n_empty, (h_dummy, " ", "L"))

    # legend with actual entries in different colors
    legend_cols = int(math.ceil(len(legend_entries) / 3.))
    legend_rows = min(len(legend_entries), 3)
    legend = r.routines.create_legend(pad=pad, width=legend_cols * 150, height=legend_rows * 30,
        props={"NColumns": legend_cols})
    r.fill_legend(legend, legend_entries)
    draw_objs.append(legend)
    legend_box = r.routines.create_legend_box(legend, pad, "trl",
        props={"LineWidth": 0, "FillColor": colors.white_trans_70})
    draw_objs.insert(-1, legend_box)
639

640
    # model parameter labels
641
    if model_parameters:
Marcel Rieger's avatar
Marcel Rieger committed
642
        draw_objs.extend(draw_model_parameters(model_parameters, pad))
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661

    # cms label
    cms_labels = r.routines.create_cms_labels(pad=pad)
    draw_objs.extend(cms_labels)

    # campaign label
    if campaign:
        campaign_label = to_root_latex(campaign_labels.get(campaign, campaign))
        campaign_label = r.routines.create_top_right_label(campaign_label, pad=pad)
        draw_objs.append(campaign_label)

    # draw all objects
    r.routines.draw_objects(draw_objs)

    # save
    r.update_canvas(canvas)
    canvas.SaveAs(path)


662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
def plot_nuisance_likelihood_scans(
    path,
    poi,
    workspace,
    dataset,
    fit_diagnostics_path,
    fit_name="fit_s",
    skip_parameters=None,
    only_parameters=None,
    parameters_per_page=1,
    scan_points=201,
    x_min=-2.,
    x_max=2,
    y_log=False,
    model_parameters=None,
    campaign=None,
):
    """
    Creates a plot showing the change of the negative log-likelihood, obtained *poi*, when varying
    values of nuisance paramaters and saves it at *path*. The calculation of the likelihood change
    requires the RooFit *workspace* to read the model config, a RooDataSet *dataset* to construct
    the functional likelihood, and the output file *fit_diagnostics_path* of the combine fit
    diagnostics for reading pre- and post-fit parameters for the fit named *fit_name*, defaulting
    to ``"fit_s"``.

    Nuisances to skip, or to show exclusively can be configured via *skip_parameters* and
    *only_parameters*, respectively, which can be lists of patterns. *parameters_per_page* defines
    the number of parameter curves that are drawn in the same canvas page. The scan range and
    granularity is set via *scan_points*, *x_min* and *x_max*. When *y_log* is *True*, the y-axis is
    plotted with a logarithmic scale. *model_parameters* can be a dictionary of key-value pairs of
    model parameters. *campaign* should refer to the name of a campaign label defined in
    *dhi.config.campaign_labels*.

    Example: https://cms-hh.web.cern.ch/tools/inference/tasks/postfit.html#nuisance-parameter-influence-on-likelihood
    """
    import plotlib.root as r
    ROOT = import_ROOT()

    # helper to convert a RooArgSet  into a dictionary mapping names to value-errors pairs
    def convert_argset(argset):
        data = OrderedDict()
        it = argset.createIterator()
        while True:
            param = it.Next()
            if not param:
                break
            data[param.GetName()] = (param.getVal(), param.getErrorHi(), param.getErrorLo())
        return data

    # get the best fit value and prefit data from the diagnostics file
    f = ROOT.TFile(fit_diagnostics_path, "READ")
    best_fit = f.Get(fit_name)
    fit_args = best_fit.floatParsFinal()
    prefit_params = convert_argset(f.Get("nuisances_prefit"))

    # get the model config from the workspace
    model_config = workspace.genobj("ModelConfig")

    # build the nll object
    nll_args = ROOT.RooLinkedList()
    nll_args.Add(ROOT.RooFit.Constrain(model_config.GetNuisanceParameters()))
    nll_args.Add(ROOT.RooFit.Extended(model_config.GetPdf().canBeExtended()))
    nll = model_config.GetPdf().createNLL(dataset, nll_args)

    # save the best fit in a snap shot
    snapshot_name = "best_fit_parameters"
    workspace.saveSnapshot(snapshot_name, ROOT.RooArgSet(fit_args), True)

    # prepare parameters to plot, stored in groups
    param_names = [[]]
    for param_name in prefit_params:
        if only_parameters and not multi_match(param_name, only_parameters):
            continue
        if skip_parameters and multi_match(param_name, skip_parameters):
            continue
        if parameters_per_page < 1 or len(param_names[-1]) < parameters_per_page:
            param_names[-1].append(param_name)
        else:
            param_names.append([param_name])

    # prepare the scan values, ensure that 0 is contained
    scan_values = np.linspace(x_min, x_max, scan_points).tolist()
    if 0 not in scan_values:
        scan_values = sorted(scan_values + [0.])

    # go through nuisances
    canvas = None
    for _param_names in param_names:
        # setup the default style and create canvas and pad
        first_canvas = canvas is None
        r.setup_style()
        canvas, (pad,) = r.routines.create_canvas(pad_props={"Logy": y_log})
        pad.cd()

        # start the multi pdf file
        if first_canvas:
            canvas.Print(path + "[")

        # get nll curves for all parameters on this page
        curve_data = []
        for param_name in _param_names:
            pre_u, pre_d = prefit_params[param_name][1:3]
            workspace.loadSnapshot(snapshot_name)
            param = workspace.var(param_name)
            if not param:
                raise Exception("parameter {} not found in workspace".format(param_name))
            param_bf = param.getVal()
            nll_base = nll.getVal()
            x_values, y_values = [], []
            for x in scan_values:
                param.setVal(param_bf + (pre_u if x >= 0 else -pre_d) * x)
                x_values.append(param.getVal())
                y_values.append(2 * (nll.getVal() - nll_base))
            curve_data.append((param_name, x_values, y_values))

        # get y range
        y_min_value = min(min(y_values) for _, _, y_values in curve_data)
        y_max_value = max(max(y_values) for _, _, y_values in curve_data)
        if y_log:
            y_min = 1.e-3
            y_max = y_min * 10**(1.35 * math.log10(y_max_value / y_min))
        else:
            y_min = y_min_value
            y_max = 1.35 * (y_max_value - y_min)

        # dummy histogram to control axes
        x_title = "(#theta - #theta_{best}) / #Delta#theta_{pre}"
        y_title = "Change in -2 log(L)"
        h_dummy = ROOT.TH1F("dummy", ";{};{}".format(x_title, y_title), 1, x_min, x_max)
        r.setup_hist(h_dummy, pad=pad, props={"LineWidth": 0, "Minimum": y_min, "Maximum": y_max})
        draw_objs = [(h_dummy, "HIST")]
        legend_entries = []

        # nll graphs
        for (param_name, x, y), col in zip(curve_data, color_sequence[:len(curve_data)]):
            g_nll = create_tgraph(len(x), x, y)
            r.setup_graph(g_nll, props={"LineWidth": 2, "LineStyle": 1}, color=colors[col])
            draw_objs.append((g_nll, "SAME,C"))
            legend_entries.append((g_nll, to_root_latex(param_name), "L"))

        # legend
        legend_cols = min(int(math.ceil(len(legend_entries) / 4.)), 3)
        legend_rows = int(math.ceil(len(legend_entries) / float(legend_cols)))
        legend = r.routines.create_legend(pad=pad, width=legend_cols * 210, n=legend_rows,
            props={"NColumns": legend_cols, "TextSize": 18})
        r.fill_legend(legend, legend_entries)
        draw_objs.append(legend)
        legend_box = r.routines.create_legend_box(legend, pad, "trl",
            props={"LineWidth": 0, "FillColor": colors.white_trans_70})
        draw_objs.insert(-1, legend_box)

        # model parameter labels
        if model_parameters:
            draw_objs.extend(draw_model_parameters(model_parameters, pad))

        # cms label
        cms_labels = r.routines.create_cms_labels(pad=pad)
        draw_objs.extend(cms_labels)

        # campaign label
        if campaign:
            campaign_label = to_root_latex(campaign_labels.get(campaign, campaign))
            campaign_label = r.routines.create_top_right_label(campaign_label, pad=pad)
            draw_objs.append(campaign_label)

        # draw objects, update and save
        r.routines.draw_objects(draw_objs)
        r.update_canvas(canvas)
        canvas.Print(path)

    # finish the pdf
    if canvas:
        canvas.Print(path + "]")


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
def evaluate_likelihood_scan_1d(poi_values, dnll2_values, poi_min=None):
    """
    Takes the results of a 1D likelihood profiling scan given by the *poi_values* and the
    corresponding *delta_2nll* values, performs an interpolation and returns certain results of the
    scan in a dict. When *poi_min* is *None*, it is estimated from the interpolated curve.

    The returned fields are:

    - ``interp``: The generated interpolation function.
    - ``poi_min``: The poi value corresponding to the minimum delta nll value.
    - ``poi_p1``: The poi value corresponding to the +1 sigma variation, or *None* when the
      calculation failed.
    - ``poi_m1``: The poi value corresponding to the -1 sigma variation, or *None* when the
      calculation failed.
    - ``poi_p2``: The poi value corresponding to the +2 sigma variation, or *None* when the
      calculation failed.
    - ``poi_m2``: The poi value corresponding to the -2 sigma variation, or *None* when the
      calculation failed.
    - ``num_min``: A Number instance representing the best fit value and its 1 sigma uncertainty.
    """
    # ensure we are dealing with arrays
    poi_values = np.array(poi_values)
    dnll2_values = np.array(dnll2_values)

    # store ranges
    poi_values_min = poi_values.min()
    poi_values_max = poi_values.max()

865
    # remove values where dnll2 is nan
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
    mask = ~np.isnan(dnll2_values)
    poi_values = poi_values[mask]
    dnll2_values = dnll2_values[mask]

    # first, obtain an interpolation function
    # interp = scipy.interpolate.interp1d(poi_values, dnll2_values, kind="cubic")
    interp = scipy.interpolate.interp1d(poi_values, dnll2_values, kind="linear")

    # get the minimum when not set
    if poi_min is None:
        objective = lambda x: abs(interp(x))
        bounds = (poi_values_min + 1e-4, poi_values_max - 1e-4)
        res = minimize_1d(objective, bounds)
        if res.status != 0:
            raise Exception("could not find minimum of nll2 interpolation: {}".format(res.message))
        poi_min = res.x[0]

    # helper to get the outermost intersection of the nll curve with a certain value
    def get_intersections(v):
        def minimize(bounds):
886
887
888
889
890
891
892
893
            # get a good starting point within the bounds and close to poi_min
            linspace = np.linspace(bounds[0], bounds[1], 100)
            for start in sorted(linspace, key=lambda x: abs(x - poi_min)):
                if interp(start) > v:
                    break
            else:
                start = poi_min
            # minimize
894
            objective = lambda x: (interp(x) - v) ** 2.0
895
            res = minimize_1d(objective, bounds, start=start)
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
            return res.x[0] if res.status == 0 and (bounds[0] < res.x[0] < bounds[1]) else None

        return (
            minimize((poi_min, poi_values_max - 1e-4)),
            minimize((poi_values_min + 1e-4, poi_min)),
        )

    # get the intersections with values corresponding to 1 and 2 sigma
    # (taken from solving chi2_1_cdf(x) = 1 or 2 sigma gauss intervals)
    poi_p1, poi_m1 = get_intersections(chi2_levels[1][1])
    poi_p2, poi_m2 = get_intersections(chi2_levels[1][2])

    # create a Number object wrapping the best fit value and its 1 sigma error when given
    unc = None
    if poi_p1 is not None and poi_m1 is not None:
        unc = (poi_p1 - poi_min, poi_min - poi_m1)
    num_min = Number(poi_min, unc)

    return DotDict(
        interp=interp,
        poi_min=poi_min,
        poi_p1=poi_p1,
        poi_m1=poi_m1,
        poi_p2=poi_p2,
        poi_m2=poi_m2,
        num_min=num_min,
    )


def evaluate_likelihood_scan_2d(
    poi1_values, poi2_values, dnll2_values, poi1_min=None, poi2_min=None
):
    """
    Takes the results of a 2D likelihood profiling scan given by *poi1_values*, *poi2_values* and
    the corresponding *dnll2_values* values, performs an interpolation and returns certain results
    of the scan in a dict. The two lists of poi values should represent an expanded grid, so that
    *poi1_values*, *poi2_values* and *dnll2_values* should all be 1D with the same length. When
    *poi1_min* and *poi2_min* are *None*, they are estimated from the interpolated curve.

    The returned fields are:

    - ``interp``: The generated interpolation function.
    - ``poi1_min``: The poi1 value corresponding to the minimum delta nll value.
    - ``poi2_min``: The poi2 value corresponding to the minimum delta nll value.
    - ``poi1_p1``: The poi1 value corresponding to the +1 sigma variation, or *None* when the
      calculation failed.
    - ``poi1_m1``: The poi1 value corresponding to the -1 sigma variation, or *None* when the
      calculation failed.
    - ``poi1_p2``: The poi1 value corresponding to the +2 sigma variation, or *None* when the
      calculation failed.
    - ``poi1_m2``: The poi1 value corresponding to the -2 sigma variation, or *None* when the
      calculation failed.
    - ``poi2_p1``: The poi2 value corresponding to the +1 sigma variation, or *None* when the
      calculation failed.
    - ``poi2_m1``: The poi2 value corresponding to the -1 sigma variation, or *None* when the
      calculation failed.
    - ``poi2_p2``: The poi2 value corresponding to the +2 sigma variation, or *None* when the
      calculation failed.
    - ``poi2_m2``: The poi2 value corresponding to the -2 sigma variation, or *None* when the
      calculation failed.
    - ``num1_min``: A Number instance representing the poi1 minimum and its 1 sigma uncertainty.
    - ``num2_min``: A Number instance representing the poi2 minimum and its 1 sigma uncertainty.
    """
    # ensure we are dealing with arrays
    poi1_values = np.array(poi1_values)
    poi2_values = np.array(poi2_values)
    dnll2_values = np.array(dnll2_values)

    # store ranges
    poi1_values_min = poi1_values.min()
    poi1_values_max = poi1_values.max()
    poi2_values_min = poi2_values.min()
    poi2_values_max = poi2_values.max()

970
    # remove values where dnll2 is nan
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
    mask = ~np.isnan(dnll2_values)
    poi1_values = poi1_values[mask]
    poi2_values = poi2_values[mask]
    dnll2_values = dnll2_values[mask]

    # obtain an interpolation function
    # interp = scipy.interpolate.interp2d(poi1_values, poi2_values, dnll2_values)
    # interp = scipy.interpolate.SmoothBivariateSpline(poi1_values, poi2_values, dnll2_values,
    #     kx=2, ky=2)
    coords = np.stack([poi1_values, poi2_values], axis=1)
    interp = scipy.interpolate.CloughTocher2DInterpolator(coords, dnll2_values)

    # get the minima
    if poi1_min is None or poi2_min is None:
        objective = lambda x: interp(*x) ** 2.0
        bounds1 = (poi1_values_min + 1e-4, poi1_values_max - 1e-4)
        bounds2 = (poi2_values_min + 1e-4, poi2_values_max - 1e-4)
        res = scipy.optimize.minimize(objective, [1.0, 1.0], tol=1e-7, bounds=[bounds1, bounds2])
        if res.status != 0:
            raise Exception("could not find minimum of nll2 interpolation: {}".format(res.message))
        poi1_min = res.x[0]
        poi2_min = res.x[1]

    # helper to get the outermost intersection of the nll curve with a certain value
    def get_intersections(v, n_poi):
        if n_poi == 1:
            poi_values_min, poi_values_max = poi1_values_min, poi1_values_max
            poi_min = poi1_min
999
            _interp = lambda x: interp(x, poi2_min)
1000
        else:
For faster browsing, not all history is shown. View entire blame