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 inputsPath = os.path.dirname(genInputFiles[0])
59 numberOfFiles = len(genInputFiles)
63 for file
in genInputFiles:
66 if tarfile.is_tarfile(file):
67 tar = tarfile.open(file)
68 tar.extractall(inputsPath)
70 file_1 = file.replace(
"tar.gz.1",
"events")
71 self.
logger.
info(
'Extracted tar file, and renaming {0} to {1}'.format ( file, file_1 ) )
75 allFiles.append(file_1)
76 with open(file_1,
'r')
as f:
77 first_line = f.readline()
78 self.
logger.
info(
'first_line {0}'.format ( first_line ) )
79 if(
not (
"LesHouche" in first_line)):
80 raise RuntimeError(
"%s is NOT a LesHouche file" % file)
107 self.
logger.
info(
'Input files: {0} {1} {2}'.
format( hto4lLHE4e, hto4lLHE4mu, hto4lLHE2e2mu ) )
110 allFiles += [ hto4lLHE4e ]
111 allFiles += [ hto4lLHE4mu ]
112 allFiles += [ hto4lLHE2e2mu ]
126 """Post-process the LHE file to update the weights for a negative Hto4l weight.
128 1) check if the event weight (XWGTUP) is -1 (can be +/-1). If so, remember this so that the
129 weights in the <rwgt> block can be corrected by multiplying by -1
130 2) After correcting the weights in the <rwgt> block, overwrite XWGTUP by the weight at id=0.
131 Not completely sure if this step is necessary, but is done for Prophecy4f as well.
132 Note: we do NOT save the original XWGTUP as an extra weight (i.e. +/-1). Normally, the sign of
133 any weight should indicate this. The one exception is when BOTH PowHeg and Hto4l has a negative
134 weight and so all weights are positive. This is pretty rare.
136 #@param powheg_LHE_output Name of LHE file produced by merge of Hto4l files.
138 @author RD Schaffer <r.d.schaffer@cern.ch>
141 self.
logger.
info(
'Starting reweight_for_negative_weights' )
144 preamble = LHE.preamble(powheg_LHE_output)
145 postamble = LHE.postamble(powheg_LHE_output)
148 powheg_LHE_updated =
"{}.updated".
format(powheg_LHE_output)
149 with open(powheg_LHE_updated,
"w")
as f_output:
150 f_output.write(
"{}".
format(preamble))
153 for input_event
in LHE.event_iterator(powheg_LHE_output):
159 input_lines = input_event.splitlines(
True)
162 NUP, IDPRUP, XWGTUP, SCALUP, AQEDUP, AQCDUP = input_lines[1].
split()
163 has_neg_wgt =
float(XWGTUP) < 0.
168 self.
logger.warning(
'could not get first line of event')
174 for input_line
in input_event.splitlines(
True):
177 if input_line.find(
"<wgt") >= 0:
178 weights = input_line.split()
181 wgt_tag, wid, wgt, wgt_tag_end = input_line.split()
185 output_line =
"{} {} {} {} \n".
format(wgt_tag, wid, wgt[1:], wgt_tag_end)
187 output_line =
"{} {} -{} {} \n".
format(wgt_tag, wid, wgt, wgt_tag_end)
193 output_event += output_line
if output_line
is not None else input_line
195 output_event = input_event
198 output_event_updated = LHE.update_XWGTUP_with_reweighted_nominal(output_event)
199 f_output.write(output_event_updated)
201 f_output.write(postamble)
205 shutil.move(powheg_LHE_output,
"{}.lhe_nominal_weight_updater_backup".
format(powheg_LHE_output))
206 shutil.move(powheg_LHE_updated, powheg_LHE_output)
221 @input_powheg_file_name.setter
246 @output_hto4l4e_file_name.setter
251 @output_hto4l4mu_file_name.setter
256 @output_hto4l2e2mu_file_name.setter
276 if(os.path.exists(outputFile)):
277 self.
logger.
info(
'outputFile {0} already exists. Will rename to {1}.OLD'.format ( outputFile, outputFile ) )
278 os.rename(outputFile,outputFile+
".OLD")
279 output =
open(outputFile,
'w')
282 for file
in listOfFiles:
283 cmd =
"grep /event "+file+
" | wc -l"
284 nevents+=
int(subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=
True))
286 for file
in listOfFiles:
289 self.
logger.
info(
'*** Starting to merge file {0}'.format ( file ) )
290 for line
in open(file,
"r"):
296 if(
"<event" in line
and inHeader):
298 if(len(holdHeader)<1):
304 elif(
not inHeader
and not (
"</LesHouchesEvents>" in line)):
308 if(
"nevents" in line):
310 tmp = line.split(
"=")
311 line = line.replace(tmp[0],
str(nevents))
312 elif(
"numevts" in line):
314 tmp = line.split(
" ")
316 line = line.replace(tmp[1],nnn)
318 output.write(
"</LesHouchesEvents>\n")