Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
powheg_base.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 from AthenaCommon import Logging
4 from .configurable import Configurable
5 from ..utility import check_svn_revision, FileParser
6 import math
7 import os
8 import glob
9 
10 
11 logger = Logging.logging.getLogger("PowhegControl")
12 
13 
15  """! Base class for PowhegBox processes.
16 
17  All process types inherit from this class.
18 
19  @author James Robinson <james.robinson@cern.ch>
20  @author Stefan Richter <stefan.richter@cern.ch>
21  """
22 
23  def hoppet_info(self):
24  '''
25  Returns a list of strings to be treated as info messages in the log
26  They otherwise throw an error with HOPPET v. 1.2.0
27  Cf. AGENE-2016
28  '''
29  return ["-----------------------------------------------------------",
30  "Welcome to HOPPET v. 1.2.0",
31  "Higher Order Perturbative Parton Evolution Toolkit",
32  "Written by Gavin P. Salam (2001-2012)",
33  "with contributions from Juan Rojo",
34  "Frederic Dreyer and Alexander Karlberg",
35  "It is made available under the GNU public license,",
36  "with the additional request that if you use it or any",
37  "derivative of it in scientific work then you should cite:",
38  "G.P. Salam & J. Rojo, CPC 180(2009)120 (arXiv:0804.3755).",
39  "You are also encouraged to cite the original references,",
40  "for LO, NLO and NNLO splitting functions, the QCD",
41  "1, 2 and 3 loop beta functions and the coupling and",
42  "PDF and coupling mass threshold matching functions."]
43 
44  def hoppet_warning(self):
45  '''
46  Returns a list of strings to be treated as warning messages in the log
47  They otherwise throw an error
48  '''
49  return ["WARNING in InitMTMNNLO: using parametrisation (less accuracte) for A2PShg"]
50 
51  def openloops_error(self):
52  '''
53  Returns a list of strings to be treated as error messages in the log
54  They otherwise do not throw an error
55  '''
56  return ["[POWHEG-BOX+OpenLoops] Process not found!"]
57 
58  def __init__(self, base_directory, version, executable_name, cores, powheg_executable="pwhg_main", is_reweightable=True, warning_output = [], info_output = [], error_output = [], **kwargs):
59  """! Constructor.
60 
61  @param base_directory path to PowhegBox code.
62  @param version PowhegBox version.
63  @param executable_name folder containing appropriate PowhegBox executable.
64  @param powheg_executable name of the powheg executable.
65  @param warning_output list of patterns which if found in the output will be treated as warning in the log.
66  @param error_output list of patterns which if found in the output will be treated as error in the log.
67  @param info_output list of patterns which if found in the output will be treated as info in the log.
68  """
69  super(PowhegBase, self).__init__()
70 
71 
72  self.executable = os.path.join(base_directory, version, executable_name, powheg_executable)
73 
74 
75  os.environ["PYTHONPATH"] = os.path.join(base_directory, version, executable_name, "python") + ":" + os.environ.get("PYTHONPATH", "")
76 
77 
78  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "amplitudes", "obj-gnu") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
79  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "QCDLoop-1.95", "ff", "obj-gnu") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
80  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "Virtuals", "obj-gnu") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
81  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "obj-gfortran") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
82  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "obj-gfortran", "proclib") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
83  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "obj-gnu") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
84  os.environ["LD_LIBRARY_PATH"] = os.path.join(base_directory, version, executable_name, "obj-gnu", "proclib") + ":" + os.environ.get("LD_LIBRARY_PATH", "")
85 
86 
87 
88  directories = ['obj-gfortran', 'OpenLoops2', 'obj-gnu']
89  logger.info("OpenLoopsPath (before) = {0}".format(os.getenv('OpenLoopsPath')))
90  for directory in directories:
91  if os.path.isdir(os.path.join(base_directory, version, executable_name, directory, 'proclib')):
92  OLPath = os.path.join(base_directory, version, executable_name, directory)
93  os.environ['OpenLoopsPath'] = OLPath
94  logger.info("OpenLoopsPath (after) = {0}".format(os.getenv('OpenLoopsPath')))
95  break
96 
97 
98  self.process_revision = check_svn_revision(os.path.dirname(self.executable))
99 
100 
102  one_character_version = self.powheg_version.replace('V', '')[0]
103  POWHEGVER = '{v}_r{rev}'.format(v=one_character_version, rev=self.process_revision)
104  logger.info('MetaData: POWHEGVER = {0}'.format(POWHEGVER))
105  os.environ["POWHEGVER"] = POWHEGVER # does not export to parent shell, but might be sufficient inside job?
106 
107 
108  self.powhegbox_revision = check_svn_revision(os.path.dirname(os.path.dirname(self.executable)))
109 
110 
111  self.cores = cores
112 
113 
114  self.algorithms = []
115 
116 
117  self.externals = {}
118 
119 
121 
122  # Universal keywords that are set from the run arguments
123  self.add_keyword("ebeam1", kwargs.get("beam_energy", None))
124  self.add_keyword("ebeam2", kwargs.get("beam_energy", None))
125  self.add_keyword("iseed", int(kwargs.get("random_seed", None)))
126  self.add_keyword("numevts", kwargs.get("nEvents", None))
127 
128  # Add parameter validation functions
129  self.validation_functions.append("validate_integration_parameters")
130 
131 
132  self.is_reweightable = is_reweightable
133 
134 
136 
137 
139 
140 
141  self.warning_output = warning_output
142  self.info_output = info_output
143  self.error_output = error_output
144 
145 
148 
149  def add_algorithm(self, alg_or_process):
150  """! Add an algorithm or external process to the sequence.
151 
152  @param process Algorithm or external process to add.
153  """
154  # Either add to the list of algorithms to schedule
155  if isinstance(alg_or_process, str):
156  self.algorithms.append(alg_or_process)
157  # ... or add as an external process
158  else:
159  self.externals[alg_or_process.name] = alg_or_process
160 
161  @property
162  def files_for_cleanup(self):
163  """! Wildcarded list of files created by this process that can be deleted."""
164  raise AttributeError("Names of unneeded files are not known for this process!")
165 
166  @property
168  """! Wildcarded list of integration files that might be created by this process."""
169  raise AttributeError("Integration file names are not known for this process!")
170 
171  @property
173  """! Wildcarded list of integration files that are needed for this process."""
174  raise AttributeError("Integration file names are not known for this process!")
175 
176  @property
177  def powheg_version(self):
178  """! Version of PowhegBox process."""
179  raise AttributeError("Powheg version is not known!")
180 
181  @property
182  def default_PDFs(self):
183  """! Default PDFs for this process."""
184  raise AttributeError("Default PDFs are not known for this process!")
185 
186  @property
187  def default_scales(self):
188  """! Default scale variations for this process."""
189  raise AttributeError("Default scales are not known for this process!")
190 
191  def prepare_to_parallelise(self, n_cores):
192  """! Scale calculation parameters by n_cores."""
193  __nEvents_unscaled = self.parameters_by_keyword("numevts")[0].value
194  for keyword in ["ncall1", "ncall1rm", "ncall2", "ncall2rm", "nubound", "numevts"]:
195  for parameter in self.parameters_by_keyword(keyword):
196  if int(parameter.value) > 0:
197  parameter.value = int(math.ceil(float(parameter.value) / n_cores))
198  __nEvents_scaled = self.parameters_by_keyword("numevts")[0].value
199  logger.info("Scaling number of events per job from {} down to {}".format(__nEvents_unscaled, __nEvents_scaled))
200  # Freeze parallelstage parameters before printing list for user
201  [parameter.freeze() for parameter in self.parameters_by_name("parallelstage")]
202 
203  def stage_is_completed(self, stage):
204  """! Set whether the specified POWHEG-BOX generation stage is complete."""
205  # Perform manual check to allow re-use of grids in multicore mode
206  return False
207 
209  """! Validate any parameters which need it before preparing runcard."""
210  for function_name in self.validation_functions:
211  getattr(self, function_name)()
212 
214  """! Validate integration keywords by forcing to integer values."""
215  self.expose() # convenience call to simplify syntax
216  for name in ("foldcsi", "foldphi", "foldy", "itmx1", "itmx2", "ncall1", "ncall2", "nEvents"):
217  for parameter in self.parameters_by_name(name):
218  try:
219  parameter.value = int(parameter.value)
220  except TypeError:
221  logger.fatal("Failed to validate {} with value {}".format(name, parameter.value))
222  raise
223 
224  def check_decay_mode(self, decay_mode, allowed_decay_modes=None):
225  """! Check whether a decay mode is allowed an raise an exception if it is not."""
226  if allowed_decay_modes is None:
227  allowed_decay_modes = self.allowed_decay_modes
228  if decay_mode not in allowed_decay_modes:
229  logger.warning("Decay mode {} not recognised!".format(decay_mode))
230  logger.info("Allowed decay modes are:")
231  for allowed_decay_mode in allowed_decay_modes:
232  logger.info("... {}".format(allowed_decay_mode))
233  raise ValueError("Decay mode {} not recognised!".format(decay_mode))
234 
236 
237  search_strings = [] # list of filename patters to be searched for
238  found_files = [] # list of found files will be printed out for info
239  missing_patterns = [] # list of filename patters which hasn't been found but would be needed for pre-made integration grids
240  try:
241  search_strings = self.mandatory_integration_file_names
242  except AttributeError:
243  logger.fatal("No integration grid file name patterns defined for this process.")
244  raise
245  for s in search_strings:
246  found = glob.glob(s)
247  if found != []:
248  found_files += found
249  else:
250  missing_patterns.append(s)
251  if missing_patterns == []:
252  logger.info("Integration grid files found locally. Event generation shall continue, skipping the integration step.")
253  logger.info("Integration grid files found locally: {}".format(found_files))
254  else:
255  logger.info("Integration grid files needed were not found locally. Event generation shall continue, starting by the integration step.")
256  logger.info("Missing integration grid files with these patterns: {}".format(missing_patterns))
257  logger.info("Integration grid files found locally (if any): {}".format(found_files))
258 
259 
260  def modify_parameter(self, stage = 0):
261 
262  #skip modifying if dict is empty
263  if not bool(self.parameterStageDict):
264  return
265 
266  logger.info("Modifying parameters for the stages : {0}".format(self.parameterStageDict))
267 
268 
269  for key in self.parameterStageDict:
270  settingsList = self.parameterStageDict[key]
271  if abs(settingsList[2] - stage) <=1e-09:
272  self.set_parameter_in_config(key,settingsList[1])
273  else:
274  self.set_parameter_in_config(key,settingsList[0])
275 
276  def set_parameter_in_config(self, key, value):
277  FileParser("powheg.input").text_replace(key+".*", key+" {}".format(value))
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
python.processes.configurable.Configurable.expose
def expose(self)
Add all names to the interface of this object.
Definition: configurable.py:46
python.processes.powheg_base.PowhegBase.hoppet_info
def hoppet_info(self)
Definition: powheg_base.py:23
python.processes.powheg_base.PowhegBase.validate_parameters
def validate_parameters(self)
Validate any parameters which need it before preparing runcard.
Definition: powheg_base.py:208
python.utility.revision_checking.check_svn_revision
def check_svn_revision(path)
Definition: revision_checking.py:6
python.processes.powheg_base.PowhegBase.warning_output
warning_output
Special treatment for some log messages.
Definition: powheg_base.py:141
python.processes.powheg_base.PowhegBase.add_algorithm
def add_algorithm(self, alg_or_process)
Add an algorithm or external process to the sequence.
Definition: powheg_base.py:149
python.processes.powheg_base.PowhegBase.hoppet_warning
def hoppet_warning(self)
Definition: powheg_base.py:44
python.processes.powheg_base.PowhegBase
Base class for PowhegBox processes.
Definition: powheg_base.py:14
vtune_athena.format
format
Definition: vtune_athena.py:14
python.processes.configurable.Configurable.parameters_by_name
def parameters_by_name(self, name)
Retrieve all parameters that use a given name.
Definition: configurable.py:64
python.processes.powheg_base.PowhegBase.error_output
error_output
Definition: powheg_base.py:143
python.processes.powheg_base.PowhegBase.default_scales
def default_scales(self)
Default scale variations for this process.
Definition: powheg_base.py:187
python.processes.powheg_base.PowhegBase.check_using_integration_files
def check_using_integration_files(self)
Definition: powheg_base.py:235
python.processes.configurable.Configurable.add_keyword
def add_keyword(self, keyword, value=None, name=None, frozen=None, hidden=None, description=None, **kwargs)
Register configurable parameter that is exposed to the user.
Definition: configurable.py:21
python.processes.powheg_base.PowhegBase.integration_file_names
def integration_file_names(self)
Wildcarded list of integration files that might be created by this process.
Definition: powheg_base.py:167
python.processes.powheg_base.PowhegBase.parameterStageDict
parameterStageDict
Dictionary used to change parameters of the Powheg input.
Definition: powheg_base.py:147
python.processes.powheg_base.PowhegBase.use_XML_reweighting
use_XML_reweighting
Switch to determine whether XML reweighting should be used.
Definition: powheg_base.py:135
python.processes.powheg_base.PowhegBase.prepare_to_parallelise
def prepare_to_parallelise(self, n_cores)
Scale calculation parameters by n_cores.
Definition: powheg_base.py:191
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.processes.powheg_base.PowhegBase.openloops_error
def openloops_error(self)
Definition: powheg_base.py:51
python.processes.powheg_base.PowhegBase.set_parameter_in_config
def set_parameter_in_config(self, key, value)
Definition: powheg_base.py:276
python.processes.configurable.Configurable.parameters_by_keyword
def parameters_by_keyword(self, keyword)
Retrieve all parameters that use a given keyword.
Definition: configurable.py:57
python.processes.powheg_base.PowhegBase.powheg_version
def powheg_version(self)
Version of PowhegBox process.
Definition: powheg_base.py:177
python.processes.powheg_base.PowhegBase.process_revision
process_revision
Add to Python path "python" directory on POWHEG process directory.
Definition: powheg_base.py:98
python.processes.powheg_base.PowhegBase.__init__
def __init__(self, base_directory, version, executable_name, cores, powheg_executable="pwhg_main", is_reweightable=True, warning_output=[], info_output=[], error_output=[], **kwargs)
Constructor.
Definition: powheg_base.py:58
python.processes.powheg_base.PowhegBase.mandatory_integration_file_names
def mandatory_integration_file_names(self)
Wildcarded list of integration files that are needed for this process.
Definition: powheg_base.py:172
python.processes.powheg_base.PowhegBase.validation_functions
validation_functions
List of validation functions to run before preparing runcard.
Definition: powheg_base.py:120
python.processes.powheg_base.PowhegBase.check_decay_mode
def check_decay_mode(self, decay_mode, allowed_decay_modes=None)
Check whether a decay mode is allowed an raise an exception if it is not.
Definition: powheg_base.py:224
python.processes.powheg_base.PowhegBase.powhegbox_revision
powhegbox_revision
Log the PowhegBox version and process-code revision for AMI etc.
Definition: powheg_base.py:108
python.processes.powheg_base.PowhegBase.validate_integration_parameters
def validate_integration_parameters(self)
Validate integration keywords by forcing to integer values.
Definition: powheg_base.py:213
python.processes.powheg_base.PowhegBase.remove_oldStyle_rwt_comments
remove_oldStyle_rwt_comments
Switch to determine if the #rwgt and #pdf comments should be kept in lhe files despite using xml rewe...
Definition: powheg_base.py:138
python.processes.powheg_base.PowhegBase.info_output
info_output
Definition: powheg_base.py:142
python.processes.powheg_base.PowhegBase.algorithms
algorithms
List of additional algorithms to schedule.
Definition: powheg_base.py:114
python.processes.powheg_base.PowhegBase.is_reweightable
is_reweightable
Switch to determine whether reweighting is allowed.
Definition: powheg_base.py:132
python.processes.powheg_base.PowhegBase.externals
externals
List of external processes to schedule.
Definition: powheg_base.py:117
python.processes.powheg_base.PowhegBase.default_PDFs
def default_PDFs(self)
Default PDFs for this process.
Definition: powheg_base.py:182
python.processes.powheg_base.PowhegBase.cores
cores
Number of cores to use.
Definition: powheg_base.py:111
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.processes.powheg_base.PowhegBase.executable
executable
Powheg executable that will be used.
Definition: powheg_base.py:72
python.processes.powheg_base.PowhegBase.modify_parameter
def modify_parameter(self, stage=0)
Definition: powheg_base.py:260
python.processes.powheg_base.PowhegBase.files_for_cleanup
def files_for_cleanup(self)
Wildcarded list of files created by this process that can be deleted.
Definition: powheg_base.py:162
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60
python.processes.powheg_base.PowhegBase.stage_is_completed
def stage_is_completed(self, stage)
Set whether the specified POWHEG-BOX generation stage is complete.
Definition: powheg_base.py:203
python.processes.configurable.Configurable
Class for any process which can be configured.
Definition: configurable.py:10
python.LArMinBiasAlgConfig.float
float
Definition: LArMinBiasAlgConfig.py:65