3 import os, subprocess, tarfile
4 from AthenaCommon
import Logging
5 from PowhegControl.utility
import LHE
15 __run_directory = os.environ[
'PATH']
18 __logger = Logging.logging.getLogger(
'Hto4lPowhegMerger')
21 _merger_executable =
'mergeHto4l4f.exe'
23 def __init__( self, runArgs=None, opts=None ) :
38 self.
logger.warning(
'No run arguments found! Using defaults.' )
57 genInputFiles = myinputfiles.split(
',')
58 numberOfFiles = len(genInputFiles)
62 for file
in genInputFiles:
65 if tarfile.is_tarfile(file):
66 tar = tarfile.open(file)
69 file_1 = file.replace(
"tar.gz.1",
"events")
70 self.
logger.
info(
'Extracted tar file, and renaming {0} to {1}'.format ( file, file_1 ) )
74 allFiles.append(file_1)
75 with open(file_1,
'r')
as f:
76 first_line = f.readline()
77 self.
logger.
info(
'first_line {0}'.format ( first_line ) )
78 if(
not (
"LesHouche" in first_line)):
79 raise RuntimeError(
"%s is NOT a LesHouche file" % file)
106 self.
logger.
info(
'Input files: {0} {1} {2}'.
format( hto4lLHE4e, hto4lLHE4mu, hto4lLHE2e2mu ) )
109 allFiles += [ hto4lLHE4e ]
110 allFiles += [ hto4lLHE4mu ]
111 allFiles += [ hto4lLHE2e2mu ]
125 """Post-process the LHE file to update the weights for a negative Hto4l weight.
127 1) check if the event weight (XWGTUP) is -1 (can be +/-1). If so, remember this so that the
128 weights in the <rwgt> block can be corrected by multiplying by -1
129 2) After correcting the weights in the <rwgt> block, overwrite XWGTUP by the weight at id=0.
130 Not completely sure if this step is necessary, but is done for Prophecy4f as well.
131 Note: we do NOT save the original XWGTUP as an extra weight (i.e. +/-1). Normally, the sign of
132 any weight should indicate this. The one exception is when BOTH PowHeg and Hto4l has a negative
133 weight and so all weights are positive. This is pretty rare.
135 #@param powheg_LHE_output Name of LHE file produced by merge of Hto4l files.
137 @author RD Schaffer <r.d.schaffer@cern.ch>
140 self.
logger.
info(
'Starting reweight_for_negative_weights' )
143 preamble = LHE.preamble(powheg_LHE_output)
144 postamble = LHE.postamble(powheg_LHE_output)
147 powheg_LHE_updated =
"{}.updated".
format(powheg_LHE_output)
148 with open(powheg_LHE_updated,
"wb")
as f_output:
149 f_output.write(
"{}".
format(preamble))
152 for input_event
in LHE.event_iterator(powheg_LHE_output):
158 input_lines = input_event.splitlines(
True)
161 NUP, IDPRUP, XWGTUP, SCALUP, AQEDUP, AQCDUP = input_lines[1].
split()
162 has_neg_wgt =
float(XWGTUP) < 0.
167 self.
logger.warning(
'could not get first line of event')
173 for input_line
in input_event.splitlines(
True):
176 if input_line.find(
"<wgt") >= 0:
177 weights = input_line.split()
180 wgt_tag, wid, wgt, wgt_tag_end = input_line.split()
184 output_line =
"{} {} {} {} \n".
format(wgt_tag, wid, wgt[1:], wgt_tag_end)
186 output_line =
"{} {} -{} {} \n".
format(wgt_tag, wid, wgt, wgt_tag_end)
192 output_event += output_line
if output_line
is not None else input_line
194 output_event = input_event
197 output_event_updated = LHE.update_XWGTUP_with_reweighted_nominal(output_event)
198 f_output.write(output_event_updated)
200 f_output.write(postamble)
204 shutil.move(powheg_LHE_output,
"{}.lhe_nominal_weight_updater_backup".
format(powheg_LHE_output))
205 shutil.move(powheg_LHE_updated, powheg_LHE_output)
220 @input_powheg_file_name.setter
245 @output_hto4l4e_file_name.setter
250 @output_hto4l4mu_file_name.setter
255 @output_hto4l2e2mu_file_name.setter
275 if(os.path.exists(outputFile)):
276 self.
logger.
info(
'outputFile {0} already exists. Will rename to {1}.OLD'.format ( outputFile, outputFile ) )
277 os.rename(outputFile,outputFile+
".OLD")
278 output =
open(outputFile,
'w')
281 for file
in listOfFiles:
282 cmd =
"grep /event "+file+
" | wc -l"
283 nevents+=
int(subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=
True))
285 for file
in listOfFiles:
288 self.
logger.
info(
'*** Starting to merge file {0}'.format ( file ) )
289 for line
in open(file,
"r"):
295 if(
"<event" in line
and inHeader):
297 if(len(holdHeader)<1):
303 elif(
not inHeader
and not (
"</LesHouchesEvents>" in line)):
307 if(
"nevents" in line):
309 tmp = line.split(
"=")
310 line = line.replace(tmp[0],
str(nevents))
311 elif(
"numevts" in line):
313 tmp = line.split(
" ")
315 line = line.replace(tmp[1],nnn)
317 output.write(
"</LesHouchesEvents>\n")