3 import os, subprocess, time
4 from .DecoratorFactory
import decorate
5 from AthenaCommon
import Logging
6 from PowhegControl.utility
import HeartbeatTimer, RepeatingTimer
14 __run_directory = os.environ[
'PWD']
23 __logger = Logging.logging.getLogger(
'Hto4lControl')
33 _hto4l_executable =
'Hto4l'
35 def __init__( self, runArgs=None, opts=None ) :
51 self.
logger.warning(
'No run arguments found! Using defaults.' )
54 if hasattr(runArgs,
'maxEvents')
and runArgs.maxEvents > 0 :
56 if hasattr(runArgs,
'randomSeed') :
62 def generate( self, filter_name='', filter_args='' ) :
71 if hasattr( run_card_decorator,
'finalise' ) : run_card_decorator.finalise()
74 self.
logger.
info(
'** User configurable parameters for this process **' )
75 self.
logger.
info(
': Configurable parameter : Current value : Description' )
79 self.
logger.
info(
': {0:<22} : {1:>17} : {2}'.
format( value_tuple[0], getattr(self, value_tuple[0]), value_tuple[1] ) )
89 name, value, desc = parameter_tuple
91 if isinstance(value,list) :
95 output_line =
'{0:<30}! {1}'.
format(
'{0}'.
format( value ), desc )
96 f.write(
'{0}\n'.
format(output_line) )
106 time_start = time.time()
107 self.
logger.
info(
'Starting Hto4l LHEF event generation at {0}'.
format( time.ctime( time_start ) ) )
112 heartbeat.setName(
"heartbeat thread")
113 heartbeat.daemon =
True
126 generation_end = time.time()
127 elapsed_time = generation_end - time_start
128 self.
logger.
info(
'Running nominal Hto4l took {0} for {1} events => {2:6.3f} Hz'.
format( HeartbeatTimer.readable_duration(elapsed_time), self.
nEvents, self.
nEvents / elapsed_time ) )
149 def add_parameter( self, configurable_name, value, desc='', parameter=None ) :
150 setattr( self, configurable_name, value )
151 hto4l_parameter = parameter
if parameter
is not None else configurable_name
157 return decorate( self, parameter_set, **kwargs )
162 message =
'Heartbeat: Hto4l generation has been running for {0} in total'.
format( RepeatingTimer.human_readable_time_interval(duration) )
170 if value
is None : value = getattr( self, parameter )
174 if parameter == configurable_name_tuple[0] :
175 parameter, desc = hto4l_parameter, configurable_name_tuple[1]
181 configurator.running_process.append(subprocess.Popen( [configurator._hto4l_executable,
''], stdout=subprocess.PIPE, stdin=
open(configurator.run_card_path), stderr=subprocess.STDOUT ) )
183 while configurator.running_process :
185 for process
in configurator.running_process :
187 output = process.stdout.readline().rstrip()
188 if len(output) == 0 :
break
189 configurator.logger.info(
'{0}'.
format(output) )
190 if process.poll()
is not None :
192 process.stdout.flush()
194 output = process.stdout.readline().rstrip()
195 if len(output) == 0 :
break
196 configurator.logger.info(
'{0}'.
format(output) )
198 process.stdout.close()
199 configurator.running_process.remove( process )
200 configurator.logger.info(
'Hto4l finished - all done.' )