Belle II Software  release-08-01-10
lhereader_nominal.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # Test LHEReader and LHEInputModule
5 
6 import os
7 import sys
8 import math
9 from ROOT import TFile
10 from pdg import add_particle
11 from basf2 import create_path, register_module, process, print_params, find_file
12 from modularAnalysis import fillParticleListsFromMC, printVariableValues, variablesToNtuple
13 from variables import variables as vm
14 from tempfile import TemporaryDirectory
15 
16 # check that the file exists, if not: skip the test
17 inputfile = find_file('generators/tests/event_nominal.lhe')
18 if len(inputfile) == 0:
19  sys.stderr.write(
20  'TEST SKIPPED: input file ' +
21  inputfile +
22  ' not found.')
23  sys.exit(-1)
24 
25 
26 # add the A' particle to the pdg database so we can specify it later
27 add_particle('A', 9000008, 5.5, 0.1329, 0, 0)
28 
29 # configure the LHE reade
30 lhereader = register_module('LHEInput')
31 lhereader.param('makeMaster', True)
32 lhereader.param('runNum', 1337)
33 lhereader.param('expNum', 0)
34 lhereader.param('inputFileList', [inputfile])
35 lhereader.param('useWeights', False)
36 lhereader.param('nInitialParticles', 2)
37 lhereader.param('nVirtualParticles', 0)
38 lhereader.param('wrongSignPz', False) # Using BII convention where e- goes in +z
39 print_params(lhereader)
40 
41 # prepare the path
42 testpath = create_path()
43 testpath.add_module('Progress')
44 testpath.add_module(lhereader)
45 testpath.add_module('BoostMCParticles')
46 fillParticleListsFromMC([('gamma:gen', ''), ('A:gen', '')], path=testpath)
47 
48 # dump information from basf2
49 vm.addAlias('pxcms', 'useCMSFrame(px)')
50 vm.addAlias('pycms', 'useCMSFrame(py)')
51 vm.addAlias('pzcms', 'useCMSFrame(pz)')
52 vm.addAlias('pecms', 'useCMSFrame(E)')
53 variables = ['M', 'px', 'py', 'pz', 'E',
54  'pxcms', 'pycms', 'pzcms', 'pecms']
55 
56 printVariableValues('A:gen', variables, path=testpath)
57 printVariableValues('gamma:gen', variables, path=testpath)
58 
59 variablesToNtuple('A:gen', variables, 'darkphoton', 'test.root', path=testpath)
60 variablesToNtuple('gamma:gen', variables, 'gammas', 'test.root', path=testpath)
61 
62 # temporary directory to keep cwd clean
63 with TemporaryDirectory() as tmp:
64 
65  # process the basf2 path
66  os.chdir(tmp)
67  process(testpath)
68 
69  # open output and check the momenta are what is expected
70  fi = TFile('test.root')
71  t1 = fi.Get('darkphoton')
72  t2 = fi.Get('gammas')
73 
74  assert t1.GetEntries() == 1, 'Output contains %i entries' % t1.GetEntries()
75  assert t2.GetEntries() == 1, 'Output contains %i entries' % t1.GetEntries()
76 
77  t1.GetEntry(0)
78  t2.GetEntry(0)
79 
80  assert t1.__run__ == 1337, 'Run number not set correctly'
81  assert t1.__experiment__ == 0, 'Experiment number not set correctly'
82  assert t2.M == 0, 'Photon is not as expected'
83 
84  # precision is low since MCParticle class is using floats to store particle momenta
85  # when changed to double precision was much better
86  eps = 2e-5
87 
88  # test momenta balance in CMS
89  assert math.isclose(t1.pxcms, -t2.pxcms, rel_tol=eps), 'Momenta don\'t balance'
90  assert math.isclose(t1.pycms, -t2.pycms, rel_tol=eps), 'Momenta don\'t balance'
91  assert math.isclose(t1.pzcms, -t2.pzcms, rel_tol=eps), 'Momenta don\'t balance'
92 
93  # test that dark photon CM momentum is close to LHE input generators/tests/event.lhe
94  assert math.isclose(t1.pxcms, +1.4572035746e-03, rel_tol=eps), 'CMS momenta are not as expected'
95  assert math.isclose(t1.pycms, +2.5198484375e-03, rel_tol=eps), 'CMS momenta are not as expected'
96  assert math.isclose(t1.pzcms, -3.8826254795e+00, rel_tol=eps), 'CMS momenta are not as expected'
97  assert math.isclose(t1.pecms, 6.6973734293e+00, rel_tol=eps), 'CMS momenta are not as expected'
98 
99  # test that dark photon mass is as in LHE
100  assert math.isclose(t1.M, 5.4571074540e+00, rel_tol=eps), 'Mass is not as expected'
101 
102  # test dark photon momentum in LAB
103  assert math.isclose(t1.px, 0.24481175912036506892, rel_tol=eps), 'Boosted momenta are not as expected'
104  assert math.isclose(t1.py, 0.002519848437500000083, rel_tol=eps), 'Boosted momenta are not as expected'
105  assert math.isclose(t1.pz, -2.1368737390471825854, rel_tol=eps), 'Boosted momenta are not as expected'
106 
107  # test photon momentum in LAB
108  assert math.isclose(t2.px, 0.21182522798639694117, rel_tol=eps), 'Boosted momenta are not as expected'
109  assert math.isclose(t2.py, -0.002519848437500000083, rel_tol=eps), 'Boosted momenta are not as expected'
110  assert math.isclose(t2.pz, 5.1364143846516343572, rel_tol=eps), 'Boosted momenta are not as expected'
111 
112  fi.Close()