44 def convert(self):
45 '''
46 Modifies `events.lhe` output file from Madgraph by doing the following:
47 - Replaces init block
48 - Changes photon px and py to zero and energy to pz
49 - Recalculates energy scales as an average of lepton pair transverse momentum
50 '''
51 if not self.done:
52 DOMTree = xml.dom.minidom.parse(self.inFileName)
53 collection = DOMTree.documentElement
54
55
56 init = collection.getElementsByTagName('init')
57 init_repl = r'''
58 13 -13 2.510000e+03 2.510000e+03 0 0 0 0 3 1
59 1.000000e+00 0.000000e+00 1.000000e+00 9999
60 '''
61 init[0].firstChild.data = init_repl
62
63
64
65 event_header = r'^(\s*\S*\s*\S*\s*\S*\s*)(\S*)(.*)$'
66
67
68
69 event_particle = r'^(\s*)([0-9-]+)(\s+[0-9-]+\s+[0-9-]+\s+[0-9-]+\s+[0-9-]+\s+[0-9-]+\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(\s+)(\S+)(.*)$'
70
71 events = collection.getElementsByTagName('event')
72 for i, event in enumerate(events):
73
74
75 nodes = event.getElementsByTagName('mgrwt')
76 for node in nodes:
77 parent = node.parentNode
78 parent.removeChild(node)
79
80 new_particles = []
81 lepton_mom = []
82
83 particles = re.findall(event_particle, event.firstChild.data, re.MULTILINE)
84 for particle in particles:
85 particle = list(particle)
86 if particle[1] == '22':
87
88 particle[3] = '0.'
89 particle[5] = '0.'
90 particle[9] = particle[7]
91
92 if self.mode == 'Tauolapp':
93 if i%2 == 0:
94 particle[1] = '11' if float(particle[7]) > 0.0 else '-11'
95 else:
96 particle[1] = '-11' if float(particle[7]) > 0.0 else '11'
97 new_particles.append(''.join(particle))
98 elif particle[1] in self.leptons:
99
100 lepton_mom.append(float(particle[3]))
101 lepton_mom.append(float(particle[5]))
102 new_particles.append(''.join(particle))
103 else:
104 new_particles.append(''.join(particle))
105
106
107 l1_px, l1_py, l2_px, l2_py = lepton_mom
108 l1_pt = math.sqrt(l1_px**2 + l1_py**2)
109 l2_pt = math.sqrt(l2_px**2 + l2_py**2)
110 energy_scale = f'{(l1_pt + l2_pt) / 2.:.9E}'
111
112 header = re.search(event_header, event.firstChild.data, re.MULTILINE)
113 header = list(header.groups())
114 header[1] = energy_scale
115
116 if self.generator == 'Superchic':
117 event.firstChild.data = '\n'.join([''.join(header)] + new_particles) + '\n'
118 if self.generator == 'Madgraph5':
119 event.firstChild.data = '\n'.join([''.join(header)] + new_particles)
120
121 with open(self.outFileName, 'w') as output:
122 output.write(DOMTree.toxml().
replace(
'<?xml version="1.0" ?>',
' '))
123
124 self.done = True
125
126 return StatusCode.Success
127
std::string replace(std::string s, const std::string &s2, const std::string &s3)