HPS-MC
test_generators.py
Go to the documentation of this file.
1 import unittest
2 import os
3 
4 from hpsmc.generators import EventGenerator, EGS5, MG, MG4, MG5
5 
6 
7 class TestEventGenerator(unittest.TestCase):
8 
10  event_generator = EventGenerator('generator')
11  self.assertEqual(event_generator.required_parameters(), ['nevents'])
12 
13 
14 class TestEGS5(unittest.TestCase):
15 
16  def test_init(self):
17  egs5 = EGS5()
18  self.assertEqual(egs5.bunches, 5e5)
19 
20  def test_init_kwrags(self):
21  egs5 = EGS5(inputs="input/file.root", nevents=100)
22  self.assertEqual(egs5.nevents, 100)
23  self.assertEqual(egs5.inputs, "input/file.root")
24 
26  egs5 = EGS5()
27  self.assertEqual(egs5.required_parameters(), ['seed', 'target_thickness', 'beam_energy', 'num_electrons'])
28 
30  egs5 = EGS5()
31  self.assertEqual(egs5.optional_parameters(), ['bunches'])
32 
33  def test_output_files(self):
34  egs5 = EGS5()
35  egs5_moller = EGS5(name='moller')
36  self.assertEqual(egs5.output_files(), ['beam.stdhep'])
37  self.assertEqual(egs5_moller.output_files(), ['moller.stdhep'])
38 
40  egs5 = EGS5()
41  params = {'seed': 1, 'target_thickness': 1.0, 'beam_energy': 1.0, 'num_electrons': 1}
42  egs5.set_parameters(params)
43  self.assertEqual(egs5.seed, 1)
44  self.assertEqual(egs5.target_thickness, 1.0)
45  self.assertEqual(egs5.beam_energy, 1.0)
46  self.assertEqual(egs5.num_electrons, 1)
47 
48  def test_setup(self):
49  egs5 = EGS5()
50  params = {'seed': 1, 'bunches': 1, 'target_thickness': 0.0004062, 'beam_energy': 1100.00, 'num_electrons': 625}
51  egs5.set_parameters(params)
52  egs5.setup()
53  egs5_dir = os.getenv("HPSMC_DIR", None) + '/share/generators/egs5'
54  self.assertEqual(egs5.egs5_dir, egs5_dir)
55  self.assertEqual(egs5.egs5_data_dir, egs5_dir + '/data')
56  self.assertEqual(egs5.egs5_config_dir, egs5_dir + '/config')
57 
58  with open("seed.dat", 'r') as seed_file:
59  seed_vals = [line.split() for line in seed_file]
60  self.assertEqual(seed_vals[0][0], str(1))
61  self.assertEqual(seed_vals[0][1], str(egs5.target_thickness))
62  self.assertEqual(seed_vals[0][2], str(egs5.beam_energy))
63  self.assertEqual(seed_vals[0][3], str(egs5.num_electrons * params['bunches']))
64 
65  # remove created symlinks and files
66  os.remove(os.getcwd() + '/data')
67  os.remove(os.getcwd() + '/pgs5job.pegs5inp')
68  os.remove(os.getcwd() + '/seed.dat')
69 
70 
71 class test_MG(unittest.TestCase):
72 
73  def test_init(self):
74  mg = MG('mg')
75  self.assertEqual(mg.param_card, "param_card.dat")
76  self.assertEqual(mg.event_types, ['unweighted', 'weighted'])
77  self.assertEqual(mg.name, 'mg')
78 
79  def test_output_files(self):
80  mg = MG('mg')
81  self.assertEqual(mg.output_files(), ['mg_unweighted_events.lhe.gz', 'mg_events.lhe.gz'])
82 
84  mg = MG('mg')
85  self.assertEqual(mg.required_parameters(), ['nevents', 'run_params'])
86 
88  mg = MG('mg')
89  self.assertEqual(mg.optional_parameters(), ['seed', 'param_card', 'apmass', 'map', 'mpid', 'mrhod'])
90 
92  mg = MG('mg')
93  params = {'nevents': 1, 'run_params': '1pt1', 'seed': 1, 'param_card': 'param_card.dat', 'apmass': 1, 'map': 1, 'mpid': 1, 'mrhod': 1}
94  mg.set_parameters(params)
95  self.assertEqual(mg.nevents, 1)
96  self.assertEqual(mg.run_params, '1pt1')
97  self.assertEqual(mg.seed, 1)
98  self.assertEqual(mg.param_card, 'param_card.dat')
99  self.assertEqual(mg.apmass, 1)
100  self.assertEqual(mg.map, 1)
101  self.assertEqual(mg.mpid, 1)
102  self.assertEqual(mg.mrhod, 1)
103  self.assertEqual(mg.run_card, 'run_card_' + params['run_params'] + '.dat')
104 
105  def test_setup(self):
106  mg = MG('mg')
107  params = {'nevents': 1, 'run_params': '1pt1', 'seed': 1, 'param_card': 'param_card.dat', 'apmass': 1, 'map': 1, 'mpid': 1, 'mrhod': 1}
108  mg.set_parameters(params)
109  mg.setup()
110  mg_dir = os.getenv("HPSMC_DIR", None) + '/share/generators'
111  self.assertEqual(mg.madgraph_dir, mg_dir)
112 
113 
114 class test_MG4(unittest.TestCase):
115 
116  def test_init(self):
117  mg4 = MG4()
118  self.assertEqual(mg4.name, 'ap')
119  self.assertEqual(mg4.event_types, ['unweighted', 'weighted'])
120 
121  def test_init_BH(self):
122  mg4 = MG4('BH')
123  self.assertEqual(mg4.name, 'BH')
124 
125  def test_init_RAD(self):
126  mg4 = MG4('RAD')
127  self.assertEqual(mg4.name, 'RAD')
128 
129  def test_init_TM(self):
130  mg4 = MG4('TM')
131  self.assertEqual(mg4.name, 'TM')
132 
133  def test_init_trigger(self):
134  mg4 = MG4('trigg')
135  self.assertEqual(mg4.name, 'trigg')
136 
137  def test_init_tritrig(self):
138  mg4 = MG4('tritrig')
139  self.assertEqual(mg4.name, 'tritrig')
140 
141  def test_init_wab(self):
142  mg4 = MG4('wab')
143  self.assertEqual(mg4.name, 'wab')
144 
146  self.assertRaises(Exception, lambda: MG4('some_invalid_name'))
147 
149  mg4 = MG4()
150  self.assertEqual(mg4.get_install_dir(), os.getenv("HPSMC_DIR", None) + '/share/generators/madgraph4/src')
151 
152 
153 class test_MG5(unittest.TestCase):
154 
155  def test_init(self):
156  mg5 = MG5()
157  self.assertEqual(mg5.name, 'tritrig')
158  self.assertEqual(mg5.event_types, ['unweighted', 'weighted'])
159 
160  def test_init_BH(self):
161  mg5 = MG5('BH')
162  self.assertEqual(mg5.name, 'BH')
163 
164  def test_init_RAD(self):
165  mg5 = MG5('RAD')
166  self.assertEqual(mg5.name, 'RAD')
167 
168  def test_init_simp(self):
169  mg5 = MG5('simp')
170  self.assertEqual(mg5.name, 'simp')
171 
173  self.assertRaises(Exception, lambda: MG5('some_invalid_name'))
174 
176  mg5 = MG5()
177  self.assertEqual(mg5.get_install_dir(), os.getenv("HPSMC_DIR", None) + '/share/generators/madgraph5/src')
178 
179 
180 if __name__ == '__main__':
181  unittest.main()
Run the EGS5 event generator to produce a StdHep file.
Definition: generators.py:28
Event generator base class.
Definition: generators.py:13
Run the MadGraph 4 event generator.
Definition: generators.py:329
Run the MadGraph 5 event generator.
Definition: generators.py:399
Abstract class for MadGraph generators.
Definition: generators.py:179