ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Generators
Prophecy4fControl
python
ProphecyConfig.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2
3
import
os, subprocess, time
4
from
DecoratorFactory
import
decorate
5
from
AthenaCommon
import
Logging
6
from
PowhegControl.utility
import
HeartbeatTimer
7
8
9
12
class
ProphecyConfig
(object) :
13
14
__run_directory = os.environ[
'PWD'
]
15
16
17
__logger = Logging.logging.getLogger(
'ProphecyControl'
)
18
19
21
_prophecy_executable =
'Prophecy4f'
22
23
def
__init__
( self, runArgs=None, opts=None ) :
24
25
26
self.
__output_events_file_name
=
'ProphecyOTF._1.events'
27
28
29
self.
__fixed_parameters
= []
30
self.
__configurable_parameters
= {}
31
self.
__run_card_decorators
= []
32
33
34
self.
add_parameter_set
(
'fromDefault'
)
35
36
37
if
runArgs
is
None
:
38
self.
logger
.warning(
'No run arguments found! Using defaults.'
)
39
else
:
40
# Read values from runArgs
41
if
hasattr(runArgs,
'maxEvents'
)
and
runArgs.maxEvents > 0 :
42
self.
nEvents
= int( 1.1 * runArgs.maxEvents + 0.5 )
43
if
hasattr(runArgs,
'randomSeed'
) :
44
self.
random_seed
= runArgs.randomSeed
45
46
if
100 * self.
nEvents
> self.
nEvents_weighted
:
47
self.
logger
.warning(
'There are {0} events requested with {1} weighted ones.'
.format( self.
nEvents
, self.
nEvents_weighted
) )
48
self.
nEvents_weighted
= 100 * self.
nEvents
49
self.
logger
.warning(
'Forcing weighted events to be {0}.'
.format( self.
nEvents_weighted
) )
50
51
52
53
def
generate
( self, filter_name='', filter_args='' ) :
54
self.
generateRunCard
()
55
self.
generateEvents
()
56
57
58
def
generateRunCard
(self) :
59
60
61
for
run_card_decorator
in
self.
run_card_decorators
:
62
if
hasattr( run_card_decorator,
'finalise'
) : run_card_decorator.finalise()
63
64
65
self.
logger
.info(
'** User configurable parameters for this process **'
)
66
self.
logger
.info(
': Configurable parameter : Current value : Description'
)
67
for
value_tuple
in
sorted( self.
configurable_parameters
.values(), key=
lambda
x: x[0].lower() ) :
68
self.
logger
.info(
': {0:<22} : {1:>17} : {2}'
.format( value_tuple[0], getattr(self, value_tuple[0]), value_tuple[1] ) )
69
70
71
[ self.
fix_parameter
( parameter=value_tuple[0], desc=value_tuple[1] )
for
value_tuple
in
self.
configurable_parameters
.values() ]
72
73
74
self.
logger
.info(
'Writing Prophecy runcard to {0}'
.format( self.
run_card_path
) )
75
with
open( self.
run_card_path
,
'w'
)
as
f :
76
for
parameter_tuple
in
sorted( self.
fixed_parameters
, key=
lambda
x: x[0].lower() ) :
77
name, value, desc = parameter_tuple
78
# Set starting value to first in list when multiple values are provided
79
if
isinstance(value,list) :
80
value = value[0]
81
self.
__enable_reweighting
=
True
82
output_line =
'{0:<30}! {1}'
.format(
'{0}={1}'
.format( name, value ), desc )
83
f.write(
'{0}\n'
.format(output_line) )
84
self.
logger
.info(
'Wrote {0}'
.format( output_line ) )
85
86
87
self.
logger
.info(
'Using executable: {0}'
.format( self.
_prophecy_executable
) )
88
self.
logger
.info(
'Number of events and weight events: {0} {1}.'
.format( self.
nEvents
, self.
nEvents_weighted
) )
89
90
91
92
def
generateEvents
(self) :
93
94
time_start = time.time()
95
self.
logger
.info(
'Starting Prophecy LHEF event generation at {0}'
.format( time.ctime( time_start ) ) )
96
97
98
heartbeat = HeartbeatTimer(600.,
"{}/eventLoopHeartBeat.txt"
.format(self.
__run_directory
))
99
heartbeat.setName(
"heartbeat thread"
)
100
heartbeat.daemon =
True
# Allow program to exit if this is the only live thread
101
heartbeat.start()
102
103
104
self.
logger
.info(
'Removing old Prophecy LHE files'
)
105
try
:
106
os.remove(
'plot*.lhe plot*.ev*ts'
)
107
except
OSError :
108
pass
109
110
112
if
not
os.path.exists(
'UNWEIGHTEDEVENTS'
):
113
os.makedirs(
'UNWEIGHTEDEVENTS'
)
114
self.
logger
.info(
'Created directory UNWEIGHTEDEVENTS'
)
115
116
117
self.
running_process
= []
118
119
self.
runProphecy
()
120
121
122
heartbeat.cancel()
123
124
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 ) )
128
129
self.
logger
.info(
'Removing Prophecy born LHE file'
)
130
try
:
131
os.remove(
'plot_unweighted_born.lhe'
)
132
except
OSError :
133
pass
134
135
136
try
:
137
os.rename(
'UNWEIGHTEDEVENTS/plot_unweighted.lhe'
, self.
output_events_file_name
)
138
self.
logger
.info(
'Moved plot_unweighted.lhe to {0}'
.format(self.
output_events_file_name
) )
139
except
OSError :
140
self.
logger
.warning(
'No output LHEF file found! Probably because the Prophecy process was killed before finishing.'
)
141
142
143
self.
logger
.info(
'Finished at {0}'
.format( time.asctime() ) )
144
return
145
146
147
148
def
add_parameter
( self, configurable_name, value, desc='', parameter=None ) :
149
setattr( self, configurable_name, value )
# add new attribute
150
prophecy_parameter = parameter
if
parameter
is
not
None
else
configurable_name
151
self.
configurable_parameters
[prophecy_parameter] = ( configurable_name, desc )
152
153
154
155
def
add_parameter_set
( self, parameter_set, **kwargs ) :
156
return
decorate( self, parameter_set, **kwargs )
157
158
159
160
def
emit_heartbeat
(self, duration) :
161
message =
'Heartbeat: Prophecy generation has been running for {0} in total'
.format( RepeatingTimer.human_readable_time_interval(duration) )
# noqa: F821
162
self.
logger
.info( message )
163
with
open(
'{0}/eventLoopHeartBeat.txt'
.format( self.
__run_directory
),
'w'
)
as
f : f.write( message )
164
165
166
def
fix_parameter
( self, parameter, value=None, desc='' ) :
167
# Get previously set value if not overwriting
168
if
value
is
None
: value = getattr( self, parameter )
169
# Remove it from the configurable list if it was there
170
for
prophecy_parameter, configurable_name_tuple
in
self.
configurable_parameters
.items() :
171
# Retrieve Prophecy parameter name and description if there is a match
172
if
parameter == configurable_name_tuple[0] :
173
parameter, desc = prophecy_parameter, configurable_name_tuple[1]
174
self.
configurable_parameters
.pop(prophecy_parameter)
175
break
176
self.
fixed_parameters
.append( (parameter, value, desc) )
177
178
def
runProphecy
(configurator) :
179
configurator.running_process.append(subprocess.Popen( [configurator._prophecy_executable,
''
], stdout=subprocess.PIPE, stdin=open(configurator.run_card_path), stderr=subprocess.STDOUT ) )
180
181
while
configurator.running_process :
182
# Write output buffer if any
183
for
process
in
configurator.running_process :
184
while
True
:
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
:
# process has ended
189
# Flush buffer and print final output (if any) to screen
190
process.stdout.flush()
191
while
True
:
192
output = process.stdout.readline().rstrip()
193
if
len(output) == 0 :
break
194
configurator.logger.info(
'{0}'
.format(output) )
195
# Close output stream and remove process from list
196
process.stdout.close()
197
configurator.running_process.remove( process )
198
configurator.logger.info(
'Prophecy finished - all done.'
)
199
200
201
@property
202
def
run_directory
(self) :
203
return
self.
__run_directory
204
205
206
207
@property
208
def
configurable_parameters
(self) :
209
return
self.
__configurable_parameters
210
211
212
213
@property
214
def
fixed_parameters
(self) :
215
return
self.
__fixed_parameters
216
217
218
219
@property
220
def
logger(self) :
221
return
self.
__logger
222
223
224
225
@property
226
def
output_events_file_name
(self) :
227
return
self.
__output_events_file_name
228
229
230
@output_events_file_name.setter
231
def
output_events_file_name
(self, value) :
232
self.
__output_events_file_name
= value
233
234
235
236
@property
237
def
prophecy_directory
(self) :
238
return
self.__prophecy_directory
239
240
241
242
@property
243
def
run_card_decorators
(self) :
244
return
self.
__run_card_decorators
245
246
247
248
@property
249
def
run_card_path
(self) :
250
return
'{0}/prophecy.input'
.format( self.
run_directory
)
251
python.ProphecyConfig.ProphecyConfig
Base class for configurable objects in the jobOptions.
Definition
ProphecyConfig.py:12
python.ProphecyConfig.ProphecyConfig.prophecy_directory
prophecy_directory(self)
Get Prophecy directory.
Definition
ProphecyConfig.py:236
python.ProphecyConfig.ProphecyConfig.generateRunCard
generateRunCard(self)
Initialise runcard with generic options.
Definition
ProphecyConfig.py:57
python.ProphecyConfig.ProphecyConfig.add_parameter
add_parameter(self, configurable_name, value, desc='', parameter=None)
Register configurable parameter.
Definition
ProphecyConfig.py:147
python.ProphecyConfig.ProphecyConfig.generateEvents
generateEvents(self)
Run normal event generation.
Definition
ProphecyConfig.py:91
python.ProphecyConfig.ProphecyConfig.__fixed_parameters
list __fixed_parameters
Set up lists of parameters and decorators.
Definition
ProphecyConfig.py:28
python.ProphecyConfig.ProphecyConfig.nEvents
nEvents
Add universal functionality.
Definition
ProphecyConfig.py:41
python.ProphecyConfig.ProphecyConfig.nEvents_weighted
int nEvents_weighted
Print final preparation message.
Definition
ProphecyConfig.py:45
python.ProphecyConfig.ProphecyConfig.add_parameter_set
add_parameter_set(self, parameter_set, **kwargs)
Alias to DecoratorFactory.decorate.
Definition
ProphecyConfig.py:154
python.ProphecyConfig.ProphecyConfig.__run_card_decorators
list __run_card_decorators
Definition
ProphecyConfig.py:30
python.ProphecyConfig.ProphecyConfig.__init__
__init__(self, runArgs=None, opts=None)
Definition
ProphecyConfig.py:22
python.ProphecyConfig.ProphecyConfig.run_card_decorators
run_card_decorators
Finalise registered decorators.
Definition
ProphecyConfig.py:60
python.ProphecyConfig.ProphecyConfig.run_directory
run_directory
Definition
ProphecyConfig.py:249
python.ProphecyConfig.ProphecyConfig.fix_parameter
fix_parameter(self, parameter, value=None, desc='')
Register non-configurable parameter.
Definition
ProphecyConfig.py:165
python.ProphecyConfig.ProphecyConfig.random_seed
random_seed
Definition
ProphecyConfig.py:43
python.ProphecyConfig.ProphecyConfig.configurable_parameters
configurable_parameters(self)
Get dictionary of configurable parameters.
Definition
ProphecyConfig.py:207
python.ProphecyConfig.ProphecyConfig.fixed_parameters
fixed_parameters
Write out final runcard.
Definition
ProphecyConfig.py:75
python.ProphecyConfig.ProphecyConfig._prophecy_executable
str _prophecy_executable
This must be defined by each derived class - don't change it in the jobOptions!
Definition
ProphecyConfig.py:20
python.ProphecyConfig.ProphecyConfig.__enable_reweighting
bool __enable_reweighting
Definition
ProphecyConfig.py:80
python.ProphecyConfig.ProphecyConfig.__run_directory
__run_directory
Definition
ProphecyConfig.py:13
python.ProphecyConfig.ProphecyConfig.emit_heartbeat
emit_heartbeat(self, duration)
Output a heartbeat message.
Definition
ProphecyConfig.py:159
python.ProphecyConfig.ProphecyConfig.__output_events_file_name
str __output_events_file_name
This needs to be set so that Generate_trf finds an appropriate file format for showering.
Definition
ProphecyConfig.py:25
python.ProphecyConfig.ProphecyConfig.run_card_path
run_card_path
Write out final runcard.
Definition
ProphecyConfig.py:73
python.ProphecyConfig.ProphecyConfig.runProphecy
runProphecy(configurator)
Definition
ProphecyConfig.py:177
python.ProphecyConfig.ProphecyConfig.running_process
list running_process
Remove any existing .lhe files to avoid repeated events.
Definition
ProphecyConfig.py:116
python.ProphecyConfig.ProphecyConfig.output_events_file_name
output_events_file_name
Move output to correctly named file.
Definition
ProphecyConfig.py:136
python.ProphecyConfig.ProphecyConfig.logger
logger(self)
Get handle to logger.
Definition
ProphecyConfig.py:219
python.ProphecyConfig.ProphecyConfig.__configurable_parameters
dict __configurable_parameters
Definition
ProphecyConfig.py:29
python.ProphecyConfig.ProphecyConfig.__logger
__logger
Setup athena-compatible logger.
Definition
ProphecyConfig.py:16
generate
Definition
generate.cxx:28
Generated on
for ATLAS Offline Software by
1.17.0