1
################################################################################
3
# Copyright (c) 2011 The MadGraph5_aMC@NLO Development team and Contributors
5
# This file is a part of the MadGraph5_aMC@NLO project, an application which
6
# automatically generates Feynman diagrams and matrix elements for arbitrary
7
# high-energy processes in the Standard Model and beyond.
9
# It is subject to the MadGraph5_aMC@NLO license which should accompany this
12
# For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch
14
################################################################################
15
from __future__ import division
17
import madgraph.various.histograms as histograms
21
import tests.IOTests as IOTests
22
import madgraph.various.misc as misc
24
_file_path = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
25
_HwU_source = os.path.join(_file_path,os.pardir,'input_files','MADatNLO.HwU')
28
class TestHistograms(unittest.TestCase):
29
"""Test that Histograms are correctly read, parsed, manipulated, written
33
""" Load the histograms"""
35
# load the base histograms
36
self.histo_list = histograms.HwUList(_HwU_source)
38
def test_histograms_operations(self):
39
""" We test that basic operations are correctly handled """
41
histo_list = copy.copy(self.histo_list)
43
# "Testing 'Hist1 - 2.0 + 2.0 == Hist1'"
44
my_hist = histo_list[0]+2.0
46
self.assertTrue(abs(2.0-(my_hist.bins[0].wgts['central']/histo_list[0].bins[0].wgts['central'])-\
47
(my_hist.bins[0].wgts[(1.0,2.0)]/histo_list[0].bins[0].wgts[(1.0,2.0)]))<1.0e-14)
49
# "Testing 'Hist1 - Hist2 + Hist2 == Hist1'"
50
my_hist = histo_list[0]+histo_list[1]
51
my_hist = my_hist-histo_list[1]
52
self.assertTrue(abs(2.0-(my_hist.bins[0].wgts['central']/histo_list[0].bins[0].wgts['central'])-\
53
(my_hist.bins[0].wgts[(1.0,2.0)]/histo_list[0].bins[0].wgts[(1.0,2.0)]))<1.0e-14)
55
#"Testing 'Hist1 * 2.0 / 2.0 == Hist1'"
56
my_hist = histo_list[0]*2.0
58
self.assertTrue(abs(2.0-(my_hist.bins[0].wgts['central']/histo_list[0].bins[0].wgts['central'])-\
59
(my_hist.bins[0].wgts[(1.0,2.0)]/histo_list[0].bins[0].wgts[(1.0,2.0)]))<1.0e-14)
61
#"Testing 'Hist1 * Hist2 / Hist2 == Hist1'"
62
my_hist = histo_list[0]*histo_list[1]
63
my_hist = my_hist/histo_list[1]
64
self.assertTrue(abs(2.0-(my_hist.bins[0].wgts['central']/histo_list[0].bins[0].wgts['central'])-\
65
(my_hist.bins[0].wgts[(1.0,2.0)]/histo_list[0].bins[0].wgts[(1.0,2.0)]))<1.0e-14)
67
def test_output_reload(self):
68
""" Outputs existing HwU histograms in the gnuplot format and makes sure
69
that they remain identical when reloading them."""
71
one_histo = histograms.HwUList([copy.copy(self.histo_list[0])])
73
with misc.TMP_directory() as tmpdir:
74
one_histo.output(pjoin(tmpdir,'OUT'), format='gnuplot')
75
new_histo = histograms.HwUList(pjoin(tmpdir,'OUT.HwU'))
77
one_histo = one_histo[0][0]
78
one_histo.trim_auxiliary_weights()
79
new_histo = new_histo[0]
80
self.assertEqual(one_histo.type, new_histo.type)
81
self.assertEqual(one_histo.title,new_histo.title)
82
self.assertEqual(one_histo.x_axis_mode,new_histo.x_axis_mode)
83
self.assertEqual(one_histo.y_axis_mode,new_histo.y_axis_mode)
84
self.assertEqual(one_histo.bins.weight_labels,
85
new_histo.bins.weight_labels)
86
self.assertEqual(len(one_histo.bins),len(new_histo.bins))
87
for i, bin in enumerate(one_histo.bins):
88
self.assertEqual(set(bin.wgts.keys()),
89
set(new_histo.bins[i].wgts.keys()))
90
for label, wgt in bin.wgts.items():
91
self.assertEqual(wgt,new_histo.bins[i].wgts[label])
93
class IOTest_Histogram(IOTests.IOTestManager):
94
"""To compare the gnuplot and HwU output of a full-fledged output."""
96
@IOTests.createIOTest()
97
def testIO_gnuplot_histo_output(self):
98
""" target: HistoOut.HwU
99
target: HistoOut.gnuplot
101
histo_list = histograms.HwUList(_HwU_source)
102
histo_list.output(pjoin(self.IOpath,'HistoOut'), format = 'gnuplot')
b'\\ No newline at end of file'