~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to tests/unit_tests/various/test_histograms.py

  • Committer: olivier Mattelaer
  • Date: 2015-03-05 00:14:16 UTC
  • mfrom: (258.1.9 2.3)
  • mto: (258.8.1 2.3)
  • mto: This revision was merged to the branch mainline in revision 259.
  • Revision ID: olivier.mattelaer@uclouvain.be-20150305001416-y9mzeykfzwnl9t0j
partial merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
################################################################################
 
2
#
 
3
# Copyright (c) 2011 The MadGraph5_aMC@NLO Development team and Contributors
 
4
#
 
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.
 
8
#
 
9
# It is subject to the MadGraph5_aMC@NLO license which should accompany this 
 
10
# distribution.
 
11
#
 
12
# For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch
 
13
#
 
14
################################################################################
 
15
from __future__ import division
 
16
 
 
17
import madgraph.various.histograms as histograms
 
18
import os
 
19
import unittest
 
20
import copy
 
21
import tests.IOTests as IOTests
 
22
import madgraph.various.misc as misc
 
23
 
 
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')
 
26
pjoin = os.path.join
 
27
 
 
28
class TestHistograms(unittest.TestCase):
 
29
    """Test that Histograms are correctly read, parsed, manipulated, written
 
30
    out."""
 
31
    
 
32
    def setUp(self):
 
33
        """ Load the histograms"""
 
34
 
 
35
        # load the base histograms
 
36
        self.histo_list = histograms.HwUList(_HwU_source)
 
37
    
 
38
    def test_histograms_operations(self):
 
39
        """ We test that basic operations are correctly handled """
 
40
        
 
41
        histo_list = copy.copy(self.histo_list)
 
42
    
 
43
        # "Testing 'Hist1 - 2.0 + 2.0 == Hist1'"
 
44
        my_hist = histo_list[0]+2.0
 
45
        my_hist = my_hist-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)
 
48
 
 
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)
 
54
        
 
55
        #"Testing 'Hist1 * 2.0 / 2.0 == Hist1'"
 
56
        my_hist = histo_list[0]*2.0
 
57
        my_hist = my_hist/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)
 
60
 
 
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)
 
66
    
 
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."""
 
70
        
 
71
        one_histo = histograms.HwUList([copy.copy(self.histo_list[0])])
 
72
        
 
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'))
 
76
        
 
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])
 
92
        
 
93
class IOTest_Histogram(IOTests.IOTestManager):
 
94
    """To compare the gnuplot and HwU output of a full-fledged output."""
 
95
    
 
96
    @IOTests.createIOTest()
 
97
    def testIO_gnuplot_histo_output(self):
 
98
        """ target: HistoOut.HwU
 
99
            target: HistoOut.gnuplot
 
100
        """
 
101
        histo_list = histograms.HwUList(_HwU_source)
 
102
        histo_list.output(pjoin(self.IOpath,'HistoOut'), format = 'gnuplot')
 
 
b'\\ No newline at end of file'