HPS-MC
test_tools_stdheptools.py
Go to the documentation of this file.
1 import unittest
2 
3 from hpsmc.tools import StdHepTool, BeamCoords, RandomSample, DisplaceTime, DisplaceUni, AddMother, AddMotherFullTruth, MergePoisson, MergeFiles
4 
5 
6 class TestStdHepTools(unittest.TestCase):
7 
8  def test_init(self):
9  stdheptool = StdHepTool(name='tool')
10  self.assertEqual(stdheptool.name, 'tool')
11  self.assertEqual(stdheptool.command, 'stdhep_tool')
12 
13  def test_cmd_args(self):
14  stdheptool = StdHepTool(name='beam_coords', seed=1, inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'])
15  self.assertEqual(stdheptool.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output.stdhep', '-s', '1'])
16 
18  stdheptool = StdHepTool(name='tool', inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'])
19  self.assertEqual(stdheptool.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output.stdhep'])
20 
22  stdheptool = StdHepTool(name='tool', outputs=['output.stdhep'])
23  self.assertRaises(Exception, lambda: stdheptool.cmd_args())
24 
26  stdheptool = StdHepTool(name='tool', inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output1.stdhep', 'output2.stdhep'])
27  self.assertEqual(len(stdheptool.output_files()), 2)
28  self.assertRaises(Exception, lambda: stdheptools.cmd_args())
29 
30 
31 class TestBeamCoords(unittest.TestCase):
32 
33  def test_init(self):
34  beam_coords = BeamCoords()
35  self.assertEqual(beam_coords.name, 'beam_coords')
36  self.assertEqual(beam_coords.append_tok, 'rot')
37 
39  beam_coords = BeamCoords()
40  self.assertEqual(beam_coords.optional_parameters(), ['beam_sigma_x', 'beam_sigma_y', 'beam_rot_x', 'beam_rot_y', 'beam_rot_z', 'target_x', 'target_y', 'target_z'])
41 
42  def test_cmd_args(self):
43  beam_coords = BeamCoords(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'])
44  beam_coords.set_parameters({'beam_sigma_x': 11, 'beam_sigma_y': 12, 'beam_rot_x': 21, 'beam_rot_y': 22, 'beam_rot_z': 23, 'target_x': 31, 'target_y': 32, 'target_z': 33})
45  self.assertEqual(beam_coords.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output.stdhep', '-s', '1', '-x', '11', '-y', '12', '-u', '21', '-v', '22', '-w', '23', '-X', '31', '-Y', '32', '-Z', '33'])
46 
47 
48 class TestRandomSample(unittest.TestCase):
49 
50  def test_init(self):
51  random_sample = RandomSample()
52  self.assertEqual(random_sample.name, 'random_sample')
53  self.assertEqual(random_sample.append_tok, 'sampled')
54 
56  random_sample = RandomSample()
57  self.assertEqual(random_sample.optional_parameters(), ['nevents', 'mu'])
58 
59  def test_cmd_args(self):
60  random_sample = RandomSample(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'], seed=2)
61  random_sample.set_parameters({'nevents': 100, 'mu': 1, 'seed': 2})
62  self.assertEqual(random_sample.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output', '-s', '2', '-N', '1', '-n', '100', '-m', '1'])
63 
64 
65 class TestDisplaceTime(unittest.TestCase):
66 
67  def test_init(self):
68  displace_time = DisplaceTime()
69  self.assertEqual(displace_time.name, 'lhe_tridents_displacetime')
70  self.assertEqual(displace_time.output_ext, '.stdhep')
71 
73  displace_time = DisplaceTime()
74  self.assertEqual(displace_time.optional_parameters(), ['ctau'])
75 
76  def test_cmd_args(self):
77  displace_time = DisplaceTime(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'])
78  displace_time.set_parameters({'ctau': 1})
79  self.assertEqual(displace_time.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output.stdhep', '-s', '1', '-l', '1'])
80 
81 
82 class TestDisplaceUni(unittest.TestCase):
83 
84  def test_init(self):
85  displace_uni = DisplaceUni()
86  self.assertEqual(displace_uni.name, 'lhe_tridents_displaceuni')
87  self.assertEqual(displace_uni.output_ext, '.stdhep')
88 
90  displace_uni = DisplaceUni()
91  self.assertEqual(displace_uni.optional_parameters(), ['ctau'])
92 
93  def test_cmd_args(self):
94  displace_uni = DisplaceUni(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'])
95  displace_uni.set_parameters({'ctau': 1})
96  self.assertEqual(displace_uni.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output.stdhep', '-s', '1', '-l', '1'])
97 
98 
99 class TestAddMother(unittest.TestCase):
100 
101  def test_init(self):
102  add_mother = AddMother()
103  self.assertEqual(add_mother.name, 'add_mother')
104  self.assertEqual(add_mother.append_tok, 'mom')
105 
106 
107 class TestMotherFullTruth(unittest.TestCase):
108 
109  def test_init(self):
110  add_mother_full_truth = AddMotherFullTruth(inputs=['input1.stdhep', 'input2.lhe'], outputs=['output.stdhep'])
111  self.assertEqual(add_mother_full_truth.name, 'add_mother_full_truth')
112  self.assertEqual(add_mother_full_truth.append_tok, 'mom_full_truth')
113 
115  self.assertRaises(Exception, lambda: AddMotherFullTruth(outputs=['output.stdhep']), msg="Must have 2 input files: a stdhep file and a lhe file in order")
116  self.assertRaises(Exception, lambda: AddMotherFullTruth(inputs=['input1.stdhep', 'input2.lhe', 'input3.stdhep'], outputs=['output.stdhep']), msg="Must have 2 input files: a stdhep file and a lhe file in order")
117  self.assertRaises(Exception, lambda: AddMotherFullTruth(inputs=['input1.stdhep'], outputs=['output.stdhep']), msg="Must have 2 input files: a stdhep file and a lhe file in order")
118  self.assertRaises(Exception, lambda: AddMotherFullTruth(inputs=['input1.root', 'input2.lhe'], outputs=['output.stdhep']), msg="The first input file must be a stdhep file")
119  self.assertRaises(Exception, lambda: AddMotherFullTruth(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep']), msg="The second input file must be a lhe file")
120 
121  def test_cmd_args(self):
122  add_mother_full_truth = AddMotherFullTruth(inputs=['input1.stdhep', 'input2.lhe'], outputs=['output.stdhep'])
123  self.assertEqual(add_mother_full_truth.cmd_args(), ['input1.stdhep', 'input2.lhe', 'output.stdhep'])
124 
125 
126 class TestMergePoisson(unittest.TestCase):
127 
128  def test_init(self):
129  merge_poisson = MergePoisson()
130  self.assertEqual(merge_poisson.name, 'merge_poisson')
131  self.assertEqual(merge_poisson.append_tok, 'sampled')
132  self.assertEqual(merge_poisson.xsec, 0)
133 
135  merge_poisson = MergePoisson()
136  self.assertEqual(merge_poisson.required_parameters(), ['target_thickness', 'num_electrons'])
137 
139  merge_poisson = MergePoisson(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'], xsec=1)
140  merge_poisson.set_parameters({'target_thickness': 0.0004062, 'num_electrons': 625})
141  merge_poisson.setup()
142  res = 6.306e-14 * 0.0004062 * 625
143  self.assertEqual(merge_poisson.mu, res)
144 
146  merge_poisson = MergePoisson(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'], xsec=20)
147  merge_poisson.set_parameters({'target_thickness': 0.0004062, 'num_electrons': 625})
148  merge_poisson.setup()
149  res = 6.306e-14 * 0.0004062 * 625 * 20
150  self.assertEqual(merge_poisson.mu, res)
151 
153  merge_poisson = MergePoisson(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'], xsec=1)
154  merge_poisson.set_parameters({'target_thickness': 0.000875, 'num_electrons': 625})
155  merge_poisson.setup()
156  res = 6.306e-14 * 0.000875 * 625
157  self.assertEqual(merge_poisson.mu, res)
158 
159  def test_cmd_args(self):
160  merge_poisson = MergePoisson(inputs=['input1.stdhep', 'input2.stdhep'], outputs=['output.stdhep'], xsec=1)
161  merge_poisson.set_parameters({'target_thickness': 0.0004062, 'num_electrons': 625, 'mu': 1, 'seed': 2, 'nevents': 100})
162  merge_poisson.setup()
163  self.assertEqual(merge_poisson.cmd_args(), ['input1.stdhep', 'input2.stdhep', 'output', '-s', '2', '-m', str(6.306e-14 * 0.0004062 * 625), '-N', '1', '-n', '100'])
164 
165 
166 class TestMergeFiles(unittest.TestCase):
167 
168  def test_init(self):
169  merge_files = MergeFiles()
170  self.assertEqual(merge_files.name, 'merge_files')
171 
173  merge_files = MergeFiles()
174  self.assertEqual(merge_files.optional_parameters(), [])
175 
177  merge_files = MergeFiles()
178  self.assertEqual(merge_files.required_parameters(), [])
179 
180 
181 if __name__ == '__main__':
182  unittest.main()
Add full truth mother particles for physics samples.
Definition: tools.py:742
Add mother particles for physics samples.
Definition: tools.py:730
Transform StdHep events into beam coordinates.
Definition: tools.py:516
Convert LHE files to StdHep, displacing the time by given ctau.
Definition: tools.py:660
Convert LHE files to StdHep, displacing the time by given ctau.
Definition: tools.py:695
Merge StdHep files.
Definition: tools.py:846
Merge StdHep files, applying poisson sampling.
Definition: tools.py:769
Randomly sample StdHep events into a new file.
Definition: tools.py:589
Generic class for StdHep tools.
Definition: tools.py:468
Tools that can be used in HPSMC jobs.
Definition: tools.py:1