Belle II Software  release-06-02-00
lhereader.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # Test LHEReader and LHEInputModule
5 
6 import os
7 import math
8 from ROOT import TFile
9 from pdg import add_particle
10 from basf2 import create_path, register_module, process, print_params, find_file
11 from modularAnalysis import fillParticleListsFromMC
12 from modularAnalysis import variablesToNtuple as v2nt
13 from variables import variables as vm
14 from beamparameters import add_beamparameters
15 from tempfile import TemporaryDirectory
16 
17 # check that the file exists, if not: skip the test
18 inputfile = find_file('generators/tests/event.lhe')
19 if len(inputfile) == 0:
20  sys.stderr.write(
21  'TEST SKIPPED: input file ' +
22  filepath +
23  ' not found.')
24  sys.exit(-1)
25 
26 
27 # add the A' particle to the pdg database so we can specify it later
28 add_particle('A', 9000008, 5.5, 0.1329, 0, 0)
29 
30 # configure the LHE reade
31 lhereader = register_module('LHEInput')
32 lhereader.param('makeMaster', True)
33 lhereader.param('runNum', 1337)
34 lhereader.param('expNum', 0)
35 lhereader.param('inputFileList', [inputfile])
36 lhereader.param('useWeights', False)
37 lhereader.param('nInitialParticles', 2)
38 lhereader.param('nVirtualParticles', 0)
39 lhereader.param('wrongSignPz', True) # because Belle II convention is different to LEP etc
40 print_params(lhereader)
41 
42 # prepare the path
43 testpath = create_path()
44 testpath.add_module('Progress')
45 testpath.add_module(lhereader)
46 testpath.add_module('BoostMCParticles')
47 fillParticleListsFromMC([('gamma:gen', ''), ('A:gen', '')], path=testpath)
48 
49 # dump information from basf2
50 vm.addAlias('pxcms', 'useCMSFrame(px)')
51 vm.addAlias('pycms', 'useCMSFrame(py)')
52 vm.addAlias('pzcms', 'useCMSFrame(pz)')
53 vm.addAlias('pecms', 'useCMSFrame(E)')
54 variables = ['M', 'px', 'py', 'pz', 'E',
55  'pxcms', 'pycms', 'pzcms', 'pecms']
56 v2nt('A:gen', variables, 'darkphoton', 'test.root', path=testpath)
57 v2nt('gamma:gen', variables, 'gammas', 'test.root', path=testpath)
58 
59 # temporary directory to keep cwd clean
60 with TemporaryDirectory() as tmp:
61 
62  # process the basf2 path
63  os.chdir(tmp)
64  process(testpath)
65 
66  # open output and check the momenta are what is expected
67  fi = TFile('test.root')
68  t1 = fi.Get('darkphoton')
69  t2 = fi.Get('gammas')
70 
71  assert t1.GetEntries() == 1, 'Output contains %i entries' % t1.GetEntries()
72  assert t2.GetEntries() == 1, 'Output contains %i entries' % t1.GetEntries()
73 
74  t1.GetEntry(0)
75  t2.GetEntry(0)
76 
77  assert t1.__run__ == 1337, 'Run number not set correctly'
78  assert t1.__experiment__ == 0, 'Experiment number not set correctly'
79  assert t2.M == 0, 'Photon is not as expected'
80 
81  # a float is only 7 decimal digits of precision, we might get improvements
82  # to the variable manager precision later
83  eps = 1e-5
84  assert math.isclose(t1.M, 5.4571074540e+00, rel_tol=eps), 'Mass is not as expected'
85 
86  assert math.isclose(t1.pxcms, -t2.pxcms, rel_tol=eps), 'Momenta don\'t balance'
87  assert math.isclose(t1.pycms, -t2.pycms, rel_tol=eps), 'Momenta don\'t balance'
88  assert math.isclose(t1.pzcms, -t2.pzcms, rel_tol=eps), 'Momenta don\'t balance'
89 
90  assert math.isclose(t1.pxcms, -0.023859674786793544, rel_tol=eps), 'CMS momenta are not as expected'
91  assert math.isclose(t1.pycms, 0.0025198485236614943, rel_tol=eps), 'CMS momenta are not as expected'
92  assert math.isclose(t1.pzcms, -3.882552444880825, rel_tol=eps), 'CMS momenta are not as expected'
93  assert math.isclose(t1.pecms, 6.697373505125157, rel_tol=eps), 'CMS momenta are not as expected'
94 
95  assert t1.px == 0.24189484119415283, 'Boosted momenta are not as expected'
96  assert t1.py == 0.0025198485236614943, 'Boosted momenta are not as expected'
97  assert t1.pz == -2.136873722076416, 'Boosted momenta are not as expected'
98 
99  assert t2.px == 0.21474215388298035, 'Boosted momenta are not as expected'
100  assert t2.py == -0.0025198485236614943, 'Boosted momenta are not as expected'
101  assert t2.pz == 5.136414527893066, 'Boosted momenta are not as expected'
102 
103  fi.Close()