HPS-MC
test_tools_sim.py
Go to the documentation of this file.
1 import unittest
2 import configparser
3 import os
4 
5 from hpsmc.tools import SimBase, Sim
6 
7 
8 class TestSimBase(unittest.TestCase):
9 
11  sim_base = SimBase(name="sim_base")
12  self.assertEqual(sim_base.required_parameters(), ["detector"])
13 
15  sim_base = SimBase(name="sim_base")
16  self.assertEqual(sim_base.optional_parameters(), ["nevents", "macros", "run_number"])
17 
18  def test_detector_file(self):
19  sim_base = SimBase(name="sim_base")
20  parser = configparser.ConfigParser()
21  config_file = ['test_helpers/.hpsmc_test_cfg']
22  parser.read(config_file)
23  sim_base.config(parser)
24  params = {"detector": "some_detector"}
25  sim_base.set_parameters(params)
26  self.assertEqual(sim_base.detector_file(), "some/detector/dir/some_detector/some_detector.lcdd")
27 
28 
29 class TestSim(unittest.TestCase):
30 
31  def test_init(self):
32  sim = Sim()
33  self.assertEqual(sim.name, "hps-sim")
34  self.assertEqual(sim.command, "hps-sim")
35  self.assertEqual(sim.output_ext, ".slcio")
36 
38  sim = Sim()
39  self.assertEqual(sim.required_config(), ["hps_sim_dir", "hps_fieldmaps_dir", "detector_dir"])
40 
41  def test_config(self):
42  sim = Sim()
43  parser = configparser.ConfigParser()
44  config_file = ['test_helpers/.hpsmc_test_cfg']
45  parser.read(config_file)
46  sim.config(parser)
47  self.assertEqual(sim.detector_dir, "some/detector/dir")
48  self.assertEqual(sim.hps_fieldmaps_dir, "some/fieldmaps/dir")
49  self.assertEqual(sim.hps_sim_dir, "test_helpers/simdir")
50 
51  def test_setup(self):
52  sim = Sim(inputs=["some/path/to/input.stdhep"], outputs=["some/path/to/output.slcio"])
53  parser = configparser.ConfigParser()
54  config_file = ['test_helpers/.hpsmc_test_cfg']
55  parser.read(config_file)
56  sim.config(parser)
57  params = {"detector": "some_detector"}
58  sim.set_parameters(params)
59  sim.setup()
60  self.assertEqual(sim.env_script, "test_helpers/simdir/bin/hps-sim-env.sh")
61  self.assertTrue(os.path.islink(os.getcwd() + os.path.sep + "fieldmap"))
62  os.unlink(os.getcwd() + "/fieldmap")
63 
65  sim = Sim(inputs=["some/path/to/input.stdhep"], outputs=["some/path/to/output.slcio"], seed=2, nevents=10)
66  parser = configparser.ConfigParser()
67  config_file = ['test_helpers/.hpsmc_test_cfg']
68  parser.read(config_file)
69  sim.config(parser)
70  params = {"detector": "some_detector", "macros": ["macro1.mac", "macro2.mac"]}
71  sim.set_parameters(params)
72  sim.setup()
73  sim.write_run_macro()
74  self.assertTrue(os.path.isfile("run.macro"))
75  with open("run.macro", "r") as f:
76  lines = f.readlines()
77  self.assertEqual(lines[0], "/lcdd/url some/detector/dir/some_detector/some_detector.lcdd\n")
78  self.assertEqual(lines[1], "/run/initialize\n")
79  self.assertEqual(lines[2], "/random/seed 2\n")
80  self.assertEqual(lines[3], "/hps/generators/create StdHepGen STDHEP\n")
81  self.assertEqual(lines[4], "/hps/generators/StdHepGen/file some/path/to/input.stdhep\n")
82  self.assertEqual(lines[5], "/control/execute macro1.mac\n")
83  self.assertEqual(lines[6], "/control/execute macro2.mac\n")
84  self.assertEqual(lines[7], "/hps/lcio/recreate\n")
85  self.assertEqual(lines[8], "/hps/lcio/file some/path/to/output.slcio\n")
86  self.assertEqual(lines[9], "/run/beamOn 10")
87 
88  os.remove(os.getcwd() + "/run.macro")
89  os.unlink(os.getcwd() + "/fieldmap")
90 
91 
92 if __name__ == "__main__":
93  unittest.main()
Tools that can be used in HPSMC jobs.
Definition: tools.py:1