-
Notifications
You must be signed in to change notification settings - Fork 22
/
__init__.py
390 lines (332 loc) · 12.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import gzip
import xml.etree.ElementTree as ET
import graphviz
from particle import latex_to_html_name
from particle.converters.bimap import DirectionalMaps
from particle.exceptions import MatchingIDNotFound
from pylhe._version import version as __version__
from pylhe.awkward import register_awkward, to_awkward
__all__ = [
"__version__",
"LHEEvent",
"LHEEventInfo",
"LHEFile",
"LHEInit",
"LHEParticle",
"LHEProcInfo",
"read_lhe",
"read_lhe_init",
"read_lhe_with_attributes",
"read_num_events",
"register_awkward",
"to_awkward",
]
def __dir__():
return __all__
# retrieve mapping of PDG ID to particle name as LaTeX string
_PDGID2LaTeXNameMap, _ = DirectionalMaps("PDGID", "LATEXNAME", converters=(int, str))
class LHEFile:
def __init__(self):
pass
class LHEEvent:
def __init__(
self, eventinfo, particles, weights=None, attributes=None, optional=None
):
self.eventinfo = eventinfo
self.particles = particles
self.weights = weights
self.attributes = attributes
self.optional = optional
for p in self.particles:
p.event = self
self._graph = None
@property
def graph(self):
"""
Get the `graphviz.Digraph` object.
The user now has full control ...
E.g., see the source with my_LHEEvent_instance.graph.source.
When not in notebooks the graph can easily be visualized with the
`graphviz.Digraph.render` or `graphviz.Digraph.view` functions, e.g.:
my_LHEEvent_instance.graph.render(filename="test", format="pdf", view=True, cleanup=True)
"""
if self._graph is None:
self._build_graph()
return self._graph
def _build_graph(self):
"""
Navigate the particles in the event and produce a Digraph in the DOT language.
"""
def safe_html_name(name):
"""
Get a safe HTML name from the LaTex name.
"""
try:
return latex_to_html_name(name)
except Exception:
return name
self._graph = graphviz.Digraph()
for i, p in enumerate(self.particles):
try:
iid = int(p.id)
name = _PDGID2LaTeXNameMap[iid]
texlbl = f"${name}$"
label = f'<<table border="0" cellspacing="0" cellborder="0"><tr><td>{safe_html_name(name)}</td></tr></table>>'
except MatchingIDNotFound:
texlbl = str(int(p.id))
label = f'<<table border="0" cellspacing="0" cellborder="0"><tr><td>{texlbl}</td></tr></table>>'
self._graph.node(
str(i), label=label, attr_dict=str(p.__dict__), texlbl=texlbl
)
for i, p in enumerate(self.particles):
for mom in p.mothers():
self._graph.edge(str(self.particles.index(mom)), str(i))
def _repr_mimebundle_(
self,
include=None,
exclude=None,
**kwargs,
):
"""
IPython display helper.
"""
try:
return self.graph._repr_mimebundle_(
include=include, exclude=exclude, **kwargs
)
except AttributeError:
return {"image/svg+xml": self.graph._repr_svg_()} # for graphviz < 0.19
class LHEEventInfo:
fieldnames = ["nparticles", "pid", "weight", "scale", "aqed", "aqcd"]
def __init__(self, **kwargs):
if set(kwargs.keys()) != set(self.fieldnames):
raise RuntimeError(
f"LHEEventInfo constructor expects fields {self.fieldnames}! Got {kwargs.keys()}."
)
for k, v in kwargs.items():
setattr(self, k, v)
@classmethod
def fromstring(cls, string):
return cls(**dict(zip(cls.fieldnames, map(float, string.split()))))
class LHEParticle:
fieldnames = [
"id",
"status",
"mother1",
"mother2",
"color1",
"color2",
"px",
"py",
"pz",
"e",
"m",
"lifetime",
"spin",
]
def __init__(self, **kwargs):
if set(kwargs.keys()) != set(self.fieldnames):
raise RuntimeError(
f"LHEParticle constructor expects fields {self.fieldnames}! Got {kwargs.keys()}."
)
for k, v in kwargs.items():
setattr(self, k, v)
@classmethod
def fromstring(cls, string):
return cls(**dict(zip(cls.fieldnames, map(float, string.split()))))
def mothers(self):
first_idx = int(self.mother1) - 1
second_idx = int(self.mother2) - 1
return [
self.event.particles[idx] for idx in {first_idx, second_idx} if idx >= 0
]
class LHEInit(dict):
"""Store the <init> block as dict."""
fieldnames = [
"beamA",
"beamB",
"energyA",
"energyB",
"PDFgroupA",
"PDFgroupB",
"PDFsetA",
"PDFsetB",
"weightingStrategy",
"numProcesses",
]
def __init__(self):
pass
@classmethod
def fromstring(cls, string):
return dict(zip(cls.fieldnames, map(float, string.split())))
class LHEProcInfo(dict):
"""Store the process info block as dict."""
fieldnames = ["xSection", "error", "unitWeight", "procId"]
def __init__(self):
pass
@classmethod
def fromstring(cls, string):
return dict(zip(cls.fieldnames, map(float, string.split())))
def _extract_fileobj(filepath):
"""
Checks to see if a file is compressed, and if so, extract it with gzip
so that the uncompressed file can be returned.
It returns a file object containing XML data that will be ingested by
``xml.etree.ElementTree.iterparse``.
Args:
filepath: A path-like object or str.
Returns:
_io.BufferedReader or gzip.GzipFile: A file object containing XML data.
"""
with open(filepath, "rb") as gzip_file:
header = gzip_file.read(2)
gzip_magic_number = b"\x1f\x8b"
return (
gzip.GzipFile(filepath) if header == gzip_magic_number else open(filepath, "rb")
)
def read_lhe_init(filepath):
"""
Read and return the init blocks. This encodes the weight group
and related things according to https://arxiv.org/abs/1405.1067
Args:
filepath: A path-like object or str.
Returns:
dict: Dictionary containing the init blocks of the LHE file.
"""
initDict = {}
with _extract_fileobj(filepath) as fileobj:
for event, element in ET.iterparse(fileobj, events=["start", "end"]):
if element.tag == "init":
data = element.text.split("\n")[1:-1]
initDict["initInfo"] = LHEInit.fromstring(data[0])
initDict["procInfo"] = [LHEProcInfo.fromstring(d) for d in data[1:]]
if element.tag == "initrwgt":
initDict["weightgroup"] = {}
index = 0
for child in element:
# Find all weightgroups
if child.tag == "weightgroup" and child.attrib != {}:
try:
wg_type = child.attrib["type"]
except KeyError:
try:
wg_type = child.attrib["name"]
except KeyError:
print(
"weightgroup must have attribute 'type' or 'name'"
)
raise
_temp = {"attrib": child.attrib, "weights": {}}
# Iterate over all weights in this weightgroup
for w in child:
if w.tag != "weight":
continue
try:
wg_id = w.attrib["id"]
except KeyError:
print("weight must have attribute 'id'")
raise
_temp["weights"][wg_id] = {
"attrib": w.attrib,
"name": w.text.strip(),
"index": index,
}
index += 1
initDict["weightgroup"][wg_type] = _temp
if element.tag == "LesHouchesEvents":
initDict["LHEVersion"] = float(element.attrib["version"])
if element.tag == "event":
break
return initDict
def read_lhe(filepath):
try:
with _extract_fileobj(filepath) as fileobj:
for event, element in ET.iterparse(fileobj, events=["end"]):
if element.tag == "event":
data = element.text.strip().split("\n")
eventdata, particles = data[0], data[1:]
eventinfo = LHEEventInfo.fromstring(eventdata)
particles = particles[: int(eventinfo.nparticles)]
particle_objs = [LHEParticle.fromstring(p) for p in particles]
yield LHEEvent(eventinfo, particle_objs)
except ET.ParseError as excep:
print("WARNING. Parse Error:", excep)
return
def _get_index_to_id_map(init):
"""
Produce a dictionary to map weight indices to the id of the weight.
It is used for LHE files where there is only a list of weights per event.
This dictionary is then used to map the list of weights to their weight id.
Ideally, this needs to be done only once and the dictionary can be reused.
Args:
init (dict): init block as returned by read_lhe_init
Returns:
dict: {weight index: weight id}
"""
ret = {}
for wg in init["weightgroup"].values():
for id, w in wg["weights"].items():
ret[w["index"]] = id
return ret
def read_lhe_with_attributes(filepath):
"""
Iterate through file, similar to read_lhe but also set
weights and attributes.
"""
index_map = None
try:
with _extract_fileobj(filepath) as fileobj:
for event, element in ET.iterparse(fileobj, events=["end"]):
if element.tag == "event":
eventdict = {}
data = element.text.strip().split("\n")
eventdata, particles = data[0], data[1:]
eventdict["eventinfo"] = LHEEventInfo.fromstring(eventdata)
eventdict["particles"] = []
eventdict["weights"] = {}
eventdict["attrib"] = element.attrib
eventdict["optional"] = []
for p in particles:
if not p.strip().startswith("#"):
eventdict["particles"] += [LHEParticle.fromstring(p)]
else:
eventdict["optional"].append(p.strip())
for sub in element:
if sub.tag == "weights":
if not index_map:
index_map = _get_index_to_id_map(
read_lhe_init(filepath)
)
for i, w in enumerate(sub.text.split()):
if w and not index_map[i] in eventdict["weights"]:
eventdict["weights"][index_map[i]] = float(w)
if sub.tag == "rwgt":
for r in sub:
if r.tag == "wgt":
eventdict["weights"][r.attrib["id"]] = float(
r.text.strip()
)
# yield eventdict
yield LHEEvent(
eventdict["eventinfo"],
eventdict["particles"],
eventdict["weights"],
eventdict["attrib"],
eventdict["optional"],
)
except ET.ParseError as excep:
print("WARNING. Parse Error:", excep)
return
def read_num_events(filepath):
"""
Moderately efficient way to get the number of events stored in a file.
"""
try:
with _extract_fileobj(filepath) as fileobj:
return sum(
element.tag == "event"
for event, element in ET.iterparse(fileobj, events=["end"])
)
except ET.ParseError as excep:
print("WARNING. Parse Error:", excep)
return -1