3 import os, subprocess, time, tarfile
4 from AthenaCommon
import Logging
5 from PowhegControl.utility
import HeartbeatTimer
12 __run_directory = os.environ[
'PATH']
15 __logger = Logging.logging.getLogger(
'ProphecyPowhegMerger')
18 _merger_executable =
'mergeProphecy4f.exe'
20 def __init__( self, runArgs=None, opts=None ) :
35 self.
logger.warning(
'No run arguments found! Using defaults.' )
44 self.
logger.
info(
'Starting ProphecyPowhegMerger merge' )
52 myinputfiles = powhegLHE
53 genInputFiles = myinputfiles.split(
',')
54 numberOfFiles = len(genInputFiles)
58 for file
in genInputFiles:
61 if tarfile.is_tarfile(file):
62 tar = tarfile.open(file)
65 file_1 = file.replace(
"tar.gz.1",
"events")
66 self.
logger.
info(
'Extracted tar file, and renaming {0} to {1}'.format ( file, file_1 ) )
70 with open(file_1,
'r')
as f:
71 first_line = f.readline()
72 self.
logger.
info(
'first_line {0}'.format ( first_line ) )
73 if(
not (
"LesHouche" in first_line)):
74 raise RuntimeError(
"%s is NOT a LesHouche file" % file)
75 allFiles.append(file_1)
76 powhegLHE_input =
"merged_powheg_events.lhe"
80 time_start = time.time()
81 self.
logger.
info(
'Input files: {0} {1} {2} {3}'.
format( powhegLHE_input, prophecyLHE4e, prophecyLHE4mu, prophecyLHE2e2mu ) )
85 heartbeat.setName(
"heartbeat thread")
86 heartbeat.daemon =
True
91 if not os.path.isfile( powhegLHE_input ):
93 raise ValueError(
'File {0} does NOT exist.'.
format( powhegLHE_input ))
96 if not os.path.isfile( prophecyLHE4e ):
98 raise ValueError(
'File {0} does NOT exist.'.
format( prophecyLHE4e ))
101 if not os.path.isfile( prophecyLHE4mu ):
103 raise ValueError(
'File {0} does NOT exist.'.
format( prophecyLHE4mu ))
106 if not os.path.isfile( prophecyLHE2e2mu ):
108 raise ValueError(
'File {0} does NOT exist.'.
format( prophecyLHE2e2mu ))
113 os.rename( prophecyLHE4e, prophecyLHE4e +
'.tmp' )
115 self.
logger.
error(
'Moving of file {0} failed - not expected.'.
format( prophecyLHE4e ) )
118 os.rename( prophecyLHE4mu, prophecyLHE4mu +
'.tmp' )
120 self.
logger.
error(
'Moving of file {0} failed - not expected.'.
format( prophecyLHE4mu ) )
123 os.rename( prophecyLHE2e2mu, prophecyLHE2e2mu +
'.tmp' )
125 self.
logger.
error(
'Moving of file {0} failed - not expected.'.
format( prophecyLHE2e2mu ) )
129 self.
runMerging(powhegLHE_input, prophecyLHE4e +
'.tmp', prophecyLHE4mu +
'.tmp', prophecyLHE2e2mu +
'.tmp', random_seed)
134 generation_end = time.time()
135 elapsed_time = generation_end - time_start
136 self.
logger.
info(
'Running ProphecyPowhegMerger took {0}.'.
format( HeartbeatTimer.readable_duration(elapsed_time) ) )
138 self.
logger.
info(
'Removing initial LHE files of Prophecy and Powheg stored as *tmp.' )
145 def runMerging(configurator, powhegLHE, prophecyLHE4e, prophecyLHE4mu, prophecyLHE2e2mu, random_seed, stdin=None) :
146 configurator.logger.info(
'runMerging on {0}, {1}, {2} and {3}'.
format( powhegLHE, prophecyLHE4e, prophecyLHE4mu, prophecyLHE2e2mu ) )
147 if configurator.logger.level >= Logging.logging.DEBUG :
148 configurator.running_process.append(subprocess.Popen( [configurator._merger_executable,
'--inPowheg',powhegLHE,
'--inProphecy4e',prophecyLHE4e,
'--inProphecy4mu',prophecyLHE4mu,
'--inProphecy2e2mu',prophecyLHE2e2mu,
'--outLHE',configurator.output_events_file_name,
'--randomSeed',random_seed,
'--debug'], stdout=subprocess.PIPE, stdin=stdin, stderr=subprocess.STDOUT ) )
150 configurator.running_process.append(subprocess.Popen( [configurator._merger_executable,
'--inPowheg',powhegLHE,
'--inProphecy4e',prophecyLHE4e,
'--inProphecy4mu',prophecyLHE4mu,
'--inProphecy2e2mu',prophecyLHE2e2mu,
'--outLHE',configurator.output_events_file_name,
'--randomSeed',random_seed,
'--debug'], stdout=subprocess.PIPE, stdin=stdin, stderr=subprocess.STDOUT ) )
151 configurator.logger.info(
'runMerging run mergeProphecy4f: --inPowheg {0} --inProphecy4e {1} --inProphecy4mu {2} --inProphecy2e2mu {3} --outLHE {4} --randomSeed {5}'.
format( powhegLHE, prophecyLHE4e, prophecyLHE4mu, prophecyLHE2e2mu, configurator.output_events_file_name,random_seed) )
153 while configurator.running_process :
155 for process
in configurator.running_process :
157 output = process.stdout.readline().rstrip()
158 if len(output) == 0 :
break
159 configurator.logger.info(
'{0}'.
format(output) )
160 if process.poll()
is not None :
162 process.stdout.flush()
164 output = process.stdout.readline().rstrip()
165 if len(output) == 0 :
break
166 configurator.logger.info(
'{0}'.
format(output) )
168 process.stdout.close()
169 configurator.running_process.remove( process )
170 configurator.logger.info(
'Merging finished - all done.' )
183 @input_powheg_file_name.setter
206 @input_prophecy4e_file_name.setter
211 @input_prophecy4mu_file_name.setter
215 @input_prophecy2e2mu_file_name.setter
231 if(os.path.exists(outputFile)):
232 self.
logger.
info(
'outputFile {0} already exists. Will rename to {1}.OLD'.format ( outputFile, outputFile ) )
233 os.rename(outputFile,outputFile+
".OLD")
234 output =
open(outputFile,
'w')
237 for file
in listOfFiles:
238 cmd =
"grep /event "+file+
" | wc -l"
239 nevents+=
int(subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=
True))
241 for file
in listOfFiles:
244 self.
logger.
info(
'*** Starting to merge file {0}'.format ( file ) )
245 for line
in open(file,
"r"):
251 if(
"<event" in line
and inHeader):
253 if(len(holdHeader)<1):
259 elif(
not inHeader
and not (
"</LesHouchesEvents>" in line)):
263 if(
"nevents" in line):
265 tmp = line.split(
"=")
266 line = line.replace(tmp[0],
str(nevents))
267 elif(
"numevts" in line):
269 tmp = line.split(
" ")
271 line = line.replace(tmp[1],nnn)
273 output.write(
"</LesHouchesEvents>\n")