3 import os, subprocess, time
4 from DecoratorFactory
import decorate
5 from AthenaCommon
import Logging
6 from PowhegControl.utility
import HeartbeatTimer
14 __run_directory = os.environ[
'PWD']
17 __logger = Logging.logging.getLogger(
'ProphecyControl')
21 _prophecy_executable =
'Prophecy4f'
23 def __init__( self, runArgs=None, opts=None ) :
38 self.
logger.warning(
'No run arguments found! Using defaults.' )
41 if hasattr(runArgs,
'maxEvents')
and runArgs.maxEvents > 0 :
42 self.
nEvents =
int( 1.1 * runArgs.maxEvents + 0.5 )
43 if hasattr(runArgs,
'randomSeed') :
53 def generate( self, filter_name='', filter_args='' ) :
62 if hasattr( run_card_decorator,
'finalise' ) : run_card_decorator.finalise()
65 self.
logger.
info(
'** User configurable parameters for this process **' )
66 self.
logger.
info(
': Configurable parameter : Current value : Description' )
68 self.
logger.
info(
': {0:<22} : {1:>17} : {2}'.
format( value_tuple[0], getattr(self, value_tuple[0]), value_tuple[1] ) )
77 name, value, desc = parameter_tuple
79 if isinstance(value,list) :
82 output_line =
'{0:<30}! {1}'.
format(
'{0}={1}'.
format( name, value ), desc )
83 f.write(
'{0}\n'.
format(output_line) )
94 time_start = time.time()
95 self.
logger.
info(
'Starting Prophecy LHEF event generation at {0}'.
format( time.ctime( time_start ) ) )
99 heartbeat.setName(
"heartbeat thread")
100 heartbeat.daemon =
True
104 self.
logger.
info(
'Removing old Prophecy LHE files' )
106 os.remove(
'plot*.lhe plot*.ev*ts' )
112 if not os.path.exists(
'UNWEIGHTEDEVENTS'):
113 os.makedirs(
'UNWEIGHTEDEVENTS')
114 self.
logger.
info(
'Created directory UNWEIGHTEDEVENTS' )
125 generation_end = time.time()
126 elapsed_time = generation_end - time_start
127 self.
logger.
info(
'Running nominal Prophecy took {0} for {1} events => {2:6.3f} Hz'.
format( HeartbeatTimer.readable_duration(elapsed_time), self.
nEvents, self.
nEvents / elapsed_time ) )
129 self.
logger.
info(
'Removing Prophecy born LHE file' )
131 os.remove(
'plot_unweighted_born.lhe' )
140 self.
logger.warning(
'No output LHEF file found! Probably because the Prophecy process was killed before finishing.' )
148 def add_parameter( self, configurable_name, value, desc='', parameter=None ) :
149 setattr( self, configurable_name, value )
150 prophecy_parameter = parameter
if parameter
is not None else configurable_name
156 return decorate( self, parameter_set, **kwargs )
161 message =
'Heartbeat: Prophecy generation has been running for {0} in total'.
format( RepeatingTimer.human_readable_time_interval(duration) )
168 if value
is None : value = getattr( self, parameter )
172 if parameter == configurable_name_tuple[0] :
173 parameter, desc = prophecy_parameter, configurable_name_tuple[1]
179 configurator.running_process.append(subprocess.Popen( [configurator._prophecy_executable,
''], stdout=subprocess.PIPE, stdin=
open(configurator.run_card_path), stderr=subprocess.STDOUT ) )
181 while configurator.running_process :
183 for process
in configurator.running_process :
185 output = process.stdout.readline().rstrip()
186 if len(output) == 0 :
break
187 configurator.logger.info(
'{0}'.
format(output) )
188 if process.poll()
is not None :
190 process.stdout.flush()
192 output = process.stdout.readline().rstrip()
193 if len(output) == 0 :
break
194 configurator.logger.info(
'{0}'.
format(output) )
196 process.stdout.close()
197 configurator.running_process.remove( process )
198 configurator.logger.info(
'Prophecy finished - all done.' )
230 @output_events_file_name.setter
238 return self.__prophecy_directory