-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcfm2text.py
executable file
·1129 lines (984 loc) · 34.1 KB
/
mcfm2text.py
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 python3
from pathlib import Path
from functools import reduce
from operator import add
from math import sqrt
import argparse
import numbers
import re
############################################################
#### Main Methods ####
############################################################
def histo_mk(args):
inputs = [Path(path) for path in args.input if Path(path).exists()]
ninputs = len(inputs)
# get mcfm output in list
mcfm_histos = [read_hists(mcfm_in) for mcfm_in in inputs]
try:
# get nested list of all histograms
all_histos = map(mcfm2hist, mcfm_histos)
# transpose for reduce call
all_histos_tp = list(map(lambda *sl : list(sl), *all_histos))
# Add together histograms with like observables
histos = [reduce(add, hist) for hist in all_histos_tp]
except TypeError:
print("No valid inputs found")
exit()
return histos
def scale_var(args):
central = [Path(path) for path in args.central_scale
if Path(path).exists()]
variation = [Path(path) for path in args.variation_scales
if Path(path).exists()]
try:
cen_hist = [read_mcfmhisto(hist_in) for hist_in in central]
var_hist = [read_mcfmhisto(hist_in) for hist_in in variation]
# match up like observables
all_hist = [ [cen] + [var for var in var_hist if var.obs == cen.obs]
for cen in cen_hist ]
except:
# pass
print("Something nasty happened processing histograms for the scale variations")
exit()
# central scale
cen_scale = [ cen.xsecs for cen in cen_hist]
# extract min and max values
min_scale = [ [min(xsec_tup) for xsec_tup in
zip(*[hist.xsecs for hist in obs])]
for obs in all_hist ]
max_scale = [ [max(xsec_tup) for xsec_tup in
zip(*[hist.xsecs for hist in obs])]
for obs in all_hist ]
# if we want symmetric error bands need to compute the maximum that the
# envelope differs from the central value.
if args.symmetric:
# calculate the absolute error of the symmetric bands
sym_scale = [ [max(abs(mxb - cb), abs(mnb - cb)) for cb, mnb, mxb
in zip(cen, mini, maxi)]
for cen, mini, maxi in zip(cen_scale,
min_scale, max_scale) ]
# reconstruct min_scale with the symmetric error band
min_scale = [ [cb - mnb for cb, mnb in zip(cen, sym)]
for cen, sym in zip(cen_scale, sym_scale)]
max_scale = [ [cb + mxb for cb, mxb in zip(cen, sym)]
for cen, sym in zip(cen_scale, sym_scale)]
result = [scalesymhisto(cen.obs, cen.nbins, cen.xmin, cen.xmax,
cen.bins, cen.xsecs, err)
for cen, err in zip(cen_hist, sym_scale)]
else:
result = [scalehisto(cen.obs, cen.nbins, cen.xmin, cen.xmax,
cen.bins, cen.xsecs, mini, maxi)
for cen, mini, maxi in zip(cen_hist, min_scale, max_scale)]
return result
def match_nlo(args):
try:
# inputs
lo_in = [Path(path) for path in args.lo if Path(path).exists()]
virt_in = [Path(path) for path in args.virt if Path(path).exists()]
real_in = [Path(path) for path in args.real if Path(path).exists()]
# number of inputs for consistency checks
num_lo_in = len(lo_in)
num_virt_in = len(virt_in)
num_real_in = len(real_in)
except TypeError:
print("there need to be inputs for lo, virt and real")
exit()
except:
print("something weird and unexpected happened")
exit()
if num_lo_in == num_virt_in == num_real_in:
# histograms from inputs
lo_histos = [read_mcfmhisto(hist_in) for hist_in in lo_in]
virt_histos = [read_mcfmhisto(hist_in) for hist_in in virt_in]
real_histos = [read_mcfmhisto(hist_in) for hist_in in real_in]
# derived histograms
nlo_histos = [lo + virt + real for lo, virt, real
in zip(lo_histos, virt_histos, real_histos)]
else:
print("Unequal numbers of inputs")
exit()
return nlo_histos
def match_nlo_nll(args):
try:
# inputs
lo_in = [Path(path) for path in args.lo if Path(path).exists()]
virt_in = [Path(path) for path in args.virt if Path(path).exists()]
real_in = [Path(path) for path in args.real if Path(path).exists()]
nll_in = [Path(path) for path in args.nll if Path(path).exists()]
nllexpd_in = [Path(path) for path in args.nllexpd
if Path(path).exists()]
# number of inputs for consistency checks
num_lo_in = len(lo_in)
num_virt_in = len(virt_in)
num_real_in = len(real_in)
num_nll_in = len(nll_in)
num_nllexpd_in = len(nllexpd_in)
except TypeError:
print("there need to be inputs for lo, virt and real")
exit()
except:
print("something weird and unexpected happened")
exit()
if num_lo_in == num_virt_in == num_real_in == num_nll_in == num_nllexpd_in:
# histograms from inputs
lo_histos = [read_mcfmhisto(hist_in) for hist_in in lo_in]
virt_histos = [read_mcfmhisto(hist_in) for hist_in in virt_in]
real_histos = [read_mcfmhisto(hist_in) for hist_in in real_in]
nll_histos = [read_mcfmhisto(hist_in) for hist_in in nll_in]
nll1_histos = [read_mcfmhisto(hist_in) for hist_in in nllexpd_in]
# derived histograms
nlo1_histos = [virt + real for virt, real
in zip(virt_histos, real_histos)]
matched_histos = [nll*(1 + (nlo1-nll1)/lo) for lo, nlo1, nll, nll1 in
zip(lo_histos, nlo1_histos, nll_histos, nll1_histos)]
else:
print("Unequal numbers of inputs")
exit()
return matched_histos
def match_nlo_nnll(args):
# assume the default of scheme a throughout
try:
# inputs
lo_in = [Path(path) for path in args.lo
if Path(path).exists()]
virt_in = [Path(path) for path in args.virt
if Path(path).exists()]
real_in = [Path(path) for path in args.real
if Path(path).exists()]
nnll_in = [Path(path) for path in args.nnll
if Path(path).exists()]
nnllexpd_in = [Path(path) for path in args.nnllexpd
if Path(path).exists()]
lumi0_in = [Path(path) for path in args.lumi0
if Path(path).exists()]
lumi1_in = [Path(path) for path in args.lumi1
if Path(path).exists()]
if args.scheme == "b":
real_incl_in = [Path(path) for path in args.real_inclusive
if Path(path).exists]
# number of inputs for consistency checks
num_lo_in = len(lo_in)
num_virt_in = len(virt_in)
num_real_in = len(real_in)
num_nnll_in = len(nnll_in)
num_nnllexpd_in = len(nnllexpd_in)
num_lumi0_in = len(lumi0_in)
num_lumi1_in = len(lumi1_in)
if args.scheme == "b":
num_real_incl_in = len(real_incl_in)
except TypeError:
print("there need to be inputs for lo, virt and real")
exit()
except:
print("something weird and unexpected happened")
exit()
if (num_lo_in == num_virt_in == num_real_in == num_nnll_in ==
num_nnllexpd_in == num_lumi0_in == num_lumi1_in):
# histograms from inputs
lo_histos = [read_mcfmhisto(hist_in) for hist_in in lo_in]
virt_histos = [read_mcfmhisto(hist_in) for hist_in in virt_in]
real_histos = [read_mcfmhisto(hist_in) for hist_in in real_in]
nnll_histos = [read_mcfmhisto(hist_in) for hist_in in nnll_in]
nnll1_histos = [read_mcfmhisto(hist_in) for hist_in in nnllexpd_in]
lumi0_histos = [read_mcfmhisto(hist_in) for hist_in in lumi0_in]
lumi1_histos = [read_mcfmhisto(hist_in) for hist_in in lumi1_in]
# derived histograms
dlumi_histos = [lumi1/lumi0 for lumi0, lumi1
in zip(lumi0_histos, lumi1_histos)]
Sigma1_histos = [virt + real for virt, real
in zip(virt_histos, real_histos)]
matched_histos = [nnll*(1 + (Sigma1-nnll1)/(lo*(1+dlumi)))
for lo, Sigma1, nnll, nnll1, dlumi
in zip(lo_histos, Sigma1_histos, nnll_histos,
nnll1_histos, dlumi_histos)]
else:
print("Unequal numbers of inputs")
exit()
if args.scheme == "b":
if num_real_incl_in == num_lo_in:
real_incl_histos = [read_mcfmhisto(hist_in) for hist_in
in real_incl_in]
sigma1_histos = [virt + real for virt, real
in zip(virt_histos, real_incl_histos)]
matched_histos = [ma - sigma1*nnll/(lo*(1+dlumi))
for ma, lo, sigma1, nnll, dlumi in
zip(matched_histos, lo_histos, sigma1_histos,
nnll_histos, dlumi_histos)]
return matched_histos
def match_nnll_pure(args):
# assume the default of scheme a throughout
try:
# inputs
nnll_in = [Path(path) for path in args.nnll
if Path(path).exists()]
if args.scheme == "b":
lo_in = [Path(path) for path in args.lo
if Path(path).exists()]
virt_in = [Path(path) for path in args.virt
if Path(path).exists()]
real_incl_in = [Path(path) for path in args.real_inclusive
if Path(path).exists]
# number of inputs for consistency checks
num_nnll_in = len(nnll_in)
if args.scheme == "b":
num_lo_in = len(lo_in)
num_virt_in = len(virt_in)
num_real_incl_in = len(real_incl_in)
except TypeError:
print("there need to be inputs for nnll")
exit()
except:
print("something weird and unexpected happened")
exit()
# histograms from inputs
nnll_histos = [read_mcfmhisto(hist_in) for hist_in in nnll_in]
# derived histograms
matched_histos = [nnll for nnll in nnll_histos]
if args.scheme == "b":
if num_nnll_in == num_lo_in == num_virt_in == num_real_incl_in:
lo_histos = [read_mcfmhisto(hist_in) for hist_in in lo_in]
virt_histos = [read_mcfmhisto(hist_in) for hist_in in virt_in]
real_incl_histos = [read_mcfmhisto(hist_in) for hist_in
in real_incl_in]
sigma1_histos = [virt + real for virt, real
in zip(virt_histos, real_incl_histos)]
matched_histos = [ma - sigma1*nnll/lo
for ma, lo, sigma1, nnll in
zip(matched_histos, lo_histos, sigma1_histos,
nnll_histos)]
return matched_histos
def efficiency(args):
vetoed = [Path(path) for path in args.vetoed
if Path(path).exists()]
inclusive = [Path(path) for path in args.inclusive
if Path(path).exists()]
try:
veto_hist = [read_mcfmhisto(hist_in) for hist_in in vetoed]
incl_hist = [read_mcfmhisto(hist_in) for hist_in in inclusive]
# have a check to make sure observables align and there are
# equal numbers of inputs?
except:
pass
# efficiency
eff = [veto/incl for veto, incl in zip(veto_hist, incl_hist)]
return eff
def jet_veto_efficiency(args):
efficiency = [Path(path) for path in args.efficiency
if Path(path).exists()]
inclusive = [Path(path) for path in args.inclusive_xsec
if Path(path).exists()]
try:
eff_hist = [read_scalesymhisto(hist_in) for hist_in in efficiency]
incl_hist = [read_scalesymhisto(hist_in) for hist_in in inclusive]
# have a check to make sure observables align and there are
# equal numbers of inputs?
except:
pass
# JVE
jve = [incl*eff for incl, eff in zip(incl_hist, eff_hist)]
return jve
def delta(args):
pass
############################################################
#### Parser ####
############################################################
parser = argparse.ArgumentParser(description="""""")
subparsers = parser.add_subparsers(title="",
description="",
help="")
parser_data = subparsers.add_parser("histo_mk",
description="",
help="")
parser_data.add_argument(
'input',
metavar='input(s)',
type=str,
action='store',
nargs='+',
help="mcfm .dat file(s) to be processed")
parser_data.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_data.add_argument(
'--average',
action='store_true',
help="""Average over the input files, i.e. report histograms as sum over
inputs divide the number of inputs. One should use this if runs were
performed in parallel, but with the same setup""")
parser_data.set_defaults(func=histo_mk)
parser_scale = subparsers.add_parser(
"scale_var",
description="",
help="")
parser_scale.add_argument(
'-cs', '--central_scale',
type=str,
action='store',
nargs='+',
help="""mcfm histogram file(s) for a given set of
observables that consistitute the central scale""")
parser_scale.add_argument(
'-vs', '--variation_scales',
type=str,
action='store',
nargs='*',
help="""mcfm histogram file(s) that constitute the scale
variations to be performed""")
parser_scale.add_argument(
'-s', '--symmetric',
dest='symmetric',
action='store_true',
help="""Symmetrise the uncertainty bands by taking the maximum of the upper
and lower uncertainties""")
parser_scale.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_scale.set_defaults(func=scale_var)
parser_match = subparsers.add_parser(
"match",
description="",
help="")
match_subparsers = parser_match.add_subparsers(
title="subcommands",
description="des",
help="help me")
parser_match_nlo = match_subparsers.add_parser(
"nlo",
description="",
help="")
parser_match_nlo.add_argument(
'-l', '--lo',
dest='lo',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo.add_argument(
'-v', '--virt',
required=True,
dest='virt',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo.add_argument(
'-r', '--real',
required=True,
dest='real',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_match_nlo.set_defaults(func=match_nlo)
parser_match_nlo_nll = match_subparsers.add_parser(
"nlo+nll",
description="",
help="")
parser_match_nlo_nll.add_argument(
'-l', '--lo',
dest='lo',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nll.add_argument(
'-v', '--virt',
dest='virt',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nll.add_argument(
'-r', '--real',
dest='real',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nll.add_argument(
'--nll',
dest='nll',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nll.add_argument(
'--nllexpd',
dest='nllexpd',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nll.add_argument(
'-ri', '--real_inclusive',
dest='real_inclusive',
type=str,
action='store',
nargs='+',
help="""The inclusive real cross section with no constraints on QCD
radiation applied, only necessary for scheme 'b'""")
parser_match_nlo_nll.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_match_nlo_nll.set_defaults(func=match_nlo_nll)
parser_match_nlo_nnll = match_subparsers.add_parser(
"nlo+nnll",
description="",
help="")
parser_match_nlo_nnll.add_argument(
'-s', '--scheme',
dest='scheme',
type=str,
action='store',
default='a',
choices=['a', 'b'],
help="""""")
parser_match_nlo_nnll.add_argument(
'-l', '--lo',
dest='lo',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'-v', '--virt',
dest='virt',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'-r', '--real',
dest='real',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'--nnll',
dest='nnll',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'--nnllexpd',
dest='nnllexpd',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'--lumi0',
dest='lumi0',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'--lumi1',
dest='lumi1',
type=str,
action='store',
nargs='+',
help='')
parser_match_nlo_nnll.add_argument(
'-ri', '--real_inclusive',
dest='real_inclusive',
type=str,
action='store',
nargs='+',
help="""The inclusive real cross section with no constraints on QCD
radiation applied, only necessary for scheme 'b'""")
parser_match_nlo_nnll.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_match_nlo_nnll.set_defaults(func=match_nlo_nnll)
parser_match_nnll_pure = match_subparsers.add_parser(
"nnll_pure",
description="",
help="")
parser_match_nnll_pure.add_argument(
'-s', '--scheme',
dest='scheme',
type=str,
action='store',
default='a',
choices=['a', 'b'],
help="""""")
parser_match_nnll_pure.add_argument(
'--nnll',
dest='nnll',
type=str,
action='store',
nargs='+',
help='')
parser_match_nnll_pure.add_argument(
'-l', '--lo',
dest='lo',
type=str,
action='store',
nargs='+',
help='')
parser_match_nnll_pure.add_argument(
'-v', '--virt',
dest='virt',
type=str,
action='store',
nargs='+',
help='')
parser_match_nnll_pure.add_argument(
'-ri', '--real_inclusive',
dest='real_inclusive',
type=str,
action='store',
nargs='+',
help="""The inclusive real cross section with no constraints on QCD
radiation applied, only necessary for scheme 'b'""")
parser_match_nnll_pure.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_match_nnll_pure.set_defaults(func=match_nnll_pure)
parser_efficiency = subparsers.add_parser(
"efficiency",
description="",
help="")
parser_efficiency.add_argument(
'-c', '--with_cuts',
dest='vetoed',
type=str,
action='store',
nargs='+',
help="""""")
parser_efficiency.add_argument(
'-i', '--inclusive',
dest='inclusive',
type=str,
action='store',
nargs='+',
help="""""")
parser_efficiency.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_efficiency.set_defaults(func=efficiency)
parser_jve = subparsers.add_parser(
"JVE",
description="",
help="")
parser_jve.add_argument(
'-e', '--efficiency',
dest='efficiency',
type=str,
action='store',
nargs='+',
help="""""")
parser_jve.add_argument(
'-x', '--inclusive_xsec',
dest='inclusive_xsec',
type=str,
action='store',
nargs='+',
help="""""")
parser_jve.add_argument(
'-o', '--output',
dest='output',
type=str,
action='store',
default='histogram',
help="""The prefix that is attached at the start of the output filename""")
parser_jve.set_defaults(func=jet_veto_efficiency)
parser_delta = subparsers.add_parser("delta", help="")
############################################################
#### Histogram Class ####
############################################################
#### mcfm .dat output
## Header information
## Histogram Block
# newline
# HIST = number
# observable name
# data
# newline
# AVG RMS INTEGRAL
# newline
# ENTRIES O`FLOW U`FLOW
# newline
## Histogram Block
# ...
class MixedObs(Exception):
pass
class mcfmhisto(object):
def __init__(self, obs, nbins, xmin, xmax, bins, xsecs):
self.obs = obs
self.nbins = nbins
self.xmin = xmin
self.xmax = xmax
self.bins = bins
self.xsecs = xsecs
def __repr__(self):
return "hist blah"
# pass
def __str__(self):
pretty_histo = """## BEGIN HEADER
# observable name:
# {obs}
# nbins\txmin\txmax
# {nbins} {xmin} {xmax}
## END HEADER
## BEGIN HISTOGRAM
""".format(obs=self.obs, nbins=self.nbins, xmin=self.xmin, xmax=self.xmax)
for bin, xsec in zip(self.bins, self.xsecs):
pretty_histo += "{:08.3f}\t{:014.10E}\n".format(bin, xsec)
pretty_histo += "## END HISTOGRAM"
return pretty_histo
def __format__(self):
pass
def __len__(self):
assert len(self.bins) == len(self.xsecs)
return len(self.bins)
def __neg__(self):
xsecs = [-x for x in self.xsecs]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
def __pos__(self):
xsecs = [+x for x in self.xsecs]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
def __pow__(self, other):
if isinstance(other, numbers.Real):
# catch negative powers of zero that would give divide by zero
# exceptions we tactically assume that this errors always comes
# from the matching where we have expressions like 0*(1 + 0/0)
# which is well defined
xsecs = [0.0 if x == 0 and other <= 0
else x**other for x in self.xsecs]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
else:
return NotImplemented
def __add__(self, other):
if isinstance(other, numbers.Real):
xsecs = [other + x for x in self.xsecs]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
elif isinstance(other, mcfmhisto):
try:
if self.obs == None:
pass
elif other.obs == None:
pass
elif self.obs != other.obs:
raise MixedObs
xsecs = [x + y for x, y in zip(self.xsecs, other.xsecs)]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
except TypeError:
if self.xsecs is None and other.xsecs is None:
return self
elif self.xsecs is None:
return other
elif other.xsecs is None:
return self
else:
return Exception
except MixedObs:
return "You can't mix observables, this doesn't make sense"
else:
return NotImplemented
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
return self + -other
def __rsub__(self, other):
return -self + other
def __mul__(self, other):
if isinstance(other, numbers.Real):
xsecs = [other * x for x in self.xsecs]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
elif isinstance(other, mcfmhisto):
try:
if self.obs == None:
pass
elif other.obs == None:
pass
elif self.obs != other.obs:
raise MixedObs
xsecs = [x * y for x, y in zip(self.xsecs, other.xsecs)]
return mcfmhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, xsecs)
except TypeError:
if self.xsecs is None and other.xsecs is None:
return self
elif self.xsecs is None:
return other
elif other.xsecs is None:
return self
else:
return Exception
except MixedObs:
return "You can't mix observables, this doesn't make sense"
else:
return NotImplemented
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
return self * other**-1
def __rtruediv__(self, other):
return self**-1 * other
class scalehisto(object):
def __init__(self, obs, nbins, xmin, xmax, bins, central_scale,
min_scale, max_scale):
self.obs = obs
self.nbins = nbins
self.xmin = xmin
self.xmax = xmax
self.bins = bins
self.central_scale = central_scale
self.min_scale = min_scale
self.max_scale = max_scale
def __str__(self):
pretty_histo = """## BEGIN HEADER
# observable name:
# {obs}
# nbins\txmin\txmax
# {nbins} {xmin} {xmax}
## END HEADER
## BEGIN HISTOGRAM
""".format(obs=self.obs, nbins=self.nbins, xmin=self.xmin, xmax=self.xmax)
for bin, cen, mini, maxi in zip(self.bins,
self.central_scale,
self.min_scale,
self.max_scale):
pretty_histo += "{:08.3f}\t{:014.10E}\t{:014.10E}\t{:014.10E}\n".format(bin, cen, mini, maxi)
pretty_histo += "## END HISTOGRAM"
return pretty_histo
class scalesymhisto(object):
def __init__(self, obs, nbins, xmin, xmax, bins, central_scale, sym_scale):
self.obs = obs
self.nbins = nbins
self.xmin = xmin
self.xmax = xmax
self.bins = bins
self.central_scale = central_scale
self.sym_scale = sym_scale
def __str__(self):
pretty_histo = """## BEGIN HEADER
# observable name:
# {obs}
# nbins\txmin\txmax
# {nbins} {xmin} {xmax}
## END HEADER
## BEGIN HISTOGRAM
""".format(obs=self.obs, nbins=self.nbins, xmin=self.xmin, xmax=self.xmax)
for bin, cen, err in zip(self.bins,
self.central_scale,
self.sym_scale):
pretty_histo += "{:08.3f}\t{:014.10E}\t{:014.10E}\n".format(bin, cen, err)
pretty_histo += "## END HISTOGRAM"
return pretty_histo
def __mul__(self, other):
if isinstance(other, numbers.Real):
central_scales = [other * x for x in self.central_scale]
sym_scales = [other * x for x in self.sym_scale]
return scalesymhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, central_scales, sym_scales)
elif isinstance(other, scalesymhisto):
try:
if self.obs == None:
pass
elif other.obs == None:
pass
elif self.obs != other.obs:
raise MixedObs
central_scales = [x * y for x, y in zip(self.central_scale, other.central_scale)]
sym_scales = [err_quad(sc, sdc, oc, odc) for sc, sdc, oc, odc
in zip(self.central_scale,
self.sym_scale,
other.central_scale,
other.sym_scale)]
# sym_scales = [abs(sc*oc)*sqrt( (sdc/sc)**2 + (odc/oc)**2 )
# for sc, sdc, oc, odc
# in zip(self.central_scale,
# self.sym_scale,
# other.central_scale,
# other.sym_scale)]
return scalesymhisto(self.obs, self.nbins, self.xmin, self.xmax,
self.bins, central_scales, sym_scales)
except TypeError:
if self.xsecs is None and other.xsecs is None:
return self
elif self.xsecs is None:
return other
elif other.xsecs is None:
return self
else:
return Exception
except MixedObs:
return "You can't mix observables, this doesn't make sense"
else:
return NotImplemented
def __rmul__(self, other):
return self.__mul__(other)
############################################################
#### Histogram Parsing ####
############################################################
def parse_header(header_in):
pass
def parse_histo(histo_in):
try:
obs_name, *data, integ, shots = histo_in
bins = []
xsecs = []
for line in data:
bin, xsec, xsecerr = line.split()
bins.append(float(bin))
xsecs.append(float(xsec))
nbins = len(bins)
xmin = bins[0]
xmax = bins[-1]
# currently unused
# avg, rms, integral = integ.split()
# entries, uflow, oflow = shots.split()
return mcfmhisto(obs_name, nbins, xmin, xmax, bins, xsecs)
except ValueError:
return mcfmhisto(None, None, None, None, None, None)
############################################################
#### Helper Functions ####
############################################################