Add a series of additional weights to an LHE file.
38 def reweighter(process, weight_groups, powheg_LHE_output):
39 """! Add a series of additional weights to an LHE file.
41 @param process PowhegBox process.
42 @param weight_groups OrderedDict containing groups of weights to add.
43 @param powheg_LHE_output Name of LHE file produced by PowhegBox.
45 logger.info(
"Starting to run PowhegControl PDF and QCD scale reweighter")
50 non_weight_attributes = [
"parameter_names",
"combination_method",
"keywords"]
53 xml_kwds = {
"renscfact":
"renscfact",
"facscfact":
"facscfact",
"lhans1":
"lhapdf",
"lhans2":
"lhapdf"}
56 _idx_scale_start, _idx_PDF_start, _idx_other_start = 1001, 2001, 3001
58 if not process.use_XML_reweighting:
59 logger.warning(
"... XML-style reweighting is not enabled for this process. Will use old (multiple-pass) method.")
62 for group_name, weight_group
in weight_groups.items():
63 logger.info(
"Preparing weight group: {:<19} with {} weights".
format(group_name, len(weight_group) - len(non_weight_attributes)))
66 is_parallel_xml_compatible = process.use_XML_reweighting
and all([k
in xml_kwds.keys()
for _kw_set
in weight_group[
"keywords"]
for k
in _kw_set])
67 if is_parallel_xml_compatible:
68 _keywords = [
list(
set([xml_kwds[k]
for k
in _kw_set]))
for _kw_set
in weight_group[
"keywords"]]
70 if process.use_XML_reweighting:
71 logger.warning(
"... this weight group is incompatible with XML-style reweighting. Will use old (multiple-pass) method.")
72 _keywords = weight_group[
"keywords"]
73 keyword_dict = dict((n, k)
for n, k
in zip(weight_group[
"parameter_names"], _keywords))
76 tuple_kwargs = {
"keywords": keyword_dict,
"combine": weight_group[
"combination_method"],
"group": group_name,
"parallel_xml_compatible": is_parallel_xml_compatible}
80 weight_list.append(
WeightTuple(parameter_settings=weight_group[
"nominal"], ID=0, name=
"nominal", **tuple_kwargs))
83 elif group_name ==
"scale_variation":
84 for idx, name
in enumerate([k
for k
in weight_group.keys()
if k
not in non_weight_attributes], start=_idx_scale_start):
85 weight_list.append(
WeightTuple(parameter_settings=weight_group[name], ID=idx, name=name, **tuple_kwargs))
86 _idx_scale_start = idx + 1
89 elif group_name ==
"PDF_variation":
90 for idx, name
in enumerate([k
for k
in weight_group.keys()
if k
not in non_weight_attributes], start=_idx_PDF_start):
91 weight_list.append(
WeightTuple(parameter_settings=weight_group[name], ID=idx, name=name, **tuple_kwargs))
92 _idx_PDF_start = idx + 1
96 for idx, name
in enumerate([k
for k
in weight_group.keys()
if k
not in non_weight_attributes], start=_idx_other_start):
97 weight_list.append(
WeightTuple(parameter_settings=weight_group[name], ID=idx, name=name, **tuple_kwargs))
98 _idx_other_start = idx + 1
101 if os.path.isfile(
"pwgcounters.dat"):
102 shutil.copy(
"pwgcounters.dat",
"pwgcounters.dat.bak")
106 shutil.copy(
"powheg.input",
"powheg.input.before_reweighting")
108 raise IOError(
"Powheg input card ('powheg.input') cannot be found at the start of reweighting. In a normal setup, PowhegControl generates this input card. Its absence is probably a sign of problems --- please investigate or contact the PowhegControl experts.")
112 shutil.copy(powheg_LHE_output,
"{}.before_reweighting".
format(powheg_LHE_output))
114 raise IOError(
"Nominal LHE file could not be found. Probably POWHEG-BOX crashed during event generation.")
117 if process.use_XML_reweighting:
120 weight_list = [
WeightTuple(ID=0, name=
"nominal", group=
"nominal", parallel_xml_compatible=
True, parameter_settings=[], keywords=
None, combine=
None)] + weight_list
122 FileParser(
"powheg.input").text_replace(
"pdfreweight .*",
"pdfreweight 0")
124 xml_lines, serial_xml_weight_list, current_weightgroup = [], [],
None
125 for weight
in weight_list:
127 if weight.parallel_xml_compatible
and weight.group != current_weightgroup:
128 xml_lines.append(
"</weightgroup>")
129 current_weightgroup = weight.group
130 xml_lines.append(
"<weightgroup name='{}' combine='{}'>".
format(weight.group, weight.combine))
132 if weight.parallel_xml_compatible:
133 keyword_pairs_str =
" ".
join([
"{}={}".
format(k, v)
for p, v
in weight.parameter_settings
for k
in weight.keywords[p]])
134 xml_lines.append(
"<weight id='{}'> {} </weight>".
format(weight.ID, keyword_pairs_str))
136 serial_xml_weight_list.append(weight)
137 xml_lines.append(xml_lines.pop(0))
138 n_parallel_xml_weights = len(weight_list) - len(serial_xml_weight_list)
141 if n_parallel_xml_weights > 0:
143 with open(
"reweighting_input.xml",
"w")
as f_rwgt:
144 f_rwgt.write(
"<initrwgt>\n")
145 [f_rwgt.write(
"{}\n".
format(xml_line))
for xml_line
in xml_lines]
146 f_rwgt.write(
"</initrwgt>")
151 FileParser(
"powheg.input").text_replace(
"rwl_file .*",
"rwl_file 'reweighting_input.xml'")
152 FileParser(
"powheg.input").text_replace(
"rwl_add .*",
"rwl_add 1")
153 FileParser(
"powheg.input").text_replace(
"clobberlhe .*",
"clobberlhe 1")
155 logger.info(
"Preparing simultaneous calculation of {} additional weights for generated events.".
format(n_parallel_xml_weights))
158 if list(process.parameters_by_name(
"manyseeds"))[0].value == 1:
159 if process.powheg_version ==
"RES":
160 os.system(
'cp reweighting_input.xml backup_of_reweighting_input.xml')
161 os.system(
'cp powheg.input powheg.input.for_reweighting')
164 FileParser(
"powheg.input").text_replace(
"manyseeds .*",
"manyseeds 0")
165 FileParser(
"powheg.input").text_replace(
"parallelstage .*",
"parallelstage -1")
166 os.system(
'cp reweighting_input.xml backup_of_reweighting_input.xml')
167 os.system(
'cp powheg.input powheg.input.for_reweighting')
170 os.system(
'cp reweighting_input.xml backup_of_reweighting_input.xml')
171 os.system(
'cp powheg.input powheg.input.for_reweighting')
178 if len(serial_xml_weight_list) > 0:
179 logger.info(
"Preparing individual calculation of {} additional weights for generated events.".
format(len(serial_xml_weight_list)))
180 shutil.move(
"reweighting_input.xml",
"reweighting_input.nominal")
181 for idx_weight, weight
in enumerate(serial_xml_weight_list, start=1):
182 add_single_weight(process, weight, idx_weight, len(serial_xml_weight_list), use_XML=
True)
184 shutil.move(
"reweighting_input.nominal",
"reweighting_input.xml")
189 logger.info(
"Preparing individual calculation of {} additional weights for generated events.".
format(len(weight_list)))
190 for idx_weight, weight
in enumerate(weight_list, start=1):
194 if process.use_XML_reweighting:
195 comment_patterns = [
"#pdf",
"#rwgt",
"#new weight",
"#matching",
" #Random" ]
196 if process.remove_oldStyle_rwt_comments:
198 logger.info(
"Removing comment lines from lhe file - these lines can be added back using the 'remove_oldStyle_rwt_comments=False' argument in generate()")
200 for pattern
in comment_patterns:
201 removed = FileParser(powheg_LHE_output).text_remove(
"^"+pattern)
202 logger.info(
"{} line(s) starting with '{}' were removed from {}".
format(removed, pattern, powheg_LHE_output))
204 logger.info(
"Fixing comment lines from lhe file - these lines can be simply removed using the 'remove_oldStyle_rwt_comments=True' argument in generate()")
205 for pattern
in comment_patterns:
210 for weight
in weight_list:
211 replacelist += [[
".* id='{}' .*".
format(weight.ID),
"<weight id='{weight_id}'>{weight_name}</weight>".
format(weight_id=weight.ID, weight_name=weight.name), 1]]
214 replacelist += [[
'LesHouchesEvents version="1.0"',
'LesHouchesEvents version="3.0"', 1]]
215 FileParser(powheg_LHE_output).text_replace_multi(replacelist)
218 shutil.move(
"powheg_nominal.input",
"powheg.input")
219 if os.path.isfile(
"pwgcounters.dat.bak"):
220 shutil.move(
"pwgcounters.dat.bak",
"pwgcounters.dat")