-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathdo_plots.py
More file actions
1161 lines (991 loc) · 40.4 KB
/
Copy pathdo_plots.py
File metadata and controls
1161 lines (991 loc) · 40.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
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
970
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
999
1000
#!/usr/bin/env python
'''
Create plots out of the histograms produced in previous stages
'''
import sys
import os
import os.path
import ntpath
import importlib
import copy
import re
import logging
from typing import Any
import ROOT # type: ignore
from utils import random_string, boolean_of
ROOT.gROOT.SetBatch(True)
ROOT.gStyle.SetOptStat(0)
ROOT.gStyle.SetOptTitle(0)
LOGGER = logging.getLogger('FCCAnalyses.plot')
# _____________________________________________________________________________
def removekey(d: dict, key: str) -> dict:
'''
Remove dictionary element.
'''
r = dict(d)
del r[key]
return r
def sorted_dict_values(dic: dict) -> list:
''''
Sort values in the dictionary.
'''
keys = sorted(dic)
return [dic[key] for key in keys]
def formatStatUncHist(hists, name, hstyle=3254):
hist_tot = hists[0].Clone(name + "_unc")
for h in hists[1:]:
hist_tot.Add(h)
hist_tot.SetFillColor(ROOT.kBlack)
hist_tot.SetMarkerSize(0)
hist_tot.SetLineWidth(0)
hist_tot.SetFillStyle(hstyle)
return hist_tot
# _____________________________________________________________________________
def get_minmax_range(histos: list[Any],
xmin: float,
xmax: float):
'''
Find min and max values for the y-axis among the provided histograms in a
specified x-axis range.
'''
if len(histos) == 0:
LOGGER.warning('Histograms not provided!')
return 1e-5, 1
hist_name = 'hist_tot_' + random_string(12)
hist_tot = histos[0].Clone(hist_name)
for hist in histos[1:]:
hist_tot.Add(hist)
vals = []
for i in range(0, hist_tot.GetNbinsX()+1):
if hist_tot.GetBinLowEdge(i) > xmin or \
hist_tot.GetBinLowEdge(i+1) < xmax:
if hist_tot.GetBinContent(i) != 0:
vals.append(hist_tot.GetBinContent(i))
if len(vals) == 0:
return 1e-5, 1
return min(vals), max(vals)
# _____________________________________________________________________________
def determine_lumi_scaling(config: dict[str, Any],
infile: ROOT.TFile,
initial_scale: float = 1.0) -> float:
'''
Determine whether to (re)scale histograms in the file to luminosity.
'''
scale: float = initial_scale
# Check if histograms were already scaled to lumi
try:
scaled: bool = infile.scaled.GetVal()
except AttributeError:
LOGGER.error('Input file does not contain scaling '
'information!\n %s\nAborting...', infile.GetName())
sys.exit(3)
if scaled:
try:
int_lumi_in_file: float = infile.intLumi.GetVal()
except AttributeError:
LOGGER.error('Can not load integrated luminosity '
'value from the input file!\n %s\n'
'Aborting...', infile.GetName())
if config['int-lumi'] != int_lumi_in_file:
LOGGER.warning(
'Histograms are already scaled to different '
'luminosity value!\n'
'Luminosity in the input file is %s pb-1 and '
'luminosity requested in plots script is %s pb-1.',
int_lumi_in_file, config['int-lumi'])
if config['do-scale']:
LOGGER.warning(
'Rescaling from %s pb-1 to %s pb-1...',
int_lumi_in_file, config['int-lumi'])
scale *= config['int-lumi'] / int_lumi_in_file
else:
if config['do-scale']:
scale = scale * config['int-lumi']
return scale
# _____________________________________________________________________________
def load_hists(var: str,
label: str,
sel: str,
config: dict[str, Any],
rebin: int) -> tuple[dict[str, Any], dict[str, Any]]:
'''
Load all histograms needed for the plot
'''
try:
signal = config['plots'][label]['signal']
except KeyError:
signal = {}
try:
backgrounds = config['plots'][label]['backgrounds']
except KeyError:
backgrounds = {}
hsignal: dict[str, Any] = {}
for s in signal:
hsignal[s] = []
for filepathstem in signal[s]:
infilepath = config['input-dir'] + filepathstem + '_' + sel + \
'_histo.root'
if not os.path.isfile(infilepath):
LOGGER.info('File "%s" not found!\nSkipping it...', infilepath)
continue
with ROOT.TFile(infilepath, 'READ') as infile:
hist = copy.deepcopy(infile.Get(var))
hist.SetDirectory(0)
scale = determine_lumi_scaling(config,
infile,
config['scale-sig'])
hist.Scale(scale)
hist.Rebin(rebin)
if len(hsignal[s]) == 0:
hsignal[s].append(hist)
else:
hist.Add(hsignal[s][0])
hsignal[s][0] = hist
hbackgrounds: dict[str, Any] = {}
for b in backgrounds:
hbackgrounds[b] = []
for filepathstem in backgrounds[b]:
infilepath = config['input-dir'] + filepathstem + '_' + sel + \
'_histo.root'
if not os.path.isfile(infilepath):
LOGGER.info('File "%s" not found!\nSkipping it...', infilepath)
continue
with ROOT.TFile(infilepath) as infile:
hist = copy.deepcopy(infile.Get(var))
hist.SetDirectory(0)
scale = determine_lumi_scaling(config,
infile,
config['scale-bkg'])
hist.Scale(scale)
hist.Rebin(rebin)
if len(hbackgrounds[b]) == 0:
hbackgrounds[b].append(hist)
else:
hist.Add(hbackgrounds[b][0])
hbackgrounds[b][0] = hist
for s in hsignal:
if len(hsignal[s]) == 0:
hsignal = removekey(hsignal, s)
for b in hbackgrounds:
if len(hbackgrounds[b]) == 0:
hbackgrounds = removekey(hbackgrounds, b)
return hsignal, hbackgrounds
# _____________________________________________________________________________
def mapHistosFromHistmaker(config: dict[str, Any],
hist_name: str,
param,
hist_cfg):
rebin = hist_cfg['rebin'] if 'rebin' in hist_cfg else 1
LOGGER.info('Get histograms for %s', hist_name)
signal = param.procs['signal']
backgrounds = param.procs['backgrounds']
scaleSig = hist_cfg['scaleSig'] if 'scaleSig' in hist_cfg else 1
hsignal: dict[str, Any] = {}
for s in signal:
hsignal[s] = []
for f in signal[s]:
fin = f"{param.inputDir}/{f}.root"
if not os.path.isfile(fin):
LOGGER.info('File "%s" not found!\nSkipping it...', fin)
continue
with ROOT.TFile(fin) as tf:
h = tf.Get(hist_name)
hh = copy.deepcopy(h)
hh.SetDirectory(0)
LOGGER.info('ScaleSig: %g', scaleSig)
hh.Scale(param.intLumi*scaleSig)
hh.Rebin(rebin)
if len(hsignal[s]) == 0:
hsignal[s].append(hh)
else:
hh.Add(hsignal[s][0])
hsignal[s][0] = hh
hbackgrounds: dict[str, Any] = {}
for b in backgrounds:
hbackgrounds[b] = []
for f in backgrounds[b]:
fin = f"{param.inputDir}/{f}.root"
if not os.path.isfile(fin):
LOGGER.info('File "%s" not found!\nSkipping it...', fin)
continue
with ROOT.TFile(fin) as tf:
h = tf.Get(hist_name)
hh = copy.deepcopy(h)
hh.SetDirectory(0)
hh.Scale(param.intLumi)
hh.Rebin(rebin)
if len(hbackgrounds[b]) == 0:
hbackgrounds[b].append(hh)
else:
hh.Add(hbackgrounds[b][0])
hbackgrounds[b][0] = hh
for s in hsignal:
if len(hsignal[s]) == 0:
hsignal = removekey(hsignal, s)
for b in hbackgrounds:
if len(hbackgrounds[b]) == 0:
hbackgrounds = removekey(hbackgrounds, b)
if not hsignal:
LOGGER.error('No signal input files found!\nAborting...')
sys.exit(3)
return hsignal, hbackgrounds
# _____________________________________________________________________________
def runPlots(config: dict[str, Any],
args,
var,
sel,
script_module,
hsignal,
hbackgrounds,
extralab):
# Below are settings for separate signal and background legends
if config['split-leg']:
legsize = 0.04 * (len(hsignal))
legsize2 = 0.04 * (len(hbackgrounds))
leg = ROOT.TLegend(0.15, 0.60 - legsize, 0.50, 0.62)
leg2 = ROOT.TLegend(0.60, 0.60 - legsize2, 0.88, 0.62)
if config['leg-position'][0] is not None and \
config['leg-position'][2] is not None:
leg.SetX1(config['leg-position'][0])
leg.SetX2((config['leg-position'][0] +
config['leg-position'][2]) / 2)
leg2.SetX2((config['leg-position'][0] +
config['leg-position'][2]) / 2)
leg2.SetX2(config['leg-position'][0])
if config['leg-position'][1] is not None:
leg.SetY1(config['leg-position'][1])
leg2.SetY1(config['leg-position'][1])
if config['leg-position'][3] is not None:
leg.SetY2(config['leg-position'][3])
leg2.SetY2(config['leg-position'][3])
leg2.SetFillColor(0)
leg2.SetFillStyle(0)
leg2.SetLineColor(0)
leg2.SetShadowColor(10)
leg2.SetTextSize(config['legend-text-size'])
leg2.SetTextFont(42)
else:
legsize = 0.04 * (len(hbackgrounds) + len(hsignal))
leg = ROOT.TLegend(0.68, 0.86 - legsize, 0.96, 0.88)
leg2 = None
if config['leg-position'][0] is not None:
leg.SetX1(config['leg-position'][0])
if config['leg-position'][1] is not None:
leg.SetY1(config['leg-position'][1])
if config['leg-position'][2] is not None:
leg.SetX2(config['leg-position'][2])
if config['leg-position'][3] is not None:
leg.SetY2(config['leg-position'][3])
leg.SetFillColor(0)
leg.SetFillStyle(0)
leg.SetLineColor(0)
leg.SetShadowColor(10)
leg.SetTextSize(config['legend-text-size'])
leg.SetTextFont(42)
for b in hbackgrounds:
if config['split-leg']:
leg2.AddEntry(hbackgrounds[b][0], script_module.legend[b], "f")
else:
leg.AddEntry(hbackgrounds[b][0], script_module.legend[b], "f")
for s in hsignal:
leg.AddEntry(hsignal[s][0], script_module.legend[s], "l")
yields = {}
for s in hsignal:
yields[s] = [script_module.legend[s],
hsignal[s][0].Integral(0, -1),
hsignal[s][0].GetEntries()]
for b in hbackgrounds:
yields[b] = [script_module.legend[b],
hbackgrounds[b][0].Integral(0, -1),
hbackgrounds[b][0].GetEntries()]
histos = []
colors = []
nsig = len(hsignal)
nbkg = len(hbackgrounds)
for sig in hsignal:
histos.append(hsignal[sig][0])
colors.append(script_module.colors[sig])
for bkg in hbackgrounds:
histos.append(hbackgrounds[bkg][0])
colors.append(script_module.colors[bkg])
lt = 'FCCAnalyses: FCC-hh Simulation (Delphes)'
rt = f'#sqrt{{s}} = {script_module.energy:.1f} TeV, ' \
f'{config["int-lumi-label"]}'
if 'ee' in script_module.collider:
lt = 'FCCAnalyses: FCC-ee Simulation (Delphes)'
rt = f'#sqrt{{s}} = {script_module.energy:.1f} GeV, ' \
f'{config["int-lumi-label"]}'
customLabel = ""
try:
customLabel = script_module.customLabel
except AttributeError:
LOGGER.debug('No custom label, using nothing...')
if 'AAAyields' in var:
plot_params = {'xaxis': 'lin',
'yaxis': 'lin',
'stack-sig': 'stack'}
draw_plot(config, plot_params,
var,
'events', leg, lt, rt,
script_module.formats,
script_module.outdir + "/" + sel, histos,
colors, script_module.ana_tex, extralab,
customLabel, nsig, nbkg, leg2, yields,
config['plot-stat-unc'])
return
for sig_stacking in config['stack-sig']:
plot_params = {'stack-sig': sig_stacking}
for xaxis_scaling in config['x-axis-scale-types']:
plot_params_x = {**plot_params, 'xaxis': xaxis_scaling}
for yaxis_scaling in config['y-axis-scale-types']:
plot_params_x_y = {**plot_params_x, 'yaxis': yaxis_scaling}
plot_name = var
if len(config['stack-sig']) > 1:
plot_name += '_' + sig_stacking
if len(config['x-axis-scale-types']) > 1:
plot_name += '_' + xaxis_scaling + 'x'
if len(config['y-axis-scale-types']) > 1:
plot_name += '_' + yaxis_scaling + 'y'
draw_plot(config, plot_params_x_y,
plot_name,
'events', leg, lt, rt,
script_module.formats,
script_module.outdir + "/" + sel,
histos, colors, script_module.ana_tex,
extralab, customLabel, nsig, nbkg, leg2,
yields, config['plot-stat-unc'])
# _____________________________________________________________________________
def runPlotsHistmaker(config: dict[str, Any],
args,
hist_name: str,
param,
hist_cfg):
output = hist_cfg['output']
hsignal, hbackgrounds = mapHistosFromHistmaker(config,
hist_name,
param,
hist_cfg)
if hasattr(param, "splitLeg"):
splitLeg = param.splitLeg
else:
splitLeg = False
if hasattr(param, "plotStatUnc"):
plotStatUnc = param.plotStatUnc
else:
plotStatUnc = False
# Below are settings for separate signal and background legends
if splitLeg:
legsize = 0.04 * (len(hsignal))
legsize2 = 0.04 * (len(hbackgrounds))
legCoord = [0.15, 0.60 - legsize, 0.50, 0.62]
leg2 = ROOT.TLegend(0.60, 0.60 - legsize2, 0.88, 0.62)
leg2.SetFillColor(0)
leg2.SetFillStyle(0)
leg2.SetLineColor(0)
leg2.SetShadowColor(10)
leg2.SetTextSize(config['legend-text-size'])
leg2.SetTextFont(42)
else:
legsize = 0.04*(len(hbackgrounds)+len(hsignal))
legCoord = [0.68, 0.86 - legsize, 0.96, 0.88]
try:
legCoord = param.legendCoord
except AttributeError:
LOGGER.debug('No legCoord, using default one...')
legCoord = [0.68, 0.86-legsize, 0.96, 0.88]
leg2 = None
leg = ROOT.TLegend(
(config['leg-position'][0]
if config['leg-position'][0] is not None
else legCoord[0]),
(config['leg-position'][1]
if config['leg-position'][1] is not None
else legCoord[1]),
(config['leg-position'][2]
if config['leg-position'][2] is not None
else legCoord[2]),
(config['leg-position'][3]
if config['leg-position'][3] is not None
else legCoord[3])
)
leg.SetFillColor(0)
leg.SetFillStyle(0)
leg.SetLineColor(0)
leg.SetShadowColor(10)
leg.SetTextSize(config['legend-text-size'])
leg.SetTextFont(42)
for b in hbackgrounds:
if splitLeg:
leg2.AddEntry(hbackgrounds[b][0], param.legend[b], "f")
else:
leg.AddEntry(hbackgrounds[b][0], param.legend[b], "f")
for s in hsignal:
leg.AddEntry(hsignal[s][0], param.legend[s], "l")
yields = {}
for s in hsignal:
yields[s] = [param.legend[s],
hsignal[s][0].Integral(0, -1),
hsignal[s][0].GetEntries()]
for b in hbackgrounds:
yields[b] = [param.legend[b],
hbackgrounds[b][0].Integral(0, -1),
hbackgrounds[b][0].GetEntries()]
histos = []
colors = []
nsig = len(hsignal)
nbkg = len(hbackgrounds)
for s in hsignal:
histos.append(hsignal[s][0])
colors.append(param.colors[s])
for b in hbackgrounds:
histos.append(hbackgrounds[b][0])
colors.append(param.colors[b])
xtitle = hist_cfg['xtitle'] if 'xtitle' in hist_cfg else ""
ytitle = hist_cfg['ytitle'] if 'ytitle' in hist_cfg else "Events"
xmin = hist_cfg['xmin'] if 'xmin' in hist_cfg else -1
xmax = hist_cfg['xmax'] if 'xmax' in hist_cfg else -1
ymin = hist_cfg['ymin'] if 'ymin' in hist_cfg else -1
ymax = hist_cfg['ymax'] if 'ymax' in hist_cfg else -1
stack = boolean_of(hist_cfg['stack'],"histogram stack") if 'stack' in hist_cfg else False
logx = boolean_of(hist_cfg['logx'], hist_cfg['xtitle']) if 'logx' in hist_cfg else False
logy = boolean_of(hist_cfg['logy'], hist_cfg['ytitle']) if 'logy' in hist_cfg else False
extralab = hist_cfg['extralab'] if 'extralab' in hist_cfg else ""
intLumi = f'L = {param.intLumi / 1e+06:.0f} ab^{{-1}}'
if hasattr(param, "intLumiLabel"):
intLumi = getattr(param, "intLumiLabel")
lt = 'FCCAnalyses: FCC-hh Simulation (Delphes)'
rt = f'#sqrt{{s}} = {param.energy:.1f} TeV, L = {intLumi}'
if 'ee' in param.collider:
lt = 'FCCAnalyses: FCC-ee Simulation (Delphes)'
rt = f'#sqrt{{s}} = {param.energy:.1f} GeV, {intLumi}'
customLabel = ""
try:
customLabel = param.customLabel
except AttributeError:
LOGGER.debug('No customLabel, using nothing...')
plot_params: dict[str, Any] = {}
plot_params['stack-sig'] = 'stack' if stack else 'nostack'
plot_params['xaxis'] = 'log' if logx else 'lin'
plot_params['yaxis'] = 'log' if logy else 'lin'
draw_plot(config, plot_params,
output, ytitle,
leg, lt, rt,
param.formats,
param.outdir,
histos, colors, param.ana_tex,
extralab, customLabel, nsig, nbkg, leg2,
yields, plotStatUnc,
xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
xtitle=xtitle)
if 'dumpTable' in hist_cfg and hist_cfg['dumpTable']:
if type(xtitle) != list:
LOGGER.error('Can only dump a table of yields for cutflow plots.')
quit()
procs = list(hsignal.keys()) + list(hbackgrounds.keys())
hists = hsignal | hbackgrounds
scaleSig = hist_cfg['scaleSig'] if 'scaleSig' in hist_cfg else 1.
hists[procs[0]][0].Scale(1./scaleSig) # undo signal scaling
cuts = xtitle
out_orig = sys.stdout
with open(f"{param.outdir}/{output}.txt", 'w') as f:
sys.stdout = f
formatted_row = '{:<10} {:<15} ' + ' '.join(['{:<15}']*len(procs))
print(formatted_row.format(*(["Cut", "Significance"]+procs)))
print(formatted_row.format(*(["----------"]+["-------------"]*(len(procs)+1))))
for i,cut in enumerate(cuts):
s = hists[procs[0]][0].GetBinContent(i+1)
s_plus_b = sum([hists[p][0].GetBinContent(i+1) for p in procs])
significance = s/(s_plus_b**0.5) if s_plus_b > 0 else 0
row = ["Cut %d"%i, "%.3f"%significance]
for j,proc in enumerate(procs):
yield_ = hists[proc][0].GetBinContent(i+1)
row.append("%.4e" % (yield_))
print(formatted_row.format(*row))
sys.stdout = out_orig
# _____________________________________________________________________________
def draw_plot(config: dict[str, Any],
plot_params: dict[str, Any],
plot_name: str, ylabel, legend, leftText, rightText, formats,
out_dir, histos, colors, ana_tex, extralab,
customLabel, nsig, nbkg, legend2=None, yields=None,
plotStatUnc=False, xmin=-1, xmax=-1, ymin=-1, ymax=-1,
xtitle=""):
'''
Do the actual drawing of the plot onto the ROOT's canvas.
'''
if len(histos) == 0:
LOGGER.warning('No histograms provided!\n - plot name: "%s"\n'
'Continuing...', plot_name)
return
# Setup canvas
canvas = ROOT.TCanvas(plot_name, plot_name, 800, 800)
if plot_params['xaxis'] == 'lin':
canvas.SetLogx(0)
else:
canvas.SetLogx(1)
if plot_params['yaxis'] == 'lin':
canvas.SetLogy(0)
else:
canvas.SetLogy(1)
canvas.SetTicks(1, 1)
canvas.SetLeftMargin(0.14)
canvas.SetRightMargin(0.08)
# Adjust y-axis label
hist0_name = str(histos[0].GetXaxis().GetTitle())
if any(unit in hist0_name for unit in ['GeV', 'TeV']):
unit = 'GeV'
if 'TeV' in str(histos[0].GetXaxis().GetTitle()):
unit = 'TeV'
bwidth = histos[0].GetBinWidth(1)
if bwidth.is_integer():
ylabel += f' / {bwidth} {unit}'
else:
ylabel += f' / {bwidth:.2f} {unit}'
nbins = 1 if not isinstance(xtitle, list) else len(xtitle)
h_dummy = ROOT.TH1D("h_dummy", "", nbins, 0, nbins)
if nbins == 1:
h_dummy.GetXaxis().SetTitle(
histos[0].GetXaxis().GetTitle() if xtitle == "" else xtitle)
h_dummy.GetYaxis().SetTitleOffset(1.95)
h_dummy.GetXaxis().SetTitleOffset(
1.2*h_dummy.GetXaxis().GetTitleOffset())
else: # for cutflow plots
for i, label in enumerate(xtitle):
h_dummy.GetXaxis().SetBinLabel(i+1, label)
h_dummy.GetXaxis().LabelsOption("u")
h_dummy.GetXaxis().SetLabelSize(1.1*h_dummy.GetXaxis().GetLabelSize())
h_dummy.GetXaxis().SetLabelOffset(
1.5*h_dummy.GetXaxis().GetLabelOffset())
h_dummy.GetYaxis().SetTitle(ylabel)
# define stacked histo
hStack = ROOT.THStack("hstack", "")
hStackBkg = ROOT.THStack("hstackbkg", "")
hStackSig = ROOT.THStack("hstacksig", "")
BgMCHistYieldsDic = {}
# first plot backgrounds (sorted by the yields)
for i in range(nsig, nsig+nbkg):
hist = histos[i]
hist.SetLineWidth(1)
hist.SetLineColor(ROOT.kBlack)
hist.SetFillColor(colors[i])
if hist.Integral() > 0:
BgMCHistYieldsDic[hist.Integral()] = hist
else:
BgMCHistYieldsDic[-1*nbkg] = hist
# sort stack by yields (smallest to largest)
BgMCHistYieldsSorted = sorted_dict_values(BgMCHistYieldsDic)
for hist in BgMCHistYieldsSorted:
hStack.Add(hist)
hStackBkg.Add(hist)
# add the signal histograms
for i in range(nsig):
hist = histos[i]
hist.SetLineWidth(3)
hist.SetLineColor(colors[i])
hStack.Add(hist)
hStackSig.Add(hist)
if xmin != -1 and xmax != -1:
h_dummy.GetXaxis().SetLimits(xmin, xmax)
h_dummy.Draw("HIST")
if plot_params['stack-sig'] == 'stack':
hStack.Draw("HIST SAME")
if plotStatUnc:
# sig+bkg uncertainty
hUnc_sig_bkg = formatStatUncHist(hStack.GetHists(), "sig_bkg")
hUnc_sig_bkg.Draw("E2 SAME")
else:
hStackBkg.Draw("HIST SAME")
hStackSig.Draw("HIST SAME NOSTACK")
if plotStatUnc:
# bkg-only uncertainty
if hStackBkg.GetNhists() != 0:
hUnc_bkg = formatStatUncHist(hStackBkg.GetHists(), "bkg_only")
hUnc_bkg.Draw("E2 SAME")
for sHist in hStackSig.GetHists():
# sigs uncertainty
hUnc_sig = formatStatUncHist([sHist], "sig", 3245)
hUnc_sig.Draw("E2 SAME")
# x limits
if xmin == -1:
xmin = hStack.GetStack().Last().GetBinLowEdge(1)
if xmax == -1:
xmax = hStack.GetStack().Last().GetBinLowEdge(
hStack.GetStack().Last().GetNbinsX() + 1
)
if plot_params['xaxis'] == 'log':
if xmin <= 0.:
LOGGER.error('Log scale for x-axis can\'t start at: %g\n'
' - plot name: %s\nContinuing...', xmin, plot_name)
return
if xmax <= 0.:
LOGGER.error('Log scale for x-axis can\'t end at: %g\n'
' - plot name: %s\nContinuing...', xmax, plot_name)
return
h_dummy.GetXaxis().SetLimits(xmin, xmax)
# y limits
if plot_params['stack-sig'] == 'stack':
ymin_, ymax_ = get_minmax_range(hStack.GetHists(), xmin, xmax)
else:
if hStackSig.GetNhists() != 0 and hStackBkg.GetNhists() != 0:
ymin_sig, ymax_sig = get_minmax_range(hStackSig.GetHists(),
xmin, xmax)
ymin_bkg, ymax_bkg = get_minmax_range(hStackBkg.GetHists(),
xmin, xmax)
ymin_ = min(ymin_sig, ymin_bkg)
ymax_ = max(ymax_sig, ymax_bkg)
elif hStackSig.GetNhists() == 0:
ymin_, ymax_ = get_minmax_range(hStackBkg.GetHists(), xmin, xmax)
elif hStackBkg.GetNhists() == 0:
ymin_, ymax_ = get_minmax_range(hStackSig.GetHists(), xmin, xmax)
if ymin == -1:
ymin = ymin_*0.1 if plot_params['yaxis'] == 'log' else 0
if ymax == -1:
ymax = ymax_*1000. if plot_params['yaxis'] == 'log' else 1.4*ymax_
if plot_params['yaxis'] == 'log':
if ymin <= 0.:
LOGGER.error('Log scale for y-axis can\'t start at: %g\n'
' - plot name: %s\nContinuing...', ymin, plot_name)
return
if ymax <= 0.:
LOGGER.error('Log scale for y-axis can\'t end at: %g\n'
' - plot name: %s\nContinuing...', ymax, plot_name)
return
h_dummy.SetMaximum(ymax)
h_dummy.SetMinimum(ymin)
legend.Draw()
if legend2 is not None:
legend2.Draw()
latex = ROOT.TLatex()
latex.SetNDC()
latex.SetTextAlign(31)
latex.SetTextSize(0.04)
text = '#it{' + leftText + '}'
latex.DrawLatex(0.90, 0.94, text)
text = '#it{'+customLabel+'}'
latex.SetTextAlign(12)
latex.SetNDC(ROOT.kTRUE)
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.85, text)
rightText = re.split(",", rightText)
text = '#bf{#it{' + rightText[0] + '}}'
latex.SetTextAlign(12)
latex.SetNDC(ROOT.kTRUE)
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.81, text)
rightText[1] = rightText[1].replace(" ", "")
text = '#bf{#it{' + rightText[1] + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.18, 0.76, text)
text = '#bf{#it{' + ana_tex + '}}'
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.71, text)
text = '#bf{#it{' + extralab + '}}'
latex.SetTextSize(0.025)
latex.DrawLatex(0.18, 0.66, text)
if config['scale-sig'] != 1.0:
text = '#bf{#it{Signal Scaling = ' + f'{config["scale-sig"]:.3g}' + \
'}}'
latex.SetTextSize(0.025)
latex.DrawLatex(0.18, 0.63, text)
if config['scale-bkg'] != 1.0:
text = '#bf{#it{Background Scaling = ' + \
f'{config["scale-bkg"]:.3g}' + '}}'
latex.SetTextSize(0.025)
latex.DrawLatex(0.18, 0.63, text)
canvas.RedrawAxis()
canvas.GetFrame().SetBorderSize(12)
canvas.Modified()
canvas.Update()
if 'AAAyields' in plot_name:
dummyh = ROOT.TH1F("", "", 1, 0, 1)
dummyh.SetStats(0)
dummyh.GetXaxis().SetLabelOffset(999)
dummyh.GetXaxis().SetLabelSize(0)
dummyh.GetYaxis().SetLabelOffset(999)
dummyh.GetYaxis().SetLabelSize(0)
dummyh.Draw("AH")
legend.Draw()
latex.SetNDC()
latex.SetTextAlign(31)
latex.SetTextSize(0.04)
text = '#it{' + leftText + '}'
latex.DrawLatex(0.90, 0.92, text)
text = '#bf{#it{' + rightText[0] + '}}'
latex.SetTextAlign(12)
latex.SetNDC(ROOT.kTRUE)
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.83, text)
text = '#bf{#it{' + rightText[1] + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.18, 0.78, text)
text = '#bf{#it{' + ana_tex + '}}'
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.73, text)
text = '#bf{#it{' + extralab + '}}'
latex.SetTextSize(0.025)
latex.DrawLatex(0.18, 0.68, text)
text = '#bf{#it{Signal Scaling = ' + f'{config["scale-sig"]:.3g}' + \
'}}'
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.57, text)
text = '#bf{#it{Background Scaling = ' + \
f'{config["scale-bkg"]:.3g}' + '}}'
latex.SetTextSize(0.04)
latex.DrawLatex(0.18, 0.52, text)
dy = 0
text = '#bf{#it{' + 'Process' + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.18, 0.45, text)
text = '#bf{#it{' + 'Yields' + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.5, 0.45, text)
text = '#bf{#it{' + 'Raw MC' + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.75, 0.45, text)
for y in yields:
text = '#bf{#it{' + yields[y][0] + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.18, 0.4-dy*0.05, text)
stry = str(yields[y][1])
stry = stry.split('.', maxsplit=1)[0]
text = '#bf{#it{' + stry + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.5, 0.4-dy*0.05, text)
stry = str(yields[y][2])
stry = stry.split('.', maxsplit=1)[0]
text = '#bf{#it{' + stry + '}}'
latex.SetTextSize(0.035)
latex.DrawLatex(0.75, 0.4-dy*0.05, text)
dy += 1
save_canvas(canvas, plot_name, formats, out_dir)
# _____________________________________________________________________________
def save_canvas(canvas, plot_name: str, formats: list[str], out_dir: str):
'''
Saving canvas in multiple formats.
'''
if not formats:
LOGGER.error('No output formats specified!\nAborting...')
sys.exit(3)
if not os.path.exists(out_dir):
os.system("mkdir -p " + out_dir)
for ext in formats:
out_path = os.path.join(out_dir, plot_name) + "." + ext
canvas.SaveAs(out_path)
LOGGER.debug(out_path)
# _____________________________________________________________________________
def run(args):
'''
Run over all the plots.
'''
ROOT.gROOT.SetBatch(True)
ROOT.gErrorIgnoreLevel = ROOT.kWarning
module_path = os.path.abspath(args.script_path)
module_dir = os.path.dirname(module_path)
base_name = os.path.splitext(ntpath.basename(args.script_path))[0]
# Load plot script as module
sys.path.insert(0, module_dir)
script_module = importlib.import_module(base_name)
# Merge script and command line arguments into one configuration object
# Also check the script attributes
config: dict[str, Any] = {}
# Input directory
config['input-dir'] = os.getcwd()
if hasattr(script_module, 'indir'):
config['input-dir'] = script_module.indir
if hasattr(script_module, 'inputDir'):
config['input-dir'] = script_module.inputDir
if args.input_dir is not None:
config['input-dir'] = args.input_dir
LOGGER.info('Input directory: %s', config['input-dir'])
# Output directory
config['output-dir'] = os.getcwd()
if hasattr(script_module, 'outdir'):
config['output-dir'] = script_module.outdir
if hasattr(script_module, 'outputDir'):
config['output-dir'] = script_module.outputDir
if args.output_dir is not None:
config['output-dir'] = args.output_dir
LOGGER.info('Output directory: %s', config['output-dir'])
# Output file types
config['output-file-types'] = ['png', 'pdf']
if hasattr(script_module, 'formats'):
config['output-file-types'] = script_module.formats
msg = 'Output file types to be used: [' + \
', '.join(config['output-file-types']) + ']'
LOGGER.info(msg)
# Integrated luminosity
config['int-lumi'] = 1.
if hasattr(script_module, 'intLumi'):
config['int-lumi'] = script_module.intLumi
else:
LOGGER.debug('No integrated luminosity provided, using 1.0 pb-1.')
LOGGER.info('Integrated luminosity: %g pb-1', config['int-lumi'])
# Whether to scale histograms to luminosity
config['do-scale'] = 1.0
if hasattr(script_module, 'doScale'):
config['do-scale'] = script_module.doScale
else:
LOGGER.debug('No scaling to luminosity requested, scaling won\'t be '
'done.')
config['do-scale'] = False
if config['do-scale']:
LOGGER.info('Histograms will be scaled to luminosity.')
# Scale factor to apply to all signal histograms
config['scale-sig'] = 1.0
if hasattr(script_module, 'scaleSig'):
config['scale-sig'] = script_module.scaleSig
else:
LOGGER.debug('No scale factor for signal provided, using 1.0.')
LOGGER.info('Scale factor for signal: %g', config['scale-sig'])
# Scale factor to apply to all background histograms
config['scale-bkg'] = 1.0
if hasattr(script_module, 'scaleBkg'):
config['scale-bkg'] = script_module.scaleBkg
else:
LOGGER.debug('No scale factor for background provided, using 1.0.')
LOGGER.info('Scale factor for background: %g', config['scale-sig'])
# Stacking of the signal histograms
config['stack-sig'] = ['stack']
if hasattr(script_module, 'stacksig'):