~9marusia6/maus/beamlinesimulation

« back to all changes in this revision

Viewing changes to tests/integration/test_simulation/test_accumulate_coloured_particles/test_accumulate_coloured_particles.py

  • Committer: justinchristensen at berkeley
  • Date: 2012-06-08 08:47:52 UTC
  • mfrom: (669.1.1 release)
  • Revision ID: justinchristensen@berkeley.edu-20120608084752-5x3f3rzflhhcewwi
new version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
test_accumulate_coloured_particles.py
 
3
"""
 
4
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
5
#
 
6
#  MAUS is free software: you can redistribute it and/or modify
 
7
#  it under the terms of the GNU General Public License as published by
 
8
#  the Free Software Foundation, either version 3 of the License, or
 
9
#  (at your option) any later version.
 
10
#
 
11
#  MAUS is distributed in the hope that it will be useful,
 
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#  GNU General Public License for more details.
 
15
#
 
16
#  You should have received a copy of the GNU General Public License
 
17
#  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import unittest
 
20
import os
 
21
import subprocess
 
22
import glob
 
23
 
 
24
MAUS_ROOT_DIR = os.getenv("MAUS_ROOT_DIR")
 
25
SIM_PATH = os.path.join(MAUS_ROOT_DIR, "bin", "simulate_mice.py")
 
26
TEST_DIR = os.path.join(MAUS_ROOT_DIR, "tests", "integration", \
 
27
                        "test_simulation", "test_accumulate_coloured_particles")
 
28
CONFIG_PATH = os.path.join(TEST_DIR, "test_particles_config.txt")
 
29
FILE_NAME = os.environ['MAUS_ROOT_DIR']+'/tmp/accumulate_tracks'
 
30
 
 
31
def run_simulations():
 
32
    """
 
33
    Run simulation to generate some data. We only want to do this once, so I
 
34
    pull it out into a separate part of the test.
 
35
    """
 
36
    test_out = open(FILE_NAME, 'w')
 
37
    subproc = subprocess.Popen([SIM_PATH, '-configuration_file', CONFIG_PATH], \
 
38
                                      stdout=test_out, stderr=subprocess.STDOUT)
 
39
    subproc.wait()
 
40
    test_out.close()
 
41
 
 
42
def file_cleanup():
 
43
    """
 
44
    This method cleans up the vrml and simulation.out files produced by the 
 
45
    simulation. The first method must check the vrml file but it is not needed 
 
46
    afterwards.
 
47
    """
 
48
    for filename in glob.glob('g4_*.wrl'):
 
49
        os.remove(filename)
 
50
    for filename in glob.glob('*.out'):
 
51
        os.remove(filename)
 
52
 
 
53
class AccumulateColouredParticles(unittest.TestCase): #pylint: disable = R0904
 
54
    """
 
55
    This class has two tests. One checks that particles are accumulated into one
 
56
    vrml output file when the tag is selected in configuration defaults. The 
 
57
    second checks that the DrawByParticleID model has been registered so 
 
58
    particles will have specified colours in the vrml.
 
59
    """
 
60
    def setUp(self): # pylint: disable=C0103, C0202
 
61
        """ Run Simulation """
 
62
        run_simulations()
 
63
    
 
64
    def test_for_accumulated_tracks(self):  #pylint: disable=R0201
 
65
        """ Check that we have accumulated particles """
 
66
        if len(glob.glob('g4_*.wrl')) > 1:
 
67
            raise Exception('more than one vrml, tracks not accumulated')
 
68
        file_cleanup()
 
69
 
 
70
    def test_coloured_particles(self):
 
71
        """ Check that we have coloured particles """
 
72
        test_in = open(FILE_NAME, 'r')
 
73
        found_model = 0
 
74
        for lines in test_in.readlines():
 
75
            if lines.find('G4TrajectoryDrawByParticleID') >= 0:
 
76
                found_model += 1
 
77
        self.assertEqual(found_model, 1, 'DrawByParticleID model not found')
 
78
        file_cleanup()
 
79
 
 
80
if __name__ == '__main__':
 
81
    unittest.main()