6 from AthenaCommon
import Logging
7 LHEToolsLog = Logging.logging.getLogger(
'MCJobOptionUtils/LHETools.py')
19 self.__dict__.
update({field: self.FIELD_TYPES[field](value)
20 for field, value
in kwargs.items()})
24 if len(cls.FIELD_NAMES) != len(string.split()):
25 LHEToolsLog.warning(
"Inconsistent number of fields in lhe file line:")
26 LHEToolsLog.warning(string)
29 return cls(**dict(zip(cls.FIELD_NAMES, string.split())))
31 LHEToolsLog.warning(
"Impossible to interpret lhe file line:")
32 LHEToolsLog.warning(string)
36 return self.FMT.
format(**{field: getattr(self, field)
for field
in self.FIELD_NAMES})
if not self.
invalid else ""
41 FIELD_NAMES = [
'nparticles',
'id',
'weight',
'scale',
'A_QED',
'A_QCD']
54 FMT =
"{nparticles:7d} {id:6d} {weight: .5E} {scale: .5E} {A_QED: .5E} {A_QCD: .5E}"
60 'pdg_id',
'status',
'mother0',
'mother1',
'colour0',
'colour1',
'px',
61 'py',
'pz',
'e',
'm',
'lifetime',
'spin'
82 FMT =
"{pdg_id:8d} {status:5d} {mother0:5d} {mother1:5d} {colour0:5d} {colour1:5d} {px: .9E} {py: .9E} {pz: .9E} {e: .9E} {m: .9E} {lifetime: .5E} {spin: .3E}"
85 return (self.event.particles[mo]
86 for mo
in xrange(self.mother0 - 1, self.mother1))
89 self_id = self.
index() + 1
91 if p.mother0 <= self_id <= p.mother1:
95 return self.event.particles.index(self)
99 def __init__(self, info, particles, extra_lines=None, spurious_event_markup=False):
113 spurious_event_markup =
False
114 event_markup = re.compile(
'<event>')
116 for i, datum
in enumerate(lines):
118 if event_markup.match(datum):
119 LHEToolsLog.warning(
"Spurious <event> markup was found in the middle of an event record. Probably the result of one event being incompletely written.")
120 spurious_event_markup =
True
124 info = EventInfo.from_string(datum)
126 LHEToolsLog.warning(
"Could not retrieve EventInfo from input lhe file")
130 if 1 <= i <= info.nparticles:
131 p = Particle.from_string(datum)
133 LHEToolsLog.warning(
"Could not retrieve Particle from input lhe file")
139 extra_lines.append(datum)
141 return cls(info=info,
143 extra_lines=extra_lines,
144 spurious_event_markup=spurious_event_markup)
153 lines.append(p.to_string() +
'\n')
156 return ''.
join(lines)
162 if not isinstance(index, (int)):
163 LHEToolsLog.error(
"Index must be integer")
169 if not isinstance(index, (int)):
170 LHEToolsLog.error(
"Index must be integer")
194 mo0, mo1 = p_rm.mother0, p_rm.mother1
199 if p.mother0 == i_lhe
or p.mother1 == i_lhe:
200 if p.mother0 != p.mother1:
201 LHEToolsLog.error(
"Can not delete particle whose child has also other parents")
211 if p.mother0 > i_lhe:
213 if p.mother1 > i_lhe:
217 self.
info.nparticles -= 1
221 if inFileName ==
None:
222 LHEToolsLog.error(
"No input file for Duplicate finder")
226 LHEToolsLog.info(
"Possible duplicated events in lhe file will be printed out if any")
229 LHEToolsLog.info(
"Possible duplicated events in lhe file will be printed out if any")
232 fScript =
open(
"finder.awk",
'w')
234 """BEGIN{RS="<event>"; ORS=""; FS="</event>"; OFS=""; nDuplicates=0}
237 print $1 > \""""+inFileName+
"-new"+
"""\"
242 if (c[$1]==1) {print \"<event>\"$1\"</event>\" > \""""+inFileName+
"-new"+
"""\" }
243 else {print \"<event>\"$1\"</event>\" > \""""+inFileName+
"-duplicates"+
"""\"; nDuplicates++}
246 END{print \"</LesHouchesEvents>\\n\" > \""""+inFileName+
"-new"+
"""\"; print nDuplicates}""")
249 command =
'awk -f finder.awk '+inFileName+
" > nDuplicates.txt"
253 LHEToolsLog.error(
"Impossible to execute awk script")
257 fnDuplicates =
open(
"nDuplicates.txt",
'r')
258 s = fnDuplicates.read().rstrip()
262 fDuplicates =
open(inFileName+
"-duplicates",
'r')
264 LHEToolsLog.info(s+
" duplicate events were found in "+inFileName)
266 LHEToolsLog.info(
"Possible duplicated events in lhe file will be printed out if any")
267 for line
in fDuplicates:
268 LHEToolsLog.info(line)
272 LHEToolsLog.error(
"Duplicate events found. To avoid this crash, use crashmode=False. Exiting...")
275 LHEToolsLog.info(
"Duplicate events found. Replacing input file "+inFileName+
" by fixed file "+inFileName+
"-new")
276 LHEToolsLog.info(
"Old file saved in "+inFileName+
"-old")
277 os.rename(inFileName,inFileName+
"-old")
278 os.rename(inFileName+
"-new",inFileName)
280 LHEToolsLog.info(
"No duplicate events was found in "+inFileName)
281 os.remove(inFileName+
"-new")